【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模型

bash 复制代码
# 如果下面命令报错则使用 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

下载在本地后的内容

本地环境

bash 复制代码
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

其他依赖

bash 复制代码
git clone https://github.com/InternLM/xtuner.git
cd xtuner
conda activate llama3-xtuner
pip install -e .

运行llama3-instruct

Llama3-Tutorial-main

bash 复制代码
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"

python 复制代码
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 开始训练

bash 复制代码
xtuner train configs/assistant/llama3_8b_instruct_qlora_assistant.py

保存的结果

2.4 Adapter PTH 转 HF 格式

bash 复制代码
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的模型与教程代码在

bash 复制代码
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是手动下载的

bash 复制代码
streamlit run ./Llama3-Tutorial-main/tools/internstudio_web_demo.py \
  ./Llama3-Tutorial-main/work_dirs//llama3_hf_merged
相关推荐
昨夜见军贴06168 分钟前
功能决定效率:IACheck的AI审核在生产型检测报告中的实践观察
人工智能
传说故事26 分钟前
【论文自动阅读】Goal Force: 教视频模型实现Physics-Conditioned Goals
人工智能·深度学习·视频生成
186******2053136 分钟前
项目开发基础知识:从概念到落地的全流程指南
大数据·人工智能
说私域39 分钟前
AI智能名片商城小程序数据清洗的持续运营策略与实践研究
大数据·人工智能·小程序·流量运营·私域运营
sunshine8851 小时前
合规性管理:财务安全与业务连续性的双重保障
大数据·运维·人工智能
lusasky1 小时前
Claude Code v2.1.0+ 版本集成LSP
大数据·数据库·人工智能
yusur1 小时前
中科驭数CEO鄢贵海:AI尚处“Day 1”,算力基建的价值外溢如同高铁
人工智能·科技·dpu·中科驭数
小鸡吃米…1 小时前
机器学习 —— 数据缩放
人工智能·python·机器学习
2501_941337061 小时前
YOLO11-C3k2-RAB改进模型在航拍军事目标检测中的应用与实现
人工智能·目标检测·目标跟踪
qwy7152292581631 小时前
9-数字水印的嵌入和提取
人工智能·opencv·计算机视觉