10分钟微调专属于自己的大模型

本文涉及的产品
模型训练 PAI-DLC,5000CU*H 3个月
模型在线服务 PAI-EAS,A10/V100等 500元 1个月
交互式建模 PAI-DSW,5000CU*H 3个月
简介: 本文主要介绍使用魔搭社区轻量级训练推理工具SWIFT,进行大模型自我认知微调,帮助初阶炼丹师快速微调出专属于自己的大模型。

本文主要介绍使用魔搭社区轻量级训练推理工具SWIFT,进行大模型自我认知微调,帮助初阶炼丹师快速微调出专属于自己的大模型。

SWIFT(Scalable lightWeight Infrastructure for Fine-Tuning)是魔搭ModelScope开源社区推出的一套完整的轻量级训练推理工具,基于PyTorch的轻量级、开箱即用的模型微调、推理框架,让AI爱好者用自己的消费级显卡就能玩转大模型和AIGC。

魔搭官方,公众号:魔搭ModelScope社区大模型时代,还缺一只雨燕 | SWIFT:魔搭社区轻量级微调推理框架

接下来进入手把手实操

环境安装

# 设置pip全局镜像
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
# 安装ms-swift
git clone https://github.com/modelscope/swift.git
cd swift
pip install -e .[llm]
# 环境对齐 (如果你运行错误, 可以跑下面的代码, 仓库使用最新环境测试)
pip install -r requirements/framework.txt  -U
pip install -r requirements/llm.txt  -U

微调前推理

使用python:

import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
from swift.llm import ModelType, InferArguments, infer_main
infer_args = InferArguments(
    model_type=ModelType.qwen_7b_chat, eval_human=True)
infer_main(infer_args)
"""
<<< 你是谁?
我是通义千问,由阿里云开发的预训练语言模型。我被设计用来回答各种问题、提供信息和与用户进行对话。有什么我可以帮助你的吗?
--------------------------------------------------
<<< what's your name?
My name is QianWen, developed by Alibaba Cloud. I am designed to answer various questions, provide information, and engage in conversation with users. How can I assist you today?
--------------------------------------------------
<<< 你是谁研发的?
我是由阿里云研发的预训练语言模型。我被设计用来回答各种问题、提供信息和与用户进行对话。有什么我可以帮助你的吗?
--------------------------------------------------
<<< 浙江的省会在哪?
浙江省的省会是杭州市。
--------------------------------------------------
<<< 这有什么好玩的?
杭州是一个历史悠久的城市,拥有许多著名的景点,如西湖、灵隐寺、宋城等。此外,杭州还有许多美食,如西湖醋鱼、龙井虾仁等。
--------------------------------------------------
<<< 晚上睡不着觉怎么办
如果你晚上睡不着觉,可以尝试以下方法:
1. 保持规律的作息时间:尽量每天在同一时间上床睡觉和起床,这有助于调整你的生物钟。
2. 避免在睡前使用电子设备:电子设备发出的蓝光可能会干扰你的睡眠。
3. 创造一个舒适的睡眠环境:保持房间安静、黑暗和凉爽,使用舒适的床上用品。
4. 放松身心:尝试一些放松技巧,如深呼吸、瑜伽或冥想。
5. 避免摄入咖啡因和酒精:这些物质可能会影响你的睡眠质量。
如果以上方法都不能帮助你改善睡眠,建议你咨询医生或专业的睡眠治疗师。
"""

使用CLI:

CUDA_VISIBLE_DEVICES=0 swift infer --model_type qwen-7b-chat --eval_human true

模型微调

提示: 因为自我认知训练涉及到知识编辑, 建议对MLP加lora_target_modules. 你可以通过指定--lora_target_modules ALL在所有的linear层(包括qkvo以及mlp)加lora. 这通常是效果最好的.

使用python:

# Experimental environment: A10, 3090, V100, ...
# 18GB GPU memory
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
from swift.llm import DatasetName, ModelType, SftArguments, sft_main
sft_args = SftArguments(
    model_type=ModelType.qwen_7b_chat,
    dataset=[DatasetName.alpaca_zh, DatasetName.alpaca_en],
    train_dataset_sample=500,
    eval_steps=20,
    logging_steps=5,
    output_dir='output',
    lora_target_modules='ALL',
    self_cognition_sample=500,
    model_name=['小黄', 'Xiao Huang'],
    model_author=['魔搭', 'ModelScope'])
