Skip to content

Share one unsorted expert dispatch across Gemma prompt and decode rows - #384

Merged
davidtai merged 3 commits into
mainfrom
feat/gemma4-batched-prompt-experts
Nov 3, 2026
Merged

davidtai merged 3 commits into
mainfrom
feat/gemma4-batched-prompt-experts

Conversation

@jonathan308

Copy link
Copy Markdown

Proposed changes

The registered Gemma prompt policy ran each prompt token's expert block as its own subgraph, about six kernel launches per token per layer, and batch decode did the same per lane. SwitchGLU.callAndWeightedReduceRows now projects all rows in one call. Pinned by Layr-Labs/d-inference#1704.

  • Every (row, expert) pair is still a native M=1 gathered product, and the size-8 top-K reduction keeps its one-row layout, so each row keeps the bytes of its own one-row callAndWeightedReduce call. Only the launch count changes.
  • 64 or more assignments are sorted by expert for weight locality, without the sorted hint, and rows return to their order before the reduction.
  • The call is package, not public, because Qwen4's tiled expert kernels choose a route by assignment count; its documentation names that exception.

Measured (from the commit messages; synthetic, not the full model): one expert layer at production geometry, 4-bit, 2,048 tokens 37.2 to 30.6 ms and 512 tokens 9.8 to 8.2 ms, on an M5 Ultra. Bytes equal the per-token oracle in Gemma4PrefillExpertTests on the CPU, and on the GPU at production geometry with GEMMA4_EXPERT_ROWS_GPU.

Conflict to expect: Libraries/MLXLMCommon/SwitchLayers.swift conflicts with feat/mimo-v26-tensor-parallel, where both change projectExperts' signature and the down projection call. Land this PR last, then refresh it from main with the resolution already in cb2fc27f, which keeps both parameters.

Not validated here: other GPU generations, full-model and pair runs.

Merge with a merge commit, not a squash. The d-inference PRs pin these exact commits, and a merge commit keeps them on main. Replaying the stack with squashes conflicts; with merge commits it is clean.

The order for all six is in the 0.10.7 list: feat/gemma4-tensor-parallel, feat/mimo-v26-tensor-parallel, feat/mimo-v26-tp-speculative, perf/qwen27-tp-row-exact, perf/qwen27-tp-prompt-wavefront, then feat/gemma4-batched-prompt-experts.

Together with the other five branches, this one is in integration/0.10.7-rehearsal (cb2fc27f), the SDK side of Layr-Labs/d-inference#1726.

Checklist

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

AI usage

  • I have read this PR description in full and approve it as my own, and it
    accurately describes the code changes.
  • AI usage disclosure: the commits and this description were written by Claude Code. The owner has allowed agents to
    open PRs on this fork for d-inference work, so this was opened from a branch that was already pushed; no code changed
    in opening it. It has had no human review.

🤖 Generated with Claude Code

jonathan308 and others added 3 commits October 31, 2026 22:16
The registered Gemma prompt policy (gemma4-bf16-token-expert-reduction-v1)
ran each prompt token's expert block as its own subgraph: about six kernel
launches per token per layer. Batch decode did the same per lane.

SwitchGLU.callAndWeightedReduceRows now projects all rows in one call with
their assignments kept in row order. Every (row, expert) pair is still a
native M=1 gathered product (gather_qmv: the kernel depends only on N, K,
bits and dtype), and the size-8 top-K reduction keeps its one-row
col_reduce_small layout (it depends only on the hidden width). Each row
therefore keeps the bytes of its own one-row callAndWeightedReduce call,
which is the existing contract. Only the launch count changes.

Evidence:
- CPU: the updated Gemma4PrefillExpertTests compare the shared call with
  the per-token oracle at 7 to 512 tokens and decode rows 2, 3 and 8, at
  4 and 8 bits. They also check that the shared call is unsorted and the
  rectangular path still sorts.
- GPU (M5 Ultra; opt-in GEMMA4_EXPERT_ROWS_GPU): the bytes are equal for
  1 to 2048 rows at a geometry with the production kernel classes. They
  are also equal at hidden 2816, width 704 and 128 experts.
- Synthetic timing of one expert block at production geometry (4-bit,
  debug build): 512 tokens 74.6 -> 9.8 ms; 2048 tokens 349.9 -> 36.8 ms.

Not validated here: other GPU generations, full-model or pair runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
callAndWeightedReduceRows now sorts 64 or more assignments by expert, so
consecutive threadgroups read the same expert's weights. It does not pass
the sorted hint to the gathers: every (row, expert) pair stays an M=1
gather_qmv product, rather than taking the sorted gather_qmm_rhs route.
Rows return to their order (scatterUnsort) before the unchanged top-K
reduction, so each row keeps the bytes of its own one-row call.

The unused sortsAssignments switch is replaced by hintsSorted. Calls
that hint sorted indices, and the MiMo NAX routes, are unchanged.

Evidence:
- Gemma4PrefillExpertTests on CPU: bytes equal; the one call carries every
  route, grouped by expert from 64 assignments, never hinted as sorted.
- GPU equality at the production kernel classes and at production
  geometry (M5 Ultra).
- Gemma4StagePagedSyntheticTests (GPU) and Gemma4BatchDecodeTests (CPU)
  pass.
- Synthetic production-geometry expert layer, 4-bit: 2,048 tokens
  37.2 -> 30.6 ms (per-token graph 369.5 ms); 512 tokens 9.8 -> 8.2 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- SwitchGLU.callAndWeightedReduceRows is package, not public: its only
  caller is Gemma4Experts. Its documentation names the profile its per-row
  guarantee does not cover: Qwen4's tiled expert kernels choose a route by
  assignment count.
- Gemma4Experts takes the shared call through one branch for registered
  prompt frames and independent decode rows (the two bodies were identical).
- Gemma4PrefillExpertTests checks a single row, and rows that all route to
  the same experts (every sort key tied), against one-row calls on the CPU.
  The opt-in GPU check adds tied rows at 9 and 513.

Validation (debug darkbloom-cluster harness with this test file linked in,
metallib 5b97893c from nested MLX 72dffa2d, M5 Ultra):
- CPU: Gemma4PrefillExpertTests, Gemma4BatchDecodeTests,
  Gemma4BatchArithmeticAgreementTests, GemmaPrefillArithmeticTests,
  GemmaPrefixChunkTests, ResidentAdmissionGemma4Tests and
  Gemma4StagePagedSyntheticTests (GPU): 29 tests pass.
- GEMMA4_EXPERT_ROWS_GPU=production: bytes equal at both geometries,
  4 and 8 bits, tied rows included.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jonathan308
jonathan308 marked this pull request as ready for review November 2, 2026 19:19
@jonathan308
jonathan308 requested a review from davidtai November 2, 2026 19:19
@davidtai
davidtai merged commit 79a5eed into main Nov 3, 2026
22 checks passed
@davidtai
davidtai deleted the feat/gemma4-batched-prompt-experts branch November 3, 2026 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants