W3-F v2: staged engine + decide_many context batching (bounded groups) - #35
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rebase of #34 onto c022fa5 with the review fixes.
F1 STRUCTURE:
run_parallel_generationsplit into explicit stages — no sentinel dict, noif _prebuilt is not Nonebranches:_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 pathsrun_parallel_generation= prior + prefill + score + assembleF2 MEMORY:
run_parallel_generation_batchedbounds 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_passlands in every result's telemetry.Batching: one
_score_rowscall per group overgroup_size × Rrows with per-row cache slots (cache_slotsparam — each chunk merges exactly its own slots;BatchKVCache.mergeleft-pads different prompt lengths). Caches passed UNMERGED (re-merging a BatchKVCache fails: offset is an array).decide_many→ batched path via_assemble_decision(shared withdecide).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.