[PR #5108] [CLOSED] [Engine] prefill: room after the MMQ row table for ggml's stream-k fixup read (Xid 31 on long prompts) #5310

Closed
opened 2026-11-07 16:34:15 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/5108
Author: @quantforge-br
Created: 11/3/2026
Status: ❌ Closed

Base: main ← Head: fix/mmq-ids-overread


📝 Commits (1)

  • 8d5c059 prefill: room after the MMQ row table for ggml's stream-k fixup read (Xid 31, "prefill copy_i32: an illegal memory access" on long prompts)

📊 Changes

1 file changed (+14 additions, -5 deletions)

View changed files

📝 src/prefill/prefill.cpp (+14 -5)

📄 Description

Problem and result

On a two-GPU CUDA layer split, a long prompt can end the engine with prefill copy_i32: an illegal memory access was encountered. The kernel logs Xid 31 (FAULT_PDE, ACCESS_TYPE_VIRT_READ) on the second card. copy_i32 is not the kernel that faults: it is the first check() after the fault.

The cause is a read past the end of the MMQ row table ids_identity. ggml's mul_mat_q_stream_k_fixup (ggml/src/ggml-cuda/mmq.cuh:1353 at the pinned 3cf03257) loads ids_dst[col_low + jt*J + j] for a whole J-column tile before it compares the tile with the expert's rows. The main kernel skips an empty tile first; the fixup only skips its writes. Its tiles run to ceil(max_rows / J) * J for every expert of the launch, so a fixup block in an empty tile of a late expert reads past the end of the table. llama.cpp takes this buffer from a padded pool. Strata allocates it at its exact size (T * K rows, in Prefill::carve). When the table ends a 2 MiB chunk, the read faults on the next, unmapped chunk.

Before, on our setup: 2 of 30 fresh ~235k-token prompts crashed, and the 4 crashing texts we found crash every time. After: the same 30 prompts and the 4 texts all pass. Details below.

Since 0.2.11 (engine 0.1.57) more cards take this path by default. On compute capability 8.0 and up, Unsloth UD-Q4/Q5/Q6_K_XL files read the prompt on MMQ, and their gate/up and down products pass this same table as ids (prefill.cpp:5702 and :5717 at 9f7d977d). The pinned ggml turns stream-k on for Q4_K, Q5_K and Q6_K in its Ampere config, and sm_120 uses the Ampere rows. So this fix covers those files too, with no extra change. @Norhiro pointed this out in a comment below. We checked it in the source only; we have not run a K-quant pack.

No open issue reports this cause. Threads with the same error line that may be this bug. None is confirmed, because we did not run the reporters' setups:

  • #1468: prefill copy_i32: an illegal memory access on long prompts, CUDA layer split (RTX 3080 Laptop + RTX 3070 Laptop). The reporter found that all 9 crashes they traced were on the second stage. An earlier log there also shows prefill mmq: mul_mat_q: an illegal memory access. The issue was closed in the tracker reset without a cause. The R9700 (RDNA4) reports in the same thread cannot be this read: no RDNA MMQ config in the pinned ggml uses stream-k.
  • #2778 (single RTX 2080 Ti, Windows, the same copy_i32 line) and #5051 (single RTX 3080 Ti Laptop, Xid 31 FAULT_PDE, layer-major prefill). Weaker matches: neither log names the faulting kernel, and the #5051 fault address is not at the start of a 2 MiB chunk.

Approach

The table now has T * K + T + 256 rows (mmq_ids_rows) instead of T * K. The size is the same in carve and in bytes_needed_impl. The identity is written over all rows, at the three places that write it (bind_stage_helper, run_impl, lm_run).

Why this bound: from an expert's first row the fixup reads at most ceil(max_rows / J) * J rows. An expert takes at most one row per token (max_rows <= T), and J is at most 128 in every MMQ config of the pinned ggml. So every read ends before row T * K + T + 128. The main kernel's read of up to J - 1 rows after an expert's last row also stays inside. With T = 3072 and K = 10 the slack is 13.3 KB of VRAM per stage. The rows past T * K are never routed to, so the products do not change.

A guard in ggml's fixup (if (jt*J >= col_diff) return; before the ids load) would remove the read at its source. That is a change to the pinned llama.cpp code, so it is not in this PR.

Validation

Environment: Linux 7.2, 2x RTX 3090 24 GB (sm_86, no NVLink, 270 W cap), driver 615.78.08, CUDA 13.4. Model: ISTA-DASLab/Qwen3.8-Flash-Next-GSQ-RCO-Coder-GGUF IQ1_M (native pack, shipped Coder expert profile). Engine args: --layer-split 24 --prefill 3072 --max-context 262144 --kv int8 --expert-cache auto --spec 4 --spec-min-p 0.7 --mtp <rt> --pool-workers 15 --vram-reserve-mib 1536 --mmap-experts --trim-stage-weights, with STRATA_PREFILL_LAYER_MAJOR=0. Build: CUDA, Release, CMAKE_CUDA_ARCHITECTURES=86, STRATA_NATIVE_EXPERTS=ON. Prompts: synthetic texts of 234,296-234,941 tokens, a fresh text per seed, 0 reused tokens. Xid lines were read from journalctl -k.

Passed:

  • This head, 8d5c059e on main ffa80d73 (engine 0.1.56): the 4 texts that crash unpatched builds and 2 fresh prompts, 6 runs. All answered, 0 Xid. Prompt read 4,460-4,565 tok/s.
  • 4b9e05a2 (engine 0.1.51), the same commit before its rebase:
    • Unpatched: the 4 texts crash 4 of 4 runs. Each crash is Xid 31 on the second card at the same address modulo 512 MiB (0x1f200000, the start of a 2 MiB chunk), with prefill copy_i32: an illegal memory access was encountered.
    • Patched: those 4 texts and 28 fresh prompts, 32 runs. All answered, 0 Xid. Prompt read median 4,343 tok/s (4,100-4,500).
  • 7dd5cf18 (engine 0.1.49.2):
    • 30 fresh prompts (2 of the 4 texts are among them). Unpatched: 28 of 30 answered, and the 2 crashes are Xid 31. Patched: 30 of 30 answered, 0 Xid. Prompt read median 4,395 tok/s (4,275-4,431).
    • compute-sanitizer --tool memcheck on one crashing text. Unpatched: Invalid __global__ read of size 4 bytes in mul_mat_q<IQ2_S,128> (mmq.cuh:1131) and in mul_mat_q_stream_k_fixup<IQ2_S,96> (mmq.cuh:1353), each a few bytes "after the nearest allocation ... of size 123.136 bytes" (123,136). That allocation is the table: 3072 × 10 × 4 bytes plus the 256 bytes that take() already adds. These reads stay inside the mapped chunk, so they do not fault by themselves; the run under the sanitizer did not reach the read that faults. Patched: ERROR SUMMARY: 0 errors over 165,888 tokens (54 chunks). The client's timeout ended the run under the sanitizer.
  • HIP, compile only: this head and its base ffa80d73, with the ROCm that setup installs for gfx110X (AMD's TheRock wheels rocm[libraries,devel]==7.10.0a20251120: HIP 7.2.53210, Clang 22). We have no AMD GPU, so nothing ran.
    • Wave32 backend, with the flags of setup.py's HIP engine build (-DSTRATA_ENABLE_HIP=ON -DSTRATA_PREFILL_MMQ=ON -DSTRATA_Q6K_EXPERTS=ON -DSTRATA_MMQ_KQUANTS=ON -DCMAKE_HIP_ARCHITECTURES=gfx1100), host GCC 16.2: strata builds and links on both commits, and the patched binary prints strata 0.1.56. prefill.cpp gives the same 312 warnings on both, so the patch adds none.
    • gfx906 backend, with the flags in docs/AMD_HIP.md (-DSTRATA_HIP_GFX906=ON -DCMAKE_HIP_ARCHITECTURES=gfx906), clang with GCC 15.3's libstdc++: prefill.cpp compiles on both commits with the same 306 warnings. (With GCC 16's libstdc++ the base already stopped earlier: its <format> does not compile under HIP's __noinline__ macro, first in src/core/weights.cpp.)

Failed: none caused by this patch. The gfx906 backend's strata does not link, on this head or on its base ffa80d73: src/core/dflash2.cpp calls cudaDeviceGetStreamPriorityRange and cudaStreamCreateWithPriority, which include/strata/platform/hip_compat/ does not map. This came in with 3ae2123b and is still on main 9f7d977d; it is outside this PR.

Not run:

  • The unpatched head with the 4 crashing texts. The unpatched runs above are on 0.1.51 and 0.1.49.2. main 9f7d977d still allocates the table at T * K rows, and this branch merges onto it without conflicts.
  • compute-sanitizer on 0.1.51 or 0.1.56.
  • HIP hardware: the HIP builds above were compiled, not run. In the pinned ggml only the CDNA MMQ configs use stream-k on HIP. The RDNA configs (wave32 backend) and the GCN config (gfx906) do not, so on those builds the change only adds rows.
  • Other HIP architectures. prefill.cpp is host C++ in both HIP backends, so it compiles the same for every architecture.
  • SYCL: not touched. sycl/src/prefill/prefill.cpp is a separate file.
  • A single GPU, other chunk sizes, other packs. --prefill 3072 on this pack happens to end the table about 7.4 KB before a 2 MiB chunk boundary on the second card. Any layout that ends the table near a chunk boundary can fault.
  • A K-quant pack (UD-Q4/Q5/Q6_K_XL). That path is checked in the source only.

Limits

  • The fix is slack, not a bound check. It relies on the fixup reading fewer than T + 256 rows past an expert's first row, which holds for the pinned ggml (J <= 128). A ggml update that raises the largest J would need a larger slack.
  • The peer-device table (pp->ident in set_peer, --peer-device) is also allocated at its exact size. This PR does not change it, and we do not run --peer-device.
  • No speed change is claimed.

🔄 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/5108 **Author:** [@quantforge-br](https://github.com/quantforge-br) **Created:** 11/3/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/mmq-ids-overread` --- ### 📝 Commits (1) - [`8d5c059`](https://github.com/Niko1221/Strata/commit/8d5c059e9874edd60f45db4456642d8dde14c9c8) prefill: room after the MMQ row table for ggml's stream-k fixup read (Xid 31, "prefill copy_i32: an illegal memory access" on long prompts) ### 📊 Changes **1 file changed** (+14 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `src/prefill/prefill.cpp` (+14 -5) </details> ### 📄 Description ## Problem and result On a two-GPU CUDA layer split, a long prompt can end the engine with `prefill copy_i32: an illegal memory access was encountered`. The kernel logs Xid 31 (`FAULT_PDE`, `ACCESS_TYPE_VIRT_READ`) on the second card. `copy_i32` is not the kernel that faults: it is the first `check()` after the fault. The cause is a read past the end of the MMQ row table `ids_identity`. ggml's `mul_mat_q_stream_k_fixup` (`ggml/src/ggml-cuda/mmq.cuh:1353` at the pinned `3cf03257`) loads `ids_dst[col_low + jt*J + j]` for a whole J-column tile before it compares the tile with the expert's rows. The main kernel skips an empty tile first; the fixup only skips its writes. Its tiles run to `ceil(max_rows / J) * J` for every expert of the launch, so a fixup block in an empty tile of a late expert reads past the end of the table. llama.cpp takes this buffer from a padded pool. Strata allocates it at its exact size (`T * K` rows, in `Prefill::carve`). When the table ends a 2 MiB chunk, the read faults on the next, unmapped chunk. Before, on our setup: 2 of 30 fresh ~235k-token prompts crashed, and the 4 crashing texts we found crash every time. After: the same 30 prompts and the 4 texts all pass. Details below. Since 0.2.11 (engine 0.1.57) more cards take this path by default. On compute capability 8.0 and up, Unsloth UD-Q4/Q5/Q6_K_XL files read the prompt on MMQ, and their gate/up and down products pass this same table as `ids` (`prefill.cpp:5702` and `:5717` at `9f7d977d`). The pinned ggml turns stream-k on for Q4_K, Q5_K and Q6_K in its Ampere config, and sm_120 uses the Ampere rows. So this fix covers those files too, with no extra change. @Norhiro pointed this out in a comment below. We checked it in the source only; we have not run a K-quant pack. ## Related discussion No open issue reports this cause. Threads with the same error line that may be this bug. None is confirmed, because we did not run the reporters' setups: - #1468: `prefill copy_i32: an illegal memory access` on long prompts, CUDA layer split (RTX 3080 Laptop + RTX 3070 Laptop). The reporter found that all 9 crashes they traced were on the second stage. An earlier log there also shows `prefill mmq: mul_mat_q: an illegal memory access`. The issue was closed in the tracker reset without a cause. The R9700 (RDNA4) reports in the same thread cannot be this read: no RDNA MMQ config in the pinned ggml uses stream-k. - #2778 (single RTX 2080 Ti, Windows, the same `copy_i32` line) and #5051 (single RTX 3080 Ti Laptop, Xid 31 `FAULT_PDE`, layer-major prefill). Weaker matches: neither log names the faulting kernel, and the #5051 fault address is not at the start of a 2 MiB chunk. ## Approach The table now has `T * K + T + 256` rows (`mmq_ids_rows`) instead of `T * K`. The size is the same in `carve` and in `bytes_needed_impl`. The identity is written over all rows, at the three places that write it (`bind_stage_helper`, `run_impl`, `lm_run`). Why this bound: from an expert's first row the fixup reads at most `ceil(max_rows / J) * J` rows. An expert takes at most one row per token (`max_rows <= T`), and J is at most 128 in every MMQ config of the pinned ggml. So every read ends before row `T * K + T + 128`. The main kernel's read of up to `J - 1` rows after an expert's last row also stays inside. With `T = 3072` and `K = 10` the slack is 13.3 KB of VRAM per stage. The rows past `T * K` are never routed to, so the products do not change. A guard in ggml's fixup (`if (jt*J >= col_diff) return;` before the `ids` load) would remove the read at its source. That is a change to the pinned llama.cpp code, so it is not in this PR. ## Validation Environment: Linux 7.2, 2x RTX 3090 24 GB (sm_86, no NVLink, 270 W cap), driver 615.78.08, CUDA 13.4. Model: ISTA-DASLab/Qwen3.8-Flash-Next-GSQ-RCO-Coder-GGUF IQ1_M (native pack, shipped Coder expert profile). Engine args: `--layer-split 24 --prefill 3072 --max-context 262144 --kv int8 --expert-cache auto --spec 4 --spec-min-p 0.7 --mtp <rt> --pool-workers 15 --vram-reserve-mib 1536 --mmap-experts --trim-stage-weights`, with `STRATA_PREFILL_LAYER_MAJOR=0`. Build: CUDA, Release, `CMAKE_CUDA_ARCHITECTURES=86`, `STRATA_NATIVE_EXPERTS=ON`. Prompts: synthetic texts of 234,296-234,941 tokens, a fresh text per seed, 0 reused tokens. Xid lines were read from `journalctl -k`. **Passed:** - This head, `8d5c059e` on `main` `ffa80d73` (engine 0.1.56): the 4 texts that crash unpatched builds and 2 fresh prompts, 6 runs. All answered, 0 Xid. Prompt read 4,460-4,565 tok/s. - `4b9e05a2` (engine 0.1.51), the same commit before its rebase: - Unpatched: the 4 texts crash 4 of 4 runs. Each crash is Xid 31 on the second card at the same address modulo 512 MiB (`0x1f200000`, the start of a 2 MiB chunk), with `prefill copy_i32: an illegal memory access was encountered`. - Patched: those 4 texts and 28 fresh prompts, 32 runs. All answered, 0 Xid. Prompt read median 4,343 tok/s (4,100-4,500). - `7dd5cf18` (engine 0.1.49.2): - 30 fresh prompts (2 of the 4 texts are among them). Unpatched: 28 of 30 answered, and the 2 crashes are Xid 31. Patched: 30 of 30 answered, 0 Xid. Prompt read median 4,395 tok/s (4,275-4,431). - `compute-sanitizer --tool memcheck` on one crashing text. Unpatched: `Invalid __global__ read of size 4 bytes` in `mul_mat_q<IQ2_S,128>` (`mmq.cuh:1131`) and in `mul_mat_q_stream_k_fixup<IQ2_S,96>` (`mmq.cuh:1353`), each a few bytes "after the nearest allocation ... of size 123.136 bytes" (123,136). That allocation is the table: 3072 × 10 × 4 bytes plus the 256 bytes that `take()` already adds. These reads stay inside the mapped chunk, so they do not fault by themselves; the run under the sanitizer did not reach the read that faults. Patched: `ERROR SUMMARY: 0 errors` over 165,888 tokens (54 chunks). The client's timeout ended the run under the sanitizer. - HIP, compile only: this head and its base `ffa80d73`, with the ROCm that setup installs for gfx110X (AMD's TheRock wheels `rocm[libraries,devel]==7.10.0a20251120`: HIP 7.2.53210, Clang 22). We have no AMD GPU, so nothing ran. - Wave32 backend, with the flags of `setup.py`'s HIP engine build (`-DSTRATA_ENABLE_HIP=ON -DSTRATA_PREFILL_MMQ=ON -DSTRATA_Q6K_EXPERTS=ON -DSTRATA_MMQ_KQUANTS=ON -DCMAKE_HIP_ARCHITECTURES=gfx1100`), host GCC 16.2: `strata` builds and links on both commits, and the patched binary prints `strata 0.1.56`. `prefill.cpp` gives the same 312 warnings on both, so the patch adds none. - gfx906 backend, with the flags in `docs/AMD_HIP.md` (`-DSTRATA_HIP_GFX906=ON -DCMAKE_HIP_ARCHITECTURES=gfx906`), clang with GCC 15.3's libstdc++: `prefill.cpp` compiles on both commits with the same 306 warnings. (With GCC 16's libstdc++ the base already stopped earlier: its `<format>` does not compile under HIP's `__noinline__` macro, first in `src/core/weights.cpp`.) **Failed:** none caused by this patch. The gfx906 backend's `strata` does not link, on this head or on its base `ffa80d73`: `src/core/dflash2.cpp` calls `cudaDeviceGetStreamPriorityRange` and `cudaStreamCreateWithPriority`, which `include/strata/platform/hip_compat/` does not map. This came in with `3ae2123b` and is still on `main` `9f7d977d`; it is outside this PR. **Not run:** - The unpatched head with the 4 crashing texts. The unpatched runs above are on 0.1.51 and 0.1.49.2. `main` `9f7d977d` still allocates the table at `T * K` rows, and this branch merges onto it without conflicts. - compute-sanitizer on 0.1.51 or 0.1.56. - HIP hardware: the HIP builds above were compiled, not run. In the pinned ggml only the CDNA MMQ configs use stream-k on HIP. The RDNA configs (wave32 backend) and the GCN config (gfx906) do not, so on those builds the change only adds rows. - Other HIP architectures. `prefill.cpp` is host C++ in both HIP backends, so it compiles the same for every architecture. - SYCL: not touched. `sycl/src/prefill/prefill.cpp` is a separate file. - A single GPU, other chunk sizes, other packs. `--prefill 3072` on this pack happens to end the table about 7.4 KB before a 2 MiB chunk boundary on the second card. Any layout that ends the table near a chunk boundary can fault. - A K-quant pack (UD-Q4/Q5/Q6_K_XL). That path is checked in the source only. ## Limits - The fix is slack, not a bound check. It relies on the fixup reading fewer than `T + 256` rows past an expert's first row, which holds for the pinned ggml (`J <= 128`). A ggml update that raises the largest J would need a larger slack. - The peer-device table (`pp->ident` in `set_peer`, `--peer-device`) is also allocated at its exact size. This PR does not change it, and we do not run `--peer-device`. - No speed change is claimed. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 16:34:15 +07:00
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#5310
No description provided.