Repository navigation
CUDA: support duplicate expert ids in mul_mat_id - #26294
Nas01010101 wants to merge 6 commits into
Conversation
|
Hi @Nas01010101, thanks for your contribution! Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:
Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below. |
|
Initially I closed this PR because it seemed out of scope. But @ngxson's new changes require this fix, so opening it again. |
|
Related to #19182 where the model intentionally use duplicated expert, NOT an undesired behavior. CUDA is the only backend that doesn't support this case. Measured: less than < 1% perf lost with this PR llama-bench -m ../models/Qwen3.6-35B-A3B-UD-IQ2_M.ggufmaster:
PR:
|
|
@ggml-org/ggml-cuda any objections to this? |
|
/bot review |
Automated code reviewI've completed the static review. Here are my findings. Code review: CUDA fix duplicate expert id compaction in mul_mat_id (#26294)Checklists run: Scope/quick-reject gate, Security, ggml/backend, General. (No new model/arch, public API, server, or multimodal paths touched.) The core algorithmic change is correct: replacing the per-token Blocking(point 1) Shared-memory
This is reachable in exactly the degenerate-routing domain the PR targets: e.g. one expert that is the top pick for all Will slow the review(point 2) The generic fallback path is still buggy. (point 3) Test coverage gap. The new case covers only Nits(point 4) (point 5) The two new comments in (point 6) AI disclosure: the PR description discloses AI assistance and references an This review was generated automatically by pi coding agent using |
validated on #19182 , still not fixed the problem
ORippler
left a comment
There was a problem hiding this comment.
At the very least, we have to be consistent between templated and non-templated versions of mm_ids_helper. Beyond that, I don't understand when/why we would want to route a token to the same expert multiple times. In what models do we generate selected experts by means other than ggml_argsort_top_k? AI tells me it could be done in DSV4 (@am17an please confirm if this is a valid use-case)
|
It's the longcat models I think |
Hmm I see. We can merge this one then once we are consistent between templated and non-templated versions of |
|
Tested on RTX 3080 Laptop (CUDA 12.9). Found a third instance of the same bug: |
60fd1f1 to
f3ff969
Compare
am17an
left a comment
There was a problem hiding this comment.
@JohannesGaessler this PR fixes the currently failing CI. Can you take a look?
|
I had seen this PR in my backlog but at a glance it seemed overengineered. I'll take a look. |
|
There are edge cases that cannot be fixed simply by modifying |
Hi Johannes Thanks for looking into this. I tried fixing the two perf issues from #30477 on top of your commit:
Pushing it here. Feel free to fold whatever is useful into #30477 instead, I'm fine closing this one. |
f3ff969 to
ef88eb1
Compare
|
Let's close this PR then. |
When a token's top-k list contains the same expert more than once, mul_mat_id dropped or corrupted columns; on the quantized path this showed up as an illegal memory access inside quantize_mmq_q8_1. Ports the upstream fix (PR #26294, which builds on #30477): - mm_ids_helper keeps shared memory at one entry per token and stores a first-match plus a count; duplicate slots are expanded at write-out via a prefix sum over per-token counts (no extra shared memory). - MMQ/MMF tiles address columns through the packed-tile mapping instead of launching ceil(ncols_max/J) mostly-empty tiles per expert. - The host fallback no longer breaks at the first match. Two adaptations for this fork: - GGML_USE_HIP: the new ntx <= 65535 assert guards the CUDA grid.y limit; ROCm accepts larger grids (a 65625-tile case is legal here). - Rebased onto the ROCmFPX MMQ/MMF specializations. Verification: MUL_MAT_ID 953/953 (was: GPU hang); full suite 18781/18785 (the 4 failures are the documented f16-KV deep-context precision edges, same as stock); MUL_MAT rocmfp4 24/24, rocmfpx 61/61; dense end-to-end pp2048 709.9 t/s (-1.5%, noise band), pp16384 610.1 (-0.6%), tg256 14.03 unchanged.
Overview
Fixes #24591.
When a token's top-k list contains the same expert more than once,
mul_mat_idon CUDA dropped or corrupted columns, which showed up as an illegal memory access inggml_cuda_mul_mat_q.This now builds on Johannes's fix from #30477 (kept as the first commit, with his authorship) and removes the
n_expert_usedit introduced:mm_ids_helper: shared memory stays at one entry per token (n_tokens, as on master).Each token stores its first matching slot plus a count; duplicate slots are expanded only when writing out, using a prefix sum over the per-token counts. Without duplicates the write-out is the same as masterMMQ + MMF tiles: instead of launching
ceil(ncols_max/J)column tiles per expert (mostly empty whenncols_max = ne12*n_expert_used), both kernels iterate over a packed tile index. A warp prefix sum overceil(cnt_e/J)fromexpert_boundsmaps tile -> (expert, jt), same idea as vLLM'smoe_align_block_size. The host bound isceil(n_cols/J) + min(n_experts, n_cols).MMQ launch: when that bound has >= 90% wave efficiency, MoE uses the tiled launch like master (one block per tile, tiles past the last real one exit immediately). Otherwise it uses stream-k, which reads the real tile count on device before splitting work. Forcing stream-k for MoE was ~3% slower on gpt-oss, which is why the tiled path is kept.
MMF: the small-batch path loops over the rank of each match per token, so it handles duplicates and the
return false; // FIXMEis gone.Tests: the
#if 0around the duplicate-idtest-backend-opscases is removed.Additional information
RTX 3080 Laptop (sm_86, CUDA 12.4):
test-backend-ops -o MUL_MAT_ID(dup tests on)test-backend-opsperf -o MUL_MAT_ID, time vs master (geomean, 132 cases)I also ran MUL_MAT_ID and MUL_MAT with the MoE launch forced to tiled and forced to stream-k: both pass. Only tested on NVIDIA sm_86; the AMD path and 256-expert models are untested.
Requirements