Repository navigation
perf(megatron): reduce NPU gradient, RMSNorm and MoE permutation overhead - #10513
Draft
addsubmuldiv wants to merge 7 commits into
Draft
addsubmuldiv wants to merge 7 commits into
addsubmuldiv wants to merge 7 commits into
Conversation
addsubmuldiv
marked this pull request as ready for review
November 4, 2026 09:53
There was a problem hiding this comment.
🟡 Changes recommended
The copy-before-scaling operation can overflow mixed-dtype outputs and rejects partially overlapping views, requiring a scaled temporary and regression tests.
1 open issue
What changed in this PR
This PR reduces host synchronization in ms-swift’s NPU Megatron gradient norm and scaling operations while leaving unrelated backend behavior unchanged.
Changes:
- Keeps norm and overflow reductions on-device and batches eligible in-place scaling.
- Installs the replacements after MegatronAdaptor import, with signature checks and idempotence.
- Adds CPU/NPU tests for numerical behavior, synchronization, and installation.
| File | Description |
|---|---|
tests/utils/test_npu_megatron_performance_optimizer.py |
Tests norm/scaling behavior, overflow, layouts, and scalar-read avoidance. |
tests/utils/test_npu_megatron_performance_install.py |
Checks installed aliases, repeat installation, and unchanged unrelated functions. |
swift/model/npu_patch/megatron_optimizer.py |
Implements shared norm/scaling helpers and patch installation. |
swift/megatron/__init__.py |
Installs and logs the NPU gradient patches after MegatronAdaptor import. |
Review effort: Balanced
Comment on lines
+48
to
+49
| dst.copy_(src) | ||
| dst.mul_(scale) |
addsubmuldiv
marked this pull request as draft
November 4, 2026 12:18
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

PR type
PR information
Related issue: N/A
Megatron training on Ascend NPU regressed after upgrading from ms-swift 4.5.3 / MCore 0.16 / MindSpeed to main / MCore 0.18 / MegatronAdaptor + TransformerEngineNPU. This change reduces host overhead in normalization, fragmented-gradient processing and non-fused MoE permutation.
Patches install after MegatronAdaptor and before model construction, updating cached MCore optimizer references with signature checks and idempotence. GC policy, weighted SwiGLU and the fused MoE implementation are unchanged. Runtime patches and Ascend examples only; no documentation changes. Standalone validation scripts are kept locally outside this PR.
Experiment results
Qwen3-Omni LoRA: original configuration
Eight Ascend A3 NPU devices, Qwen3-Omni-30B-A3B-Instruct, BF16, TP=1/EP=8/PP=1, GBS=32/MBS=2, rank=8/alpha=32, max_length=4096, full/uniform/1 recomputation, frozen vision/aligner and
moe_permute_fusion=true. Both versions use the same physical devices, checkpoint, fixed mixed text/image dataset and aligned sampling/preprocessing seeds. The original MAmmoTH-VL dataset is not cached, so these results apply to the fixed comparison dataset.Each version runs 120 steps without profiler/evaluation/checkpoint saving. Discard steps 1–20; synchronize NPU execution at step boundaries and report the maximum duration across all eight ranks.
The upgraded stack takes 4.85% less time in this final pair; all five consecutive 20-step windows improve. All eight ranks have matching trainable parameter names/shapes/dtypes and first four microbatch hashes: 144 LoRA modules, 3168 tensors and 42,074,112 trainable parameters per rank. All 120 loss/grad_norm values are finite; Loss MAE is 0.00090136, max absolute difference 0.00387621. These checks do not establish long-term convergence equivalence. No foreign process was detected on the selected devices during accepted runs.
For diagnosis, GBS=128 with unchanged MBS=2 and operator settings increases step duration to 6.533 s old vs 7.116 s upgraded before the new normalization/buffer changes (+8.9%). Device compute time stays similar; host wrappers, GC frequency and collective waiting increase. A complete four-run counterbalanced crossover in the original GBS=32 configuration measures each optimization independently at the same training-step positions. Every run contains all four conditions; condition counts are balanced across runs and the two A3 card allocations. After warmup and block-transition exclusions, each condition has 72 matched step positions.
These are geometric means of same-step duration ratios. Adding gradient-buffer processing on top of RMSNorm saves a further 5.42%; adding RMSNorm on top of gradient-buffer processing saves 6.30%. Including transition steps gives similar reductions (6.85%, 6.46%, 12.00%). This ablation and the final old/new pair are separate experiments with different reference baselines.
Retained non-fused permutation optimization
An earlier 16-NPU Omni full-parameter ablation uses TP=1/EP=8/PP=2, GBS=8/MBS=1, packing/max_length=1024 and
moe_permute_fusion=false. Over 100 steps, discarding 20 warmup steps, the gradient patch alone measures 1.6784 s/step; adding stable FP32 permutation measures 0.9951 s/step (40.7% less time). Earlier old-stack reference: 1.0164 s/step. These separate-round references do not imply a precise paired cross-version percentage.An earlier 8-NPU Qwen3.5-35B-A3B LoRA test with all 330 targets in both groups, fused permutation and max_length=1024 measures 3.8901 s/step with original TENPU gradient functions vs 3.1479 s with the gradient patch. This is a different configuration from the coverage-aligned examples.
Validation
The standalone validation suite is intentionally excluded from this PR to keep the diff focused on runtime code. It is retained locally and was rerun after removing the repository test files:
tests/run.py: 23 passed.Repository checks:
These checks pass.
Checklist