论文 | English Documentation | 中文文档
请扫描下面的二维码来加入我们的交流群:
| Discord Group | 微信群 |
|---|---|
![]() | ![]() |
🍲 ms-swift是魔搭社区提供的大模型与多模态大模型微调部署框架,现已支持600+纯文本大模型与400+多模态大模型的训练(预训练、微调、人类对齐)、推理、评测、量化与部署。其中大模型包括:Qwen3、Qwen3.5、InternLM3、GLM4.5、Mistral、DeepSeek-R1、Llama4等模型,多模态大模型包括:Qwen3-VL、Qwen3-Omni、Llava、InternVL3.5、MiniCPM-V-4、Ovis2.5、GLM4.5-V、DeepSeek-VL2等模型。
🍔 除此之外,ms-swift汇集了最新的训练技术,包括集成Megatron并行技术,包括TP、PP、CP、EP等为训练提供加速,以及众多GRPO算法族强化学习的算法,包括:GRPO、DAPO、GSPO、SAPO、CISPO、RLOO、Reinforce++等提升模型智能。ms-swift支持广泛的训练任务,包括DPO、KTO、RM、CPO、SimPO、ORPO等偏好学习算法,以及Embedding、Reranker、序列分类任务。ms-swift提供了大模型训练全链路的支持,包括使用vLLM、SGLang和LMDeploy对推理、评测、部署模块提供加速,以及使用GPTQ、AWQ、BNB、FP8技术对大模型进行量化。
为什么选择ms-swift?
--sequence_parallel_size N。--enable_dft_loss true。训练脚本参考这里swift sample命令。强化微调脚本参考这里,大模型API蒸馏采样脚本参考这里。--infer_backend vllm/lmdeploy即可。使用pip进行安装:
pip install ms-swift -U
# 使用uv
pip install uv
uv pip install ms-swift -U --torch-backend=auto
从源代码安装:
# pip install git+https://github.com/modelscope/ms-swift.git
git clone https://github.com/modelscope/ms-swift.git
cd ms-swift
# main分支为swift4.x。若安装swift3.x,请运行以下命令
# git checkout release/3.12
pip install -e .
# 使用uv
uv pip install -e . --torch-backend=auto
运行环境:
| 范围 | 推荐 | 备注 | |
|---|---|---|---|
| python | >=3.9 | 3.11/3.12 | |
| cuda | cuda12 | 使用cpu、npu、mps则无需安装 | |
| torch | >=2.0 | 2.8.0/2.10.0 | |
| transformers | >=4.33 | 4.57.6/5.2.0 | |
| modelscope | >=1.23 | ||
| peft | >=0.11,<0.19 | ||
| flash_attn | 2.8.3/3.0.0b1 | ||
| trl | >=0.15,<0.29 | 0.28.0 | RLHF |
| deepspeed | >=0.14 | 0.18.8 | 训练 |
| vllm | >=0.5.1 | 0.11.0/0.17.1 | 推理/部署 |
| sglang | >=0.4.6 | 推理/部署 | |
| lmdeploy | >=0.5 | 0.10.1 | 推理/部署 |
| evalscope | >=1.0 | 评测 | |
| gradio | 5.32.1 | Web-UI/App |
更多可选依赖可以参考这里。
10分钟在单卡3090上对Qwen3-4B-Instruct-2507进行自我认知微调:
# 13GB
CUDA_VISIBLE_DEVICES=0 \
swift sft \
--model Qwen/Qwen3-4B-Instruct-2507 \
--tuner_type lora \
--dataset 'AI-ModelScope/alpaca-gpt4-data-zh#500' \
'AI-ModelScope/alpaca-gpt4-data-en#500' \
'swift/self-cognition#500' \
--torch_dtype bfloat16 \
--num_train_epochs 1 \
--per_device_train_batch_size 1 \
--per_device_eval_batch_size 1 \
--learning_rate 1e-4 \
--lora_rank 8 \
--lora_alpha 32 \
--target_modules all-linear \
--gradient_accumulation_steps 16 \
--eval_steps 50 \
--save_steps 50 \
--save_total_limit 2 \
--logging_steps 5 \
--max_length 2048 \
--output_dir output \
--warmup_ratio 0.05 \
--dataloader_num_workers 4 \
--model_author swift \
--model_name swift-robot
小贴士:
--dataset <dataset_path>。--model_author和--model_name参数只有当数据集中包含swift/self-cognition时才生效。--model <model_id/model_path>即可。--use_hf true即可。训练完成后,使用以下命令对训练后的权重进行推理:
--adapters需要替换成训练生成的last checkpoint文件夹。由于adapters文件夹中包含了训练的参数文件args.json,因此不需要额外指定--model,--system,swift会自动读取这些参数。如果要关闭此行为,可以设置--load_args false。# 使用交互式命令行进行推理
CUDA_VISIBLE_DEVICES=0 \
swift infer \
--adapters output/vx-xxx/checkpoint-xxx \
--stream true \
--temperature 0 \
--max_new_tokens 2048
# merge-lora并使用vLLM进行推理加速
CUDA_VISIBLE_DEVICES=0 \
swift infer \
--adapters output/vx-xxx/checkpoint-xxx \
--stream true \
--merge_lora true \
--infer_backend vllm \
--vllm_max_model_len 8192 \
--temperature 0 \
--max_new_tokens 2048
最后,使用以下命令将模型推送到ModelScope:
CUDA_VISIBLE_DEVICES=0 \ swift export \ --adapters output/vx-xxx/checkpoint-xxx \ --push_to_hub true \ --hub_model_id '<your-model-id>' \ --hub_token '<your-sdk-token>' \ --use_hf false
Web-UI是基于gradio界面技术的零门槛训练、部署界面方案,具体可以查看这里。
swift web-ui

