llama3入门训练和部署

教程一

https://www.youtube.com/watch?v=oxTVzGwKeoU

https://www.youtube.com/watch?v=oxTVzGwKeoU

  1. 下载 alpaca 数据集(斯坦福大学的数据集,通过gpt 转换成)
    https://huggingface.co/datasets/shibing624/alpaca-zh
  2. 将自己的数据集json 添加到上面下载的数据集中
  3. 使用 unsloth 进行微调 python app.py
    训练后的模型训练为 gguf 格式
from unsloth import FastLanguageModel
import torch

from trl import SFTTrainer
from transformers import TrainingArguments




max_seq_length = 2048 # 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-bnb-4bit",
    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 = """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

file_path = "/home/Ubuntu/alpaca_gpt4_data_zh.json"


dataset = load_dataset("json", data_files={"train": file_path}, split="train")

dataset = dataset.map(formatting_prompts_func, batched = True,)




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,
        max_steps = 60,
        learning_rate = 2e-4,
        fp16 = not torch.cuda.is_bf16_supported(),
        bf16 = torch.cuda.is_bf16_supported(),
        logging_steps = 1,
        optim = "adamw_8bit",
        weight_decay = 0.01,
        lr_scheduler_type = "linear",
        seed = 3407,
        output_dir = "outputs",
    ),
)

trainer_stats = trainer.train()

model.save_pretrained_gguf("dir", tokenizer, quantization_method = "q4_k_m")
model.save_pretrained_gguf("dir", tokenizer, quantization_method = "q8_0")
model.save_pretrained_gguf("dir", tokenizer, quantization_method = "f16") 
  1. 本地加载微调好的大模型
    使用工具: Ollama / LM Studio
    先安装 ollama
    创建一个空文件,Modelfile ,输入:

使用:
在这里插入图片描述

教程二

https://www.bilibili.com/video/BV1AH4y137tR/

1.准备数据 json 格式 数据集文件可以是多个json 文件
2.官网下载模型已微调的中文模型
https://huggingface.co/shenzhi-wang/Llama3-8B-Chinese-Chat-GGUF-8bit/tree/v1
3. 微调工具: peft / llamafactory /unsloth
4. 微调方式: lora 微调之后并不是模型的所有,需要再合并上原始的模型 (官网有脚本案例)
5. 量化 llama.cpp (github) 注意: 进入llama.cpp 文件的格式都必须是.gguf 的格式,有工具可以进行转换
1)格式转化
2) quantize.exe
6. 部署 ollama lmstudio

ollama: modefile文件里修改模型路径

  1. phidata 做本地知识库
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
LLaMA+Alpaca是一个基于PyTorch的自然语言处理框架,主要用于生成式对话模型的训练部署。以下是搭建、部署训练LLaMA+Alpaca的步骤: 1. 安装依赖项:在安装LLaMA+Alpaca之前需要安装以下依赖项: - Python 3.7或更高版本 - PyTorch 1.7或更高版本 - Transformers 4.0或更高版本 - Flask 2. 下载代码:可以从LLaMA+Alpaca的GitHub页面上下载代码,也可以使用以下命令从GitHub上克隆代码: ``` git clone https://github.com/microsoft/LLaMA.git ``` 3. 部署:可以使用以下命令启动LLaMA+Alpaca的部署: ``` cd LLaMA/deployment python app.py ``` 这将会在本地启动一个Flask服务器并提供对话API。 4. 训练模型:可以使用以下命令在LLaMA+Alpaca上训练对话模型: ``` python train.py \ --dataset_path <path-to-dataset> \ --tokenizer_name <tokenizer-name> \ --model_name_or_path <model-name-or-path> \ --output_dir <output-dir> \ --num_train_epochs <num-epochs> \ --per_device_train_batch_size <batch-size> \ --gradient_accumulation_steps <accumulation-steps> \ --learning_rate <learning-rate> ``` 其中,\<path-to-dataset>是对话数据集的路径,\<tokenizer-name>和\<model-name-or-path>分别是使用的tokenizer和模型名称或路径,\<output-dir>是输出路径,\<num-epochs>是训练的epoch数,\<batch-size>是每个GPU上的批量大小,\<accumulation-steps>是梯度累积步骤数,\<learning-rate>是学习率。 5. 部署新模型:可以使用以下命令将新训练的模型部署到Flask服务器上: ``` python update_model.py \ --model_path <path-to-model> \ --tokenizer_name <tokenizer-name> \ --model_name <model-name> ``` 其中,\<path-to-model>是新训练的模型路径,\<tokenizer-name>是使用的tokenizer名称,\<model-name>是新模型名称。 以上就是搭建、部署训练LLaMA+Alpaca的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闪闪发亮的小星星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值