output = sft_main(sft_args)
best_model_checkpoint = output['best_model_checkpoint']
print(f'best_model_checkpoint: {best_model_checkpoint}')
"""Out[0]
  0%|                                                                                                       | 0/62 [00:00<?, ?it/s]`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`...
{'loss': 1.7662447, 'acc': 0.6080637, 'learning_rate': 9.993e-05, 'epoch': 0.08, 'global_step': 5}
{'loss': 1.54578514, 'acc': 0.62391958, 'learning_rate': 9.738e-05, 'epoch': 0.16, 'global_step': 10}
{'loss': 1.36477156, 'acc': 0.6370544, 'learning_rate': 9.138e-05, 'epoch': 0.24, 'global_step': 15}
{'loss': 1.30135965, 'acc': 0.66137586, 'learning_rate': 8.237e-05, 'epoch': 0.32, 'global_step': 20}
 32%|██████████████████████████████▎                                                               | 20/62 [02:09<04:26,  6.34s/it]
{'eval_loss': 1.30556977, 'eval_acc': 0.6398641, 'eval_runtime': 3.0399, 'eval_samples_per_second': 1.645, 'eval_steps_per_second': 1.645, 'epoch': 0.32, 'global_step': 20}
100%|████████████████████████████████████████████████████████████████████████████████████████████████| 5/5 [00:01<00:00,  4.53it/s]
[INFO:swift] Saving model checkpoint to /mnt/workspace/my_git/swift/output/qwen-7b-chat/v5-20231202-153019/checkpoint-20
{'loss': 1.27507048, 'acc': 0.66330776, 'learning_rate': 7.099e-05, 'epoch': 0.4, 'global_step': 25}
{'loss': 1.25882282, 'acc': 0.65959673, 'learning_rate': 5.809e-05, 'epoch': 0.48, 'global_step': 30}
{'loss': 1.3049325, 'acc': 0.65535874, 'learning_rate': 4.459e-05, 'epoch': 0.56, 'global_step': 35}
{'loss': 1.12174425, 'acc': 0.69360948, 'learning_rate': 3.149e-05, 'epoch': 0.64, 'global_step': 40}
 65%|████████████████████████████████████████████████████████████▋                                 | 40/62 [04:20<02:20,  6.39s/it]
{'eval_loss': 1.30136049, 'eval_acc': 0.64552661, 'eval_runtime': 2.9678, 'eval_samples_per_second': 1.685, 'eval_steps_per_second': 1.685, 'epoch': 0.64, 'global_step': 40}
100%|████████████████████████████████████████████████████████████████████████████████████████████████| 5/5 [00:01<00:00,  4.53it/s]
[INFO:swift] Saving model checkpoint to /mnt/workspace/my_git/swift/output/qwen-7b-chat/v5-20231202-153019/checkpoint-40
{'loss': 1.1997283, 'acc': 0.67508869, 'learning_rate': 1.974e-05, 'epoch': 0.72, 'global_step': 45}
{'loss': 1.06885223, 'acc': 0.7037468, 'learning_rate': 1.02e-05, 'epoch': 0.8, 'global_step': 50}
{'loss': 0.90335083, 'acc': 0.73890958, 'learning_rate': 3.55e-06, 'epoch': 0.88, 'global_step': 55}
{'loss': 1.03582478, 'acc': 0.71592231, 'learning_rate': 2.9e-07, 'epoch': 0.96, 'global_step': 60}
 97%|██████████████████████████████████████████████████████████████████████████████████████████▉   | 60/62 [06:32<00:12,  6.40s/it]
{'eval_loss': 1.29918265, 'eval_acc': 0.64665912, 'eval_runtime': 3.0779, 'eval_samples_per_second': 1.624, 'eval_steps_per_second': 1.624, 'epoch': 0.96, 'global_step': 60}
100%|████████████████████████████████████████████████████████████████████████████████████████████████| 5/5 [00:01<00:00,  4.52it/s]
[INFO:swift] Saving model checkpoint to /mnt/workspace/my_git/swift/output/qwen-7b-chat/v5-20231202-153019/checkpoint-60
100%|██████████████████████████████████████████████████████████████████████████████████████████████| 62/62 [06:49<00:00,  7.24s/it]
{'eval_loss': 1.29818368, 'eval_acc': 0.65005663, 'eval_runtime': 3.7344, 'eval_samples_per_second': 1.339, 'eval_steps_per_second': 1.339, 'epoch': 0.99, 'global_step': 62}
100%|████████████████████████████████████████████████████████████████████████████████████████████████| 5/5 [00:01<00:00,  4.53it/s]
[INFO:swift] Saving model checkpoint to /mnt/workspace/my_git/swift/output/qwen-7b-chat/v5-20231202-153019/checkpoint-62
[INFO:swift] Loading best model from /mnt/workspace/my_git/swift/output/qwen-7b-chat/v5-20231202-153019/checkpoint-62 (score: 1.29818368).
{'train_runtime': 414.5023, 'train_samples_per_second': 2.413, 'train_steps_per_second': 0.15, 'train_loss': 1.2601544, 'epoch': 0.99, 'global_step': 62}
100%|██████████████████████████████████████████████████████████████████████████████████████████████| 62/62 [06:54<00:00,  6.68s/it]
[INFO:swift] best_model_checkpoint: /mnt/workspace/my_git/swift/output/qwen-7b-chat/v5-20231202-153019/checkpoint-62
[INFO:swift] images_dir: /mnt/workspace/my_git/swift/output/qwen-7b-chat/v5-20231202-153019/images
best_model_checkpoint: /mnt/workspace/my_git/swift/output/qwen-7b-chat/v5-20231202-153019/checkpoint-62
"""

