設定 Gemini Enterprise Agent Platform 無伺服器訓練的容器設定

執行 Gemini Enterprise Agent Platform 無伺服器訓練時,您必須指定要讓 Gemini Enterprise Agent Platform 執行的機器學習 (ML) 程式碼。如要這麼做,請為自訂容器在預先建構的容器上執行的 Python 訓練應用程式,設定訓練容器設定。

如要決定使用自訂容器或預先建構的容器,請參閱訓練程式碼需求

本文說明在上述任一情況下,您必須指定的 Agent Platform API 欄位。

指定容器設定的位置

WorkerPoolSpec 中指定設定詳細資料。視您執行無伺服器訓練的方式而定,請將這個 WorkerPoolSpec 放在下列其中一個 API 欄位中:

如果您執行分散式訓練,可以為每個工作站集區使用不同的設定。

設定容器

視您使用的是預先建構的容器或自訂容器,您必須在 WorkerPoolSpec 中指定不同欄位。選取適用情境的分頁標籤:

預先建構的容器

  1. 選取支援您打算用於訓練的 ML 架構的預先建構容器。在 pythonPackageSpec.executorImageUri 欄位中,指定其中一個容器映像檔的 URI。

  2. pythonPackageSpec.packageUris 欄位中,指定 Python 訓練應用程式的 Cloud Storage URI。

  3. pythonPackageSpec.pythonModule 欄位中指定訓練應用程式的進入點模組

  4. 您也可以在「pythonPackageSpec.args欄位」中,指定要傳遞至訓練應用程式進入點模組的指令列引數清單。

下列範例會標示出建立 CustomJob 時指定這些容器設定的位置:

控制台

在 Google Cloud 控制台中,您無法直接建立 CustomJob。不過,您可以建立 TrainingPipeline,藉此建立 CustomJob。在 Google Cloud 控制台中建立 TrainingPipeline 時,您可以在「訓練容器」步驟的特定欄位中指定預先建構的容器設定:

  • pythonPackageSpec.executorImageUri:使用「模型架構」和「模型架構版本」下拉式清單。

  • pythonPackageSpec.packageUris:使用「套件位置」欄位。

  • pythonPackageSpec.pythonModule:使用「Python module」(Python 模組) 欄位。

  • pythonPackageSpec.args:使用「Arguments」欄位。

gcloud

gcloud ai custom-jobs create \
  --region=LOCATION \
  --display-name=JOB_NAME \
  --python-package-uris=PYTHON_PACKAGE_URIS \
  --worker-pool-spec=machine-type=MACHINE_TYPE,replica-count=REPLICA_COUNT,executor-image-uri=PYTHON_PACKAGE_EXECUTOR_IMAGE_URI,python-module=PYTHON_MODULE

如需更多背景資訊,請參閱建立CustomJob的指南。

自訂容器

  1. containerSpec.imageUri 欄位中,指定自訂容器的 Artifact Registry 或 Docker Hub URI。

  2. 如要覆寫容器中的 ENTRYPOINTCMD 指令,請指定 containerSpec.commandcontainerSpec.args 欄位。 這些欄位會根據下列規則影響容器的執行方式:

    • 如果兩個欄位都未指定:容器會根據 ENTRYPOINT 指令和 CMD 指令 (如有) 執行。請參閱 Docker 說明文件,瞭解 CMDENTRYPOINT 如何互動

    • 如果只指定 containerSpec.command容器會執行 containerSpec.command 值,取代其 ENTRYPOINT 指示。如果容器有 CMD 指令,系統會忽略該指令。

    • 如果只指定 containerSpec.args容器會根據 ENTRYPOINT 指示執行,且 containerSpec.args 的值會取代 CMD 指示。

    • 如果同時指定這兩個欄位:容器會執行,並以 containerSpec.command 取代 ENTRYPOINT 指令,以及以 containerSpec.args 取代 CMD 指令。

以下範例著重說明建立 CustomJob 時,可指定部分容器設定的位置:

控制台

在 Google Cloud 控制台中,您無法直接建立 CustomJob。不過,您可以建立 TrainingPipeline,藉此建立 CustomJob。在 Google Cloud 控制台中建立TrainingPipeline時,您可以在「訓練容器」步驟的特定欄位中指定自訂容器設定:

  • containerSpec.imageUri:使用「容器映像檔」欄位。

  • containerSpec.command:這個 API 欄位無法在Google Cloud 控制台中設定。

  • containerSpec.args:使用「Arguments」欄位。

gcloud

gcloud ai custom-jobs create \
  --region=LOCATION \
  --display-name=JOB_NAME \
  --worker-pool-spec=machine-type=MACHINE_TYPE,replica-count=REPLICA_COUNT,container-image-uri=CUSTOM_CONTAINER_IMAGE_URI

Java

在試用這個範例之前,請先按照「使用用戶端程式庫的 Agent Platform 快速入門導覽課程」中的 Java 設定說明操作。詳情請參閱 Agent Platform Java API 參考文件

如要向 Agent Platform 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。


import com.google.cloud.aiplatform.v1.AcceleratorType;
import com.google.cloud.aiplatform.v1.ContainerSpec;
import com.google.cloud.aiplatform.v1.CustomJob;
import com.google.cloud.aiplatform.v1.CustomJobSpec;
import com.google.cloud.aiplatform.v1.JobServiceClient;
import com.google.cloud.aiplatform.v1.JobServiceSettings;
import com.google.cloud.aiplatform.v1.LocationName;
import com.google.cloud.aiplatform.v1.MachineSpec;
import com.google.cloud.aiplatform.v1.WorkerPoolSpec;
import java.io.IOException;

// Create a custom job to run machine learning training code in Vertex AI
public class CreateCustomJobSample {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String project = "PROJECT";
    String displayName = "DISPLAY_NAME";

    // Vertex AI runs your training application in a Docker container image. A Docker container
    // image is a self-contained software package that includes code and all dependencies. Learn
    // more about preparing your training application at
    // https://cloud.google.com/vertex-ai/docs/training/overview#prepare_your_training_application
    String containerImageUri = "CONTAINER_IMAGE_URI";
    createCustomJobSample(project, displayName, containerImageUri);
  }

  static void createCustomJobSample(String project, String displayName, String containerImageUri)
      throws IOException {
    JobServiceSettings settings =
        JobServiceSettings.newBuilder()
            .setEndpoint("us-central1-aiplatform.googleapis.com:443")
            .build();
    String location = "us-central1";

    // 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 (JobServiceClient client = JobServiceClient.create(settings)) {
      MachineSpec machineSpec =
          MachineSpec.newBuilder()
              .setMachineType("n1-standard-4")
              .setAcceleratorType(AcceleratorType.NVIDIA_TESLA_T4)
              .setAcceleratorCount(1)
              .build();

      ContainerSpec containerSpec =
          ContainerSpec.newBuilder().setImageUri(containerImageUri).build();

      WorkerPoolSpec workerPoolSpec =
          WorkerPoolSpec.newBuilder()
              .setMachineSpec(machineSpec)
              .setReplicaCount(1)
              .setContainerSpec(containerSpec)
              .build();

      CustomJobSpec customJobSpecJobSpec =
          CustomJobSpec.newBuilder().addWorkerPoolSpecs(workerPoolSpec).build();

      CustomJob customJob =
          CustomJob.newBuilder()
              .setDisplayName(displayName)
              .setJobSpec(customJobSpecJobSpec)
              .build();
      LocationName parent = LocationName.of(project, location);
      CustomJob response = client.createCustomJob(parent, customJob);
      System.out.format("response: %s\n", response);
      System.out.format("Name: %s\n", response.getName());
    }
  }
}

Node.js

在試用這個範例之前,請先按照「使用用戶端程式庫的 Agent Platform 快速入門導覽課程」中的 Node.js 設定說明操作。詳情請參閱 Agent Platform Node.js API 參考文件

如要向 Agent Platform 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。

/**
 * TODO(developer): Uncomment these variables before running the sample.\
 * (Not necessary if passing values as arguments)
 */

// const customJobDisplayName = 'YOUR_CUSTOM_JOB_DISPLAY_NAME';
// const containerImageUri = 'YOUR_CONTAINER_IMAGE_URI';
// const project = 'YOUR_PROJECT_ID';
// const location = 'YOUR_PROJECT_LOCATION';

// Imports the Google Cloud Job Service Client library
const {JobServiceClient} = require('@google-cloud/aiplatform');

// Specifies the location of the api endpoint
const clientOptions = {
  apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};

// Instantiates a client
const jobServiceClient = new JobServiceClient(clientOptions);

async function createCustomJob() {
  // Configure the parent resource
  const parent = `projects/${project}/locations/${location}`;
  const customJob = {
    displayName: customJobDisplayName,
    jobSpec: {
      workerPoolSpecs: [
        {
          machineSpec: {
            machineType: 'n1-standard-4',
            acceleratorType: 'NVIDIA_TESLA_T4',
            acceleratorCount: 1,
          },
          replicaCount: 1,
          containerSpec: {
            imageUri: containerImageUri,
            command: [],
            args: [],
          },
        },
      ],
    },
  };
  const request = {parent, customJob};

  // Create custom job request
  const [response] = await jobServiceClient.createCustomJob(request);

  console.log('Create custom job response:\n', JSON.stringify(response));
}
createCustomJob();

Python

如要瞭解如何安裝或更新 Vertex AI SDK for Python,請參閱「安裝 Vertex AI SDK for Python」。 詳情請參閱 Python API 參考文件

from google.cloud import aiplatform


def create_custom_job_sample(
    project: str,
    display_name: str,
    container_image_uri: str,
    location: str = "us-central1",
    api_endpoint: str = "us-central1-aiplatform.googleapis.com",
):
    # The AI Platform services require regional API endpoints.
    client_options = {"api_endpoint": api_endpoint}
    # Initialize client that will be used to create and send requests.
    # This client only needs to be created once, and can be reused for multiple requests.
    client = aiplatform.gapic.JobServiceClient(client_options=client_options)
    custom_job = {
        "display_name": display_name,
        "job_spec": {
            "worker_pool_specs": [
                {
                    "machine_spec": {
                        "machine_type": "n1-standard-4",
                        "accelerator_type": aiplatform.gapic.AcceleratorType.NVIDIA_TESLA_K80,
                        "accelerator_count": 1,
                    },
                    "replica_count": 1,
                    "container_spec": {
                        "image_uri": container_image_uri,
                        "command": [],
                        "args": [],
                    },
                }
            ]
        },
    }
    parent = f"projects/{project}/locations/{location}"
    response = client.create_custom_job(parent=parent, custom_job=custom_job)
    print("response:", response)

如需更多背景資訊,請參閱建立CustomJob的指南。

後續步驟