Skip to content

feat(model): optimize Xing4 Grouped MM and add Ascend FSDP2 LoRA SFT example - #10654

Draft
GUINTSAGA wants to merge 1 commit into
modelscope:mainfrom
GUINTSAGA:xing4-ascend-fsdp2
Draft

GUINTSAGA wants to merge 1 commit into
modelscope:mainfrom
GUINTSAGA:xing4-ascend-fsdp2

Conversation

@GUINTSAGA

@GUINTSAGA GUINTSAGA commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

PR type

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

PR information

This PR optimizes Grouped MM expert computation for Xing4.0-29B-A4B on Ascend NPUs and adds an FSDP2 LoRA SFT training example.

In the tested Ascend NPU environment, the native Grouped MM path performs individual expert matrix multiplications. This PR reuses the existing NPU Grouped Matmul implementation in ms-swift to improve end-to-end training performance.

The main changes include:

  • Reuse the existing GmmFunction for Xing4 expert computation while preserving the original weight layout, dtype conversion, and FP32 routing semantics.
  • Wrap the original Experts Forward through the existing NPU Model Patch mechanism. The optimized implementation is enabled only for the NPU Grouped MM path, leaving other paths unchanged.
  • Add an FSDP2 LoRA SFT training example with Grouped MM, FlashAttention 2, packing, and usage documentation.
  • Add regression tests covering expert dispatch, weight layouts, packed expert LoRA, mixed dtypes, and FP32 routing precision.

Experiment results

Environment

Component Version
Hardware Ascend A3
PyTorch 2.10.0
torch-npu 2.10.0.post4
Transformers 5.14.1
PEFT 0.20.0
Accelerate 1.14.0
CANN 9.1.0
Triton-Ascend 3.2.2

Training configuration

The native and optimized Grouped MM implementations were evaluated for 120 LoRA SFT steps on 8 Ascend A3 NPUs, using identical initial model weights, LoRA parameters, training data, data order, and training configurations.

Configuration Value
Distributed strategy FSDP2
Activation checkpointing Enabled
Precision BF16
Attention FlashAttention 2
Packing Enabled
LoRA (rank / alpha / dropout) 8 / 32 / 0
Per-device batch size / Gradient accumulation 1 / 4
Max length 2048

Training comparison

Both implementations successfully completed 120 training steps. The loss and gradient norm remained finite throughout training, with closely matching training curves.

xing4_training_comparison
Metric MAE Max absolute error
Loss 0.000391 0.001569
Gradient Norm 0.000494 0.007710

Performance comparison

Each run consists of 120 optimizer steps, with the first 20 steps used for warmup and the remaining 100 steps used for performance measurement. Checkpoint saving is disabled during benchmarking.

Metric Native Patched Improvement
Mean step time (s) 26.785 21.425 20.01%
P95 step time (s) 27.578 22.261 19.28%
Input tokens/s 2441.63 3052.46 25.02%

The measured results show a 20.01% reduction in average step time and a 25.02% increase in token throughput.

Intermittent external workloads were observed during the native run.

Tests

# CPU: 8 passed, 1 skipped
TORCH_DEVICE_BACKEND_AUTOLOAD=0 python -m pytest tests/models/test_xing4_0_moe.py -v

# NPU: 9 passed
SWIFT_RUN_NPU_E2E=1 python -m pytest tests/models/test_xing4_0_moe.py -v

Checklist

  • I have linked the related issue above, if there is one.
  • I have reviewed and understand every changed line, and I can explain how the change works and why it is correct.
  • I have run the relevant tests or reproductions locally, and included the commands and results above (if applicable).
  • I am responsible for the content of this PR, regardless of how it was produced.

- Reuse the existing NPU GMM implementation for Xing4 grouped experts while preserving FP32 routing precision.
- Add an Ascend FSDP2 LoRA SFT example with FlashAttention 2 and packing.
- Add regression tests for expert dispatch, weight layouts and packed LoRA gradients.
- Fix uninitialized buffers in the existing tiny-model test fixture.

Co-authored-by: Codex

This branch has not been deployed

No deployments
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.

1 participant