Repository navigation
[Engine] Gemma 4: a coalesced prompt read routes every co-read's tokens from the wrong rows (shift rin_ with the call) - #325
Draft
mihaidobrescu1111 wants to merge 1 commit into
Conversation
Gemma4::shift_rows() moved mlp_ but not rin_, so router_scores() for a co-read pack routed every call's tokens from another call's rows.
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.
Problem and result
With Gemma 4 on strata-poly and several batch slots, prompts that the server reads together (STRATA_COALESCE_PREFILL,
on by default when the engine says
co_read=1) are routed to the wrong experts. The engine log shows such slots asprompt N tokens = N reused + 0 read. The model then sees a garbled prompt: in our tool-call test it ignores thesystem prompt and the tools and answers in plain text, for example:
403 Forbidden… unauthorized attempt to accesscore_logic.sys…"Cause:
Engine::layer_pack()(engine/src/poly/engine.cpp:7906) runs each call of a pack throughlayer_head()atits own rows of the per-row buffers, by
shift_rows(by).Gemma4::shift_rows()(engine/src/poly/gemma4.cpp:865)moves
mlp_but notrin_, the router's input thatGemma4::layer_head()writes (gemma4.cpp:823). So every callwrites its
rin_rows at row 0, each over the one before. After the loop,moe_rows()(engine.cpp:6322) callsrouter_scores(L, p.m), which for Gemma readsrin_(gemma4.cpp:831) for all the pack's rows: row 0 on holds thelast call's rows, and the rest is what earlier calls left there. So the routed experts of every call in the pack (the co-reads and the lead's own tokens) are picked
from another call's tokens, while the experts' input (
xn_, shifted byEngine::shift_rows()) is the right one.The KV of those prompts is then wrong for every layer after the first MoE layer.
Why only Gemma 4 and only coalesced reads:
read()makes packs of more than one call only with co-reads (n_co > 0); without them a pack is one call andbyis 0.Engine::router_scores()readsxn_/xq_, whichEngine::shift_rows()moves. Only Gemma 4 routes from abuffer of its own (
rin_).batch_heads(), sostep()runs onelayer_head()for all rows, unshifted.Packs under
kStageRows(32) rows are not affected either:layer_head()then runsmoe_router()per call, at thesame (unshifted) rows it just wrote.
After the fix, the same test gives 60 of 60 correct tool calls, with 20 coalesced slots among them.
N reused + 0 read)Fisher exact, two-sided: failures 7/42 before vs 0/60 after, p = 0.0015; coalesced requests only, 5/10 before vs 0/20
after, p = 0.0018. After vs the build without coalesced reads: 0/60 vs 0/48.
Related discussion
none
Approach
Gemma4::shift_rows():rin_ += by * n_embd_;besidemlp_.rin_is a per-row buffer ofrows_rows likexn_(assign_scratch()), so the shifted rows stay inside it wheneverxn_'s do.Gemma4::layer_head()writes:xn_,xq_,r_,fo_,rl_are moved byEngine::shift_rows(); the attention scratch (qraw_,q_,k_,v_,att_,ao_,score_) andcur_areused only inside the call's own head.
mlp_is already moved. Per-layer token embeddings are refused at load, sothere is no other Gemma buffer read for the whole pack.
byis 0 there).Validation
Passed:
cmake -S engine -B build-poly -G Ninja -DCMAKE_BUILD_TYPE=Release -DSTRATA_ENABLE_CUDA=ON -DSTRATA_BUILD_TESTS=OFF -DCMAKE_CUDA_ARCHITECTURES=89 -DSTRATA_NATIVE_EXPERTS=ONandcmake --build build-poly --target strata-poly, rc=0.python -m unittest serve.test_gemmain terminal/: 26 tests OK.Model gemma-4-26B-A4B-it-qat-UD-Q4_K_XL + the MTP drafter (Q8_0) as
--draft-model. Server:--engine strata, 3 batch slots, engine flags--max-context 65536 --kv int8 --tail-skip 0 --expert-profile <file> --conversation-cache-mib 4096 --conversation-cache-slots 8 --vram-reserve-mib 512, STRATA_COALESCE_PREFILL unset(the default). Sampling temperature 1.0, top_p 0.95, top_k 64.
non-streamed, sent 3 at a time; 10 rounds on a freshly started server. Pass =
finish_reasontool_callswithvalid JSON arguments. The same prompts and settings as the runs on main in the table above.
N reused + 0 read(2 a round, as on main).median of 3 = 100.1 tok/s, against 103.2 and 99.5 in two runs on main.
Failed:
none observed.
Not run:
This PR stays a draft until those builds are checked. Help wanted: could someone with an AMD (HIP) or Intel
(SYCL) setup build
strata-polywith this change and confirm it compiles? A Gemma 4 run on that hardware would bea bonus.
in the tree, and one needs a Gemma 4 model on a GPU. A natural regression check: start strata-poly with 2 batch
slots, send the same prompt (> 32 tokens) as a lead and as a
BCOco-read, and compare the first 32 greedy tokensof both slots with a solo read. Before this fix the co-read slot differs; after it, all three should match.
-DSTRATA_BUILD_TESTS=OFF).Limits
60 passes is strong evidence but not a proof for every pack shape.
STRATA_COALESCE_PREFILL=0, which turns off coalesced reads.