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

系列课程代码+文档(前2节课可跳过)https://github.com/SmartFlowAI/Llama3-Tutorial
课程视频https://space.bilibili.com/3546636263360696/channel/series
XTunerhttps://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

在这里插入图片描述

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

曾小蛙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值