论文 | English Documentation | 中文文档
请扫描下面的二维码来加入我们的交流群:
Discord Group | 微信群 |
---|---|
![]() |
![]() |
🍲 ms-swift是魔搭社区提供的大模型与多模态大模型微调部署框架,现已支持500+大模型与200+多模态大模型的训练(预训练、微调、人类对齐)、推理、评测、量化与部署。其中大模型包括:Qwen3、Qwen3-MoE、Qwen2.5、InternLM3、GLM4、Mistral、DeepSeek-R1、Yi1.5、TeleChat2、Baichuan2、Gemma2等模型,多模态大模型包括:Qwen2.5-VL、Qwen2-Audio、Llama4、Llava、InternVL3、MiniCPM-V-2.6、GLM4v、Xcomposer2.5、Yi-VL、DeepSeek-VL2、Phi3.5-Vision、GOT-OCR2等模型。
🍔 除此之外,ms-swift汇集了最新的训练技术,包括LoRA、QLoRA、Llama-Pro、LongLoRA、GaLore、Q-GaLore、LoRA+、LISA、DoRA、FourierFt、ReFT、UnSloth、和Liger等轻量化训练技术,以及DPO、GRPO、RM、PPO、KTO、CPO、SimPO、ORPO等人类对齐训练方法。ms-swift支持使用vLLM和LMDeploy对推理、评测和部署模块进行加速,并支持使用GPTQ、AWQ、BNB等技术对大模型进行量化。ms-swift还提供了基于Gradio的Web-UI界面及丰富的最佳实践。
为什么选择ms-swift?
swift sample
命令。强化微调脚本参考这里,大模型API蒸馏采样脚本参考这里。--infer_backend vllm/lmdeploy
即可。使用pip进行安装:
pip install ms-swift -U
从源代码安装:
# pip install git+https://github.com/modelscope/ms-swift.git
git clone https://github.com/modelscope/ms-swift.git
cd ms-swift
pip install -e .
运行环境:
范围 | 推荐 | 备注 | |
---|---|---|---|
python | >=3.9 | 3.10 | |
cuda | cuda12 | 使用cpu、npu、mps则无需安装 | |
torch | >=2.0 | ||
transformers | >=4.33 | 4.51 | |
modelscope | >=1.23 | ||
peft | >=0.11,<0.16 | ||
trl | >=0.13,<0.18 | 0.17 | RLHF |
deepspeed | >=0.14 | 0.14.5 | 训练 |
vllm | >=0.5.1 | 0.8 | 推理/部署/评测 |
lmdeploy | >=0.5 | 0.8 | 推理/部署/评测 |
evalscope | >=0.11 | 评测 |
更多可选依赖可以参考这里。
10分钟在单卡3090上对Qwen2.5-7B-Instruct进行自我认知微调:
# 22GB
CUDA_VISIBLE_DEVICES=0 \
swift sft \
--model Qwen/Qwen2.5-7B-Instruct \
--train_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 \
--system 'You are a helpful assistant.' \
--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 \
--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的方式进行训练和推理。下面给出训练和推理的伪代码,具体可以查看这里。
训练:
# 获取模型和template,并加入可训练的LoRA模块
model, tokenizer = get_model_tokenizer(model_id_or_path, ...)
template = get_template(model.model_meta.template, tokenizer, ...)
model = Swift.prepare_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)
# 进行训练
trainer = Seq2SeqTrainer(
model=model,
args=training_args,
data_collator=template.data_collator,
train_dataset=train_dataset,
eval_dataset=val_dataset,
template=template,
)
trainer.train()
推理:
# 使用原生pytorch引擎进行推理
engine = PtEngine(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
即可。常用链接 |
---|
🔥命令行参数 |
支持的模型和数据集 |
自定义模型, 🔥自定义数据集 |
大模型教程 |
支持的训练方法:
方法 | 全参数 | LoRA | QLoRA | Deepspeed | 多机 | 多模态 |
---|---|---|---|---|---|---|
预训练 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
指令监督微调 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
DPO训练 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
GRPO训练 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
奖励模型训练 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
PPO训练 | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ |
KTO训练 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
CPO训练 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
SimPO训练 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
ORPO训练 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
分类模型训练 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
Embedding模型训练 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
预训练:
# 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 \
--train_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 \
--train_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 \
--train_type lora \
--output_dir output \
...
CUDA_VISIBLE_DEVICES=0 swift infer \
--model Qwen/Qwen2.5-7B-Instruct \
--stream true \
--infer_backend pt \
--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 pt \
--temperature 0 \
--max_new_tokens 2048
CUDA_VISIBLE_DEVICES=0 swift app \
--model Qwen/Qwen2.5-7B-Instruct \
--stream true \
--infer_backend pt \
--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 pt \
--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},
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。