Llama 3.2 发布:多模态视觉模型与 1B/3B 端侧文本模型上线 Hugging Face
Llama can now see and run on your device - welcome Llama 3.2
Meta 发布 Llama 3.2,共 10 个开放权重模型,包括 11B 和 90B 两个尺寸的视觉模型(各含 base 与指令微调版)以及 1B、3B 端侧文本模型,均已上线 Hugging Face Hub。
Meta 与 Hugging Face 同步放出 10 个开放权重模型,含 11B/90B 视觉版与 1B/3B 端侧文本版,可据此判断本地多模态与端侧部署的可行边界。
Llama 3.2 发布了!今天,我们欢迎 Llama 系列 的下一个迭代版本加入 Hugging Face。这一次,我们很高兴与 Meta 合作发布多模态和小型模型。Hub 上提供了十个开放权重模型(5 个多模态模型和 5 个纯文本模型)。
Llama 3.2 Vision 有两种规模:11B 适用于在消费级 GPU 上进行高效部署和开发,90B 适用于大规模应用。两个版本都提供基础版和指令微调版。除了四个多模态模型外,Meta 还发布了支持视觉功能的新版 Llama Guard。Llama Guard 3 是一个防护模型,可以对模型输入和生成内容进行分类,包括检测有害的多模态提示或助手响应。
Llama 3.2 还包括可以在设备上运行的小型纯文本语言模型。它们有两种新规模(1B 和 3B),提供基础版和指令版,并且在其规模下具有强大的能力。还有一个 1B 的小型 Llama Guard 版本,可以在生产用例中与这些模型或更大的文本模型一起部署。
在发布的功能和集成中,我们有:
- Hub 上的模型检查点
- Hugging Face Transformers 和 TGI 对 Vision 模型的集成
- 与 Inference Endpoints、Google Cloud、Amazon SageMaker 和 DELL Enterprise Hub 的推理与部署集成
- 使用 transformers🤗 和 TRL 在单个 GPU 上微调 Llama 3.2 11B Vision
目录
- 什么是 Llama 3.2 Vision?
- Llama 3.2 许可证变更。抱歉,欧盟 :(
- Llama 3.2 1B 和 3B 有什么特别之处?
- 演示
- 使用 Hugging Face Transformers
- Llama 3.2 1B 和 3B 语言模型
- Llama 3.2 Vision
- 设备端
- Llama.cpp 和 Llama-cpp-python
- Transformers.js
- 微调 Llama 3.2
- Hugging Face 合作伙伴集成
- 其他资源
- 致谢
什么是 Llama 3.2 Vision?
Llama 3.2 Vision 是 Meta 发布的最强大的开放多模态模型。它具有出色的视觉理解和推理能力,可用于完成各种任务,包括视觉推理和定位、文档问答以及图像文本检索。思维链(CoT)答案通常非常好,这使得视觉推理特别强大。
Llama 3.2 Vision 可用于处理文本和图像,也可仅处理文本。使用图像文本提示时,模型可以接受英语输入,而仅使用文本提示时,模型可以处理多种语言。
纯文本模式下的完整语言列表为:
- 英语
- 德语
- 法语
- 意大利语
- 葡萄牙语
- 印地语
- 西班牙语
- 泰语
这些模型的架构基于 Llama 3.1 LLM 与视觉塔和图像适配器的组合。使用的文本模型是 Llama 3.1 8B 用于 Llama 3.2 11B Vision 模型,以及 Llama 3.1 70B 用于 3.2 90B Vision 模型。据我们了解,在训练视觉模型期间,文本模型被冻结以保持纯文本性能。
下面您可以找到来自 11B 指令微调模型的一些推理示例,展示了现实世界知识、文档推理和信息图表理解能力。
| 输入图像 | 输入提示 | 答案 |
|---|---|---|
![]() |
它位于哪里?附近还有哪些其他景点? |
The image depicts the Grand Palace in Bangkok, Thailand. The Grand Palace is a complex of buildings that served as the official residence of the King of Siam and the administrative seat of government for 150 years. It is one of the most popular tourist attractions in Bangkok and a must-see for anyone visiting the city.
大皇宫附近的其他一些景点包括: |
![]() |
根据这张图,干旱发生在哪里? |
2016年干旱地区 根据该图,2016年干旱发生在两个不同的地区: * 东部和南部非洲:该地区经历了严重干旱。 * 智利:智利经历了50多年来最干旱的一月。这些地区在那段时期面临严重的水资源短缺问题。 |
![]() |
从发票日期到到期日期需要多长时间?请简短回答。 |
发票日期:2019年2月11日 到期日期:2019年2月26日 时间差计算: 2019年2月26日 - 2019年2月11日 = 15天 发票日期与到期日期之间的时间差为15天。 |
视觉模型的上下文长度为128k个token,这允许进行可能包含图像的多轮对话。然而,该模型在处理单张图像时效果最佳,因此transformers实现仅处理输入中提供的最后一张图像。这可以保持质量并节省内存。
11B基础模型支持448的瓦片尺寸,而指令版本和90B模型均使用560的瓦片尺寸。这些模型在包含多样化数据混合的60亿图像-文本对的大规模数据集上进行了训练。这使它们成为下游任务微调的绝佳候选。作为参考,您可以查看下方Meta报告的11B、90B及其指令微调版本在一些基准测试中的对比。请参阅模型卡以获取更多基准测试和详细信息。
| 11B | 11B(指令微调) | 90B | 90B(指令微调) | 指标 | |
|---|---|---|---|---|---|
| MMMU(验证集) | 41.7 | 50.7(CoT) | 49.3(零样本) | 60.3(CoT) | 微平均准确率 |
| VQAv2 | 66.8(验证集) | 75.2(测试集) | 73.6(验证集) | 78.1(测试集) | 准确率 |
| DocVQA | 62.3(验证集) | 88.4(测试集) | 70.7(验证集) | 90.1(测试集) | ANLS |
| AI2D | 62.4 | 91.1 | 75.3 | 92.3 | 准确率 |
我们预计这些模型的文本能力将分别与8B和70B Llama 3.1模型相当,因为我们的理解是文本模型在视觉模型训练期间被冻结。因此,文本基准测试应与8B和70B一致。
Llama 3.2许可证变更。抱歉,欧盟 :(
关于许可条款,Llama 3.2的许可证与Llama 3.1非常相似,但在可接受使用政策中有一个关键区别:任何居住在欧盟的个人,或主要营业地在欧盟的公司,均未被授予使用Llama 3.2中包含的多模态模型的许可权利。此限制不适用于包含任何此类多模态模型的产品或服务的最终用户,因此人们仍然可以使用视觉变体构建全球产品。
Llama 3.2 1B 和 3B 有什么特别之处?
Llama 3.2 系列包含 1B 和 3B 文本模型。这些模型专为设备端使用场景设计,例如提示重写、多语言知识检索、摘要任务、工具使用以及本地运行的助手。在这些规模上,它们的表现优于许多现有的开源模型,并能与规模大数倍的模型竞争。在后面的章节中,我们将展示如何离线运行这些模型。
这些模型遵循与 Llama 3.1 相同的架构。它们使用多达 9 万亿个 token 进行训练,并且仍然支持 128k token 的长上下文长度。这些模型是多语言的,支持英语、德语、法语、意大利语、葡萄牙语、印地语、西班牙语和泰语。
还有一个新版本的小型 Llama Guard,即 Llama Guard 3 1B,它可以与这些模型一起部署,用于评估多轮对话中最近的用户或助手响应。它使用一组预定义的类别(此版本新增)可以自定义或排除,以适应开发者的使用场景。有关 Llama Guard 使用的更多详细信息,请参阅模型卡片。
额外好处:Llama 3.2 接触的语言范围比上述支持的 8 种语言更广泛。鼓励开发者针对其特定的语言使用场景对 Llama 3.2 模型进行微调。
我们通过 Open LLM Leaderboard 评估套件对基础模型进行了测试,而指令模型则在三个流行的基准上进行了评估,这些基准衡量指令遵循能力,并与 LMSYS Chatbot Arena 有很好的相关性:IFEval、AlpacaEval 和 MixEval-Hard。以下是基础模型的结果,其中包含 Llama-3.1-8B 作为参考:
| 模型 | BBH | MATH Lvl 5 | GPQA | MUSR | MMLU-PRO | 平均 |
|---|---|---|---|---|---|---|
| Meta-Llama-3.2-1B | 4.37 | 0.23 | 0.00 | 2.56 | 2.26 | 1.88 |
| Meta-Llama-3.2-3B | 14.73 | 1.28 | 4.03 | 3.39 | 16.57 | 8.00 |
| Meta-Llama-3.1-8B | 25.29 | 4.61 | 6.15 | 8.98 | 24.95 | 14.00 |
以下是指令模型的结果,其中包含 Llama-3.1-8B-Instruct 作为参考:
| 模型 | AlpacaEval (LC) | IFEval | MixEval-Hard | 平均 |
|---|---|---|---|---|
| Meta-Llama-3.2-1B-Instruct | 7.17 | 58.92 | 26.10 | 30.73 |
| Meta-Llama-3.2-3B-Instruct | 20.88 | 77.01 | 31.80 | 43.23 |
| Meta-Llama-3.1-8B-Instruct | 25.74 | 76.49 | 44.10 | 48.78 |
值得注意的是,3B 模型在 IFEval 上与 8B 模型一样强大!这使得该模型非常适合代理应用,在这些应用中,遵循指令对于提高可靠性至关重要。对于这种规模的模型来说,如此高的 IFEval 分数非常令人印象深刻。
1B 和 3B 指令微调模型都支持工具使用。工具由用户在零样本设置中指定(模型没有关于开发者将使用的工具的先前信息)。因此,Llama 3.1 模型内置的工具(brave_search 和 wolfram_alpha)不再可用。
由于规模较小,这些小型模型可以用作更大模型的助手,并执行辅助生成(也称为推测解码)。这里是一个使用 Llama 3.2 1B 模型作为 Llama 3.1 8B 模型助手的示例。对于离线使用场景,请查看文章后面的设备端部分。
演示
您可以在以下演示中试用这三个 Instruct 模型:
- 使用 Llama 3.2 11B Vision Instruct 的 Gradio Space
- 由 Gradio 驱动的 Llama 3.2 3B Space
- 在 WebGPU 上运行的 Llama 3.2 3B
- 由 MLC Web-LLM 驱动的 WebGPU Llama 3.2 3B
使用 Hugging Face Transformers
纯文本检查点与之前发布的版本具有相同的架构,因此无需更新您的环境。然而,鉴于新架构,Llama 3.2 Vision 需要更新 Transformers。请确保将您的安装升级到 4.45.0 或更高版本。
pip install "transformers>=4.45.0" --upgrade
升级后,您可以使用新的 Llama 3.2 模型,并利用 Hugging Face 生态系统的所有工具。
Llama 3.2 1B & 3B 语言模型
您只需几行代码即可使用 Transformers 运行 1B 和 3B 文本模型检查点。模型检查点以 bfloat16 精度上传,但您也可以使用 float16 或量化权重。内存需求取决于模型大小和权重精度。下表显示了使用不同配置进行推理所需的大致内存:
| 模型大小 | BF16/FP16 | FP8 | INT4 |
|---|---|---|---|
| 3B | 6.5 GB | 3.2 GB | 1.75 GB |
| 1B | 2.5 GB | 1.25 GB | 0.75 GB |
from transformers import pipeline
import torch
model_id = "meta-llama/Llama-3.2-3B-Instruct"
pipe = pipeline(
"text-generation",
model=model_id,
torch_dtype=torch.bfloat16,
device_map="auto",
)
messages = [
{"role": "user", "content": "Who are you? Please, answer in pirate-speak."},
]
outputs = pipe(
messages,
max_new_tokens=256,
)
response = outputs[0]["generated_text"][-1]["content"]
print(response)
# Arrrr, me hearty! Yer lookin' fer a bit o' information about meself, eh? Alright then, matey! I be a language-generatin' swashbuckler, a digital buccaneer with a penchant fer spinnin' words into gold doubloons o' knowledge! Me name be... (dramatic pause)...Assistant! Aye, that be me name, and I be here to help ye navigate the seven seas o' questions and find the hidden treasure o' answers! So hoist the sails and set course fer adventure, me hearty! What be yer first question?
几个细节:
我们以
bfloat16加载模型。如上所述,这是 Meta 发布的原始检查点使用的类型,因此这是确保最佳精度或进行评估的推荐运行方式。根据您的硬件,float16 可能更快。默认情况下,transformers 使用与原始 meta 代码库相同的采样参数(temperature=0.6 和 top_p=0.9)。我们尚未进行广泛测试,欢迎探索!
Llama 3.2 Vision
视觉模型更大,因此运行它们比小型文本模型需要更多内存。作为参考,11B 视觉模型在 4 位模式下推理时大约需要 10 GB 的 GPU RAM。
使用指令调优的 Llama Vision 模型进行推理的最简单方法是使用内置的聊天模板。输入具有 user 和 assistant 角色以指示对话轮次。与文本模型的一个区别是不支持系统角色。用户轮次可以包含图像-文本或纯文本输入。要指示输入包含图像,请在输入的内容部分添加 {"type": "image"},然后将图像数据传递给 processor:
import requests
import torch
from PIL import Image
from transformers import MllamaForConditionalGeneration, AutoProcessor
model_id = "meta-llama/Llama-3.2-11B-Vision-Instruct"
model = MllamaForConditionalGeneration.from_pretrained(
model_id,
torch_dtype=torch.bfloat16,
device="cuda",
)
processor = AutoProcessor.from_pretrained(model_id)
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/0052a70beed5bf71b92610a43a52df6d286cd5f3/diffusers/rabbit.jpg"
image = Image.open(requests.get(url, stream=True).raw)
messages = [
{"role": "user", "content": [
{"type": "image"},
{"type": "text", "text": "Can you please describe this image in just one sentence?"}
]}
]
input_text = processor.apply_chat_template(
messages, add_generation_prompt=True,
)
inputs = processor(
image,
input_text,
add_special_tokens=False,
return_tensors="pt",
).to(model.device)
output = model.generate(**inputs, max_new_tokens=70)
print(processor.decode(output[0][inputs["input_ids"].shape[-1]:]))
## The image depicts a rabbit dressed in a blue coat and brown vest, standing on a dirt road in front of a stone house.
您可以继续关于图像的对话。但请记住,如果您在新的用户轮次中提供新图像,模型将从那一刻起引用新图像。您不能同时查询两个不同的图像。这是之前对话继续的示例,我们在对话中添加了助手轮次并询问更多细节:
messages = [
{"role": "user", "content": [
{"type": "image"},
{"type": "text", "text": "Can you please describe this image in just one sentence?"}
]},
{"role": "assistant", "content": "The image depicts a rabbit dressed in a blue coat and brown vest, standing on a dirt road in front of a stone house."},
{"role": "user", "content": "What is in the background?"}
]
input_text = processor.apply_chat_template(
messages,
add_generation_prompt=True,
)
inputs = processor(image, input_text, return_tensors="pt").to(model.device)
output = model.generate(**inputs, max_new_tokens=70)
print(processor.decode(output[0][inputs["input_ids"].shape[-1]:]))
这是我们得到的回复:
In the background, there is a stone house with a thatched roof, a dirt road, a field of flowers, and rolling hills.
您还可以使用 bitsandbytes 库自动量化模型,以 8 位甚至 4 位模式加载它。以下是以 4 位加载生成管道的方法:
import torch
from transformers import MllamaForConditionalGeneration, AutoProcessor
+from transformers import BitsAndBytesConfig
+bnb_config = BitsAndBytesConfig(
+ load_in_4bit=True,
+ bnb_4bit_quant_type="nf4",
+ bnb_4bit_compute_dtype=torch.bfloat16
)
model = MllamaForConditionalGeneration.from_pretrained(
model_id,
- torch_dtype=torch.bfloat16,
- device="cuda",
+ quantization_config=bnb_config,
)
然后,您可以像之前一样应用聊天模板、使用处理器并调用模型。
设备端
您可以使用以下几个开源库直接在设备的 CPU/GPU/浏览器上运行 Llama 3.2 1B 和 3B。
Llama.cpp & Llama-cpp-python
Llama.cpp 是跨平台设备端 ML 推理的首选框架。我们在此集合中为 1B 和 3B 模型提供了量化的 4 位和 8 位权重。我们期望社区接受这些模型并创建额外的量化和微调。您可以在这里找到所有量化的 Llama 3.2 模型。
以下是如何直接使用 llama.cpp 使用这些检查点。
通过 brew 安装 llama.cpp(适用于 Mac 和 Linux)。
brew install llama.cpp
你可以使用 CLI 运行单次生成,或调用 llama.cpp 服务器,它与 OpenAI 消息规范兼容。
你可以使用类似这样的命令运行 CLI:
llama-cli --hf-repo hugging-quants/Llama-3.2-3B-Instruct-Q8_0-GGUF --hf-file llama-3.2-3b-instruct-q8_0.gguf -p "The meaning to life and the universe is"
你可以这样启动服务器:
llama-server --hf-repo hugging-quants/Llama-3.2-3B-Instruct-Q8_0-GGUF --hf-file llama-3.2-3b-instruct-q8_0.gguf -c 2048
你也可以使用 llama-cpp-python 在 Python 中以编程方式访问这些模型。使用以下命令从 PyPI 安装该库:
pip install llama-cpp-python
然后,你可以按如下方式运行模型:
from llama_cpp import Llama
llm = Llama.from_pretrained(
repo_id="hugging-quants/Llama-3.2-3B-Instruct-Q8_0-GGUF",
filename="*q8_0.gguf",
)
output = llm.create_chat_completion(
messages = [
{
"role": "user",
"content": "What is the capital of France?"
}
]
)
print(output)
Transformers.js
你甚至可以使用 Transformers.js 在浏览器中(或任何 JavaScript 运行时,如 Node.js、Deno 或 Bun)运行 Llama 3.2。你可以在 Hub 上找到 ONNX 模型。如果尚未安装,你可以使用以下命令从 NPM 安装该库:
npm i @huggingface/transformers
然后,你可以按如下方式运行模型:
import { pipeline } from "@huggingface/transformers";
// Create a text generation pipeline
const generator = await pipeline("text-generation", "onnx-community/Llama-3.2-1B-Instruct");
// Define the list of messages
const messages = [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Tell me a joke." },
];
// Generate a response
const output = await generator(messages, { max_new_tokens: 128 });
console.log(output[0].generated_text.at(-1).content);
示例输出
Here's a joke for you:
What do you call a fake noodle?
An impasta!
I hope that made you laugh! Do you want to hear another one?
MLC.ai Web-LLM
MLC.ai Web-LLM 是一个高性能的浏览器内 LLM 推理引擎,通过硬件加速将语言模型推理直接带到 Web 浏览器上。一切都在浏览器内运行,无需服务器支持,并通过 WebGPU 加速。
WebLLM 与 OpenAI API 完全兼容。也就是说,你可以在任何开源模型上本地使用相同的 OpenAI API,功能包括流式传输、JSON 模式、函数调用等。
你可以从 npm 安装 Web-LLM
npm install @mlc/web-llm
然后,你可以按如下方式运行模型:
// Import everything
import * as webllm from "@mlc-ai/web-llm";
// Or only import what you need
import { CreateMLCEngine } from "@mlc-ai/web-llm";
// Callback function to update model loading progress
const initProgressCallback = (initProgress) => {
console.log(initProgress);
}
const selectedModel = "Llama-3.2-3B-Instruct-q4f32_1-MLC";
const engine = await CreateMLCEngine(
selectedModel,
{ initProgressCallback: initProgressCallback }, // engineConfig
);
成功初始化引擎后,你现在可以通过 engine.chat.completions 接口使用 OpenAI 风格的聊天 API 来调用聊天补全。
const messages = [
{ role: "system", content: "You are a helpful AI assistant." },
{ role: "user", content: "Explain the meaning of life as a pirate!" },
]
const reply = await engine.chat.completions.create({
messages,
});
console.log(reply.choices[0].message);
console.log(reply.usage);
微调 Llama 3.2
TRL 开箱即支持使用 Llama 3.2 文本模型进行聊天和微调:
# Chat
trl chat --model_name_or_path meta-llama/Llama-3.2-3B
# Fine-tune
trl sft --model_name_or_path meta-llama/Llama-3.2-3B \
--dataset_name HuggingFaceH4/no_robots \
--output_dir Llama-3.2-3B-Instruct-sft \
--gradient_checkpointing
TRL 中也通过此脚本支持微调 Llama 3.2 Vision。
# Tested on 8x H100 GPUs
accelerate launch --config_file=examples/accelerate_configs/deepspeed_zero3.yaml \
examples/sft_visual_chat/sft_visual_chat.py \
--dataset_name HuggingFaceH4/llava-instruct-mix-vsft \
--model_name_or_path meta-llama/Llama-3.2-11B-Vision-Instruct \
--per_device_train_batch_size 8 \
--gradient_accumulation_steps 8 \
--output_dir Llama-3.2-11B-Vision-Instruct-sft \
--bf16 \
--torch_dtype bfloat16 \
--gradient_checkpointing
你还可以查看此 notebook,了解使用 transformers 和 PEFT 进行 LoRA 微调。
Hugging Face 合作伙伴集成
我们目前正在与 AWS、Google Cloud、Microsoft Azure 和 DELL 的合作伙伴合作,将 Llama 3.2 11B、90B 添加到 Amazon SageMaker、Google Kubernetes Engine、Vertex AI Model Catalog、Azure AI Studio、DELL Enterprise Hub。容器可用后,我们将立即更新此部分,你可以订阅 Hugging Squad 以获取电子邮件更新。
其他资源
致谢
如果没有数千名社区成员的贡献,在生态系统中发布此类模型并提供支持和评估是不可能的,他们为 transformers、text-generation-inference、vllm、pytorch、LM Eval Harness 以及许多其他项目做出了贡献。感谢 VLLM 团队在测试和报告问题方面的帮助。本次发布离不开 Clémentine、Alina、Elie 和 Loubna 对 LLM 评估的支持,Nicolas Patry、Olivier Dehaene 和 Daniël de Kok 对 Text Generation Inference 的支持;Lysandre、Arthur、Pavel、Edward Beeching、Amy、Benjamin、Joao、Pablo、Raushan Turganbay、Matthew Carrigan 和 Joshua Lochner 对 transformers、transformers.js、TRL 和 PEFT 的支持;Nathan Sarrazin 和 Victor 让 Llama 3.2 在 Hugging Chat 中可用;Brigitte Tousignant 和 Florent Daudens 负责沟通;Hub 团队的 Julien、Simon、Pierric、Eliott、Lucain、Alvaro、Caleb 和 Mishig 负责 Hub 开发和发布功能。
非常感谢 Meta 团队发布 Llama 3.2,并将其提供给开放 AI 社区!
来源:Hugging Face Blog · huggingface.co




