Skip to content

refactor: remove gate normalization in DecoderUnit - #222

Merged
hzhangxyz merged 1 commit into
mainfrom
refactor/remove-gate-normalization
Mar 27, 2026
Merged

refactor: remove gate normalization in DecoderUnit#222
hzhangxyz merged 1 commit into
mainfrom
refactor/remove-gate-normalization

Conversation

@hzhangxyz

Copy link
Copy Markdown
Member

Simplify the gate computation by removing the normalization step. The gate values are now used directly from softmax scatter without the additional sum normalization.

Simplify the gate computation by removing the normalization step.
The gate values are now used directly from softmax scatter without
the additional sum normalization.
Copilot AI review requested due to automatic review settings March 27, 2026 02:18

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

Pull request overview

This PR updates the DeepSeekMoE-style routed feed-forward gating inside DecoderUnit by removing the post-topk renormalization step, so routed expert weights are taken directly from the original softmax probabilities at the selected indices.

Changes:

  • Removed gate_prime construction and the gate = gate_prime / gate_prime.sum(...) normalization.
  • Use a single gate tensor built via scatter_ of the softmax probabilities at top-k indices.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

# gate_prime, gate: batch * site * routed
gate_prime = torch.zeros_like(similarity).scatter_(-1, top_k_indices, similarity.gather(-1, top_k_indices))
gate = gate_prime / gate_prime.sum(dim=-1).unsqueeze(-1)
# gate, gate: batch * site * routed

Copilot AI Mar 27, 2026

Copy link

Choose a reason for hiding this comment

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

The inline comment is now inaccurate/typoed after removing gate_prime (it reads gate, gate: ...). Please update it to describe the single gate tensor to avoid confusion when reading the routing logic.

Suggested change
# gate, gate: batch * site * routed
# gate: batch * site * routed (top-k routed gating weights)

Copilot uses AI. Check for mistakes.
@hzhangxyz
hzhangxyz merged commit f572f51 into main Mar 27, 2026
12 checks passed
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