首页 > 其他分享 >unsloth微调llama3实战全过程

unsloth微调llama3实战全过程

时间:2024-06-17 09:36:40浏览次数:18  
标签:seq max 微调 llama3 unsloth 全过程 text model

1、为什么要进行大模型微调

微调的定义

大模型微调是利用特定领域的数据集对已预训练的大模型进行进一步训练的过程。它旨在优化模型在特定任务上的性能,使模型能够更好地适应和完成特定领域的任务。

微调的核心原因

定制化功能:微调的核心原因是赋予大模型更加定制化的功能。通用大模型虽然强大,但在特定领域可能表现不佳。通过微调,可以使模型更好地适应特定领域的需求和特征。
领域知识学习:通过引入特定领域的数据集进行微调,大模型可以学习该领域的知识和语言模式。这有助于模型在特定任务上取得更好的性能。

2、unsloth简介

unsloth微调Llama 3, Mistral和Gemma速度快2-5倍,内存减少80% !unsloth是一个开源项目,它可以比HuggingFace快2-5倍地微调Llama 3、Mistral和Gemma语言模型,同时内存消耗减少80%。
github:https://github.com/unslothai/unsloth

3、实战教学

1、硬件环境
我选择的是在Autodl租用的显卡进行的微调。好处:1、省时省力,不需要自己搭建硬件环境。2、价格便宜,初学阶段使用3080*2,20G显存完全够用,每小时只要1.08元。无卡开机只要0.01元。
2、软件环境
采用的是Conda 进行安装

conda create --name unsloth_env python=3.10
conda activate unsloth_env

conda install pytorch-cuda=12.1 pytorch cudatoolkit xformers -c pytorch -c nvidia -c xformers
pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"

pip install --no-deps trl peft accelerate bitsandbytes

git拉取代码可能拉不下来,使用autodl提供的学术资源加速。拉取速度不快,多等会。

source /etc/network_turbo

3、运行代码
由于网络原因,可能无法访问huggingface上的资源,可以使用国内的镜像站。https://hf-mirror.com/

pip install -U huggingface_hub

export HF_ENDPOINT=https://hf-mirror.com

下面的代码需要保存成.py文件,上传到服务器root目录下。使用python test-unlora.py运行。
微调前测试模型

from unsloth import FastLanguageModel
import torch
max_seq_length = 2048
dtype = None
load_in_4bit = True
model, tokenizer = FastLanguageModel.from_pretrained(
    model_name = "unsloth/llama-3-8b-bnb-4bit", 
    max_seq_length = max_seq_length, 
    dtype = dtype,     
    load_in_4bit = load_in_4bit,
    token = "https://hf-mirror.com"  
)

alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
### Instruction:
{}
### Input:
{}
### Response:
{}"""

FastLanguageModel.for_inference(model) 
inputs = tokenizer(
[
    alpaca_prompt.format(
        "海绵宝宝的书法是不是叫做海绵体",
        "", 
        "", 
    )
], return_tensors = "pt").to("cuda")

from transformers import TextStreamer
text_streamer = TextStreamer(tokenizer)
_ = model.generate(**inputs, streamer = text_streamer, max_new_tokens = 128)

EOS_TOKEN = tokenizer.eos_token # 必须添加 EOS_TOKEN
def formatting_prompts_func(examples):
    instructions = examples["instruction"]
    inputs       = examples["input"]
    outputs      = examples["output"]
    texts = []
    for instruction, input, output in zip(instructions, inputs, outputs):
        # 必须添加EOS_TOKEN,否则生成将永无止境
        text = alpaca_prompt.format(instruction, input, output) + EOS_TOKEN
        texts.append(text)
    return { "text" : texts, }
pass

上面的代码会拉取镜像,估计要10分钟左右。上述返回没问题,则可以进行微调。
微调系统盘50G空间不够,需要购买数据盘50-100G。
微调模型

import os
from unsloth import FastLanguageModel
import torch
from trl import SFTTrainer
from transformers import TrainingArguments
from datasets import load_dataset

#加载模型
max_seq_length = 2048
dtype = None
load_in_4bit = True
model, tokenizer = FastLanguageModel.from_pretrained(
    model_name = "unsloth/llama-3-8b-bnb-4bit", 
    max_seq_length = max_seq_length, 
    dtype = dtype,     
    load_in_4bit = load_in_4bit,
    token = "https://hf-mirror.com"
)

#准备训练数据
alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
### Instruction:
{}
### Input:
{}
### Response:
{}"""

EOS_TOKEN = tokenizer.eos_token # 必须添加 EOS_TOKEN
def formatting_prompts_func(examples):
    instructions = examples["instruction"]
    inputs       = examples["input"]
    outputs      = examples["output"]
    texts = []
    for instruction, input, output in zip(instructions, inputs, outputs):
        # 必须添加EOS_TOKEN,否则无限生成
        text = alpaca_prompt.format(instruction, input, output) + EOS_TOKEN
        texts.append(text)
    return { "text" : texts, }
pass

#hugging face数据集路径
dataset = load_dataset("kigner/ruozhiba-llama3", split = "train")
dataset = dataset.map(formatting_prompts_func, batched = True,)

#设置训练参数
model = FastLanguageModel.get_peft_model(
    model,
    r = 16,
    target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
                      "gate_proj", "up_proj", "down_proj",],
    lora_alpha = 16,
    lora_dropout = 0, 
    bias = "none",    
    use_gradient_checkpointing = True,
    random_state = 3407,
    max_seq_length = max_seq_length,
    use_rslora = False,  
    loftq_config = None, 
)

trainer = SFTTrainer(
    model = model,
    train_dataset = dataset,
    dataset_text_field = "text",
    max_seq_length = max_seq_length,
    tokenizer = tokenizer,
    args = TrainingArguments(
        per_device_train_batch_size = 2,
        gradient_accumulation_steps = 4,
        warmup_steps = 10,
        max_steps = 60,
        fp16 = not torch.cuda.is_bf16_supported(),
        bf16 = torch.cuda.is_bf16_supported(),
        logging_steps = 1,
        output_dir = "outputs",
        optim = "adamw_8bit",
        weight_decay = 0.01,
        lr_scheduler_type = "linear",
        seed = 3407,
    ),
)
#开始训练
trainer.train()

#保存微调模型
model.save_pretrained("lora_model") 

#合并模型,保存为16位hf
model.save_pretrained_merged("outputs", tokenizer, save_method = "merged_16bit",)

#合并模型,并量化成4位gguf
#model.save_pretrained_gguf("model", tokenizer, quantization_method = "q4_k_m")


如果需要量化成4位,则解开最后注释。量化过需要磁盘空间比较大。

标签:seq,max,微调,llama3,unsloth,全过程,text,model
From: https://www.cnblogs.com/shanren/p/18251730

相关文章

  • 使用GPT学术优化软件访问本地llama3-8b大模型
    (硬件环境:笔记本电脑,intel处理器i9-13900HX、64G内存、NVIDIARTX4080(12G)、操作系统windows11家庭版)一、下载中科院GPT学术优化(GPTAcademic)1.在浏览器输入:https://github.com/binary-husky/gpt_academic。在网页的右方找到并点击“Releases”。2.选择适合自己的版本,......
  • 一次讲透单片机毕业设计全过程
    毕业设计一般分以下几个过程选题(重中之重如何选题、罗列功能、避免给自己挖坑)写开题报告和任务书(最重要是功能实现介绍)实物设计或仿真设计(电路设计、代码编写)撰写论文(架构如何设计、几部分组成以及阐述什么内容)制作答辩PPT、参加毕设答辩(PPT怎么写,如何通过答辩)恭喜毕业以下......
  • TCP四次挥手全过程详解
    TCP四次挥手全过程有几点需要澄清:1.首先,tcp四次挥手只有主动和被动方之分,没有客户端和服务端的概念2.其次,发送报文段是tcp协议栈的行为,用户态调用close会陷入到内核态3.再者,图中的情况前提是双方程序正常运行,程序在挥手过程中崩溃的情况后面会讲到过程详解(时间顺序)1.......
  • 本地配置离线的llama3大模型实现chatgpt对话详细教程
    参考:Llama3本地部署及API接口本地调试,15分钟搞定最新MetaAI开源大模型本地Windows电脑部署_llama3本地部署-CSDN博客 正在下载-----importrequestsimportjsonurl="http://localhost:11434/api/generate"data={&......
  • 利用ollama本地部署Llama3大语言模型
    Meta在开源大模型方面越战越勇,近日推出的Llama3在各方面都被公认为是最出色的。利用ollama在本地部署后使用了一会,感觉确实是行云流水。简单介绍下本地部署的流程:1、下载ollama:https://ollama.com/在这里下载win环境下的.exe文件,下载后直接安装即可。2、部署Llama3:......
  • 本地如何通过Ollama部署llama3、phi3等本地大模型?
    一、ollama是什么?在本地启动并运行大型语言模型。运行Llama3,Mistral,Gemma,CodeLlama和其他模型。自定义并创建您自己的。优势如下:•快速下载+容器自动运行大模型,现在下载,马上上手。•本地利用cpu运行大模型,本地安全可靠。•ollama命令,管理大模型相对方......
  • 非常可靠,手把手教你本地部署AI大模型-llama3:70b
    Meta公司一直致力于这样一个理念:“thatopensourcenotonlyprovidesgreattechnologyfordevelopers,butalsobringsthebestoutinpeople”,翻译过来就是开源不仅为开发人员提供了出色的技术,而且还将给人们带来更好的。但是前几天李彦宏说开源模型没有未来?我们的......
  • autotrain学习-环境搭建、模型和数据集下载、训练全过程
    autotrain学习-环境搭建、模型和数据集下载、训练全过程1.参考链接2.创建容器3.安装autotrain4.解决没有真实权值的问题(不下载真实的权值)5.下载SFT微调数据集6.下载opt-125m模型(忽略权值文件)7.下载后的目录结构8.SFT训练A.生成配置文件(使用之前下载好的模型和数据集......
  • 斯坦福爆火Llama3-V竟抄袭国内开源项目,作者火速删库
        ChatGPT狂飙160天,世界已经不是之前的样子。新建了免费的人工智能中文站https://ai.weoknow.com新建了收费的人工智能中文站https://ai.hzytsoft.cn/更多资源欢迎关注斯坦福Llama3-Vvs清华MiniCPM-Llama3-V-2.5在GPT-4o出世后,Llama3的风头被狠狠盖过。......
  • (一)C#窗体应用程序打包发布安装到桌面全过程
    一、首先安装好VS2022(可以参考如下安装教程),其次在里面扩展添加VisualStudioInstallerProjects1.VS2022安装教程参考:VisualStudio2022下载安装与使用超详细教程-编程宝库(codebaoku.com)2.在VisualStudio里面安装插件二、......