キューに入れられたリソースを管理する

キューに格納されたリソースを使用すると、Cloud TPU リソースをキューに格納してリクエストできます。キューに格納されたリソースをリクエストすると、リクエストは Cloud TPU サービスによって管理されるキューに追加されます。リクエストされたリソースが使用可能になると、すぐに Google Cloud プロジェクトに割り当てられ、すぐに専用で使用できるようになります。プロジェクトを削除またはプリエンプトしない限り、プロジェクトに割り当てられたままになります。プリエンプションの対象は TPU Spot VM とプリエンプティブル TPU のみとなります。

キューに入れられたリソース リクエストで、開始時刻や終了時刻(オプション)を指定できます。開始時刻は、リクエストを入力できる最も早い時刻を指定します。指定された終了時刻までにリクエストが入力されていない場合、リクエストは期限切れになります。リクエストは有効期限が切れた後もキュー内に残ります。

キューに格納されたリソース リクエストは、次のいずれかの状態になります。

WAITING_FOR_RESOURCES
リクエストは最初の検証に合格し、キューに追加されました。この状態は、リクエストのプロビジョニングを開始するのに十分な空きリソースがあるか、割り当て区間が経過するまで維持されます。需要が高い場合、すべてのリクエストをすぐにプロビジョニングできるわけではありません。TPU の信頼性を高める必要がある場合は、予約の購入を検討してください。
PROVISIONING
リクエストがキューから選択され、リソースが割り当てられています。
ACTIVE
リクエストが割り当てられました。キューに格納されたリソース リクエストが ACTIVE 状態になると、TPU を管理するの説明に従って TPU VM を管理できます。
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。
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。
project
プロジェクト Google Cloud 。
zone
Cloud TPU を作成するゾーン
accelerator-type
アクセラレータ タイプでは、作成する Cloud TPU のバージョンとサイズを指定します。TPU のバージョンごとにサポートされているアクセラレータ タイプの詳細については、TPU のバージョンをご覧ください。
runtime-version
Cloud TPU ソフトウェアのバージョン

Console

  1. Google Cloud コンソールの [TPU] ページに移動します。

    [TPU] に移動

  2. [TPU を作成] をクリックします。

  3. [名前] フィールドに、TPU の名前を入力します。

  4. [ゾーン] ボックスで、TPU を作成するゾーンを選択します。

  5. [TPU タイプ] ボックスで、アクセラレータ タイプを選択します。アクセラレータ タイプでは、作成する Cloud TPU のバージョンとサイズを指定します。TPU の各バージョンでサポートされているアクセラレータ タイプの詳細については、TPU のバージョンをご覧ください。

  6. [TPU ソフトウェア バージョン] ボックスで、ソフトウェア バージョンを選択します。Cloud TPU VM の作成時には、インストールされる TPU ランタイム バージョンが TPU ソフトウェア バージョンによって指定されます。詳細については、TPU VM イメージをご覧ください。

  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。
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。
project
プロジェクト Google Cloud 。
zone
Cloud TPU を作成するゾーン
accelerator-type
アクセラレータ タイプでは、作成する Cloud TPU のバージョンとサイズを指定します。TPU のバージョンごとにサポートされているアクセラレータ タイプの詳細については、TPU のバージョンをご覧ください。
runtime-version
Cloud TPU ソフトウェアのバージョン。
reserved
このフラグは、Cloud TPU 予約の一部としてキュー内のリソースをリクエストするときに使用します。

Console

  1. Google Cloud コンソールの [TPU] ページに移動します。

    [TPU] に移動

  2. [TPU を作成] をクリックします。

  3. [名前] フィールドに、TPU の名前を入力します。

  4. [ゾーン] ボックスで、TPU を作成するゾーンを選択します。

  5. [TPU タイプ] ボックスで、アクセラレータ タイプを選択します。アクセラレータ タイプでは、作成する Cloud TPU のバージョンとサイズを指定します。TPU の各バージョンでサポートされているアクセラレータ タイプの詳細については、TPU のバージョンをご覧ください。

  6. [TPU ソフトウェア バージョン] ボックスで、ソフトウェア バージョンを選択します。Cloud TPU VM の作成時には、インストールされる TPU ランタイム バージョンが TPU ソフトウェア バージョンによって指定されます。詳細については、TPU VM イメージをご覧ください。

  7. [キューイングを有効にする] トグルをクリックします。

  8. [キューに登録されたリソース名] フィールドに、キューに登録されたリソース リクエストの名前を入力します。

  9. [管理] セクションを開きます。

  10. [既存の予約を使用する] チェックボックスをオンにします。

  11. [作成] をクリックして、キューに格納されたリソース リクエストを作成します。

キューに入れられた TPU Spot VM のリソースをリクエストする

Spot VM は、いつでもプリエンプトして別のワークロードに割り当てることができるリソースです。Spot VM のリソースは低コストで、Spot VM 以外のリクエストよりも早くリソースにアクセスできる場合があります。TPU Spot VM の詳細については、TPU Spot VM を管理するをご覧ください。

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 VM であることを指定するブール値のフラグ。

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 VM であることを指定するブール値のフラグ。

Console

  1. Google Cloud コンソールの [TPU] ページに移動します。

    [TPU] に移動

  2. [TPU を作成] をクリックします。

  3. [名前] フィールドに、TPU の名前を入力します。

  4. [ゾーン] ボックスで、TPU を作成するゾーンを選択します。

  5. [TPU タイプ] ボックスで、アクセラレータ タイプを選択します。アクセラレータ タイプでは、作成する Cloud TPU のバージョンとサイズを指定します。TPU の各バージョンでサポートされているアクセラレータ タイプの詳細については、TPU のバージョンをご覧ください。

  6. [TPU ソフトウェア バージョン] ボックスで、ソフトウェア バージョンを選択します。Cloud TPU VM の作成時には、インストールされる TPU ランタイム バージョンが TPU ソフトウェア バージョンによって指定されます。詳細については、TPU VM イメージをご覧ください。

  7. [キューイングを有効にする] トグルをクリックします。

  8. [キューに登録されたリソース名] フィールドに、キューに登録されたリソース リクエストの名前を入力します。

  9. [管理] セクションを開きます。

  10. [Make this a TPU Spot VM] チェックボックスをオンにします。

  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 topic datetime をご覧ください。

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 topic datetime をご覧ください。

Console

  1. Google Cloud コンソールの [TPU] ページに移動します。

    [TPU] に移動

  2. [TPU を作成] をクリックします。

  3. [名前] フィールドに、TPU の名前を入力します。

  4. [ゾーン] ボックスで、TPU を作成するゾーンを選択します。

  5. [TPU タイプ] ボックスで、アクセラレータ タイプを選択します。アクセラレータ タイプでは、作成する Cloud TPU のバージョンとサイズを指定します。TPU の各バージョンでサポートされているアクセラレータ タイプの詳細については、TPU のバージョンをご覧ください。

  6. [TPU ソフトウェア バージョン] ボックスで、ソフトウェア バージョンを選択します。Cloud TPU VM の作成時には、インストールされる TPU ランタイム バージョンが TPU ソフトウェア バージョンによって指定されます。詳細については、TPU VM イメージをご覧ください。

  7. [キューイングを有効にする] トグルをクリックします。

  8. [キューに登録されたリソース名] フィールドに、キューに登録されたリソース リクエストの名前を入力します。

  9. [リクエストの開始日時] フィールドに、リソースを割り当てる時間を入力します。

  10. [作成] をクリックして、キューに格納されたリソース リクエストを作成します。

次の例では、v5p-32 を 6 時間後に割り当てるようリクエストします。

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 時までにリクエストが処理されなかった場合、リクエストは期限切れになります。

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 時までにリクエストが処理されなかった場合、リクエストは期限切れになります。

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 トピックの日時をご覧ください。

Console

  1. Google Cloud コンソールの [TPU] ページに移動します。

    [TPU] に移動

  2. [TPU を作成] をクリックします。

  3. [名前] フィールドに、TPU の名前を入力します。

  4. [ゾーン] ボックスで、TPU を作成するゾーンを選択します。

  5. [TPU タイプ] ボックスで、アクセラレータ タイプを選択します。アクセラレータ タイプでは、作成する Cloud TPU のバージョンとサイズを指定します。TPU の各バージョンでサポートされているアクセラレータ タイプの詳細については、TPU のバージョンをご覧ください。

  6. [TPU ソフトウェア バージョン] ボックスで、ソフトウェア バージョンを選択します。Cloud TPU VM の作成時には、インストールされる TPU ランタイム バージョンが TPU ソフトウェア バージョンによって指定されます。詳細については、TPU VM イメージをご覧ください。

  7. [キューイングを有効にする] トグルをクリックします。

  8. [キューに登録されたリソース名] フィールドに、キューに登録されたリソース リクエストの名前を入力します。

  9. [リクエストをキャンセルする] フィールドに、キューに登録されたリソース リクエストが満たされなかった場合に期限切れになる時間を入力します。

  10. [作成] をクリックして、キューに格納されたリソース リクエストを作成します。

次の例では、v5p-32 をリクエストします。6 時間以内に入力しないと、リクエストは期限切れになります。

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 = tpu_v2alpha1.TpuClient()
operation = client.create_queued_resource(request=request)

response = operation.result()
print(resource.queueing_policy)
print(response.queueing_policy.valid_until_time)
# Example response:
# valid_until_duration {
#   seconds: 90
# }
# 2024-10-29 14:22:53.562090+00:00

キューに格納されたリソースを指定された時間内に割り当てるようリクエストする

開始時間または期間と終了時間または期間の両方を指定して、割り当て間隔を指定できます。

gcloud

次のコマンドは、現在の時刻から 5 時間 30 分後で、2022 年 12 月 14 日午前 9 時までに v5p-32 を作成するようリクエストします。

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 5h30m \
    --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 トピックの日時をご覧ください。
valid-after-duration
TPU をプロビジョニングできない期間。期間形式の詳細については、Google Cloud CLI トピックの日時をご覧ください。

curl

次のコマンドは、現在の時刻から 5 時間 30 分後で、2022 年 12 月 14 日午前 9 時までに v5p-32 を作成するようリクエストします。

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': {
    'validInterval': {
        'startTime': '2022-12-10T14:30:00Z',
        'endTime': '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
キューに格納されたリソースが割り当てられているプロジェクトの ID。
zone
Cloud TPU を作成するゾーン
accelerator-type
アクセラレータ タイプでは、作成する Cloud TPU のバージョンとサイズを指定します。TPU のバージョンごとにサポートされているアクセラレータ タイプの詳細については、TPU のバージョンをご覧ください。
runtime-version
Cloud TPU ソフトウェアのバージョン。
valid-until-timw
それ以降はリクエストがキャンセルされる時間。期間形式の詳細については、Google Cloud CLI トピックの日時をご覧ください。
valid-until-duration
リクエストが有効である期間。期間形式の詳細については、Google Cloud CLI トピックの日時をご覧ください。

Console

  1. Google Cloud コンソールの [TPU] ページに移動します。

    [TPU] に移動

  2. [TPU を作成] をクリックします。

  3. [名前] フィールドに、TPU の名前を入力します。

  4. [ゾーン] ボックスで、TPU を作成するゾーンを選択します。

  5. [TPU タイプ] ボックスで、アクセラレータ タイプを選択します。アクセラレータ タイプでは、作成する Cloud TPU のバージョンとサイズを指定します。TPU の各バージョンでサポートされているアクセラレータ タイプの詳細については、TPU のバージョンをご覧ください。

  6. [TPU ソフトウェア バージョン] ボックスで、ソフトウェア バージョンを選択します。Cloud TPU VM の作成時には、インストールされる TPU ランタイム バージョンが TPU ソフトウェア バージョンによって指定されます。詳細については、TPU VM イメージをご覧ください。

  7. [キューイングを有効にする] トグルをクリックします。

  8. [キューに登録されたリソース名] フィールドに、キューに登録されたリソース リクエストの名前を入力します。

  9. [リクエストの開始日時] フィールドに、リソースを割り当てる時間を入力します。

  10. [リクエストをキャンセルする] フィールドに、キューに登録されたリソース リクエストが満たされなかった場合に期限切れになる時間を入力します。

  11. [作成] をクリックして、キューに格納されたリソース リクエストを作成します。

起動スクリプトでキューに格納されたリソースをリクエストする

キューに格納されたリソースに対してプロビジョニング後に実行するスクリプトを指定できます。

gcloud

gcloud コマンドを使用する場合、--metadata フラグまたは --metadata-from-file フラグのいずれかを使用して、それぞれスクリプト コマンド、スクリプト コードを記述したファイルを指定できます。次の例では、startup-script.sh スクリプトを実行するキューに格納されたリソース リクエストを作成します。

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 us-central1-a \
    --metadata-from-file='startup-script=startup-script.sh'

コマンドフラグの説明

queued-resource-request-id
キューに入れられたリソース リクエストのユーザー割り当て ID。
node-id
リクエストへの応答として作成された TPU のユーザー定義 ID。
project
キューに格納されたリソースが割り当てられているプロジェクトの ID。
zone
Cloud TPU を作成するゾーン
accelerator-type
アクセラレータ タイプでは、作成する Cloud TPU のバージョンとサイズを指定します。TPU のバージョンごとにサポートされているアクセラレータ タイプの詳細については、TPU のバージョンをご覧ください。
runtime-version
Cloud TPU ソフトウェアのバージョン。
validInterval
リクエストがキャンセルされた後、リクエストが有効な期間。期間形式の詳細については、Google Cloud CLI トピックの日時をご覧ください。
metadata-from-file
メタデータを含むファイルを指定します。メタデータ ファイルの完全修飾パスを指定しない場合、コマンドはメタデータ ファイルが現在のディレクトリにあると想定します。この例では、キュー内のリソースがプロビジョニングされるときに実行される起動スクリプトがファイルに含まれています。
metadata
リクエストのメタデータを指定します。この例では、メタデータは、キューに入れられたリソースがプロビジョニングされるときに実行される起動スクリプト コマンドです。

curl

curl を使用する場合は、JSON コンテンツにスクリプト コードを記述する必要があります。次の例では、JSON 本文にインライン スクリプトを含めています。

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: 'v5e-8',
            runtime_version: 'v2-alpha-tpuv5-lite',
            metadata: {
                "startup-script": "#! /bin/bash\npwd > /tmp/out.txt\nwhoami >> /tmp/out.txt"
            }
        }
        }
    },
'queueing_policy': {
    'validInterval': {
        'startTime': '2022-12-10T14:30:00Z',
        'endTime': '2022-12-14T09:00:00Z'
    }
    },
}" \
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 ソフトウェアのバージョン。
validInterval
リクエストがキャンセルされた後、リクエストが有効な期間。期間形式の詳細については、Google Cloud CLI トピックの日時をご覧ください。
metadata-from-file
メタデータを含むファイルを指定します。メタデータ ファイルの完全修飾パスを指定しない場合、コマンドはメタデータ ファイルが現在のディレクトリにあることを前提とします。この例では、キュー内のリソースがプロビジョニングされるときに実行される起動スクリプトがファイルに含まれています。
metadata
リクエストのメタデータを指定します。この例では、メタデータは、キューに入れられたリソースがプロビジョニングされるときに実行される起動スクリプト コマンドです。

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.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class CreateQueuedResourceWithStartupScript {
  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 startup script.
  public static QueuedResource createQueuedResource(
      String projectId, String zone, String queuedResourceId,
      String nodeName, String tpuType, String tpuSoftwareVersion)
      throws IOException, ExecutionException, InterruptedException {
    String parent = String.format("projects/%s/locations/%s", projectId, zone);
    String startupScriptContent = "#!/bin/bash\necho \"Hello from the startup script!\"";
    // Add startup script to metadata
    Map<String, String> metadata = new HashMap<>();
    metadata.put("startup-script", startupScriptContent);
    String queuedResourceForTpu =  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()) {
      Node node =
          Node.newBuilder()
              .setName(nodeName)
              .setAcceleratorType(tpuType)
              .setRuntimeVersion(tpuSoftwareVersion)
              .setQueuedResource(queuedResourceForTpu)
              .putAllMetadata(metadata)
              .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();
      // You can wait until TPU Node is READY,
      // and check its status using getTpuVm() from "tpu_vm_get" sample.

      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
# This startup script updates numpy to the latest version and logs the output to a file.
script = {
    "startup-script": """#!/bin/bash
echo "Hello World" > /var/log/hello.log
sudo pip3 install --upgrade numpy >> /var/log/hello.log 2>&1
"""
}
node.metadata = script
# Enabling external IPs for internet access from the TPU node for updating numpy
node.network_config = tpu_v2alpha1.NetworkConfig(
    enable_external_ips=True,
)

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.tpu.node_spec[0].node.metadata)
# Example response:
# projects/[project_id]/locations/[zone]/queuedResources/resource-name
# {'startup-script': '#!/bin/bash\n    echo "Hello World" > /var/log/hello.log\n
# sudo pip3 install --upgrade numpy >> /var/log/hello.log 2>&1\n    '}

指定したネットワークとサブネットワークでキューに入れられたリソースをリクエストする

キューに格納されたリソース リクエストで、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 v5e-8 \
    --runtime-version v2-alpha-tpuv5-lite \
    --network network-name \
    --subnetwork subnetwork-name

コマンド パラメータの説明

queued-resource-id
キューに入れられたリソース リクエストのユーザー割り当て ID。
node-id
キューに入れられたリソース リクエストの割り当て時に作成される TPU のユーザー割り当て ID。
project
プロジェクト Google Cloud 。
zone
Cloud TPU を作成するゾーン
accelerator-type
アクセラレータ タイプでは、作成する Cloud TPU のバージョンとサイズを指定します。TPU のバージョンごとにサポートされているアクセラレータ タイプの詳細については、TPU のバージョンをご覧ください。
runtime-version
Cloud TPU ソフトウェアのバージョン。
reserved
このフラグは、Cloud TPU 予約の一部としてキュー内のリソースをリクエストするときに使用します。
network
キューに入れられたリソースが属するネットワーク。
subnetwork
キューに入れられたリソースが属するサブネットワーク。

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': 'v5e-8',
        'runtime_version': 'v2-alpha-tpuv5-lite',
        'network_config': {
            'network': 'network-name',
            'subnetwork': 'subnetwork-name',
            'enable_external_ips': true
        }
    }
    },
    '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。
project
プロジェクト Google Cloud 。
zone
Cloud TPU を作成するゾーン
accelerator-type
アクセラレータ タイプでは、作成する Cloud TPU のバージョンとサイズを指定します。TPU のバージョンごとにサポートされているアクセラレータ タイプの詳細については、TPU のバージョンをご覧ください。
runtime-version
Cloud TPU ソフトウェアのバージョン。
reserved
このフラグは、Cloud TPU 予約の一部としてキュー内のリソースをリクエストするときに使用します。
network
キューに入れられたリソースが属するネットワーク。
subnetwork
キューに入れられたリソースが属するサブネットワーク。

Console

  1. Google Cloud コンソールの [TPU] ページに移動します。

    [TPU] に移動

  2. [TPU を作成] をクリックします。

  3. [名前] フィールドに、TPU の名前を入力します。

  4. [ゾーン] ボックスで、TPU を作成するゾーンを選択します。

  5. [TPU タイプ] ボックスで、アクセラレータ タイプを選択します。アクセラレータ タイプでは、作成する Cloud TPU のバージョンとサイズを指定します。TPU の各バージョンでサポートされているアクセラレータ タイプの詳細については、TPU のバージョンをご覧ください。

  6. [TPU ソフトウェア バージョン] ボックスで、ソフトウェア バージョンを選択します。Cloud TPU VM の作成時には、インストールされる TPU ランタイム バージョンが TPU ソフトウェア バージョンによって指定されます。詳細については、TPU VM イメージをご覧ください。

  7. [キューイングを有効にする] トグルをクリックします。

  8. [キューに登録されたリソース名] フィールドに、キューに登録されたリソース リクエストの名前を入力します。

  9. [ネットワーク] セクションを開きます。

  10. [ネットワーク] フィールドと [サブネットワーク] フィールドで、使用するネットワークとサブネットワークを選択します。

  11. [作成] をクリックして、キューに格納されたリソース リクエストを作成します。

Java

Cloud TPU に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。

import com.google.api.gax.retrying.RetrySettings;
import com.google.cloud.tpu.v2alpha1.CreateQueuedResourceRequest;
import com.google.cloud.tpu.v2alpha1.NetworkConfig;
import com.google.cloud.tpu.v2alpha1.Node;
import com.google.cloud.tpu.v2alpha1.QueuedResource;
import com.google.cloud.tpu.v2alpha1.TpuClient;
import com.google.cloud.tpu.v2alpha1.TpuSettings;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import org.threeten.bp.Duration;

public class CreateQueuedResourceWithNetwork {
  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 = "europe-west4-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";
    // The name of the network you want the node to connect to.
    // The network should be assigned to your project.
    String networkName = "YOUR_COMPUTE_TPU_NETWORK";

    createQueuedResourceWithNetwork(projectId, zone, queuedResourceId, nodeName,
        tpuType, tpuSoftwareVersion, networkName);
  }

  // Creates a Queued Resource with network configuration.
  public static QueuedResource createQueuedResourceWithNetwork(
      String projectId, String zone, String queuedResourceId, String nodeName,
      String tpuType, String tpuSoftwareVersion, String networkName)
      throws IOException, ExecutionException, InterruptedException {
    // With these settings the client library handles the Operation's polling mechanism
    // and prevent CancellationException error
    TpuSettings.Builder clientSettings =
        TpuSettings.newBuilder();
    clientSettings
        .createQueuedResourceSettings()
        .setRetrySettings(
            RetrySettings.newBuilder()
                .setInitialRetryDelay(Duration.ofMillis(5000L))
                .setRetryDelayMultiplier(2.0)
                .setInitialRpcTimeout(Duration.ZERO)
                .setRpcTimeoutMultiplier(1.0)
                .setMaxRetryDelay(Duration.ofMillis(45000L))
                .setTotalTimeout(Duration.ofHours(24L))
                .build());
    // 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(clientSettings.build())) {
      String parent = String.format("projects/%s/locations/%s", projectId, zone);
      String region = zone.substring(0, zone.length() - 2);

      // Specify the network and subnetwork that you want to connect your TPU to.
      NetworkConfig networkConfig =
          NetworkConfig.newBuilder()
              .setEnableExternalIps(true)
              .setNetwork(String.format("projects/%s/global/networks/%s", projectId, networkName))
              .setSubnetwork(
                  String.format(
                      "projects/%s/regions/%s/subnetworks/%s", projectId, region, networkName))
              .build();

      // Create a node
      Node node =
          Node.newBuilder()
              .setName(nodeName)
              .setAcceleratorType(tpuType)
              .setRuntimeVersion(tpuSoftwareVersion)
              .setNetworkConfig(networkConfig)
              .setQueuedResource(
                  String.format(
                      "projects/%s/locations/%s/queuedResources/%s",
                      projectId, zone, queuedResourceId))
              .build();

      // Create queued resource
      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)
              .setQueuedResource(queuedResource)
              .setQueuedResourceId(queuedResourceId)
              .build();

      // You can wait until TPU Node is READY,
      // and check its status using getTpuVm() from "tpu_vm_get" sample.

      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"
# network = "default"

node = tpu_v2alpha1.Node()
node.accelerator_type = tpu_type
node.runtime_version = runtime_version
# Setting network configuration
node.network_config = tpu_v2alpha1.NetworkConfig(
    network=network,  # Update if you want to use a specific network
    subnetwork="default",  # Update if you want to use a specific subnetwork
    enable_external_ips=True,
    can_ip_forward=True,
)

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.tpu.node_spec[0].node.network_config)
print(resource.tpu.node_spec[0].node.network_config.network == "default")
# Example response:
# network: "default"
# subnetwork: "default"
# enable_external_ips: true
# can_ip_forward: true

キューに入れられたリソース リクエストを削除する

キューに入れられたリソース リクエストとそのリクエストに関連付けられた TPU を削除するには、キューに入れられたリソース リクエストを削除します。

gcloud

--force フラグを queued-resource delete コマンドに渡します。

gcloud compute tpus queued-resources delete your-queued-resource-id \
    --project your-project-id \
    --zone us-central1-a \
    --force \
    --async

コマンドフラグの説明

your-queued-resource-id
キューに入れられたリソース リクエストのユーザー割り当て ID。
project
キューに格納されたリソースが割り当てられている Google Cloud プロジェクト。
zone
削除する Cloud TPU のゾーン
force
TPU VM とキューに入れられたリソース リクエストの両方を削除します。

curl

curl リクエストでクエリ パラメータ force=true を使用します。

curl -X DELETE -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://tpu.googleapis.com/v2/projects/your-project-id/locations/us-central1-a/queuedResources/your-queued-resource-id?force=true

コマンドフラグの説明

your-queued-resource-id
キューに入れられたリソース リクエストのユーザー割り当て ID。
project
キューに格納されたリソースが割り当てられているプロジェクト。 Google Cloud
zone
削除する Cloud TPU のゾーン
force
TPU VM とキューに入れられたリソース リクエストの両方を削除します。

Console

  1. Google Cloud コンソールの [TPU] ページに移動します。

    [TPU] に移動

  2. [キューに登録されているリソース] タブをクリックします。

  3. キューに登録されているリソース リクエストの横にあるチェックボックスをオンにします。

  4. [削除] をクリックします。

Java

Cloud TPU に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。

import com.google.api.gax.retrying.RetrySettings;
import com.google.cloud.tpu.v2alpha1.DeleteQueuedResourceRequest;
import com.google.cloud.tpu.v2alpha1.TpuClient;
import com.google.cloud.tpu.v2alpha1.TpuSettings;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import org.threeten.bp.Duration;

public class DeleteForceQueuedResource {
  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.
    String projectId = "YOUR_PROJECT_ID";
    // The zone in which the TPU was created.
    String zone = "us-central1-f";
    // The name for your Queued Resource.
    String queuedResourceId = "QUEUED_RESOURCE_ID";

    deleteForceQueuedResource(projectId, zone, queuedResourceId);
  }

  // Deletes a Queued Resource asynchronously with --force flag.
  public static void deleteForceQueuedResource(
      String projectId, String zone, String queuedResourceId)
          throws ExecutionException, InterruptedException, IOException {
    String name = String.format("projects/%s/locations/%s/queuedResources/%s",
        projectId, zone, queuedResourceId);
    // With these settings the client library handles the Operation's polling mechanism
    // and prevent CancellationException error
    TpuSettings.Builder clientSettings =
        TpuSettings.newBuilder();
    clientSettings
        .deleteQueuedResourceSettings()
        .setRetrySettings(
            RetrySettings.newBuilder()
                .setInitialRetryDelay(Duration.ofMillis(5000L))
                .setRetryDelayMultiplier(2.0)
                .setInitialRpcTimeout(Duration.ZERO)
                .setRpcTimeoutMultiplier(1.0)
                .setMaxRetryDelay(Duration.ofMillis(45000L))
                .setTotalTimeout(Duration.ofHours(24L))
                .build());

    // 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(clientSettings.build())) {
      DeleteQueuedResourceRequest request =
          DeleteQueuedResourceRequest.newBuilder().setName(name).setForce(true).build();
      // Waiting for updates in the library. Until then, the operation will complete successfully,
      // but the user will receive an error message with UnknownException and IllegalStateException.
      tpuClient.deleteQueuedResourceAsync(request).get();

      System.out.printf("Deleted Queued Resource: %s\n", name);
    }
  }
}

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-b"
# queued_resource_name = "resource-name"

client = tpu_v2alpha1.TpuClient()
request = tpu_v2alpha1.DeleteQueuedResourceRequest(
    name=f"projects/{project_id}/locations/{zone}/queuedResources/{queued_resource_name}",
    force=True,  # Set force=True to delete the resource with tpu nodes.
)

try:
    op = client.delete_queued_resource(request=request)
    op.result()
    print(f"Queued resource '{queued_resource_name}' successfully deleted.")
except TypeError as e:
    print(f"Error deleting resource: {e}")
    print(f"Queued resource '{queued_resource_name}' successfully deleted.")

TPU を直接削除する場合は、次の例に示すように、キューに登録されたリソースも削除する必要があります。TPU を削除すると、キューに入れられたリソース リクエストは SUSPENDED 状態に移行し、その後、キューに入れられたリソース リクエストを削除できます。

gcloud

TPU を削除します。

$ gcloud compute tpus tpu-vm delete your-node-id \
    --project=your-project-id \
    --zone=us-central1-a \
    --quiet

コマンドフラグの説明

project
キューに格納されたリソースが割り当てられているプロジェクト。 Google Cloud
zone
削除する Cloud TPU のゾーン
your-node-id
削除する TPU の名前。

TPU を削除すると、関連するキューに入れられたリソースは SUSPENDING 状態になり、その後 SUSPENDED 状態になります。キューに格納されたリソースが SUSPENDED 状態の場合、削除できます。

gcloud compute tpus queued-resources delete your-queued-resource-id \
    --project your-project-id \
    --zone us-central1-a

コマンドフラグの説明

queued-resource-request-id
キューに入れられたリソース リクエストのユーザー割り当て ID。
project
キューに格納されたリソースが割り当てられているプロジェクト。 Google Cloud
zone
削除する Cloud TPU のゾーン

curl

TPU を削除します。

curl -X DELETE -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://tpu.googleapis.com/v2/projects/your-project/locations/us-central1-a/nodes?node_id=your-node-id

コマンドフラグの説明

project
キューに格納されたリソースが割り当てられているプロジェクト。 Google Cloud
zone
削除する Cloud TPU のゾーン
your-node-id
削除する TPU の名前。

TPU を削除すると、関連するキューに入れられたリソースは SUSPENDING 状態になり、その後 SUSPENDED 状態になります。キューに格納されたリソースが SUSPENDED 状態の場合、削除できます。

curl -X DELETE -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://tpu.googleapis.com/v2/projects/your-project-id/locations/us-central1-a/queuedResources/your-queued-resource-id

コマンドフラグの説明

queued-resource-request-id
キューに入れられたリソース リクエストのユーザー割り当て ID。
project
キューに格納されたリソースが割り当てられているプロジェクト。 Google Cloud
zone
削除する Cloud TPU のゾーン

Console

TPU を削除します。

  1. Google Cloud コンソールの [TPU] ページに移動します。

    [TPU] に移動

  2. TPU の横にあるチェックボックスをオンにします。

  3. [削除] をクリックします。

TPU を削除すると、関連するキューに入れられたリソースは、停止中状態になり、その後停止状態になります。キューに格納されたリソースが [Suspended] 状態の場合、削除できます。

  1. [キューに登録されているリソース] タブをクリックします。
  2. キューに登録されているリソース リクエストの横にあるチェックボックスをオンにします。
  3. [削除] をクリックします。

Java

Cloud TPU に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。

import com.google.api.gax.longrunning.OperationTimedPollAlgorithm;
import com.google.api.gax.retrying.RetrySettings;
import com.google.cloud.tpu.v2.DeleteNodeRequest;
import com.google.cloud.tpu.v2.NodeName;
import com.google.cloud.tpu.v2.TpuClient;
import com.google.cloud.tpu.v2.TpuSettings;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import org.threeten.bp.Duration;

public class DeleteTpuVm {

  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 = "europe-west4-a";
    // The name for your TPU.
    String nodeName = "YOUR_TPU_NAME";

    deleteTpuVm(projectId, zone, nodeName);
  }

  // Deletes a TPU VM with the specified name in the given project and zone.
  public static void deleteTpuVm(String projectId, String zone, String nodeName)
      throws IOException, ExecutionException, InterruptedException {
    // With these settings the client library handles the Operation's polling mechanism
    // and prevent CancellationException error
    TpuSettings.Builder clientSettings =
        TpuSettings.newBuilder();
    clientSettings
        .deleteNodeOperationSettings()
        .setPollingAlgorithm(
            OperationTimedPollAlgorithm.create(
                RetrySettings.newBuilder()
                    .setInitialRetryDelay(Duration.ofMillis(5000L))
                    .setRetryDelayMultiplier(1.5)
                    .setMaxRetryDelay(Duration.ofMillis(45000L))
                    .setInitialRpcTimeout(Duration.ZERO)
                    .setRpcTimeoutMultiplier(1.0)
                    .setMaxRpcTimeout(Duration.ZERO)
                    .setTotalTimeout(Duration.ofHours(24L))
                    .build()));

    // 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(clientSettings.build())) {
      String name = NodeName.of(projectId, zone, nodeName).toString();

      DeleteNodeRequest request = DeleteNodeRequest.newBuilder().setName(name).build();

      tpuClient.deleteNodeAsync(request).get();
      System.out.println("TPU VM deleted");
    }
  }
}

TPU を削除すると、関連するキューに入れられたリソースは SUSPENDING 状態になり、その後 SUSPENDED 状態になります。キューに格納されたリソースが SUSPENDED 状態の場合、削除できます。

import com.google.cloud.tpu.v2alpha1.DeleteQueuedResourceRequest;
import com.google.cloud.tpu.v2alpha1.TpuClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class DeleteQueuedResource {
  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.
    String projectId = "YOUR_PROJECT_ID";
    // The zone in which the TPU was created.
    String zone = "us-central1-f";
    // The name for your Queued Resource.
    String queuedResourceId = "QUEUED_RESOURCE_ID";

    deleteQueuedResource(projectId, zone, queuedResourceId);
  }

  // Deletes a Queued Resource asynchronously.
  public static void deleteQueuedResource(String projectId, String zone, String queuedResourceId)
      throws ExecutionException, InterruptedException, IOException {
    String name = 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()) {
      // Before deleting the queued resource it is required to delete the TPU VM.
      // For more information about deleting TPU
      // see https://cloud.google.com/tpu/docs/managing-tpus-tpu-vm

      DeleteQueuedResourceRequest request =
              DeleteQueuedResourceRequest.newBuilder().setName(name).build();

      tpuClient.deleteQueuedResourceAsync(request).get();
    }
  }
}

Python

Cloud TPU に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。

from google.cloud import tpu_v2

# TODO(developer): Update and un-comment below lines
# project_id = "your-project-id"
# zone = "us-central1-b"
# tpu_name = "tpu-name"

client = tpu_v2.TpuClient()
try:
    client.delete_node(
        name=f"projects/{project_id}/locations/{zone}/nodes/{tpu_name}"
    )
    print("The TPU node was deleted.")
except Exception as e:
    print(e)

TPU を削除すると、関連するキューに入れられたリソースは SUSPENDING 状態になり、その後 SUSPENDED 状態になります。キューに格納されたリソースが SUSPENDED 状態の場合、削除できます。

from google.cloud import tpu_v2alpha1

# TODO(developer): Update and un-comment below lines
# project_id = "your-project-id"
# zone = "us-central1-b"
# queued_resource_name = "resource-name"

client = tpu_v2alpha1.TpuClient()
name = (
    f"projects/{project_id}/locations/{zone}/queuedResources/{queued_resource_name}"
)

try:
    op = client.delete_queued_resource(name=name)
    op.result()
    print(f"Queued resource '{queued_resource_name}' successfully deleted.")
except TypeError as e:
    print(f"Error deleting resource: {e}")
    print(f"Queued resource '{queued_resource_name}' successfully deleted.")

キューに入れられたリソース リクエストの状態と診断情報を取得する

キューに入れられたリソース リクエストの状態と診断情報を取得します。

gcloud

gcloud compute tpus queued-resources describe queued-resource-request-id \
    --project your-project-id \
    --zone us-central1-a

コマンドフラグの説明

queued-resource-request-id
キューに入れられたリソース リクエストのユーザー割り当て ID。
project
キューに格納されたリソースが割り当てられているプロジェクトの ID。
zone
Cloud TPU を作成するゾーン

curl

curl -X GET -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://tpu.googleapis.com/v2/projects/your-project-id/locations/us-central1-a/queuedResources/your-queued-resource-id

コマンドフラグの説明

queued-resource-request-id
キューに入れられたリソース リクエストのユーザー割り当て ID。
project
キューに格納されたリソースが割り当てられているプロジェクトの ID。
zone
Cloud TPU を作成するゾーン

Console

  1. Google Cloud コンソールの [TPU] ページに移動します。

    [TPU] に移動

  2. [キューに登録されているリソース] タブをクリックします。

  3. キューに登録されているリソース リクエストの名前をクリックします。

TPU がプロビジョニングされたら、TPU ページに移動して TPU を見つけ、対応するキューに格納されたリソース リクエストの名前をクリックして、キューに格納されたリソース リクエストの詳細を確認することもできます。

Java

Cloud TPU に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。

import com.google.cloud.tpu.v2alpha1.GetQueuedResourceRequest;
import com.google.cloud.tpu.v2alpha1.QueuedResource;
import com.google.cloud.tpu.v2alpha1.TpuClient;
import java.io.IOException;

public class GetQueuedResource {
  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Google Cloud project.
    String projectId = "YOUR_PROJECT_ID";
    // The zone in which the TPU was created.
    String zone = "us-central1-f";
    // The name for your Queued Resource.
    String queuedResourceId = "QUEUED_RESOURCE_ID";

    getQueuedResource(projectId, zone, queuedResourceId);
  }

  // Get a Queued Resource.
  public static QueuedResource getQueuedResource(
      String projectId, String zone, String queuedResourceId) throws IOException {
    String name = 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()) {
      GetQueuedResourceRequest request =
          GetQueuedResourceRequest.newBuilder().setName(name).build();

      return tpuClient.getQueuedResource(request);
    }
  }
}

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-b"
# queued_resource_name = "resource-name"

client = tpu_v2alpha1.TpuClient()
name = (
    f"projects/{project_id}/locations/{zone}/queuedResources/{queued_resource_name}"
)
resource = client.get_queued_resource(name=name)
print("Resource name:", resource.name)
print(resource.state)
# Example response:
# Resource name: projects/{project_id}/locations/{zone}/queuedResources/resource-name
# State.ACTIVE

リクエストが失敗した場合、出力にエラー情報が含まれます。リソースを待機しているリクエストの場合、出力は次のようになります。

gcloud

    name: projects/your-project-id/locations/us-central1-a/queuedResources/your-queued-resource-id
    state:
    state: WAITING_FOR_RESOURCES
    tpu:
    nodeSpec:
    - node:
        acceleratorType: v4-8
        bootDisk: {}
        networkConfig:
            enableExternalIps: true
        queuedResource: projects/your-project-number/locations/us-central1-a/queuedResources/your-queued-resource-id
        runtimeVersion: v2-alpha-tpuv5-lite
        schedulingConfig: {}
        serviceAccount: {}
        shieldedInstanceConfig: {}
        useTpuVm: true
        nodeId: your-node-id
        parent: projects/your-project-number/locations/us-central1-a

Console

[キューに格納されたリソースのステータス] フィールドに「リソースを待機中」と表示されます。

プロジェクト内のキューに入れられたリソース リクエストを一覧表示する

プロジェクト内のキューに入れられたリソース リクエストを一覧表示します。

gcloud

gcloud compute tpus queued-resources list --project your-project-id \
    --zone us-central1-a

コマンドフラグの説明

project
キューに格納されたリソースが割り当てられているプロジェクト。 Google Cloud
zone
Cloud TPU を作成するゾーン

curl

curl -X GET -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://tpu.googleapis.com/v2/projects/your-project-id/locations/your-zone/queuedResources

コマンドフラグの説明

project
キューに格納されたリソースが割り当てられているプロジェクト。 Google Cloud
zone
Cloud TPU を作成するゾーン

Console

  1. Google Cloud コンソールの [TPU] ページに移動します。

    [TPU] に移動

  2. [キューに登録されているリソース] タブをクリックします。

Java

Cloud TPU に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。

import com.google.cloud.tpu.v2alpha1.ListQueuedResourcesRequest;
import com.google.cloud.tpu.v2alpha1.QueuedResource;
import com.google.cloud.tpu.v2alpha1.TpuClient;
import com.google.cloud.tpu.v2alpha1.TpuClient.ListQueuedResourcesPage;
import java.io.IOException;

public class ListQueuedResources {
  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Google Cloud project.
    String projectId = "YOUR_PROJECT_ID";
    // The zone in which the TPU was created.
    String zone = "us-central1-a";

    listQueuedResources(projectId, zone);
  }

  // List Queued Resources.
  public static ListQueuedResourcesPage listQueuedResources(
      String projectId, String zone) throws IOException {
    String parent = String.format("projects/%s/locations/%s", projectId, zone);
    // 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()) {
      ListQueuedResourcesRequest request =
          ListQueuedResourcesRequest.newBuilder().setParent(parent).build();
      ListQueuedResourcesPage response =  tpuClient.listQueuedResources(request).getPage();

      for (QueuedResource queuedResource : response.iterateAll()) {
        System.out.println(queuedResource.getName());
      }
      return response;
    }
  }
}

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-b"

client = tpu_v2alpha1.TpuClient()
parent = f"projects/{project_id}/locations/{zone}"
resources = client.list_queued_resources(parent=parent)
for resource in resources:
    print("Resource name:", resource.name)
    print("TPU id:", resource.tpu.node_spec[0].node_id)
# Example response:
# Resource name: projects/{project_id}/locations/{zone}/queuedResources/resource-name
# TPU id: tpu-name