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

简介: 本文主要介绍使用魔搭社区轻量级训练推理工具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月前
|
存储 机器学习/深度学习 人工智能
大模型微调技术:LoRA原理与实践
本文深入解析大语言模型微调中的关键技术——低秩自适应(LoRA)。通过分析全参数微调的计算瓶颈,详细阐述LoRA的数学原理、实现机制和优势特点。文章包含完整的PyTorch实现代码、性能对比实验以及实际应用场景,为开发者提供高效微调大模型的实践指南。
2619 2
|
6月前
|
XML JSON 数据库
大模型不听话?试试提示词微调
想象一下,你向大型语言模型抛出问题,满心期待精准回答,得到的却是答非所问,是不是让人抓狂?在复杂分类场景下,这种“大模型不听话”的情况更是常见。
390 9
|
5月前
|
人工智能 自然语言处理 测试技术
有没有可能不微调也能让大模型准确完成指定任务?(少样本学习)
对于我这种正在从0到1构建AI产品的一人公司来说,Few Shots学习的最大价值在于:用最少的资源获得最大的效果。我不需要大量的标注数据,不需要复杂的模型训练,只需要精心设计几个示例,就能让大模型快速理解我的业务场景。
424 43
|
4月前
|
存储 数据采集 自然语言处理
56_大模型微调:全参数与参数高效方法对比
随着大型语言模型(LLM)规模的不断增长,从数百亿到数千亿参数,传统的全参数微调方法面临着计算资源消耗巨大、训练效率低下等挑战。2025年,大模型微调技术已经从早期的全参数微调发展到如今以LoRA、QLoRA为代表的参数高效微调方法,以及多种技术融合的复杂策略。本文将深入对比全参数微调和参数高效微调的技术原理、适用场景、性能表现和工程实践,为研究者和工程师提供全面的技术参考。
|
4月前
|
机器学习/深度学习 存储 人工智能
大模型微调:从理论到实践的全面指南
🌟蒋星熠Jaxonic:AI探索者,专注大模型微调技术。从LoRA到RLHF,实践医疗、法律等垂直领域模型优化,分享深度学习的科学与艺术,共赴二进制星河的极客征程。
大模型微调:从理论到实践的全面指南
|
5月前
|
机器学习/深度学习 数据采集 算法
大模型微调技术综述与详细案例解读
本文是一篇理论与实践结合的综述文章,综合性全面介绍大模型微调技术。本文先介绍大模型训练的两类场景:预训练和后训练,了解业界常见的模型训练方法。在后训练介绍内容中,引出模型微调(模型微调是属于后训练的一种)。然后,通过介绍业界常见的模型微调方法,以及通过模型微调实操案例的参数优化、微调过程介绍、微调日志解读,让读者对模型微调有更加直观的了解。最后,我们详细探讨数据并行训练DDP与模型并行训练MP两类模型并行训练技术,讨论在实际项目中如何选择两类并行训练技术。
|
9月前
|
机器学习/深度学习 数据采集 存储
大模型微调知识与实践分享
本文详细介绍了大型语言模型(LLM)的结构、参数量、显存占用、存储需求以及微调过程中的关键技术点,包括Prompt工程、数据构造、LoRA微调方法等。
2264 72
大模型微调知识与实践分享

热门文章

最新文章