Skip to content

[Megatron] feat: Support compatibility enhancements of vp_stage#5580

Open
ChibiQuest wants to merge 6 commits intoverl-project:mainfrom
ChibiQuest:fix/fix_vp_stage
Open

[Megatron] feat: Support compatibility enhancements of vp_stage#5580
ChibiQuest wants to merge 6 commits intoverl-project:mainfrom
ChibiQuest:fix/fix_vp_stage

Conversation

@ChibiQuest
Copy link
Contributor

@ChibiQuest ChibiQuest commented Mar 13, 2026

What does this PR do?

Add concise overview of what this PR aims to achieve or accomplish. Reference related GitHub issues and PRs that help with the review.

6

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, fully_async, one_step_off
    • 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.

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.

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 compatibility enhancements for vp_stage in Megatron, primarily by dynamically checking the signature of get_transformer_layer_offset. My review focuses on a critical bug and a performance issue in this implementation. The current code will raise a NameError due to a missing import and also introduces a performance bottleneck by repeatedly inspecting a function signature in a hot path. I've provided a suggestion to resolve both issues by performing the check once at module import time.

Comment on lines +334 to +340
sig = inspect.signature(get_transformer_layer_offset)

if "vp_stage" in sig.parameters:
offset = get_transformer_layer_offset(tf_config, vp_stage=vp_rank)
else:
offset = get_transformer_layer_offset(tf_config)

Copy link
Contributor

Choose a reason for hiding this comment

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

critical

This change introduces two issues:

  1. NameError: The inspect module is used without being imported, which will cause a runtime NameError.
  2. Performance: Calling inspect.signature() inside get_current_rank_layer_info is inefficient. This function is in a hot path (called per micro-batch), and the signature of get_transformer_layer_offset will not change at runtime. This check should be performed only once when the module is imported.

To fix this, please add import inspect at the top of the file and cache the result of the signature check in a module-level variable.

For example, at the top of verl/utils/megatron/router_replay_utils.py:

import inspect
from megatron.core.transformer.transformer_layer import get_transformer_layer_offset

_GET_TRANSFORMER_LAYER_OFFSET_HAS_VP_STAGE = "vp_stage" in inspect.signature(get_transformer_layer_offset).parameters

Then, you can use this cached variable here.

Suggested change
sig = inspect.signature(get_transformer_layer_offset)
if "vp_stage" in sig.parameters:
offset = get_transformer_layer_offset(tf_config, vp_stage=vp_rank)
else:
offset = get_transformer_layer_offset(tf_config)
if _GET_TRANSFORMER_LAYER_OFFSET_HAS_VP_STAGE:
offset = get_transformer_layer_offset(tf_config, vp_stage=vp_rank)
else:
offset = get_transformer_layer_offset(tf_config)

sig = inspect.signature(get_transformer_layer_offset)
if "vp_stage" in sig.parameters:
layer_offset = get_transformer_layer_offset(config, vp_stage=vp_stage, pp_rank=pp_rank)
else:
Copy link
Collaborator

Choose a reason for hiding this comment

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

pp_rank is not defined in TransformerConfig while megatron version == v0.12.1

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