目錄
簡介
1. 創建虛擬環境
2 安裝ms-swift
3安裝其他依賴庫
4. 下載數據集
5.開始embedding模型訓練
6. 自定義數據格式和對應的Loss類型
(1) infoNCE損失? ? ?
(2)余弦相似度損失
(3)對比學習損失
(4).在線對比學習損失
(5)損失函數總結
簡介
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、GKD、KTO、CPO、SimPO、ORPO等人類對齊訓練方法。ms-swift支持使用vLLM、SGLang和LMDeploy對推理、評測和部署模塊進行加速,并支持使用GPTQ、AWQ、BNB等技術對大模型進行量化。ms-swift還提供了基于Gradio的Web-UI界面及豐富的最佳實踐。
https://github.com/modelscope/ms-swift?tab=readme-ov-file
1. 創建虛擬環境
conda create -n swift_venv python=3.12 -y
conda init bash && source /root/.bashrc
conda activate swift_venv
conda install ipykernel
ipython kernel install --user --name=swift_venv
2 安裝ms-swift
#1使用pip安裝,把ms-swift作為一個庫,安裝到anaconda的虛擬環境中
pip install ms-swift -U#2從源碼克隆安裝(推薦)pip install git+https://github.com/modelscope/ms-swift.git#3從源碼安裝
git clone https://github.com/modelscope/ms-swift.git
cd ms-swift
pip install -e .
3安裝其他依賴庫
pip install deepspeed liger-kernel
pip install scikit-learn
pip install -U sentence-transformers
# 建議科學上?后,再執?下?的命令
pip install flash-attn --no-build-isolation
4. 下載數據集
# 設置下?命令,?需科學上?
export HF_ENDPOINT=https://hf-mirror.com
pip install --upgrade huggingface_hub
huggingface-cli download --repo-type dataset --resume-download microsoft/ms_marco --local-dir 下載保存路徑
數據格式:
5.開始embedding模型訓練
# 把下面命令,保存為train.sh格式, 運行bash train.sh命令,啟動訓練CUDA_VISIBLE_DEVICES=0 \
swift sft \--model /data/qwen3_embedding/Qwen3-Embedding-0.6B \--task_type embedding \--train_type full \ #訓練模式full lora --torch_dtype bfloat16 \--num_train_epochs 100 \ #訓練輪數--per_device_train_batch_size 16 \ #訓練批次--per_device_eval_batch_size 16\--learning_rate 1e-4 \--lora_rank 8 \--lora_alpha 8 \--target_modules all-linear \ #目標模塊 all-attention--gradient_accumulation_steps 1 \--eval_steps 1 \--save_steps 1 \--save_total_limit 2 \--logging_steps 5 \--max_length 512 \--output_dir output \ #輸出路徑--warmup_ratio 0.05 \--dataloader_num_workers 8 \--model_author swift \--model_name swift-robot \--split_dataset_ratio 0.3 \ #train 和val分割比例--dataset /home/dataset \ #數據集路徑--loss_type infonce # 損失函數3種類型 contrastive cosine_similarity infonce
?使用swift sft --help命令查詢有哪些訓練設置參數。
6. 自定義數據格式和對應的Loss類型
(1) infoNCE損失? ? ?
? --loss_type? infonce
對?學習損失函數,最?化正樣
本對相似度,最?化負樣本對相似度 .
使?批內對?學習策略,將同批次內其他樣本作為負樣本.
數據格式:
[{"query": "如何學習編程?","response": "可以從Python語言開始入門,它語法簡單適合初學者。","rejected_response": ["隨便看看書就會了", "編程很難學不會的"]},{"query": "推薦一款性價比高的手機","response": "Redmi Note系列在2000元價位段表現均衡,值得考慮。","rejected_response": ["越貴的手機越好", "蘋果手機永遠是最好的"]}
]
?
(2)余弦相似度損失
?--loss_type cosine_similarity
直接優化預測相似度與真實相似度標簽的差異 ,使? MSE 損失計算 ||input_label - cosine_sim(u,v)||_2
數據格式:
[{"query": "A dog is barking loudly.","response": "The canine is making loud barking noises.","label": 0.8},{"query": "Children are playing in the park.","response": "Kids are playing in the playground.","label": 1.0},{"query": "The sun is shining brightly.","response": "Bright sunlight is visible.","label": 0.7}
]
(3)對比學習損失
?--loss_type contrastive
經典的對?學習損失,正樣本拉近,負樣本推遠 需要設置 margin 參數。
[{"query": "A dog is barking loudly.","response": "The canine is making loud barking noises.","label": 1},{"query": "Children are playing in the park.","response": "Kids are playing in the playground.","label": 1}]
(4).在線對比學習損失
--loss_type online_contrastive
對?學習的改進版本,選擇困難正樣本和困難負樣本 通常?標準對?學習效果更好。
(5)損失函數總結
?