Skip to content

[compat] MTP compat megatron-core main branch / dev branch#127

Merged
Jintao-Huang merged 5 commits into
modelscope:mainfrom
Jintao-Huang:mtp_compat_main_dev
Jun 17, 2026
Merged

[compat] MTP compat megatron-core main branch / dev branch#127
Jintao-Huang merged 5 commits into
modelscope:mainfrom
Jintao-Huang:mtp_compat_main_dev

Conversation

@Jintao-Huang

@Jintao-Huang Jintao-Huang commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request adds compatibility support for Megatron Core (mcore) version 0.19.0rc0 and above by conditionally handling different logging signatures of MTPLossLoggingHelper. The review feedback suggests optimizing performance by moving the import of _compute_mtp_acceptance_counts to the module level, avoiding repeated import overhead inside the training loop.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread src/mcore_bridge/model/gpt_model.py
Comment thread src/mcore_bridge/model/gpt_model.py Outdated
@Jintao-Huang

Copy link
Copy Markdown
Collaborator Author

/gemini review

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request adds compatibility support for Megatron-Core (mcore) version 0.19.0rc0 and above in gpt_model.py. It conditionally uses MTPLossLoggingHelper.save_metrics_to_tracker and computes multi-token prediction acceptance counts if the helper method is available, falling back to the original save_loss_to_tracker for older versions. The feedback suggests moving the inline import of _compute_mtp_acceptance_counts to the module level using a try...except block to avoid repeated import overhead inside the training loop and to adhere to PEP 8 guidelines.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

logger = get_logger()

mcore_016 = version.parse(megatron.core.__version__) >= version.parse('0.16.0rc0')
mcore_019 = version.parse(megatron.core.__version__) >= version.parse('0.19.0rc0')

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

To avoid importing _compute_mtp_acceptance_counts repeatedly inside the training loop, we can conditionally import it at the module level using a try...except ImportError block. This adheres to PEP 8 guidelines which recommend placing imports at the top of the file.

mcore_019 = version.parse(megatron.core.__version__) >= version.parse('0.19.0rc0')

try:
    from megatron.core.transformer.multi_token_prediction import _compute_mtp_acceptance_counts
except ImportError:
    _compute_mtp_acceptance_counts = None
References
  1. PEP 8 recommends placing imports at the top of the file, just after any module comments and docstrings, and before module globals and constants. (link)

Comment on lines +512 to +523
if hasattr(MTPLossLoggingHelper, 'save_metrics_to_tracker'):
# mcore >= 0.19 main branch: save_metrics_to_tracker with correct/total
from megatron.core.transformer.multi_token_prediction import _compute_mtp_acceptance_counts
correct, total = _compute_mtp_acceptance_counts(
mtp_logits, mtp_labels, loss_mask_, output_layer=None, runtime_gather_output=True)
MTPLossLoggingHelper.save_metrics_to_tracker(
mtp_loss_for_log,
correct,
total,
mtp_layer_number,
self.config.mtp_unroll_steps,
avg_group=avg_group)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Importing _compute_mtp_acceptance_counts inside the training loop (which runs mtp_unroll_steps times per forward pass) introduces unnecessary overhead. By using the module-level imported function, we can avoid this overhead and improve performance.

                    if hasattr(MTPLossLoggingHelper, 'save_metrics_to_tracker') and _compute_mtp_acceptance_counts is not None:
                        # mcore >= 0.19 main branch: save_metrics_to_tracker with correct/total
                        correct, total = _compute_mtp_acceptance_counts(
                            mtp_logits, mtp_labels, loss_mask_, output_layer=None, runtime_gather_output=True)
                        MTPLossLoggingHelper.save_metrics_to_tracker(
                            mtp_loss_for_log,
                            correct,
                            total,
                            mtp_layer_number,
                            self.config.mtp_unroll_steps,
                            avg_group=avg_group)

@Jintao-Huang Jintao-Huang merged commit 79a99b8 into modelscope:main Jun 17, 2026
1 check 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