[MoE] Fix fp32 crash in token combine (scatter_add dtype mismatch)
torch._grouped_mm only supports bfloat16, so the grouped experts always return bf16 outputs regardless of model dtype -- but LocalTokenDispatcher.combine / AllToAllTokenDispatcher.combine allocate the output buffer with torch.zeros_like(x_TD) (the model dtype). At a non-bf16 dtype (e.g. float32), deterministic_scatter_add then gets out in fp32 and src in bf16 and raises scatter(): Expected self.dtype to be equal to src.dtype. This is why the GptOss GraphTrainer trace tests (which run at float32) were skipped.
Fix: upcast the routed output to the buffer dtype (routed_output_RD.to(out_TD.dtype)) right before the scatter in both combine paths. It's a no-op in bf16 (no change to existing numerics or bitwise-determinism hashes) and a lossless upcast otherwise. Living in models/common/token_dispatcher.py, it covers all MoE models (gpt_oss, deepseek_v3, qwen3, llama4). Re-enables TestTraceModels.test_gpt_oss and TestTraceFSDP.test_gpt_oss_fsdp.
Testing:
- Verified on H100 against the real
deterministic_scatter_add: without=fp32/src=bf16it reproduces the originalscatter(): Expected self.dtype...error; after.to(out.dtype)it succeeds with correct fp32 output; andbf16.to(bf16)returns the same tensor, confirming the bf16 path is untouched (no-op). - Full
gpt_osstrace tests (pytest torchtitan/experiments/graph_trainer/tests/test_trace_module.py -k gpt_oss) plus the bitwise-determinism guardrail are covered by CI.
The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:
ciflow/8gpu
Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.
@ZYInSea has imported this pull request. If you are a Meta employee, you can view this in D107854096.
lgtm, some final comments
@tianyu-l @kashif @xmfan sorry that I accidentally closed the PR and previous approval got reset, updated comments per request, can any of you take a look and approve again?
still error on graph trainer
still error on graph trainer
From the failing CI result:
=========================== short test summary info ============================
FAILED torchtitan/experiments/graph_trainer/tests/test_passes.py::TestCustomCodegenPass::test_custom_codegen_with_profiler_and_hot_reload - AssertionError: False is not true : File modification should be detected
To execute this test, run the following from the base repo dir:
python test_custom_codegen.py TestCustomCodegenPass.test_custom_codegen_with_profiler_and_hot_reload
This message can be suppressed by setting PYTORCH_PRINT_REPRO_ON_FAILURE=0
======= 1 failed, 93 passed, 1 skipped, 17 warnings in 107.91s (0:01:47) =======
tested locally using provided comand from base folder:
python -m pytest
"torchtitan/experiments/graph_trainer/tests/test_custom_codegen.py::TestCustomCodegenPass::test_custom_codegen_with_profiler_and_hot_reload"
result shows it passed locally, seems to me its a flacky pipeline issue. submitted a empty commit to trigger a new run
oops same error? cc @SherlockNoMad if you have insights.
Hi @ZYInSea!
Thank you for your pull request.
We require contributors to sign our Contributor License Agreement, and yours needs attention.
You currently have a record in our system, but the CLA is no longer valid, and will need to be resubmitted.
Process
In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.
Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.
If you have received this in error or have any questions, please contact us at [email protected]. Thanks!