北京时间4月19日凌晨,Meta公司通过其官方网站宣布了Llama系列的最新开源大模型:Llama-3。
继Llama-1、Llama-2以及Code-Llama之后,Llama-3作为该系列的第三代模型,在众多关键的基准测试中取得了全面的进步,其性能在当前业界的同类模型中处于领先地位。
01
Llama大模型家族概述
02
Llama3技术特性
01
模型架构
02
预训练数据
顶尖语言模型需要巨量优质训练数据集:Llama 3的预训练基于超过15T tokens,来自各类公开渠道,数据集规模扩大了7倍。Llama 3的预训练数据集融入了超过5%的非英语内容,覆盖了超过30种不同的语言。
保证Llama 3有最优质的训练数据集:打造了一套数据过滤流程。涵盖了启发式过滤器、NSFW内容过滤器、语义去重技术,以及用于预判数据质量的文本分类器。利用Llama 2生成了用于支持Llama 3的文本质量分类器的训练数据。
最优化融合不同源数据,构建预训练数据集:一种数据融合策略,Llama 3在处理各种场景——包括但不限于琐事问答、STEM领域、编程、历史知识等都能有出色的表现。
03
扩展预训练
并行化策略:结合了三种类型的并行化策略:数据并行化、模型并行化和流水线并行化。
04
指令微调
03
Llama3大模型微调
注:请申请一个免费 T4 GPU 来运行该脚本
%cd /content/%rm -rf LLaMA-Factory!git clone https://github.com/hiyouga/LLaMA-Factory.git%cd LLaMA-Factory%ls!pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"!pip install --no-deps xformers==0.0.25!pip install .[bitsandbytes]
检查 GPU 环境
import torchtry:assert torch.cuda.is_available() is Trueexcept AssertionError: print("ERROR,需要 GPU 环境")更新自我认知数据集
import json%cd /content/LLaMA-Factory/NAME = "Llama-Chinese"AUTHOR = "LLaMA Factory"with open("data/identity.json", "r", encoding="utf-8") as f:dataset = json.load(f)for sample in dataset:sample["output"] = sample["output"].replace("NAME", NAME).replace("AUTHOR", AUTHOR)with open("data/identity.json", "w", encoding="utf-8") as f:json.dump(dataset, f, indent=2, ensure_ascii=False)
from llmtuner import run_expfrom llmtuner.extras.misc import torch_gc%cd /content/LLaMA-Factory/run_exp(dict(stage="sft",# 进行指令监督微调do_train=True,model_name_or_path="unsloth/llama-3-8b-Instruct-bnb-4bit", # 使用 4 比特量化版 Llama-3-8b-Instruct 模型dataset="identity,alpaca_gpt4_en,alpaca_gpt4_zh",# 使用 alpaca 和自我认知数据集template="llama3", # 使用 llama3 提示词模板finetuning_type="lora", # 使用 LoRA 适配器来节省显存lora_target="all", # 添加 LoRA 适配器至全部线性层output_dir="llama3_lora",# 保存 LoRA 适配器的路径per_device_train_batch_size=2, # 批处理大小gradient_accumulation_steps=4, # 梯度累积步数lr_scheduler_type="cosine", # 使用余弦学习率退火算法logging_steps=10,# 每 10 步输出一个记录warmup_ratio=0.1,# 使用预热学习率save_steps=1000,# 每 1000 步保存一个检查点learning_rate=5e-5, # 学习率大小num_train_epochs=3.0,# 训练轮数max_samples=300,# 使用每个数据集中的 300 条样本max_grad_norm=1.0, # 将梯度范数裁剪至 1.0quantization_bit=4, # 使用 4 比特 QLoRAloraplus_lr_ratio=16.0, # 使用 LoRA+ 算法并设置 lambda=16.0use_unsloth=True,# 使用 UnslothAI 的 LoRA 优化来加快一倍的训练速度fp16=True, # 使用 float16 混合精度训练))torch_gc()
模型推理
from llmtuner import ChatModelfrom llmtuner.extras.misc import torch_gc%cd /content/LLaMA-Factory/chat_model = ChatModel(dict(model_name_or_path="unsloth/llama-3-8b-Instruct-bnb-4bit", # 使用 4 比特量化版 Llama-3-8b-Instruct 模型adapter_name_or_path="llama3_lora",# 加载之前保存的 LoRA 适配器finetuning_type="lora",# 和训练保持一致template="llama3", # 和训练保持一致quantization_bit=4,# 加载 4 比特量化模型use_unsloth=True, # 使用 UnslothAI 的 LoRA 优化来加快一倍的推理速度))messages = []while True:query = input("\nUser: ")if query.strip() == "exit":breakif query.strip() == "clear":messages = []torch_gc()print("History has been removed.")continuemessages.append({"role": "user", "content": query}) # 把提示词添加到消息中print("Assistant: ", end="", flush=True)response = ""for new_text in chat_model.stream_chat(messages):# 流式输出print(new_text, end="", flush=True)response += new_textprint()messages.append({"role": "assistant", "content": response}) # 把回答添加到消息中torch_gc()
04
Llama3的大模型测评
Llama3系列的基础预训练模型在多个评测基准的性能表现,相较于上一代的Llama2和Llama系列,性能得到了很大提升。
05
Llama3部署与应用
01
Meta AI,使用 Llama 3 构建
02
在线体验
在线体验地址:
huggingface:https://huggingface.co/chat/
Replicate:
8B 模型:hat/https://replicate.com/meta/meta-llama-3-8b
70B 模型:https://replicate.com/meta/meta-llama-3-70b
03
本地部署
安装Ollama:通过官网提供的命令或脚本在本地环境中安装Ollama。
下载Llama3模型:使用Ollama工具下载所需的Llama3模型权重文件。
ollama pull llama3:8b
运行Llama3模型:通过Ollama启动Llama3模型,进行测试或应用。
你好!请中文回复Web UI集成:为了提供更好的交互体验,可以结合open-webui来构建一个用户友好的交互界面。
docker run -d -p 3000:8080 --add-host=host.docker.internal:host-gateway -v open-webui:/app/backend/data --name open-webui --restart always ghcr.io/open-webui/open-webui:main
06
未来展望
Meta将发布多个具有新功能的模型,包括多模态、使用多种语言交谈的能力、更长的上下文窗口和更强大的整体功能。先一睹为快~
