swift微调训练代码

python版本要求3.10以上

安装swift

pip install ms-swift -U

数据集格式

{ "messages": [

{"role": "user", "content": "\n请描述这张图片的内容"},

{"role": "assistant", "content": "这是一张包含..."} ], "images": "./images/test_image.jpg" }

微调代码

swift sft

--model /root/autodl-tmp/Qwen3-VL-2B-Thinking \ # 替换为您的 Qwen3-VL 模型路径

--dataset /root/autodl-tmp/dataset/images.json \ # 替换为您的训练集路径

--val_dataset /root/autodl-tmp/dataset/images.json \ # 替换为您的验证集路径

--output_dir ./output_qwen3vl \ # 模型输出目录

--train_type lora \ # 使用 LoRA 微调

--torch_dtype bfloat16 \ # 混合精度训练

--num_train_epochs 1 \ # 训练轮数

--per_device_train_batch_size 1 \ # 单卡批次大小

--gradient_accumulation_steps 2 \ # 梯度累积步数

--learning_rate 1e-4 \ # 学习率

--lora_rank 8 \ # LoRA 秩

--lora_alpha 32 \ # LoRA Alpha

--target_modules all-linear \ # 微调目标模块

--freeze_vit true \ # 冻结视觉编码器(节省显存)

--freeze_aligner true \ # 冻结对齐层

--attn_impl flash_attn \ # 使用 Flash-Attention 加速

--gradient_checkpointing true \ # 开启梯度检查点(进一步节省显存)

--max_length 4096 \ # 最大序列长度

--save_steps 100 \ # 每100步保存一次权重

--logging_steps 20 # 每20步打印一次日志

报错 ValueError: remaining_argv: '--train_type', 'lora' 说明框架无法识别 --train_type 这个参数。

这是因为在较新版本的 ms-swift 中,参数名称发生了变更。--train_type 已经被重命名为 --tuner_type

swift sft

--model /root/autodl-tmp/Qwen3-VL-2B-Thinking

--dataset /root/autodl-tmp/dataset/images.json

--val_dataset /root/autodl-tmp/dataset/images.json

--output_dir ./output_qwen3vl

--tuner_type lora

--torch_dtype bfloat16

--num_train_epochs 1

--per_device_train_batch_size 1

--gradient_accumulation_steps 2

--learning_rate 1e-4

--lora_rank 8

--lora_alpha 32

--target_modules all-linear

--freeze_vit true

--freeze_aligner true

--attn_impl flash_attn

--gradient_checkpointing true

--max_length 4096

--save_steps 100

--logging_steps 20

参考网址https://github.com/modelscope/ms-swift