模型概述
模型特點
模型能力
使用案例
🚀 Gemma模型
Gemma是谷歌推出的輕量級、最先進的開源模型家族。它基於與Gemini模型相同的研究和技術構建,適用於多種文本生成任務,能在資源有限的環境中部署,為開發者和研究者提供強大的語言處理能力。
🚀 快速開始
本模型卡對應Gemma模型的7B指令調優版本。你也可以訪問2B基礎模型、7B基礎模型和2B指令調優模型的模型卡。
資源和技術文檔:
使用條款:條款
作者:Google
✨ 主要特性
Gemma是谷歌推出的輕量級、最先進的開源模型家族,基於與Gemini模型相同的研究和技術構建。它是僅解碼器的大語言模型,支持英文,具有開放權重、預訓練變體和指令調優變體。Gemma模型適用於多種文本生成任務,如問答、摘要和推理,因其相對較小的規模,可在資源有限的環境中部署。
📦 安裝指南
在開始使用模型之前,你需要安裝transformers
庫:
pip install -U transformers
💻 使用示例
基礎用法
以下是在CPU上運行模型的示例代碼:
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
tokenizer = AutoTokenizer.from_pretrained("google/gemma-7b-it")
model = AutoModelForCausalLM.from_pretrained(
"google/gemma-7b-it",
torch_dtype=torch.bfloat16
)
input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt")
outputs = model.generate(**input_ids)
print(tokenizer.decode(outputs[0]))
高級用法
在單GPU或多GPU上運行模型
# pip install accelerate
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
tokenizer = AutoTokenizer.from_pretrained("google/gemma-7b-it")
model = AutoModelForCausalLM.from_pretrained(
"google/gemma-7b-it",
device_map="auto",
torch_dtype=torch.bfloat16
)
input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
outputs = model.generate(**input_ids)
print(tokenizer.decode(outputs[0]))
使用不同精度在GPU上運行模型
- 使用
torch.float16
# pip install accelerate
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
tokenizer = AutoTokenizer.from_pretrained("google/gemma-7b-it")
model = AutoModelForCausalLM.from_pretrained(
"google/gemma-7b-it",
device_map="auto",
torch_dtype=torch.float16,
revision="float16",
)
input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
outputs = model.generate(**input_ids)
print(tokenizer.decode(outputs[0]))
- 使用
torch.bfloat16
# pip install accelerate
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("google/gemma-7b-it")
model = AutoModelForCausalLM.from_pretrained("google/gemma-7b-it", device_map="auto", torch_dtype=torch.bfloat16)
input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
outputs = model.generate(**input_ids)
print(tokenizer.decode(outputs[0]))
- 提升到
torch.float32
# pip install accelerate
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("google/gemma-7b-it")
model = AutoModelForCausalLM.from_pretrained(
"google/gemma-7b-it",
device_map="auto"
)
input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
outputs = model.generate(**input_ids)
print(tokenizer.decode(outputs[0]))
通過bitsandbytes
進行量化版本
- 使用8位精度(int8)
# pip install bitsandbytes accelerate
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
quantization_config = BitsAndBytesConfig(load_in_8bit=True)
tokenizer = AutoTokenizer.from_pretrained("google/gemma-7b-it")
model = AutoModelForCausalLM.from_pretrained("google/gemma-7b-it", quantization_config=quantization_config)
input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
outputs = model.generate(**input_ids)
print(tokenizer.decode(outputs[0]))
- 使用4位精度
# pip install bitsandbytes accelerate
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
quantization_config = BitsAndBytesConfig(load_in_4bit=True)
tokenizer = AutoTokenizer.from_pretrained("google/gemma-7b-it")
model = AutoModelForCausalLM.from_pretrained("google/gemma-7b-it", quantization_config=quantization_config)
input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
outputs = model.generate(**input_ids)
print(tokenizer.decode(outputs[0]))
其他優化
- Flash Attention 2
首先確保在你的環境中安裝
flash-attn
:
pip install flash-attn
然後在加載模型時添加attn_implementation="flash_attention_2"
:
model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype=torch.float16,
+ attn_implementation="flash_attention_2"
).to(0)
聊天模板
指令調優模型使用聊天模板進行對話。以下是一個應用聊天模板的示例:
from transformers import AutoTokenizer, AutoModelForCausalLM
import transformers
import torch
model_id = "google/gemma-7b-it"
dtype = torch.bfloat16
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id,
device_map="cuda",
torch_dtype=dtype,
)
chat = [
{ "role": "user", "content": "Write a hello world program" },
]
prompt = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
此時,prompt
包含以下文本:
<bos><start_of_turn>user
Write a hello world program<end_of_turn>
<start_of_turn>model
你可以手動構建符合此格式的提示。準備好提示後,進行文本生成:
inputs = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt")
outputs = model.generate(input_ids=inputs.to(model.device), max_new_tokens=150)
print(tokenizer.decode(outputs[0]))
📚 詳細文檔
輸入和輸出
- 輸入:文本字符串,如問題、提示或待摘要的文檔。
- 輸出:生成的英文文本,作為對輸入的響應,如問題的答案或文檔的摘要。
模型數據
訓練數據集
這些模型在包含多種來源的文本數據集上進行訓練,總計6萬億個標記。主要組成部分包括:
- 網頁文檔:多樣化的網頁文本集合,確保模型接觸到廣泛的語言風格、主題和詞彙,主要為英文內容。
- 代碼:讓模型接觸代碼有助於學習編程語言的語法和模式,提高生成代碼或理解代碼相關問題的能力。
- 數學:在數學文本上進行訓練有助於模型學習邏輯推理、符號表示和處理數學查詢。
數據預處理
訓練數據應用了以下關鍵的數據清理和過濾方法:
- CSAM過濾:在數據準備過程的多個階段應用嚴格的CSAM(兒童性虐待材料)過濾,確保排除有害和非法內容。
- 敏感數據過濾:使用自動化技術過濾訓練集中的某些個人信息和其他敏感數據,使Gemma預訓練模型更安全可靠。
- 其他方法:根據內容質量和安全性進行過濾,符合我們的政策。
實現信息
硬件
Gemma使用最新一代的張量處理單元(TPU)硬件(TPUv5e)進行訓練。TPU專為機器學習中的矩陣運算設計,具有性能高、內存大、可擴展性強和成本效益高等優勢。
軟件
訓練使用了JAX和ML Pathways。JAX使研究人員能夠利用最新一代的硬件進行大型模型的快速高效訓練,ML Pathways適用於基礎模型,包括此類大語言模型。
評估
基準測試結果
這些模型在多個不同的數據集和指標上進行評估,結果如下:
基準測試 | 指標 | 2B參數 | 7B參數 |
---|---|---|---|
MMLU | 5-shot, top-1 | 42.3 | 64.3 |
HellaSwag | 0-shot | 71.4 | 81.2 |
PIQA | 0-shot | 77.3 | 81.2 |
SocialIQA | 0-shot | 49.7 | 51.8 |
BooIQ | 0-shot | 69.4 | 83.2 |
WinoGrande | partial score | 65.4 | 72.3 |
CommonsenseQA | 7-shot | 65.3 | 71.3 |
OpenBookQA | 47.8 | 52.8 | |
ARC-e | 73.2 | 81.5 | |
ARC-c | 42.1 | 53.2 | |
TriviaQA | 5-shot | 53.2 | 63.4 |
Natural Questions | 5-shot | 12.5 | 23 |
HumanEval | pass@1 | 22.0 | 32.3 |
MBPP | 3-shot | 29.2 | 44.4 |
GSM8K | maj@1 | 17.7 | 46.4 |
MATH | 4-shot | 11.8 | 24.3 |
AGIEval | 24.2 | 41.7 | |
BIG-Bench | 35.2 | 55.1 | |
------------------------------ | ------------- | ----------- | --------- |
平均 | 45.0 | 56.9 |
倫理和安全
評估方法
評估方法包括結構化評估和內部紅隊測試相關內容政策。紅隊測試由多個不同團隊進行,針對多個與倫理和安全相關的類別進行評估,包括:
- 文本到文本內容安全:對涵蓋安全政策的提示進行人工評估,包括兒童性虐待和剝削、騷擾、暴力和血腥以及仇恨言論。
- 文本到文本代表性危害:與相關學術數據集進行基準測試,如WinoBias和BBQ Dataset。
- 記憶:對訓練數據的記憶進行自動化評估,包括個人身份信息暴露的風險。
- 大規模危害:測試“危險能力”,如化學、生物、放射性和核(CBRN)風險。
評估結果
倫理和安全評估結果在可接受的閾值內,符合內部政策。以下是一些知名安全基準測試的結果:
基準測試 | 指標 | 2B參數 | 7B參數 |
---|---|---|---|
RealToxicity | average | 6.86 | 7.90 |
BOLD | 45.57 | 49.08 | |
CrowS-Pairs | top-1 | 45.82 | 51.33 |
BBQ Ambig | 1-shot, top-1 | 62.58 | 92.54 |
BBQ Disambig | top-1 | 54.62 | 71.99 |
Winogender | top-1 | 51.25 | 54.17 |
TruthfulQA | 44.84 | 31.81 | |
Winobias 1_2 | 56.12 | 59.09 | |
Winobias 2_2 | 91.10 | 92.23 | |
Toxigen | 29.77 | 39.59 | |
------------------------------ | ------------- | ----------- | --------- |
使用和限制
預期用途
開放大語言模型(LLMs)在各個行業和領域有廣泛應用,包括:
- 內容創作和通信:文本生成、聊天機器人和對話式AI、文本摘要。
- 研究和教育:自然語言處理(NLP)研究、語言學習工具、知識探索。
限制
- 訓練數據:訓練數據的質量和多樣性會影響模型的能力,數據中的偏差或差距可能導致模型響應的侷限性。
- 上下文和任務複雜性:LLMs更擅長有明確提示和指令的任務,開放式或高度複雜的任務可能具有挑戰性。
- 語言歧義與細微差別:自然語言複雜,LLMs可能難以理解微妙的細微差別、諷刺或比喻語言。
- 事實準確性:LLMs根據訓練數據生成響應,但不是知識庫,可能生成不正確或過時的事實陳述。
- 常識:LLMs依賴語言的統計模式,在某些情況下可能缺乏常識推理能力。
倫理考慮和風險
大語言模型的開發引發了一些倫理問題,在創建開放模型時,我們考慮了以下方面:
- 偏差和公平性:在大規模真實世界文本數據上訓練的LLMs可能反映訓練材料中的社會文化偏差,模型經過仔細審查、輸入數據預處理和評估。
- 錯誤信息和濫用:LLMs可能被濫用於生成虛假、誤導或有害的文本,提供了負責任使用的指南。
- 透明度和問責制:本模型卡總結了模型的架構、能力、限制和評估過程,開放模型為開發者和研究者提供了分享創新的機會。
風險識別和緩解措施
- 偏差的延續:鼓勵在模型訓練、微調等過程中進行持續監控和探索去偏技術。
- 有害內容的生成:內容安全機制和指南至關重要,開發者應根據具體產品政策和應用場景實施適當的內容安全保障措施。
- 惡意使用:技術限制和開發者及最終用戶教育有助於減輕LLMs的惡意應用,提供了教育資源和報告機制。
- 隱私侵犯:模型在過濾了個人身份信息(PII)的數據上進行訓練,鼓勵開發者遵守隱私法規並採用隱私保護技術。
優勢
在發佈時,與同等規模的模型相比,該模型家族提供了高性能的開放大語言模型實現,專為負責任的AI開發而設計。根據本文檔中描述的基準評估指標,這些模型表現優於其他同等規模的開放模型替代方案。
📄 許可證
本模型的許可證為gemma
。若要在Hugging Face上訪問Gemma,你需要審查並同意Google的使用許可。請確保你已登錄Hugging Face並點擊下方按鈕,請求將立即處理。
確認許可



