llama3.1-8B-微调

https://github.com/unslothai/unsloth

使用unslothai微调

1)准备环境

%%capture
# Installs Unsloth, Xformers (Flash Attention) and all other packages!
!pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
!pip install --no-deps "xformers<0.0.27" "trl<0.9.0" peft accelerate bitsandbytes

2)加载模型
记得先

!pip install pyarrow==8.0.0

不然会报错

from unsloth import FastLanguageModel
import torch

# 配置参数
max_seq_length = 2048  # 可以选择任意长度!我们内部自动支持RoPE Scaling
dtype = None  # None表示自动检测。对于Tesla T4、V100选择Float16,Ampere+选择Bfloat16
load_in_4bit = True  # 使用4bit量化以减少内存使用。可以设为False

# 支持的4bit预量化模型,可实现4倍下载速度提升且无OOM(内存溢出)问题
fourbit_models = [
    "unsloth/Meta-Llama-3.1-8B-bnb-4bit",      # Llama-3.1 15万亿tokens模型,速度提升2倍!
    "unsloth/Meta-Llama-3.1-8B-Instruct-bnb-4bit",
    "unsloth/Meta-Llama-3.1-70B-bnb-4bit",
    "unsloth/Meta-Llama-3.1-405B-bnb-4bit",    # 我们还上传了405b的4bit版本!
    "unsloth/Mistral-Nemo-Base-2407-bnb-4bit", # 新Mistral 12b,速度提升2倍!
    "unsloth/Mistral-Nemo-Instruct-2407-bnb-4bit",
    "unsloth/mistral-7b-v0.3-bnb-4bit",        # Mistral v3,速度提升2倍!
    "unsloth/mistral-7b-instruct-v0.3-bnb-4bit",
    "unsloth/Phi-3-mini-4k-instruct",          # Phi-3,速度提升2倍!
    "unsloth/Phi-3-medium-4k-instruct",
    "unsloth/gemma-2-9b-bnb-4bit",
    "unsloth/gemma-2-27b-bnb-4bit",            # Gemma,速度提升2倍!
]  # 更多模型见 https://huggingface.co/unsloth

# 加载模型和tokenizer
model, tokenizer = FastLanguageModel.from_pretrained(
    model_name="unsloth/Meta-Llama-3.1-8B",
    max_seq_length=max_seq_length,
    dtype=dtype,
    load_in_4bit=load_in_4bit,
    # token="hf_...",  # 如果使用需要访问权限的模型(如meta-llama/Llama-2-7b-hf),请添加token
)

3)应用LoRA技术来减少模型训练和推理时的内存使用,同时保持模型性能。

model = FastLanguageModel.get_peft_model(
    model,
    r=16,  # 选择任意大于0的数值,建议使用8、16、32、64、128
    target_modules=["q_proj", "k_proj", "v_proj", "o_proj",
                    "gate_proj", "up_proj", "down_proj"],
    lora_alpha=16,
    lora_dropout=0,  # 支持任何值,但设置为0是优化的
    bias="none",    # 支持任何值,但设置为"none"是优化的
    use_gradient_checkpointing="unsloth",  # 设置为True或"unsloth"以支持超长上下文
    random_state=3407,
    use_rslora=False,  # 支持使用rank stabilized LoRA
    loftq_config=None,  # 以及LoftQ配置
)

4)加载一个数据集,并规范格式

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 # Must add 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):
        # Must add EOS_TOKEN, otherwise your generation will go on forever!
        text = alpaca_prompt.format(instruction, input, output) + EOS_TOKEN
        texts.append(text)
    return { "text" : texts, }
pass

from datasets import load_dataset
dataset = load_dataset("leo009/mytest", split = "train")
dataset = dataset.map(formatting_prompts_func, batched = True,)

5)初始化了一个 SFTTrainer 实例,用于训练一个语言模型。SFTTrainer 是一种在 trl 库中用于特定任务(例如微调)的训练类。代码配置了训练参数、数据集和模型,并为训练过程定义了一些关键设置。

from trl import SFTTrainer
from transformers import TrainingArguments
from unsloth import is_bfloat16_supported

trainer = SFTTrainer(
    model = model,
    tokenizer = tokenizer,
    train_dataset = dataset,
    dataset_text_field = "text",
    max_seq_length = max_seq_length,
    dataset_num_proc = 2,
    packing = False, # Can make training 5x faster for short sequences.
    args = TrainingArguments(
        per_device_train_batch_size = 2,
        gradient_accumulation_steps = 4,
        warmup_steps = 5,
        # num_train_epochs = 1, # Set this for 1 full training run.
        max_steps = 60,
        learning_rate = 2e-4,
        fp16 = not is_bfloat16_supported(),
        bf16 = is_bfloat16_supported(),
        logging_steps = 1,
        optim = "adamw_8bit",
        weight_decay = 0.01,
        lr_scheduler_type = "linear",
        seed = 3407,
        output_dir = "outputs",
    ),
)

6)开始训练

trainer_stats = trainer.train()

