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

Aperta Adatta ai principianti
#10,696 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@KonnyakuMatcha ci sta già lavorando.

Dal 5/12/2026.

  • #10697 di @KonnyakuMatcha — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python, pytorch

Direzione di ricerca

Inizia in swift/utils/transformers_utils.py, in corrispondenza di get_multimodal_target_regex, e confronta le esclusioni del router con get_modules_to_not_convert intorno alla riga 337. Controlla come l’espressione regolare corrisponde a mlp.shared_expert_gate mantenendo gate_proj e le proiezioni di attenzione. Il lavoro è completato quando il gate dell’esperto condiviso viene escluso con exclude_router=True; verificalo con un modello MoE o un test mirato dell’espressione regolare.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
16.2k
Fork
1.8k
Merge medio
1g 14h
PR unite (30g)
68

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di modelscope/ms-swift

Tutte le issue di modelscope/ms-swift

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.