Skip to content

[MoE] Add overridable hooks for TokenChoiceTopKRouter - #4609

Merged
jinsooihm merged 2 commits into
pytorch:mainfrom
jinsooihm:generalize-topkrouter
Sep 14, 2026
Merged

jinsooihm merged 2 commits into
pytorch:mainfrom
jinsooihm:generalize-topkrouter

Conversation

@jinsooihm

Copy link
Copy Markdown
Contributor

TokenChoiceTopKRouter is a module with a large forward.
Some models may want to override how the score is computed or how the normalization is done.
Extracting and exposing them as overridable functions _compute_scores and _normalize_topk_scores would make it easier.

Test Plan

  • pytest -q tests/unit_tests/cpu/test_moe.py tests/unit_tests/cpu/test_router_gate_linear.py

    • 8 passed
  • Targeted pre-commit checks for moe.py and test_moe.py

    • All passed
  • pyrefly check torchtitan/models/common/moe.py

    • 0 errors
  • Compared upstream/main (773e16e) against 5f7de40 using scripts/loss_compare.py, --debug.seed=42, --debug.deterministic, and 10 training steps.

    • Qwen3 MoE: loss and grad_norm bitwise identical for all 10 steps
    • DeepSeek V3: loss and grad_norm bitwise identical for all 10 steps
    • GPT-OSS: loss and grad_norm bitwise identical for all 10 steps
    • DeepSeek V4: loss and grad_norm bitwise identical for all 10 steps

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 11, 2026

@tianyu-l tianyu-l 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.

@jinsooihm
jinsooihm force-pushed the generalize-topkrouter branch from 5f7de40 to 1671e3d Compare September 14, 2026 19:35
@jinsooihm

Copy link
Copy Markdown
Contributor Author

@tianyu-l Updated after offline discussion.

@jinsooihm
jinsooihm requested a review from tianyu-l September 14, 2026 19:40

@tianyu-l tianyu-l 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.

please address remaining comments

Comment thread torchtitan/models/common/activation.py Outdated

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.

maybe

Suggested change
class BinaryActivationFn(Function[torch.Tensor], ABC):

gate_param_init: dict[str, Callable],
top_k: int = 1,
score_func: Literal["sigmoid", "softmax", "sqrtsoftplus"] = "sigmoid",
score_func: UnaryActivationFn.Config | None = None,

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.

would it ever be None?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

All models specify the function, only the tests call it without specifying which defaults to sigmoid. We can remove it.

Comment thread torchtitan/models/common/moe.py Outdated
gate: RouterGateLinear.Config
top_k: int = 1
score_func: Literal["softmax", "sigmoid", "sqrtsoftplus"] = "sigmoid"
score_func: UnaryActivationFn.Config = field(default_factory=Sigmoid.Config)

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.

can it not have a default?

@jinsooihm
jinsooihm merged commit e070842 into pytorch:main Sep 14, 2026
15 of 16 checks passed
acisseJZhong added a commit that referenced this pull request Sep 15, 2026
## Summary

- pass an explicit Sigmoid score function to the padding-mask router
tests
- align the tests added by #4594 with the required score_func API
introduced by #4609
- fix the two CPU test failures observed in the PR #2769 CI job

## Test plan

- pytest -q tests/unit_tests/cpu/test_moe.py
- pre-commit hooks for ufmt, flake8, pydoclint, and codespell on
tests/unit_tests/cpu/test_moe.py
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.

2 participants