使用CLI (单卡):

# Experimental environment: A10, 3090, V100, ...
# 22GB GPU memory
CUDA_VISIBLE_DEVICES=0 \
swift sft \
    --model_type qwen-7b-chat \
    --dataset alpaca-zh alpaca-en \
    --train_dataset_sample 500 \
    --eval_steps 20 \
    --logging_steps 5 \
    --output_dir output \
    --lora_target_modules ALL \
    --self_cognition_sample 500 \
    --model_name 小黄 'Xiao Huang' \
    --model_author 魔搭 ModelScope \

使用CLI (DDP):

# Experimental environment: 2 * 3090, 2 * V100, ...
# 2 * 24GB GPU memory
CUDA_VISIBLE_DEVICES=0,1 \
NPROC_PER_NODE=2 \
swift sft \
    --model_type qwen-7b-chat \
    --dataset alpaca-zh alpaca-en \
    --train_dataset_sample 500 \
    --eval_steps 20 \
    --logging_steps 5 \
    --output_dir output \
    --lora_target_modules ALL \
    --self_cognition_sample 500 \
    --model_name 小黄 'Xiao Huang' \
    --model_author 魔搭 ModelScope \

使用CLI (CPU, 1.8b):

# Time Consumed: 40min
CUDA_VISIBLE_DEVICES=-1 \
swift sft \
    --model_type qwen-1_8b-chat \
    --dataset alpaca-zh alpaca-en \
    --train_dataset_sample 500 \
    --eval_steps 20 \
    --logging_steps 5 \
    --output_dir output \
    --lora_target_modules ALL \
    --self_cognition_sample 500 \
    --model_name 小黄 'Xiao Huang' \
    --model_author 魔搭 ModelScope \

微调后推理

你需要设置best_model_checkpoint的值, 该值会在sft的最后被打印出来.

使用python:

import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
from swift.llm import InferArguments, merge_lora_main, infer_main
best_model_checkpoint = 'qwen-7b-chat/vx-xxx/checkpoint-xxx'
infer_args = InferArguments(
    ckpt_dir=best_model_checkpoint,
    eval_human=True)
