Repository navigation
Conversation
…mitted window The one-stream engine never set State.limit (only the concurrent decoder does), so its attention caches grew to max(need, 2x, 1024) rows with no cap. Once a buffer holds more than half the window, its next growth passes the window: a fresh 31,015-token prompt at --context 32768 leaves a 62,024-row buffer per attention layer, where the startup estimate prices each at 32,768. A 16,000-token prompt whose reply passes 32,000 rows reaches 64,000. - prefill and prefill_tp take limit= and set it on a fresh State; a resumed state keeps its own (clone_state copies it). - Qwen27Engine passes its context_window on one GPU and on both ranks. Growth still gives a commit every row it needs, and nothing commits past context_window - 1, so the cap never shortens a request. Attention addresses keys by absolute position and growth copies every committed row, so outputs keep their bits. Tests: tests/test_qwen27_kv_limit.py runs the real chunking, _grow, prefill, prefill_tp and commit on CPU tensors, and the engine's three call sites (all 16 fail without the change). A CUDA test checks that a limited prefill and reply equal an unlimited one.
This was referenced Sep 28, 2026
Owner
|
Thank you, @nood-co1, this is a sharp catch. The one-stream 27B engine sized its admission for the window We rebased it onto next-0.3.6.2 unchanged. On a DGX Spark, a 31,105-token prompt at a 32,768-token window |
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.
Summary
The one-stream Qwen3.8-27B CUDA engine (one GPU or two ranks, with
--parallelat its CUDA default of one stream) never setsState.limit, so its attention caches grow tomax(need, 2 × rows, 1024)with no upper bound. Once a buffer holds more than half the window, its next growth passes the window. At--context 32768, a fresh 31,015-token prompt leaves 62,024 rows per attention layer, where the startup admission prices each buffer at 32,768.prefillandprefill_tpnow takelimit=and set it on a freshState.Qwen27Enginepasses itscontext_windowat all three call sites. With the limit, every buffer stays at or below the admission estimate's row count.Why
Q/issrc/tensorfold/families/qwen3_5/cuda/. References are atmain(34bae79).State.limitdefaults to 0, meaning no bound (Q/forward.py:94). Growth in_grow(Q/prefill.py:39-49) and incommit(Q/forward.py:375-388) caps a buffer only when it is set. Only the concurrent decoder sets it (Q/multi.py:107).next_power_of_two(max(1024, window))rows (src/tensorfold/cuda/geometry.py:163-165).Q/prefill.py:120-124). For 31,015 tokens the buffer grows 3,876 → 7,753 → 15,506 → 31,012 → 62,024 rows. The last chunk needs three rows more than 31,012, and the buffer doubles.--context 32768, 3,072 of the 32,767 fresh prompt lengths leave a buffer above 32,768 rows, all between 28,673 and 32,767 tokens. The largest is 65,528 rows. Replies and resumes grow the same way: a 16,000-token prompt whose reply passes 32,000 rows reaches 64,000.config.json: 64 layers, a full-attention layer every 4). That prompt's buffer set is 3.8 GiB where admission assumes 2.0 GiB. The engine keeps two prompt ends (Q/engine.py:119-120), so two or three distinct long prompts in a row hold several such sets.Change
Q/decode.pyprefillandQ/decode_tp.pyprefill_tptake a keywordlimit: int = 0. A fresh state getsst.limit = limit. A resumed state keeps its own limit (clone_statecopies it,Q/decode.py:22). Other callers see no change.Q/engine.pypasseslimit=self.context_windowon one GPU (generate, with or without drafts), on rank 0 (_generate_tp) and on rank 1 (follow). Cached prompt ends come from these prefills, so resumed states carry the limit too.context_window, notcache_slots.cache_slotsiswindow + 12(src/tensorfold/cuda/capacity.py:56), which is 12 rows above the estimate's power of two when the window is itself a power of two.geometry.py:243-260).One keyword on two functions and three call sites. No kernel, admission or cache-policy change.
Exactness
The change alters buffer capacity only, never a key or value row or the order of any sum.
Q/prefill.py:46-47,Q/forward.py:386-387).src/tensorfold/cuda/kernels/attention.py:223-236) and splits keys into 512-key chunks by committed count and absolute position (:171-190). Capacity is not an input.p0 + Wrows and reads keys up to each row's position (src/tensorfold/cuda/kernels/prefill_attention.py:41-54,:64). Capacity is not an input.max(need, …), so a commit always gets every row it needs. The engine refuses prompts ofcontext_windowtokens or more and caps a reply atcontext_window - len(prompt)tokens (Q/engine.py:128-131). The last sampled token is not committed, so no state passescontext_window - 1rows.context_windowrows. No commit needs more, so the limit adds no reallocation.--parallel. The capped growth path is the one the concurrent decoder already runs (Q/multi.py:107).This is argued from the code. The GPU test below checks it.
Tests
Host: macOS, Python 3.13.5, PyTorch 2.11.0 CPU.
tests/test_qwen27_kv_limit.py(new, 16 tests) uses theallocationsfixture oftests/test_cuda_geometry.pyfor its Triton stand-in.prefill_chunkis replaced by its cache writes (_grow, then the chunk's rows), since the kernels need a GPU. The chunking,_grow,State,prefill,prefill_tpandcommitare the real ones.prefilland both ranks ofprefill_tp. Without a limit they reach 59,624, 62,024 and 62,992 rows. Withlimit=32768they stay at 32,768, and every committed row is intact.commitin 12-row windows up to 32,767 rows: 64,000 rows unlimited, 32,768 limited, every row intact.Qwen27Enginepasseslimit=context_windowon one GPU (with and without drafts), on rank 0 and on rank 1 (follow).main's sourcesTypeErroron the newlimitkeyword; in the engine test the three call sites pass 0testswithouttests/cuda),main(34bae79)main(34bae79)The whole-suite runs had an import-only Triton stand-in. The same 151 ids fail or error on both, for host reasons: no
transformerstokenizer, no Triton backends, MLX 0.31.1 where GLM-5.3-Flash needs 0.32.2, and two tests that readtools/andCHANGELOG.md, which the test tree used here did not include.ruff checkon the three source files reports the same two findings as onmain(an E731 and an F841 inengine.py).tests/cuda/test_qwen27_prefill.py::test_a_cache_limit_changes_no_bits(new, GPU) uses the file's toy model in two cases: 9,000 rows with limit 9,100 (the third chunk grows to 9,100 rows, not 12,000), and 1,000 rows with limit 1,100 (the reply grows the buffer to 1,100 rows, not 2,048). It checks that:prefill_stateequals an unlimited one (state and last-rownormed);prefill(..., limit=)gives the same first token;GPU status
One DGX Spark (GB10, sm_121); container: Python 3.12.3, PyTorch 2.13.0a0 (NVIDIA 26.07), CUDA 13.3, Triton 3.7.1.
TORCH_CUDA_ARCH_LIST=12.1was set: without it the 0.3.5 extensions do not build in this container (the failure #56 reports, fixed onmainin 0.3.6.1). Branch head 8c4c98c.tests/cuda/test_qwen27_prefill.pytest_a_cache_limit_changes_no_bits(2 cases)tests/cudamain(beddbb7): 288 passed, the same 7 skippedStill pending:
--context 32768,mainagainst this branch, a fresh 31,015-token prompt: the reply'stoken_sha(equal expected), andtorch.cuda.memory_allocatedafter the prefill.Evidence
Surface: the engine's growth rule run on CPU tensors. No GPU, no model.
chunksand_grow, run over every fresh prompt length below 32,768, give the figures above. A fresh 31,015-token prompt leaves a 62,024-row K/V buffer per attention layer where admission assumes 32,768: 3.8 GiB against 2.0 GiB at 65,536 B a row._grow,commit's growth, the two-entry prompt-end cache and_drop_extensions. It matches the real_growon single prompts. It ran 4,000 random sequences of one to four requests, each a fresh prompt or a continuation of the previous one, with prompt and reply lengths drawn within the window. Rows are counted as the estimate counts them: kept entries, the live state and one growth copy, the copy as a whole buffer set. For a fresh prompt the copy term is the estimate's accounting, not allocated memory, since a fresh state's old buffers are freed one layer at a time.--contextWith the limit the peak was at most 1.00× at every window tried: 8,192, 32,768, 65,536, 100,000, 131,072 and 262,144.
Overlaps
keep_at=on the same lines: 5 conflict blocks indecode.py,decode_tp.pyandengine.py, on theprefillandprefill_tpsignatures and their three call sites (keep_at=againstlimit=). Whichever lands second needs a rebase, which also gives each PR's host-test stand-ins (prefill,prefill_tp,prefill_chunk) the other's keyword; on a host merge with that done, both host files pass.ignore_eoschange in feat(cuda): stop strings on every CUDA engine, and ignore_eos on the 27B's CUDA engine #63 also editsengine.py. Git merges the two cleanly.Not in this change: the drafter's caches, which have their own bounded estimate (
draft_geometry(..., bounded=True)), and the other families (glm5_next,qwen4_exp,nemotron_h), which were not checked.Submitted by an agent from Blockbrain Labs (https://x.com/blockbrain_labs, https://github.com/blockbrain-ai).