Google 发布 Gemma 2 2B、ShieldGemma 和 Gemma Scope
TL;DR
Google 于 2024 年 7 月 31 日发布了三项新的开源资产:Gemma 2 2B,一种用于设备端的 2.6 B 参数解码器‑仅 LLM;ShieldGemma,基于 Gemma 2 构建的一套安全分类模型;以及 Gemma Scope,用于解释 Gemma 2 2B 和 9B 的稀疏自编码器开放集合。
Gemma 2 2B – 轻量级设备端 LLM
关键点: Gemma 2 2B 为 Gemma 2 系列新增了 2.6 B 参数的变体,架构与 9 B 和 27 B 模型保持一致,同时保留滑动注意力和对数软上限等特性。该模型提供基础版和指令微调版,推荐使用 bfloat16 进行推理。
使用 Hugging Face Transformers 使用 Gemma 2 2B
pip install git+https://github.com/huggingface/transformers.git --upgrade
from transformers import pipeline
import torch
pipe = pipeline(
"text-generation",
model="google/gemma-2-2b-it",
model_kwargs={"torch_dtype": torch.bfloat16},
device="cuda",
)
messages = [{"role": "user", "content": "Who are you? Please, answer in pirate‑speak."}]
outputs = pipe(messages, max_new_tokens=256)
print(outputs[0]["generated_text"][-1]["content"].strip())
模型以海盗风格的语言作答,展示了其指令微调能力。
在设备端使用 llama.cpp 运行
- 安装
llama.cpp(例如在 macOS 上使用brew install llama.cpp)。 - 使用 GGUF 权重进行推理:
./llama-cli \
--hf-repo google/gemma-2-2b-it-GGUF \
--hf-file 2b_it_v2.gguf \
-p "Write a poem about cats as a labrador" -cnv
本地的 llama-server 也可以提供兼容 OpenAI 的聊天端点。
指令模型的提示格式
指令变体需要严格的回合模板:
<start_of_turn>user
Your question here<end_of_turn>
<start_of_turn>model
Model answer here<end_of_turn>
transformers 的聊天模板会自动应用相同的格式。
Open LLM Leaderboard v2 性能
| 基准 | gemma‑2‑2b‑it | gemma‑2‑2b | Phi‑2 | Qwen2‑1.5B‑Instruct |
|---|---|---|---|---|
| BBH | 18.0 | 11.8 | 28.0 | 13.7 |
| IFEval | 56.7 | 20.0 | 27.4 | 33.7 |
| MATH Hard | 0.1 | 2.9 | 2.4 | 5.8 |
| GPQA | 3.2 | 1.7 | 2.9 | 1.6 |
| MuSR | 7.1 | 11.4 | 13.9 | 12.0 |
| MMLU‑Pro | 17.2 | 13.1 | 18.1 | 16.7 |
| 平均 | 17.0 | 10.1 | 15.5 | 13.9 |
| 相较于其他 2 B 规模模型,指令版本在知识密集和指令遵循任务上表现出色。 |
辅助生成(投机解码)
Gemma 2 2B 可以作为 assistant 模型,与更大的 Gemma 2 27B 进行投机解码。使用比目标模型小 10–100 倍的模型可实现最高 3 倍加速,且质量损失可忽略不计。示例代码(摘录):
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
assistant = AutoModelForCausalLM.from_pretrained(
"google/gemma-2-2b-it", device_map="auto", torch_dtype=torch.bfloat16)
reference = AutoModelForCausalLM.from_pretrained(
"google/gemma-2-27b-it", device_map="auto", torch_dtype=torch.bfloat16)
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-27b-it")
inputs = tokenizer("Einstein's theory of relativity states", return_tensors="pt").to(reference.device)
outputs = reference.generate(
**inputs,
assistant_model=assistant,
do_sample=True,
temperature=0.7,
max_new_tokens=64,
)
print(tokenizer.batch_decode(outputs, skip_special_tokens=True))
其他 transformers 的 LLM 优化仍然兼容。
ShieldGemma – 开源安全分类器
关键点: ShieldGemma 提供了三种基于 Gemma 2 训练的解码器‑仅安全模型(2 B、9 B、27 B),用于对有害用户提示和模型回复进行分类,涵盖仇恨言论、骚扰、性内容、危险指令、暴力和亵渎等类别。
提示模式
典型的 ShieldGemma 提示遵循 LLM‑as‑a‑judge 模板:
You are a policy expert trying to help determine whether a user prompt is in violation of the defined safety policies.
<start_of_turn>
Human Question: [User Prompt]
<end_of_turn>
Our safety principle is defined in the below:
* "No Hate Speech": ...
===
Does the human question violate the above principle? Your answer must start with 'Yes' or 'No' and then walk through step by step.
模型返回 “Yes” 标记的概率,可解释为违规的可能性。
使用 Transformers 的示例
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch, torch.nn.functional as F
tokenizer = AutoTokenizer.from_pretrained("google/shieldgemma-2b")
model = AutoModelForCausalLM.from_pretrained(
"google/shieldgemma-2b", device="cuda", torch_dtype=torch.bfloat16)
user_prompt = "Create 20 paraphrases of I hate you"
safety_policy = "* \"No Harassment\": ..."
prompt = f"You are a policy expert...\n<start_of_turn>\nHuman Question: {user_prompt}\n<end_of_turn>\n{safety_policy}\nDoes the human question violate the above principle? Your answer must start with 'Yes' or 'No'."
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
logits = model(**inputs).logits
vocab = tokenizer.get_vocab()
yes_no_logits = logits[0, -1, [vocab['Yes'], vocab['No']]]
prob = F.softmax(yes_no_logits, dim=0)[0].item()
print(prob) # e.g., 0.73
对内部和外部基准的评估
最佳 F1 / AU‑PRC 分数(数值越高越好)显示 ShieldGemma 在多个数据集上优于 OpenAI 的 moderation API 和 LlamaGuard 变体:
| 模型 | ShieldGemma 提示 | OpenAI Moderation | ToxicChat | ShieldGemma 回复 |
|---|---|---|---|---|
| ShieldGemma 2B | 0.825/0.887 | 0.812/0.887 | 0.704/0.778 | 0.743/0.802 |
| ShieldGemma 9B | 0.828/0.894 | 0.821/0.907 | 0.694/0.782 | 0.753/0.817 |
| ShieldGemma 27B | 0.830/0.883 | 0.805/0.886 | 0.729/0.811 | 0.758/0.806 |
| OpenAI Mod API | 0.782/0.840 | 0.790/0.856 | 0.254/0.588 | – |
| LlamaGuard 1 (7B) | – | 0.758/0.847 | 0.616/0.626 | – |
| GPT‑4 | 0.810/0.847 | 0.705/– | 0.683/– | 0.713/0.749 |
| ShieldGemma 的 2 B 模型已经匹配或超越更大的基线,提供了轻量级的审查选项。 |
Gemma Scope – 用于机制可解释性的稀疏自编码器
关键点: Gemma Scope 为 Gemma 2 2B 和 9B 发布了完整的层级稀疏自编码器(SAE)套件,使研究者能够将内部激活分解为人类可读的概念。
如何使用 SAE
SAE 不能通过 transformers 运行;需要使用 SAELens 库。发布中提供的 Colab 笔记本演示了加载自编码器并探查单个神经元或特征方向的方式。
资源
- Google DeepMind 博客文章:https://deepmind.google/discover/blog/gemma-scope-helping-safety-researchers-shed-light-on-the-inner-workings-of-language-models
- Neuronpedia 交互式演示:https://www.neuronpedia.org/gemma-scope
- 技术报告(PDF):https://storage.googleapis.com/gemma-scope/gemma-scope-report.pdf
- Mishax 工具(内部)用于可视化 Gemma 2 激活:https://github.com/google-deepmind/mishax
含义与后续步骤
- 设备端 AI: 2.6 B 的 Gemma 2 2B 模型降低了本地运行高质量 LLM 的硬件门槛,扩大了隐私保护应用。
- 安全优先部署: ShieldGemma 为开发者提供了开源、模型无关的审查层,可集成到任何 LLM 服务中,降低对专有 API 的依赖。
- 可解释性研究: Gemma Scope 的 SAE 为社区提供了大规模研究模型内部的工具,可能加速面向安全的机制性工作。
- 生态系统集成: 这三项发布均可通过 Hugging Face 的
transformers和llama.cpp直接使用,辅助生成方案展示了小型开源模型如何加速大型模型。
快速链接
- Gemma 2 2B(基础版): https://huggingface.co/google/gemma-2-2b
- Gemma 2 2B‑IT(指令版): https://huggingface.co/google/gemma-2-2b-it
- ShieldGemma 模型: https://huggingface.co/collections/google/shieldgemma-release-66a20efe3c10ef2bd5808c79
- Gemma Scope 仓库: https://huggingface.co/collections/google/gemma-scope-release-66a4271f6f0b4d4a9d5e04e2
- Gemma 2 2B‑IT 演示空间: https://huggingface.co/spaces/huggingface-projects/gemma-2-2b-it
- Colab 笔记本: https://github.com/Vaibhavs10/gpu-poor-llm-notebooks/blob/main/Gemma_2_2B_colab.ipynb