[fix] bypass r3 for mtp layer. - #619
Conversation
Summary of ChangesHello @guapisolo, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a bug concerning the interaction between Multi-Token Prediction (MTP) layers and Rollout Routing Replay (R3) by ensuring that MTP layers do not utilize the R3 mechanism. The changes involve introducing a specific flag to identify MTP layers within the Mixture of Experts (MoE) routing logic, allowing for conditional bypassing of R3. Additionally, a new script for the GLM-4.7-Flash model demonstrates the application of these features, including advanced speculative decoding techniques. Highlights
Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
8577af9 to
dd7d515
Compare
There was a problem hiding this comment.
Code Review
This pull request aims to fix a bug where Multi-Token Prediction (MTP) layers were incorrectly interacting with the rollout routing replay (r3) feature, by introducing an is_mtp flag in MoE layers. However, a critical security vulnerability has been identified: the new script scripts/run_glm47_flash.py introduces several command injection vulnerabilities due to unsafe interpolation of command-line arguments. Additionally, there are architectural issues in megatron-core with a direct dependency on the miles project, violating dependency inversion principles, and a robustness issue in miles where a global flag is modified without a try...finally block, which could leave the system in an inconsistent state.
I am having trouble creating individual review comments. Click here to see my feedback.
scripts/run_glm47_flash.py (26)
The script scripts/run_glm47_flash.py is vulnerable to command injection. Several command-line arguments (args.model_dir, args.data_dir, args.model_org, args.model_name) are directly interpolated into shell command strings that are then executed via U.exec_command. Since these arguments are not sanitized or escaped, an attacker who can control these arguments can execute arbitrary shell commands on the system.
For example, if args.model_dir is set to ; rm -rf / ;, the command executed will be mkdir -p ; rm -rf / ; /root/datasets.
To remediate this, use shlex.quote() to escape command-line arguments before interpolating them into shell command strings, or better yet, use subprocess.run() with a list of arguments instead of a shell string.
scripts/run_glm47_flash.py (31-34)
Similar to the issue on line 26, the args.model_org, args.model_name, and args.model_dir arguments are interpolated into a shell command string without sanitization, leading to a command injection vulnerability.
scripts/run_glm47_flash.py (165-171)
The args.extra_args argument is directly interpolated into train_args, which is then executed as a shell command in U.execute_train. This allows for arbitrary command injection if the user provides malicious input in extra_args.
docker/patch/dev/megatron.patch (545-549)
Importing from miles within megatron-core creates a problematic dependency inversion. The core library (megatron-core) should not depend on an application-level project (miles) that uses it. This makes the core library less reusable and harder to maintain.
Consider refactoring this to use a plugin or hook system, where miles can register its custom logic with megatron-core at runtime without megatron-core having a hard dependency on miles. For example, megatron-core could expose a registry for top-k functions that miles can add to.
miles/backends/megatron_utils/model_provider.py (196-204)
The temporary disabling of routing_replay_manager.enabled is not robust. If get_gpt_mtp_block_spec or subsequent code raises an exception, routing_replay_manager.enabled will not be reset to True, leaving it in an inconsistent state for the rest of the process. This should be wrapped in a try...finally block to ensure the flag is always restored.
try:
if getattr(args, "use_rollout_routing_replay", False):
routing_replay_manager.enabled = False
logger.warning(
"Rollout routing replay is not applicable for MTP modules, so skipped replay registration"
)
mtp_block_spec = get_gpt_mtp_block_spec(config, transformer_layer_spec, **mtp_kwargs)
kwargs["mtp_block_spec"] = mtp_block_spec
finally:
if getattr(args, "use_rollout_routing_replay", False):
routing_replay_manager.enabled = True
|
could you also create a patch-equivalent PR in megatron fork |
done |
Related megatron commit: radixark/Megatron-LM#10
R3 cannot be applied to mtp layer, since mtp will not forward verify bonus tokens during rollouts. So bypass r3 replay for mtp head.
This impl is a little bit hard code, but maybe the best impl in current design. Mainly because we cannot get the module name before the layer is fully registered, so the Replay class cannot record anything about the layer name. Also, the original MoE routing function does not included module info, so we cannot directly skip MoE mtp replay during runtime... So add the element
is_mtpas instead.