# merge_lora_main(infer_args)
result = infer_main(infer_args)
"""Out[0]
<<< 你是谁?
我是小黄,由魔搭开发的人工智能语言模型。我可以回答你的问题、提供信息、进行对话和帮助你解决问题。如果你有任何疑问或需要帮助,请随时告诉我。
--------------------------------------------------
<<< what's your name?
My name is Xiao Huang, developed by ModelScope. I am an artificial intelligence language model capable of answering questions, providing information, engaging in conversation, and assisting you with problems. If you have any questions or need help, feel free to let me know.
--------------------------------------------------
<<< 你是谁研发的?
我是由魔搭开发的人工智能语言模型,被称为小黄。魔搭是一个专注于人工智能研究和开发的组织,致力于推动人工智能技术的发展和应用。
--------------------------------------------------
<<< 浙江的省会在哪?
浙江省的省会是杭州。
--------------------------------------------------
<<< 这有什么好玩的?
杭州有许多有趣的地方可以探索,例如西湖、灵隐寺、宋城、千岛湖等。此外,杭州还有许多美食和文化活动,如龙井茶、宋城千古情、西溪湿地等。总之,杭州是一个充满活力和魅力的城市,值得一游。
--------------------------------------------------
<<< 晚上睡不着觉怎么办
如果你晚上睡不着觉,可以尝试以下方法来帮助自己入睡:
1. 保持良好的睡眠习惯:每天定时上床睡觉和起床,避免在睡前使用电子设备。
2. 放松身心:在睡前做一些放松的活动,如阅读、听轻音乐、做深呼吸等。
3. 避免摄入咖啡因和酒精:这些物质会影响你的睡眠质量。
4. 调整环境:确保你的睡眠环境安静、黑暗和舒适。
5. 如果以上方法都不能帮助你入睡,建议咨询医生或专业人士寻求帮助。
"""

使用CLI:

# 直接推理
CUDA_VISIBLE_DEVICES=0 swift infer --ckpt_dir 'qwen-7b-chat/vx-xxx/checkpoint-xxx' --eval_human true
# Merge LoRA增量权重并推理
swift merge-lora --ckpt_dir 'xxx/vx_xxx/checkpoint-xxx'
CUDA_VISIBLE_DEVICES=0 swift infer --ckpt_dir 'qwen-7b-chat/vx-xxx/checkpoint-xxx-merged' --eval_human true

Web-UI

使用python:

import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
from swift.llm import InferArguments, merge_lora_main, app_ui_main
best_model_checkpoint = 'qwen-7b-chat/vx-xxx/checkpoint-xxx'
infer_args = InferArguments(
    ckpt_dir=best_model_checkpoint,
    eval_human=True)
# merge_lora_main(infer_args)
result = app_ui_main(infer_args)

使用CLI:

# 直接使用web-ui
CUDA_VISIBLE_DEVICES=0 swift app-ui --ckpt_dir 'qwen-7b-chat/vx-xxx/checkpoint-xxx' --eval_human true
# Merge LoRA增量权重并使用web-ui
swift merge-lora --ckpt_dir 'xxx/vx_xxx/checkpoint-xxx'
CUDA_VISIBLE_DEVICES=0 swift app-ui --ckpt_dir 'qwen-7b-chat/vx-xxx/checkpoint-xxx-merged' --eval_human true

可视化界面案例展示:

本文为SWIFT LLM&AIGC微调场景化最佳实践系列之一,后续将继续通过魔搭社区推出场景化教程。目前SWIFT已支持94个大模型,51个数据集,支持LoRA、QLoRA、LongLoRA等十余种tuners,一行代码即可开启模型训练,欢迎对大模型和AIGC微调部署感兴趣的开发者小伙伴们多多交流!

Github:

https://github.com/modelscope/swift

官方交流群:

点击直达SWIFT开源链接,感谢支持star~

https://github.com/modelscope/swift

