Repository navigation
ggml-cpu : multi-row MUL_MAT_ID dot products for few-row experts on AVX2 - #470
danielhanchen wants to merge 3 commits into
Conversation
When an expert of MUL_MAT_ID receives 2 to 8 rows (speculative drafts, short prompts, partially offloaded MoE layers), each weight block is now decoded once and dotted with all of them. Covered types: Q5_0, IQ4_NL, MXFP4, Q4_K, Q5_K, Q6_K, IQ1_S, IQ2_XXS, IQ3_XXS, IQ3_S and IQ4_XS. Each column runs the single-column AVX2 kernel's instruction sequence, so the results are bit-identical to ggml_vec_dot per row. The reference backend (use_ref) keeps the per-row loop. GGML_CPU_MMID_MULTI=0 restores the previous path. tests: test-mmid-multi checks bit identity against one vec_dot per row; test-backend-ops gets few-row MUL_MAT_ID cases and expert-shaped perf cases.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 57a144d418
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| static int on = -1; | ||
| if (on < 0) { | ||
| const char * e = getenv("GGML_CPU_MMID_MULTI"); | ||
| on = e == NULL || atoi(e) != 0; |
There was a problem hiding this comment.
Synchronize initialization of the multi-row environment switch
On the first multithreaded MUL_MAT_ID operation with multiple routed rows per chunk, several workers can enter ggml_cpu_vec_dot_multi() concurrently and read/write the shared non-atomic on. The barrier in the caller occurs before these accesses, so it does not protect this initialization. This introduces a C data race and undefined behavior even when every worker writes the same value. Initialize the switch before worker dispatch or use synchronized one-time initialization, as the tiled-matmul environment switches already do.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in a8648e2: the switch is now read once in ggml_cpu_init(), next to GGML_CPU_DISABLE_FUSION, so workers only read it.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Tested on AMD: Ryzen 9 9950X, Windows, built with ROCm 7.1 clang ( Correctness: Speed, with this PR's own Flash-Next expert-shaped perf cases. Same binary, multi-row path on vs
1-row ops take the old path and measure 0.93x to 1.00x, inside the run-to-run spread. IQ3_S lines up with the Xeon numbers and passes them at 7 rows. Q4_K gains more on Zen 5 than on the Xeon, and Q5_0 gains less. Not covered: there's no base build with these perf cases, so this relies on the kill switch restoring the old path, and no end-to-end speculative decode run yet. |
fix #396, pin #469 #470 (#473) * pr-set : pin #464, default a missing NVFP4 / FP8 weight scale to 1.0 * pr-set : repin #378 #385 #386 #431 #448 for upstream b12859..b12927 #378, #385, #386 and #431 merge upstream aa631e0 (FA bias API cleanup, b12859); #378 also keeps the Inkling router soft_max off the new short-row kernels (CUDA b12878, Vulkan b12916). #448 merges b12922 (few-row MMQ for NVFP4 adds a dst argument to ggml_cuda_should_use_mmq_few). feature-checks: add inkling2-expert-scales (MUL_MAT_ID with weight scales). * pr-set : repin #378 to keep the Inkling router soft_max on the pre-b12878 Metal kernel * pr-set : repin #378 #386 #431 for b12939 (rpc SUPPORTS_OP, BF16 supports_op), comment-only repins of #241 #243 * pr-set : drop #462, upstream took the same short conv fusion in b12942 * pr-set : pin #469 (greedy argmax of the logits row) * pr-set : repin #418 for the upstream flat CONCAT kernel in b12957 * pr-set : pin #470 (CPU multi-row MUL_MAT_ID for few-row experts) * pr-set : repin #396 (the model saver writes a shared host weight once) * feature-checks : keep cuda-short-conv-in-place with owner null, upstream b12942 carries it * pr-set : pin upstream's Qwen4 model support until the aged base carries it (b12969)
…dots GCC 11 at -O3 cannot prove that the loops over the first nc rows initialize them and fails the build with -Werror=maybe-uninitialized. The arrays are still set by those loops, so the results are unchanged.

What this does
When an expert of
MUL_MAT_IDon the CPU receives 2 to 8 rows (speculative drafts, short prompts, the CPU part of a partially offloaded MoE layer,-cmoe, or the MoE cache's CPU share), each weight block is now decoded once and dotted with all of those rows, instead of being decoded once per row.ggml_vec_dotper row.use_ref) keeps the per-row loop, sotest-backend-opsstill compares against the unchanged kernels.GGML_CPU_MMID_MULTI=0(read once when the CPU backend starts) restores the previous path.Why
On MoE models whose experts run on the CPU, a decode step with speculative drafts gives each routed expert several rows. Re-decoding the same quantized blocks for every row makes those experts memory and decode bound. The matrices measured below use Qwen3.8-Flash-Next expert shapes.
Tests
test-mmid-multi: for every covered type and 1 to 9 rows, the multi-row result is bit-identical to onevec_dotper row; a deliberately corrupted kernel is caught (fault injection).test-backend-opsgets few-rowMUL_MAT_IDeval cases for every covered type (2, 3, 5, 7, 8, 9 rows, broadcast and 8-expert variants) and expert-shaped perf cases.LLAMA_FATAL_WARNINGS=ON: native, AVX2-only and no-AVX. ctest 24/24,test-llama-archs161/161,test-chatpass.test-backend-ops -b CPU:MUL_MAT_ID1398/1398,MUL_MAT3042/3042.GGML_CPU_MMID_MULTI=0restores the previous timing with identical output hashes.GGML_CPU_ALL_VARIANTS=ON,LLAMA_FATAL_WARNINGS=ON):quants.cbuilds without warnings for all 14 x86 CPU variants. The per-row accumulator arrays are zero-initialized because GCC 11 cannot otherwise prove that the per-row loops set them.LLAMA_FATAL_WARNINGS=ONhas 0 warnings,test-mmid-multipasses andtest-backend-ops -b CPU -o MUL_MAT_IDpasses 1398/1398. An x64 (AVX2) cross-build compilesarch/x86/quants.c,ggml-cpu.candquants.cwith no warnings, and both tests pass under x64 emulation. (With fatal warnings, the x64 build only stops on an unrelated upstream file this PR does not touch.)Numbers
8 threads on a shared Xeon Platinum 8559C, Flash-Next expert shapes (8 experts of 8 used),
MUL_MAT_IDtime per op, speedup vs the base build; output hashes identical in every cell:The host was loaded; these are medians of repeated runs, and single runs varied widely.
External test (AMD Ryzen 9 9950X, Zen 5, Windows, clang with
-march=native):test-mmid-multipasses andMUL_MAT_IDpasses 1398/1398. Multi-row vsGGML_CPU_MMID_MULTI=0, medians at 2 / 3 / 4 / 7 rows:One row: 0.93x to 1.00x (noise).
How the prebuilt uses this
The unslothai/llama.cpp prebuilt is an aged upstream
b####tag plus a pinned set of PRs merged in order; this PR is one pin, based on upstream b12932 (base/upstream-800ae7e23).Not proven
MUL_MAT_IDop.