modelscope / modelscope/ms-swift

使用Lora微调时,检查点保存时出现adapter name 不匹配

Open
#8,336 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug stale
Dominant language
Python
Stars
15.7k
Forks
1.7k
Avg merge
1d 16h
Merged PRs (30d)
136

Description

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

微调Qwen3-VL时发现的问题,当我使用Lora + Trainer 进行微调训练时,检查点自动保存时报如下异常:
You passed an invalid 'selected_adapters' arguments, current supported adapter names are ['vision_only_lora'] - got ['default']. 其中 vision_only_lora 是我自定义的Lora名称

我是用的是python内方法调用的方式进行的训练,伪代码如下:

lora_config = LoraConfig(
    task_type=TaskType.CAUSAL_LM,
    r=16,
    lora_alpha=32,
    target_modules=target_modules,
    lora_dropout=0.05,
    bias='none'
)
model = Swift.prepare_model(model, lora_config, adapter_name=config.MODEL['adapter_name'])
# 训练参数
training_args = TrainingArguments(
    output_dir=f"{config.MODEL['save_path']}/step1",
    logging_dir=f"{config.LOG['output_file_path']}/training_logs/default/{train_name}",
    seed=config.seed,
    
    # 启用训练集、验证集
    do_train=True,                    # 确保启用训练
    do_eval=True,                     # 启用验证集评估
    
    # 显存均衡核心:批次与数据加载优化
    per_device_train_batch_size=batch_size,           # 训练批次大小
    gradient_accumulation_steps=gradient_steps,       # 梯度下降等效批次大小
    per_device_eval_batch_size=eval_batch_size,       # 评估批次大小
    eval_accumulation_steps=4,          # 评估累积,每进行多少步数的prediction就将 logits 移动到CPU
    dataloader_num_workers=4,           # 数据加载线程
    dataloader_pin_memory=True,         # 内存-显存直接映射(减少数据拷贝时的临时显存占用)
    dataloader_drop_last=False,         # 丢弃最后不足批次的样本
    dataloader_persistent_workers=True, # 数据加载子进程是否持续存活(不随数据集迭代结束关闭)
    
    # 主卡任务优化:减少额外显存消耗
    learning_rate=learning_rate,                                            # 学习率(保持原配置,合理值)
    weight_decay=0.01,                                                      # 权重衰减(L2 正则)
    num_train_epochs=num_train_epochs,    # 总训练轮次
    logging_strategy='steps',                                           # 日志记录策略,决定 “何时记录日志”,"steps":每 logging_steps 个 global step 记录 1 次
    logging_first_step=True,                                            # 是否在第 1 个 global_step 就记录日志
    logging_steps=1,                                                    # 日志打印间隔,每更新 1 次参数记录 1 次损失
    eval_strategy='steps',                                              # 按步数评估
    eval_steps=8,                                                       # 评估间隔
    save_steps=4,                                                       # 检查点保存间隔
    save_total_limit=4,                                                 # 最大保存检查点数量(保持原配置,避免主卡存储过多文件)
    save_only_model=True,                                               # 仅保存LoRA适配器(<100MB,主卡无需缓存完整模型)
    save_on_each_node=False,                                            # 仅主卡保存检查点(避免从卡额外保存,主卡也不会多占用存储显存)
    report_to=["tensorboard"],                                          # 日志报告工具, swanlab使用回调函数获取
    
    # 模型显存优化:激活值与分布式配置
    vit_gradient_checkpointing=True,                                # 开启视觉梯度检查点
    gradient_checkpointing=True,                                    # 开启梯度检查点
    gradient_checkpointing_kwargs={"use_reentrant": False},         # 避免重入问题(兼容新版本PyTorch)
    ddp_backend="nccl" ,
    ddp_find_unused_parameters=False,                               # DDP允许未使用参数(保持原配置,避免视觉模块参数未使用报错)
    remove_unused_columns=False,                                    # 不删除未使用数据列(保持原配置,避免数据丢失)
    bf16=True,                 # 若支持可开启,显存与fp16相当,精度更高
)

# 创建自定义数据整理器
data_collator = Qwen3VLDataCollator(
    processor=processor,
    max_length=max_length,
    pad_to_multiple_of=8  # 可选,用于优化GPU内存
)

# Trainer 定义
trainer = Trainer(
    model=model,
    template=template,
    processing_class=processor,
    args=training_args,
    train_dataset=train_dataset,  # 替换为实际的train_dataset
    eval_dataset=val_dataset,     # 替换为实际的val_dataset
    data_collator=data_collator,
    callbacks=[get_swanlab_callback()]
)

