跳到正文
Hugging Face Blog·· 2024-02-21精选AI 评分78

Google 发布开源大模型 Gemma,Hugging Face 全面接入

Welcome Gemma - Google’s new open LLM

AI 导读

Google 发布开源大模型系列 Gemma,包含 2B 和 7B 两个尺寸,各有基础版和指令微调版,上下文长度 8K tokens,可运行在消费级硬件上。Hugging Face 宣布全面支持,提供 Transformers 集成、Google Cloud 与 Inference Endpoints 部署,以及用 TRL 在单卡上微调的示例。

推荐理由

Google 开源 Gemma 系列并同步接入 Hugging Face 生态,读者可了解其规格、许可与部署路径。

正文 · AI 翻译
Gemma 模型的更新版本在此文章发布两个月后推出,请查看最新版本 在此集合中。

今天,Google 发布了一个全新的开源 LLM 系列——Gemma!很高兴看到 Google 加强其对开源 AI 的承诺,我们也很兴奋能够通过 Hugging Face 的全面集成来全力支持这一发布。

Gemma 提供两种规模:7B 参数版本,适用于在消费级 GPU 和 TPU 上进行高效部署和开发;以及 2B 版本,适用于 CPU 和设备端应用。两者均提供基础版和指令微调版。

我们与 Google 合作,确保与 Hugging Face 生态系统的最佳集成。您可以在 Hub 上找到这 4 个开放访问模型(2 个基础模型和 2 个微调模型)。在发布的功能和集成中,我们有:

目录

什么是 Gemma?

Gemma 是 Google 基于 Gemini 推出的 4 个全新 LLM 模型系列。它提供两种规模:2B 和 7B 参数,每种都有基础版(预训练)和指令微调版。所有变体都可以在各种类型的消费级硬件上运行,甚至无需量化,并且上下文长度为 8K tokens:

在最初发布一个月后,Google 发布了指令模型的新版本。这个版本具有更好的编码能力、事实性、指令遵循和多轮对话质量。该模型也不太容易以“Sure,”开头。

Gemma logo

那么,Gemma 模型有多好?以下是基础模型的概述及其在 LLM 排行榜 上与其他开放模型的性能对比(分数越高越好):

模型 许可证 商业使用? 预训练规模 [tokens] 排行榜分数 ⬇️
LLama 2 70B Chat(参考) Llama 2 许可证 ✅ 2T 67.87
Gemma-7B Gemma 许可证 ✅ 6T 63.75
DeciLM-7B Apache 2.0 ✅ 未知 61.55
PHI-2 (2.7B) MIT ✅ 1.4T 61.33
Mistral-7B-v0.1 Apache 2.0 ✅ 未知 60.97
Llama 2 7B Llama 2 许可证 ✅ 2T 54.32
Gemma 2B Gemma 许可证 ✅ 2T 46.51

Gemma 7B 是一个非常强大的模型,其性能可与 7B 权重中最好的模型相媲美,包括 Mistral 7B。Gemma 2B 就其规模而言是一个有趣的模型,但在排行榜上的得分不如同等规模中最强大的模型,例如 Phi 2。我们期待收到社区关于实际使用的反馈!

请注意,LLM Leaderboard 特别适用于衡量预训练模型的质量,而不太适用于聊天模型。我们鼓励对聊天模型运行其他基准测试,如 MT Bench、EQ Bench 和 lmsys Arena!

提示格式

基础模型没有提示格式。与其他基础模型一样,它们可以用于以合理的延续继续输入序列,或用于零样本/少样本推理。它们也是在您自己的用例上进行微调的绝佳基础。Instruct 版本具有非常简单的对话结构:

<start_of_turn>user
knock knock<end_of_turn>
<start_of_turn>model
who is there<end_of_turn>
<start_of_turn>user
LaMDA<end_of_turn>
<start_of_turn>model
LaMDA who?<end_of_turn>

为了有效使用,必须完全重现此格式。稍后我们将展示如何使用 transformers 中可用的聊天模板轻松重现 instruct 提示。

探索未知