7)使用 FastLanguageModel 进行模型推理,生成一个给定指令的响应。

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) # Enable native 2x faster inference
inputs = tokenizer(
[
    alpaca_prompt.format(
        "介绍AI超元域频道.", # instruction
        "", # input
        "", # output - leave this blank for generation!
    )
], return_tensors = "pt").to("cuda")

outputs = model.generate(**inputs, max_new_tokens = 64, use_cache = True)
tokenizer.batch_decode(outputs)

8)保存模型

model.save_pretrained("lora_model") # Local saving

tokenizer.save_pretrained("lora_model")

9)推送模型到hugging-face

model.push_to_hub("fengn/llama3.1-lora", token = "") # Online saving
tokenizer.push_to_hub("fengn/llama3.1-lora", token = "") # Online saving

10)加在本地模型进行推理

if True:
    from unsloth import FastLanguageModel
    model, tokenizer = FastLanguageModel.from_pretrained(
        model_name = "lora_model", # YOUR MODEL YOU USED FOR TRAINING
        max_seq_length = max_seq_length,
        dtype = dtype,
        load_in_4bit = load_in_4bit,
    )
    FastLanguageModel.for_inference(model) # Enable native 2x faster inference

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:
{}"""

inputs = tokenizer(
[
    alpaca_prompt.format(
        "AI超元域是谁?", # instruction
        "", # input
        "", # output - leave this blank for generation!
    )
], return_tensors = "pt").to("cuda")

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

11)

  • 16-bit 和 4-bit 合并保存: 通过将模型保存为 16-bit 或 4-bit 格式来优化存储和计算效率。
  • 仅 LoRA 适配器保存: 只保存 LoRA 适配器而不是整个模型,这对共享或部署经过特定任务微调的适配器非常有用。
# Merge to 16bit
if True: model.save_pretrained_merged("model", tokenizer, save_method = "merged_16bit",)
if True: model.push_to_hub_merged("hf/model", tokenizer, save_method = "merged_16bit", token = "")

# Merge to 4bit
if True: model.save_pretrained_merged("model", tokenizer, save_method = "merged_4bit",)
if True: model.push_to_hub_merged("hf/model", tokenizer, save_method = "merged_4bit", token = "")

# Just LoRA adapters
if True: model.save_pretrained_merged("model", tokenizer, save_method = "lora",)
if True: model.push_to_hub_merged("hf/model", tokenizer, save_method = "lora", token = "")

12)量化

# Save to 8bit Q8_0
if True: model.save_pretrained_gguf("model", tokenizer,)
# Remember to go to https://huggingface.co/settings/tokens for a token!
# And change hf to your username!
if True: model.push_to_hub_gguf("hf/model", tokenizer, token = "")

# Save to 16bit GGUF
if True: model.save_pretrained_gguf("model", tokenizer, quantization_method = "f16")
if True: model.push_to_hub_gguf("hf/model", tokenizer, quantization_method = "f16", token = "")

# Save to q4_k_m GGUF
if True: model.save_pretrained_gguf("model", tokenizer, quantization_method = "q4_k_m")
if True: model.push_to_hub_gguf("hf/model", tokenizer, quantization_method = "q4_k_m", token = "")

# Save to multiple GGUF options - much faster if you want multiple!
if True:
    model.push_to_hub_gguf(
        "hf/model", # Change hf to your username!
        tokenizer,
        quantization_method = ["q4_k_m", "q8_0", "q5_k_m",],
        token = "", # Get a token at https://huggingface.co/settings/tokens
    )

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Llama3-8b是一个开源的聊天机器人模型,可以用于自然语言处理和对话生成任务。如果您希望进行私有化部署,可以按照以下步骤进行操作: 1. 获取源代码:首先,您需要从Llama3-8b的开源代码库中获取源代码。您可以在GitHub上找到该项目,并将其克隆到本地。 2. 环境配置:在进行私有化部署之前,您需要配置适当的环境。确保您的系统满足所需的软件和硬件要求,并安装必要的依赖项。 3. 数据准备:为了训练和使用Llama3-8b模型,您需要准备相应的数据集。这可能包括对话数据、语料库等。确保数据集的质量和多样性,以提高模型的性能。 4. 模型训练:使用准备好的数据集,您可以开始训练Llama3-8b模型。根据您的需求和资源情况,您可以选择在单个GPU或多个GPU上进行训练。训练过程可能需要一定的时间和计算资源。 5. 模型部署:一旦训练完成,您可以将Llama3-8b模型部署到私有环境中。这可能涉及将模型加载到服务器或云平台,并设置相应的API接口供其他应用程序调用。 6. 安全性和隐私保护:在进行私有化部署时,确保采取适当的安全措施来保护用户数据和系统安全。这可能包括数据加密、访问控制、身份验证等。 7. 持续优化和更新:私有化部署后,您可以根据实际需求对Llama3-8b模型进行持续优化和更新。这可能包括增加新的训练数据、微调模型参数等。 希望以上步骤对您进行Llama3-8b的私有化部署提供了一些指导。如果您有任何进一步的问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值