get_multimodal_target_regex(..., exclude_router=True) does not exclude shared_expert_gate

Open Beginner friendly
#10,696 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@KonnyakuMatcha is already working on this.

Since Dec 5, 2026.

  • #10697 by @KonnyakuMatcha — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python, pytorch

Research direction

Start in swift/utils/transformers_utils.py at get_multimodal_target_regex and compare its router exclusions with get_modules_to_not_convert around line 337. Check how the regex matches mlp.shared_expert_gate while retaining gate_proj and attention projections. Done means the shared expert gate is excluded when exclude_router=True; verify with a MoE model or a focused regex test.

Written by the indexing model from the issue text.

Description

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

In swift/utils/transformers_utils.py, get_multimodal_target_regex(..., exclude_router=True) currently excludes only 'gate' (router_modules = ['gate']) for MoE models, but does not exclude 'shared_expert_gate'.

This is inconsistent with the rest of ms-swift and causes a Megatron-to-PEFT LoRA export mismatch:

  • get_modules_to_not_convert (swift/utils/transformers_utils.py:337) and quant_args.py already treat both mlp.gate and mlp.shared_expert_gate as MoE gating/router layers.
  • In HF MoE VLMs (e.g. Qwen4-35B-A3B, Qwen3.5-MoE), shared_expert_gate is a scalar nn.Linear(hidden_size, 1, bias=False) gating the shared expert, whereas Megatron Core SharedExpertMLP stores it as a raw nn.Parameter (gate_weight) and never attaches LoRA to it.
  • When swift/megatron/init.py exports a Megatron LoRA checkpoint to PEFT format via get_multimodal_target_regex(..., exclude_router='all-router' not in args.target_modules), leaving shared_expert_gate in the regex causes adapter_config.json to target shared_expert_gate even though no LoRA weights exist for it.
How to Reproduce / 如何复现
  • ms-swift: main (ece3bcc, 4.7.0.dev0)

Steps:

  1. Call get_multimodal_target_regex(model, freeze_llm=False, exclude_router=True) on a MoE model containing mlp.shared_expert_gate (such as Qwen/Qwen4-35B-A3B or Qwen3.5-MoE).
  2. Observe shared_expert_gate is still matched by the returned target_modules regex (while gate is excluded).
Additional Information / 补充信息

Keeping gate_proj and attention projections matched while excluding both gate and shared_expert_gate aligns get_multimodal_target_regex with get_modules_to_not_convert and Megatron LoRA export.

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.