Skip to content

Fix gpt-oss implementation (MoE router gate bias + top‑k renorm) - #2319

Merged
tianyu-l merged 1 commit into
pytorch:mainfrom
linyuhongg:main
Feb 5, 2026
Merged

tianyu-l merged 1 commit into
pytorch:mainfrom
linyuhongg:main

Conversation

@linyuhongg

Copy link
Copy Markdown
Contributor

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.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.

@meta-cla

meta-cla Bot commented Feb 3, 2026

Copy link
Copy Markdown

Hi @linyuhongg!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

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!

@shuhuayu

shuhuayu commented Feb 4, 2026

Copy link
Copy Markdown
Contributor

Hi @linyuhongg, thank you for the pr. Could you sign the CLA so we can proceed with the merge?

@meta-cla

meta-cla Bot commented Feb 4, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Feb 4, 2026
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.
"""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

thanks, please add an definition of the new parameter in docstring

Comment thread torchtitan/models/gpt_oss/__init__.py Outdated
route_norm=False,
route_norm=True,
route_scale=1.0,
route_gate_bias=True,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: probably just name it gate_bias, seems route_* are related to operation on scores

@wwwjn wwwjn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the fix, LGTM

@tianyu-l
tianyu-l merged commit 41753c6 into pytorch:main Feb 5, 2026
10 of 11 checks passed
TXacs pushed a commit to McmillanTAC/torchtitan that referenced this pull request Apr 13, 2026
…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.
ACharacterInASimulation pushed a commit to ACharacterInASimulation/torchtitan that referenced this pull request Apr 21, 2026
…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.
wenxie-amd pushed a commit to AMD-AGI/Primus that referenced this pull request Sep 22, 2026
## 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GPT-OSS MoE's implementation should use route_norm=True

4 participants