# 等待程序同步, 同步后再执行训练
dist.barrier()


# 开始训练
trainer.train()

报错信息如下:

File "/home/sourceroc/pythonFile/GraphRAG_RL/Qwen3_VL_RL/train/main.py", line 187, in <module>
    main(config.task_step)
  File "/home/sourceroc/pythonFile/GraphRAG_RL/Qwen3_VL_RL/train/main.py", line 146, in main
    train_step_1(device=device, train_path=train_path_list[step_index], val_path=val_path_list[step_index],
  File "/home/sourceroc/pythonFile/GraphRAG_RL/utils/wrapper.py", line 24, in wrapper
    res = func(*arg, **kwarg)
  File "/home/sourceroc/pythonFile/GraphRAG_RL/Qwen3_VL_RL/train/train_step_1.py", line 549, in train_step_1
    trainer.train()
  File "/data/users/sourceroc/.conda/envs/LLMs310/lib/python3.10/site-packages/swift/trainers/trainers.py", line 76, in train
    return super().train(*args, **kwargs)
  File "/data/users/sourceroc/.conda/envs/LLMs310/lib/python3.10/site-packages/swift/trainers/mixin.py", line 880, in train
    res = super().train(*args, **kwargs)
  File "/data/users/sourceroc/.conda/envs/LLMs310/lib/python3.10/site-packages/transformers/trainer.py", line 2325, in train
    return inner_training_loop(
  File "/data/users/sourceroc/.conda/envs/LLMs310/lib/python3.10/site-packages/transformers/trainer.py", line 2756, in _inner_training_loop
    self._maybe_log_save_evaluate(
  File "/data/users/sourceroc/.conda/envs/LLMs310/lib/python3.10/site-packages/swift/trainers/mixin.py", line 953, in _maybe_log_save_evaluate
    super()._maybe_log_save_evaluate(tr_loss, *args, **kwargs)
  File "/data/users/sourceroc/.conda/envs/LLMs310/lib/python3.10/site-packages/transformers/trainer.py", line 3228, in _maybe_log_save_evaluate
    self._save_checkpoint(model, trial)
  File "/data/users/sourceroc/.conda/envs/LLMs310/lib/python3.10/site-packages/swift/trainers/mixin.py", line 514, in _save_checkpoint
    result = super()._save_checkpoint(*args, **kwargs)
  File "/data/users/sourceroc/.conda/envs/LLMs310/lib/python3.10/site-packages/transformers/trainer.py", line 3325, in _save_checkpoint
    self.save_model(output_dir, _internal_call=True)
  File "/data/users/sourceroc/.conda/envs/LLMs310/lib/python3.10/site-packages/transformers/trainer.py", line 4227, in save_model
    self._save(output_dir)
  File "/data/users/sourceroc/.conda/envs/LLMs310/lib/python3.10/site-packages/swift/trainers/mixin.py", line 396, in _save
    self._save_model(output_dir, state_dict)
  File "/data/users/sourceroc/.conda/envs/LLMs310/lib/python3.10/site-packages/swift/trainers/mixin.py", line 360, in _save_model
    self.model.save_pretrained(output_dir, safe_serialization=save_safetensors, **save_kwargs)
  File "/data/users/sourceroc/.conda/envs/LLMs310/lib/python3.10/site-packages/peft/peft_model.py", line 230, in save_pretrained
    raise ValueError(
You passed an invalid `selected_adapters` arguments, current supported adapter names are ['vision_only_lora'] - got ['default'].

在ms-swift:3.9.1 版本中没有出现此问题,当升级到3.12.6出现此问题

我应该是修改我的实现代码(Lora微调时使用默认的 default @@的名称,还是ms-swift的代码问题,或者说我应该如何修改实现方法。
@hjh0119

How to Reproduce / 如何复现

版本:ms-swift: 3.12.6 peft:0.17.1

Additional Information / 补充信息

经过查看源代码发现,此问题的发生出现在#6950 的合并提交中(该合并在版本3.11.0第一次更新),通过查看Pull requests和Issues发现,改合并是 @hjh0119 为了修复 #6910 提交的修复代码

Contributor guide

Open the contributing guide

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.

Research direction

Start with swift/trainers/mixin.py around _save_model and the save_pretrained call, then compare the changes from #6950 with the adapter handling in PEFT 0.17.1 and the earlier #6910 fix. Reproduce with ms-swift 3.12.6 using the provided custom adapter name and checkpoint settings. Done means checkpoint saving accepts the configured adapter without breaking the default-adapter path.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.