ms-swift也支持使用python的方式进行训练和推理。下面给出训练和推理的伪代码,具体可以查看这里。
训练:
from peft import LoraConfig, get_peft_model
from swift import get_model_processor, get_template, load_dataset, EncodePreprocessor
from swift.trainers import Seq2SeqTrainer, Seq2SeqTrainingArguments
# 获取模型和template,并加入可训练的LoRA模块
model, tokenizer = get_model_processor(model_id_or_path, ...)
template = get_template(tokenizer, ...)
lora_config = LoraConfig(...)
model = get_peft_model(model, lora_config)
# 下载并载入数据集,并将文本encode成tokens
train_dataset, val_dataset = load_dataset(dataset_id_or_path, ...)
train_dataset = EncodePreprocessor(template=template)(train_dataset, num_proc=num_proc)
val_dataset = EncodePreprocessor(template=template)(val_dataset, num_proc=num_proc)
# 进行训练
training_args = Seq2SeqTrainingArguments(...)
trainer = Seq2SeqTrainer(
model=model,
args=training_args,
template=template,
train_dataset=train_dataset,
eval_dataset=val_dataset,
)
trainer.train()
推理:
from swift import TransformersEngine, InferRequest, RequestConfig
# 使用原生 transformers 引擎进行推理
engine = TransformersEngine(model_id_or_path, adapters=[lora_checkpoint])
infer_request = InferRequest(messages=[{'role': 'user', 'content': 'who are you?'}])
request_config = RequestConfig(max_tokens=max_new_tokens, temperature=temperature)
resp_list = engine.infer([infer_request], request_config)
print(f'response: {resp_list[0].choices[0].message.content}')
这里给出使用ms-swift进行训练到部署的最简示例,具体可以查看examples。
--model指定对应模型的id或者path,修改--dataset指定对应数据集的id或者path即可。--use_hf true即可。| 常用链接 |
|---|
| 🔥命令行参数 |
| Megatron-SWIFT |
| GRPO |
| 支持的模型和数据集 |
| 自定义模型, 🔥自定义数据集 |
| 大模型教程 |
支持的训练方法:
| 方法 | 全参数 | LoRA | QLoRA | Deepspeed | 多机 | 多模态 |
|---|---|---|---|---|---|---|
| 预训练 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| 指令监督微调 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| GRPO | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| GKD | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| PPO | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ |
| DPO | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| KTO | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| 奖励模型 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| CPO | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| SimPO | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| ORPO | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Embedding | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Reranker | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| 序列分类 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
预训练:
# 8*A100
NPROC_PER_NODE=8 \
CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 \
swift pt \
--model Qwen/Qwen2.5-7B \
--dataset swift/chinese-c4 \
--streaming true \
--tuner_type full \
--deepspeed zero2 \
--output_dir output \
--max_steps 10000 \
...
微调:
CUDA_VISIBLE_DEVICES=0 swift sft \ --model Qwen/Qwen2.5-7B-Instruct \ --dataset AI-ModelScope/alpaca-gpt4-data-zh \ --tuner_type lora \ --output_dir output \ ...
RLHF:
CUDA_VISIBLE_DEVICES=0 swift rlhf \ --rlhf_type dpo \ --model Qwen/Qwen2.5-7B-Instruct \ --dataset hjh0119/shareAI-Llama3-DPO-zh-en-emoji \ --tuner_type lora \ --output_dir output \ ...
ms-swift支持使用Megatron并行技术加速训练,包括大规模集群训练和MoE模型训练。以下为支持的训练方法:
| 方法 | 全参数 | LoRA | MoE | 多模态 | FP8 |
|---|---|---|---|---|---|
| 预训练 | ✅ | ✅ | ✅ | ✅ | ✅ |
| 指令监督微调 | ✅ | ✅ | ✅ | ✅ | ✅ |
| GRPO | ✅ | ✅ | ✅ | ✅ | ✅ |
| GKD | ✅ | ✅ | ✅ | ✅ | ✅ |
| DPO | ✅ | ✅ | ✅ | ✅ | ✅ |
| KTO | ✅ | ✅ | ✅ | ✅ | ✅ |
| RM | ✅ | ✅ | ✅ | ✅ | ✅ |
| Embedding | ✅ | ✅ | ✅ | ✅ | ✅ |
| Reranker | ✅ | ✅ | ✅ | ✅ | ✅ |
| 序列分类 | ✅ | ✅ | ✅ | ✅ | ✅ |
NPROC_PER_NODE=2 CUDA_VISIBLE_DEVICES=0,1 megatron sft \ --model Qwen/Qwen2.5-7B-Instruct \ --save_safetensors true \ --dataset AI-ModelScope/alpaca-gpt4-data-zh \ --tuner_type lora \ --output_dir output \ ...
ms-swift支持丰富GRPO族算法:
| 方法 | 全参数 | LoRA | 多模态 | 多机 |
|---|---|---|---|---|
| GRPO | ✅ | ✅ | ✅ | ✅ |
| DAPO | ✅ | ✅ | ✅ | ✅ |
| GSPO | ✅ | ✅ | ✅ | ✅ |
| SAPO | ✅ | ✅ | ✅ | ✅ |
| CISPO | ✅ | ✅ | ✅ | ✅ |
| CHORD | ✅ | ✅ | ✅ | ✅ |
| RLOO | ✅ | ✅ | ✅ | ✅ |
| Reinforce++ | ✅ | ✅ | ✅ | ✅ |
CUDA_VISIBLE_DEVICES=0,1,2,3 NPROC_PER_NODE=4 \ swift rlhf \ --rlhf_type grpo \ --model Qwen/Qwen2.5-7B-Instruct \ --tuner_type lora \ --use_vllm true \ --vllm_mode colocate \ --dataset AI-MO/NuminaMath-TIR#10000 \ --output_dir output \ ...
CUDA_VISIBLE_DEVICES=0 swift infer \
--model Qwen/Qwen2.5-7B-Instruct \
--stream true \
--infer_backend transformers \
--max_new_tokens 2048
# LoRA
CUDA_VISIBLE_DEVICES=0 swift infer \
--model Qwen/Qwen2.5-7B-Instruct \
--adapters swift/test_lora \
--stream true \
--infer_backend transformers \
--temperature 0 \
--max_new_tokens 2048
CUDA_VISIBLE_DEVICES=0 swift app \ --model Qwen/Qwen2.5-7B-Instruct \ --stream true \ --infer_backend transformers \ --max_new_tokens 2048 \ --lang zh
CUDA_VISIBLE_DEVICES=0 swift deploy \ --model Qwen/Qwen2.5-7B-Instruct \ --infer_backend vllm
CUDA_VISIBLE_DEVICES=0 swift sample \ --model LLM-Research/Meta-Llama-3.1-8B-Instruct \ --sampler_engine transformers \ --num_return_sequences 5 \ --dataset AI-ModelScope/alpaca-gpt4-data-zh#5
CUDA_VISIBLE_DEVICES=0 swift eval \ --model Qwen/Qwen2.5-7B-Instruct \ --infer_backend lmdeploy \ --eval_backend OpenCompass \ --eval_dataset ARC_c
CUDA_VISIBLE_DEVICES=0 swift export \ --model Qwen/Qwen2.5-7B-Instruct \ --quant_bits 4 --quant_method awq \ --dataset AI-ModelScope/alpaca-gpt4-data-zh \ --output_dir Qwen2.5-7B-Instruct-AWQ
swift export \ --model <model-path> \ --push_to_hub true \ --hub_model_id '<model-id>' \ --hub_token '<sdk-token>'
本框架使用Apache License (Version 2.0)进行许可。模型和数据集请查看原资源页面并遵守对应License。
@misc{zhao2024swiftascalablelightweightinfrastructure, title={SWIFT:A Scalable lightWeight Infrastructure for Fine-Tuning}, author={Yuze Zhao and Jintao Huang and Jinghan Hu and Xingjun Wang and Yunlin Mao and Daoze Zhang and Zeyinzi Jiang and Zhikai Wu and Baole Ai and Ang Wang and Wenmeng Zhou and Yingda Chen}, year={2024}, eprint={2408.05517}, archivePrefix={arXiv}, primaryClass={cs.CL}, url={https://arxiv.org/abs/2408.05517}, }