[Bug][Megatron] megatron sft + lora 训练 MoE 模型,target_modules 含 linear_fc1/linear_fc2 时报错 TERowParallelLinear is not supported

Open
#10,585 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python, pytorch

Research direction

Start with the Megatron LoRA adapter setup in swift/megatron/utils/utils.py and the PEFT integration in swift/tuners/peft.py, where the traceback shows adapter injection reaches PEFT's module replacement. Check how TERowParallelLinear is handled for all-linear versus explicit target modules, then reproduce with the listed Megatron SFT command and linear_fc1/linear_fc2. Done means those explicit targets inject LoRA successfully for the reported MoE setup.

Written by the indexing model from the issue text.

Description

bug
Checklist / 检查清单
  • I have searched existing issues, and this is a new bug report. / 我已经搜索过现有的 issues,确认这是一个新的 bug report。
Bug Description / Bug 描述

910B2使用 Megatron-SWIFT 对 MoE 模型(Qwen/Qwen3-30B-A3B,请改成你的实际模型)做 LoRA 微调。参考文档中 Megatron 的参数映射(linear_proj 对应 o_proj,linear_fc1 对应 gate_proj,up_proj,linear_fc2 对应 down_proj),将 target_modules 设置为:--target_modules linear_qkv linear_proj linear_fc1 linear_fc2
在注入 LoRA 时报错:> ValueError: Target module TERowParallelLinear() is not supported. Currently, only the following modules are supported: torch.nn.Linear, torch.nn.Embedding, ...

