经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 大数据/云/AI » 人工智能基础 » 查看文章
LLM 大模型学习必知必会系列(八):10分钟微调专属于自己的大模型
来源:cnblogs  作者:汀、人工智能  时间:2024/5/31 9:33:12  对本文有异议

LLM 大模型学习必知必会系列(八):10分钟微调专属于自己的大模型

1.环境安装

  1. # 设置pip全局镜像 (加速下载)
  2. pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
  3. # 安装ms-swift
  4. pip install 'ms-swift[llm]' -U
  5. # 环境对齐 (通常不需要运行. 如果你运行错误, 可以跑下面的代码, 仓库使用最新环境测试)
  6. pip install -r requirements/framework.txt -U
  7. pip install -r requirements/llm.txt -U

2.微调前推理

使用python:

  1. import os
  2. os.environ['CUDA_VISIBLE_DEVICES'] = '0'
  3. from swift.llm import ModelType, InferArguments, infer_main
  4. infer_args = InferArguments(model_type=ModelType.qwen1half_4b_chat)
  5. infer_main(infer_args)
  6. """
  7. <<< 你是谁?
  8. 我是来自阿里云的大规模语言模型,我叫通义千问。
  9. --------------------------------------------------
  10. <<< what's your name?
  11. I am Qwen, a large language model from Alibaba Cloud.
  12. --------------------------------------------------
  13. <<< 你是谁研发的?
  14. 我是阿里云自主研发的超大规模语言模型。
  15. --------------------------------------------------
  16. <<< 浙江的省会在哪?
  17. 浙江的省会是杭州。
  18. --------------------------------------------------
  19. <<< 这有什么好吃的?
  20. 浙江的美食非常丰富,比如杭州的西湖醋鱼、东坡肉、龙井虾仁、宋嫂鱼羹等都是著名的浙江美食。此外,浙江还有许多小吃,比如油条、烧麦、汤圆、粽子等。
  21. --------------------------------------------------
  22. <<< 晚上睡不着觉怎么办
  23. 晚上睡不着觉可以尝试以下几种方法:
  24. 1. 放松身心:可以尝试做一些放松身心的活动,比如听音乐、做瑜伽、冥想等。
  25. 2. 保持规律作息:尽量保持每天的作息规律,避免熬夜。
  26. 3. 避免刺激性食物:避免吃辛辣、油腻、咖啡因等刺激性食物,这些食物可能会刺激神经系统,导致失眠。
  27. 4. 适当运动:适当的运动可以帮助身体放松,有助于睡眠。
  28. 5. 睡前喝牛奶:牛奶中含有色氨酸,可以帮助身体产生褪黑激素,有助于睡眠。
  29. """

如果你要进行单样本推理, 可以参考LLM推理文档

使用CLI:

  1. CUDA_VISIBLE_DEVICES=0 swift infer --model_type qwen1half-4b-chat

3.微调

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

使用python:

  1. # Experimental environment: A10, 3090, V100, ...
  2. # 22GB GPU memory
  3. import os
  4. os.environ['CUDA_VISIBLE_DEVICES'] = '0'
  5. from swift.llm import DatasetName, ModelType, SftArguments, sft_main
  6. sft_args = SftArguments(
  7. model_type=ModelType.qwen1half_4b_chat,
  8. dataset=[f'{DatasetName.alpaca_zh}#500', f'{DatasetName.alpaca_en}#500',
  9. f'{DatasetName.self_cognition}#500'],
  10. logging_steps=5,
  11. max_length=2048,
  12. learning_rate=5e-5,
  13. warmup_ratio=0.4,
  14. output_dir='output',
  15. lora_target_modules=['ALL'],
  16. model_name=['小黄', 'Xiao Huang'],
  17. model_author=['魔搭', 'ModelScope'])
  18. output = sft_main(sft_args)
  19. best_model_checkpoint = output['best_model_checkpoint']
  20. print(f'best_model_checkpoint: {best_model_checkpoint}')
  21. """Out[0]
  22. {'loss': 1.36837471, 'acc': 0.6827153, 'grad_norm': 2.69893861, 'learning_rate': 2.7e-06, 'epoch': 0.01, 'global_step': 1}
  23. {'loss': 1.64843678, 'acc': 0.62217778, 'grad_norm': 1.68335974, 'learning_rate': 1.351e-05, 'epoch': 0.05, 'global_step': 5}
  24. {'loss': 1.81131458, 'acc': 0.59357905, 'grad_norm': 1.78167629, 'learning_rate': 2.703e-05, 'epoch': 0.11, 'global_step': 10}
  25. {'loss': 1.70607147, 'acc': 0.60849266, 'grad_norm': 1.47256434, 'learning_rate': 4.054e-05, 'epoch': 0.16, 'global_step': 15}
  26. {'loss': 1.51096973, 'acc': 0.63005199, 'grad_norm': 0.91772562, 'learning_rate': 5.405e-05, 'epoch': 0.22, 'global_step': 20}
  27. {'loss': 1.5484211, 'acc': 0.62795267, 'grad_norm': 1.11152458, 'learning_rate': 6.757e-05, 'epoch': 0.27, 'global_step': 25}
  28. {'loss': 1.43836861, 'acc': 0.64279995, 'grad_norm': 1.1565901, 'learning_rate': 8.108e-05, 'epoch': 0.33, 'global_step': 30}
  29. {'loss': 1.38720503, 'acc': 0.64892483, 'grad_norm': 0.98939317, 'learning_rate': 9.459e-05, 'epoch': 0.38, 'global_step': 35}
  30. {'loss': 1.28600607, 'acc': 0.67057638, 'grad_norm': 2.26390719, 'learning_rate': 9.455e-05, 'epoch': 0.43, 'global_step': 40}
  31. {'loss': 1.2084446, 'acc': 0.68125477, 'grad_norm': 1.39036703, 'learning_rate': 8.545e-05, 'epoch': 0.49, 'global_step': 45}
  32. {'loss': 1.39412193, 'acc': 0.64913111, 'grad_norm': 0.6860683, 'learning_rate': 7.636e-05, 'epoch': 0.54, 'global_step': 50}
  33. Train: 54%|███████████████████████████████████████████████▊ | 50/92 [02:57<02:28, 3.53s/it]
  34. {'eval_loss': 1.54409802, 'eval_acc': 0.5955491, 'eval_runtime': 0.5527, 'eval_samples_per_second': 18.092, 'eval_steps_per_second': 9.046, 'epoch': 0.54, 'global_step': 50}
  35. Val: 100%|████████████████████████████████████████████████████████████████████████████████████████████| 5/5 [00:00<00:00, 13.27it/s]
  36. [INFO:swift] Saving model checkpoint to /xxx/output/qwen1half-4b-chat/v0-20240225-194502/checkpoint-50
  37. {'loss': 1.1771349, 'acc': 0.67886224, 'grad_norm': 1.06721985, 'learning_rate': 6.727e-05, 'epoch': 0.6, 'global_step': 55}
  38. {'loss': 1.25694866, 'acc': 0.67727785, 'grad_norm': 1.27860904, 'learning_rate': 5.818e-05, 'epoch': 0.65, 'global_step': 60}
  39. {'loss': 1.18360176, 'acc': 0.70474091, 'grad_norm': 0.71210742, 'learning_rate': 4.909e-05, 'epoch': 0.71, 'global_step': 65}
  40. {'loss': 1.08381062, 'acc': 0.71071234, 'grad_norm': 1.32174027, 'learning_rate': 4e-05, 'epoch': 0.76, 'global_step': 70}
  41. {'loss': 1.23212566, 'acc': 0.68333907, 'grad_norm': 0.87663323, 'learning_rate': 3.091e-05, 'epoch': 0.82, 'global_step': 75}
  42. {'loss': 1.2107378, 'acc': 0.70353975, 'grad_norm': 0.78985584, 'learning_rate': 2.182e-05, 'epoch': 0.87, 'global_step': 80}
  43. {'loss': 1.32458553, 'acc': 0.6687315, 'grad_norm': 1.25317574, 'learning_rate': 1.273e-05, 'epoch': 0.92, 'global_step': 85}
  44. {'loss': 1.28211155, 'acc': 0.67041779, 'grad_norm': 1.10373855, 'learning_rate': 3.64e-06, 'epoch': 0.98, 'global_step': 90}
  45. Train: 100%|████████████████████████████████████████████████████████████████████████████████████████| 92/92 [05:31<00:00, 3.60s/it]
  46. {'eval_loss': 1.53501475, 'eval_acc': 0.59796807, 'eval_runtime': 0.521, 'eval_samples_per_second': 19.193, 'eval_steps_per_second': 9.597, 'epoch': 1.0, 'global_step': 92}
  47. Val: 100%|████████████████████████████████████████████████████████████████████████████████████████████| 5/5 [00:00<00:00, 13.74it/s]
  48. [INFO:swift] Saving model checkpoint to /xxx/output/qwen1half-4b-chat/v0-20240225-194502/checkpoint-92
  49. """

使用CLI (单卡):

  1. # Experimental environment: A10, 3090, V100, ...
  2. # 22GB GPU memory
  3. CUDA_VISIBLE_DEVICES=0 swift sft --model_type qwen1half-4b-chat --dataset alpaca-zh#500 alpaca-en#500 self-cognition#500 --logging_steps 5 --max_length 2048 --learning_rate 5e-5 --warmup_ratio 0.4 --output_dir output --lora_target_modules ALL --model_name 小黄 'Xiao Huang' --model_author 魔搭 ModelScope

使用CLI (DeepSpeed-ZeRO2):

