本教程介绍如何通过来自 Hugging Face 的 文本生成推理 (TGI) 服务框架,使用 Google Kubernetes Engine (GKE) 中的图形处理器 (GPU) 来应用 Gemma 大语言模型 (LLM) 系列的开放模型。在本教程中,您将从 Hugging Face 下载 2B 和 7B 参数预训练和指令调优 Gemma 系列模型,并使用运行 TGI 的容器将它们部署到 GKE Autopilot 或 Standard 集群。
如果您在部署和应用 AI/机器学习工作负载时需要利用代管式 Kubernetes 的精细控制、可伸缩性、弹性、可移植性和成本效益,那么本指南是一个很好的起点。如果您需要统一的代管式 AI 平台来经济高效地快速构建和应用机器学习模型,我们建议您试用我们的 Vertex AI 部署解决方案。
背景
您可以通过 TGI 使用 GKE 中的 GPU 应用 Gemma,从而实现一个可直接用于生产环境的强大推理服务解决方案,具备代管式 Kubernetes 的所有优势,包括高效的可伸缩性和更高的可用性。本部分介绍本指南中使用的关键技术。
Gemma
Gemma 是一组公开提供的轻量级生成式人工智能 (AI) 模型(根据开放许可发布)。这些 AI 模型可以在应用、硬件、移动设备或托管服务中运行。
在本指南中,我们介绍了以下模型:
- 适用于文本生成的 Gemma,您也可以针对特定任务对这些模型进行调优。
- CodeGemma 是功能强大的轻量级模型集合,这些模型可以执行各种编码任务,例如填充中间代码完成、代码生成、自然语言理解、数学推理和指令遵循。
如需了解详情,请参阅 Gemma 文档。
GPU
利用 GPU,您可以加速在节点上运行的特定工作负载(例如机器学习和数据处理)。GKE 提供了一系列机器类型选项以用于节点配置,包括配备 NVIDIA H100、L4 和 A100 GPU 的机器类型。
使用 GKE 中的 GPU 之前,我们建议您完成以下学习路线:
- 了解当前 GPU 版本可用性
- 了解 GKE 中的 GPU
文本生成推理 (TGI)
TGI 是 Hugging Face 的工具包,用于部署和应用 LLM。TGI 可为热门开源 LLM(包括 Gemma)实现高性能文本生成。TGI 包含以下功能:
- 具有 Flash Attention 和 PagedAttention 且经过优化的 Transformer(转换器)实现
- 连续批处理,可提高整体服务吞吐量
- 张量并行处理,用于在多个 GPU 上加快推理速度
如需了解详情,请参阅 TGI 文档。
目标
本指南适用于使用 PyTorch 的生成式 AI 客户、GKE 的新用户或现有用户、机器学习工程师、MLOps (DevOps) 工程师或是对使用 Kubernetes 容器编排功能在 H100、A100 和 L4 GPU 硬件上应用 LLM 感兴趣的平台管理员。
阅读完本指南后,您应该能够执行以下步骤:
- 使用处于 Autopilot 模式的 GKE 集群准备环境。
- 将 TGI 部署到您的集群。
- 通过 curl 和网页聊天界面,使用 TGI 应用 Gemma 2B 或 7B 模型。
准备工作
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
启用所需的 API。
-
Make sure that you have the following role or roles on the project: roles/container.admin, roles/iam.serviceAccountAdmin
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role colunn to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
前往 IAM - 选择项目。
- 点击 授予访问权限。
-
在新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。
- 在选择角色列表中,选择一个角色。
- 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
- 点击保存。
-
- 如果您还没有 Hugging Face 账号,请创建一个。
- 确保您的项目具有足够的 GPU 配额。如需了解详情,请参阅 GPU 简介和分配配额。
获取对模型的访问权限
如需获取对 Gemma 模型的访问权限以便部署到 GKE,您必须先签署许可同意协议,然后生成 Huggging Face 访问令牌。
签署许可同意协议
您必须签署同意协议才能使用 Gemma。请按照以下说明操作:
- 访问 Kaggle.com 上的模型同意页面。
- 使用您的 Hugging Face 账号验证同意情况。
- 接受模型条款。
生成一个访问令牌
如需通过 Hugging Face 访问模型,您需要 Hugging Face 令牌。
如果您还没有令牌,请按照以下步骤生成新令牌:
- 点击您的个人资料 > 设置 > 访问令牌。
- 选择新建令牌 (New Token)。
- 指定您选择的名称和一个至少为
Read
的角色。 - 选择生成令牌。
- 将生成的令牌复制到剪贴板。
准备环境
在本教程中,您将使用 Cloud Shell 来管理 Google Cloud 上托管的资源。Cloud Shell 预安装有本教程所需的软件,包括 kubectl
和 gcloud CLI。
如需使用 Cloud Shell 设置您的环境,请按照以下步骤操作:
在 Google Cloud 控制台中,点击 Google Cloud 控制台中的 激活 Cloud Shell 以启动 Cloud Shell 会话。此操作会在 Google Cloud 控制台的底部窗格中启动会话。
设置默认环境变量:
gcloud config set project PROJECT_ID export PROJECT_ID=$(gcloud config get project) export REGION=REGION export CLUSTER_NAME=tgi export HF_TOKEN=HF_TOKEN
替换以下值:
- PROJECT_ID:您的 Google Cloud 项目 ID。
- REGION:支持要使用的加速器类型的区域,例如适用于 L4 GPU 的
us-central1
。 - HF_TOKEN:您之前生成的 Hugging Face 令牌。
创建和配置 Google Cloud 资源
请按照以下说明创建所需的资源。
创建 GKE 集群和节点池
您可以在 GKE Autopilot 或 Standard 集群中的 GPU 上应用 Gemma。我们建议您使用 Autopilot 集群获得全托管式 Kubernetes 体验。如需选择最适合您的工作负载的 GKE 操作模式,请参阅选择 GKE 操作模式。
Autopilot
在 Cloud Shell 中,运行以下命令:
gcloud container clusters create-auto ${CLUSTER_NAME} \
--project=${PROJECT_ID} \
--region=${REGION} \
--release-channel=rapid \
--cluster-version=1.28
GKE 会根据所部署的工作负载的请求,创建具有所需 CPU 和 GPU 节点的 Autopilot 集群。
标准
在 Cloud Shell 中,运行以下命令以创建 Standard 集群:
gcloud container clusters create ${CLUSTER_NAME} \ --project=${PROJECT_ID} \ --region=${REGION} \ --workload-pool=${PROJECT_ID}.svc.id.goog \ --release-channel=rapid \ --num-nodes=1
集群创建可能需要几分钟的时间。
运行以下命令来为集群创建节点池:
gcloud container node-pools create gpupool \ --accelerator type=nvidia-l4,count=2,gpu-driver-version=latest \ --project=${PROJECT_ID} \ --location=${REGION} \ --node-locations=${REGION}-a \ --cluster=${CLUSTER_NAME} \ --machine-type=g2-standard-24 \ --num-nodes=1
GKE 会创建一个节点池,其中每个节点有两个 L4 GPU。
为 Hugging Face 凭据创建 Kubernetes Secret
在 Cloud Shell 中,执行以下操作:
配置
kubectl
以与您的集群通信:gcloud container clusters get-credentials ${CLUSTER_NAME} --location=${REGION}
创建包含 Hugging Face 令牌的 Kubernetes Secret:
kubectl create secret generic hf-secret \ --from-literal=hf_api_token=${HF_TOKEN} \ --dry-run=client -o yaml | kubectl apply -f -
部署 TGI
在本部分中,您将部署 TGI 容器以应用您要使用的 Gemma 模型。如需了解指令调优和预训练模型,以及为您的应用场景选择哪个模型,请参阅调优模型。
部署指令调优 Gemma 模型
如果您想部署指令调优 Gemma 模型或变体,请按照以下说明操作。
Gemma 2B-it
请按照以下说明部署 Gemma 2B 指令调优模型。
创建以下
tgi-2b-it.yaml
清单:应用清单:
kubectl apply -f tgi-2b-it.yaml
Gemma 7B-it
请按照以下说明部署 Gemma 7B 指令调优模型。
创建以下
tgi-7b-it.yaml
清单:应用清单:
kubectl apply -f tgi-7b-it.yaml
CodeGemma 7B-it
请按照以下说明部署 CodeGemma 7B 指令调优模型。
创建以下
tgi-codegemma-1.1-7b-it.yaml
清单:应用清单:
kubectl apply -f tgi-codegemma-1.1-7b-it.yaml
部署预训练 Gemma 模型
如果您想部署预训练 Gemma 模型或变体,请按照以下说明操作。
Gemma 2B
请按照以下说明部署 Gemma 2B 预训练模型。
创建以下
tgi-2b.yaml
清单:应用清单:
kubectl apply -f tgi-2b.yaml
Gemma 7B
请按照以下说明部署 Gemma 2B 预训练模型。
创建以下
tgi-7b.yaml
清单:应用清单:
kubectl apply -f tgi-7b.yaml
CodeGemma 2B
请按照以下说明部署 CodeGemma 2B 预训练模型。
创建以下
tgi-codegemma-1.1-2b.yaml
清单:应用清单:
kubectl apply -f tgi-codegemma-1.1-2b.yaml
集群中的 Pod 会从 Hugging Face 下载模型权重并启动服务引擎。
等待部署成为可用状态:
kubectl wait --for=condition=Available --timeout=700s deployment/tgi-gemma-deployment
查看正在运行的部署的日志:
kubectl logs -f -l app=gemma-server
部署资源会下载模型数据。此过程可能需要几分钟的时间。输出类似于以下内容:
2024-05-08T20:27:37.557836Z INFO text_generation_router: router/src/main.rs:317: Warming up model
2024-05-08T20:27:39.206371Z INFO text_generation_launcher: Cuda Graphs are enabled for sizes [1, 2, 4, 8, 16, 32]
2024-05-08T20:27:40.461998Z INFO text_generation_router: router/src/main.rs:354: Setting max batch total tokens to 632992
2024-05-08T20:27:40.462018Z INFO text_generation_router: router/src/main.rs:355: Connected
2024-05-08T20:27:40.462025Z WARN text_generation_router: router/src/main.rs:369: Invalid hostname, defaulting to 0.0.0.0
确保模型已完全下载,然后再继续下一部分。
应用模型
在本部分中,您将与模型互动。
设置端口转发
运行以下命令以设置到模型的端口转发:
kubectl port-forward service/llm-service 8000:8000
输出类似于以下内容:
Forwarding from 127.0.0.1:8000 -> 8000
使用 curl 与模型互动
本部分介绍如何执行基本的冒烟测试来验证所部署的预训练或指令调优模型。为简单起见,本部分介绍使用 Gemma 2B 预训练、Gemma 2B 指令调优和 CodeGemma 7B 指令调优模型的测试方法。
Gemma 2B
在新的终端会话中,使用 curl
与模型聊天:
USER_PROMPT="Java is a"
curl -X POST http://localhost:8000/generate \
-H "Content-Type: application/json" \
-d @- <<EOF
{
"inputs": "${USER_PROMPT}",
"parameters": {
"temperature": 0.90,
"top_p": 0.95,
"max_new_tokens": 128
}
}
EOF
以下输出显示了模型响应的示例:
{"generated_text":" general-purpose, high-level, class-based, object-oriented programming language. <strong>Is Java a statically typed language?</strong> Yes, Java is a statically typed language. Java also supports dynamic typing. Static typing means that the type of every variable is explicitly specified at the time of declaration. The type can be either implicit or explicit. Static typing means that if no types are assigned then it will be assumed as a primitive type.\n\n<h3>What is Java?</h3>\n\nJava is a general-purpose, class-based, object-oriented programming language. Java is one of the oldest programming languages that has gained a"}
Gemma 2B-it
在新的终端会话中,使用 curl
与模型聊天:
USER_PROMPT="I'm new to coding. If you could only recommend one programming language to start with, what would it be and why?"
curl -X POST http://localhost:8000/generate \
-H "Content-Type: application/json" \
-d @- <<EOF
{
"inputs": "<start_of_turn>user\n${USER_PROMPT}<end_of_turn>\n",
"parameters": {
"temperature": 0.90,
"top_p": 0.95,
"max_new_tokens": 128
}
}
EOF
以下输出显示了模型响应的示例:
{"generated_text":"**Python**\n\n**Reasons why Python is a great choice for beginners:**\n\n* **Simple syntax:** Python uses clear and concise syntax, making it easy for beginners to pick up.\n* **Easy to learn:** Python's syntax is based on English, making it easier to learn than other languages.\n* **Large and supportive community:** Python has a massive and active community of developers who are constantly willing to help.\n* **Numerous libraries and tools:** Python comes with a vast collection of libraries and tools that make it easy to perform various tasks, such as data manipulation, web development, and machine learning.\n* **"}
CodeGemma
在新的终端会话中,使用 curl
与模型聊天:
USER_PROMPT="Generate a python code example of a adding two numbers from a function called addNumbers"
curl -s -X POST http://localhost:8000/generate \
-H "Content-Type: application/json" \
-d @- <<EOF | jq -r .generated_text
{
"inputs": "<start_of_turn>user\n${USER_PROMPT}<end_of_turn>\n",
"parameters": {
"temperature": 0.90,
"top_p": 0.95,
"max_new_tokens": 2000
}
}
EOF
以下输出显示了模型响应的示例:
def addNumbers(num1, num2):
sum = num1 + num2
return sum
# Get the input from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Call the addNumbers function
sum = addNumbers(num1, num2)
# Print the result
print("The sum of", num1, "and", num2, "is", sum)
(可选)通过 Gradio 聊天界面与模型互动
在本部分中,您将构建一个网页聊天应用,可让您与指令调优模型互动。为简单起见,本部分仅介绍使用 2B-it 模型的测试方法。
Gradio 是一个 Python 库,它具有一个可为聊天机器人创建界面的 ChatInterface
封装容器。
部署聊天界面
在 Cloud Shell 中,将以下清单保存为
gradio.yaml
:应用清单:
kubectl apply -f gradio.yaml
等待部署成为可用状态:
kubectl wait --for=condition=Available --timeout=300s deployment/gradio
使用聊天界面
在 Cloud Shell 中,运行以下命令:
kubectl port-forward service/gradio 8080:8080
这会创建从 Cloud Shell 到 Gradio 服务的端口转发。
点击 Cloud Shell 任务栏右上角的 网页预览按钮。点击在端口 8080 上预览。浏览器中会打开一个新的标签页。
使用 Gradio 聊天界面与 Gemma 互动。添加提示,然后点击提交。
问题排查
- 如果您收到
Empty reply from server
消息,则容器可能尚未完成模型数据下载。再次检查 Pod 的日志中是否包含Connected
消息,该消息表明模型已准备好进行应用。 - 如果您看到
Connection refused
,请验证您的端口转发已启用。
清理
为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。
删除已部署的资源
为避免因您在本指南中创建的资源导致您的 Google Cloud 账号产生费用,请运行以下命令:
gcloud container clusters delete ${CLUSTER_NAME} \
--region=${REGION}
后续步骤
- 详细了解 GKE 中的 GPU。
- 查看 GitHub 中的示例代码,了解如何在其他加速器(包括 A100 和 H100 GPU)上将 Gemma 与 TGI 搭配使用。
- 了解如何在 Autopilot 中部署 GPU 工作负载。
- 了解如何在 Standard 中部署 GPU 工作负载。
- 浏览 TGI 文档。
- 探索 Vertex AI Model Garden。
- 了解如何使用 GKE 平台编排功能运行经过优化的 AI/机器学习工作负载。