Mengelola resource dalam antrean

Resource dalam antrean memungkinkan Anda meminta resource Cloud TPU dalam antrean. Saat Anda meminta resource yang diantrekan, permintaan akan ditambahkan ke antrean yang dikelola oleh layanan Cloud TPU. Saat tersedia, resource yang diminta akan ditetapkan ke project Google Cloud Anda untuk penggunaan eksklusif langsung. ID ini akan tetap ditetapkan ke project Anda kecuali jika Anda menghapusnya atau ID tersebut didahului. Hanya VM Spot TPU dan TPU preemptible yang memenuhi syarat untuk preempt.

Anda dapat menentukan waktu mulai dan berakhir opsional dalam permintaan resource yang diantrekan. Waktu mulai menentukan waktu paling awal untuk mengisi permintaan. Jika permintaan belum terisi pada waktu berakhir yang ditentukan, masa berlaku permintaan akan berakhir. Permintaan tetap berada dalam antrean setelah masa berlakunya habis.

Permintaan resource dalam antrean dapat berada dalam salah satu status berikut:

WAITING_FOR_RESOURCES
Permintaan telah lulus validasi awal dan telah ditambahkan ke antrean. Status ini akan tetap berlaku hingga ada resource gratis yang memadai untuk mulai menyediakan permintaan Anda atau interval alokasi berakhir. Jika permintaan tinggi, tidak semua permintaan dapat segera disediakan. Jika Anda memerlukan ketersediaan TPU yang lebih andal, pertimbangkan untuk membeli reservasi.
PROVISIONING
Permintaan telah dipilih dari antrean dan resource-nya sedang dialokasikan.
ACTIVE
Permintaan telah dialokasikan. Saat permintaan resource yang diantrean berada dalam status ACTIVE, Anda dapat mengelola VM TPU seperti yang dijelaskan dalam Mengelola TPU.
FAILED
Permintaan tidak dapat diselesaikan, baik karena ada masalah dengan permintaan atau resource yang diminta tidak tersedia dalam interval alokasi. Permintaan akan tetap berada dalam antrean hingga dihapus secara eksplisit.
SUSPENDING
Resource yang terkait dengan permintaan sedang dihapus.
SUSPENDED
Resource yang ditentukan dalam permintaan telah dihapus. Jika permintaan berada dalam status SUSPENDED, permintaan tersebut tidak lagi memenuhi syarat untuk alokasi lebih lanjut.

Prasyarat

Sebelum menjalankan perintah dalam panduan ini, Anda harus menginstal Google Cloud CLI, membuat project Google Cloud , dan mengaktifkan Cloud TPU API. Untuk mengetahui petunjuknya, lihat Menyiapkan lingkungan Cloud TPU.

Meminta resource dalam antrean on demand

Resource on-demand tidak akan di-preempt, tetapi kuota on-demand tidak menjamin bahwa akan ada cukup resource Cloud TPU yang tersedia untuk memenuhi permintaan Anda. Untuk informasi selengkapnya tentang resource on-demand, lihat Jenis kuota.

gcloud

gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project \
    --zone us-central2-b \
    --accelerator-type v4-8 \
    --runtime-version tpu-vm-tf-2.18.0-pjrt

Deskripsi parameter perintah

queued-resource-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
node-id
ID TPU yang ditetapkan pengguna yang dibuat saat permintaan resource yang diantrekan dialokasikan.
project
Project
Anda Google Cloud .
zone
Zona tempat Anda berencana membuat Cloud TPU.
accelerator-type
Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.
runtime-version
Versi software 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-central2-b',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v4-8',
        'runtime_version': 'tpu-vm-tf-2.18.0-pjrt',
        }
    }
    }
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-central2-b/queuedResources?queued_resource_id=your-queued-resource-id

Deskripsi parameter perintah

queued-resource-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
node-id
ID TPU yang ditetapkan pengguna yang dibuat saat permintaan resource yang diantrekan dialokasikan.
project
Project
Anda Google Cloud .
zone
Zona tempat Anda berencana membuat Cloud TPU.
accelerator-type
Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.
runtime-version
Versi software Cloud TPU.

Konsol

  1. Di konsol Google Cloud, buka halaman TPUs:

    Buka TPU

  2. Klik Buat TPU.

  3. Di kolom Nama, masukkan nama untuk TPU Anda.

  4. Di kotak Zone, pilih zona tempat Anda ingin membuat TPU.

  5. Di kotak TPU type, pilih jenis akselerator. Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.

  6. Di kotak Versi software TPU, pilih versi software. Saat membuat VM Cloud TPU, versi software TPU menentukan versi runtime TPU yang akan diinstal. Untuk mengetahui informasi selengkapnya, lihat Image VM TPU.

  7. Klik tombol Aktifkan antrean.

  8. Di kolom Queued resource name, masukkan nama untuk permintaan resource yang diantrekan.

  9. Klik Buat untuk membuat permintaan resource yang diantrekan.

Java

Untuk melakukan autentikasi ke Cloud TPU, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

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-f";
    // 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 = "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 tpuSoftwareVersion = "tpu-vm-tf-2.14.1";
    // 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

Untuk melakukan autentikasi ke Cloud TPU, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

from google.cloud import tpu_v2alpha1

# TODO(developer): Update and un-comment below lines
# project_id = "your-project-id"
# zone = "us-central1-b"
# tpu_name = "tpu-name"
# tpu_type = "v2-8"
# runtime_version = "tpu-vm-tf-2.17.0-pjrt"
# 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 = 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.state)
# Example response:
# projects/[project_id]/locations/[zone]/queuedResources/resource-name
# State.WAITING_FOR_RESOURCES

Ukuran slice default untuk resource yang diantrekan sesuai permintaan

Saat menggunakan kuota on-demand, Anda harus meminta ukuran slice yang kurang dari batas default untuk jenis akselerator yang Anda gunakan. Permintaan yang melebihi batas default akan ditolak oleh sistem.

Tabel berikut menunjukkan jenis TPU dan batas defaultnya.

Jenis akselerator Batas default (dalam jumlah TensorCore)
v2 128
v3 128
v4 384
v5 32

Jika Anda memerlukan ukuran slice yang lebih besar, hubungi dukungan Cloud TPU untuk mendapatkan informasi tambahan.

Meminta resource yang diantrekan menggunakan reservasi

Anda dapat meminta resource yang diantrekan menggunakan reservasi. Untuk membeli reservasi, hubungi Google Cloud tim akun Anda.

gcloud

gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project \
    --zone us-central2-b \
    --accelerator-type v4-8 \
    --runtime-version tpu-vm-tf-2.18.0-pjrt \
    --reserved

Deskripsi parameter perintah

queued-resource-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
node-id
ID TPU yang ditetapkan pengguna yang dibuat saat permintaan resource yang diantrekan dialokasikan.
project
Project
Anda Google Cloud .
zone
Zona tempat Anda berencana membuat Cloud TPU.
accelerator-type
Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.
runtime-version
Versi software Cloud TPU.
reserved
Gunakan tanda ini saat meminta resource yang diantrekan sebagai bagian dari reservasi 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-central2-b',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v4-8',
        'runtime_version': 'tpu-vm-tf-2.18.0-pjrt',
        }
    }
    },
    'guaranteed': {
    'reserved': true,
    }
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-central2-b/queuedResources?queued_resource_id=your-queued-resource-id

Deskripsi parameter perintah

queued-resource-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
node-id
ID TPU yang ditetapkan pengguna yang dibuat saat permintaan resource yang diantrekan dialokasikan.
project
Project
Anda Google Cloud .
zone
Zona tempat Anda berencana membuat Cloud TPU.
accelerator-type
Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.
runtime-version
Versi software Cloud TPU.
reserved
Gunakan tanda ini saat meminta resource yang diantrekan sebagai bagian dari reservasi Cloud TPU.

Konsol

  1. Di konsol Google Cloud, buka halaman TPUs:

    Buka TPU

  2. Klik Buat TPU.

  3. Di kolom Nama, masukkan nama untuk TPU Anda.

  4. Di kotak Zone, pilih zona tempat Anda ingin membuat TPU.

  5. Di kotak TPU type, pilih jenis akselerator. Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.

  6. Di kotak Versi software TPU, pilih versi software. Saat membuat VM Cloud TPU, versi software TPU menentukan versi runtime TPU yang akan diinstal. Untuk mengetahui informasi selengkapnya, lihat Image VM TPU.

  7. Klik tombol Aktifkan antrean.

  8. Di kolom Queued resource name, masukkan nama untuk permintaan resource yang diantrekan.

  9. Luaskan bagian Management.

  10. Centang kotak Gunakan reservasi yang ada.

  11. Klik Buat untuk membuat permintaan resource yang diantrekan.

Meminta resource yang diantrekan TPU Spot VM

Spot VM adalah resource yang dapat di-preempt dan ditetapkan ke workload lain kapan saja. Resource Spot VM harganya lebih murah, dan Anda mungkin mendapatkan akses ke resource lebih cepat dibandingkan dengan permintaan VM non-Spot. Untuk mengetahui informasi selengkapnya tentang Spot VM TPU, lihat Mengelola Spot VM TPU.

gcloud

gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project-id \
    --zone us-central2-b \
    --accelerator-type v4-8 \
    --runtime-version tpu-vm-tf-2.18.0-pjrt \
    --spot

Deskripsi parameter perintah

queued-resource-request-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
node-id
ID TPU yang ditentukan pengguna yang dibuat sebagai respons terhadap permintaan.
project
ID project tempat resource yang diantrekan dialokasikan.
zone
Zona tempat Anda berencana membuat Cloud TPU.
accelerator-type
Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.
runtime-version
Versi software Cloud TPU.
spot
Flag boolean yang menentukan bahwa resource yang diantrekan adalah 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-central2-b',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v4-8',
        'runtime_version': 'tpu-vm-tf-2.18.0-pjrt'
        }
    }
    },
    'spot': {}
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-central2-b/queuedResources?queued_resource_id=your-queued-resource-id

Deskripsi parameter perintah

queued-resource-request-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
node-id
ID TPU yang ditentukan pengguna yang dibuat sebagai respons terhadap permintaan.
project
ID project tempat resource yang diantrekan dialokasikan.
zone
Zona tempat Anda berencana membuat Cloud TPU.
accelerator-type
Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.
runtime-version
Versi software Cloud TPU.
spot
Flag boolean yang menentukan bahwa resource yang diantrekan adalah Spot VM.

Konsol

  1. Di konsol Google Cloud, buka halaman TPUs:

    Buka TPU

  2. Klik Buat TPU.

  3. Di kolom Nama, masukkan nama untuk TPU Anda.

  4. Di kotak Zone, pilih zona tempat Anda ingin membuat TPU.

  5. Di kotak TPU type, pilih jenis akselerator. Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.

  6. Di kotak Versi software TPU, pilih versi software. Saat membuat VM Cloud TPU, versi software TPU menentukan versi runtime TPU yang akan diinstal. Untuk mengetahui informasi selengkapnya, lihat Image VM TPU.

  7. Klik tombol Aktifkan antrean.

  8. Di kolom Queued resource name, masukkan nama untuk permintaan resource yang diantrekan.

  9. Luaskan bagian Management.

  10. Centang kotak Make this a TPU Spot VM.

  11. Klik Buat.

Java

Untuk melakukan autentikasi ke Cloud TPU, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

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-f";
    // 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 = "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 tpuSoftwareVersion = "tpu-vm-tf-2.14.1";
    // 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

Untuk melakukan autentikasi ke Cloud TPU, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

from google.cloud import tpu_v2alpha1

# TODO(developer): Update and un-comment below lines
# project_id = "your-project-id"
# zone = "us-central1-b"
# tpu_name = "tpu-name"
# tpu_type = "v2-8"
# runtime_version = "tpu-vm-tf-2.17.0-pjrt"
# 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 = 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.state)
# Example response:
# projects/[project_id]/locations/[zone]/queuedResources/resource-name
# State.WAITING_FOR_RESOURCES

Meminta resource yang diantrekan untuk dialokasikan sebelum atau setelah waktu yang ditentukan

Anda dapat menentukan waktu mulai atau waktu akhir opsional dalam permintaan resource yang diantrekan. Waktu mulai atau durasi mulai menentukan waktu paling awal untuk mengisi permintaan. Waktu berakhir atau durasi berakhir menentukan berapa lama permintaan tetap valid. Jika permintaan belum terisi pada waktu akhir yang ditentukan atau dalam durasi yang ditentukan, permintaan akan habis masa berlakunya. Setelah masa berlakunya habis, permintaan tersebut tetap berada dalam antrean, tetapi tidak lagi memenuhi syarat untuk dialokasikan.

Anda juga dapat menentukan interval alokasi dengan menentukan waktu mulai atau durasi dan waktu berakhir atau durasi.

Untuk mengetahui daftar format stempel waktu dan durasi yang didukung, lihat Tanggal waktu.

Meminta resource yang diantrekan untuk dipenuhi setelah waktu yang ditentukan

Dalam permintaan resource yang diantrekan, Anda dapat menentukan waktu atau durasi setelah resource harus dialokasikan.

gcloud

Perintah berikut meminta TPU v4-4096 yang akan dialokasikan setelah pukul 09.00 pada 14 Desember 2022.

gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project-id \
    --zone us-central2-b \
    --accelerator-type v4-4096 \
    --runtime-version tpu-vm-tf-2.18.0-pod-pjrt \
    --valid-after-time 2022-12-14T09:00:00Z

Deskripsi parameter perintah

queued-resource-request-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
node-id
ID TPU yang ditentukan pengguna yang dibuat sebagai respons terhadap permintaan.
project
Project Google Cloud tempat resource yang diantrekan dialokasikan.
zone
Zona tempat Anda berencana membuat Cloud TPU.
accelerator-type
Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.
runtime-version
Versi software Cloud TPU.
valid-after-time
Waktu setelah resource dialokasikan. Untuk mengetahui informasi selengkapnya tentang format durasi, lihat tanggal waktu topik Google Cloud CLI.

curl

Perintah berikut meminta TPU v4-4096 yang akan dialokasikan setelah pukul 09.00 pada 14 Desember 2022.

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-central2-b',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v4-4096',
        'runtime_version': 'tpu-vm-tf-2.18.0-pod-pjrt',
        }
    }
    },
    'queueing_policy': {
    'valid_after_time': {
        'seconds': 2022-12-14T09:00:00Z
    }
    }
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-central2-b/queuedResources?queued_resource_id=your-queued-resource-id

Deskripsi parameter perintah

queued-resource-request-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
node-id
ID TPU yang ditentukan pengguna yang dibuat sebagai respons terhadap permintaan.
project
Project Google Cloud tempat resource yang diantrekan dialokasikan.
zone
Zona tempat Anda berencana membuat Cloud TPU.
accelerator-type
Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.
runtime-version
Versi software Cloud TPU.
valid-after-time
Waktu setelah resource dialokasikan. Untuk mengetahui informasi selengkapnya tentang format durasi, lihat tanggal waktu topik Google Cloud CLI.

Konsol

  1. Di konsol Google Cloud, buka halaman TPUs:

    Buka TPU

  2. Klik Buat TPU.

  3. Di kolom Nama, masukkan nama untuk TPU Anda.

  4. Di kotak Zone, pilih zona tempat Anda ingin membuat TPU.

  5. Di kotak TPU type, pilih jenis akselerator. Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.

  6. Di kotak Versi software TPU, pilih versi software. Saat membuat VM Cloud TPU, versi software TPU menentukan versi runtime TPU yang akan diinstal. Untuk mengetahui informasi selengkapnya, lihat Image VM TPU.

  7. Klik tombol Aktifkan antrean.

  8. Di kolom Queued resource name, masukkan nama untuk permintaan resource yang diantrekan.

  9. Di kolom Start request on, masukkan waktu setelah resource harus dialokasikan.

  10. Klik Buat untuk membuat permintaan resource yang diantrekan.

