建立執行階段範本

本頁面說明如何在 Colab Enterprise 中建立執行階段範本。

如要在筆記本中執行程式碼,您可以使用稱為「執行時間」的運算資源。您可以使用預設執行階段,也可以使用透過執行階段範本建立的執行階段。您可以建立執行階段範本,並根據需求設定範本,以便根據需求改善執行階段的效能、成本和其他特性。

進一步瞭解執行階段和執行階段範本

事前準備

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Vertex AI, Dataform, and Compute Engine APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Vertex AI, Dataform, and Compute Engine APIs.

    Enable the APIs

  8. 必要的角色

    如要取得在 Colab Enterprise 中建立執行階段範本所需的權限,請要求管理員授予您專案的 Colab Enterprise 管理員 (roles/aiplatform.colabEnterpriseAdmin) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。

    您或許還可透過自訂角色或其他預先定義的角色取得必要權限。

    建立執行階段範本

    如要建立執行階段範本,您可以使用 Google Cloud 主控台、Google Cloud CLI、REST API 或 Terraform。

    控制台

    如要建立執行階段範本,請按照下列步驟操作:

    1. 在 Google Cloud 控制台中,前往 Colab Enterprise 的「Runtime templates」(執行階段範本) 頁面。

      前往「Runtime templates」(執行階段範本)

    2. 按一下 「新增範本」

      畫面上會顯示「Create new runtime template」對話方塊。

    執行階段基本資訊

    1. 在「執行階段基本資訊」專區中,輸入「顯示名稱」

    2. 在「Region」選單中,選取要建立執行階段範本的區域。

    3. 選用:新增執行階段範本的「說明」

    4. 選用步驟:如要新增標籤,請按一下 「Add label」,然後輸入「Key」和「Value」組合。如要新增更多標籤,請重複這個步驟。

    5. 按一下「繼續」

    設定運算資源

    1. 在「Configure compute」(設定運算) 區段的「Machine type」(機器類型) 選單中,選取機器類型。如要瞭解機器類型,請參閱機器系列資源與比較指南

      如果您選取含有 GPU 的機器類型,請選取「加速器類型」和「加速器數量」。如果無法選取所需的 GPU 數量,可能需要增加配額。請參閱「申請調整配額」一文。

    2. 在「資料磁碟類型」選單中,選取磁碟類型。

    3. 在「資料磁碟大小」欄位中,輸入大小 (以 GB 為單位)。

    4. 在「Idle shutdown」(閒置關機) 專區中:

      • 如要關閉閒置關機功能,請清除「Enable idle shutdown」

      • 如要變更閒置時間長度,請在「關機前的閒置時間 (分鐘)」 中,將數字改為所需的閒置時間長度。在 Google Cloud 控制台中,您可以將這項設定設為 10 到 1440 之間的任何整數值。

    5. 按一下「繼續」

    環境

    1. 在「環境」部分中,選取「環境」。 預設值為「最新」 (目前為 Python 3.11)。

    2. 按一下「繼續」

    網路與安全

    1. 在「網路和安全性」部分的「網路」選單中,選取所需網路。如果您未選取網路,系統會選取預設網路。

    2. 在「Subnetwork」(子網路) 選單中選取子網路。

    3. 如要關閉公開網際網路存取權,請取消勾選「Enable public internet access」

    4. 如要關閉使用者憑證存取權,請取消勾選「啟用使用者憑證」

    完成建立執行階段範本

    按一下「Create」,完成建立執行階段範本。

    執行階段範本會顯示在「Runtime templates」分頁的清單中。

    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
    如果成功,回應主體會包含 Operation 的例項。

    詳情請參閱 notebookRuntimeTemplates.create REST API 說明文件

    Terraform

    如要瞭解如何套用或移除 Terraform 設定,請參閱「基本 Terraform 指令」。 詳情請參閱 Terraform 供應器參考說明文件

    以下範例使用 google_colab_runtime_template Terraform 資源建立 Colab Enterprise 執行階段範本。

    resource "google_compute_network" "my_network" {
      name = "{{index $.Vars "network_name"}}"
      auto_create_subnetworks = false
    }
    
    resource "google_compute_subnetwork" "my_subnetwork" {
      name   = "{{index $.Vars "network_name"}}"
      network = google_compute_network.my_network.id
      region = "us-central1"
      ip_cidr_range = "10.0.1.0/24"
    }
    
    resource "google_colab_runtime_template" "{{$.PrimaryResourceId}}" {
      name        = "{{index $.Vars "runtime_template_name"}}"
      display_name = "Runtime template full"
      location    = "us-central1"
      description = "Full runtime template"
      machine_spec {
        machine_type     = "n1-standard-2"
        accelerator_type = "NVIDIA_TESLA_T4"
        accelerator_count = "1"
      }
    
      data_persistent_disk_spec {
        disk_type    = "pd-standard"
        disk_size_gb = 200
      }
    
      network_spec {
        enable_internet_access = true
        network = google_compute_network.my_network.id
        subnetwork = google_compute_subnetwork.my_subnetwork.id
      }
    
      labels = {
        k = "val"
      }
    
      idle_shutdown_config {
        idle_timeout = "3600s"
      }
    
      euc_config {
        euc_disabled = false
      }
    
      shielded_vm_config {
        enable_secure_boot = false
      }
    
      network_tags = ["abc", "def"]
    
      encryption_spec {
        kms_key_name = "{{index $.Vars "key_name"}}"
      }
    }
    

    授予執行階段範本的存取權

    建立執行階段範本後,您必須授予存取權,讓使用者主體能夠使用該範本。只有具備下列權限的使用者,才能從執行階段範本建立執行階段

    • 存取執行階段範本。
    • 建立執行階段所需的權限。

    請參閱「管理執行階段範本的存取權」。

    刪除執行階段範本

    如何刪除執行階段範本:

    1. 在 Google Cloud 控制台中,前往 Colab Enterprise 的「Runtime templates」(執行階段範本) 頁面。

      前往「Runtime templates」(執行階段範本)

    2. 在「Region」選單中,選取包含執行階段範本的區域。

    3. 選取要刪除的執行階段範本。

    4. 按一下「Delete」圖示

    5. 按一下「確認」。

    疑難排解

    本節說明如何解決在 Colab Enterprise 中建立執行階段範本時發生的問題。

    無法選取足夠的 GPU

    建立執行階段範本時,您無法選取所需的 GPU 數量。這可能是因為您的配額不足。

    Colab Enterprise 會使用 Compute Engine 的 GPU 配額。詳情請參閱 Compute Engine 配額與限制總覽

    如要解決這個問題,請要求調整配額

    後續步驟