[PR #5418] [Engine] fix layer-major prefill overflowing the group-scratch row cap #5417

Open
opened 2026-11-07 16:35:04 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/5418
Author: @WilliamStone
Created: 11/6/2026
Status: 🔄 Open

Base: main ← Head: fix/lm-group-cap-scratch


📝 Commits (1)

  • b621394 prefill: cap layer-major expert groups at the group-scratch row cap

📊 Changes

1 file changed (+4 additions, -2 deletions)

View changed files

📝 src/prefill/prefill.cpp (+4 -2)

📄 Description

This bug was first isolated in the Speedy fork (DevDock-AI/Speedy); the fix was submitted there and accepted as DevDock-AI/Speedy PR #4. This PR upstreams the same change. Both G = m.T * K sites are unchanged on origin/main and tag v0.2.11.

Problem

Long prompts (~29K tokens and up) crash mid-prefill on the layer-major path with STRATA_PF_GROUP_SCRATCH=1: the engine stops and the server returns HTTP 503 (the engine stopped unexpectedly). Other prompt sizes may surface as an HTTP 400 instead, depending on where the overflow lands in memory.

Root cause

STRATA_PF_GROUP_SCRATCH allocates the per-group MoE scratch (GU/H/Hq) for group_cap_rows() rows — 15,872 rows with T=8192. The layer-major path still packed expert entries with G = m.T * K (81,920 here), so a routed group over the cap ran as one range. swiglu wrote rows past the end of m.H into m.Hq; quantize then read the overwritten Hq scales as inf, the down GEMM produced 0x7fffffff NaN rows, and moe_scatter_add carried them into acc.

Evidence from the fork's instrumented runs (2×GPU, CUDA0(Nvidia 3060): layers 0–23 / CUDA1(Nvidia 5070): layers 24–47; same code path):

group 10 (nr 16276): acc[8192..16384) 404 bad rows [4565..8159]
                     Dm 404/16276 bad rows [15872..16275]   # exactly 16276-15872 past the cap
Hq row 15872 d4: [4.3e-04 1.0e-03 inf inf] [inf inf 5e+33 1.9e+34] ...
post-swiglu scan of H itself: 0/16276 bad — the overflow, not the kernel, dirtied Hq

Fix

Use min(m.T * K, m.group_cap) for G in both lm_prepare() (routing-table sizing, so the tables match the entries) and lm_experts() (entry packing). Oversized groups now fall back to the existing sub-range splitting, which already asserts nra <= m.group_cap. With m.group_cap == 0 (scratch off or fused layout) the expression reduces to the old m.T * K, so the non-scratch path is unchanged.

Validation

All on a clean v0.2.10 build (CUDA 13.4, arch 86;120, Windows 11, RTX 3060 + RTX 5070 2×12 GB, layer split 24, qwen3.8-flash-next-iq3_s, --prefill auto → chunk 8192):

  • STRATA_PF_GROUP_SCRATCH=1, unpatched: a ~29.7K-token prompt → prefill mmq: gather_native: an illegal memory access was encountered on the first super-chunk.
  • STRATA_PF_GROUP_SCRATCH=0, unpatched: completes — 63,784 prompt tokens, 974 tok/s prefill.
  • STRATA_PF_GROUP_SCRATCH=1, patched: the same ~29.7K prompt completes (611 tok/s cold, ~1s warm via prompt cache); a 63,784-token prompt also completes (766 tok/s).

Limits / not run

  • CPU/HIP/SYCL/Metal backends were not built or exercised; the change is host-side packing logic in the shared prefill path.
  • Single-GPU layer-major not run. Static analysis only: layer-major defaults on only for CUDA layer splits, but STRATA_PREFILL_LAYER_MAJOR=1 forces it on one GPU, where the same G/cap mismatch applies — two cards are not required to hit this.
  • STRATA_PF_GROUP_SCRATCH=0 after the patch was verified by code inspection only (m.group_cap == 0 ⇒ G == m.T * K, identical to the previous expression) for the non-scratch path; a runtime scratch-off run on the unpatched build is listed above.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/5418 **Author:** [@WilliamStone](https://github.com/WilliamStone) **Created:** 11/6/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/lm-group-cap-scratch` --- ### 📝 Commits (1) - [`b621394`](https://github.com/Niko1221/Strata/commit/b62139404106ef7edf599cf7e5d094369f766757) prefill: cap layer-major expert groups at the group-scratch row cap ### 📊 Changes **1 file changed** (+4 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `src/prefill/prefill.cpp` (+4 -2) </details> ### 📄 Description This bug was first isolated in the Speedy fork (DevDock-AI/Speedy); the fix was submitted there and accepted as [DevDock-AI/Speedy PR #4](https://github.com/DevDock-AI/Speedy/pull/4). This PR upstreams the same change. Both `G = m.T * K` sites are unchanged on `origin/main` and tag `v0.2.11`. ## Problem Long prompts (~29K tokens and up) crash mid-prefill on the layer-major path with `STRATA_PF_GROUP_SCRATCH=1`: the engine stops and the server returns HTTP 503 (the engine stopped unexpectedly). Other prompt sizes may surface as an HTTP 400 instead, depending on where the overflow lands in memory. ## Root cause `STRATA_PF_GROUP_SCRATCH` allocates the per-group MoE scratch (GU/H/Hq) for `group_cap_rows()` rows — 15,872 rows with T=8192. The layer-major path still packed expert entries with `G = m.T * K` (81,920 here), so a routed group over the cap ran as one range. `swiglu` wrote rows past the end of `m.H` into `m.Hq`; quantize then read the overwritten `Hq` scales as inf, the down GEMM produced 0x7fffffff NaN rows, and `moe_scatter_add` carried them into `acc`. Evidence from the fork's instrumented runs (2×GPU, CUDA0(Nvidia 3060): layers 0–23 / CUDA1(Nvidia 5070): layers 24–47; same code path): ``` group 10 (nr 16276): acc[8192..16384) 404 bad rows [4565..8159] Dm 404/16276 bad rows [15872..16275] # exactly 16276-15872 past the cap Hq row 15872 d4: [4.3e-04 1.0e-03 inf inf] [inf inf 5e+33 1.9e+34] ... post-swiglu scan of H itself: 0/16276 bad — the overflow, not the kernel, dirtied Hq ``` ## Fix Use `min(m.T * K, m.group_cap)` for `G` in both `lm_prepare()` (routing-table sizing, so the tables match the entries) and `lm_experts()` (entry packing). Oversized groups now fall back to the existing sub-range splitting, which already asserts `nra <= m.group_cap`. With `m.group_cap == 0` (scratch off or fused layout) the expression reduces to the old `m.T * K`, so the non-scratch path is unchanged. ## Validation All on a clean v0.2.10 build (CUDA 13.4, arch `86;120`, Windows 11, RTX 3060 + RTX 5070 2×12 GB, layer split 24, `qwen3.8-flash-next-iq3_s`, `--prefill auto` → chunk 8192): - `STRATA_PF_GROUP_SCRATCH=1`, unpatched: a ~29.7K-token prompt → `prefill mmq: gather_native: an illegal memory access was encountered` on the first super-chunk. - `STRATA_PF_GROUP_SCRATCH=0`, unpatched: completes — 63,784 prompt tokens, 974 tok/s prefill. - `STRATA_PF_GROUP_SCRATCH=1`, patched: the same ~29.7K prompt completes (611 tok/s cold, ~1s warm via prompt cache); a 63,784-token prompt also completes (766 tok/s). ## Limits / not run - CPU/HIP/SYCL/Metal backends were not built or exercised; the change is host-side packing logic in the shared prefill path. - Single-GPU layer-major not run. Static analysis only: layer-major defaults on only for CUDA layer splits, but `STRATA_PREFILL_LAYER_MAJOR=1` forces it on one GPU, where the same `G`/cap mismatch applies — two cards are not required to hit this. - `STRATA_PF_GROUP_SCRATCH=0` after the patch was verified by code inspection only (`m.group_cap == 0` ⇒ `G == m.T * K`, identical to the previous expression) for the non-scratch path; a runtime scratch-off run on the unpatched build is listed above. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#5417
No description provided.