Skip to content

Add an opt-in route_norm flag for the legacy MoE gate - #8769

Open
Aravind-11 wants to merge 1 commit into
deepspeedai:masterfrom
Aravind-11:moe-optin-route-norm
Open

Aravind-11 wants to merge 1 commit into
deepspeedai:masterfrom
Aravind-11:moe-optin-route-norm

Conversation

@Aravind-11

Copy link
Copy Markdown

Draft. Please say whether this shape is welcome before review-for-merge. The measurement and the questions for maintainers are in #8768.

What

Legacy MoE / TopKGate gain an opt-in route_norm: Optional[bool] = None.

  • None keeps the historical combine weights. top1gating leaves the chosen expert's softmax probability. top2gating and topkgating rescale the kept weights so they sum to 1.
  • True rescales for every k. A kept top-1 weight becomes 1.
  • False leaves the raw softmax probabilities for every k.
  • The flag is applied on both the sparse return (MOELayer) and the dense return (direct callers and inference).
  • Auxiliary loss is computed before this step and does not change.
  • A dropped token stays 0, because 0 / eps is 0.
  • The argument is trailing and defaulted, so deepspeed/ops/transformer/inference/moe_inference.py still constructs TopKGate positionally and stays on the historical rule.

This is not expert_parallel.route_norm. That key configures the AutoEP router, which already has its own flag.

Why the default is not one bool

On 991ccf0f, the same logits give top-1 combine weights [0.710100, 0.870166, 0.378175, 0.799164] and topkgating(k=1) weights of all ones. The numbers and the setup are in #8768. Either unifying choice changes the MoE output of existing checkpoints. None is the default that preserves both rules. Router parameter tensors are unchanged; the layer output and the expert gradients change when the flag is set.

Tests

CPU, torch 2.5.1:

pytest tests/unit/v1/moe/test_moe.py -k "route_norm or capacity_exceeds or preserves_min_capacity or preserves_tensor_parallel"

10 passed, 37 deselected. The six new tests check:

  • omitting the flag, passing None, and passing the historical bool are bitwise equal, for k=1, k=2, and k=3, on the sparse and dense returns
  • explicit True / False changes only the combine weights; expert indices, capacity, the dispatch mask, and l_aux stay equal
  • top-1 default weights equal the chosen softmax probability, and route_norm=True makes a kept weight 1, including a dropped token staying 0
  • top-1 and topkgating(k=1) l_aux match, top-2 and top-k l_aux do not, and route_norm does not move either loss
  • MoE() and MoE(route_norm=None) match after a state-dict copy; MoE(k=1, route_norm=True) changes the output and not l_aux; MoE(k=2, route_norm=True) matches the default and route_norm=False does not
  • the positional inference TopKGate constructor leaves route_norm unset

The distributed classes in that file were not run. yapf (column limit 119) and flake8 are clean on the edited Python files.

Not in this diff

@tohtana is the code owner for deepspeed/moe/. The questions are in #8768. I will change the flag name or the default if you want a different shape, and I will close this if you would rather not take it.

top1gating leaves the chosen expert's softmax probability in the combine
weight. top2gating and topkgating rescale the kept weights so they sum to 1.
Making those agree changes the MoE output for existing checkpoints, so
route_norm defaults to None and each gate keeps its historical rule.
True or False overrides all three gates. Auxiliary loss is unchanged.

Signed-off-by: ARAVINDHAN T <arvindhant01@gmail.com>
@goransh-walia

Copy link
Copy Markdown

This looks good to me at a glance — thanks for putting it together.

@Aravind-11
Aravind-11 marked this pull request as ready for review October 8, 2026 22:11
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-08T22:14:54.942367Z 79df454 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 79df454fae

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread deepspeed/moe/layer.py
enable_expert_tensor_parallelism: bool = False,
top2_2nd_expert_sampling: bool = True) -> None:
top2_2nd_expert_sampling: bool = True,
route_norm: Optional[bool] = None) -> None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Add the required Signed-off-by trailer

Because this SHA has one parent, it is a non-merge commit, but git log --format='%b' b11e0a1924c934183ecfbe01e225c81a04a123af shows that its message has no Signed-off-by trailer. Recreate the commit with --signoff using the configured identity so that it satisfies the repository's commit/CI policy.

AGENTS.md reference: AGENTS.md:L6-L9

Useful? React with 👍 / 👎.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants