【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
相关推荐
Wnq1007212 小时前
世界模型 AI:认知跃迁的可行性与本质性挑战
人工智能
穷人小水滴12 小时前
科幻 「备用肉身虫」 系列设定集 (AI 摘要)
人工智能·aigc·科幻·未来·小说·设定
老赵聊算法、大模型备案12 小时前
北京市生成式人工智能服务已备案信息公告(2025年12月11日)
人工智能·算法·安全·aigc
咬人喵喵12 小时前
上下文窗口:AI 的“大脑容量”
人工智能
workflower12 小时前
时序数据获取事件
开发语言·人工智能·python·深度学习·机器学习·结对编程
weixin_4461224612 小时前
一个案例验证 LLM大模型编码能力哪家强
人工智能
老蒋新思维13 小时前
创客匠人峰会深度解析:知识变现的 “信任 - 效率” 双闭环 —— 从 “单次交易” 到 “终身复购” 的增长密码
大数据·网络·人工智能·tcp/ip·重构·数据挖掘·创客匠人
java1234_小锋14 小时前
Transformer 大语言模型(LLM)基石 - Transformer架构详解 - 编码器(Encoder)详解以及算法实现
深度学习·语言模型·transformer
大刘讲IT14 小时前
面向中小企业的企业AI Agent未来3年构建蓝图规划
人工智能·经验分享·ai·开源·制造
yzx99101314 小时前
深度学习的进化之路:从感知机到通用智能的曙光
人工智能·深度学习