如果你使用的是3090等卡, 可以降低max_length来减少显存消耗.

  1. # Experimental environment: 4 * 3090
  2. # 4 * 24GB GPU memory
  3. CUDA_VISIBLE_DEVICES=0,1,2,3 NPROC_PER_NODE=4 swift sft --model_type qwen1half-4b-chat --dataset alpaca-zh#500 alpaca-en#500 self-cognition#500 --logging_steps 5 --max_length 2048 --learning_rate 5e-5 --warmup_ratio 0.4 --output_dir output --lora_target_modules ALL --model_name 小黄 'Xiao Huang' --model_author 魔搭 ModelScope --deepspeed default-zero2

4.微调后推理

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

使用python:

  1. import os
  2. os.environ['CUDA_VISIBLE_DEVICES'] = '0'
  3. from swift.llm import InferArguments, merge_lora, infer_main
  4. best_model_checkpoint = 'qwen1half-4b-chat/vx-xxx/checkpoint-xxx'
  5. infer_args = InferArguments(ckpt_dir=best_model_checkpoint)
  6. merge_lora(infer_args, device_map='cpu')
  7. result = infer_main(infer_args)
  8. """Out[0]
  9. <<< 你是谁?
  10. 我是魔搭的人工智能助手,我的名字叫小黄。我可以回答你的问题、提供信息、进行对话等等。如果你有任何问题或需要帮助,请随时告诉我。
  11. --------------------------------------------------
  12. <<< what's your name?
  13. I am Xiao Huang, an artificial intelligence assistant developed by ModelScope.
  14. --------------------------------------------------
  15. <<< 你是谁研发的?
  16. 我是由魔搭研发的。
  17. --------------------------------------------------
  18. <<< 浙江的省会在哪?
  19. 浙江省的省会是杭州。
  20. --------------------------------------------------
  21. <<< 这有什么好吃的?
  22. 浙江的美食非常丰富,比如杭州的西湖醋鱼、东坡肉、龙井虾仁等等都是非常有名的菜肴。此外,浙江还有许多小吃,比如油条、烧饼、汤圆等等。
  23. --------------------------------------------------
  24. <<< 晚上睡不着觉怎么办
  25. 晚上睡不着觉可以尝试以下几种方法:
  26. 1. 放松身心:可以尝试做一些放松身心的活动,比如听音乐、冥想、深呼吸等等。
  27. 2. 保持规律的作息:尽量保持规律的作息,避免熬夜。
  28. 3. 避免刺激性物质:避免摄入咖啡因、酒精等刺激性物质,这些物质可能会干扰你的睡眠。
  29. 4. 适当运动:适当的运动可以帮助你放松身心,提高睡眠质量。
  30. 5. 睡前放松:可以尝试一些睡前放松的活动,比如阅读、听轻音乐、泡热水澡等等。
  31. 希望以上建议能够帮助你改善睡眠质量。
  32. """

使用CLI:

  1. # 直接推理
  2. CUDA_VISIBLE_DEVICES=0 swift infer --ckpt_dir 'qwen1half-4b-chat/vx-xxx/checkpoint-xxx'
  3. # Merge LoRA增量权重并推理
  4. # 如果你需要量化, 可以指定`--quant_bits 4`.
  5. CUDA_VISIBLE_DEVICES=0 swift export --ckpt_dir 'qwen1half-4b-chat/vx-xxx/checkpoint-xxx' --merge_lora true
  6. CUDA_VISIBLE_DEVICES=0 swift infer --ckpt_dir 'qwen1half-4b-chat/vx-xxx/checkpoint-xxx-merged'

5.Web-UI

使用python:

  1. import os
  2. os.environ['CUDA_VISIBLE_DEVICES'] = '0'
  3. from swift.llm import AppUIArguments, merge_lora, app_ui_main
  4. best_model_checkpoint = 'qwen1half-4b-chat/vx-xxx/checkpoint-xxx'
  5. app_ui_args = AppUIArguments(ckpt_dir=best_model_checkpoint)
  6. merge_lora(app_ui_args, device_map='cpu')
  7. result = app_ui_main(app_ui_args)

使用CLI:

  1. # 直接使用app-ui
  2. CUDA_VISIBLE_DEVICES=0 swift app-ui --ckpt_dir 'qwen1half-4b-chat/vx-xxx/checkpoint-xxx'
  3. # Merge LoRA增量权重并使用app-ui
  4. # 如果你需要量化, 可以指定`--quant_bits 4`.
  5. CUDA_VISIBLE_DEVICES=0 swift export --ckpt_dir 'qwen1half-4b-chat/vx-xxx/checkpoint-xxx' --merge_lora true
  6. CUDA_VISIBLE_DEVICES=0 swift app-ui --ckpt_dir 'qwen1half-4b-chat/vx-xxx/checkpoint-xxx-merged'

更多优质内容请关注公号:汀丶人工智能;会提供一些相关的资源和优质文章,免费获取阅读。

原文链接:https://www.cnblogs.com/ting1/p/18219707

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号