Skip to content

Conversation

@ji-huazhong
Copy link
Collaborator

@ji-huazhong ji-huazhong commented Nov 21, 2025

PR type

  • Bug Fix
  • New Feature
  • Document Updates
  • More Models or Datasets Support

PR information

Write the detail information belongs to this PR.

Experiment results

Paste your experiment result here(if needed).

Quick Start

  1. setup your npu environment (ensure the matching CANN/PTA versions are installed)
  2. install mindspeed(megatron)
git clone https://github.com/NVIDIA/Megatron-LM.git
cd Megatron-LM
git checkout core_v0.12.1
export PYTHONPATH=$PYTHONPATH:<your_local_megatron_lm_path>
export MEGATRON_LM_PATH=<your_local_megatron_lm_path>
git clone https://gitcode.com/Ascend/MindSpeed.git
cd MindSpeed
git reset --hard 1a38359e06764cb315b34281440e5f0cb9f4cb7a
pip install -e .
  1. install ms-swift
git clone https://github.com/ji-huazhong/ms-swift.git -b mindspeed
cd ms-swift
pip install -e .

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @ji-huazhong, 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 introduces preliminary support for MindSpeed, allowing the Megatron framework to leverage Ascend NPUs for accelerated training. The changes involve adapting the Megatron initialization and SFT training processes with conditional imports and patching mechanisms to ensure compatibility and optimize performance on NPU hardware, particularly for Flash Attention. Additionally, a new example script is provided to guide users in setting up and running MoE training on Ascend NPUs.

Highlights

  • MindSpeed Integration: Added support for MindSpeed to enable Megatron operations on Ascend NPUs, allowing for specialized hardware acceleration.
  • NPU-Specific Adaptations: Implemented conditional logic to import and utilize mindspeed.megatron_adaptor for environment setup and SFT training, including a workaround for transformer_engine and explicitly enabling Flash Attention for NPU.
  • New Example Script: Introduced vl-moe.sh, a shell script demonstrating how to run Megatron SFT with Mixture-of-Experts (MoE) on Ascend NPUs, complete with specific training configurations.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

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 support for MindSpeed on Ascend NPUs by conditionally importing and patching the environment based on the mindspeed library's availability. The changes are primarily in swift/megatron/init.py and swift/megatron/train/sft.py. A new shell script, vl-moe.sh, is also added to demonstrate training on NPUs. My review includes a suggestion to refactor a part of the logic for improved clarity and a fix for a style issue in the new script.

Comment on lines +38 to +44
megatron_args = asdict(args)
if megatron_args["attention_backend"] != "local":
megatron_args["use_flash_attn"] = True
# MindSpeed requires passing `use_flash_attn` to Megatron
# to enable flash attention on Ascend NPU.
self.args.use_flash_attn = True
repatch(megatron_args)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The logic for setting use_flash_attn can be simplified for better clarity and to avoid redundancy. It's clearer to modify args.use_flash_attn directly and then create the megatron_args dictionary from the updated args object. This avoids modifying both the dictionary and the args object separately for the same value.

Suggested change
megatron_args = asdict(args)
if megatron_args["attention_backend"] != "local":
megatron_args["use_flash_attn"] = True
# MindSpeed requires passing `use_flash_attn` to Megatron
# to enable flash attention on Ascend NPU.
self.args.use_flash_attn = True
repatch(megatron_args)
if args.attention_backend != "local":
# MindSpeed requires passing `use_flash_attn` to Megatron
# to enable flash attention on Ascend NPU.
args.use_flash_attn = True
megatron_args = asdict(args)
repatch(megatron_args)

--recompute_method uniform \
--recompute_num_layers 1 \
--attention_backend flash
# --moe_permute_fusion true \ No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This commented-out line includes an unnecessary trailing backslash. Additionally, the file is missing a newline character at the end. According to POSIX standards, a text file should conclude with a newline to ensure compatibility with various command-line tools.

Suggested change
# --moe_permute_fusion true \
# --moe_permute_fusion true

@Jintao-Huang
Copy link
Collaborator

https://github.com/modelscope/ms-swift/pull/6661/files

Hello 😊. This looks like a duplicate PR.

@@ -0,0 +1,45 @@
PYTORCH_NPU_ALLOC_CONF='expandable_segments:True' \
Copy link
Collaborator

Choose a reason for hiding this comment

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

It is recommended to place the examples in the examples/npu/megatron directory.

In the future, the npu directory can provide more NPU-related scripts.

It is recommended to add a section on NPU environment installation in the Megatron-SWIFT Quick Start documentation.

@ji-huazhong
Copy link
Collaborator Author

https://github.com/modelscope/ms-swift/pull/6661/files

Hello 😊. This looks like a duplicate PR.

@Jintao-Huang Thank you for your feedback😄. This PR is still in WIP stage. I'd like to submit a draft PR to share my progress with the surrounding teams. We still need to do further precision alignment and performance tuning.

@ji-huazhong ji-huazhong marked this pull request as draft November 21, 2025 03:07
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