本页面介绍如何在 Colab Enterprise 中创建运行时模板。
如需在笔记本中运行代码,请使用名为运行时的计算资源。您可以使用默认运行时或根据运行时模板创建的运行时。通过创建运行时模板,您可以配置模板以根据需要优化运行时的性能、费用和其他特性。
详细了解运行时和运行时模板。
准备工作
- 登录您的 Google Cloud 账号。 如果您是新手,请创建一个账号来评估我们的产品在实际场景中的表现。 Google Cloud新客户还可获享 $300 赠金,用于 运行、测试和部署工作负载。
-
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 role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Vertex AI, Dataform, and Compute Engine APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. 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 role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Vertex AI, Dataform, and Compute Engine APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
所需的角色
如需获得在 Colab Enterprise 中创建运行时模板所需的权限,请让您的管理员为您授予项目的 Colab Enterprise Admin (roles/aiplatform.colabEnterpriseAdmin) IAM 角色。如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
创建运行时模板
如需创建运行时模板,您可以使用 Google Cloud 控制台、 Google Cloud CLI、REST API 或 Terraform。
控制台
如需创建运行时模板,请执行以下操作:
-
在 Google Cloud 控制台中,前往 Colab Enterprise 运行时模板 页面。
-
点击 新建模板。
此时会显示创建新的运行时模板对话框。
运行时基础知识
-
在运行时基本信息部分中,输入显示名称。
-
在区域菜单中,选择需要运行时模板的区域。
-
可选:添加运行时模板的说明。
-
可选:如需添加标签,请点击 添加标签,然后输入键和值对。 如需添加更多标签,请重复此步骤。
-
点击继续。
配置计算
-
在配置计算部分的机器类型菜单中,选择机器类型。如需了解机器类型,请参阅机器系列资源和比较指南。
如果您选择的是带有 GPU 的机器类型,请选择加速器类型和加速器数量。如果您无法选择所需的 GPU 数量,可能需要增加配额。请参阅 申请配额调整。
-
在数据磁盘类型菜单中,选择磁盘类型。
-
在数据磁盘大小字段中,输入大小(以 GB 为单位)。
-
在空闲机器关停部分中:
-
如需关闭空闲机器关停,请清除启用空闲机器关停。
-
如需更改非活跃时间段,请在关停前保持非活跃状态的时间(分钟)部分中,将数字更改为所需的非活跃分钟数。在 Google Cloud 控制台中,您可以将此设置设为 10 到 1440 之间的任何整数值。
-
-
点击继续。
环境
-
在环境 部分中,选择一个环境 。 默认值为最新 (目前为 Python 3.12)。
-
可选:在 启动后脚本 字段中,输入 启动后脚本的 URI。如需详细了解如何使用启动后脚本,请参阅 使用启动后脚本。
-
可选:在启动后脚本网址下,选择启动后脚本的行为。默认行为是运行一次。如需了解详情,请参阅 启动后脚本行为。
-
可选:如需添加环境变量,请点击 添加环境变量, 然后输入键和值对。 如需添加更多环境变量,请重复此步骤。
-
点击继续。
网络和安全
-
在网络和安全部分的网络菜单中,选择一个网络。如果您未选择网络,则系统会选择您的默认网络。
-
在子网菜单中,选择一个子网。
-
如需停用公共互联网访问权限,请清除启用公共互联网访问权限复选框。
-
如需停用最终用户凭据访问权限,请清除启用最终用户凭据复选框。
完成运行时模板的创建
点击创建以完成运行时模板的创建。
您的运行时模板会显示在运行时模板标签页上的列表中。
gcloud
在使用下面的命令数据之前,请先进行以下替换:
DISPLAY_NAME:运行时模板的显示名称。PROJECT_ID:您的项目 ID。REGION:您希望运行时模板所在的区域。-
MACHINE_TYPE:要用于运行时的机器类型。 ACCELERATOR_TYPE:要用于运行时的硬件加速器类型。ACCELERATOR_COUNT:要用于运行时的加速器数量。
执行以下命令:
Linux、macOS 或 Cloud Shell
gcloud colab runtime-templates create --display-name="DISPLAY_NAME" \ --project=PROJECT_ID \ --region=REGION \ --machine-type=MACHINE_TYPE \ --accelerator-type=ACCELERATOR_TYPE \ --accelerator-count=ACCELERATOR_COUNT
Windows (PowerShell)
gcloud colab runtime-templates create --display-name="DISPLAY_NAME" ` --project=PROJECT_ID ` --region=REGION ` --machine-type=MACHINE_TYPE ` --accelerator-type=ACCELERATOR_TYPE ` --accelerator-count=ACCELERATOR_COUNT
Windows (cmd.exe)
gcloud colab runtime-templates create --display-name="DISPLAY_NAME" ^ --project=PROJECT_ID ^ --region=REGION ^ --machine-type=MACHINE_TYPE ^ --accelerator-type=ACCELERATOR_TYPE ^ --accelerator-count=ACCELERATOR_COUNT
如需详细了解用于从命令行创建运行时模板的命令,请参阅 gcloud CLI 文档。
REST
在使用任何请求数据之前,请先进行以下替换:
REGION:您希望运行时模板所在的区域。PROJECT_ID:您的项目 ID。DISPLAY_NAME:运行时模板的显示名称。-
MACHINE_TYPE:要用于运行时的机器类型。 ACCELERATOR_TYPE:要用于运行时的硬件加速器类型。ACCELERATOR_COUNT:要用于运行时的加速器数量。
HTTP 方法和网址:
POST https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/notebookRuntimeTemplates
请求 JSON 正文:
{
"displayName": "DISPLAY_NAME",
"machineSpec": {
{
"machineType": MACHINE_TYPE
"acceleratorType": ACCELERATOR_TYPE,
"acceleratorCount": ACCELERATOR_COUNT,
}
},
}
如需发送请求,请选择以下方式之一:
curl
将请求正文保存在名为 request.json 的文件中,然后执行以下命令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/notebookRuntimeTemplates"
PowerShell
将请求正文保存在名为 request.json 的文件中,然后执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/notebookRuntimeTemplates" | Select-Object -Expand Content
如需了解详情,请参阅 notebookRuntimeTemplates.create REST API 文档。
Terraform
如需了解如何应用或移除 Terraform 配置,请参阅基本 Terraform 命令。 如需了解详情,请参阅 Terraform 提供方参考文档。
以下示例使用
google_colab_runtime_template
Terraform 资源创建 Colab Enterprise
运行时模板。
授予对运行时模板的访问权限
创建运行时模板后,您必须授予该模板的访问权限,以使主账号可以使用它。主账号只有在具有以下权限时才能根据运行时模板创建运行时:
- 运行时模板的访问权限。
- 创建运行时所需的权限。