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 模型的架構相同,同時保留滑動注意力與 logits 軟上限等特性。該模型提供基礎版與指令微調版,建議使用 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 執行

  1. 安裝 llama.cpp(例如在 macOS 上使用 brew install llama.cpp)。
  2. 使用 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 可作為較大型 Gemma 2 27B 的 assistant 模型,用於推測解碼。使用比目標模型小 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 的審核 API 與 LlamaGuard 變體:

模型 SG Prompt OpenAI Mod ToxicChat SG Response
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 發布了完整的層級稀疏自編碼器(SAE)套件,適用於 Gemma 2 2B 與 9B,讓研究人員能將內部激活分解為人類可讀的概念。

如何使用 SAE

SAE 不能透過 transformers 執行;必須使用 SAELens 函式庫。發佈中提供的 Colab 筆記本示範了載入自編碼器並探測單一神經元或特徵方向。

資源


影響與未來步驟

  • 裝置端 AI: 2.6 B 的 Gemma 2 2B 模型降低了在本地執行高品質 LLM 的硬體門檻,擴大了隱私保護的應用。
  • 安全優先部署: ShieldGemma 為開發者提供開源、與模型無關的審核層,可整合至任何 LLM 服務,減少對專有 API 的依賴。
  • 可解釋性研究: Gemma Scope 的 SAE 為社群提供大規模研究模型內部的工具,可能加速以安全為導向的機制研究。
  • 生態系統整合: 這三項發佈皆可直接透過 Hugging Face 的 transformersllama.cpp 使用,且輔助生成的範例展示了較小的開源模型如何加速較大的模型。

快速連結

Sources