【问题标题】:How to pass environment variables to gcloud beta ai custom-jobs create with custom container (Vertex AI)如何将环境变量传递给使用自定义容器(Vertex AI)创建的 gcloud beta ai 自定义作业
【发布时间】:2021-11-17 00:01:41
【问题描述】:

我正在谷歌的 Vertex AI 中运行自定义训练作业。执行自定义作业的简单 gcloud 命令将使用类似于以下语法的内容(可以查看该命令的完整文档 here):

gcloud beta ai custom-jobs create --region=us-central1 \
--display-name=test \
--config=config.yaml

config.yaml 文件中,可以指定机器和加速器 (GPU) 类型等,在我的例子中,指向一个位于 Google Artifact Registry 中的自定义容器,该容器执行训练代码(指定在containerSpecimageUri 部分)。示例配置文件可能如下所示:

# config.yaml
workerPoolSpecs:
  machineSpec:
    machineType: n1-highmem-2
    acceleratorType: NVIDIA_TESLA_P100
    acceleratorCount: 2
  replicaCount: 1
  containerSpec:
    imageUri: {URI_FOR_CUSTOM_CONATINER}
    args:
    - {ARGS TO PASS TO CONTAINER ENTRYPOINT COMMAND}

我们正在运行的代码需要将一些运行时环境变量(需要安全)传递给容器。在API documentationcontainerSpec 中,它说可以设置环境变量如下:

# config.yaml
workerPoolSpecs:
  machineSpec:
    machineType: n1-highmem-2
    acceleratorType: NVIDIA_TESLA_P100
    acceleratorCount: 2
  replicaCount: 1
  containerSpec:
    imageUri: {URI_FOR_CUSTOM_CONATINER}
    args:
    - {ARGS TO PASS TO CONTAINER ENTRYPOINT COMMAND}
    env:
    - name: SECRET_ONE
      value: $SECRET_ONE
    - name: SECRET_TWO
      value: $SECRET_TWO

当我尝试将 env 标志添加到 containerSpec 时,我收到一条错误消息,指出它不是容器规范的一部分:

ERROR: (gcloud.beta.ai.custom-jobs.create) INVALID_ARGUMENT: Invalid JSON payload received. Unknown name "env" at 'custom_job.job_spec.worker_pool_specs[0].container_spec': Cannot find field.
- '@type': type.googleapis.com/google.rpc.BadRequest
  fieldViolations:
  - description: "Invalid JSON payload received. Unknown name \"env\" at 'custom_job.job_spec.worker_pool_specs[0].container_spec':\
      \ Cannot find field."
    field: custom_job.job_spec.worker_pool_specs[0].container_spec

知道如何使用自定义容器在 Vertex AI 自定义作业中安全地设置运行时环境变量吗?

【问题讨论】:

  • 我认为这是ContainerSpec 文档或其实现中的错误。你的方法是正确的,你应该能够像你一样定义环境变量。我建议在 Google 的 Issue Tracker 上为 Cloud Machine Learning Engine 提交一个错误
  • 谢谢,我会试一试
  • @JmeCS 你能试试不带beta 参数的gcloud 命令吗? REST API 有两个版本——v1v1beta1,其中“v1beta1”在ContainerSpec 中没有env 选项,但“v1”有。 gcloud ai custom-jobs create 不会抛出错误。
  • @KabilanMohanraj - 谢谢,我不知道 API 有非测试版。你是对的,“v1”规范不会引发错误!您是否知道是否可以从启动作业的机器中获取环境变量?我尝试在 yaml 中设置值,例如:${var},但它不起作用

标签: docker google-cloud-platform gcloud google-cloud-sdk google-cloud-vertex-ai


【解决方案1】:

REST API 有两个版本——“v1”和“v1beta1”,其中“v1beta1”在ContainerSpec 中没有env 选项,但“v1”有。不带 beta 参数的 gcloud ai custom-jobs create 命令不会引发错误,因为它使用版本“v1”进行 API 调用。

yaml 文件中的环境变量可以通过以下方式传递给自定义容器:

这是我用来测试需求的示例自定义训练应用程序的 docker 文件。有关培训应用程序的更多信息,请参阅此codelab

FROM gcr.io/deeplearning-platform-release/tf2-cpu.2-3
WORKDIR /root

WORKDIR /

# Copies the trainer code to the docker image.
COPY trainer /trainer


# Copies the bash script to the docker image.
COPY commands.sh /scripts/commands.sh

# Bash command to make the script file an executable
RUN ["chmod", "+x", "/scripts/commands.sh"]


# Command to execute the file
ENTRYPOINT ["/scripts/commands.sh"]

# Sets up the entry point to invoke the trainer.
# ENTRYPOINT "python" "-m" $SECRET_TWO ⇒ To use the environment variable  
# directly in the docker ENTRYPOINT. In case you are not using a bash script, 
# the trainer can be invoked directly from the docker ENTRYPOINT.

下面是docker容器中使用的commands.sh文件,用于测试环境变量是否传递给容器。

#!/bin/bash
mkdir /root/.ssh
echo $SECRET_ONE
python -m $SECRET_TWO

config.yaml 文件示例

# config.yaml
workerPoolSpecs:
  machineSpec:
    machineType: n1-highmem-2
  replicaCount: 1
  containerSpec:
    imageUri: gcr.io/infosys-kabilan/mpg:v1
    env:
    - name: SECRET_ONE
      value: "Passing the environment variables"
    - name: SECRET_TWO
      value: "trainer.train"

下一步,我构建了容器并将其推送到 Google Container Repository。现在,可以运行gcloud ai custom-jobs create --region=us-central1 --display-name=test --config=config.yaml 来创建自定义训练作业,并且可以在作业日志中看到commands.sh 文件的输出,如下所示。

【讨论】:

    猜你喜欢
    • 2021-12-24
    • 2021-12-28
    • 1970-01-01
    • 2021-08-27
    • 2022-10-15
    • 2022-10-06
    • 2021-12-26
    • 2022-08-23
    • 2022-10-06
    相关资源
    最近更新 更多