- 低精度模型微调项目,非常适合新手来了解模型微调的原理及如何降低显存消耗,并且代码非常简洁,对于刚入门的新手很友好
- 扮演皇帝身边的女人——甄嬛,陪你畅聊宫廷趣事。
- 一张 ≤ 9 GB 显存的消费卡就能完成 QLoRA 微调的中文宫廷角色扮演模型!
- 基于 Meta-Llama-3.1-8B-Instruct + 4-bit 量化 + QLoRA,训练与推理全程显存占用 < 9 GB,RTX 3060/4060 即可玩转。
⚔️ 显存实测对比:
| 方案 | 基座模型 | 微调方式 | 量化位深 | 训练显存 (实测) | 推理显存 (实测) | 硬件门槛 |
|---|---|---|---|---|---|---|
| 官方全量微调 | Llama-3-8B | Full Fine-tune | 16-bit | ≈ 160 GB | ≈ 16 GB | 2× A100 (80G) |
| 标准 LoRA | Llama-3-8B | LoRA r=8 | 16-bit | ≈ 26–28 GB | ≈ 16 GB | RTX 3090/4090 |
| 该项目 | Llama-3-8B | LoRA r=8 | 4-bit NF4 | ≈ 7.5–8.5 GB ⭐ | ≈ 5.5–6.5 GB ⭐ | RTX 4070Ti 12G ✅ |
| 皇上 | 嬛嬛 |
|---|---|
| 皇上:今天心情不太好。 | 嬛嬛:皇上忧国忧民,臣妾愿为皇上抚琴一曲,以解烦忧。 |
| 皇上:给朕讲个笑话。 | 嬛嬛:从前有座山,山里有座庙,庙里有个小太监在偷吃桂花糕……皇上可还满意? |
conda create -n huanhuan python=3.12 -y
conda activate huanhuanpip install torch==2.4.0 torchvision==0.19.0 torchaudio==2.4.0 \
-f https://mirrors.aliyun.com/pytorch-wheels/cu121/git clone https://github.com/eason69113-source/Chat_HuanHuan.git
cd Chat-HuanHuanpython get_model.py # 自动下载 Llama-3.1-8B-Instruct 到 ./model# 已随仓库附带 ./output/llama3_1_instruct_lora/checkpoint-700
# 如想自己训练,见「训练」章节pip install -r requirements.txtpython inference.py输入 quit 退出。
- 准备数据
把对话 JSON 放入data/huanhuan.json,格式示例:[ {"instruction": "皇上:今天天气如何?", "input": "", "output": "嬛嬛:回皇上,今日风和日丽,宜游园赏花。"}, ... ] - 启动训练
默认 3 epoch,单卡 RTX 4070Ti 约 60 分钟。
python train.py
训练完自动保存到./output/llama3_1_instruct_lora/checkpoint-xxx
HuanHuan-Chat
├── main
│ └── data/ # 训练数据
│ └── get_model.py/ # 下载基座模型
│ └── train.py/ # LoRA 微调脚本
│ └── inference.py/ # 交互式推理
├── output
│ └── llama3_1_instruct_lora
│ └── checkpoint-700 # 已训练权重
├── requirements.txt # 环境配置
└── README.md
| 阶段 | 显存 | 备注 |
|---|---|---|
| 推理 | ≈ 6 GB | 4-bit 量化,单卡 3060 可跑 |
| 训练 | ≈ 9 GB | RTX 4070Ti 12G |
- 基座模型: Meta-Llama-3.1-8B-Instruct
- 微调方案: LoRA (r=8, α=32, dropout=0.1)
- 量化: bitsandbytes 4-bit NF4
- 框架: transformers + peft + modelscope
- 对话模板: Llama-3 官方 chat_template
Apache-2.0
- Meta Llama-3 团队
- ModelScope 社区