技术报告包含了基础模型的训练和评估过程信息,但没有关于数据集组成和预处理的详细细节。我们知道它们是用来自各种来源的数据训练的,主要是网络文档、代码和数学文本。数据经过过滤以删除 CSAM 内容和 PII,以及进行许可检查。

同样,对于 Gemma instruct 模型,没有分享关于微调数据集或与 SFT 和 RLHF 相关的超参数的细节。

演示

您可以在 Hugging Chat 上与 Gemma Instruct 模型聊天!请查看此链接:https://huggingface.co/chat/models/google/gemma-1.1-7b-it

使用 🤗 Transformers

使用 Transformers 4.38 版本,您可以使用 Gemma 并利用 Hugging Face 生态系统中的所有工具,例如:

  • 训练和推理脚本及示例
  • 安全文件格式(safetensors)
  • 与 bitsandbytes(4 位量化)、PEFT(参数高效微调)和 Flash Attention 2 等工具的集成
  • 用于运行模型生成的实用程序和辅助工具
  • 用于导出模型以进行部署的机制

此外,Gemma 模型与 torch.compile() 兼容,支持 CUDA 图,在推理时提供约 4 倍加速!

要使用 transformers 与 Gemma 模型,请确保安装最新版本的 transformers:

pip install --upgrade transformers

以下代码片段展示了如何使用 gemma-7b-it 与 transformers。它需要约 18 GB 的 RAM,这包括 3090 或 4090 等消费级 GPU。

from transformers import pipeline
import torch

pipe = pipeline(
    "text-generation",
    model="google/gemma-7b-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,
    do_sample=True,
    temperature=0.7,
    top_k=50,
    top_p=0.95
)
assistant_response = outputs[0]["generated_text"][-1]["content"]
print(assistant_response)

Avast me, me hearty. I am a pirate of the high seas, ready to pillage and plunder. Prepare for a tale of adventure and booty!

我们使用了 bfloat16,因为那是参考精度,并且所有评估都是以此方式运行的。在 float16 中运行可能在您的硬件上更快。

您还可以自动量化模型,以 8 位甚至 4 位模式加载它。4 位加载需要约 9 GB 内存来运行,使其与许多消费级显卡和 Google Colab 中的所有 GPU 兼容。以下是如何以 4 位加载生成管道:

pipeline = pipeline(
    "text-generation",
    model=model,
    model_kwargs={
        "torch_dtype": torch.float16,
        "quantization_config": {"load_in_4bit": True}
    },
)

有关使用 transformers 与模型的更多细节,请查看模型卡片。

JAX 权重

所有 Gemma 模型变体都可用于 PyTorch,如上所述,或 JAX / Flax。要加载 Flax 权重,您需要使用仓库中的 flax 修订版,如下所示:

import jax.numpy as jnp
from transformers import AutoTokenizer, FlaxGemmaForCausalLM

model_id = "google/gemma-2b"

tokenizer = AutoTokenizer.from_pretrained(model_id)
tokenizer.padding_side = "left"

model, params = FlaxGemmaForCausalLM.from_pretrained(
        model_id,
        dtype=jnp.bfloat16,
        revision="flax",
        _do_init=False,
)

inputs = tokenizer("Valencia and Málaga are", return_tensors="np", padding=True)
output = model.generate(**inputs, params=params, max_new_tokens=20, do_sample=False)
output_text = tokenizer.batch_decode(output.sequences, skip_special_tokens=True)

['Valencia and Málaga are two of the most popular tourist destinations in Spain. Both cities boast a rich history, vibrant culture,']

请查看此笔记本,以获取关于如何在 Colab TPU 上并行化 JAX 推理的全面实践指南!

与 Google Cloud 集成

您可以通过 Vertex AI 或 Google Kubernetes Engine (GKE) 在 Google Cloud 上部署和训练 Gemma,使用文本生成推理和 Transformers。

要从 Hugging Face 部署 Gemma 模型,请前往 模型页面 并点击 Deploy -> Google Cloud。 这将带你进入 Google Cloud Console,你可以在那里一键将 Gemma 部署到 Vertex AI 或 GKE 上。Text Generation Inference 为 Google Cloud 上的 Gemma 提供支持,并且是我们与 Google Cloud 合作中的首个集成。

