GoogleがGemma 2 2B、ShieldGemma、Gemma Scopeをリリース
TL;DR
Googleは2024年7月31日に3つの新しいオープンソース資産をリリースしました:Gemma 2 2B(デバイス上で使用できる2.6 Bパラメータのデコーダー専用LLM)、ShieldGemma(Gemma 2上に構築された安全分類モデルのスイート)、そしてGemma Scope(Gemma 2 2Bと9Bを解釈するためのスパースオートエンコーダーのオープンコレクション)。
Gemma 2 2B – 軽量でデバイス上で動作するLLM
Key point: 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は、より大きな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 – オープンソース安全分類器
Key point: ShieldGemmaはGemma 2上で訓練された3つのデコーダー専用安全モデル(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 プロンプト | OpenAI Mod | ToxicChat | SG 応答 |
|---|---|---|---|---|
| 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 – メカニズム的解釈性のためのスパースオートエンコーダー
Key point: Gemma ScopeはGemma 2 2Bと9B向けの層別スパースオートエンコーダー(SAE)全套装をリリースし、研究者が内部活性化を人間が読める概念に分解できるようにします。
SAEsの使用方法
SAEsは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はコミュニティにスケールでモデル内部を研究するツールを提供し、安全志向のメカニズム作業を加速させる可能性があります。
- エコシステム統合: 3つのリリースはすべて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