建立及執行使用 GPU 的工作

本文說明如何建立及執行使用繪圖處理單元 (GPU) 的工作。如要進一步瞭解 GPU 的功能和限制,請參閱 Compute Engine 說明文件中的「關於 GPU」一文。

建立 Batch 工作時,您可以選擇使用 GPU 加速處理特定工作負載。使用 GPU 的工作常見用途包括密集資料處理和人工智慧 (AI) 工作負載,例如機器學習 (ML)。

事前準備

  1. 如果您從未使用過 Batch,請參閱「開始使用 Batch」,並完成專案和使用者的必要條件,啟用 Batch。
  2. 如要取得建立作業所需的權限,請要求管理員授予下列 IAM 角色:

    如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。

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

建立使用 GPU 的工作

如要建立使用 GPU 的工作,請按照下列步驟操作:

  1. 規劃使用 GPU 的工作需求
  2. 根據您識別出的需求和方法建立工作。 如需瞭解如何使用建議選項建立工作,請參閱本文的「建立使用 GPU 的範例工作」一節。

規劃使用 GPU 的工作需求

建立使用 GPU 的工作前,請先規劃工作需求,如以下章節所述:

  1. 選取 GPU 機器類型和用量選項
  2. 安裝 GPU 驅動程式
  3. 定義相容的 VM 資源

步驟 1:選取 GPU 機器類型和用量選項

工作需求會因偏好的 GPU 機器類型和消耗量選項而異,且各選項可能互有關聯。

如要為工作選取 GPU 機器類型和用量選項,請完成下列步驟:

  1. 找出並選取優先順序:

  2. 根據第一個選項的相容性,選取其餘選項。

    1. 如要找出與第一個選項相容的選項,請參閱 Compute Engine 說明文件中的「依機器類型提供的用量選項」。

    2. 請參閱其餘章節,分別瞭解如何選取消耗量選項選取 GPU 機器類型

選取 GPU 機型

如要為工作選取 GPU 機器類型並瞭解工作需求,請完成下列步驟:

  1. 選取 GPU 機器類型:如要查看可用的 GPU 機器類型 (GPU 類型、GPU 數量和機器類型 (vCPU 和記憶體) 的有效組合) 及其建議用途,請參閱 Compute Engine 說明文件中的「GPU 機器類型」和「加速器最佳化機器系列」。

  2. 瞭解 GPU 機器類型的工作需求: 指定 GPU 機器類型的工作所需欄位,會因下表中的類別而異:

    GPU 機器類型及其工作需求

    加速器最佳化 VM 的 GPU:如果 VM 的機型來自加速器最佳化機型系列,系統會自動連接特定類型和數量的 GPU。

    如要將 GPU 用於加速器最佳化 VM,建議您指定機器類型。 每個加速器最佳化機器類型僅支援特定類型和數量的 GPU,因此無論您是否指定這些值,功能上都等同於加速器最佳化機器類型。

    具體來說,Batch 也支援只為加速器最佳化 VM 指定 GPU 類型和數量,但產生的 vCPU 和記憶體選項通常非常有限。因此,建議您確認可用的 vCPU 和記憶體選項是否符合作業的工作需求。

    N1 VM 的 GPU:您必須指定要附加至每個 VM 的 GPU 類型和數量,且必須附加至N1 機器系列的 VM。

    如要將 GPU 用於 N1 VM,建議您至少指定 GPU 類型和 GPU 數量。請確認值組合符合N1 機器類型的有效 GPU 選項。使用任何特定類型和數量的 GPU 時,N1 VM 的 vCPU 和記憶體選項相當彈性。除非您使用 Google Cloud 控制台建立工作,否則 Batch 會自動選取符合工作任務需求的機器類型。

選取用量方案

本節將摘要說明如何為工作選取用量選項,以及每個選項的工作需求。

下表根據用例列出 Batch 的可用用量選項和相關規定,並依資源可用性從大致最高到最低排序。

總而言之,我們建議大多數使用者為工作選取下列用量選項:

  • 除非其他條件成立,否則請使用彈性啟動 VM (搶先版) (如果符合您的需求,建議使用此選項) 或隨選 VM。
  • 如果您的工作需要極高的資源可用性保證,或您有任何無法刪除的未使用預訂,請使用日曆模式預訂 (搶先版) (如果符合您的需求,建議使用此模式) 或預訂。
  • (選用) 如果工作負載的容錯能力極高,不妨嘗試使用 Spot VM,或許能降低成本。
用量方案和工作需求

預訂

  • 適用情況:如果您希望資源可用性有極高的保障,且無法使用日曆模式預留項目,或是您有現有的預留項目可能未使用,我們建議您為作業預留資源。

  • 詳細資料:建立預留項目後,系統會開始收取指定 VM 的費用,價格與執行 VM 相同,直到您刪除預留項目為止。使用預留項目的 VM 不會產生額外費用,但無論是否使用預留項目,都會產生費用。

    如要進一步瞭解預留項目,請參閱 Compute Engine 說明文件。

Batch 會為可耗用未使用的預留 VM 的工作使用預留項目。如要進一步瞭解使用預留項目的工作需求,請參閱「使用 VM 預留項目確保資源可用性」頁面。

日曆模式預訂 (搶先體驗)

  • 用途:如果您想使用支援日曆模式預留的 GPU 機型,且需要至少 1 天到最多 90 天的資源可用性保證,建議您為工作預留日曆模式資源。

  • 詳細資料:建立工作前,您必須要求或找出可使用的日曆模式預訂。如果 Compute Engine 在日曆模式下滿足未來的預留項目要求,自動建立的日曆模式預留項目會產生與下列項目相同的費用:在整個預留項目期間,以 Dynamic Workload Scheduler 定價執行指定的 VM,這項定價提供高達 53% 的隨選價格折扣。找出可用的自動建立日曆模式預留項目後,建立會耗用日曆模式預留項目的工作,與建立會耗用預留項目的工作非常相似。

    如要進一步瞭解日曆模式預留項目,請參閱 Compute Engine 說明文件。

Batch 會為符合下列所有條件的工作使用日曆模式預訂:

彈性啟動 VM (預先發布版)

  • 適用情境:如果您的工作可以容忍盡量提供可用性,並換取折扣價格和最多 7 天的執行時間,建議使用彈性啟動 VM。

    與隨選和 Spot VM 相比,彈性啟動 VM 可讓您更輕鬆地同時存取許多 GPU 資源。舉例來說,彈性啟動 VM 可減少資源無法使用所導致的延遲或問題,有助於排定工作。

  • 詳細資料:如果系統滿足建立使用彈性啟動 VM 的工作要求,則 VM 會按照Dynamic Workload Scheduler 定價計費,最多可享以量計價價格 47% 的折扣。

    如要進一步瞭解彈性啟動 VM,請參閱 Compute Engine 說明文件中的彈性啟動 VM

Batch 會針對符合下列所有條件的工作使用彈性啟動 VM:

以量計價

  • 用途:預設情況下,我們建議大多數其他工作都使用隨選模式。1

  • 詳細資料:通常是存取 Compute Engine VM 的預設方式。隨選功能可讓您在需要時存取資源 (如有)。資源會遭到刪除,直到工作完成或達到執行時間限制為止。

根據預設,Batch 會對大多數其他工作使用以量計價模式。如要確保作業使用隨選執行個體,請完成下列所有步驟:1

  • provisioningModel 欄位設為 STANDARD
  • 封鎖預訂。如要防止作業自動使用相容的預留項目,請將 reservation 欄位設為 NO_RESERVATION。詳情請參閱「建立及執行無法耗用預留 VM 的工作」。

1例外狀況:對於使用 A3 系列機器的作業,預設行為較為特殊,建議您不要使用隨選 VM,也不要將 provisioningModel 欄位設為未定義,而是使用彈性啟動 VM 的作業需求 由於資源可用性低,不建議使用 A3 機器系列隨選執行個體。因此,如果工作使用 A3 系列機器、封鎖預留項目,且 provisioningModel 欄位未定義或設為 STANDARD,預設的消耗選項會是唯一:行為類似於彈性啟動 VM,將執行時間限制為 7 天;不過,由於這些工作不會收到 Dynamic Workload Scheduler 定價的折扣,因此不建議使用。如果使用 A3 VM 的工作可容忍 7 天的執行時間限制,建議您使用彈性啟動 VM,而非隨選 VM。否則,除非您將 provisioningModel 設為 STANDARD,並手動驗證作業無法耗用預訂項目,且未將 reservation 欄位設為 NO_RESERVATION,否則您無法確保使用 A3 VM 的作業會使用隨選項目。

Spot VM

  • 用途:建議您嘗試使用 Spot VM,降低容錯工作負載的成本。

  • 詳細資料:如果系統滿足建立使用 Spot VM 的作業要求,則 VM 會按照Spot VM 定價計費,這類定價經常變動,且折扣幅度最大,最多可享以量計價價格 91% 的折扣。不過,Spot VM 不一定隨時可用,且隨時可能遭到先占。

    如要進一步瞭解 Spot VM,請參閱 Compute Engine 說明文件。

Batch 會針對符合下列所有條件的工作使用 Spot VM:

  • provisioningModel 欄位設為 SPOT
  • (選用) 封鎖預訂。您無法搭配預留項目使用 Spot VM。具體來說,這項工作可以視需要將 reservation 欄位設為 NO_RESERVATION。詳情請參閱「建立及執行無法耗用預留 VM 的工作」。

步驟 2:安裝 GPU 驅動程式

如要使用 GPU 執行工作,請務必安裝 GPU 驅動程式。如要安裝 GPU 驅動程式,請選用下列其中一種方法:

步驟 3:定義相容的 VM 資源

如要瞭解定義作業 VM 資源的規定和選項,請參閱「作業資源」。

總而言之,定義使用 GPU 的工作適用的 VM 資源時,您必須完成下列所有步驟:

  • 請確認 GPU 機型適用於作業 VM 的位置。

    如要瞭解哪些區域提供 GPU 機器類型,請參閱 Compute Engine 說明文件中的「加速器可用性」一文。

  • 如果您指定工作機型,請確認該機型有足夠的 vCPU 和記憶體,可滿足工作任務需求。使用 Google Cloud 控制台建立工作時,必須指定工作的機器類型。建立使用 GPU 的工作時,建議指定機器類型,以便使用加速器最佳化 VM。

  • 請務必使用有效方法,為工作定義 VM 資源:

    • 直接使用 instances[].policy 欄位定義 VM 資源 (建議盡可能採用)。範例所示。
    • 使用 instances[].instanceTemplate 欄位,透過範本定義 VM 資源。 如要透過自訂映像檔手動安裝 GPU 驅動程式,就必須使用這個方法。詳情請參閱使用 VM 執行個體範本定義工作資源

建立使用 GPU 的範例工作

以下各節說明如何為每個用量選項建立範例工作。具體來說,範例工作使用建議選項:全部自動安裝 GPU 驅動程式,並全部直接定義 VM 資源。

用量方案 工作指示範例
預訂日曆模式預訂 (搶先體驗)

使用 VM 預留項目確保資源可用性

彈性啟動 VM (預先發布版)

使用 GPU 和彈性啟動 VM

隨選Spot VM

根據 GPU 機器類型選取下列其中一個選項:

使用 GPU 和彈性啟動 VM

您可以透過 Dynamic Workload Scheduler,使用 gcloud CLI 或 Batch API 建立工作,讓 A3 VM 使用 GPU。

gcloud

  1. 建立 JSON 檔案,安裝 GPU 驅動程式、指定支援彈性啟動 VM 的 GPU 機器類型、封鎖預訂,並在支援 GPU 機器類型的位置執行。

    舉例來說,如要建立使用 Flex-start VM 和 GPU 的基本指令碼工作,以加速器最佳化 VM 執行,請建立包含下列內容的 JSON 檔案。

    {
        "taskGroups": [
            {
                "taskSpec": {
                    "runnables": [
                        {
                            "script": {
                                "text": "echo Hello world from task ${BATCH_TASK_INDEX}."
                            }
                        }
                    ]
                },
                "taskCount": 3,
                "parallelism": 1
            }
        ],
        "allocationPolicy": {
            "instances": [
                {
                    "installGpuDrivers": INSTALL_GPU_DRIVERS,
                    "policy": {
                        "provisioningModel":"FLEX_START",
                        "machineType": "MACHINE_TYPE",
                        "reservation": "NO_RESERVATION"
                    }
                }
            ],
            "location": {
                "allowedLocations": [
                    "ALLOWED_LOCATIONS"
                ]
            }
        },
        "logsPolicy": {
            "destination": "CLOUD_LOGGING"
        }
    }
    

    更改下列內容:

    • INSTALL_GPU_DRIVERS:設為 true 時,Batch 會從第三方位置擷取 policy 欄位中指定的 GPU 類型所需驅動程式,並代您安裝。如果將這個欄位設為 false (預設值),您必須手動安裝 GPU 驅動程式,才能使用這項工作的任何 GPU。

    • MACHINE_TYPE:支援彈性啟動 VM 的 GPU 機器類型。詳情請參閱 Compute Engine 說明文件中的「依機器類型提供的用量選項」。

    • ALLOWED_LOCATIONS:您可以選擇使用allowedLocations[] 欄位,指定允許工作 VM 執行的地區或地區中的特定區域,例如 regions/us-central1 允許地區 us-central1 中的所有區域。請務必指定提供您要用於這項工作的 GPU 機型的位置。否則,如果省略這個欄位,請確保工作地點提供 GPU 機器類型。

  2. 如要建立及執行作業,請使用 gcloud alpha batch jobs submit 指令

    gcloud batch jobs submit JOB_NAME \
        --location LOCATION \
        --config JSON_CONFIGURATION_FILE
    

    更改下列內容:

    • JOB_NAME:作業名稱。

    • LOCATION:工作地點

    • JSON_CONFIGURATION_FILE:JSON 檔案的路徑,內含作業的設定詳細資料。

API

v1alpha jobs.create 方法發出 POST 要求,安裝 GPU 驅動程式、指定支援彈性啟動 VM 的 GPU 機器類型、封鎖預訂,並在支援 GPU 機器類型的位置執行。

舉例來說,如要建立基本指令碼工作,並使用彈性啟動 VM 和 GPU 進行加速器最佳化 VM,請提出下列要求:

POST https://batch.googleapis.com/v1alpha/projects/PROJECT_ID/locations/LOCATION/jobs?job_id=JOB_NAME

{
    "taskGroups": [
        {
            "taskSpec": {
                "runnables": [
                    {
                        "script": {
                            "text": "echo Hello world from task ${BATCH_TASK_INDEX}."
                        }
                    }
                ]
            },
            "taskCount": 3,
            "parallelism": 1
        }
    ],
    "allocationPolicy": {
        "instances": [
            {
                "installGpuDrivers": INSTALL_GPU_DRIVERS,
                "policy": {
                    "provisioningModel":"FLEX_START",
                    "machineType": "MACHINE_TYPE",
                    "reservation": "NO_RESERVATION"
                }
            }
        ],
        "location": {
            "allowedLocations": [
                "ALLOWED_LOCATIONS"
            ]
        }
    },
    "logsPolicy": {
        "destination": "CLOUD_LOGGING"
    }
}

更改下列內容:

  • PROJECT_ID:專案的專案 ID

  • LOCATION:工作地點

  • JOB_NAME:作業名稱。

  • INSTALL_GPU_DRIVERS:設為 true 時,Batch 會從第三方位置擷取 policy 欄位中指定的 GPU 類型所需驅動程式,並代您安裝。如果將這個欄位設為 false (預設值),您必須手動安裝 GPU 驅動程式,才能使用這項工作的任何 GPU。

  • MACHINE_TYPE:支援彈性啟動 VM 的 GPU 機器類型。詳情請參閱 Compute Engine 說明文件中的「依機器類型提供的用量選項」。

  • ALLOWED_LOCATIONS:您可以選擇使用allowedLocations[] 欄位,指定允許工作 VM 執行的地區或地區中的特定區域,例如 regions/us-central1 允許地區 us-central1 中的所有區域。請務必指定提供您要用於這項工作的 GPU 機型的位置。否則,如果省略這個欄位,請確保工作地點提供 GPU 機器類型。

使用 GPU 搭配加速器最佳化 VM

您可以使用Google Cloud 主控台、gcloud CLI、Batch API、Java、Node.js 或 Python,建立使用 GPU 的工作,以搭配加速器最佳化 VM。

控制台

如要使用 Google Cloud 控制台建立使用 GPU 的工作,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Job list」(工作清單) 頁面。

    前往工作清單

  2. 按一下 「建立」。「建立批次工作」頁面隨即開啟。左側窗格會選取「工作詳細資料」頁面。

  3. 設定「工作詳細資料」頁面:

    1. 選用:在「Job name」(工作名稱) 欄位中,自訂工作名稱。

      例如輸入 example-gpu-job

    2. 設定「工作詳細資料」部分:

      1. 在「新增可執行項目」視窗中,為這項工作新增至少一個指令碼或容器,以供執行。

        舉例來說,如要建立基本指令碼工作,請按照下列步驟操作:

        1. 勾選「指令碼」核取方塊。系統會顯示一個欄位。

        2. 在欄位中輸入下列指令碼:

          echo Hello world from task ${BATCH_TASK_INDEX}.
          
        3. 按一下 [完成]

      2. 在「Task count」(工作數) 欄位中,輸入這項工作的工作數。

        例如輸入 3

      3. 選用:在「Parallelism」(平行處理) 欄位中,輸入要同時執行的工作數量。

        例如,輸入 1 (預設值)。

  4. 設定「資源規格」頁面:

    1. 在左側窗格中,按一下「資源規格」。 「資源規格」頁面隨即開啟。

    2. 在「VM 佈建模型」部分,為這項作業的 VM 選取下列其中一個消耗選項

      • 如果您的工作可以承受先占,且您想要使用折扣價的 Spot VM,請選取「Spot」

      • 如要使用隨選 VM,請選取「標準」 (預設)。

    3. 選取這項工作的地點

      1. 在「Region」(區域) 欄位中選取區域。

      2. 在「區域」欄位中,執行下列其中一項操作:

        • 如要限制這項工作只能在特定區域中執行,請選取區域。

        • 否則請選取「不限」 (預設)。

      位置
    4. 為這項工作的 VM 選取 GPU 機器類型:

      1. 在機器系列選項中,按一下「GPU」

      2. 在「GPU type」(GPU 類型) 欄位中,選取 GPU 類型。然後在「Number of GPUs」(GPU 數量) 欄位中,選取每個 VM 的 GPU 數量。

        如果您選取加速器最佳化 VM 的其中一種 GPU 類型,則「機器類型」欄位只會根據您選取的 GPU 類型和數量,提供一種機器類型選項。

      3. 如要自動安裝 GPU 驅動程式,請選取「GPU driver installation」(安裝 GPU 驅動程式) (預設)。

    5. 設定各項工作所需的 VM 資源量:

      1. 在「Cores」(核心) 欄位中,輸入每個工作使用的 vCPUs 數量。

        例如,輸入 1 (預設值)。

      2. 在「記憶體」欄位中,輸入每個工作使用的 RAM 容量 (以 GB 為單位)。

        例如,輸入 0.5 (預設值)。

    6. 按一下 [完成]

  5. 選用:設定這項工作的其他欄位

  6. 選用:如要檢查工作設定,請在左側窗格中按一下「預覽」

  7. 點選「建立」

    「Job details」(工作詳細資料) 頁面會顯示您建立的工作。

gcloud

  1. 建立 JSON 檔案,安裝 GPU 驅動程式、指定加速器最佳化機器家族的機器類型,並在具有 GPU 機器類型的位置執行。

    舉例來說,如要建立使用 GPU 的基本指令碼作業,以加速器最佳化 VM 執行,請建立內容如下的 JSON 檔案:

    {
        "taskGroups": [
            {
                "taskSpec": {
                    "runnables": [
                        {
                            "script": {
                                "text": "echo Hello world from task ${BATCH_TASK_INDEX}."
                            }
                        }
                    ]
                },
                "taskCount": 3,
                "parallelism": 1
            }
        ],
        "allocationPolicy": {
            "instances": [
                {
                    "installGpuDrivers": INSTALL_GPU_DRIVERS,
                    "policy": {
                        "provisioningModel": "PROVISIONING_MODEL",
                        "machineType": "MACHINE_TYPE",
                        "reservation": "NO_RESERVATION"
                    }
                }
            ],
            "location": {
                "allowedLocations": [
                    "ALLOWED_LOCATIONS"
                ]
            }
        },
        "logsPolicy": {
            "destination": "CLOUD_LOGGING"
        }
    }
    

    更改下列內容:

  2. 如要建立及執行作業,請使用 gcloud batch jobs submit 指令

    gcloud batch jobs submit JOB_NAME \
        --location LOCATION \
        --config JSON_CONFIGURATION_FILE
    

    更改下列內容:

    • JOB_NAME:作業名稱。

    • LOCATION:工作地點

    • JSON_CONFIGURATION_FILE:JSON 檔案的路徑,內含作業的設定詳細資料。

API

jobs.create 方法發出 POST 要求,安裝 GPU 驅動程式、指定加速器最佳化機型系列的機器類型,並在具有 GPU 機器類型的位置執行。

舉例來說,如要建立使用 GPU 的基本指令碼工作,以加速器最佳化 VM 執行個體執行,請提出下列要求:

POST https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs?job_id=JOB_NAME

{
    "taskGroups": [
        {
            "taskSpec": {
                "runnables": [
                    {
                        "script": {
                            "text": "echo Hello world from task ${BATCH_TASK_INDEX}."
                        }
                    }
                ]
            },
            "taskCount": 3,
            "parallelism": 1
        }
    ],
    "allocationPolicy": {
        "instances": [
            {
                "installGpuDrivers": INSTALL_GPU_DRIVERS,
                "policy": {
                    "provisioningModel": "PROVISIONING_MODEL",
                    "machineType": "MACHINE_TYPE",
                    "reservation": "NO_RESERVATION"
                }
            }
        ],
        "location": {
            "allowedLocations": [
                "ALLOWED_LOCATIONS"
            ]
        }
    },
    "logsPolicy": {
        "destination": "CLOUD_LOGGING"
    }
}

更改下列內容:

Java


import com.google.cloud.batch.v1.AllocationPolicy;
import com.google.cloud.batch.v1.AllocationPolicy.Accelerator;
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicy;
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicyOrTemplate;
import com.google.cloud.batch.v1.BatchServiceClient;
import com.google.cloud.batch.v1.CreateJobRequest;
import com.google.cloud.batch.v1.Job;
import com.google.cloud.batch.v1.LogsPolicy;
import com.google.cloud.batch.v1.Runnable;
import com.google.cloud.batch.v1.Runnable.Script;
import com.google.cloud.batch.v1.TaskGroup;
import com.google.cloud.batch.v1.TaskSpec;
import com.google.protobuf.Duration;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateGpuJob {

  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Google Cloud project you want to use.
    String projectId = "YOUR_PROJECT_ID";
    // Name of the region you want to use to run the job. Regions that are
    // available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
    String region = "europe-central2";
    // The name of the job that will be created.
    // It needs to be unique for each project and region pair.
    String jobName = "JOB_NAME";
    // Optional. When set to true, Batch fetches the drivers required for the GPU type
    // that you specify in the policy field from a third-party location,
    // and Batch installs them on your behalf. If you set this field to false (default),
    // you need to install GPU drivers manually to use any GPUs for this job.
    boolean installGpuDrivers = false;
    // Accelerator-optimized machine types are available to Batch jobs. See the list
    // of available types on: https://cloud.google.com/compute/docs/accelerator-optimized-machines
    String machineType = "g2-standard-4";

    createGpuJob(projectId, region, jobName, installGpuDrivers, machineType);
  }

  // Create a job that uses GPUs
  public static Job createGpuJob(String projectId, String region, String jobName,
                                  boolean installGpuDrivers, String machineType)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {
      // Define what will be done as part of the job.
      Runnable runnable =
          Runnable.newBuilder()
              .setScript(
                  Script.newBuilder()
                      .setText(
                          "echo Hello world! This is task ${BATCH_TASK_INDEX}. "
                                  + "This job has a total of ${BATCH_TASK_COUNT} tasks.")
                      // You can also run a script from a file. Just remember, that needs to be a
                      // script that's already on the VM that will be running the job.
                      // Using setText() and setPath() is mutually exclusive.
                      // .setPath("/tmp/test.sh")
                      .build())
              .build();

      TaskSpec task = TaskSpec.newBuilder()
                  // Jobs can be divided into tasks. In this case, we have only one task.
                  .addRunnables(runnable)
                  .setMaxRetryCount(2)
                  .setMaxRunDuration(Duration.newBuilder().setSeconds(3600).build())
                  .build();

      // Tasks are grouped inside a job using TaskGroups.
      // Currently, it's possible to have only one task group.
      TaskGroup taskGroup = TaskGroup.newBuilder()
          .setTaskCount(3)
          .setParallelism(1)
          .setTaskSpec(task)
          .build();

      // Policies are used to define on what kind of virtual machines the tasks will run.
      // Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
      InstancePolicy instancePolicy =
          InstancePolicy.newBuilder().setMachineType(machineType).build();  

      // Policies are used to define on what kind of virtual machines the tasks will run on.
      AllocationPolicy allocationPolicy =
          AllocationPolicy.newBuilder()
              .addInstances(
                  InstancePolicyOrTemplate.newBuilder()
                      .setInstallGpuDrivers(installGpuDrivers)
                      .setPolicy(instancePolicy)
                      .build())
              .build();

      Job job =
          Job.newBuilder()
              .addTaskGroups(taskGroup)
              .setAllocationPolicy(allocationPolicy)
              .putLabels("env", "testing")
              .putLabels("type", "script")
              // We use Cloud Logging as it's an out of the box available option.
              .setLogsPolicy(
                  LogsPolicy.newBuilder().setDestination(LogsPolicy.Destination.CLOUD_LOGGING))
              .build();

      CreateJobRequest createJobRequest =
          CreateJobRequest.newBuilder()
              // The job's parent is the region in which the job will run.
              .setParent(String.format("projects/%s/locations/%s", projectId, region))
              .setJob(job)
              .setJobId(jobName)
              .build();

      Job result =
          batchServiceClient
              .createJobCallable()
              .futureCall(createJobRequest)
              .get(5, TimeUnit.MINUTES);

      System.out.printf("Successfully created the job: %s", result.getName());

      return result;
    }
  }
}

Node.js

// Imports the Batch library
const batchLib = require('@google-cloud/batch');
const batch = batchLib.protos.google.cloud.batch.v1;

// Instantiates a client
const batchClient = new batchLib.v1.BatchServiceClient();

/**
 * TODO(developer): Update these variables before running the sample.
 */
// Project ID or project number of the Google Cloud project you want to use.
const projectId = await batchClient.getProjectId();
// Name of the region you want to use to run the job. Regions that are
// available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
const region = 'europe-central2';
// The name of the job that will be created.
// It needs to be unique for each project and region pair.
const jobName = 'batch-gpu-job';
// The GPU type. You can view a list of the available GPU types
// by using the `gcloud compute accelerator-types list` command.
const gpuType = 'nvidia-l4';
// The number of GPUs of the specified type.
const gpuCount = 1;
// Optional. When set to true, Batch fetches the drivers required for the GPU type
// that you specify in the policy field from a third-party location,
// and Batch installs them on your behalf. If you set this field to false (default),
// you need to install GPU drivers manually to use any GPUs for this job.
const installGpuDrivers = false;
// Accelerator-optimized machine types are available to Batch jobs. See the list
// of available types on: https://cloud.google.com/compute/docs/accelerator-optimized-machines
const machineType = 'g2-standard-4';

// Define what will be done as part of the job.
const runnable = new batch.Runnable({
  script: new batch.Runnable.Script({
    commands: ['-c', 'echo Hello world! This is task ${BATCH_TASK_INDEX}.'],
  }),
});

const task = new batch.TaskSpec({
  runnables: [runnable],
  maxRetryCount: 2,
  maxRunDuration: {seconds: 3600},
});

// Tasks are grouped inside a job using TaskGroups.
const group = new batch.TaskGroup({
  taskCount: 3,
  taskSpec: task,
});

// Policies are used to define on what kind of virtual machines the tasks will run on.
// In this case, we tell the system to use "g2-standard-4" machine type.
// Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
const instancePolicy = new batch.AllocationPolicy.InstancePolicy({
  machineType,
  // Accelerator describes Compute Engine accelerators to be attached to the VM
  accelerators: [
    new batch.AllocationPolicy.Accelerator({
      type: gpuType,
      count: gpuCount,
      installGpuDrivers,
    }),
  ],
});

const allocationPolicy = new batch.AllocationPolicy.InstancePolicyOrTemplate({
  instances: [{installGpuDrivers, policy: instancePolicy}],
});

const job = new batch.Job({
  name: jobName,
  taskGroups: [group],
  labels: {env: 'testing', type: 'script'},
  allocationPolicy,
  // We use Cloud Logging as it's an option available out of the box
  logsPolicy: new batch.LogsPolicy({
    destination: batch.LogsPolicy.Destination.CLOUD_LOGGING,
  }),
});
// The job's parent is the project and region in which the job will run
const parent = `projects/${projectId}/locations/${region}`;

async function callCreateBatchGPUJob() {
  // Construct request
  const request = {
    parent,
    jobId: jobName,
    job,
  };

  // Run request
  const [response] = await batchClient.createJob(request);
  console.log(JSON.stringify(response));
}

await callCreateBatchGPUJob();

Python

from google.cloud import batch_v1


def create_gpu_job(project_id: str, region: str, job_name: str) -> batch_v1.Job:
    """
    This method shows how to create a sample Batch Job that will run
    a simple command on Cloud Compute instances on GPU machines.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.
        region: name of the region you want to use to run the job. Regions that are
            available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
        job_name: the name of the job that will be created.
            It needs to be unique for each project and region pair.

    Returns:
        A job object representing the job created.
    """
    client = batch_v1.BatchServiceClient()

    # Define what will be done as part of the job.
    task = batch_v1.TaskSpec()
    runnable = batch_v1.Runnable()
    runnable.script = batch_v1.Runnable.Script()
    runnable.script.text = "echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."
    # You can also run a script from a file. Just remember, that needs to be a script that's
    # already on the VM that will be running the job. Using runnable.script.text and runnable.script.path is mutually
    # exclusive.
    # runnable.script.path = '/tmp/test.sh'
    task.runnables = [runnable]

    # We can specify what resources are requested by each task.
    resources = batch_v1.ComputeResource()
    resources.cpu_milli = 2000  # in milliseconds per cpu-second. This means the task requires 2 whole CPUs.
    resources.memory_mib = 16  # in MiB
    task.compute_resource = resources

    task.max_retry_count = 2
    task.max_run_duration = "3600s"

    # Tasks are grouped inside a job using TaskGroups.
    # Currently, it's possible to have only one task group.
    group = batch_v1.TaskGroup()
    group.task_count = 4
    group.task_spec = task

    # Policies are used to define on what kind of virtual machines the tasks will run on.
    # In this case, we tell the system to use "g2-standard-4" machine type.
    # Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
    policy = batch_v1.AllocationPolicy.InstancePolicy()
    policy.machine_type = "g2-standard-4"

    instances = batch_v1.AllocationPolicy.InstancePolicyOrTemplate()
    instances.policy = policy
    instances.install_gpu_drivers = True
    allocation_policy = batch_v1.AllocationPolicy()
    allocation_policy.instances = [instances]

    job = batch_v1.Job()
    job.task_groups = [group]
    job.allocation_policy = allocation_policy
    job.labels = {"env": "testing", "type": "container"}
    # We use Cloud Logging as it's an out of the box available option
    job.logs_policy = batch_v1.LogsPolicy()
    job.logs_policy.destination = batch_v1.LogsPolicy.Destination.CLOUD_LOGGING

    create_request = batch_v1.CreateJobRequest()
    create_request.job = job
    create_request.job_id = job_name
    # The job's parent is the region in which the job will run
    create_request.parent = f"projects/{project_id}/locations/{region}"

    return client.create_job(create_request)

為 N1 VM 使用 GPU

您可以使用 Google Cloud 控制台、gcloud CLI、Batch API、Java、Node.js 或 Python,建立使用 N1 VM GPU 的工作。

控制台

如要使用 Google Cloud 控制台建立使用 GPU 的工作,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Job list」(工作清單) 頁面。

    前往工作清單

  2. 按一下 「建立」。「建立批次工作」頁面隨即開啟。左側窗格會選取「工作詳細資料」頁面。

  3. 設定「工作詳細資料」頁面:

    1. 選用:在「Job name」(工作名稱) 欄位中,自訂工作名稱。

      例如輸入 example-gpu-job

    2. 設定「工作詳細資料」部分:

      1. 在「新增可執行項目」視窗中,為這項工作新增至少一個指令碼或容器,以供執行。

        舉例來說,如要建立基本指令碼工作,請按照下列步驟操作:

        1. 勾選「指令碼」核取方塊。系統會顯示一個欄位。

        2. 在欄位中輸入下列指令碼:

          echo Hello world from task ${BATCH_TASK_INDEX}.
          
        3. 按一下 [完成]

      2. 在「Task count」(工作數) 欄位中,輸入這項工作的工作數。

        例如輸入 3

      3. 選用:在「Parallelism」(平行處理) 欄位中,輸入要同時執行的工作數量。

        例如,輸入 1 (預設值)。

  4. 設定「資源規格」頁面:

    1. 在左側窗格中,按一下「資源規格」。 「資源規格」頁面隨即開啟。

    2. 在「VM 佈建模型」部分,為這項作業的 VM 選取下列其中一個消耗選項

      • 如果您的工作可以承受先占,且您想要使用折扣價的 Spot VM,請選取「Spot」

      • 如要使用隨選 VM,請選取「標準」 (預設)。

    3. 選取這項工作的地點

      1. 在「Region」(區域) 欄位中選取區域。

      2. 在「區域」欄位中,執行下列其中一項操作:

        • 如要限制這項工作只能在特定區域中執行,請選取區域。

        • 否則請選取「不限」 (預設)。

      位置。
    4. 為這項工作的 VM 選取 GPU 機器類型:

      1. 在機器系列選項中,按一下「GPU」

      2. 在「GPU type」(GPU 類型) 欄位中,選取 GPU 類型。

        如果您選取N1 VM 的其中一種 GPU 類型,則「系列」欄會設為「N1」

      3. 在「Number of GPUs」(GPU 數量) 欄位中,選取每個 VM 的 GPU 數量。

      4. 在「Machine type」(機器類型) 欄位中,選取機器類型。

      5. 如要自動安裝 GPU 驅動程式,請選取「GPU driver installation」(安裝 GPU 驅動程式) (預設)。

    5. 設定各項工作所需的 VM 資源量:

      1. 在「Cores」(核心) 欄位中,輸入每個工作使用的 vCPUs 數量。

        例如,輸入 1 (預設值)。

      2. 在「記憶體」欄位中,輸入每個工作使用的 RAM 容量 (以 GB 為單位)。

        例如,輸入 0.5 (預設值)。

    6. 按一下 [完成]

  5. 選用:設定這項工作的其他欄位

  6. 選用:如要檢查工作設定,請在左側窗格中按一下「預覽」

  7. 點選「建立」

    「Job details」(工作詳細資料) 頁面會顯示您建立的工作。

gcloud

  1. 建立 JSON 檔案,安裝 GPU 驅動程式、定義 accelerators[] 欄位的 typecount 子欄位,並在具有 GPU 機器類型的位置執行。

    舉例來說,如要建立使用 N1 VM 的 GPU,並讓 Batch 選取確切 N1 機型的基本指令碼工作,請建立含有下列內容的 JSON 檔案:

    {
        "taskGroups": [
            {
                "taskSpec": {
                    "runnables": [
                        {
                            "script": {
                                "text": "echo Hello world from task ${BATCH_TASK_INDEX}."
                            }
                        }
                    ]
                },
                "taskCount": 3,
                "parallelism": 1
            }
        ],
        "allocationPolicy": {
            "instances": [
                {
                    "installGpuDrivers": INSTALL_GPU_DRIVERS,
                    "policy": {
                        "provisioningModel": "PROVISIONING_MODEL",
                        "reservation": "NO_RESERVATION",
                        "accelerators": [
                            {
                                "type": "GPU_TYPE",
                                "count": GPU_COUNT
                            }
                        ]
                    }
                }
            ],
            "location": {
                "allowedLocations": [
                    "ALLOWED_LOCATIONS"
                ]
            }
        },
        "logsPolicy": {
            "destination": "CLOUD_LOGGING"
        }
    }
    

    更改下列內容:

  2. 如要建立及執行作業,請使用 gcloud batch jobs submit 指令

    gcloud batch jobs submit JOB_NAME \
        --location LOCATION \
        --config JSON_CONFIGURATION_FILE
    

    更改下列內容:

    • JOB_NAME:作業名稱。

    • LOCATION:工作地點

    • JSON_CONFIGURATION_FILE:JSON 檔案的路徑,內含作業的設定詳細資料。

API

jobs.create 方法發出 POST 要求,安裝 GPU 驅動程式、定義 accelerators[] 欄位的 typecount 子欄位,並使用具有 GPU 機器類型的位置。

舉例來說,如要建立使用 N1 VM 的 GPU,並讓 Batch 選取確切 N1 機器類型的基本指令碼工作,請提出下列要求:

POST https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs?job_id=JOB_NAME

{
    "taskGroups": [
        {
            "taskSpec": {
                "runnables": [
                    {
                        "script": {
                            "text": "echo Hello world from task ${BATCH_TASK_INDEX}."
                        }
                    }
                ]
            },
            "taskCount": 3,
            "parallelism": 1
        }
    ],
    "allocationPolicy": {
        "instances": [
            {
                "installGpuDrivers": INSTALL_GPU_DRIVERS,
                "policy": {
                    "provisioningModel": "PROVISIONING_MODEL",
                    "reservation": "NO_RESERVATION",
                    "accelerators": [
                        {
                            "type": "GPU_TYPE",
                            "count": GPU_COUNT
                        }
                    ]
                }
            }
        ],
        "location": {
            "allowedLocations": [
                "ALLOWED_LOCATIONS"
            ]
        }
    },
    "logsPolicy": {
        "destination": "CLOUD_LOGGING"
    }
}

更改下列內容:

  • PROJECT_ID:專案的專案 ID

  • LOCATION:工作地點

  • JOB_NAME:作業名稱。

  • INSTALL_GPU_DRIVERS:設為 true 時,Batch 會從第三方位置擷取 policy 欄位中指定的 GPU 類型所需驅動程式,並代您安裝。如果將這個欄位設為 false (預設值),您必須手動安裝 GPU 驅動程式,才能使用這項工作的任何 GPU。

  • PROVISIONING_MODEL消耗選項的佈建模式:STANDARD 代表隨選,SPOT 代表 Spot VM。

  • GPU_TYPEGPU 類型。您可以使用 gcloud compute accelerator-types list 指令,查看可用 GPU 類型的清單。這個欄位僅適用於 N1 VM 的 GPU。

  • GPU_COUNT:指定類型的 GPU 數量。如要進一步瞭解有效選項,請參閱 N1 系列機器的 GPU 機型。這個欄位僅適用於 N1 VM 的 GPU。

  • ALLOWED_LOCATIONS:您可以選擇使用 allowedLocations[] 欄位,指定允許工作 VM 執行的地區或地區中的特定區域,例如 regions/us-central1 允許地區 us-central1 中的所有區域。請務必指定提供您要用於這項工作的 GPU 機型的位置。否則,如果省略這個欄位,請確保工作地點提供 GPU 機器類型。

Java


import com.google.cloud.batch.v1.AllocationPolicy;
import com.google.cloud.batch.v1.AllocationPolicy.Accelerator;
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicy;
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicyOrTemplate;
import com.google.cloud.batch.v1.BatchServiceClient;
import com.google.cloud.batch.v1.CreateJobRequest;
import com.google.cloud.batch.v1.Job;
import com.google.cloud.batch.v1.LogsPolicy;
import com.google.cloud.batch.v1.Runnable;
import com.google.cloud.batch.v1.Runnable.Script;
import com.google.cloud.batch.v1.TaskGroup;
import com.google.cloud.batch.v1.TaskSpec;
import com.google.protobuf.Duration;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateGpuJobN1 {

  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Google Cloud project you want to use.
    String projectId = "YOUR_PROJECT_ID";
    // Name of the region you want to use to run the job. Regions that are
    // available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
    String region = "europe-central2";
    // The name of the job that will be created.
    // It needs to be unique for each project and region pair.
    String jobName = "JOB_NAME";
    // Optional. When set to true, Batch fetches the drivers required for the GPU type
    // that you specify in the policy field from a third-party location,
    // and Batch installs them on your behalf. If you set this field to false (default),
    // you need to install GPU drivers manually to use any GPUs for this job.
    boolean installGpuDrivers = false;
    // The GPU type. You can view a list of the available GPU types
    // by using the `gcloud compute accelerator-types list` command.
    String gpuType = "nvidia-tesla-t4";
    // The number of GPUs of the specified type.
    int gpuCount = 2;

    createGpuJob(projectId, region, jobName, installGpuDrivers, gpuType, gpuCount);
  }

  // Create a job that uses GPUs
  public static Job createGpuJob(String projectId, String region, String jobName,
                                  boolean installGpuDrivers, String gpuType, int gpuCount)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {
      // Define what will be done as part of the job.
      Runnable runnable =
          Runnable.newBuilder()
              .setScript(
                  Script.newBuilder()
                      .setText(
                          "echo Hello world! This is task ${BATCH_TASK_INDEX}. "
                                  + "This job has a total of ${BATCH_TASK_COUNT} tasks.")
                      // You can also run a script from a file. Just remember, that needs to be a
                      // script that's already on the VM that will be running the job.
                      // Using setText() and setPath() is mutually exclusive.
                      // .setPath("/tmp/test.sh")
                      .build())
              .build();

      TaskSpec task = TaskSpec.newBuilder()
                  // Jobs can be divided into tasks. In this case, we have only one task.
                  .addRunnables(runnable)
                  .setMaxRetryCount(2)
                  .setMaxRunDuration(Duration.newBuilder().setSeconds(3600).build())
                  .build();

      // Tasks are grouped inside a job using TaskGroups.
      // Currently, it's possible to have only one task group.
      TaskGroup taskGroup = TaskGroup.newBuilder()
          .setTaskCount(3)
          .setParallelism(1)
          .setTaskSpec(task)
          .build();

      // Accelerator describes Compute Engine accelerators to be attached to the VM.
      Accelerator accelerator = Accelerator.newBuilder()
          .setType(gpuType)
          .setCount(gpuCount)
          .build();

      // Policies are used to define on what kind of virtual machines the tasks will run on.
      AllocationPolicy allocationPolicy =
          AllocationPolicy.newBuilder()
              .addInstances(
                  InstancePolicyOrTemplate.newBuilder()
                      .setInstallGpuDrivers(installGpuDrivers)
                      .setPolicy(InstancePolicy.newBuilder().addAccelerators(accelerator))
                      .build())
              .build();

      Job job =
          Job.newBuilder()
              .addTaskGroups(taskGroup)
              .setAllocationPolicy(allocationPolicy)
              .putLabels("env", "testing")
              .putLabels("type", "script")
              // We use Cloud Logging as it's an out of the box available option.
              .setLogsPolicy(
                  LogsPolicy.newBuilder().setDestination(LogsPolicy.Destination.CLOUD_LOGGING))
              .build();

      CreateJobRequest createJobRequest =
          CreateJobRequest.newBuilder()
              // The job's parent is the region in which the job will run.
              .setParent(String.format("projects/%s/locations/%s", projectId, region))
              .setJob(job)
              .setJobId(jobName)
              .build();

      Job result =
          batchServiceClient
              .createJobCallable()
              .futureCall(createJobRequest)
              .get(5, TimeUnit.MINUTES);

      System.out.printf("Successfully created the job: %s", result.getName());

      return result;
    }
  }
}

Node.js

// Imports the Batch library
const batchLib = require('@google-cloud/batch');
const batch = batchLib.protos.google.cloud.batch.v1;

// Instantiates a client
const batchClient = new batchLib.v1.BatchServiceClient();

/**
 * TODO(developer): Update these variables before running the sample.
 */
// Project ID or project number of the Google Cloud project you want to use.
const projectId = await batchClient.getProjectId();
// Name of the region you want to use to run the job. Regions that are
// available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
const region = 'europe-central2';
// The name of the job that will be created.
// It needs to be unique for each project and region pair.
const jobName = 'batch-gpu-job-n1';
// The GPU type. You can view a list of the available GPU types
// by using the `gcloud compute accelerator-types list` command.
const gpuType = 'nvidia-tesla-t4';
// The number of GPUs of the specified type.
const gpuCount = 1;
// Optional. When set to true, Batch fetches the drivers required for the GPU type
// that you specify in the policy field from a third-party location,
// and Batch installs them on your behalf. If you set this field to false (default),
// you need to install GPU drivers manually to use any GPUs for this job.
const installGpuDrivers = false;
// Accelerator-optimized machine types are available to Batch jobs. See the list
// of available types on: https://cloud.google.com/compute/docs/accelerator-optimized-machines
const machineType = 'n1-standard-16';

// Define what will be done as part of the job.
const runnable = new batch.Runnable({
  script: new batch.Runnable.Script({
    commands: ['-c', 'echo Hello world! This is task ${BATCH_TASK_INDEX}.'],
  }),
});

const task = new batch.TaskSpec({
  runnables: [runnable],
  maxRetryCount: 2,
  maxRunDuration: {seconds: 3600},
});

// Tasks are grouped inside a job using TaskGroups.
const group = new batch.TaskGroup({
  taskCount: 3,
  taskSpec: task,
});

// Policies are used to define on what kind of virtual machines the tasks will run on.
// In this case, we tell the system to use "g2-standard-4" machine type.
// Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
const instancePolicy = new batch.AllocationPolicy.InstancePolicy({
  machineType,
  // Accelerator describes Compute Engine accelerators to be attached to the VM
  accelerators: [
    new batch.AllocationPolicy.Accelerator({
      type: gpuType,
      count: gpuCount,
      installGpuDrivers,
    }),
  ],
});

const allocationPolicy = new batch.AllocationPolicy.InstancePolicyOrTemplate({
  instances: [{installGpuDrivers, policy: instancePolicy}],
});

const job = new batch.Job({
  name: jobName,
  taskGroups: [group],
  labels: {env: 'testing', type: 'script'},
  allocationPolicy,
  // We use Cloud Logging as it's an option available out of the box
  logsPolicy: new batch.LogsPolicy({
    destination: batch.LogsPolicy.Destination.CLOUD_LOGGING,
  }),
});
// The job's parent is the project and region in which the job will run
const parent = `projects/${projectId}/locations/${region}`;

async function callCreateBatchGPUJobN1() {
  // Construct request
  const request = {
    parent,
    jobId: jobName,
    job,
  };

  // Run request
  const [response] = await batchClient.createJob(request);
  console.log(JSON.stringify(response));
}

await callCreateBatchGPUJobN1();

Python

from google.cloud import batch_v1


def create_gpu_job(
    project_id: str, region: str, zone: str, job_name: str
) -> batch_v1.Job:
    """
    This method shows how to create a sample Batch Job that will run
    a simple command on Cloud Compute instances on GPU machines.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.
        region: name of the region you want to use to run the job. Regions that are
            available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
        zone: name of the zone you want to use to run the job. Important in regard to GPUs availability.
            GPUs availability can be found here: https://cloud.google.com/compute/docs/gpus/gpu-regions-zones
        job_name: the name of the job that will be created.
            It needs to be unique for each project and region pair.

    Returns:
        A job object representing the job created.
    """
    client = batch_v1.BatchServiceClient()

    # Define what will be done as part of the job.
    task = batch_v1.TaskSpec()
    runnable = batch_v1.Runnable()
    runnable.script = batch_v1.Runnable.Script()
    runnable.script.text = "echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."
    # You can also run a script from a file. Just remember, that needs to be a script that's
    # already on the VM that will be running the job. Using runnable.script.text and runnable.script.path is mutually
    # exclusive.
    # runnable.script.path = '/tmp/test.sh'
    task.runnables = [runnable]

    # We can specify what resources are requested by each task.
    resources = batch_v1.ComputeResource()
    resources.cpu_milli = 2000  # in milliseconds per cpu-second. This means the task requires 2 whole CPUs.
    resources.memory_mib = 16  # in MiB
    task.compute_resource = resources

    task.max_retry_count = 2
    task.max_run_duration = "3600s"

    # Tasks are grouped inside a job using TaskGroups.
    # Currently, it's possible to have only one task group.
    group = batch_v1.TaskGroup()
    group.task_count = 4
    group.task_spec = task

    # Policies are used to define on what kind of virtual machines the tasks will run on.
    # Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
    policy = batch_v1.AllocationPolicy.InstancePolicy()
    policy.machine_type = "n1-standard-16"

    accelerator = batch_v1.AllocationPolicy.Accelerator()
    # Note: not every accelerator is compatible with instance type
    # Read more here: https://cloud.google.com/compute/docs/gpus#t4-gpus
    accelerator.type_ = "nvidia-tesla-t4"
    accelerator.count = 1

    policy.accelerators = [accelerator]
    instances = batch_v1.AllocationPolicy.InstancePolicyOrTemplate()
    instances.policy = policy
    instances.install_gpu_drivers = True
    allocation_policy = batch_v1.AllocationPolicy()
    allocation_policy.instances = [instances]

    location = batch_v1.AllocationPolicy.LocationPolicy()
    location.allowed_locations = ["zones/us-central1-b"]
    allocation_policy.location = location

    job = batch_v1.Job()
    job.task_groups = [group]
    job.allocation_policy = allocation_policy
    job.labels = {"env": "testing", "type": "container"}
    # We use Cloud Logging as it's an out of the box available option
    job.logs_policy = batch_v1.LogsPolicy()
    job.logs_policy.destination = batch_v1.LogsPolicy.Destination.CLOUD_LOGGING

    create_request = batch_v1.CreateJobRequest()
    create_request.job = job
    create_request.job_id = job_name
    # The job's parent is the region in which the job will run
    create_request.parent = f"projects/{project_id}/locations/{region}"

    return client.create_job(create_request)

後續步驟