Llama-3 8b-sft

本文介绍了如何在资源有限的环境下,利用Unsloth、Qlora和Galore技术,在GoogleColab中免费训练LLAMA-3-8b模型,包括代码示例和必要的环境设置步骤。
摘要由CSDN通过智能技术生成

如果您不想手动复制代码,请使用 Colab 文档:

  • https://colab.research.google.com/drive/1bX4BsjLcdNJnoAf7lGXmWOgaY8yekg8p?usp=sharing

Copy from my announcement in my discord:

If anyone wants to train their own llama-3-8b model for free on any dataset
 that has around 1,500 lines of data or less you can now do it easily by using
 the code I provided in the model card for my test model in this repo and
 google colab. The training for this model uses (Unsloth + Qlora + Galore) to
 achieve the ability for training under such low vram. 

For anyone that is new to coding and training Ai, all your really have to edit is

  1. (max_seq_length = 8192) To match the max tokens of the dataset or model you are using
  2. (model_name = “unsloth/llama-3-8b-Instruct”,) Change what model you are finetuning, this setup is specifically for llama-3-8b
  3. (alpaca_prompt =) Change the prompt format, this one is setup to meet llama-3-8b-instruct format, but match it to your specifications.
  4. (dataset = load_dataset(“Replete-AI/code-test-dataset”, split = “train”)) What dataset you are using from huggingface
  5. (model.push_to_hub_merged(“rombodawg/test_dataset_Codellama-3-8B”, tokenizer, save_method = “merged_16bit”, token = “”))
  6. For the above you need to change “rombodawg” to your Hugginface name, “test_dataset_Codellama-3-8B” to the model name you want saved as, and in token = “” you need to put your huggingface write token so the model can be saved.
%%capture
import torch
major_version, minor_version = torch.cuda.get_device_capability()
# Must install separately since Colab has torch 2.2.1, which breaks packages
!pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
if major_version >= 8:
    # Use this for new GPUs like Ampere, Hopper GPUs (RTX 30xx, RTX 40xx, A100, H100, L40)
    !pip install --no-deps packaging ninja einops flash-attn xformers trl peft accelerate bitsandbytes
else:
    # Use this for older GPUs (V100, Tesla T4, RTX 20xx)
    !pip install --no-deps xformers trl peft accelerate bitsandbytes
pass
!pip install galore_torch

from unsloth import FastLanguageModel
import torch
max_seq_length = 8192 # Choose any! We auto support RoPE Scaling internally!
dtype = None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+
load_in_4bit = True # Use 4bit quantization to reduce memory usage. Can be False.

# 4bit pre quantized models we support for 4x faster downloading + no OOMs.
fourbit_models = [
    "unsloth/mistral-7b-bnb-4bit",
    "unsloth/mistral-7b-instruct-v0.2-bnb-4bit",
    "unsloth/llama-2-7b-bnb-4bit",
    "unsloth/gemma-7b-bnb-4bit",
    "unsloth/gemma-7b-it-bnb-4bit", # Instruct version of Gemma 7b
    "unsloth/gemma-2b-bnb-4bit",
    "unsloth/gemma-2b-it-bnb-4bit", # Instruct version of Gemma 2b
    "unsloth/llama-3-8b-bnb-4bit", # [NEW] 15 Trillion token Llama-3
] # More models at https://huggingface.co/unsloth

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name = "unsloth/llama-3-8b-Instruct",
    max_seq_length = max_seq_length,
    dtype = dtype,
    load_in_4bit = load_in_4bit,
    # token = "hf_...", # use one if using gated models like meta-llama/Llama-2-7b-hf
)
model = FastLanguageModel.get_peft_model(
    model,
    r = 16, # Choose any number > 0 ! Suggested 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, # Supports any, but = 0 is optimized
    bias = "none",    # Supports any, but = "none" is optimized
    # [NEW] "unsloth" uses 30% less VRAM, fits 2x larger batch sizes!
    use_gradient_checkpointing = "unsloth", # True or "unsloth" for very long context
    random_state = 3407,
    use_rslora = False,  # We support rank stabilized LoRA
    loftq_config = None, # And LoftQ
)
alpaca_prompt = """<|begin_of_text|><|start_header_id|>system<|end_header_id|>

Below is an instruction that describes a task, Write a response that appropriately completes the request.<|eot_id|><|start_header_id|>user<|end_header_id|>

{}<|eot_id|><|start_header_id|>assistant<|end_header_id|>{}"""

EOS_TOKEN = tokenizer.eos_token # Must add EOS_TOKEN
def formatting_prompts_func(examples):
    inputs       = examples["human"]
    outputs      = examples["assistant"]
    texts = []
    for input, output in zip(inputs, outputs):
        # Must add EOS_TOKEN, otherwise your generation will go on forever!
        text = alpaca_prompt.format(input, output) + EOS_TOKEN
        texts.append(text)
    return { "text" : texts, }
pass

from datasets import load_dataset
dataset = load_dataset("Replete-AI/code-test-dataset", split = "train")
dataset = dataset.map(formatting_prompts_func, batched = True,)
from trl import SFTTrainer
from transformers import TrainingArguments
from galore_torch import GaLoreAdamW8bit
import torch.nn as nn
galore_params = []
target_modules_list = ["attn", "mlp"]
for module_name, module in model.named_modules():
    if not isinstance(module, nn.Linear):
        continue

    if not any(target_key in module_name for target_key in target_modules_list):
        continue

    print('mod ', module_name)
    galore_params.append(module.weight)
id_galore_params = [id(p) for p in galore_params]
regular_params = [p for p in model.parameters() if id(p) not in id_galore_params]


param_groups = [{'params': regular_params},
                {'params': galore_params, 'rank': 64, 'update_proj_gap': 200, 'scale': 0.25, 'proj_type': 'std'}]

optimizer = GaLoreAdamW8bit(param_groups, lr=2e-5)

trainer = SFTTrainer(
    model = model,
    tokenizer = tokenizer,
    train_dataset = dataset,
    optimizers=(optimizer, None),
    dataset_text_field = "text",
    max_seq_length = max_seq_length,
    dataset_num_proc = 2,
    packing = True, # Can make training 5x faster for short sequences.
    args = TrainingArguments(
        per_device_train_batch_size = 1,
        gradient_accumulation_steps = 4,
        warmup_steps = 5,
        learning_rate = 2e-4,
        fp16 = not torch.cuda.is_bf16_supported(),
        bf16 = torch.cuda.is_bf16_supported(),
        logging_steps = 1,
        weight_decay = 0.01,
        lr_scheduler_type = "linear",
        seed = 3407,
        output_dir = "outputs",
    ),
)
trainer_stats = trainer.train()
model.save_pretrained_merged("model", tokenizer, save_method = "merged_16bit",)
model.push_to_hub_merged("rombodawg/test_dataset_Codellama-3-8B", tokenizer, save_method = "merged_16bit", token = "")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值