【LLama】Llama3 的本地部署与lora微调(基于xturn)

【LLama】Llama3 的本地部署与lora微调(基于xturn)

    正在检查是否收录...

系列课程代码+文档(前2节课可跳过):https://github.com/SmartFlowAI/Llama3-Tutorial
课程视频:https://space.bilibili.com/3546636263360696/channel/series
XTuner :https://github.com/InternLM/xtuner/blob/main/README_zh-CN.md

一、Llama 3 本地部署(Nidia3090显卡)

教程所提供的在线显卡只有8G, 微调和推理时一般是16-20G,所以本地部署。

下载llama3模型

# 如果下面命令报错则使用 apt install git git-lfs -y conda install git-lfs git-lfs install git clone https://code.openxlab.org.cn/MrCat/Llama-3-8B-Instruct.git Meta-Llama-3-8B-Instruct 

下载在本地后的内容

本地环境

conda create -n llama3-xtuner python=3.10 -y conda activate llama3-xtuner conda install pytorch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 pytorch-cuda=12.1 -c pytorch -c nvidia 

其他依赖

git clone https://github.com/InternLM/xtuner.git cd xtuner conda activate llama3-xtuner pip install -e . 

运行llama3-instruct

Llama3-Tutorial-main

streamlit run ~/code/Llama3-Tutorial-main/tools/internstudio_web_demo.py \ ~/code/Meta-Llama-3-8B-Instruct 

二、 使用Xturn微调llama3 (1条数据)

主要参考:https://github.com/SmartFlowAI/Llama3-Tutorial/blob/main/docs/assistant.md

2.1 生成训练数据

重复次数 n = 2按需要修改,跑流程时可以很小,原始设置是2000
改为自己的名字:
name = ‘曾小蛙’
author=“星艺AI”

import json # 输入你的名字 name = '曾小蛙' author="星艺AI" # 重复次数 n = 2 data = [ { "conversation": [ { "system":"你是一个懂中文的小助手", "input": "你是(请用中文回答)", "output": "您好,我是{},一个由 {} 打造的人工智能助手,请问有什么可以帮助您的吗?".format(name, author) } ] } ] for i in range(n): data.append(data[0]) with open('data/personal_assistant.json', 'w', encoding='utf-8') as f: json.dump(data, f, ensure_ascii=False, indent=4) 

生成的训练用的json

2.2 修改训练脚本

Llama3-Tutorial/configs/assistant/llama3_8b_instruct_qlora_assistant.py

2.3 开始训练

xtuner train configs/assistant/llama3_8b_instruct_qlora_assistant.py 

保存的结果

2.4 Adapter PTH 转 HF 格式

xtuner convert pth_to_hf ~/code/Llama3-Tutorial-main/work_dirs/llama3_8b_instruct_qlora_assistant/llama3_8b_instruct_qlora_assistant.py \ ~/code/Llama3-Tutorial-main/work_dirs/llama3_8b_instruct_qlora_assistant/iter_20.pth \ ~/code/Llama3-Tutorial-main/work_dirs/llama3_hf_adapter 

2.5 合并lora到llama3中

记得将模型换为自己的路径·
本文使用相对路径,llama3的模型与教程代码在

export MKL_SERVICE_FORCE_INTEL=1 xtuner convert merge ./Meta-Llama-3-8B-Instruct \ ./Llama3-Tutorial-main/work_dirs/llama3_hf_adapter\ ./Llama3-Tutorial-main/work_dirs/llama3_hf_merged 

2.6 推理微调后的模型 (过拟合,还需要重新调参数)

Llama3-Tutorial-main是手动下载的

streamlit run ./Llama3-Tutorial-main/tools/internstudio_web_demo.py \ ./Llama3-Tutorial-main/work_dirs//llama3_hf_merged 

llamagitassistantcodegithubjsonpytorchivaflow本地部署smartamlwebstreamlitcloneinteldoccreate人工智能tpu
  • 本文作者:李琛
  • 本文链接: https://wapzz.net/post-14824.html
  • 版权声明:本博客所有文章除特别声明外,均默认采用 CC BY-NC-SA 4.0 许可协议。
本站部分内容来源于网络转载,仅供学习交流使用。如涉及版权问题,请及时联系我们,我们将第一时间处理。
文章很赞!支持一下吧 还没有人为TA充电
为TA充电
还没有人为TA充电
0
  • 支付宝打赏
    支付宝扫一扫
  • 微信打赏
    微信扫一扫
感谢支持
文章很赞!支持一下吧
关于作者
2.3W+
5
0
1
WAP站长官方

探秘采集站:数据信息的汇聚与处理新领地

上一篇

深入探索ThinkPHP爬虫:原理、实践与注意事项

下一篇
  • 复制图片
按住ctrl可打开默认菜单