🍲 ms-swift is an official framework provided by the ModelScope community for fine-tuning and deploying large language models and multi-modal large models. It currently supports the training (pre-training, fine-tuning, human alignment), inference, evaluation, quantization, and deployment of 500+ large models and 200+ multi-modal large models. These large language models (LLMs) include models such as Qwen3, Qwen3-MoE, Qwen2.5, InternLM3, GLM4, Mistral, DeepSeek-R1, Yi1.5, TeleChat2, Baichuan2, and Gemma2. The multi-modal LLMs include models such as Qwen2.5-VL, Qwen2-Audio, Llama3.4, Llava, InternVL2.5, MiniCPM-V-2.6, GLM4v, Xcomposer2.5, Yi-VL, DeepSeek-VL2, Phi3.5-Vision, and GOT-OCR2.
🍔 Additionally, ms-swift incorporates the latest training technologies, including lightweight techniques such as LoRA, QLoRA, Llama-Pro, LongLoRA, GaLore, Q-GaLore, LoRA+, LISA, DoRA, FourierFt, ReFT, UnSloth, and Liger, as well as human alignment training methods like DPO, GRPO, RM, PPO, KTO, CPO, SimPO, and ORPO. ms-swift supports acceleration of inference, evaluation, and deployment modules using vLLM and LMDeploy, and it supports model quantization with technologies like GPTQ, AWQ, and BNB. Furthermore, ms-swift offers a Gradio-based Web UI and a wealth of best practices.
Why choose ms-swift?
🍎 Model Types: Supports 500+ pure text large models, 200+ multi-modal large models, as well as All-to-All multi-modal models, sequence classification models, and embedding models, covering the entire process from training to deployment.
Dataset Types: Comes with 150+ pre-training, fine-tuning, human alignment, multi-modal datasets, and supports custom datasets.
Hardware Support: Compatible with CPU, RTX series, T4/V100, A10/A100/H100, Ascend NPU, MPS, etc.
Distributed Training: Supports distributed data parallel (DDP), device_map simple model parallelism, DeepSpeed ZeRO2/ZeRO3, FSDP, and other distributed training techniques.
Quantization Training: Supports training quantized models like BNB, AWQ, GPTQ, AQLM, HQQ, EETQ.
RLHF Training: Supports human alignment training methods such as DPO, GRPO, RM, PPO, KTO, CPO, SimPO, ORPO for both pure text and multi-modal large models.
🍓 Multi-Modal Training: Supports training on different modalities like images, videos, and audio, for tasks like VQA, captioning, OCR, and grounding.
Interface Training: Provides capabilities for training, inference, evaluation, quantization through an interface, completing the whole large model pipeline.
Plugin and Extension: Supports custom model and dataset extensions, as well as customization of components like loss, metric, trainer, loss-scale, callback, optimizer.
🍉 Toolbox Capabilities: Offers not only training support for large models and multi-modal large models but also covers the entire process of inference, evaluation, quantization, and deployment.
Inference Acceleration: Supports inference acceleration engines like PyTorch, vLLM, LmDeploy, and provides OpenAI API for accelerating inference, deployment, and evaluation modules.
Model Evaluation: Uses EvalScope as the evaluation backend and supports evaluation on 100+ datasets for both pure text and multi-modal models.
Model Quantization: Supports AWQ, GPTQ, and BNB quantized exports, with models that can use vLLM/LmDeploy for inference acceleration and continue training.
🎉 News
🎁 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 training script.
🎁 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.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.
If you want to train with a custom dataset, you can refer to this guide to organize your dataset format and specify --dataset <dataset_path>.
The --model_author and --model_name parameters are only effective when the dataset includes swift/self-cognition.
To train with a different model, simply modify --model <model_id/model_path>.
By default, ModelScope is used for downloading models and datasets. If you want to use HuggingFace, simply specify --use_hf true.
After training is complete, use the following command to infer with the trained weights:
Here, --adapters should be replaced with the last checkpoint folder generated during training. Since the adapters folder contains the training parameter file args.json, there is no need to specify --model, --system separately; Swift will automatically read these parameters. To disable this behavior, you can set --load_args false.
shell
1# Using an interactive command line for inference.2CUDA_VISIBLE_DEVICES=0\3swift infer \4 --adapters output/vx-xxx/checkpoint-xxx \5 --stream true\6 --temperature 0\7 --max_new_tokens 204889# merge-lora and use vLLM for inference acceleration10CUDA_VISIBLE_DEVICES=0\11swift infer \12 --adapters output/vx-xxx/checkpoint-xxx \13 --stream true\14 --merge_lora true\15 --infer_backend vllm \16 --max_model_len 8192\17 --temperature 0\18 --max_new_tokens 2048
Finally, use the following command to push the model to ModelScope:
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:
python
1# Retrieve the model and template, and add a trainable LoRA module2model, tokenizer = get_model_tokenizer(model_id_or_path,...)3template = get_template(model.model_meta.template, tokenizer,...)4model = Swift.prepare_model(model, lora_config)56# Download and load the dataset, and encode the text into tokens7train_dataset, val_dataset = load_dataset(dataset_id_or_path,...)8train_dataset = EncodePreprocessor(template=template)(train_dataset, num_proc=num_proc)9val_dataset = EncodePreprocessor(template=template)(val_dataset, num_proc=num_proc)1011# Train the model12trainer = Seq2SeqTrainer(13 model=model,14 args=training_args,15 data_collator=template.data_collator,16 train_dataset=train_dataset,17 eval_dataset=val_dataset,18 template=template,19)20trainer.train()
Inference:
python
1# Perform inference using the native PyTorch engine2engine = PtEngine(model_id_or_path, adapters=[lora_checkpoint])3infer_request = InferRequest(messages=[{'role':'user','content':'who are you?'}])4request_config = RequestConfig(max_tokens=max_new_tokens, temperature=temperature)56resp_list = engine.infer([infer_request], request_config)7print(f'response: {resp_list[0].choices[0].message.content}')
✨ Usage
Here is a minimal example of training to deployment using ms-swift. For more details, you can check the examples.
If you want to use other models or datasets (including multimodal models and datasets), you only need to modify --model to specify the corresponding model's ID or path, and modify --dataset to specify the corresponding dataset's ID or path.
By default, ModelScope is used for downloading models and datasets. If you want to use HuggingFace, simply specify --use_hf true.
This framework is licensed under the Apache License (Version 2.0). For models and datasets, please refer to the original resource page and follow the corresponding License.
📎 Citation
bibtex
1@misc{zhao2024swiftascalablelightweightinfrastructure,
2 title={SWIFT:A Scalable lightWeight Infrastructure for Fine-Tuning},
3 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},
4 year={2024},
5 eprint={2408.05517},
6 archivePrefix={arXiv},
7 primaryClass={cs.CL},
8 url={https://arxiv.org/abs/2408.05517},
9}