
Modelscope社區網站
中文英語
紙|英語文檔| | |
swift2.x en doc | swift2.x中文文檔
您可以通過添加我們的小組與我們聯繫並與我們溝通:
| DISCORD組 | 微信集團 |
|---|---|
![]() | ![]() |
? MS Swift是由Modelscope社區提供的官方框架,用於微調和部署大型語言模型和多模式大型模型。它目前支持培訓(預訓練,微調,人類對齊),400多個大型模型和150多個多模式大型模型的推理,評估,量化和部署。這些大型語言模型(LLM)包括Qwen2.5,Llama3.3,GLM4,InternLM2.5,Yi1.5,Mismtral,DeepSeek2.5,Baichuan2,Gemma2,gemma2和telechat2。多模式LLM包括QWEN2-VL,QWEN2-AUDIO,LLAMA3.2-VISION,LLAVA,INTENTVL2.5,MinICPM-V-2.6,GLM4V,XCOMPOSER2.5,YI-II-VL,YI-vl,DeepSeek-VL2,Phi3.5-Vision和Got-vision和Got-ocr2。
?此外,MS Swift收集了最新的培訓技術,包括Lora,Qlora,Llama-Pro,Longlora,Galore,Galore,Q-Galore,Lora+,Lisa,Dora,Dora,Foura,Fourierft,Reft,Reft,Unsploth和Liger。 MS-SWIFT支持使用VLLM和LMDEPLOY的推理,評估和部署模塊的加速,並使用GPTQ,AWQ和BNB等技術支持大型模型和多模式大型模型的量化。為了幫助研究人員和開發人員進行微調和更輕鬆地應用大型模型,MS-Swift還提供了基於Gradio的Web-UI界面和大量最佳實踐。
為什麼選擇MS-Swift?
--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 . 單個3090 GPU上QWEN2.5-7B教學的10分鐘自我認知微調:
# 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訓練完成後,使用以下命令對受過訓練的權重執行推理。 --adapters選項應替換為培訓生成的最後一個檢查點文件夾。由於適配器文件夾包含來自培訓的參數文件,因此無需單獨指定--model或--system 。
# 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
--max_model_len 8192
--temperature 0
--max_new_tokens 2048Web-UI是基於Gradio接口技術的零閾值培訓和部署接口解決方案。有關更多詳細信息,您可以在此處檢查。
SWIFT_UI_LANG=en swift web-ui
MS-SWIFT還支持使用Python的培訓和推理。以下是用於訓練和推理的偽代碼。有關更多詳細信息,您可以在此處參考。
訓練:
# Retrieve the model and template, and add a trainable LoRA module
model , tokenizer = get_model_tokenizer ( model_id_or_path , ...)
template = get_template ( model . model_meta . template , tokenizer , ...)
model = Swift . prepare_model ( model , lora_config )
# Download and load the dataset, and encode the text into 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 )
# Train the model
trainer = Seq2SeqTrainer (
model = model ,
args = training_args ,
data_collator = template . data_collator ,
train_dataset = train_dataset ,
eval_dataset = val_dataset ,
template = template ,
)
trainer . train ()推理:
# Perform inference using the native PyTorch engine
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部署培訓的最簡單示例。有關更多詳細信息,您可以檢查示例。
| 有用的鏈接 |
|---|
| 命令行參數 |
| 支持的模型和數據集 |
| 自定義模型,自定義數據集 |
| LLM教程 |
預訓練:
# 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 100000
...微調:
CUDA_VISIBLE_DEVICES=0 swift sft
--model Qwen/Qwen2.5-7B-Instruct
--dataset AI-ModelScope/alpaca-gpt4-data-en
--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:en
--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 2048CUDA_VISIBLE_DEVICES=0 swift deploy
--model Qwen/Qwen2.5-7B-Instruct
--infer_backend vllmCUDA_VISIBLE_DEVICES=0 swift eval
--model Qwen/Qwen2.5-7B-Instruct
--infer_backend lmdeploy
--eval_dataset ARC_cCUDA_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此框架是根據Apache許可證(版本2.0)獲得許可的。對於模型和數據集,請參閱原始資源頁面並遵循相應的許可證。
@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 } ,
}