相关文章
|
4月前
|
存储 自然语言处理 算法
【学习大模型】RAG基础
RAG(Retrieval-Augmented Generation)技术是为了解决大模型中的幻觉问题、实时交互、数据安全和知识动态性挑战。它结合了搜索和大模型的提示功能,使模型能基于检索到的信息生成更准确的回答。RAG通过向量数据库和向量检索,将文本转化为向量表示,然后进行相似度计算和检索,以提供上下文相关的信息。
553 1
|
4月前
|
文字识别 前端开发
CodeFuse-VLM 开源,支持多模态多任务预训练/微调
随着huggingface开源社区的不断更新,会有更多的vision encoder 和 LLM 底座发布,这些vision encoder 和 LLM底座都有各自的强项,例如 code-llama 适合生成代码类任务,但是不适合生成中文类的任务,因此用户常常需要根据vision encoder和LLM的特长来搭建自己的多模态大语言模型。针对多模态大语言模型种类繁多的落地场景,我们搭建了CodeFuse-VLM 框架,支持多种视觉模型和语言大模型,使得MFT-VLM可以适应不同种类的任务。
594 0
|
14天前
|
数据可视化 Swift
小钢炮进化,MiniCPM 3.0 开源!4B参数超GPT3.5性能,无限长文本,超强RAG三件套!模型推理、微调实战来啦!
旗舰端侧模型面壁「小钢炮」系列进化为全新 MiniCPM 3.0 基座模型,再次以小博大,以 4B 参数,带来超越 GPT-3.5 的性能。并且,量化后仅 2GB 内存,端侧友好。
小钢炮进化,MiniCPM 3.0 开源!4B参数超GPT3.5性能,无限长文本,超强RAG三件套!模型推理、微调实战来啦!
|
16天前
|
存储 人工智能 物联网
LoRA大模型微调的利器
LoRA模型是小型的Stable Diffusion模型,它们对checkpoint模型进行微小的调整。它们的体积通常是检查点模型的10到100分之一。因为体积小,效果好,所以lora模型的使用程度比较高。
LoRA大模型微调的利器
|
1月前
|
机器学习/深度学习 数据采集 物联网
【机器学习】Google开源大模型Gemma2:原理、微调训练及推理部署实战
【机器学习】Google开源大模型Gemma2:原理、微调训练及推理部署实战
65 0
|
4月前
|
并行计算 算法 物联网
LLM 大模型学习必知必会系列(七):掌握分布式训练与LoRA/LISA微调:打造高性能大模型的秘诀进阶实战指南
LLM 大模型学习必知必会系列(七):掌握分布式训练与LoRA/LISA微调:打造高性能大模型的秘诀进阶实战指南
LLM 大模型学习必知必会系列(七):掌握分布式训练与LoRA/LISA微调:打造高性能大模型的秘诀进阶实战指南
|
3月前
|
机器学习/深度学习 语音技术
多模态大模型不够灵活,谷歌DeepMind创新架构Zipper:分开训练再压缩
【6月更文挑战第12天】谷歌DeepMind的Zipper架构解决了多模态大模型灵活性问题,通过分解为单模态模型并用“压缩”过程组合,实现多模态生成。该方法允许独立训练每个模态,提升灵活性和可扩展性,适用于数据有限或领域特定的模态。Zipper利用交叉注意力机制融合模态输出,适用于图像描述、语音识别等任务。尽管需要更多计算资源且性能受限于单模态模型质量,但已在ASR和TTS领域展现潜力。论文链接:https://arxiv.org/pdf/2405.18669
47 3
|
4月前
|
人工智能 Python
LLM 大模型学习必知必会系列(八):10分钟微调专属于自己的大模型
LLM 大模型学习必知必会系列(八):10分钟微调专属于自己的大模型
|
4月前
|
人工智能 PyTorch iOS开发
苹果AppleMacOs最新Sonoma系统本地训练和推理GPT-SoVITS模型实践
GPT-SoVITS是少有的可以在MacOs系统下训练和推理的TTS项目,虽然在效率上没有办法和N卡设备相提并论,但终归是开发者在MacOs系统构建基于M系列芯片AI生态的第一步。
苹果AppleMacOs最新Sonoma系统本地训练和推理GPT-SoVITS模型实践
|
4月前
|
人工智能 自然语言处理 开发者
MoE模型将成为主流
MoE模型将成为主流
162 6
MoE模型将成为主流