Skip to content

Conversation

@HollowMan6
Copy link
Collaborator

@HollowMan6 HollowMan6 commented Jan 24, 2026

What does this PR do?

DeepSeek-V3-style MoE employs a hybrid architecture with the first three layers as dense FFN blocks before switching to MoE layers, which means not every layer has a router.

This PR fixes DeepSeek V3 architecture for router replay R3, as vLLM reports routed_experts across all transformer layers (including dense). Megatron only has routers for MoE layers. Mapping with i + offset silently shifts every MoE layer after a dense layer. So, when routed‑experts tensors include dense layers (full num_layers), we should map replay data by each router’s global layer_number; Otherwise, we should fall back to local offset indexing and validate bounds to catch mismatches. We also patch TopKRouter.set_layer_number to store the global layer number in each RouterReplay instance so global alignment is reliable with VPP/PP.

Dependent on vllm-project/vllm#33013

Checklist Before Starting

  • Search for similar PRs. Paste at least one query link here: ...
  • Format the PR title as [{modules}] {type}: {description} (This will be checked by the CI)
    • {modules} include fsdp, megatron, veomni, sglang, vllm, rollout, trainer, ci, training_utils, recipe, hardware, deployment, ray, worker, single_controller, misc, perf, model, algo, env, tool, ckpt, doc, data, cfg, reward
    • If this PR involves multiple modules, separate them with , like [megatron, fsdp, doc]
    • {type} is in feat, fix, refactor, chore, test
    • If this PR breaks any API (CLI arguments, config, function signature, etc.), add [BREAKING] to the beginning of the title.
    • Example: [BREAKING][fsdp, megatron] feat: dynamic batching

Test

For changes that can not be tested by CI (e.g., algorithm implementation, new model support), validate by experiment(s) and show results like training curve plots, evaluation results, etc.

Without this fix:
image

With this fix, it looks good now:
image

API and Usage Example

Demonstrate how the API changes if any, and provide usage example(s) if possible.

# Add code snippet or script demonstrating how to use this

Design & Code Changes

Demonstrate the high-level design if this PR is complex, and list the specific changes.

Checklist Before Submitting

Important

Please check all the following items before requesting a review, otherwise the reviewer might deprioritize this PR for review.

✨ Presented to you with Mind Lab - A Lab for Experiential Intelligence.

Copilot AI review requested due to automatic review settings January 24, 2026 21:08
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly addresses an issue in the router replay logic by aligning replay data with global layer indices when appropriate. The change introduces a conditional logic to use the global layer_number from router instances and falls back to local offset indexing otherwise, which aligns with the stated goal. The addition of a bounds check for the layer index is a good defensive measure. I've included one suggestion to make the logic more robust by failing fast if a router is missing the layer_number attribute when global indexing is expected, which would prevent silent errors from potential misconfigurations.

Copy link
Contributor

Copilot AI left a comment

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 adjusts how router replay (R3) maps recorded routing indices back to router instances in Megatron, aiming to use global layer indices when routed‑experts tensors span all transformer layers and otherwise fall back to local offsets. The goal is to correctly align replay data for architectures like DeepSeek V3 where routers and layers may not map 1:1 by simple local offsets.

Changes:

  • Compute num_layers_in_data from the replay tensor and detect whether it matches tf_config.num_layers to decide between global and local layer indexing.
  • For each router instance, determine a layer_idx either from a layer_number attribute (global index path) or from the original i + offset scheme (local index path).
  • Add explicit bounds checking on layer_idx and raise a ValueError if the computed index is outside the replay data’s layer dimension.

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

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a fix for router replay in the DeepSeek V3 architecture, aligning router replay data with global layer indices when the data encompasses all layers, and correctly falling back to local offset indexing otherwise. The changes involve patching TopKRouter to store the global layer index and updating the data loading logic in set_router_replay_data to use this global index when appropriate. The implementation is sound, includes necessary fallbacks for robustness, and adds a validation check for layer indices. The code appears to correctly address the issue described, and I did not find any issues of high or critical severity.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


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

…data with global layer indices

This PR fixes DeepSeek V3 architecture for router replay R3, as when
routed‑experts tensors include dense layers (full `num_layers`), we should
map replay data by each router’s global layer_number;
Otherwise, we should fall back to local offset indexing and
validate bounds to catch mismatches.

Signed-off-by: Hollow Man <hollowman@opensuse.org>
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.

1 participant