AI ONES

使用Colab_LLaMA_Factory_...

使用Colab_LLaMA_Factory_LoRA微调_Llama3(可自定义数据)

avatarwuxiongwei

目录:

使用 LLaMA Factory 微调 Llama-3 中文对话模型

项目主页: https://github.com/hiyouga/LLaMA-Factory

这个过程超级简单,半个多小时在T4上就能跑完。

完全可以替换成自己的数据,支持中文数据。

安装 LLaMA Factory 依赖

1%cd /content/ 2%rm -rf LLaMA-Factory 3!git clone https://github.com/hiyouga/LLaMA-Factory.git 4%cd LLaMA-Factory 5%ls 6!pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git" 7!pip install --no-deps xformers==0.0.25 8!pip install .[bitsandbytes]

更新自我认知数据集

1import json 2 3%cd /content/LLaMA-Factory/ 4 5NAME = "New LLama3" 6AUTHOR = "AI ONES" 7 8with open("data/identity.json", "r", encoding="utf-8") as f: 9 dataset = json.load(f) 10 11for sample in dataset: 12 sample["output"] = sample["output"].replace("NAME", NAME).replace("AUTHOR", AUTHOR) 13 14with open("data/identity.json", "w", encoding="utf-8") as f: 15 json.dump(dataset, f, indent=2, ensure_ascii=False)

微调过程大约需要 30 分钟。

1from llmtuner import run_exp 2from llmtuner.extras.misc import torch_gc 3 4%cd /content/LLaMA-Factory/ 5 6run_exp(dict( 7 stage="sft", # 进行指令监督微调 8 do_train=True, 9 model_name_or_path="unsloth/llama-3-8b-Instruct-bnb-4bit", # 使用 4 比特量化版 Llama-3-8b-Instruct 模型 10 dataset="identity,alpaca_gpt4_en,alpaca_gpt4_zh", # 使用 alpaca 和自我认知数据集 11 template="llama3", # 使用 llama3 提示词模板 12 finetuning_type="lora", # 使用 LoRA 适配器来节省显存 13 lora_target="all", # 添加 LoRA 适配器至全部线性层 14 output_dir="llama3_lora", # 保存 LoRA 适配器的路径 15 per_device_train_batch_size=2, # 批处理大小 16 gradient_accumulation_steps=4, # 梯度累积步数 17 lr_scheduler_type="cosine", # 使用余弦学习率退火算法 18 logging_steps=10, # 每 10 步输出一个记录 19 warmup_ratio=0.1, # 使用预热学习率 20 save_steps=1000, # 每 1000 步保存一个检查点 21 learning_rate=5e-5, # 学习率大小 22 num_train_epochs=3.0, # 训练轮数 23 max_samples=300, # 使用每个数据集中的 300 条样本 24 max_grad_norm=1.0, # 将梯度范数裁剪至 1.0 25 quantization_bit=4, # 使用 4 比特 QLoRA 26 loraplus_lr_ratio=16.0, # 使用 LoRA+ 算法并设置 lambda=16.0 27 use_unsloth=True, # 使用 UnslothAI 的 LoRA 优化来加快一倍的训练速度 28 fp16=True, # 使用 float16 混合精度训练 29)) 30 31torch_gc()

模型推理

1from llmtuner import ChatModel 2from llmtuner.extras.misc import torch_gc 3 4%cd /content/LLaMA-Factory/ 5 6chat_model = ChatModel(dict( 7 model_name_or_path="unsloth/llama-3-8b-Instruct-bnb-4bit", # 使用 4 比特量化版 Llama-3-8b-Instruct 模型 8 adapter_name_or_path="llama3_lora", # 加载之前保存的 LoRA 适配器 9 finetuning_type="lora", # 和训练保持一致 10 template="llama3", # 和训练保持一致 11 quantization_bit=4, # 加载 4 比特量化模型 12 use_unsloth=True, # 使用 UnslothAI 的 LoRA 优化来加快一倍的推理速度 13)) 14 15messages = [] 16while True: 17 query = input("\nUser: ") 18 if query.strip() == "exit": 19 break 20 21 if query.strip() == "clear": 22 messages = [] 23 torch_gc() 24 print("History has been removed.") 25 continue 26 27 messages.append({"role": "user", "content": query}) # 把提示词添加到消息中 28 print("Assistant: ", end="", flush=True) 29 response = "" 30 for new_text in chat_model.stream_chat(messages): # 流式输出 31 print(new_text, end="", flush=True) 32 response += new_text 33 print() 34 messages.append({"role": "assistant", "content": response}) # 把回答添加到消息中 35 36torch_gc()

colab代码地址:https://colab.research.google.com/drive/1RF1YuGTyUHFcVCLUO36KwqCBSr1G_n6w?usp=sharing

原创声明:本文为本人原创作品,首发于AI ONES https://wuxiongwei.com,如果转载,请保留本文链接,谢谢。
上一篇

LangChain RAG 下册

下一篇

LangChain RAG 上册