ms-swift README

The project's README from github.com/modelscope/ms-swift. As one markdown file: README.md.

SWIFT (Scalable lightWeight Infrastructure for Fine-Tuning)



ModelScope Community Website
中文   |   English  

modelscope/ms-swift | Trendshift

Paper   | English Documentation   |   中文文档  

📖 Table of Contents

☎ Groups

You can contact us and communicate with us by adding our group:

Discord Group WeChat Group

📝 Introduction

🍲 ms-swift is a large model and multimodal large model fine-tuning and deployment framework provided by the ModelScope community. It now supports training (pre-training, fine-tuning, human alignment), inference, evaluation, quantization, and deployment for 600+ text-only large models and 400+ multimodal large models. Large models include: Qwen3, Qwen3.5, InternLM3, GLM4.5, Mistral, DeepSeek-R1, Llama4, etc. Multimodal large models include: Qwen3-VL, Qwen3-Omni, Kimi-K3, Llava, InternVL3.5, MiniCPM-V-4, Ovis2.5, GLM4.5-V, DeepSeek-VL2, etc.

🍔 In addition, ms-swift integrates the latest training technologies, including Megatron parallelism techniques such as TP, PP, CP, EP to accelerate training, as well as numerous GRPO algorithm family reinforcement learning algorithms including: GRPO, DAPO, GSPO, SAPO, CISPO, RLOO, Reinforce++, etc. to enhance model intelligence. ms-swift supports a wide range of training tasks, including preference learning algorithms such as DPO, KTO, RM, CPO, SimPO, ORPO, as well as Embedding, Reranker, and sequence classification tasks. ms-swift provides full-pipeline support for large model training, including acceleration for inference, evaluation, and deployment modules using vLLM, SGLang, and LMDeploy, as well as model quantization using GPTQ, AWQ, BNB, and FP8 technologies.

Why Choose ms-swift?

🎉 News

More
  • 🎁 2025.06.11: Support for using Megatron parallelism techniques for RLHF training. The training script can be found here.
  • 🎁 2025.05.29: Support sequence parallel in pretrain, sft, dpo and grpo, check script here.
  • 🎁 2025.05.11: GRPO now supports custom processing logic for reward models. See the GenRM example here.
  • 🎁 2025.04.15: The ms-swift paper has been accepted by AAAI 2025. You can find the paper at this link.
  • 🎁 2025.03.23: Multi-round GRPO is now supported for training multi-turn dialogue scenarios (e.g., agent tool calling). Please refer to the doc.
  • 🎁 2025.03.16: Support for Megatron's parallel training techniques is now available. Please see the Megatron-SWIFT training documentation.
  • 🎁 2025.03.15: Fine-tuning of embedding models for both pure text and multimodal models is supported. Please check the training script.
  • 🎁 2025.03.05: The hybrid mode for GRPO is supported, with a script for training a 72B model on 4 GPUs (4*80G) available here. Tensor parallelism with vllm is also supported, with the training script available here.
  • 🎁 2025.02.21: The GRPO algorithm now supports LMDeploy, with the training script available here. Additionally, the performance of the GRPO algorithm has been tested, achieving a training speed increase of up to 300% using various tricks. Please check the WanDB table here.
  • 🎁 2025.02.21: The swift sample command is now supported. The reinforcement fine-tuning script can be found here, and the large model API distillation sampling script is available here.
  • 🔥 2025.02.12: Support for the GRPO (Group Relative Policy Optimization) training algorithm has been added. Documentation is available here.
  • 🎁 2024.12.04: Major update to ms-swift 3.0. Please refer to the release notes and changes.
  • 🎉 2024.08.12: The ms-swift paper has been published on arXiv and can be read here.
  • 🔥 2024.08.05: Support for using evalscope as a backend for evaluating large models and multimodal models.
  • 🔥 2024.07.29: Support for using vllm and lmdeploy to accelerate inference for large models and multimodal models. When performing infer/deploy/eval, you can specify --infer_backend vllm/lmdeploy.
  • 🔥 2024.07.24: Support for human preference alignment training for multimodal large models, including DPO/ORPO/SimPO/CPO/KTO/RM/PPO.
  • 🔥 2024.02.01: Support for Agent training! The training algorithm is derived from this paper.

🛠️ Installation

To install using pip:

pip install ms-swift -U

# Using uv
pip install uv
uv pip install ms-swift -U --torch-backend=auto

To install from source:

# pip install git+https://github.com/modelscope/ms-swift.git

git clone https://github.com/modelscope/ms-swift.git
cd ms-swift
# The main branch is for swift 4.x. To install swift 3.x, please run the following command:
# git checkout release/3.12
pip install -e .

# Using uv
uv pip install -e . --torch-backend=auto

Running Environment:

