Skip to content

W3-F v2: staged engine + decide_many context batching (bounded groups) - #35

Merged
bnsd55 merged 1 commit into
mainfrom
w3f-batch-v2
Sep 18, 2026
Merged

bnsd55 merged 1 commit into
mainfrom
w3f-batch-v2

Conversation

@bnsd55

@bnsd55 bnsd55 commented Sep 18, 2026

Copy link
Copy Markdown
Owner

Rebase of #34 onto c022fa5 with the review fixes.

F1 STRUCTURE: run_parallel_generation split into explicit stages — no sentinel dict, no if _prebuilt is not None branches:

  • _build_schema_rows(schema, tokenizer, scoring) — row build, context-independent
  • _prefill(model, tokenizer, context, schema)PrefillResult(base_ids, cache, t_prefill_ms)
  • _score = _score_rows (the ONE padded/broadcast/gather copy)
  • _assemble(schema, built, scored, ...) — row dispatch + trie scoring + W3-E rescore + W3-D MAP + second pass + telemetry; shared verbatim by both paths
  • run_parallel_generation = prior + prefill + score + assemble

F2 MEMORY: run_parallel_generation_batched bounds contexts per pass from the measured budget (_contexts_per_pass = _memory_budget_bytes / per-context cache nbytes, ≥1) and loops over context groups — 500 contexts never hold 500 caches. contexts_per_pass lands in every result's telemetry.

Batching: one _score_rows call per group over group_size × R rows with per-row cache slots (cache_slots param — each chunk merges exactly its own slots; BatchKVCache.merge left-pads different prompt lengths). Caches passed UNMERGED (re-merging a BatchKVCache fails: offset is an array). decide_many → batched path via _assemble_decision (shared with decide).

Parity: batched parsed values + per-field probabilities identical to separate calls (<1e-9) on the deterministic fake — identical rows, identical per-context cache state, only batch width differs.

Tests (8 in test_w3f_batch.py): parity, input order, shared pass telemetry, empty contexts, constraints flow, decide_many e2e, contexts_per_pass bounding, single-context schema.

547 passed, ruff clean.

F1 STRUCTURE: run_parallel_generation split into explicit stages —
_build_schema_rows(schema, tokenizer, scoring) -> row build (context-
independent), _prefill(model, tokenizer, context, schema) -> PrefillResult
(base_ids, cache, t_prefill_ms), _score = _score_rows (the ONE copy), and
new _assemble(schema, built, scored, ...) -> result dict (row dispatch +
trie scoring + W3-E rescore + W3-D MAP + second pass + telemetry). No
sentinel dict, no .get(..., 0.0) defaults, no 'if _prebuilt is not None'
branches: run_parallel_generation = prior + prefill + score + assemble.

F2 MEMORY: run_parallel_generation_batched bounds contexts per pass from
the measured budget (_contexts_per_pass = _memory_budget_bytes(
_CHUNK_TARGET_FRACTION) // per-context cache nbytes, at least 1) and loops
over context groups — 500 contexts never hold 500 caches. Each group gets
its own merged scoring pass. contexts_per_pass is in every result's
telemetry.

The batched path calls the SAME _prefill/_build_schema_rows/_score_rows/
_assemble stages as the per-context path; the only difference is one
_score_rows call per group with per-row cache slots (cache_slots param on
_score_rows: each chunk merges exactly its own slots; None = the original
single-cache broadcast). Caches are passed UNMERGED — re-merging a
BatchKVCache fails (offset is an array, not an int).

W3-F tests (8): parity (batched == separate, <1e-9), input order, shared
pass telemetry, empty contexts, constraints flow, decide_many end-to-end,
contexts_per_pass bounding, single-context schema.
@bnsd55
bnsd55 merged commit 957cc50 into main Sep 18, 2026
2 checks passed
@bnsd55
bnsd55 deleted the w3f-batch-v2 branch September 18, 2026 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant