管理已排队的资源

借助已排队的资源,您可以按排队方式请求 Cloud TPU 资源。 在您请求已排队的资源时,请求会被添加到由 Cloud TPU 服务维护的队列中。请求的资源可用后,会分配给您的 Google Cloud 项目,供您立即专门使用。除非您将该资源删除或被抢占,否则该资源仍会分配给您的项目。只有 TPU Spot 虚拟机和抢占式 TPU 才符合抢占条件。

您可以在已排队的资源请求中指定可选的开始时间和结束时间。开始时间用于指定完成请求的最早时间。如果请求未在指定的结束时间之前完成,则请求会失效。请求在失效后仍会保留在队列中。

已排队的资源请求可以处于以下任一状态:

WAITING_FOR_RESOURCES
请求已通过初始验证,并已添加到队列中。在有足够的可用资源可开始预配您的请求或分配时间间隔到期之前,请求会保持此状态。当需求较高时,并非所有请求都可以立即预配。如果您需要更可靠的 TPU 可获取性,请考虑购买预留。
PROVISIONING
已从队列中选择请求,并且正在分配其资源。
ACTIVE
请求已分配。当已排队的资源请求处于 ACTIVE 状态时,您可以按照管理 TPU 中所述管理 TPU 虚拟机。
FAILED
请求无法完成,原因是请求存在问题,或者请求的资源在分配时间间隔内不可用。请求会保留在队列中,直到它被明确删除为止。
SUSPENDING
与请求关联的资源正在被删除。
SUSPENDED
请求中指定的资源已被删除。当请求处于 SUSPENDED 状态时,就不再符合进一步分配的条件。

前提条件

在运行本指南中的命令之前,您必须安装 Google Cloud CLI、创建 Google Cloud 项目并启用 Cloud TPU API。如需查看相关说明,请参阅设置 Cloud TPU 环境

如果您使用的是某个 Cloud 客户端库,请按照适用于您所用语言的设置说明执行操作。

请求按需排队的资源

按需资源不会被抢占,但按需配额不能保证有足够的可用 Cloud TPU 资源来满足您的请求。如需详细了解按需资源,请参阅配额类型

gcloud

gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project-id \
    --zone us-central1-a \
    --accelerator-type v5litepod-8 \
    --runtime-version v2-alpha-tpuv5-lite

命令参数说明

queued-resource-id
已排队的资源请求的用户分配 ID。
node-id
用户分配的 TPU ID,该 ID 是在分配已排队的资源请求时创建的。
project
您的 Google Cloud 项目。
zone
拟在其中创建 Cloud TPU 的可用区
accelerator-type
加速器类型用于指定您要创建的 Cloud TPU 的版本和大小。如需详细了解每个 TPU 版本支持的加速器类型,请参阅 TPU 版本
runtime-version
Cloud TPU 软件版本

curl

curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
-d "{
    'tpu': {
    'node_spec': {
        'parent': 'projects/your-project-number/locations/us-central1-a',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v5litepod-8',
        'runtime_version': 'v2-alpha-tpuv5-lite',
        }
    }
    }
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-central1-a/queuedResources?queued_resource_id=your-queued-resource-id

命令参数说明

queued-resource-id
已排队的资源请求的用户分配 ID。
node-id
用户分配的 TPU ID,该 ID 是在分配已排队的资源请求时创建的。
project
您的 Google Cloud 项目。
zone
拟在其中创建 Cloud TPU 的可用区
accelerator-type
加速器类型用于指定您要创建的 Cloud TPU 的版本和大小。如需详细了解每个 TPU 版本支持的加速器类型,请参阅 TPU 版本
runtime-version
Cloud TPU 软件版本

控制台

  1. 在 Google Cloud 控制台中,前往 TPU 页面:

    前往 TPU

  2. 点击创建 TPU

  3. 名称字段中,输入 TPU 的名称。

  4. 可用区框中,选择您要在其中创建 TPU 的可用区。

  5. TPU 类型框中,选择加速器类型。加速器类型用于指定您要创建的 Cloud TPU 的版本和大小。如需详细了解每个 TPU 版本支持的加速器类型,请参阅 TPU 版本

  6. TPU 软件版本框中,选择软件版本。创建 Cloud TPU 虚拟机时,TPU 软件版本用于指定要安装的 TPU 运行时的版本。如需了解详情,请参阅 TPU 软件版本

  7. 点击启用排队切换开关。

  8. 已排队资源的名称字段中,输入已排队的资源请求的名称。

  9. 点击创建以创建已排队的资源请求。

Java

如需向 Cloud TPU 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证

import com.google.cloud.tpu.v2alpha1.CreateQueuedResourceRequest;
import com.google.cloud.tpu.v2alpha1.Node;
import com.google.cloud.tpu.v2alpha1.QueuedResource;
import com.google.cloud.tpu.v2alpha1.TpuClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateQueuedResource {
  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 create a node.
    String projectId = "YOUR_PROJECT_ID";
    // The zone in which to create the TPU.
    // For more information about supported TPU types for specific zones,
    // see https://cloud.google.com/tpu/docs/regions-zones
    String zone = "us-central1-a";
    // The name for your TPU.
    String nodeName = "YOUR_NODE_ID";
    // The accelerator type that specifies the version and size of the Cloud TPU you want to create.
    // For more information about supported accelerator types for each TPU version,
    // see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
    String tpuType = "v5litepod-4";
    // Software version that specifies the version of the TPU runtime to install.
    // For more information see https://cloud.google.com/tpu/docs/runtimes
    String tpuSoftwareVersion = "v2-tpuv5-litepod";
    // The name for your Queued Resource.
    String queuedResourceId = "QUEUED_RESOURCE_ID";

    createQueuedResource(
        projectId, zone, queuedResourceId, nodeName, tpuType, tpuSoftwareVersion);
  }

  // Creates a Queued Resource
  public static QueuedResource createQueuedResource(String projectId, String zone,
      String queuedResourceId, String nodeName, String tpuType, String tpuSoftwareVersion)
          throws IOException, ExecutionException, InterruptedException, TimeoutException {
    String resource = String.format("projects/%s/locations/%s/queuedResources/%s",
            projectId, zone, queuedResourceId);
    // 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 (TpuClient tpuClient = TpuClient.create()) {
      String parent = String.format("projects/%s/locations/%s", projectId, zone);
      Node node =
          Node.newBuilder()
              .setName(nodeName)
              .setAcceleratorType(tpuType)
              .setRuntimeVersion(tpuSoftwareVersion)
              .setQueuedResource(resource)
              .build();

      QueuedResource queuedResource =
          QueuedResource.newBuilder()
              .setName(queuedResourceId)
              .setTpu(
                  QueuedResource.Tpu.newBuilder()
                      .addNodeSpec(
                          QueuedResource.Tpu.NodeSpec.newBuilder()
                              .setParent(parent)
                              .setNode(node)
                              .setNodeId(nodeName)
                              .build())
                      .build())
              .build();

      CreateQueuedResourceRequest request =
          CreateQueuedResourceRequest.newBuilder()
              .setParent(parent)
              .setQueuedResourceId(queuedResourceId)
              .setQueuedResource(queuedResource)
              .build();

      return tpuClient.createQueuedResourceAsync(request).get(1, TimeUnit.MINUTES);
    }
  }
}

Python

如需向 Cloud TPU 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证

from google.cloud import tpu_v2alpha1

# TODO(developer): Update and un-comment below lines
# project_id = "your-project-id"
# zone = "us-central1-a"
# tpu_name = "tpu-name"
# tpu_type = "v5litepod-4"
# runtime_version = "v2-tpuv5-litepod"
# queued_resource_name = "resource-name"

node = tpu_v2alpha1.Node()
node.accelerator_type = tpu_type
# To see available runtime version use command:
# gcloud compute tpus versions list --zone={ZONE}
node.runtime_version = runtime_version

node_spec = tpu_v2alpha1.QueuedResource.Tpu.NodeSpec()
node_spec.parent = f"projects/{project_id}/locations/{zone}"
node_spec.node_id = tpu_name
node_spec.node = node

resource = tpu_v2alpha1.QueuedResource()
resource.tpu = tpu_v2alpha1.QueuedResource.Tpu(node_spec=[node_spec])

request = tpu_v2alpha1.CreateQueuedResourceRequest(
    parent=f"projects/{project_id}/locations/{zone}",
    queued_resource_id=queued_resource_name,
    queued_resource=resource,
)

client = tpu_v2alpha1.TpuClient()
operation = client.create_queued_resource(request=request)

response = operation.result()
print(response.name)
print(response.state)
# Example response:
# projects/[project_id]/locations/[zone]/queuedResources/resource-name
# State.WAITING_FOR_RESOURCES

使用预留请求已排队的资源

您可以使用预留请求已排队的资源。如需购买预留,请与您的 Google Cloud 客户支持团队联系。

gcloud

gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project-id \
    --zone us-central1-a \
    --accelerator-type v5litepod-8 \
    --runtime-version v2-alpha-tpuv5-lite \
    --reserved

命令参数说明

queued-resource-id
已排队的资源请求的用户分配 ID。
node-id
用户分配的 TPU ID,该 ID 是在分配已排队的资源请求时创建的。
project
您的 Google Cloud 项目。
zone
拟在其中创建 Cloud TPU 的可用区
accelerator-type
加速器类型用于指定您要创建的 Cloud TPU 的版本和大小。如需详细了解每个 TPU 版本支持的加速器类型,请参阅 TPU 版本
runtime-version
Cloud TPU 软件版本
reserved
在请求已排队的资源作为 Cloud TPU 预留的一部分时使用此标志。

curl

curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
-d "{
    'tpu': {
    'node_spec': {
        'parent': 'projects/your-project-number/locations/us-central1-a',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v5litepod-8',
        'runtime_version': 'v2-alpha-tpuv5-lite',
        }
    }
    },
    'guaranteed': {
    'reserved': true,
    }
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-central1-a/queuedResources?queued_resource_id=your-queued-resource-id

命令参数说明

queued-resource-id
已排队的资源请求的用户分配 ID。
node-id
用户分配的 TPU ID,该 ID 是在分配已排队的资源请求时创建的。
project
您的 Google Cloud 项目。
zone
拟在其中创建 Cloud TPU 的可用区
accelerator-type
加速器类型用于指定您要创建的 Cloud TPU 的版本和大小。如需详细了解每个 TPU 版本支持的加速器类型,请参阅 TPU 版本
runtime-version
Cloud TPU 软件版本
reserved
在请求已排队的资源作为 Cloud TPU 预留的一部分时使用此标志。

控制台

  1. 在 Google Cloud 控制台中,前往 TPU 页面:

    前往 TPU

  2. 点击创建 TPU

  3. 名称字段中,输入 TPU 的名称。

  4. 可用区框中,选择您要在其中创建 TPU 的可用区。

  5. TPU 类型框中,选择加速器类型。加速器类型用于指定您要创建的 Cloud TPU 的版本和大小。如需详细了解每个 TPU 版本支持的加速器类型,请参阅 TPU 版本

  6. TPU 软件版本框中,选择软件版本。创建 Cloud TPU 虚拟机时,TPU 软件版本用于指定要安装的 TPU 运行时的版本。如需了解详情,请参阅 TPU 软件版本

  7. 点击启用排队切换开关。

  8. 已排队资源的名称字段中,输入已排队的资源请求的名称。

  9. 展开管理部分。

  10. 选中使用现有预留复选框。

  11. 点击创建以创建已排队的资源请求。

请求 TPU Spot 虚拟机已排队的资源

Spot 虚拟机是一种资源,可以随时抢占并分配给其他工作负载。与非 Spot 虚拟机请求相比,Spot 虚拟机资源的费用更低,并且您可能更快地访问资源。如需详细了解 TPU Spot 虚拟机,请参阅管理 TPU Spot 虚拟机

gcloud

gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project-id \
    --zone us-central1-a \
    --accelerator-type v5litepod-8 \
    --runtime-version v2-alpha-tpuv5-lite \
    --spot

命令参数说明

queued-resource-request-id
已排队的资源请求的用户分配 ID。
node-id
:响应请求而创建的 TPU 的用户定义 ID。
project
:已排队的资源分配到的项目的 ID。
zone
拟在其中创建 Cloud TPU 的可用区
accelerator-type
加速器类型用于指定您要创建的 Cloud TPU 的版本和大小。如需详细了解每个 TPU 版本支持的加速器类型,请参阅 TPU 版本
runtime-version
Cloud TPU 软件版本
spot
一个布尔值标志,用于指定已排队的资源是 Spot 虚拟机。

curl

curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
-d "{
    'tpu': {
    'node_spec': {
        'parent': 'projects/your-project-number/locations/us-central1-a',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v5litepod-8',
        'runtime_version': 'v2-alpha-tpuv5-lite'
        }
    }
    },
    'spot': {}
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-central1-a/queuedResources?queued_resource_id=your-queued-resource-id

命令参数说明

queued-resource-request-id
已排队的资源请求的用户分配 ID。
node-id
:响应请求而创建的 TPU 的用户定义 ID。
project
:已排队的资源分配到的项目的 ID。
zone
拟在其中创建 Cloud TPU 的可用区
accelerator-type
加速器类型用于指定您要创建的 Cloud TPU 的版本和大小。如需详细了解每个 TPU 版本支持的加速器类型,请参阅 TPU 版本
runtime-version
Cloud TPU 软件版本
spot
一个布尔值标志,用于指定已排队的资源是 Spot 虚拟机。

控制台

  1. 在 Google Cloud 控制台中,前往 TPU 页面:

    前往 TPU

  2. 点击创建 TPU

  3. 名称字段中,输入 TPU 的名称。

  4. 可用区框中,选择您要在其中创建 TPU 的可用区。

  5. TPU 类型框中,选择加速器类型。加速器类型用于指定您要创建的 Cloud TPU 的版本和大小。如需详细了解每个 TPU 版本支持的加速器类型,请参阅 TPU 版本

  6. TPU 软件版本框中,选择软件版本。创建 Cloud TPU 虚拟机时,TPU 软件版本用于指定要安装的 TPU 运行时的版本。如需了解详情,请参阅 TPU 软件版本

  7. 点击启用排队切换开关。

  8. 已排队资源的名称字段中,输入已排队的资源请求的名称。

  9. 展开管理部分。

  10. 选中将此项设置为 TPU Spot 虚拟机复选框。

  11. 点击创建

Java

如需向 Cloud TPU 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证

import com.google.cloud.tpu.v2alpha1.CreateQueuedResourceRequest;
import com.google.cloud.tpu.v2alpha1.Node;
import com.google.cloud.tpu.v2alpha1.QueuedResource;
import com.google.cloud.tpu.v2alpha1.SchedulingConfig;
import com.google.cloud.tpu.v2alpha1.TpuClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class CreateSpotQueuedResource {
  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Google Cloud project you want to create a node.
    String projectId = "YOUR_PROJECT_ID";
    // The zone in which to create the TPU.
    // For more information about supported TPU types for specific zones,
    // see https://cloud.google.com/tpu/docs/regions-zones
    String zone = "us-central1-a";
    // The name for your TPU.
    String nodeName = "YOUR_TPU_NAME";
    // The accelerator type that specifies the version and size of the Cloud TPU you want to create.
    // For more information about supported accelerator types for each TPU version,
    // see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
    String tpuType = "v5litepod-4";
    // Software version that specifies the version of the TPU runtime to install.
    // For more information see https://cloud.google.com/tpu/docs/runtimes
    String tpuSoftwareVersion = "v2-tpuv5-litepod";
    // The name for your Queued Resource.
    String queuedResourceId = "QUEUED_RESOURCE_ID";

    createQueuedResource(
        projectId, zone, queuedResourceId, nodeName, tpuType, tpuSoftwareVersion);
  }

  // Creates a Queued Resource with --preemptible flag.
  public static QueuedResource createQueuedResource(
      String projectId, String zone, String queuedResourceId,
      String nodeName, String tpuType, String tpuSoftwareVersion)
      throws IOException, ExecutionException, InterruptedException {
    // 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 (TpuClient tpuClient = TpuClient.create()) {
      String parent = String.format("projects/%s/locations/%s", projectId, zone);
      String resourceName = String.format("projects/%s/locations/%s/queuedResources/%s",
              projectId, zone, queuedResourceId);
      SchedulingConfig schedulingConfig = SchedulingConfig.newBuilder()
          .setPreemptible(true)
          .build();

      Node node =
          Node.newBuilder()
              .setName(nodeName)
              .setAcceleratorType(tpuType)
              .setRuntimeVersion(tpuSoftwareVersion)
              .setSchedulingConfig(schedulingConfig)
              .setQueuedResource(resourceName)
              .build();

      QueuedResource queuedResource =
          QueuedResource.newBuilder()
              .setName(queuedResourceId)
              .setTpu(
                  QueuedResource.Tpu.newBuilder()
                      .addNodeSpec(
                          QueuedResource.Tpu.NodeSpec.newBuilder()
                              .setParent(parent)
                              .setNode(node)
                              .setNodeId(nodeName)
                              .build())
                      .build())
              .build();

      CreateQueuedResourceRequest request =
          CreateQueuedResourceRequest.newBuilder()
              .setParent(parent)
              .setQueuedResourceId(queuedResourceId)
              .setQueuedResource(queuedResource)
              .build();

      return tpuClient.createQueuedResourceAsync(request).get();
    }
  }
}

Python

如需向 Cloud TPU 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证

from google.cloud import tpu_v2alpha1

# TODO(developer): Update and un-comment below lines
# project_id = "your-project-id"
# zone = "us-central1-a"
# tpu_name = "tpu-name"
# tpu_type = "v5litepod-4"
# runtime_version = "v2-tpuv5-litepod"
# queued_resource_name = "resource-name"

node = tpu_v2alpha1.Node()
node.accelerator_type = tpu_type
# To see available runtime version use command:
# gcloud compute tpus versions list --zone={ZONE}
node.runtime_version = runtime_version

node_spec = tpu_v2alpha1.QueuedResource.Tpu.NodeSpec()
node_spec.parent = f"projects/{project_id}/locations/{zone}"
node_spec.node_id = tpu_name
node_spec.node = node

resource = tpu_v2alpha1.QueuedResource()
resource.tpu = tpu_v2alpha1.QueuedResource.Tpu(node_spec=[node_spec])
# Create a spot resource
resource.spot = tpu_v2alpha1.QueuedResource.Spot()

request = tpu_v2alpha1.CreateQueuedResourceRequest(
    parent=f"projects/{project_id}/locations/{zone}",
    queued_resource_id=queued_resource_name,
    queued_resource=resource,
)

client = tpu_v2alpha1.TpuClient()
operation = client.create_queued_resource(request=request)
response = operation.result()

print(response.name)
print(response.state)
# Example response:
# projects/[project_id]/locations/[zone]/queuedResources/resource-name
# State.WAITING_FOR_RESOURCES

请求在指定时间之前或之后分配已排队的资源

您可以在已排队的资源请求中指定可选的开始时间结束时间。开始时间或开始时长用于指定完成请求的最早时间。结束时间或结束时长用于指定请求保持有效的时长。如果请求未在指定的结束时间之前或指定的时长内完成,则请求会失效。请求失效后,会保留在队列中,但不再符合分配条件。

您还可以通过指定开始时间或时长以及结束时间或时长来指定分配时间间隔

如需查看支持的时间戳和时长格式列表,请参阅日期时间

请求在指定的时间后执行已排队的资源

在已排队的资源请求中,您可以指定在多久时间或时长之后应分配资源。

gcloud

以下命令请求在 2022 年 12 月 14 日上午 9 点之后分配 v5p-4096 TPU。

gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project-id \
    --zone us-east5-a \
    --accelerator-type v5p-4096 \
    --runtime-version v2-alpha-tpuv5 \
    --valid-after-time 2022-12-14T09:00:00Z

命令参数说明

queued-resource-request-id
已排队的资源请求的用户分配 ID。
node-id
:响应请求而创建的 TPU 的用户定义 ID。
project
:已排队的资源分配到的 Google Cloud 项目。
zone
拟在其中创建 Cloud TPU 的可用区
accelerator-type
加速器类型用于指定您要创建的 Cloud TPU 的版本和大小。如需详细了解每个 TPU 版本支持的加速器类型,请参阅 TPU 版本
runtime-version
Cloud TPU 软件版本
valid-after-time
在什么时间之后应分配资源。如需详细了解时长格式,请参阅 Google Cloud CLI 主题日期时间

curl

以下命令请求在 2022 年 12 月 14 日上午 9 点之后分配 v5p-4096 TPU。

curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
-d "{
    'tpu': {
    'node_spec': {
        'parent': 'projects/your-project-number/locations/us-east5-a',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v5p-4096',
        'runtime_version': 'v2-alpha-tpuv5',
        }
    }
    },
    'queueing_policy': {
    'valid_after_time': {
        'seconds': 2022-12-14T09:00:00Z
    }
    }
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-east5-a/queuedResources?queued_resource_id=your-queued-resource-id

命令参数说明

queued-resource-request-id
已排队的资源请求的用户分配 ID。
node-id
:响应请求而创建的 TPU 的用户定义 ID。
project
:已排队的资源分配到的 Google Cloud 项目。
zone
拟在其中创建 Cloud TPU 的可用区
accelerator-type
加速器类型用于指定您要创建的 Cloud TPU 的版本和大小。如需详细了解每个 TPU 版本支持的加速器类型,请参阅 TPU 版本
runtime-version
Cloud TPU 软件版本
valid-after-time
在多久时间之后应分配资源。如需详细了解时长格式,请参阅 Google Cloud CLI 主题日期时间

控制台

  1. 在 Google Cloud 控制台中,前往 TPU 页面:

    前往 TPU

  2. 点击创建 TPU

  3. 名称字段中,输入 TPU 的名称。

  4. 可用区框中,选择您要在其中创建 TPU 的可用区。

  5. TPU 类型框中,选择加速器类型。加速器类型用于指定您要创建的 Cloud TPU 的版本和大小。如需详细了解每个 TPU 版本支持的加速器类型,请参阅 TPU 版本

  6. TPU 软件版本框中,选择软件版本。创建 Cloud TPU 虚拟机时,TPU 软件版本用于指定要安装的 TPU 运行时的版本。如需了解详情,请参阅 TPU 软件版本

  7. 点击启用排队切换开关。

  8. 已排队资源的名称字段中,输入已排队的资源请求的名称。

  9. 请求开始时间字段中,输入在多久时间之后应分配资源。

  10. 点击创建以创建已排队的资源请求。

以下示例请求在六小时后分配 v5p-32。

gcloud

    gcloud compute tpus queued-resources create your-queued-resource-id \
        --node-id your-node-id \
        --project your-project-id \
        --zone us-east5-a \
        --accelerator-type v5p-32 \
        --runtime-version v2-alpha-tpuv5 \
        --valid-after-duration 6h

命令参数说明

queued-resource-request-id
已排队的资源请求的用户分配 ID。
node-id
:响应请求而创建的 TPU 的用户定义 ID。
project
:已排队的资源分配到的 Google Cloud 项目。
zone
拟在其中创建 Cloud TPU 的可用区
accelerator-type
加速器类型用于指定您要创建的 Cloud TPU 的版本和大小。如需详细了解每个 TPU 版本支持的加速器类型,请参阅 TPU 版本
runtime-version
Cloud TPU 软件版本
valid-after-duration
在多久时长之前不得预配 TPU。如需详细了解时长格式,请参阅 Google Cloud CLI 主题日期时间

curl

curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
-d "{
    'tpu': {
    'node_spec': {
        'parent': 'projects/your-project-number/locations/us-east5-a',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v5p-32',
        'runtime_version': 'v2-alpha-tpuv5',
        }
    }
    },
'queueing_policy': {
    'valid_after_duration': {
        'seconds': 21600
    }
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-east5-a/queuedResources?queued_resource_id=your-queued-resource-id

命令参数说明

queued-resource-request-id
已排队的资源请求的用户分配 ID。
node-id
:响应请求而创建的 TPU 的用户定义 ID。
project
:已排队的资源分配到的 Google Cloud 项目。
zone
拟在其中创建 Cloud TPU 的可用区
accelerator-type
加速器类型用于指定您要创建的 Cloud TPU 的版本和大小。如需详细了解每个 TPU 版本支持的加速器类型,请参阅 TPU 版本
runtime-version
Cloud TPU 软件版本
valid-after-duration
在多久时长之前不得预配 TPU。如需详细了解时长格式,请参阅 Google Cloud CLI 主题日期时间

Java

如需向 Cloud TPU 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证

import com.google.cloud.tpu.v2alpha1.CreateQueuedResourceRequest;
import com.google.cloud.tpu.v2alpha1.Node;
import com.google.cloud.tpu.v2alpha1.QueuedResource;
import com.google.cloud.tpu.v2alpha1.TpuClient;
import com.google.protobuf.Duration;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class CreateTimeBoundQueuedResource {

  public static void main(String[] args)
          throws IOException, ExecutionException, InterruptedException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Google Cloud project you want to create a node.
    String projectId = "YOUR_PROJECT_ID";
    // The zone in which to create the TPU.
    // For more information about supported TPU types for specific zones,
    // see https://cloud.google.com/tpu/docs/regions-zones
    String zone = "us-central2-b";
    // The name of your node.
    String nodeId = "YOUR_NODE_ID";
    // The accelerator type that specifies the version and size of the Cloud TPU you want to create.
    // For more information about supported accelerator types for each TPU version,
    // see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
    String acceleratorType = "v2-8";
    // Software version that specifies the version of the TPU runtime to install.
    // For more information see https://cloud.google.com/tpu/docs/runtimes
    String runtimeVersion = "v2-tpuv5-litepod";
    // The name of your Queued Resource.
    String queuedResourceId = "YOUR_QUEUED_RESOURCE_ID";

    createTimeBoundQueuedResource(projectId, nodeId,
        queuedResourceId, zone, acceleratorType, runtimeVersion);
  }

  // Creates a Queued Resource with time bound configuration.
  public static QueuedResource createTimeBoundQueuedResource(
      String projectId, String nodeId, String queuedResourceId,
      String zone, String acceleratorType, String runtimeVersion)
          throws IOException, ExecutionException, InterruptedException {
    // 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 (TpuClient tpuClient = TpuClient.create()) {
      String parent = String.format("projects/%s/locations/%s", projectId, zone);
      // Create a Duration object representing 6 hours.
      Duration validAfterDuration = Duration.newBuilder().setSeconds(6 * 3600).build();
      // You could also use timestamps like this:
      // Timestamp validAfterTime = Timestamps.parse("2024-10-14T09:00:00Z");

      Node node =
          Node.newBuilder()
              .setName(nodeId)
              .setAcceleratorType(acceleratorType)
              .setRuntimeVersion(runtimeVersion)
              .setQueuedResource(
                  String.format(
                      "projects/%s/locations/%s/queuedResources/%s",
                      projectId, zone, queuedResourceId))
              .build();

      QueuedResource queuedResource =
          QueuedResource.newBuilder()
              .setName(queuedResourceId)
              .setTpu(
                  QueuedResource.Tpu.newBuilder()
                      .addNodeSpec(
                          QueuedResource.Tpu.NodeSpec.newBuilder()
                              .setParent(parent)
                              .setNode(node)
                              .setNodeId(nodeId)
                              .build())
                      .build())
              .setQueueingPolicy(
                  QueuedResource.QueueingPolicy.newBuilder()
                      .setValidAfterDuration(validAfterDuration)
                      // .setValidAfterTime(validAfterTime)
                      .build())
              .build();

      CreateQueuedResourceRequest request =
          CreateQueuedResourceRequest.newBuilder()
              .setParent(parent)
              .setQueuedResource(queuedResource)
              .setQueuedResourceId(queuedResourceId)
              .build();

      return tpuClient.createQueuedResourceAsync(request).get();
    }
  }
}

请求在指定的时间后过期的已排队资源

在已排队的资源请求中,您可以指定已排队的资源请求保持有效的时长。如果请求未在您指定的时间或时长内完成,则请求会失效。

gcloud

以下命令请求 v5p-4096 TPU。如果请求未在 2022 年 12 月 14 日上午 9:00 之前完成,则请求会失效。

gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project-id \
    --zone us-east5-a \
    --accelerator-type v5p-4096 \
    --runtime-version v2-alpha-tpuv5 \
    --valid-until-time 2022-12-14T09:00:00Z

命令参数说明

queued-resource-request-id
已排队的资源请求的用户分配 ID。
node-id
:响应请求而创建的 TPU 的用户定义 ID。
project
:已排队的资源分配到的项目的 ID。
zone
拟在其中创建 Cloud TPU 的可用区
accelerator-type
加速器类型用于指定您要创建的 Cloud TPU 的版本和大小。如需详细了解每个 TPU 版本支持的加速器类型,请参阅 TPU 版本
runtime-version
Cloud TPU 软件版本
valid-until-time
:在多久时间之后请求会被取消。如需详细了解时长格式,请参阅 Google Cloud CLI 主题日期格式

curl

以下命令请求 v5p-4096 TPU。如果请求未在 2022 年 12 月 14 日上午 9:00 之前完成,则请求会失效。

curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
-d "{
    'tpu': {
    'node_spec': {
        'parent': 'projects/your-project-number/locations/us-east5-a',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v5p-4096',
        'runtime_version': 'v2-alpha-tpuv5',
        }
    }
    },
    'queueing_policy': {
    'valid_until_time': {
        'seconds': 1655197200
    }
    }
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-east5-a/queuedResources?queued_resource_id=your-queued-resource-id

命令参数说明

queued-resource-request-id
已排队的资源请求的用户分配 ID。
node-id
:响应请求而创建的 TPU 的用户定义 ID。
project
:已排队的资源分配到的项目的 ID。
zone
拟在其中创建 Cloud TPU 的可用区
accelerator-type
加速器类型用于指定您要创建的 Cloud TPU 的版本和大小。如需详细了解每个 TPU 版本支持的加速器类型,请参阅 TPU 版本
runtime-version
Cloud TPU 软件版本
valid-until-time
:在多久时间之后请求会被取消。如需详细了解时长格式,请参阅 Google Cloud CLI 主题日期格式

控制台

  1. 在 Google Cloud 控制台中,前往 TPU 页面:

    前往 TPU

  2. 点击创建 TPU

  3. 名称字段中,输入 TPU 的名称。

  4. 可用区框中,选择您要在其中创建 TPU 的可用区。

  5. TPU 类型框中,选择加速器类型。加速器类型用于指定您要创建的 Cloud TPU 的版本和大小。如需详细了解每个 TPU 版本支持的加速器类型,请参阅 TPU 版本

  6. TPU 软件版本框中,选择软件版本。创建 Cloud TPU 虚拟机时,TPU 软件版本用于指定要安装的 TPU 运行时的版本。如需了解详情,请参阅 TPU 软件版本

  7. 点击启用排队切换开关。

  8. 已排队资源的名称字段中,输入已排队的资源请求的名称。

  9. 请求取消时间字段中,输入已排队的资源请求在未完成时应失效的时间。

  10. 点击创建以创建已排队的资源请求。

以下示例请求 v5p-32。如果在六小时内未完成请求,则请求会失效。

gcloud

    gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project-id \
    --zone us-east5-a \
    --accelerator-type v5p-32 \
    --runtime-version v2-alpha-tpuv5 \
    --valid-until-duration 6h

命令参数说明

queued-resource-request-id
已排队的资源请求的用户分配 ID。
node-id
:响应请求而创建的 TPU 的用户定义 ID。
project
:已排队的资源分配到的 Google Cloud 项目。
zone
拟在其中创建 Cloud TPU 的可用区
accelerator-type
加速器类型用于指定您要创建的 Cloud TPU 的版本和大小。如需详细了解每个 TPU 版本支持的加速器类型,请参阅 TPU 版本
runtime-version
Cloud TPU 软件版本
valid-until-duration
请求的有效时长。如需详细了解时长格式,请参阅 Google Cloud CLI 主题日期格式

curl

curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
-d "{
    'tpu': {
    'node_spec': {
        'parent': 'projects/your-project-number/locations/us-east5-a',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v5p-32',
        'runtime_version': 'v2-alpha-tpuv5',
        }
    }
    },
'queueing_policy': {
    'valid_until_duration': {
        'seconds': 21600
    }
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-east5-a/queuedResources?queued_resource_id=your-queued-resource-id

命令参数说明

queued-resource-request-id
已排队的资源请求的用户分配 ID。
node-id
:响应请求而创建的 TPU 的用户定义 ID。
project
:已排队的资源分配到的 Google Cloud 项目。
zone
拟在其中创建 Cloud TPU 的可用区
accelerator-type
加速器类型用于指定您要创建的 Cloud TPU 的版本和大小。如需详细了解每个 TPU 版本支持的加速器类型,请参阅 TPU 版本
runtime-version
Cloud TPU 软件版本
valid-until-duration
请求的有效时长。如需详细了解时长格式,请参阅 Google Cloud CLI 主题日期格式

Python

如需向 Cloud TPU 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证

from google.cloud import tpu_v2alpha1

# TODO(developer): Update and un-comment below lines
# project_id = "your-project-id"
# zone = "us-central1-a"
# tpu_name = "tpu-name"
# tpu_type = "v5litepod-4"
# runtime_version = "v2-tpuv5-litepod"
# queued_resource_name = "resource-name"

node = tpu_v2alpha1.Node()
node.accelerator_type = tpu_type
# To see available runtime version use command:
# gcloud compute tpus versions list --zone={ZONE}
node.runtime_version = runtime_version

node_spec = tpu_v2alpha1.QueuedResource.Tpu.NodeSpec()
node_spec.parent = f"projects/{project_id}/locations/{zone}"
node_spec.node_id = tpu_name
node_spec.node = node

resource = tpu_v2alpha1.QueuedResource()
resource.tpu = tpu_v2alpha1.QueuedResource.Tpu(node_spec=[node_spec])

# Use one of the following queueing policies
resource.queueing_policy = tpu_v2alpha1.QueuedResource.QueueingPolicy(
    # valid_after_duration = "6000s", # Duration after which a resource should be allocated
    valid_until_duration="90s",  # Specify how long a queued resource request remains valid
    # valid_after_time="2024-10-31T09:00:00Z", # Specify a time after which a resource should be allocated
    # valid_until_time="2024-10-29T16:00:00Z",  # Specify a time before which the resource should be allocated
)

request = tpu_v2alpha1.CreateQueuedResourceRequest(
    parent=f"projects/{project_id}/locations/{zone}",
    queued_resource_id=queued_resource_name,
    queued_resource=resource,
)

client =