【大模型系列篇】LLaMA-Factory大模型微调实践 - 从零开始

前一次我们使用了NVIDIA TensorRT-LLM 大模型推理框架对智谱chatglm3-6b模型格式进行了转换和量化压缩,并成功部署了推理服务,有兴趣的同学可以翻阅《NVIDIA TensorRT-LLM 大模型推理框架实践》,今天我们来实践如何通过LLaMA-Factory对大模型进行Lora微调。

首先我们来认识一下LLaMA-Factory,它是一个在 GitHub 上开源的项目,为大语言模型(LLM)的训练、微调和部署提供了一个简便且高效的框架。该项目旨在简化和加速 LLaMA以及其他多种大型语言模型的微调过程,使得即使是非专业用户也能轻松上手。

功能介绍

https://llamafactory.readthedocs.io/zh-cn/latest/

LLaMA-Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调,框架特性包括:

  • 模型种类:LLaMA、LLaVA、Mistral、Mixtral-MoE、Qwen、Yi、Gemma、Baichuan、ChatGLM、Phi 等等。
  • 训练算法:(增量)预训练、(多模态)指令监督微调、奖励模型训练、PPO 训练、DPO 训练、KTO 训练、ORPO 训练等等。
  • 运算精度:16 比特全参数微调、冻结微调、LoRA 微调和基于 AQLM/AWQ/GPTQ/LLM.int8/HQQ/EETQ 的 2/3/4/5/6/8 比特 QLoRA 微调。
  • 优化算法:GaLore、BAdam、DoRA、LongLoRA、LLaMA Pro、Mixture-of-Depths、LoRA+、LoftQ 和 PiSSA。
  • 加速算子:FlashAttention-2 和 Unsloth。
  • 推理引擎:Transformers 和 vLLM。
  • 实验面板:LlamaBoard、TensorBoard、Wandb、MLflow 等等。

安装部署

我们本次采用docker方式部署启动LLaMA-Factory镜像容器,本次实践前提需要让容器能访问到宿主机的GPU资源,大家可以参考之前整理的《GPU资源容器化访问使用指南》一步一步操作。

下载源码

复制代码
#【github加速计划】速度慢,可访问 git clone https://gitcode.com/gh_mirrors/ll/LLaMA-Factory.git
git clone --depth 1 https://github.com/hiyouga//LLaMA-Factory.git

构建镜像,启动容器

复制代码
cd LLaMA-Factory/docker/docker-cuda/
#构建镜像,启动服务
docker compose up -d

进入容器,启动webui服务

复制代码
# 进入容器
docker compose exec llamafactory bash
# 启动webui服务
llamafactory-cli webui

# 模型下载加速- USE_MODELSCOPE_HUB设为1,表示模型来源是ModelScope 
# 需要安装 pip install modelscope
export USE_MODELSCOPE_HUB=1 && llamafactory-cli webui

https://llamafactory.readthedocs.io/zh-cn/latest/getting_started/webui.html

访问 0.0.0.0:7860

模型微调

数据预处理

https://llamafactory.readthedocs.io/zh-cn/latest/getting_started/data_preparation.html

微调样本集数据格式

复制代码
[
    {
        "instruction":"用户指令(必填)",
        "input":"用户输入(选填)",
        "output":"模型回答(必填)",
        "system":"系统提示词(选填)",
        "history":[
            ["第一轮指令(选填)","第一轮回答(选填)"],
            ["第二轮指令(选填)","第二轮回答(选填)"]
        ]
    }
]

data/dataset_info.json 添加本地样本集

选择准备的数据集

选择训练轮数: 50

点击开始,进行微调

模型微调参数

复制代码
# Model config ChatGLMConfig 
{
  "_name_or_path": "/root/.cache/modelscope/hub/ZhipuAI/chatglm3-6b",
  "add_bias_linear": false,
  "add_qkv_bias": true,
  "apply_query_key_layer_scaling": true,
  "apply_residual_connection_post_layernorm": false,
  "architectures": [
    "ChatGLMModel"
  ],
  "attention_dropout": 0.0,
  "attention_softmax_in_fp32": true,
  "auto_map": {
    "AutoConfig": "configuration_chatglm.ChatGLMConfig",
    "AutoModel": "modeling_chatglm.ChatGLMForConditionalGeneration",
    "AutoModelForCausalLM": "modeling_chatglm.ChatGLMForConditionalGeneration",
    "AutoModelForSeq2SeqLM": "modeling_chatglm.ChatGLMForConditionalGeneration",
    "AutoModelForSequenceClassification": "modeling_chatglm.ChatGLMForSequenceClassification"
  },
  "bias_dropout_fusion": true,
  "classifier_dropout": null,
  "eos_token_id": 2,
  "ffn_hidden_size": 13696,
  "fp32_residual_connection": false,
  "hidden_dropout": 0.0,
  "hidden_size": 4096,
  "kv_channels": 128,
  "layernorm_epsilon": 1e-05,
  "model_type": "chatglm",
  "multi_query_attention": true,
  "multi_query_group_num": 2,
  "num_attention_heads": 32,
  "num_layers": 28,
  "original_rope": true,
  "pad_token_id": 0,
  "padded_vocab_size": 65024,
  "post_layer_norm": true,
  "pre_seq_len": null,
  "prefix_projection": false,
  "quantization_bit": 0,
  "rmsnorm": true,
  "seq_length": 8192,
  "tie_word_embeddings": false,
  "torch_dtype": "float16",
  "transformers_version": "4.43.4",
  "use_cache": true,
  "vocab_size": 65024
}

模型微调前后对比

从Train切换至Chat,点击加载模型,进行模型推理部署

微调前对话

微调后对话

相关推荐
DogDaoDao1 天前
神经网络稀疏化设计构架方法和原理深度解析
人工智能·pytorch·深度学习·神经网络·大模型·剪枝·网络稀疏
车骑1 天前
一个支持国外技术聚合翻译自动化的开源脚本
大模型·github
吏部侍郎1 天前
腾讯终于对Claude code下手了?我拿它跑完一个真实项目,结果有点意外…
大模型·ai编程
居7然1 天前
解锁AI智能体:上下文工程如何成为架构落地的“魔法钥匙”
人工智能·架构·大模型·智能体·上下文工程
mask哥1 天前
详解mcp以及agen架构设计与实现
java·微服务·flink·大模型·ai agent·springai·mcp
shizidushu1 天前
Hugging Face NLP课程学习记录 - 3. 微调一个预训练模型
人工智能·学习·自然语言处理·微调·huggingface
大千AI助手2 天前
线性预热机制(Linear Warmup):深度学习训练稳定性的关键策略
人工智能·深度学习·大模型·模型训练·学习率·warmup·线性预热机制
人工智能培训3 天前
AI提示词(Prompt)基础核心知识点
大模型·prompt·提示词·input
骑士9991113 天前
llama_factory 安装以及大模型微调
llama
自信的小螺丝钉3 天前
【AI知识点】模型训练优化之——混合精度训练
人工智能·ai·大模型·混合精度训练