doku做网站,西安比较好的软件公司,如何设计公众号,wordpress post编辑本文主要介绍使用魔搭社区轻量级训练推理工具SWIFT#xff0c;进行大模型自我认知微调#xff0c;帮助初阶炼丹师快速微调出专属于自己的大模型。 SWIFT#xff08;Scalable lightWeight Infrastructure for Fine-Tuning#xff09;是魔搭ModelScope开源社区推出的一套完整…本文主要介绍使用魔搭社区轻量级训练推理工具SWIFT进行大模型自我认知微调帮助初阶炼丹师快速微调出专属于自己的大模型。 SWIFTScalable 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] 0from swift.llm import ModelType, InferArguments, infer_main
infer_args InferArguments(model_typeModelType.qwen_7b_chat, eval_humanTrue)
infer_main(infer_args)你是谁
我是通义千问由阿里云开发的预训练语言模型。我被设计用来回答各种问题、提供信息和与用户进行对话。有什么我可以帮助你的吗
--------------------------------------------------whats 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_DEVICES0 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] 0from swift.llm import DatasetName, ModelType, SftArguments, sft_mainsft_args SftArguments(model_typeModelType.qwen_7b_chat,dataset[DatasetName.alpaca_zh, DatasetName.alpaca_en],train_dataset_sample500,eval_steps20,logging_steps5,output_diroutput,lora_target_modulesALL,self_cognition_sample500,model_name[小黄, Xiao Huang],model_author[魔搭, ModelScope])
output sft_main(sft_args)
best_model_checkpoint output[best_model_checkpoint]
print(fbest_model_checkpoint: {best_model_checkpoint})Out[0]0%| | 0/62 [00:00?, ?it/s]use_cacheTrue is incompatible with gradient checkpointing. Setting use_cacheFalse...
{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:0904: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:0100: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:2002: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:0100: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:3200: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:0100: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:4900: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:0100: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:5400: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_DEVICES0 \
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_DEVICES0,1 \
NPROC_PER_NODE2 \
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] 0from swift.llm import InferArguments, merge_lora_main, infer_mainbest_model_checkpoint qwen-7b-chat/vx-xxx/checkpoint-xxx
infer_args InferArguments(ckpt_dirbest_model_checkpoint,eval_humanTrue)
# merge_lora_main(infer_args)
result infer_main(infer_args)Out[0]你是谁
我是小黄由魔搭开发的人工智能语言模型。我可以回答你的问题、提供信息、进行对话和帮助你解决问题。如果你有任何疑问或需要帮助请随时告诉我。
--------------------------------------------------whats 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_DEVICES0 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_DEVICES0 swift infer --ckpt_dir qwen-7b-chat/vx-xxx/checkpoint-xxx-merged --eval_human trueWeb-UI
使用python:
import os
os.environ[CUDA_VISIBLE_DEVICES] 0from swift.llm import InferArguments, merge_lora_main, app_ui_mainbest_model_checkpoint qwen-7b-chat/vx-xxx/checkpoint-xxx
infer_args InferArguments(ckpt_dirbest_model_checkpoint,eval_humanTrue)
# merge_lora_main(infer_args)
result app_ui_main(infer_args)使用CLI:
# 直接使用web-ui
CUDA_VISIBLE_DEVICES0 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_DEVICES0 swift app-ui --ckpt_dir qwen-7b-chat/vx-xxx/checkpoint-xxx-merged --eval_human true可视化界面案例展示 本文为SWIFT LLMAIGC微调场景化最佳实践系列之一后续将继续通过魔搭社区推出场景化教程。目前SWIFT已支持94个大模型51个数据集支持LoRA、QLoRA、LongLoRA等十余种tuners一行代码即可开启模型训练欢迎对大模型和AIGC微调部署感兴趣的开发者小伙伴们多多交流
Github
https://github.com/modelscope/swift
技术交流群
前沿技术资讯、算法交流、求职内推、算法竞赛、面试交流(校招、社招、实习)等、与 10000来自港科大、北大、清华、中科院、CMU、腾讯、百度等名校名企开发者互动交流~
建了技术答疑、交流群想要进交流群、需要资料的同学可以直接加微信号mlc2060。加的时候备注一下研究方向 学校/公司CSDN即可。然后就可以拉你进群了。 方式①、添加微信号mlc2060备注技术交流 方式②、微信搜索公众号机器学习社区后台回复技术交流 资料1
资料2