Add examples for MoE models - Mixtral in TE#2642
Conversation
Greptile SummaryThis PR adds a complete Transformer Engine tutorial for HuggingFace Mixtral (MoE), covering BF16 GroupedLinear, FP8, and MXFP8 expert-parallel fine-tuning, and addresses many previously flagged issues (wrong
Confidence Score: 4/5Close to mergeable — the major routing, weight-loading, and distributed-training correctness issues from earlier rounds are all addressed. The critical dispatch bugs (map_type, m_splits, deprecated probs kwarg, wrong num_out_tokens) are fixed, the DP sampler and gradient sync look correct, and dependency gaps are closed. Remaining findings are quality and performance suggestions that do not break correctness. utils.py (dead total_loss accumulator, foreach=False optimizer regression for EP>1); te_mixtral.py (duplicated sort-index helper vs te_moe_dispatch.py) Important Files Changed
Sequence DiagramsequenceDiagram
participant HF as HF Mixtral weights
participant INIT as init_te_mixtral_model
participant MODEL as TEMixtralForCausalLM
participant DISP as AllToAllTokenDispatcher
participant TE as TE GroupedLinear
INIT->>HF: "from_pretrained device=cpu"
INIT->>MODEL: ForCausalLM(te_config).to(cuda)
INIT->>MODEL: replace_params(hf_sd, te_sd)
INIT->>MODEL: "load_state_dict strict=False"
INIT->>MODEL: set_ep_groups(ep_group)
MODEL->>DISP: set_ep_group(ep_group)
Note over MODEL,DISP: Forward pass training
MODEL->>DISP: dispatch(hidden, selected_experts, weights)
DISP->>DISP: "moe_permute map_type=index"
DISP-->>DISP: "all_to_all_single EP>1"
DISP->>DISP: _build_expert_sort_indices
DISP-->>MODEL: DispatchOutput
MODEL->>TE: _experts_ffn_op(expert_input, split_sizes)
TE-->>MODEL: expert_output
MODEL->>DISP: combine(expert_output, handle)
DISP-->>DISP: "reverse all_to_all EP>1"
DISP->>DISP: "moe_unpermute map_type=index"
DISP-->>MODEL: combined output N H
Reviews (52): Last reviewed commit: "fix none mask" | Re-trigger Greptile |
|
Hi @sudhakarsingh27 can you check if this addresses your comments? Tested in 2x H100. |
Agreed! I think any example should show some perf gain and include the whole weight mapping so the user can run the example. |
|
Documentation build is not working, if you fix it please ping me and I'll review. |
d7301a6 to
fcc7e35
Compare
72e0e45 to
226173a
Compare
c3a50c1 to
590814a
Compare
|
Want your agent to iterate on Greptile's feedback? Try greploops. |
|
Hi @pggPL I've fixed the doc build and refined the content - can you help check? |
|
I was not reviewing it in detail, I would like to discuss on high-level flow of the tutorial first.
Apart of that it would be nice to prepare documentation (not tutorial) explaining why grouped linear is needed and which permute kernels we provide. I will work on that in different PR and it will land in Features section in our docs. |
| if isinstance(past_key_values, InferenceParams): | ||
| # input_ids is None when the caller supplies inputs_embeds directly. | ||
| _ref = input_ids if input_ids is not None else inputs_embeds | ||
| lengths = ( | ||
| attention_mask.sum(dim=1).tolist() | ||
| if attention_mask.shape[:2] == _ref.shape[:2] | ||
| else [1] * _ref.shape[0] | ||
| ) | ||
| past_key_values.pre_step(OrderedDict(zip(list(range(len(lengths))), lengths))) |
There was a problem hiding this comment.
attention_mask is None crash inside InferenceParams branch
attention_mask is an optional parameter with no None-guard before line 852. In the decode step of model.generate() the mask is frequently omitted, and attention_mask.shape[:2] will raise AttributeError: 'NoneType' object has no attribute 'shape', crashing every auto-regressive generation call.
if isinstance(past_key_values, InferenceParams):
_ref = input_ids if input_ids is not None else inputs_embeds
lengths = (
attention_mask.sum(dim=1).tolist()
if attention_mask is not None and attention_mask.shape[:2] == _ref.shape[:2]
else [1] * _ref.shape[0]
)
past_key_values.pre_step(OrderedDict(zip(list(range(len(lengths))), lengths)))
jberchtold-nvidia
left a comment
There was a problem hiding this comment.
Thanks! Rebase is now fixed
It is my own review and requested changes were resolved. However, the PR itself contains PyTorch usage so I should not give full approval on the PR
Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com>
Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com>
Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com>
Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com>
Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com>
Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com>
Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com>
Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com>
Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com>
Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com>
Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com>
Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com>
Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com>
Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com>
* rebase and add mixtral moe example Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * edit tutorial wording and remove nvtx profiling markers Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Address feedback for fused MLP and make table consistent Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * add moe description and code snippet Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix image header and grammar Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * add top2 expert in diagram, add perf verdict, and code highlight Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix grammar and code highlighting Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * add here is the expected output Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * add code highlight to mxfp8 and callout note Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * change nv mixtral to te Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * rename nv to te in all code Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * add elif Co-authored-by: Sudhakar Singh <sudhakars@nvidia.com> Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix Dtensor and address tiers Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix d tensor Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix ordering of weights Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix backtick and hyperlink Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix backtick and hyperlink 2nd Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * remove dtensor Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix none mask Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> --------- Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sudhakar Singh <sudhakars@nvidia.com> Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
* rebase and add mixtral moe example Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * edit tutorial wording and remove nvtx profiling markers Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Address feedback for fused MLP and make table consistent Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * add moe description and code snippet Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix image header and grammar Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * add top2 expert in diagram, add perf verdict, and code highlight Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix grammar and code highlighting Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * add here is the expected output Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * add code highlight to mxfp8 and callout note Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * change nv mixtral to te Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * rename nv to te in all code Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * add elif Co-authored-by: Sudhakar Singh <sudhakars@nvidia.com> Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix Dtensor and address tiers Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix d tensor Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix ordering of weights Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix backtick and hyperlink Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix backtick and hyperlink 2nd Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * remove dtensor Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix none mask Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> --------- Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sudhakar Singh <sudhakars@nvidia.com> Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> Signed-off-by: yangfan.bai <yangfan.bai@shopee.com>
* rebase and add mixtral moe example Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * edit tutorial wording and remove nvtx profiling markers Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Address feedback for fused MLP and make table consistent Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * add moe description and code snippet Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix image header and grammar Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * add top2 expert in diagram, add perf verdict, and code highlight Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix grammar and code highlighting Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * add here is the expected output Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * add code highlight to mxfp8 and callout note Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * change nv mixtral to te Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * rename nv to te in all code Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * add elif Co-authored-by: Sudhakar Singh <sudhakars@nvidia.com> Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix Dtensor and address tiers Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix d tensor Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix ordering of weights Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix backtick and hyperlink Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix backtick and hyperlink 2nd Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * remove dtensor Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> * fix none mask Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> --------- Signed-off-by: Faradawn Yang <73060648+faradawn@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sudhakar Singh <sudhakars@nvidia.com> Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> Signed-off-by: yangfan.bai <yangfan.bai@shopee.com>
Summary
This PR adds a complete tutorial for integrating HuggingFace Mixtral (MoE) with Transformer Engine, addressing the gap identified in #2573.
What's included
te_mixtral.py— Drop-inTEMixtralSparseMoeBlockthat replaces HF's loop-over-experts with TE'sGroupedLinear(batched GEMM) +moe_permute/moe_unpermute. Includesreplace_moe_blockcontext manager,TEMixtralForCausalLMwith HF weight loading, andreplace_paramsfor expert weight packing.utils.py— Data loading, BF16/FP8 model init, Accelerate wrapping, fine-tuning loop — mirrorste_llama/utils.pystyle.requirements.txt— Pinned dependencies matching the Llama/Gemma tutorials.te_llamaandte_gemma, covering:Bug fix
Corrected the
m_splitscalculation flagged by the automated review:Scope
Covers all topics requested in #2573: