Skip to content

docs(cuda): record the MoE expert overlap and why no grouped kernel - #2796

Merged
inureyes merged 1 commit into
mainfrom
update/issue-2784-moe-gather-traffic
Oct 31, 2026
Merged

inureyes merged 1 commit into
mainfrom
update/issue-2784-moe-gather-traffic

Conversation

@inureyes

Copy link
Copy Markdown
Member

#2784 asked how much a kernel that reads each selected expert once could save, and to close with the number if it is small. The conclusion is not to build it. Record: docs/benchmark_results/moe-distinct-experts-n1x-2026-10-31.md.

Gemma 4 26B on an RTX Spark N1X, 8 different prompts, temperature 0:

Clients Pairs Distinct, median Mean read reduction
8 64 45 30.4%
4 32 28 12.5%

Timing gate_proj over only distinct experts in the live path gives about 1.95 ms against about 2.5 ms for 64 pairs. Gathers are about 54% of a layer, so a perfect grouped kernel saves about 13% aggregate at 8 clients. That omits the kernel's cost for the other rows, and 4 clients, the default slot count, would gain about 5% by the same ratio. The times use forced evals, so only the ratios are used, and that timing patch is not in this PR.

Corrections

  • perf(cuda): MoE batched decode reads top_k * B expert matrices per gather #2784's body and the fold record said everything before the gather takes at most 1.9 ms. That profile repeated one prompt across rows. With different prompts it is about 4.8 ms of about 13 ms, and the gathers about 7.1 ms. The record now says so.
  • scripts/bench_serving_concurrency.py sends one prompt to every client, so identical rows pick the same experts (87.5% read reduction here). The fold record now notes this. Shared-weight reads do not depend on prompt content.

Behavior

MLXCEL_PROFILE_MOE_INNER=1 also prints [MOE] name=distinct_experts tokens=T pairs=P distinct=D. It reads indices back to the host only under that variable.

Not measured

Other MoE models and GB10. gpt-oss 20B has E=32, so it can overlap more, but its mxfp4 gather takes a different kernel.

fmt, clippy on the lib with --features cuda, and the env var docs check pass locally.

Closes #2784

A kernel reading each selected expert once could cut Gemma 4 26B's gather reads by 30.4% at 8 clients with different prompts, and 12.5% at 4. Timing the live path over distinct experts bounds that at about 22% of a gather and about 13% aggregate at 8 clients, before a real kernel's cost for the other rows. That does not justify a variable-row kernel for one decode path, so none was built.

`MLXCEL_PROFILE_MOE_INNER=1` now prints the distinct-expert count so this can be repeated. The fold record's 1.9 ms attention figure came from repeated prompts; with different prompts it is about 4.8 ms, and the record now says so.

Closes #2784
@inureyes inureyes added status:done Completed type:performance Performance improvements priority:medium Medium priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers labels Oct 31, 2026
@inureyes
inureyes merged commit 999f9f6 into main Oct 31, 2026
33 checks passed
@inureyes
inureyes deleted the update/issue-2784-moe-gather-traffic branch October 31, 2026 12:31
@inureyes inureyes added this to the 0.8 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core mlxcel-core: MLX FFI, primitives, KV cache, layers priority:medium Medium priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(cuda): MoE batched decode reads top_k * B expert matrices per gather

1 participant