Skip to content

[Docs] Add Mixture of Experts guide - #3494

Open
pggPL wants to merge 56 commits into
NVIDIA:mainfrom
pggPL:docs_moe
Open

[Docs] Add Mixture of Experts guide#3494
pggPL wants to merge 56 commits into
NVIDIA:mainfrom
pggPL:docs_moe

Conversation

@pggPL

@pggPL pggPL commented Sep 7, 2026

Copy link
Copy Markdown
Collaborator

Adds a Mixture of Experts guide covering routing, token permutation, grouped expert computation, and expert parallelism.

Includes concise PyTorch and JAX examples.

pggPL and others added 30 commits May 4, 2026 15:00
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…PI entries

- Add code snippets and SVG figures referenced by mixture_of_experts.rst
  (moe_permute / moe_unpermute / grouped_linear tabbed examples for both
  PyTorch and JAX)
- Add JAX API reference entries for token_dispatch, token_combine and
  grouped_dense so the cross-references from the MoE page resolve
- Make wording framework-neutral where it was PyTorch-only (Grouped GEMM
  instead of GroupedLinear/grouped linear in shared sections, both
  m_splits and group_sizes mentioned, figure labels generalized)
- Tighten routing-kernel intro: consolidate the redundant "multiple
  variants exist / see API ref" notes into one paragraph next to the
  example, and explicitly state that the kernels are differentiable
- Sharpen merging_probs explanation (top-1 vs top-k) and explicitly
  describe what token_dispatch / token_combine return
- Snippet cleanups: define previously undefined symbols, drop the JAX
  probs= argument from the basic example and explain its purpose in a
  comment, document the ignored permuted_probs / pad_offsets outputs
- Reorder MoE entry in the docs/index.rst toctree

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
…ecision

Add Router (score function + top-k + load-balancing loss) and Putting-it-together
sections, plus token-probabilities / padding-and-alignment / chunk-sort subsections
and a fused-expert-MLP note. New SVG figures and PyTorch/JAX snippets. Add the
router and moe_permute_and_pad_with_probs API reference entries (PyTorch and JAX)
and sort_chunks_by_index (JAX).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…ument EP APIs

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…iagram

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…MM, grouped MLP, EP

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
… in introduction

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…e introduction

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…section

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…mework specifics to snippets

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…P conditions

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…LP figure

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…w layer figure

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…ections

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…-framework API)

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…, MXFP8 dispatch, shared experts

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
pggPL and others added 16 commits September 7, 2026 18:09
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…transfer path)

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…p placement footer

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…zero-copy vs CUDA graphs

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…viour

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
@pggPL pggPL added the documentation Improvements or additions to documentation label Sep 8, 2026
…th -W

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>

# Conflicts:
#	docs/_static/css/diagram-colors.css
@pggPL
pggPL marked this pull request as ready for review September 8, 2026 10:48
@greptile-apps

greptile-apps Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no actionable defects remain, and the prior expert-parallel masking finding was manually resolved after the example was corrected.

Summary

  • Documents routing, load balancing, token permutation, grouped expert computation, and expert parallelism.
  • Adds concise framework-specific examples and diagrams, including dark-theme styling.
  • Improves public router and JAX MoE docstring formatting.
  • Revises the grouped-MLP explanation to distinguish fused FC1 activation from the separate FC2 grouped GEMM.
  • Corrects the PyTorch expert-parallel example by clearing unused receive-buffer tail rows before expert computation and combine.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  T[Input tokens] --> R[Router and top-k]
  R --> D{Expert placement}
  D -->|Single device| P[Token permutation]
  D -->|Expert parallel| EP[All-to-all dispatch]
  P --> G[Grouped expert MLP]
  EP --> G
  G --> C{Combine path}
  C -->|Single device| U[Token unpermute and weighted combine]
  C -->|Expert parallel| EC[All-to-all combine]
  U --> O[MoE output]
  EC --> O
Loading

Comment thread docs/features/mixture_of_experts/moe_expert_parallel_pytorch.py
…P snippet

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
@pggPL

pggPL commented Sep 10, 2026

Copy link
Copy Markdown
Collaborator Author

Hi @vthumbe1503 @phu0ngng this is PR with docs for MoE operations and EP. Can you have a look?

)

# Transformer Engine: one grouped dense call. group_sizes is a device array.
# On Blackwell, BF16 and MXFP8 inputs without bias run as a single grouped GEMM

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small update, Hopper BF16 is now supported since this PR merged yesterday: #3083

# On Blackwell, BF16 and MXFP8 inputs without bias run as a single grouped GEMM
# with the group sizes kept on the device; other cases launch one GEMM per
# expert and copy group_sizes to the host first.
grouped_out = te_dense.grouped_dense(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pggPL How do we want to handle group alignment in the docs? This does support MXFP8 but we require group sizes to be aligned to a multiple of 128 for MXFP8, so if a user passes in un-aligned groups we can get IMA, illegal instruction, or incorrect results

For now, I've been guiding users to our more monolithic MoE block to avoid this complexity. But the same constraints apply to TE/PyTorch, so if you've found a better way to explain the nuances of this alignment, let me know and I'm open to adding it.

The on-device group size alignment is difficult since we can't assert it on Host without introducing runtime overhead

)

# 3. Experts: one grouped call over all expert token blocks.
expert_out = te_dense.grouped_dense(permuted, expert_weights, group_sizes=group_sizes)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment, I'd prefer guiding users to our monolithic MoE layer here:

It's simpler and handles things like group size alignment automatically and hides it from the user. We do also want to highlight the lower-level APIs like this at some point in the future, but when we do so we need to significantly mark all the caveats like group size alignment and other constraints, which may be changing as we add new fused kernel support. Additionally, te_permutation.token_dispatch/combine are only recommended for non-EP, with EP enabled the TE EP APIs should be used. This is handled automatically by the TE MoEBlock, so my preference is to guide users to this monolithic MoE block as a starting point rather than these lower-level APIs

If Phuong wants to include a dedicated doc on TE EP APIs specifically, I'm okay with that lower-level API being highlighted. For other things, like grouped GEMM, I'd prefer to hide the alignment complexity in the TE MoEBlock and limit usage of lower-level APIs like grouped_dense to only users who are not doing a standard MoE architecture and are okay with handling the alignment constraints.

x_by_expert = jnp.split(x, split_indices, axis=0)

# Baseline: one matmul per expert.
loop_out = jnp.concatenate(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding JAX documentation as well @pggPL! 🙌

I've reviewed and left a few comments. Let me know what you think. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants