Repository navigation
cuda: page MoE experts for 2-8 token calls, and keep a VRAM reserve for the page slabs - #417
Conversation
The expert page slabs are allocated lazily at the first decode, after the compute buffers are reserved, so they took every byte that was left. Flash-attention scratch then had no room to grow at long context and the next deep prompt failed with out-of-memory. Check free memory before allocating a slab and keep GGML_MOE_PAGE_RESERVE_MB (default 1024) free. A tensor that does not get a slab reads its experts from host memory, as before. Mistral Small 4 TQ4_1S, 256k context, 7 expert layers on host, MI210: VRAM after the first requests 63.96 -> 63.20 GiB; a 201k-token prompt now completes with VRAM flat at 63.20 GiB. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The expert address table keeps an entry per expert, and a call only repoints the entries for the experts it routes. Expert paging ran only for single-token calls, so a call of 2 to 8 tokens read the table as the last single-token call left it. If an expert had been evicted from its slot since then, its entry still named that slot, and the batch read another expert's weights. Short prompt tails and speculative verify steps are batches of this size. Run the residency plan for every batch size up to MMVQ_MAX_BATCH_SIZE. When several tokens route the same expert, it is copied once and every token reads the copy. Checked with a seeded sequence of 400 calls of 1 to 8 tokens against the same sequence with paging off: 20% of outputs differed before, none after, at 8, 64 and 512 slots. MUL_MAT_ID 979/979 on gfx90a. Co-Authored-By: Claude Opus 5.5 <[email protected]>
TheTom
left a comment
There was a problem hiding this comment.
Ran the pagecheck harness from the description on a GB10 (sm_121) with 8 slots: base has 338 of 400 calls that differ from paging off, this PR has 0, and the paging kernels run on every call. -o MUL_MAT_ID for tq3_1s and tq4_1s passes. The harness needed a fallback for GB10 reporting as an integrated GPU.
| // repointed for the experts a call routes, so an expert evicted from its slot by an earlier call | ||
| // still has an entry naming that slot, which now holds a different expert. Skipping the plan for | ||
| // batches of more than one token, as this once did, made those batches read the wrong weights. | ||
| const int ids_stride_tbl = ids->nb[1] / ggml_type_size(ids->type); |
There was a problem hiding this comment.
ids_stride_tbl is the same expression as ids_stride, computed at the top of ggml_cuda_mul_mat_id_tq. Drop it and pass ids_stride to the four paging kernels.
| static const bool page_in_on = getenv("GGML_MOE_PAGE_IN") != nullptr; | ||
| #endif | ||
| if (page_in_on && expert_tab && n_tokens == 1 && nb_expert % (int64_t) sizeof(uint4) == 0) { | ||
| // Every call that reads the table must page, whatever its batch size. Table entries are only |
There was a problem hiding this comment.
This block tells the history of the bug ("as this once did"). AGENTS.md asks for 1-2 lines on the invariant, for example: every call that reads the table must run the plan, since only routed experts get their entries repointed.
|
Thanks @jasstrong. The harness is the right way to show the paging bug, and it reproduces on a GB10 here too. Two small comments inline. |
Review follow-up. ids_stride_tbl duplicated ids_stride, which is computed at the top of ggml_cuda_mul_mat_id_tq. The comment now states the invariant only. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks. Both are done in 61fb6f3: |
|
The head of this PR is on the old base ( The rebased branch is on my fork, so it can be pushed as-is: head: It picks up #414 (src1 column count in the TQ fused and cuBLAS paths), #412 (null |
…heTom#417, TheTom#371, TheTom#401, TheTom#419) # Conflicts: # AGENTS.md # docs/KV-cache-quantization.md # tests/CMakeLists.txt
Two follow-ups to the expert paging from #364. Both only matter with
GGML_MOE_PAGE_INset; with paging off nothing changes.1. Page for batched calls too (correctness). Paging ran only when
n_tokens == 1, but the kernels read the expert table at every batch size up toMMVQ_MAX_BATCH_SIZE. A table entry is only repointed for the experts a call routes. So a 2-8 token call read the table as the last single-token call left it, and an expert evicted since then still pointed at a slot that now holds another expert. Short prompt tails and speculative verify steps are batches of that size.The residency plan now runs for every batch size. The bookkeeping is sized for
n_expert_used * MMVQ_MAX_BATCH_SIZErouted entries, and when several tokens route the same expert it is copied once and every token reads the copy.2. Keep a VRAM reserve for the slabs. The slabs are allocated at the first decode, after the compute buffers, so they took everything that was left, and flash-attention scratch then had no room to grow at long context. Each slab allocation now checks free memory and leaves
GGML_MOE_PAGE_RESERVE_MB(default 1024) free; a tensor that does not get a slab reads in place, as before. Measured on our fleet build with Mistral Small 4 TQ4_1S at 256k context, 7 expert layers on host, MI210: VRAM 63.96 -> 63.20 GiB, and a 201k-token prompt that used to fail with out-of-memory now completes.Testing (MI210 gfx90a, ROCm 7.2.3). A standalone harness runs 400 seeded
MUL_MAT_IDcalls of 1-8 tokens on a TQ4_1S expert stack in pinned host memory (32 experts, 4 routed per token,GGML_MOE_HOST_EXPERTS=1 GGML_MOE_EXPERT_TABLE=1), once with paging and once without, and compares every output:feature/turboquant-kv-cachetest-backend-ops
MUL_MAT_ID: tq4_1s 44/44, tq3_1s 44/44. Perplexity with paging off is unchanged.Harness (pagecheck.cpp)
Build against a ROCm or CUDA build:
g++ -O2 -std=c++17 -Iggml/include pagecheck.cpp -Lbuild/bin -lggml -lggml-base -Wl,-rpath,build/bin -o pagecheck, then run it with and withoutGGML_MOE_PAGE_IN=1 GGML_MOE_PAGE_SLOTS=8and compare the two output files.🤖 Generated with Claude Code