Skip to content

[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
simplexai-labs:mainfrom
mihaidobrescu1111:gemma4-shift-rin
Draft

mihaidobrescu1111 wants to merge 1 commit into
simplexai-labs:mainfrom
mihaidobrescu1111:gemma4-shift-rin

Conversation

@mihaidobrescu1111

Copy link
Copy Markdown

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 as
prompt N tokens = N reused + 0 read. The model then sees a garbled prompt: in our tool-call test it ignores the
system prompt and the tools and answers in plain text, for example:

  • "Error: 403 Forbidden … unauthorized attempt to access core_logic.sys…"
  • "I'm sorry, I don't understand that. Could you please rephrase your request?"
  • "The provided text is a highly structured representation of programming logic…"
  • "Sistema inicializado. He procesado todos los parámetros lógicos…" (the prompt is English)

Cause: Engine::layer_pack() (engine/src/poly/engine.cpp:7906) runs each call of a pack through layer_head() at
its own rows of the per-row buffers, by shift_rows(by). Gemma4::shift_rows() (engine/src/poly/gemma4.cpp:865)
moves mlp_ but not rin_, the router's input that Gemma4::layer_head() writes (gemma4.cpp:823). So every call
writes its rin_ rows at row 0, each over the one before. After the loop, moe_rows() (engine.cpp:6322) calls
router_scores(L, p.m), which for Gemma reads rin_ (gemma4.cpp:831) for all the pack's rows: row 0 on holds the
last 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 by Engine::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 and
    by is 0.
  • The base Engine::router_scores() reads xn_/xq_, which Engine::shift_rows() moves. Only Gemma 4 routes from a
    buffer of its own (rin_).
  • Decode is not affected: Gemma 4 has batch_heads(), so step() runs one layer_head() for all rows, unshifted.
    Packs under kStageRows (32) rows are not affected either: layer_head() then runs moe_router() per call, at the
    same (unshifted) rows it just wrote.

After the fix, the same test gives 60 of 60 correct tool calls, with 20 coalesced slots among them.

Build requests coalesced slots (N reused + 0 read) correct tool calls failed (plain text, no tool call)
36e654b (main), 3 runs 42 10 in the 30-request run 35 7 (that run: 5 of its 10 coalesced requests; 0 of its 20 cold reads)
36e654b + this PR, 1 run 60 20 60 0
an older build without coalesced reads (3e16421, poly.10), 4 runs 48 0 48 0

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_; beside mlp_. rin_ is a per-row buffer of rows_ rows like
    xn_ (assign_scratch()), so the shifted rows stay inside it whenever xn_'s do.
  • Checked the other buffers Gemma4::layer_head() writes: xn_, xq_, r_, fo_, rl_ are moved by
    Engine::shift_rows(); the attention scratch (qraw_, q_, k_, v_, att_, ao_, score_) and cur_ are
    used only inside the call's own head. mlp_ is already moved. Per-layer token embeddings are refused at load, so
    there is no other Gemma buffer read for the whole pack.
  • No change for other models, for decode, or for reads without co-reads (by is 0 there).

Validation

Passed:

  • Build: 36e654b + this patch, Linux x86-64, CUDA 13.2, 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=ON and cmake --build build-poly --target strata-poly, rc=0.
  • python -m unittest serve.test_gemma in terminal/: 26 tests OK.
  • Hardware run: RTX 4060 Laptop 8 GB (driver 595.99.02), 32 GB RAM, Linux, CPU capped at 3.5 GHz.
    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.
    • Test: 6 chat requests with a ~500-token system prompt and one tool (each needs a lookup), thinking on,
      non-streamed, sent 3 at a time; 10 rounds on a freshly started server. Pass = finish_reason tool_calls with
      valid JSON arguments. The same prompts and settings as the runs on main in the table above.
    • Result: 60 of 60 pass, 60 of 60 with reasoning, 20 slots logged as N reused + 0 read (2 a round, as on main).
    • Decode speed unchanged (the changed line is not on the decode path): one 250-token reply, thinking off,
      median of 3 = 100.1 tok/s, against 103.2 and 99.5 in two runs on main.

Failed:
none observed.

Not run:

  • HIP, SYCL and Metal builds. The change is in the backend-independent Gemma 4 code; I have only a CUDA card.
    This PR stays a draft until those builds are checked. Help wanted: could someone with an AMD (HIP) or Intel
    (SYCL) setup build strata-poly with this change and confirm it compiles? A Gemma 4 run on that hardware would be
    a bonus.
  • Other Gemma 4 sizes and quantizations, and more than 3 slots.
  • A temperature-0 token comparison of a co-read prompt against the same prompt read alone. There is no co-read test
    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 BCO co-read, and compare the first 32 greedy tokens
    of both slots with a solo read. Before this fix the co-read slot differs; after it, all three should match.
  • The engine's unit tests (built with -DSTRATA_BUILD_TESTS=OFF).

Limits

  • One hardware run after the fix (60 requests). The failure rate before was about 1 in 6 requests with this load, so
    60 passes is strong evidence but not a proof for every pack shape.
  • Workaround for builds without the fix: STRATA_COALESCE_PREFILL=0, which turns off coalesced reads.

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.
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