Range Recommended Notes
python >=3.10 3.12
cuda cuda12.8/13.0 No need to install if using CPU, NPU, MPS
torch >=2.0 2.8.0/2.11.0
transformers >=4.33 4.57.6/5.12.1
modelscope >=1.23
datasets >=3.0,<4.8.5 3.6.0/4.8.4
peft >=0.11,<0.21
flash_attn 2.8.3/4.0.0b15
trl >=0.15,<1.0 0.29.1 RLHF
deepspeed >=0.14 0.18.9 Training
vllm >=0.5.1 0.11.0/0.23.0 Inference/Deployment
sglang >=0.4.6 Inference/Deployment
evalscope >=1.0 Evaluation
gradio 5.32.1 Web-UI/App

For more optional dependencies, you can refer to here.

🚀 Quick Start

10 minutes of self-cognition fine-tuning of Qwen3-4B-Instruct-2507 on a single 3090 GPU:

# 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

Tips:

After training is complete, use the following command to infer with the trained weights:

# Using an interactive command line for inference.
CUDA_VISIBLE_DEVICES=0 \
swift infer \
    --adapters output/vx-xxx/checkpoint-xxx \
    --stream true \
    --temperature 0 \
    --max_new_tokens 2048

# merge-lora and use vLLM for inference acceleration
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

Finally, use the following command to push the model to 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

The Web-UI is a zero-threshold training and deployment interface solution based on Gradio interface technology. For more details, you can check here.

SWIFT_UI_LANG=en swift web-ui

image.png

Using Python

ms-swift also supports training and inference using Python. Below is pseudocode for training and inference. For more details, you can refer to here.

Training:

from peft import LoraConfig, get_peft_model
from swift import get_model_processor, get_template, load_dataset, EncodePreprocessor
from swift.trainers import Seq2SeqTrainer, Seq2SeqTrainingArguments
# Retrieve the model and template, and add a trainable LoRA module
model, tokenizer = get_model_processor(model_id_or_path, ...)
template = get_template(tokenizer, ...)
lora_config = LoraConfig(...)
model = get_peft_model(model, lora_config)

# Download and load the dataset, and encode the text into tokens
train_dat

ms-swift (modelscope/ms-swift)

Searching Database…

15.9K ·GitHub
这是什么?ms-swift 是 ModelScope 社区推出的大语言模型和多模态模型微调与部署框架,把训练、推理、评估、量化和部署整合在一条流程里。
它是 Python 库、命令行工具,还是网页应用?它是一个 Python 框架,有三种使用方式:swift 命令行界面(推荐)、可以零代码训练的 Gradio Web-UI,以及普通的 Python API。
怎么安装,然后跑第一次微调?先 pip install ms-swift,然后在命令行里训练——README 演示了在单张 RTX 3090 上花 10 分钟对 Qwen3-4B-Instruct-2507 做自我认知微调,之后用 swift infer 加载你的 --adapters checkpoint 文件夹进行推理。
我的环境需要提前装好什么?需要 Python >=3.10(推荐 3.12)、PyTorch >=2.0 和 transformers >=4.33。GPU 训练要 CUDA 12.8/13.0——在 CPU、NPU 或 MPS 上则不需要。装上 vLLM 或 SGLang 可以解锁快速推理,evalscope 负责评估。
开始训练前有什么要注意的坑?模型和数据集默认从 ModelScope 下载——如果你想从 HuggingFace 拉取,加上 --use_hf true 即可。
我需要 A100 这类数据中心级 GPU 吗?不需要——消费级 RTX 显卡可以用,同时支持 A10/A100/H100、T4/V100、AMD MI300、昇腾 NPU、Apple MPS 和普通 CPU,而且量化训练下一个 7B 模型只需大约 9GB 的资源。
用它都能训练哪些模型?支持 600 多个纯文本大模型——Qwen3/Qwen3.5、DeepSeek-R1、Llama4、GLM4.5、InternLM3、Mistral——还有 400 多个多模态模型,比如 Qwen3-VL、Qwen3-Omni、Kimi-K3、InternVL3.5、Llava 和 GLM4.5-V,热门新模型发布当天就能支持。
除了 SFT,它支持 RLHF 吗?支持。它包含 GRPO 家族——GRPO、DAPO、GSPO、SAPO、CISPO、RLOO、Reinforce++、CHORD——还有 DPO、KTO、RM、CPO、SimPO、ORPO 等偏好优化算法,甚至支持 Embedding、Reranker 和序列分类任务的训练。
微调完之后,能把模型部署成 API 吗?可以——推理和部署基于 vLLM、SGLang 或 LMDeploy,对外提供 OpenAI 兼容接口;模型可以导出为 AWQ/GPTQ/FP8/BNB 量化格式;EvalScope 还能在 100 多个数据集上做评估。
Megatron-SWIFT 是干什么的?用于大规模训练:利用 Megatron 的并行策略(TP/PP/SP/CP/ETP/EP/VPP)大幅加速 MoE 训练,支持 300 多个文本模型和 100 多个多模态模型的全参数训练和 LoRA,覆盖 CPT/SFT/GRPO/DPO/KTO/RM 等任务。
What are similar projects?halo, LoongForge, verl, verl, open-r1, jeeves, unsloth, airllm
Creator
modelscope
Size
85.5 MB
Stars
15.9K
Age
3y
Commits · 90 Days