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

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

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

接下来进入手把手实操,如果你有任何疑问、想参与技术交流,文末可以加我们的技术交流群

环境安装

# 设置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

技术交流群

前沿技术资讯、算法交流、求职内推、算法竞赛、面试交流(校招、社招、实习)等、与 10000+来自港科大、北大、清华、中科院、CMU、腾讯、百度等名校名企开发者互动交流~

建了技术答疑、交流群!想要进交流群、需要资料的同学,可以直接加微信号:mlc2060。加的时候备注一下:研究方向 +学校/公司+CSDN,即可。然后就可以拉你进群了。

方式①、添加微信号:mlc2060,备注:技术交流
方式②、微信搜索公众号:机器学习社区,后台回复:技术交流

资料1
在这里插入图片描述

资料2
在这里插入图片描述

  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值