Contoh berikut meminta v4-32 dialokasikan setelah enam jam.

gcloud

    gcloud compute tpus queued-resources create your-queued-resource-id \
        --node-id your-node-id \
        --project your-project-id \
        --zone us-central2-b \
        --accelerator-type v4-32 \
        --runtime-version tpu-vm-tf-2.18.0-pod-pjrt \
        --valid-after-duration 6h

Deskripsi parameter perintah

queued-resource-request-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
node-id
ID TPU yang ditentukan pengguna yang dibuat sebagai respons terhadap permintaan.
project
Project Google Cloud tempat resource yang diantrekan dialokasikan.
zone
Zona tempat Anda berencana membuat Cloud TPU.
accelerator-type
Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.
runtime-version
Versi software Cloud TPU.
valid-after-duration
Durasi sebelum TPU tidak boleh disediakan. Untuk mengetahui informasi selengkapnya tentang format durasi, lihat Tanggal waktu topik 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-central2-b',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v4-32',
        'runtime_version': 'tpu-vm-tf-2.18.0-pod-pjrt',
        }
    }
    },
'queueing_policy': {
    'valid_after_duration': {
        'seconds': 21600
    }
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-central2-b/queuedResources?queued_resource_id=your-queued-resource-id

Deskripsi parameter perintah

queued-resource-request-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
node-id
ID TPU yang ditentukan pengguna yang dibuat sebagai respons terhadap permintaan.
project
Project Google Cloud tempat resource yang diantrekan dialokasikan.
zone
Zona tempat Anda berencana membuat Cloud TPU.
accelerator-type
Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.
runtime-version
Versi software Cloud TPU.
valid-after-duration
Durasi sebelum TPU tidak boleh disediakan. Untuk mengetahui informasi selengkapnya tentang format durasi, lihat Tanggal waktu topik Google Cloud CLI

Java

Untuk melakukan autentikasi ke Cloud TPU, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

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 = "tpu-vm-tf-2.14.1";
    // 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();
    }
  }
}

Meminta resource yang diantrekan yang akan berakhir masa berlakunya setelah waktu tertentu

Dalam permintaan resource yang diantrekan, Anda dapat menentukan berapa lama permintaan resource yang diantrekan tetap valid. Jika permintaan belum dipenuhi pada waktu atau durasi yang Anda tentukan, masa berlaku permintaan akan berakhir.

gcloud

Perintah berikut meminta TPU v4-4096. Jika permintaan tidak dipenuhi sebelum 14 Desember 2022 pukul 09.00, masa berlaku permintaan akan berakhir.

gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project-id \
    --zone us-central2-b \
    --accelerator-type v4-4096 \
    --runtime-version tpu-vm-tf-2.18.0-pod-pjrt \
    --valid-until-time 2022-12-14T09:00:00Z

Deskripsi parameter perintah

queued-resource-request-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
node-id
ID TPU yang ditentukan pengguna yang dibuat sebagai respons terhadap permintaan.
project
ID project tempat resource yang diantrekan dialokasikan.
zone
Zona tempat Anda berencana membuat Cloud TPU.
accelerator-type
Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.
runtime-version
Versi software Cloud TPU.
valid-until-time
Waktu setelah permintaan dibatalkan. Untuk mengetahui informasi selengkapnya tentang format durasi, lihat tanggal waktu topik Google Cloud CLI.

curl

Perintah berikut meminta TPU v4-4096. Jika permintaan tidak dipenuhi sebelum 14 Desember 2022 pukul 09.00, masa berlaku permintaan akan berakhir.

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-central2-b',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v4-4096',
        'runtime_version': 'tpu-vm-tf-2.18.0-pod-pjrt',
        }
    }
    },
    'queueing_policy': {
    'valid_until_time': {
        'seconds': 1655197200
    }
    }
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-central2-b/queuedResources?queued_resource_id=your-queued-resource-id

Deskripsi parameter perintah

queued-resource-request-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
node-id
ID TPU yang ditentukan pengguna yang dibuat sebagai respons terhadap permintaan.
project
ID project tempat resource yang diantrekan dialokasikan.
zone
Zona tempat Anda berencana membuat Cloud TPU.
accelerator-type
Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.
runtime-version
Versi software Cloud TPU.
valid-until-time
Waktu setelah permintaan dibatalkan. Untuk mengetahui informasi selengkapnya tentang format durasi, lihat tanggal dan waktu topik Google Cloud CLI.

Konsol

  1. Di konsol Google Cloud, buka halaman TPUs:

    Buka TPU

  2. Klik Buat TPU.

  3. Di kolom Nama, masukkan nama untuk TPU Anda.

  4. Di kotak Zone, pilih zona tempat Anda ingin membuat TPU.

  5. Di kotak TPU type, pilih jenis akselerator. Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.

  6. Di kotak Versi software TPU, pilih versi software. Saat membuat VM Cloud TPU, versi software TPU menentukan versi runtime TPU yang akan diinstal. Untuk mengetahui informasi selengkapnya, lihat Gambar VM TPU.

  7. Klik tombol Aktifkan antrean.

  8. Di kolom Queued resource name, masukkan nama untuk permintaan resource yang diantrekan.

  9. Di kolom Cancel request on, masukkan waktu saat permintaan resource dalam antrean akan berakhir masa berlakunya jika tidak terisi.

  10. Klik Buat untuk membuat permintaan resource yang diantrekan.

Contoh berikut meminta v4-32. Masa berlaku permintaan akan berakhir jika tidak terisi dalam enam jam.

gcloud

    gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project-id \
    --zone us-central2-b \
    --accelerator-type v4-32 \
    --runtime-version tpu-vm-tf-2.18.0-pod-pjrt \
    --valid-until-duration 6h

Deskripsi parameter perintah

queued-resource-request-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
node-id
ID TPU yang ditentukan pengguna yang dibuat sebagai respons terhadap permintaan.
project
Project Google Cloud tempat resource yang diantrekan dialokasikan.
zone
Zona tempat Anda berencana membuat Cloud TPU.
accelerator-type
Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.
runtime-version
Versi software Cloud TPU.
valid-until-duration
Durasi validitas permintaan. Untuk mengetahui informasi selengkapnya tentang format durasi, lihat tanggal dan waktu topik 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-central2-b',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v4-32',
        'runtime_version': 'tpu-vm-tf-2.18.0-pod-pjrt',
        }
    }
    },
'queueing_policy': {
    'valid_until_duration': {
        'seconds': 21600
    }
}" \
https://tpu.googleapis.com/v2alpha1/projects/your-project-id/locations/us-central2-b/queuedResources?queued_resource_id=your-queued-resource-id

Deskripsi parameter perintah

queued-resource-request-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
node-id
ID TPU yang ditentukan pengguna yang dibuat sebagai respons terhadap permintaan.
project
Project Google Cloud tempat resource yang diantrekan dialokasikan.
zone
Zona tempat Anda berencana membuat Cloud TPU.
accelerator-type
Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.
runtime-version
Versi software Cloud TPU.
valid-until-duration
Durasi validitas permintaan. Untuk mengetahui informasi selengkapnya tentang format durasi, lihat tanggal dan waktu topik Google Cloud CLI

Python

Untuk melakukan autentikasi ke Cloud TPU, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

from google.cloud import tpu_v2alpha1

# TODO(developer): Update and un-comment below lines
# project_id = "your-project-id"
# zone = "us-central1-b"
# tpu_name = "tpu-name"
# tpu_type = "v2-8"
# runtime_version = "tpu-vm-tf-2.17.0-pjrt"
# 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 = 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

Meminta resource yang diantrekan untuk dialokasikan dalam interval yang ditentukan

Anda dapat menentukan interval alokasi dengan menentukan waktu mulai atau durasi dan waktu akhir atau durasi.

gcloud

Perintah berikut meminta v4-32 dalam 5 jam 30 menit dari waktu saat ini, yang akan dibuat paling lambat 14 Desember 2022 pukul 09.00.

gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project-id \
    --zone us-central2-b \
    --accelerator-type v4-32 \
    --runtime-version tpu-vm-tf-2.18.0-pod-pjrt \
    --valid-after-duration 5h30m \
    --valid-until-time 2022-12-14T09:00:00Z

Deskripsi flag perintah

queued-resource-request-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
node-id
ID TPU yang ditentukan pengguna yang dibuat sebagai respons terhadap permintaan.
project
ID project tempat resource yang diantrekan dialokasikan.
zone
Zona tempat Anda berencana membuat Cloud TPU.
accelerator-type
Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.
runtime-version
Versi software Cloud TPU.
valid-until-timw
Waktu setelah permintaan dibatalkan. Untuk mengetahui informasi selengkapnya tentang format durasi, lihat tanggal waktu topik Google Cloud CLI.
valid-until-duration
Durasi validitas permintaan. Untuk mengetahui informasi selengkapnya tentang format durasi, lihat tanggal waktu topik Google Cloud CLI.

curl

Perintah berikut meminta v4-32 dalam 5 jam 30 menit dari waktu saat ini, yang akan dibuat paling lambat 14 Desember 2022 pukul 09.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-central2-b',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v4-32',
        'runtime_version': 'tpu-vm-tf-2.18.0-pod-pjrt',
        }
    }
    },
'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-central2-b/queuedResources?queued_resource_id=your-queued-resource-id

Deskripsi flag perintah

queued-resource-request-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
node-id
ID TPU yang ditentukan pengguna yang dibuat sebagai respons terhadap permintaan.
project
ID project tempat resource yang diantrekan dialokasikan.
zone
Zona tempat Anda berencana membuat Cloud TPU.
accelerator-type
Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.
runtime-version
Versi software Cloud TPU.
valid-until-timw
Waktu setelah permintaan dibatalkan. Untuk mengetahui informasi selengkapnya tentang format durasi, lihat tanggal dan waktu topik Google Cloud CLI.
valid-until-duration
Durasi validitas permintaan. Untuk mengetahui informasi selengkapnya tentang format durasi, lihat tanggal dan waktu topik Google Cloud CLI.

Konsol

  1. Di konsol Google Cloud, buka halaman TPUs:

    Buka TPU

  2. Klik Buat TPU.

  3. Di kolom Nama, masukkan nama untuk TPU Anda.

  4. Di kotak Zone, pilih zona tempat Anda ingin membuat TPU.

  5. Di kotak TPU type, pilih jenis akselerator. Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.

  6. Di kotak Versi software TPU, pilih versi software. Saat membuat VM Cloud TPU, versi software TPU menentukan versi runtime TPU yang akan diinstal. Untuk mengetahui informasi selengkapnya, lihat Gambar VM TPU.

  7. Klik tombol Aktifkan antrean.

  8. Di kolom Queued resource name, masukkan nama untuk permintaan resource yang diantrekan.

  9. Di kolom Start request on, masukkan waktu setelah resource harus dialokasikan.

  10. Di kolom Cancel request on, masukkan waktu saat permintaan resource yang diantrekan akan berakhir masa berlakunya jika tidak terisi.

  11. Klik Buat untuk membuat permintaan resource yang diantrekan.

Meminta resource yang diantrekan dengan skrip startup

Anda dapat menentukan skrip yang akan dijalankan pada resource yang diantrekan setelah disediakan.

gcloud

Saat menggunakan perintah gcloud, Anda dapat menggunakan flag --metadata atau --metadata-from-file untuk menentukan perintah skrip atau file yang berisi kode skrip. Contoh berikut membuat permintaan resource yang diantrekan yang akan menjalankan skrip startup-script.sh.

gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project \
    --zone us-central2-b \
    --accelerator-type v4-8 \
    --runtime-version tpu-vm-tf-2.12.0 \
    --metadata-from-file='startup-script=startup-script.sh'

Deskripsi flag perintah

queued-resource-request-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
node-id
ID TPU yang ditentukan pengguna yang dibuat sebagai respons terhadap permintaan.
project
ID project tempat resource yang diantrekan dialokasikan.
zone
Zona tempat Anda berencana membuat Cloud TPU.
accelerator-type
Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.
runtime-version
Versi software Cloud TPU.
validInterval
Waktu selama permintaan valid, setelah itu permintaan akan dibatalkan. Untuk mengetahui informasi selengkapnya tentang format durasi, lihat tanggal dan waktu topik Google Cloud CLI.
metadata-from-file
Menentukan file yang berisi metadata. Jika Anda tidak menentukan jalur yang sepenuhnya memenuhi syarat ke file metadata, perintah akan menganggapnya berada di direktori saat ini. Dalam contoh ini, file berisi skrip startup yang dijalankan saat resource yang diantrekan disediakan.
metadata
Menentukan metadata untuk permintaan. Dalam contoh ini, metadata adalah perintah skrip startup yang dijalankan saat resource yang diantrekan disediakan.

curl

Saat menggunakan curl, Anda harus menyertakan kode skrip dalam konten JSON. Contoh berikut menyertakan skrip inline dalam isi 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-central2-b',
        node_id: 'your-node-id',
        node: {
            accelerator_type: 'v2-8',
            runtime_version: 'tpu-vm-tf-2.18.0-pjrt',
            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-central2-b/queuedResources?queued_resource_id=your-queued-resource-id

Deskripsi flag perintah

queued-resource-request-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
node-id
ID TPU yang ditentukan pengguna yang dibuat sebagai respons terhadap permintaan.
project
ID project tempat resource yang diantrekan dialokasikan.
zone
Zona tempat Anda berencana membuat Cloud TPU.
accelerator-type
Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.
runtime-version
Versi software Cloud TPU.
validInterval
Waktu selama permintaan valid, setelah itu permintaan dibatalkan. Untuk mengetahui informasi selengkapnya tentang format durasi, lihat tanggal dan waktu topik Google Cloud CLI.
metadata-from-file
Menentukan file yang berisi metadata. Jika Anda tidak menentukan jalur yang sepenuhnya memenuhi syarat ke file metadata, perintah akan menganggapnya berada di direktori saat ini. Dalam contoh ini, file berisi skrip startup yang dijalankan saat resource yang diantrekan disediakan.
metadata
Menentukan metadata untuk permintaan. Dalam contoh ini, metadata adalah perintah skrip startup yang dijalankan saat resource yang diantrekan disediakan.

Java

Untuk melakukan autentikasi ke Cloud TPU, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

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 = "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 tpuSoftwareVersion = "tpu-vm-tf-2.14.1";
    // 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

Untuk melakukan autentikasi ke Cloud TPU, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

from google.cloud import tpu_v2alpha1

# TODO(developer): Update and un-comment below lines
# project_id = "your-project-id"
# zone = "us-central1-b"
# tpu_name = "tpu-name"
# tpu_type = "v2-8"
# runtime_version = "tpu-vm-tf-2.17.0-pjrt"
# 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 = 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    '}

Meminta resource yang diantrekan dengan jaringan dan subnetwork yang ditentukan

Dalam permintaan resource yang diantrekan, Anda dapat menentukan jaringan dan subnetwork yang ingin dihubungkan ke TPU.

gcloud

gcloud compute tpus queued-resources create your-queued-resource-id \
    --node-id your-node-id \
    --project your-project \
    --zone us-central2-b \
    --accelerator-type v4-8 \
    --runtime-version tpu-vm-tf-2.18.0-pjrt \
    --network network-name \
    --subnetwork subnetwork-name

Deskripsi parameter perintah

queued-resource-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
node-id
ID TPU yang ditetapkan pengguna yang dibuat saat permintaan resource yang diantrekan dialokasikan.
project
Project
Anda Google Cloud .
zone
Zona tempat Anda berencana membuat Cloud TPU.
accelerator-type
Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.
runtime-version
Versi software Cloud TPU.
reserved
Gunakan tanda ini saat meminta resource yang diantrekan sebagai bagian dari reservasi Cloud TPU.
network
Jaringan yang akan menjadi bagian dari resource yang diantrekan.
subnetwork
Subnetwork yang akan menjadi bagian dari resource yang diantrekan.

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-central2-b',
        'node_id': 'your-node-id',
        'node': {
        'accelerator_type': 'v4-8',
        'runtime_version': 'tpu-vm-tf-2.18.0-pjrt',
        '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-central2-b/queuedResources?queued_resource_id=your-queued-resource-id

Deskripsi parameter perintah

queued-resource-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
node-id
ID TPU yang ditetapkan pengguna yang dibuat saat permintaan resource yang diantrekan dialokasikan.
project
Project
Anda Google Cloud .
zone
Zona tempat Anda berencana membuat Cloud TPU.
accelerator-type
Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.
runtime-version
Versi software Cloud TPU.
reserved
Gunakan tanda ini saat meminta resource yang diantrekan sebagai bagian dari reservasi Cloud TPU.
network
Jaringan yang akan menjadi bagian dari resource yang diantrekan.
subnetwork
Subnetwork yang akan menjadi bagian dari resource yang diantrekan.

Konsol

  1. Di konsol Google Cloud, buka halaman TPUs:

    Buka TPU

  2. Klik Buat TPU.

  3. Di kolom Nama, masukkan nama untuk TPU Anda.

  4. Di kotak Zone, pilih zona tempat Anda ingin membuat TPU.

  5. Di kotak TPU type, pilih jenis akselerator. Jenis akselerator menentukan versi dan ukuran Cloud TPU yang ingin Anda buat. Untuk mengetahui informasi selengkapnya tentang jenis akselerator yang didukung untuk setiap versi TPU, lihat versi TPU.

  6. Di kotak Versi software TPU, pilih versi software. Saat membuat VM Cloud TPU, versi software TPU menentukan versi runtime TPU yang akan diinstal. Untuk mengetahui informasi selengkapnya, lihat Gambar VM TPU.

  7. Klik tombol Aktifkan antrean.

  8. Di kolom Queued resource name, masukkan nama untuk permintaan resource yang diantrekan.

  9. Luaskan bagian Jaringan.

  10. Di kolom Network dan Subnetwork, pilih jaringan dan subnetwork yang ingin Anda gunakan.

  11. Klik Buat untuk membuat permintaan resource yang diantrekan.

Java

Untuk melakukan autentikasi ke Cloud TPU, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

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 = "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 tpuSoftwareVersion = "tpu-vm-tf-2.14.1";
    // 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

Untuk melakukan autentikasi ke Cloud TPU, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

from google.cloud import tpu_v2alpha1

# TODO(developer): Update and un-comment below lines
# project_id = "your-project-id"
# zone = "us-central1-b"
# tpu_name = "tpu-name"
# tpu_type = "v2-8"
# runtime_version = "tpu-vm-tf-2.17.0-pjrt"
# 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 = 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

Menghapus permintaan resource yang diantrekan

Anda dapat menghapus permintaan resource yang diantrekan dan TPU yang terkait dengan permintaan tersebut dengan menghapus permintaan resource yang diantrekan:

gcloud

Teruskan flag --force ke perintah queued-resource delete:

gcloud compute tpus queued-resources delete your-queued-resource-id \
    --project my-project \
    --zone us-central2-b \
    --force \
    --async

Deskripsi flag perintah

your-queued-resource-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
project
Project Google Cloud tempat resource yang diantrekan dialokasikan.
zone
Zona Cloud TPU yang akan dihapus.
force
Hapus VM TPU dan permintaan resource yang diantrekan.

curl

Gunakan parameter kueri force=true dalam permintaan curl Anda:

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

Deskripsi flag perintah

your-queued-resource-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
project
Project Google Cloud tempat resource yang diantrekan dialokasikan.
zone
Zona Cloud TPU yang akan dihapus.
force
Hapus VM TPU dan permintaan resource yang diantrekan.

Konsol

  1. Di konsol Google Cloud, buka halaman TPUs:

    Buka TPU

  2. Klik tab Resource dalam antrean.

  3. Centang kotak di samping permintaan resource yang ada dalam antrean.

  4. Klik Delete.

Java

Untuk melakukan autentikasi ke Cloud TPU, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

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

Untuk melakukan autentikasi ke Cloud TPU, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

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.")

Jika menghapus TPU secara langsung, Anda juga perlu menghapus resource yang diantrekan, seperti yang ditunjukkan dalam contoh berikut. Saat Anda menghapus TPU, permintaan resource yang diantrekan akan bertransisi ke status SUSPENDED, setelah itu permintaan resource yang diantrekan dapat dihapus.

gcloud

Hapus TPU:

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

Deskripsi flag perintah

project
Project Google Cloud tempat resource yang diantrekan dialokasikan.
zone
Zona Cloud TPU yang akan dihapus.
your-node-id
Nama TPU yang ingin Anda hapus.

Saat Anda menghapus TPU, resource yang diantrekan terkait akan berstatus SUSPENDING, lalu berstatus SUSPENDED. Jika resource dalam antrean berada dalam status SUSPENDED, Anda dapat menghapusnya:

gcloud compute tpus queued-resources delete your-queued-resource-id \
    --project your-project-id \
    --zone us-central2-b

Deskripsi flag perintah

queued-resource-request-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
project
Project Google Cloud tempat resource yang diantrekan dialokasikan.
zone
Zona Cloud TPU yang akan dihapus.

curl

Hapus 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-central2-b/nodes?node_id=your-node-id

Deskripsi flag perintah

project
Project Google Cloud tempat resource yang diantrekan dialokasikan.
zone
Zona Cloud TPU yang akan dihapus.
your-node-id
Nama TPU yang ingin Anda hapus.

Saat Anda menghapus TPU, resource yang diantrekan terkait akan berstatus SUSPENDING, lalu berstatus SUSPENDED. Jika resource dalam antrean berada dalam status SUSPENDED, Anda dapat menghapusnya:

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-central2-b/queuedResources/your-queued-resource-id

Deskripsi flag perintah

queued-resource-request-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
project
Project Google Cloud tempat resource yang diantrekan dialokasikan.
zone
Zona Cloud TPU yang akan dihapus.

Konsol

Menghapus TPU:

  1. Di konsol Google Cloud, buka halaman TPUs:

    Buka TPU

  2. Centang kotak di samping TPU Anda.

  3. Klik Delete.

Saat Anda menghapus TPU, resource yang diantrekan terkait akan beralih ke status Suspending, lalu status Suspended. Jika resource yang diantrekan berada dalam status Ditangguhkan, Anda dapat menghapusnya:

  1. Klik tab Resource dalam antrean.
  2. Centang kotak di samping permintaan resource yang ada dalam antrean.
  3. Klik Delete.

Java

Untuk melakukan autentikasi ke Cloud TPU, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

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");
    }
  }
}

Saat Anda menghapus TPU, resource yang diantrekan terkait akan berstatus SUSPENDING, lalu berstatus SUSPENDED. Saat resource yang diantrekan berada dalam status SUSPENDED, Anda dapat menghapusnya:

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

Untuk melakukan autentikasi ke Cloud TPU, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

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)

Saat Anda menghapus TPU, resource yang diantrekan terkait akan berstatus SUSPENDING, lalu berstatus SUSPENDED. Saat resource yang diantrekan berada dalam status SUSPENDED, Anda dapat menghapusnya:

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.")

Mengambil informasi status dan diagnostik tentang permintaan resource yang diantrekan

Ambil informasi status dan diagnostik tentang permintaan resource yang diantrekan:

gcloud

gcloud compute tpus queued-resources describe queued-resource-request-id \
    --project your-project-id \
    --zone us-central2-b

Deskripsi flag perintah

queued-resource-request-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
project
ID project tempat resource yang diantrekan dialokasikan.
zone
Zona tempat Anda berencana membuat 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-central2-b/queuedResources/your-queued-resource-id

Deskripsi flag perintah

queued-resource-request-id
ID yang ditetapkan pengguna untuk permintaan resource yang diantrekan.
project
ID project tempat resource yang diantrekan dialokasikan.
zone
Zona tempat Anda berencana membuat Cloud TPU.

Konsol

  1. Di konsol Google Cloud, buka halaman TPUs:

    Buka TPU

  2. Klik tab Resource dalam antrean.

  3. Klik nama permintaan resource yang diantrekan.

Setelah TPU disediakan, Anda juga dapat melihat detail tentang permintaan resource yang diantrekan dengan membuka halaman TPU, menemukan TPU, dan mengklik nama permintaan resource yang diantrekan yang sesuai.

Java

Untuk melakukan autentikasi ke Cloud TPU, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

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

Untuk melakukan autentikasi ke Cloud TPU, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

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.state)
# Example response:
# Resource name: projects/{project_id}/locations/{zone}/queuedResources/resource-name
# State.ACTIVE

Jika permintaan gagal, output akan berisi informasi error. Untuk permintaan yang menunggu resource, output-nya terlihat mirip dengan berikut ini:

gcloud

    name: projects/your-project-id/locations/us-central2-b/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-central2-b/queuedResources/your-queued-resource-id
        runtimeVersion: tpu-vm-tf-2.10.0
        schedulingConfig: {}
        serviceAccount: {}
        shieldedInstanceConfig: {}
        useTpuVm: true
        nodeId: your-node-id
        parent: projects/your-project-number/locations/us-central2-b

Konsol

Kolom Status resource dalam antrean menampilkan Menunggu resource.

Mencantumkan permintaan resource yang diantrekan di project Anda

Cantumkan permintaan resource yang diantrekan di project Anda:

gcloud

gcloud compute tpus queued-resources list --project your-project-id \
    --zone us-central2-b

Deskripsi flag perintah

project
Project Google Cloud tempat resource yang diantrekan dialokasikan.
zone
Zona tempat Anda berencana membuat 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-central2-b/queuedResources

Deskripsi flag perintah

project
Project Google Cloud tempat resource yang diantrekan dialokasikan.
zone
Zona tempat Anda berencana membuat Cloud TPU.

Konsol

  1. Di konsol Google Cloud, buka halaman TPUs:

    Buka TPU

  2. Klik tab Resource dalam antrean.

Java

Untuk melakukan autentikasi ke Cloud TPU, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

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

Untuk melakukan autentikasi ke Cloud TPU, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

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