Repository navigation
perf(cpu-moe): split the cpu executor's work by expert when tokens share one - #916
Merged
jason-fxz merged 1 commit intoNov 20, 2026
Merged
Conversation
37 of 46 tasks
Collaborator
|
LGTM. Reproduced on a Xeon 8559C (16 cores): verify-level sharing 1.10-1.56x, no sharing within noise, y bit-identical to main in all 170 cases. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Does the "Batch size > 1" item of the CPU expert executor section in #671: split the work by expert, so an expert shared by several tokens is read once.
On main a decode step's work items are (route, 32-row block) in pass 1 and (token, 32-row block) in pass 2, claimed off one atomic counter. When two tokens of a step pick the same expert, its blocks usually go to two cores and each reads them from DRAM. An MTP / DFlash verify step sends consecutive tokens of one sequence, and those share a lot (docs/performance/decode.md: 4 tokens touch 2.3-2.75 x top_k experts).
submitgroups the step's active routes by expert: a counting sort over the experts the step uses, larger groups first, token order inside a group, no allocation.gate_upfor every route on that expert back to back, so tokens after the first read the block from its cache.format.h's note that deferred this is updated. A kernel that decodes a block once for several tokens is still not here; it only pays off where decode, not bandwidth, bounds the GEMV.test_cpu_moe_tokens_sharing_experts_match_each_token_aloneruns a batch whose tokens share experts and compares it bit for bit with each token run alone, for bf16, NVFP4, mxfp4 and ds_fp4: 7 tokens on 3 threads (pass 2 token groups of 4 and 3, one GPU route), and 70 tokens on one thread with expert 0 on every token (one pass 2 item holds all 70). It then runs the batch again after the single-token steps, which are not grouped. It passes on main too; it goes red if a grouped item reads another route's scratch, input or weight, a pass 2 block writes another token's row, or an ungrouped step leaves its expert marks set for the next step.Everything below is main f2e7f24 against this PR's code, in the RTX PRO 6000 Blackwell Server Edition test container (sm_120, PCIe 5.0 x16, 16 vCPUs, driver 610.57.04, torch 2.14.1+cu130, Triton 3.8.0), 8 pool threads. The runs were on this PR's code one comment edit before the final commit.
End to end
nvidia/Qwen3.6-35B-A3B-NVFP4, hybrid decode, tok/s, main -> this PR. Main's_cpu_moeis built in the same container from main's source and swapped in per server start; arms alternate. bs = 1: a warm-up, then two greedy requests of 512 tokens,predicted_per_secondfrom the timings report, median of the 6 requests over the 3 starts. c4: the second round of four greedy requests of 256 tokens sent together. Both arms drafted and accepted the same counts. "CPU experts" is thecpuvalue of--moe-collect-statsper layer-step, from a stats run of main in each container (the same in the three no-draft containers).--moe-cache-rateWith MTP every step verifies 4 tokens of one sequence, and that is where the shared reads are. Without drafts a bs 1 step has no expert with two routes, so it runs main's split, and c4 shares little. Those rows land at 0.96x-1.02x, and this PR's own code built a second time through main's build path (
python setup.py build_ext --inplace, the column before last) lands at 0.93x-1.02x against the same main runs, so these end-to-end runs do not resolve differences that small; the executor timings below do.bs = 1 prompt: "Write a Python function that parses an ISO 8601 date string without using datetime, then explain each step of it."
Executor alone
Main's
_cpu_moeand this PR's built in one container and loaded in one process, timed in turn on the same inputs, every call on a fresh window of a 2 GiB expert pool so nothing is warm from the other side. 4 processes x 4 rounds of fresh executors, medians. Zen 5 server CPU (AMD family 26 model 2). Inputs are real MoE-layer inputs, routing and router weights captured from prefills ofunsloth/GLM-5.3-Flash-GGUF(UD-IQ1_M),unsloth/DeepSeek-V4.1-Flash-1017-GGUF(UD-IQ1_M) andunsloth/Qwen3.6-35B-A3B-GGUF(UD-Q4_K_M), fed to each model's layer shape in the listed expert format with weights filled by the harness. "consecutive" is bs consecutive tokens of one prompt and layer, which is what a verify step routes; "scattered" is bs tokens from random positions, as separate requests would send. Speedup = main / this PR.For example GLM-5.3-Flash at bs 8 consecutive goes from 6257 us to 4883 us per layer, Qwen3.6 bf16 from 2103 us to 1426 us.
Synthetic routing on the same inputs, over the five rows: the sharing level decode.md reports for a verify step (0.625 x routes distinct) 1.08-1.42; every token on the same experts 1.14-2.15; one expert on every token and the rest distinct 0.98-1.06; a hybrid step with a third of the routes and one whole token on the GPU 1.01-1.19 at bs 4 and 8, 0.97-0.99 at bs 2. At bs 2 that hybrid step leaves one token on the CPU, so nothing is shared and it runs main's split; main timed against a second build of main reads 0.97-0.99 on those cases in this harness. Over the 20 no-sharing cases the mean is 0.995, against 0.997 for main against main. Geomean over the other 90 cases: 1.15.
How much the sharing cases gain depends on whether DRAM bandwidth bounds the expert GEMV on the host. On this CPU a bs 1 step with fresh experts takes 2.3x (GLM NVFP4), 2.5x (bf16), 1.6x (Qwen NVFP4) and 1.3x / 1.4x (ds_fp4, GGUF Q4_K) as long as one whose routes all hit one cached expert, and the gains follow that order. I have no consumer desktop to time this on.
Bit-identity with main in the same harness: every case above plus bs 3, 5, 7, 9, 11, 16, 32, 64, 65, 112, 128 in executors built for 128 tokens; independent uniform routing and steps with every route on the GPU; rows at 4x the largest captured |x|, 1e-6, zeros, bf16 subnormals and the bf16 limit; router weights 0 and 1; 0 (auto), 1, 3, 8, 12 threads; spin on and off;
decodeanddecode_submit/decode_sync(add_to)captured in CUDA graphs at bs 1 to 128 and replayed with new inputs; and fp8_block, gpt-oss mxfp4 (real gpt-oss-20b inputs), scaled GGUF and an H 2000 / I 500 bf16 shape besides the five above. 838 case checks, all bit-identical to main, and each token in a batch equals the token run alone.pytest tests/moe/test_cpu_moe.pyin that container, on this PR's build: this PR's file 46 passed, 18 skipped; main's file 34 passed, 18 skipped. On main's own build (A10, main f2e7f24): main's file 31 passed, 21 skipped; this PR's file 43 passed, 21 skipped, of which the new test's 12 cases all pass.Not tested: a consumer desktop CPU, timing at thread counts other than 8 (the bit checks cover 0, 1, 3, 8 and 12), ROCm.