Repository navigation
Fix gpt-oss implementation (MoE router gate bias + top‑k renorm) - #2319
Conversation
|
Hi @linyuhongg! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn 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 If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
|
Hi @linyuhongg, thank you for the pr. Could you sign the CLA so we can proceed with the merge? |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
| score_func (Literal["softmax", "sigmoid"]): Whether to use sigmoid or softmax for router scores. | ||
| route_norm (bool): Whether to normalize the routing scores when using sigmoid. | ||
| route_scale (float): Scaling factor applied to the routing scores. | ||
| """ |
There was a problem hiding this comment.
thanks, please add an definition of the new parameter in docstring
| route_norm=False, | ||
| route_norm=True, | ||
| route_scale=1.0, | ||
| route_gate_bias=True, |
There was a problem hiding this comment.
nit: probably just name it gate_bias, seems route_* are related to operation on scores
…orch#2319) Fixes pytorch#2258. The official gpt-oss MoE router uses a biased gate projection and computes routing weights as a softmax over the selected top‑k logits (so the selected weights sum to 1). Torchtitan’s gpt_oss configs previously used a bias-less router gate and did not renormalize the selected top‑k scores, which diverges from the reference implementation and makes `mlp.router.bias` checkpoint mapping ineffective. Changes: - Add `MoEArgs.route_gate_bias` and plumb it into `TokenChoiceTopKRouter` (`nn.Linear(..., bias=route_gate_bias)`). - Enable `route_gate_bias=True` and `route_norm=True` in all gpt_oss model configs to match the reference behavior. - With `score_func=softmax`, `route_norm=True` is equivalent to doing softmax over the selected top‑k logits.
…orch#2319) Fixes pytorch#2258. The official gpt-oss MoE router uses a biased gate projection and computes routing weights as a softmax over the selected top‑k logits (so the selected weights sum to 1). Torchtitan’s gpt_oss configs previously used a bias-less router gate and did not renormalize the selected top‑k scores, which diverges from the reference implementation and makes `mlp.router.bias` checkpoint mapping ineffective. Changes: - Add `MoEArgs.route_gate_bias` and plumb it into `TokenChoiceTopKRouter` (`nn.Linear(..., bias=route_gate_bias)`). - Enable `route_gate_bias=True` and `route_norm=True` in all gpt_oss model configs to match the reference behavior. - With `score_func=softmax`, `route_norm=True` is equivalent to doing softmax over the selected top‑k logits.
## Summary - add a TorchTitan `0.2.2` setup patch that initializes the GPT-OSS MoE router gate bias to zero after meta-device materialization - scope the workaround to `model.name=gpt_oss` and make it idempotent for long-lived processes - add regression coverage for `meta -> to_empty -> init_weights`, pre-fix non-finite router scores, finite post-fix scores, patch idempotence, model scoping, and the `0.2.2` version gate ## Root cause TorchTitan added a biased GPT-OSS router in upstream PR pytorch/torchtitan#2319, but `TokenChoiceTopKRouter.init_weights()` initializes only `gate.weight`. After TorchTitan builds on the meta device and materializes with `to_empty()`, `gate.bias` contains allocator data and can include NaN/Inf. The first router softmax then makes the training loss and gradient norm non-finite. This is specific to the pinned TorchTitan `v0.2.2`; upstream `v0.3.0` replaced this initialization path with the new Module/reset-parameters framework. ## Deterministic reproducer Failing TorchTitan seed: `10495565843751150266` Pure upstream TorchTitan command path (`torchrun -m torchtitan.train`), GPT-OSS 20B / EP8 / sequence length 4096: - before: `loss=nan`, `grad_norm=nan` - after zero-initializing router bias: `loss=12.67191`, `grad_norm=10.1298` The failing bias was captured before the router projection and contained explicit NaNs and uninitialized extreme values; no Primus launcher or patch was involved in the reproducer. ## Numerical behavior This intentionally changes initialization from undefined, allocator-dependent bias values to a deterministic zero bias. It is not bitwise/numerically equivalent to the broken path because that path has no valid baseline. Zero introduces no static expert preference; random router weights still break symmetry, and checkpoint loading overwrites the initialized bias. ## Validation - the unit regression explicitly shows that upstream initialization preserves a NaN bias sentinel and produces non-finite softmax scores, while the patched path zeroes the bias and produces finite scores - `pytest -q tests/unit_tests/backends/torchtitan/test_gptoss_router_bias_patch.py tests/unit_tests/backends/torchtitan/test_gpt_oss_sink_attention.py` — 10 passed - `ruff check` and `ruff format --check` on the changed Python files
Fixes #2258.
The official gpt-oss MoE router uses a biased gate projection and computes routing weights as a softmax over the selected top‑k logits (so the selected weights sum to 1). Torchtitan’s gpt_oss configs previously used a bias-less router gate and did not renormalize the selected top‑k scores, which diverges from the reference implementation and makes
mlp.router.biascheckpoint mapping ineffective.Changes:
MoEArgs.route_gate_biasand plumb it intoTokenChoiceTopKRouter(nn.Linear(..., bias=route_gate_bias)).route_gate_bias=Trueandroute_norm=Truein all gpt_oss model configs to match the reference behavior.score_func=softmax,route_norm=Trueis equivalent to doing softmax over the selected top‑k logits.