完整报错
[rank0]: Traceback (most recent call last):
[rank0]: File "/home/jovyan/huangwy/frameworks/latest/ms-swift/swift/cli/_megatron/sft.py", line 7, in
[rank0]: megatron_sft_main()
[rank0]: File "/home/jovyan/huangwy/frameworks/latest/ms-swift/swift/megatron/pipelines/train/sft.py", line 93, in megatron_sft_main
[rank0]: return MegatronSft(args).main()
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File "/home/jovyan/huangwy/frameworks/latest/ms-swift/swift/pipelines/base.py", line 52, in main
[rank0]: result = self.run()
[rank0]: ^^^^^^^^^^
[rank0]: File "/home/jovyan/huangwy/frameworks/latest/ms-swift/swift/megatron/pipelines/train/sft.py", line 66, in run
[rank0]: trainer = self.prepare_trainer()
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File "/home/jovyan/huangwy/frameworks/latest/ms-swift/swift/megatron/pipelines/train/sft.py", line 34, in prepare_trainer
[rank0]: return MegatronTrainer(self.args, self.template)
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File "/home/jovyan/huangwy/frameworks/latest/ms-swift/swift/megatron/trainers/base.py", line 79, in init
[rank0]: self.prepare_model()
[rank0]: File "/home/jovyan/huangwy/frameworks/latest/ms-swift/swift/megatron/trainers/base.py", line 201, in prepare_model
[rank0]: self.peft_models = self._prepare_peft_model(self.unwrapped_models)
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File "/home/jovyan/huangwy/frameworks/latest/ms-swift/swift/megatron/trainers/base.py", line 208, in _prepare_peft_model
[rank0]: peft_models = [prepare_mcore_model(args, model) for model in models]
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File "/home/jovyan/huangwy/frameworks/latest/ms-swift/swift/megatron/trainers/base.py", line 208, in
[rank0]: peft_models = [prepare_mcore_model(args, model) for model in models]
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File "/home/jovyan/huangwy/frameworks/latest/ms-swift/swift/megatron/utils/utils.py", line 200, in prepare_mcore_model
[rank0]: model = prepare_adapter(args, model)
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File "/home/jovyan/huangwy/frameworks/latest/ms-swift/swift/megatron/utils/utils.py", line 153, in prepare_adapter
[rank0]: model = Swift.prepare_model(model, lora_config)
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File "/home/jovyan/huangwy/frameworks/latest/ms-swift/swift/tuners/base.py", line 720, in prepare_model
[rank0]: return get_peft_model(model, config, **kwargs)
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File "/home/jovyan/.conda/envs/ms-swift-latest/lib/python3.11/site-packages/peft/mapping_func.py", line 122, in get_peft_model
[rank0]: return MODEL_TYPE_TO_PEFT_MODEL_MAPPING[peft_config.task_type](
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File "/home/jovyan/.conda/envs/ms-swift-latest/lib/python3.11/site-packages/peft/peft_model.py", line 2063, in init
[rank0]: super().init(model, peft_config, adapter_name, **kwargs)
[rank0]: File "/home/jovyan/.conda/envs/ms-swift-latest/lib/python3.11/site-packages/peft/peft_model.py", line 164, in init
[rank0]: self.base_model = cls(model, {adapter_name: peft_config}, adapter_name)
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File "/home/jovyan/huangwy/frameworks/latest/ms-swift/swift/tuners/peft.py", line 363, in new_init
[rank0]: self.init_origin(model, config, *args, **kwargs)
[rank0]: File "/home/jovyan/wzx/downloads/mcore-bridge-new/mcore-bridge-main/src/mcore_bridge/tuners/patcher.py", line 48, in new_init
[rank0]: origin_init(self, *args, **kwargs)
[rank0]: File "/home/jovyan/.conda/envs/ms-swift-latest/lib/python3.11/site-packages/peft/tuners/tuners_utils.py", line 318, in init
[rank0]: self.inject_adapter(self.model, adapter_name, low_cpu_mem_usage=low_cpu_mem_usage, state_dict=state_dict)
[rank0]: File "/home/jovyan/.conda/envs/ms-swift-latest/lib/python3.11/site-packages/peft/tuners/tuners_utils.py", line 917, in inject_adapter
[rank0]: self._create_and_replace(
[rank0]: File "/home/jovyan/huangwy/frameworks/latest/ms-swift/swift/tuners/peft.py", line 133, in _create_and_replace_hook
[rank0]: return self._create_and_replace_origin(peft_config, adapter_name, target, *args, **kwargs)
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File "/home/jovyan/.conda/envs/ms-swift-latest/lib/python3.11/site-packages/peft/tuners/lora/model.py", line 281, in _create_and_replace
[rank0]: new_module = self._create_new_module(lora_config, adapter_name, target, device_map=device_map, **kwargs)
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File "/home/jovyan/.conda/envs/ms-swift-latest/lib/python3.11/site-packages/peft/tuners/lora/model.py", line 436, in _create_new_module
[rank0]: raise ValueError(
[rank0]: ValueError: Target module TERowParallelLinear() is not supported. Currently, only the following modules are supported: torch.nn.Linear, torch.nn.Embedding, torch.nn.Conv1d, torch.nn.Conv2d, torch.nn.Conv3d, transformers.pytorch_utils.Conv1D, torch.nn.MultiheadAttention..
[ERROR] 2026-11-16-13:02:39 (PID:550818, Device:0, RankID:-1) ERR99999 UNKNOWN applicaiton exception
W1116 13:02:47.272000 550643 site-packages/torch/distributed/elastic/multiprocessing/api.py:900] Sending process 550819 closing signal SIGTERM
W1116 13:02:47.276000 550643 site-packages/torch/distributed/elastic/multiprocessing/api.py:900] Sending process 550820 closing signal SIGTERM
W1116 13:02:47.284000 550643 site-packages/torch/distributed/elastic/multiprocessing/api.py:900] Sending process 550821 closing signal SIGTERM
W1116 13:02:47.293000 550643 site-packages/torch/distributed/elastic/multiprocessing/api.py:900] Sending process 550822 closing signal SIGTERM
W1116 13:02:47.320000 550643 site-packages/torch/distributed/elastic/multiprocessing/api.py:900] Sending process 550823 closing signal SIGTERM
W1116 13:02:47.347000 550643 site-packages/torch/distributed/elastic/multiprocessing/api.py:900] Sending process 550824 closing signal SIGTERM
W1116 13:02:47.368000 550643 site-packages/torch/distributed/elastic/multiprocessing/api.py:900] Sending process 550825 closing signal SIGTERM
E1116 13:02:55.733000 550643 site-packages/torch/distributed/elastic/multiprocessing/api.py:874] failed (exitcode: 1) local_rank: 0 (pid: 550818) of binary: /home/jovyan/.conda/envs/ms-swift-latest/bin/python3.11
Traceback (most recent call last):
File "", line 198, in _run_module_as_main
File "", line 88, in _run_code
File "/home/jovyan/.conda/envs/ms-swift-latest/lib/python3.11/site-packages/torch/distributed/run.py", line 896, in
main()
File "/home/jovyan/.conda/envs/ms-swift-latest/lib/python3.11/site-packages/torch/distributed/elastic/multiprocessing/errors/init.py", line 355, in wrapper
return f(*args, **kwargs)
^^^^^^^^^^^^^^^^^^
File "/home/jovyan/.conda/envs/ms-swift-latest/lib/python3.11/site-packages/torch/distributed/run.py", line 892, in main
run(args)
File "/home/jovyan/.conda/envs/ms-swift-latest/lib/python3.11/site-packages/torch/distributed/run.py", line 883, in run
elastic_launch(
File "/home/jovyan/.conda/envs/ms-swift-latest/lib/python3.11/site-packages/torch/distributed/launcher/api.py", line 139, in call
return launch_agent(self._config, self._entrypoint, list(args))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jovyan/.conda/envs/ms-swift-latest/lib/python3.11/site-packages/torch/distributed/launcher/api.py", line 270, in launch_agent
--target_modules设置为all-linear运行正常

How to Reproduce / 如何复现

ascend 910b2
gpytorch 1.15.2
mcore_bridge 1.7.0.dev0
megatron-core 0.16.1
ms_swift 4.6.0.dev0
peft 0.20.0
torch 2.7.1
torch_npu 2.7.1.post2
torchaudio 2.7.1
torchvision 0.22.1

NPROC_PER_NODE=8
megatron sft
--model
--model_type 'qwen3_moe'
--check_model false
--nccl_comm_warmup true
--template 'qwen3_nothinking'
--save_safetensors true
--merge_lora false
--dataset
--tuner_type lora
--lora_rank 32
--lora_alpha 64
--lora_dropout 0.05
--target_modules all-linear
--load_from_cache_file true
--expert_model_parallel_size 8
--moe_permute_fusion true
--moe_grouped_gemm true
--moe_shared_expert_overlap true
--moe_aux_loss_coeff 1e-2
--packing true
--micro_batch_size 1
--global_batch_size 56
--recompute_granularity full
--recompute_method uniform
--recompute_num_layers 1
--num_train_epochs 2
--finetune true
--lr 3e-5
--lr_warmup_fraction 0.1
--output_dir megatron_output/Qwen3-30B-A3B/double-task-mix/SFT
--save_strategy epoch
--max_length 25201
--dataloader_num_workers 4
--dataset_num_proc 128
--no_save_optim true
--no_save_rng true
--sequence_parallel true
--cross_entropy_loss_fusion true
--gradient_accumulation_fusion false
--cross_entropy_fusion_impl native
--attention_backend flash

Additional Information / 补充信息

No response

Dominant language
Python
Stars
16.2k
Forks
1.8k
Avg merge
1d 14h
Merged PRs (30d)
68

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from modelscope/ms-swift

All issues in modelscope/ms-swift

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.