Skip to content

perf(cpu-moe): split the cpu executor's work by expert when tokens share one - #916

Merged
jason-fxz merged 1 commit into
FlashML-org:mainfrom
ischencheng:perf/cpu-moe-expert-groups
Nov 20, 2026
Merged

jason-fxz merged 1 commit into
FlashML-org:mainfrom
ischencheng:perf/cpu-moe-expert-groups

Conversation

@ischencheng

Copy link
Copy Markdown
Contributor

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).

  • submit groups 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.
  • A pass 1 item is (expert, 32-row block). One worker runs gate_up for every route on that expert back to back, so tokens after the first read the block from its cache.
  • A pass 2 item is (32-row block, group of tokens), so a shared expert's down rows are also read once per block.
  • A step where no expert has two routes (bs 1, or tokens that pick disjoint experts) is not grouped: one route per pass 1 item and one token per pass 2 item, in main's order. The only extra work there is the pass over the step's routes that finds no sharing.
  • The format kernels are untouched. Each token gets the same calls with the same arguments and the same route order in the sum, so y is bit-identical to main.

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_alone runs 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_moe is 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_second from 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 the cpu value of --moe-collect-stats per layer-step, from a stats run of main in each container (the same in the three no-draft containers).

drafts --moe-cache-rate CPU experts bs = 1 c4 same code, main's build path (bs = 1 / c4) starts per arm
MTP k=3 0.2 4.74 237.4 -> 242.8 (1.02x) 273.0 -> 280.7 (1.03x) - 3
MTP k=3 0.5 0.64 316.8 -> 321.4 (1.01x) 402.2 -> 413.2 (1.03x) - 3
off, container 1 0.2 1.96 155.3 -> 151.0 (0.97x) 240.9 -> 237.6 (0.99x) - 3
off, container 2 0.2 1.96 150.9 -> 153.4 (1.02x) 237.3 -> 238.3 (1.00x) 1.00x / 0.93x 3
off, container 3 0.2 1.96 155.6 -> 148.8 (0.96x) 238.1 -> 236.8 (0.99x) 0.94x / 0.98x 3
off, container 1 0.5 0.36 181.4 -> 178.1 (0.98x) 328.3 -> 325.1 (0.99x) - 3
off, container 2 0.5 0.36 182.6 -> 182.8 (1.00x) 327.4 -> 330.6 (1.01x) 0.99x / 1.00x 3
off, container 3 0.5 0.36 181.3 -> 183.3 (1.01x) 326.1 -> 325.3 (1.00x) 1.02x / 1.01x 3

With 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.

ft bench bw --model nvidia/Qwen3.6-35B-A3B-NVFP4 --cpu-threads 8
ft serve --model nvidia/Qwen3.6-35B-A3B-NVFP4 --max-seq-len-override 4096 --moe-strategy hybrid --moe-cache-rate <R> \
  --moe-cpu-threads 8 --max-running-requests 4 --enable-timings-report [--speculative-algorithm mtp --speculative-num-steps 3]

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_moe and 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 of unsloth/GLM-5.3-Flash-GGUF (UD-IQ1_M), unsloth/DeepSeek-V4.1-Flash-1017-GGUF (UD-IQ1_M) and unsloth/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.

layer, format consecutive bs 2 / 4 / 8 scattered bs 2 / 4 / 8 no sharing bs 1 / 2 / 4 / 8
GLM-5.3-Flash, NVFP4 1.08 / 1.19 / 1.27 1.01 / 1.08 / 1.14 0.99 / 1.00 / 1.00 / 1.00
DeepSeek-V4.1-Flash, ds_fp4 1.04 / 1.11 / 1.09 1.00 / 1.07 / 1.05 1.00 / 0.99 / 1.03 / 1.00
Qwen3.6-35B-A3B, NVFP4 1.11 / 1.17 / 1.23 1.02 / 1.11 / 1.19 0.98 / 0.98 / 0.98 / 0.98
Qwen3.6-35B-A3B, bf16 1.15 / 1.32 / 1.49 1.04 / 1.12 / 1.28 0.99 / 1.00 / 1.00 / 1.00
Qwen3.6-35B-A3B, GGUF Q4_K 1.06 / 1.12 / 1.15 1.02 / 1.08 / 1.14 0.98 / 0.99 / 0.99 / 1.00

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; decode and decode_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.py in 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.

@jason-fxz

Copy link
Copy Markdown
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.

@jason-fxz
jason-fxz merged commit c84946b into FlashML-org:main Nov 20, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants