在此教程中,您将使用 Model Garden 将 Gemma 2B 开放模型部署到受 TPU 支持的 Vertex AI 端点。您必须先将模型部署到端点,然后才能使用该模型执行在线预测。部署模型会将物理资源与模型相关联,以便以低延迟方式执行在线预测。
部署 Gemma 2B 模型后,您可以使用 PredictionServiceClient
获取在线预测结果,以通过经过训练的模型进行推理。在线预测是指向部署到端点的模型发出的同步请求。
目标
本教程介绍了如何执行以下任务:
- 使用 Model Garden 将 Gemma 2B 开放模型部署到受 TPU 支持的端点
- 使用
PredictionServiceClient
获取在线预测结果
费用
在本文档中,您将使用 Google Cloud的以下收费组件:
-
A
ct5lp-hightpu-1t
machine type with one TPU_V5 accelerator - Vertex AI prediction and explanation
如需根据您的预计使用量来估算费用,请使用价格计算器。
完成本文档中描述的任务后,您可以通过删除所创建的资源来避免继续计费。如需了解详情,请参阅清理。
准备工作
本教程要求您执行以下操作:
- 设置 Google Cloud 项目并启用 Vertex AI API
- 在您的本地机器上:
- 安装、初始化 Google Cloud CLI 并向其进行身份验证
- 安装相应编程语言的 SDK
设置 Google Cloud 项目
设置 Google Cloud 项目并启用 Vertex AI API。
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. -
安装并初始化 Google Cloud CLI。
-
如果您之前安装了 gcloud CLI,请确保通过运行此命令更新您的
gcloud
组件。gcloud components update
-
如要向 gcloud CLI 进行身份验证,请通过运行此命令生成本地应用默认凭证 (ADC) 文件。该命令启动的 Web 流程用于提供您的用户凭证。
gcloud auth application-default login
如需了解详情,请参阅 gcloud CLI 身份验证配置和 ADC 配置。
运行以下命令,安装并更新 Vertex AI SDK for Python。
pip3 install --upgrade "google-cloud-aiplatform>=1.64"
运行以下命令,安装 Python 版协议缓冲区库。
pip3 install --upgrade "protobuf>=5.28"
在 Google Cloud 控制台中,前往 Model Garden 页面。
点击 Gemma 2 模型卡片。
点击部署以打开部署模型窗格。
在部署模型窗格中,指定以下详细信息。
在部署环境部分,点击 Vertex AI。
在部署模型部分:
在资源 ID 部分,选择
gemma-2b-it
。对于模型名称和端点名称,接受默认值即可。例如:
- 模型名称:
gemma2-2b-it-1234567891234
- 端点名称:
gemma2-2b-it-mg-one-click-deploy
记下端点名称。您需要用该名称来查找代码示例中使用的端点 ID。
- 模型名称:
在部署设置部分:
对于基本设置,接受默认选项即可。
在区域部分,接受默认值或从列表中选择一个区域。记下相应区域。代码示例需要用到该区域。
在机器规格部分,选择受 TPU 支持的实例:
ct5lp-hightpu-1t (1 TPU_V5_LITEPOD; ct5lp-hightpu-1t)
。
点击部署。部署完成后,您会收到一封邮件,其中包含有关新端点的详细信息。您也可以通过依次点击在线预测 > 端点并选择相应区域,来查看端点详细信息。
PROJECT_ID
:如需查找项目 ID,请按以下步骤操作。前往 Google Cloud 控制台中的欢迎页面。
从页面顶部的项目选择器中,选择您的项目。
项目名称、项目编号和项目 ID 会显示在欢迎标头后面。
ENDPOINT_REGION
:这是您在其中部署端点的区域。ENDPOINT_ID
:如要查找端点 ID,您可以在控制台中查看,或者运行gcloud ai endpoints list
命令。您需要记下部署模型窗格中的端点名称和区域。控制台
您可以通过依次点击在线预测 > 端点并选择相应区域,来查看端点详细信息。请注意
ID
列中显示的数字。gcloud
您可以运行
gcloud ai endpoints list
命令来查看端点详细信息。gcloud ai endpoints list \ --region=ENDPOINT_REGION \ --filter=display_name=ENDPOINT_NAME
输出类似于以下内容。
Using endpoint [https://us-central1-aiplatform.googleapis.com/] ENDPOINT_ID: 1234567891234567891 DISPLAY_NAME: gemma2-2b-it-mg-one-click-deploy
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
- 取消部署模型并删除端点
- 从 Model Registry 中删除模型
- 将 PROJECT_ID 替换为您的项目名称
- 将 LOCATION_ID 替换为您在其中部署了模型和端点的区域
- 将 ENDPOINT_ID 替换为端点 ID
- 将 DEPLOYED_MODEL_NAME 替换为模型的显示名称
- 将 DEPLOYED_MODEL_ID 替换为模型 ID
运行
gcloud ai endpoints list
命令获取端点 ID。此命令会列出项目中所有端点的 ID。记下本教程中使用的端点的 ID。gcloud ai endpoints list \ --project=PROJECT_ID \ --region=LOCATION_ID
输出类似于以下内容。在输出中,该 ID 称为
ENDPOINT_ID
。Using endpoint [https://us-central1-aiplatform.googleapis.com/] ENDPOINT_ID: 1234567891234567891 DISPLAY_NAME: gemma2-2b-it-mg-one-click-deploy
运行
gcloud ai models describe
命令获取模型 ID。记下本教程中部署的模型的 ID。gcloud ai models describe DEPLOYED_MODEL_NAME \ --project=PROJECT_ID \ --region=LOCATION_ID
简略版输出如下所示。在输出中,该 ID 称为
deployedModelId
。Using endpoint [https://us-central1-aiplatform.googleapis.com/] artifactUri: [URI removed] baseModelSource: modelGardenSource: publicModelName: publishers/google/models/gemma2 ... deployedModels: - deployedModelId: '1234567891234567891' endpoint: projects/12345678912/locations/us-central1/endpoints/12345678912345 displayName: gemma2-2b-it-12345678912345 etag: [ETag removed] modelSourceInfo: sourceType: MODEL_GARDEN name: projects/123456789123/locations/us-central1/models/gemma2-2b-it-12345678912345 ...
从端点取消部署模型。您需要用到通过运行上述命令获得的端点 ID 和模型 ID。
gcloud ai endpoints undeploy-model ENDPOINT_ID \ --project=PROJECT_ID \ --region=LOCATION_ID \ --deployed-model-id=DEPLOYED_MODEL_ID
此命令没有任何输出。
运行
gcloud ai endpoints delete
命令以删除端点。gcloud ai endpoints delete ENDPOINT_ID \ --project=PROJECT_ID \ --region=LOCATION_ID
出现提示时,输入
y
以确认。此命令没有任何输出。在 Google Cloud 控制台中,从 Vertex AI 部分进入 Model Registry 页面。
在区域下拉列表中,选择您在其中部署了模型的区域。
在
gemma2-2b-it-1234567891234
行末尾,点击 操作。选择删除模型。
删除模型时,所有关联的模型版本和评估都会从 Google Cloud 项目中一并删除。
在确认提示中,点击删除。
- 详细了解 Gemma 开放模型。
- 阅读 Gemma 使用条款。
- 详细了解开放模型。
- 了解如何部署调优后的模型。
- 了解如何使用 HuggingFace Textgen Inference (TGI) 将 Gemma 2 部署到 Google Kubernetes Engine。
- 详细了解您偏好的编程语言(Python、Node.js、Java 或 Go)版的
PredictionServiceClient
。
设置 Google Cloud CLI
在您的本地机器上,设置 Google Cloud CLI。
设置相应编程语言的 SDK
如需设置本教程中使用的环境,请安装相应编程语言的 Vertex AI SDK 及协议缓冲区库。代码示例会使用协议缓冲区库中的函数将输入字典转换为 API 所需的 JSON 格式。
在您的本地机器上,点击以下标签页之一,安装相应编程语言的 SDK。
Python
在您的本地机器上,点击以下标签页之一,安装相应编程语言的 SDK。
Node.js
运行以下命令,安装或更新 Node.js 版 aiplatform
SDK。
npm install @google-cloud/aiplatform
Java
如需将 google-cloud-aiplatform
添加为依赖项,请为您的环境添加相应的代码。
带有 BOM 的 Maven
将以下 HTML 添加到 pom.xml
中:
<dependencyManagement> <dependencies> <dependency> <artifactId>libraries-bom</artifactId> <groupId>com.google.cloud</groupId> <scope>import</scope> <type>pom</type> <version>26.34.0</version> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>com.google.cloud</groupId> <artifactId>google-cloud-aiplatform</artifactId> </dependency> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java-util</artifactId> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency> </dependencies>
不带 BOM 的 Maven
将以下内容添加到 pom.xml
中:
<dependency> <groupId>com.google.cloud</groupId> <artifactId>google-cloud-aiplatform</artifactId> <version>1.1.0</version> </dependency> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java-util</artifactId> <version>5.28</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.11.0</version> </dependency>
不带 BOM 的 Gradle
将以下内容添加到 build.gradle
中:
implementation 'com.google.cloud:google-cloud-aiplatform:1.1.0'
Go
运行以下命令,安装下面的 Go 软件包。
go get cloud.google.com/go/aiplatform go get google.golang.org/protobuf go get github.com/googleapis/gax-go/v2
使用 Model Garden 部署 Gemma
将 Gemma 2B 模型部署到针对中小规模训练优化的 ct5lp-hightpu-1t
Compute Engine 机器类型。该类型的机器有一个 TPU v5e 加速器。如需详细了解如何使用 TPU 训练模型,请参阅 Cloud TPU v5e 训练。
在本教程中,您将使用 Model Garden 中的模型卡片部署指令调优的 Gemma 2B 开放模型。具体模型版本为 gemma2-2b-it
- -it
表示指令调优。
Gemma 2B 模型的参数规模较小,这意味着对资源的要求较低,同时能够提供较高的部署灵活性。
使用 PredictionServiceClient 推断 Gemma 2B
部署 Gemma 2B 后,您可以使用 PredictionServiceClient
获取以下提示的在线预测结果:“为什么天空是蓝色的?”。
代码参数
PredictionServiceClient
代码示例需要您更新以下内容。
示例代码
在相应编程语言的示例代码中,更新 PROJECT_ID
、ENDPOINT_REGION
和 ENDPOINT_ID
。然后运行代码。
Python
如需了解如何安装或更新 Vertex AI SDK for Python,请参阅安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python API 参考文档。
Node.js
在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Node.js 设置说明执行操作。 如需了解详情,请参阅 Vertex AI Node.js API 参考文档。
如需向 Vertex AI 进行身份验证,请设置应用默认凭证。 如需了解详情,请参阅为本地开发环境设置身份验证。
Java
在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Java 设置说明执行操作。 如需了解详情,请参阅 Vertex AI Java API 参考文档。
如需向 Vertex AI 进行身份验证,请设置应用默认凭证。 如需了解详情,请参阅为本地开发环境设置身份验证。
Go
在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Go 设置说明执行操作。 如需了解详情,请参阅 Vertex AI Go API 参考文档。
如需向 Vertex AI 进行身份验证,请设置应用默认凭证。 如需了解详情,请参阅为本地开发环境设置身份验证。
清理
为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。
删除项目
删除各个资源
如果您想保留项目,则删除本教程中使用的资源即可:
取消部署模型并删除端点
请使用以下方法之一取消部署模型并删除端点。
控制台
gcloud
如需使用 Google Cloud CLI 取消部署模型并删除端点,请按以下步骤操作。
在这些命令中,替换以下内容:
删除模型
控制台
gcloud
如需使用 Google Cloud CLI 删除模型,请向 gcloud ai models delete
命令提供模型的显示名称和区域。
gcloud ai models delete DEPLOYED_MODEL_NAME \
--project=PROJECT_ID \
--region=LOCATION_ID
将 DEPLOYED_MODEL_NAME 替换为模型的显示名称。将 PROJECT_ID 替换为您的项目名称。将 LOCATION_ID 替换为您在其中部署了模型的区域。