deploy on GCP

你也可以直接通过 Vertex AI Model Garden 访问 Gemma。

要从 Hugging Face 微调 Gemma 模型,请前往 模型页面 并点击 Train -> Google Cloud。 这将带你进入 Google Cloud Console,你可以在那里访问 notebook,在 Vertex AI 或 GKE 上微调 Gemma。

train on GCP

这些集成标志着我们作为与 Google 合作伙关系成果共同推出的首批产品。敬请期待更多内容!

与 Inference Endpoints 集成

你可以在 Hugging Face 的 Inference Endpoints 上部署 Gemma,它使用 Text Generation Inference 作为后端。Text Generation Inference 是 Hugging Face 开发的生产级推理容器,可轻松部署大型语言模型。它具有连续批处理、token 流式传输、用于多 GPU 快速推理的张量并行,以及生产级日志记录和追踪等功能。

要部署 Gemma 模型,请前往 模型页面 并点击 Deploy -> Inference Endpoints 组件。你可以在之前的博客文章中了解更多关于使用 Hugging Face Inference Endpoints 部署 LLM 的信息。Inference Endpoints 通过 Text Generation Inference 支持 Messages API,这让你只需更改 URL 即可从另一个闭源模型切换到开源模型。

from openai import OpenAI

# initialize the client but point it to TGI
client = OpenAI(
    base_url="<ENDPOINT_URL>" + "/v1/",  # replace with your endpoint url
    api_key="<HF_API_TOKEN>",  # replace with your token
)
chat_completion = client.chat.completions.create(
    model="tgi",
    messages=[
        {"role": "user", "content": "Why is open-source software important?"},
    ],
    stream=True,
    max_tokens=500
)

# iterate and print stream
for message in chat_completion:
    print(message.choices[0].delta.content, end="")

使用 🤗 TRL 微调

训练 LLM 在技术和计算上都具有挑战性。在本节中,我们将介绍 Hugging Face 生态系统中可用的工具,以便在消费级 GPU 上高效地训练 Gemma

下面是一个在 OpenAssistant 的聊天数据集上微调 Gemma 的示例命令。我们使用 4 位量化和 QLoRA 来节省内存,以针对所有注意力块的线性层。

首先,安装 nightly 版本的 🤗 TRL 并克隆仓库以获取训练脚本:

pip install -U transformers trl peft bitsandbytes
git clone https://github.com/huggingface/trl
cd trl

然后你可以运行该脚本:

accelerate launch --config_file examples/accelerate_configs/multi_gpu.yaml --num_processes=1 \
    trl/scripts/sft.py \
    --model_name google/gemma-7b \
    --dataset_name OpenAssistant/oasst_top1_2023-08-25 \
    --per_device_train_batch_size 2 \
    --gradient_accumulation_steps 1 \
    --learning_rate 2e-4 \
    --save_steps 20_000 \
    --use_peft \
    --lora_r 16 --lora_alpha 32 \
    --lora_target_modules q_proj k_proj v_proj o_proj \
    --load_in_4bit \
    --output_dir gemma-finetuned-openassistant

在单个 A10G 上训练大约需要 9 小时,但可以通过将 --num_processes 调整为你可用的 GPU 数量来轻松并行化。

其他资源

致谢

在生态系统中发布此类模型并提供支持和评估,离不开许多社区成员的贡献,包括 Clémentine 和 Eleuther Evaluation Harness 在 LLM 评估方面的贡献;Olivier 和 David 在 Text Generation Inference 支持方面的贡献;Simon 在 Hugging Face 上开发新的访问控制功能;Arthur、Younes 和 Sanchit 将 Gemma 集成到 transformers 中;Morgan 将 Gemma 集成到 optimum-nvidia(即将推出);Nathan、Victor 和 Mishig 让 Gemma 在 Hugging Chat 中可用。

并感谢 Google 团队发布 Gemma 并将其提供给开源 AI 社区!

来源:Hugging Face Blog · huggingface.co