【LLama】Llama3 的本地部署与lora微调(基于xturn)
创始人
2024-11-15 22:08:29

系列课程代码+文档(前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 

在这里插入图片描述

相关内容

热门资讯

裸辞做“一人公司”,我后悔了 去年这个时候,一位以色列程序员正在东南亚旅行。他顺手把一个在脑子里转了很久的想法做成了产品,一个让任...
南京建成国内首个Pre-6G试... 4月21日,2026全球6G技术与产业生态大会在南京开幕。全息互动技术展台前,一名远在北京的工作人员...
超梵求职受邀参加“2025抖音... 超梵求职受邀参加“2025抖音巨量引擎成人教育行业生态大会”,探讨分享优质内容传播,服务万千学员。 ...
摩托罗拉Razr 2026(R... IT之家 4 月 22 日消息,摩托罗拉宣布新一代 Razr 折叠手机将于 4 月 29 日在美国发...
库克卸任,特纳斯领航:苹果新纪... 苹果首席执行官蒂姆·库克将卸任,硬件工程主管约翰·特纳斯将接任,苹果公司今天宣布此事。 库克将在夏季...