Stay organized with collectionsSave and categorize content based on your preferences.
Manage queued resources
Queued resources enable you to request Cloud TPU resources in a queued manner.
When you request queued resources, the request is added to a queue maintained by
the Cloud TPU service. When the requested resource becomes available, it's
assigned to your Google Cloud project for your immediate exclusive use. It will remain
assigned to your project unless you delete it or it's preempted. Only
TPU Spot VMs and preemptible TPUs are eligible for preemption.
You can specify an optionalstart and end timein a queued resource request. The start time specifies the earliest time in
which to fill the request. If a request has not been filled by the specified end
time, the request expires. The request remains in the queue after it has expired.
Queued resource requests can be in one the following states:
WAITING_FOR_RESOURCES
The request has passed initial validation and has been added to the queue.
It remains in this state until there are sufficient free resources to begin
provisioning your request or theallocation intervalelapses. When demand is high, not all requests can be immediately
provisioned. If you need more reliable obtainability of TPUs, consider
purchasing a reservation.
PROVISIONING
The request has been selected from the queue and its resources are being allocated.
ACTIVE
The request has been allocated. When queued resource requests are in theACTIVEstate, you can manage your TPU VMs as described inManage TPUs.
FAILED
The request couldn't be completed, either because there is a problem with the
request or the requested resources were not available within the allocation interval.
The request remains in the queue until it is explicitly deleted.
SUSPENDING
The resources associated with the request are being deleted.
SUSPENDED
The resources specified in the request have been deleted. When a request
is in theSUSPENDEDstate, it's no longer eligible for further
allocation.
Prerequisites
Before you run the commands in this guide, you must install the Google Cloud CLI,
create a Google Cloud project, and enable the Cloud TPU API. For
instructions, seeSet up the Cloud TPU
environment.
If you're using one of theCloud Client Libraries, follow the setup
instructions for the language you're using:
On-demand resources won't be preempted, but on-demand quota doesn't guarantee
there will be enough available Cloud TPU resources to satisfy your request.
For more information about on-demand resources, seeQuota
types.
The accelerator type specifies the version and size of the Cloud TPU you want to create.
For more information about supported accelerator types for each TPU version, seeTPU versions.
The accelerator type specifies the version and size of the Cloud TPU you want to create.
For more information about supported accelerator types for each TPU version, seeTPU versions.
In theZonebox, select the zone where you want to create the TPU.
In theTPU typebox, select an accelerator type. The accelerator
type specifies the version and size of the Cloud TPU you want to
create. For more information about supported accelerator types for each
TPU version, seeTPU
versions.
In theTPU software versionbox, select a software version. When
creating a Cloud TPU VM, the TPU software version specifies the
version of the TPU runtime to install. For more information, seeTPU
software versions.
Click theEnable queueingtoggle.
In theQueued resource namefield, enter a name for your queued
resource request.
ClickCreateto create your queued resource request.
importcom.google.cloud.tpu.v2alpha1.CreateQueuedResourceRequest;importcom.google.cloud.tpu.v2alpha1.Node;importcom.google.cloud.tpu.v2alpha1.QueuedResource;importcom.google.cloud.tpu.v2alpha1.TpuClient;importjava.io.IOException;importjava.util.concurrent.ExecutionException;importjava.util.concurrent.TimeUnit;importjava.util.concurrent.TimeoutException;publicclassCreateQueuedResource{publicstaticvoidmain(String[]args)throwsIOException,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.StringprojectId="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-zonesStringzone="us-central1-a";// The name for your TPU.StringnodeName="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.StringtpuType="v5litepod-4";// Software version that specifies the version of the TPU runtime to install.// For more information see https://cloud.google.com/tpu/docs/runtimesStringtpuSoftwareVersion="v2-tpuv5-litepod";// The name for your Queued Resource.StringqueuedResourceId="QUEUED_RESOURCE_ID";createQueuedResource(projectId,zone,queuedResourceId,nodeName,tpuType,tpuSoftwareVersion);}// Creates a Queued ResourcepublicstaticQueuedResourcecreateQueuedResource(StringprojectId,Stringzone,StringqueuedResourceId,StringnodeName,StringtpuType,StringtpuSoftwareVersion)throwsIOException,ExecutionException,InterruptedException,TimeoutException{Stringresource=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(TpuClienttpuClient=TpuClient.create()){Stringparent=String.format("projects/%s/locations/%s",projectId,zone);Nodenode=Node.newBuilder().setName(nodeName).setAcceleratorType(tpuType).setRuntimeVersion(tpuSoftwareVersion).setQueuedResource(resource).build();QueuedResourcequeuedResource=QueuedResource.newBuilder().setName(queuedResourceId).setTpu(QueuedResource.Tpu.newBuilder().addNodeSpec(QueuedResource.Tpu.NodeSpec.newBuilder().setParent(parent).setNode(node).setNodeId(nodeName).build()).build()).build();CreateQueuedResourceRequestrequest=CreateQueuedResourceRequest.newBuilder().setParent(parent).setQueuedResourceId(queuedResourceId).setQueuedResource(queuedResource).build();returntpuClient.createQueuedResourceAsync(request).get(1,TimeUnit.MINUTES);}}}
fromgoogle.cloudimporttpu_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_versionnode_spec=tpu_v2alpha1.QueuedResource.Tpu.NodeSpec()node_spec.parent=f"projects/{project_id}/locations/{zone}"node_spec.node_id=tpu_namenode_spec.node=noderesource=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
Request a queued resource using a reservation
You can request a queued resource using a reservation. To purchase a
reservation, contact your Google Cloud account team.
The accelerator type specifies the version and size of the Cloud TPU you want to create.
For more information about supported accelerator types for each TPU version, seeTPU versions.
The accelerator type specifies the version and size of the Cloud TPU you want to create.
For more information about supported accelerator types for each TPU version, seeTPU versions.
In theZonebox, select the zone where you want to create the TPU.
In theTPU typebox, select an accelerator type. The accelerator
type specifies the version and size of the Cloud TPU you want to
create. For more information about supported accelerator types for each
TPU version, seeTPU
versions.
In theTPU software versionbox, select a software version. When
creating a Cloud TPU VM, the TPU software version specifies the
version of the TPU runtime to install. For more information, seeTPU
software versions.
Click theEnable queueingtoggle.
In theQueued resource namefield, enter a name for your queued
resource request.
Expand theManagementsection.
Select theUse existing reservationcheckbox.
ClickCreateto create your queued resource request.
Request a TPU Spot VM queued resource
ASpot VMis a resource that can be preempted
and assigned to another workload at any time. Spot VM resources
cost less, and you might get access to resources sooner compared to a
non-Spot VM request. For more information about TPU
Spot VMs, seeManage TPU Spot VMs.
The accelerator type specifies the version and size of the Cloud TPU you want to create.
For more information about supported accelerator types for each TPU version, seeTPU versions.
The accelerator type specifies the version and size of the Cloud TPU you want to create.
For more information about supported accelerator types for each TPU version, seeTPU versions.
In theZonebox, select the zone where you want to create the TPU.
In theTPU typebox, select an accelerator type. The accelerator
type specifies the version and size of the Cloud TPU you want to
create. For more information about supported accelerator types for each
TPU version, seeTPU versions.
In theTPU software versionbox, select a software version. When
creating a Cloud TPU VM, the TPU software version specifies the
version of the TPU runtime to install. For more information, seeTPU software versions.
Click theEnable queueingtoggle.
In theQueued resource namefield, enter a name for your queued
resource request.
importcom.google.cloud.tpu.v2alpha1.CreateQueuedResourceRequest;importcom.google.cloud.tpu.v2alpha1.Node;importcom.google.cloud.tpu.v2alpha1.QueuedResource;importcom.google.cloud.tpu.v2alpha1.SchedulingConfig;importcom.google.cloud.tpu.v2alpha1.TpuClient;importjava.io.IOException;importjava.util.concurrent.ExecutionException;publicclassCreateSpotQueuedResource{publicstaticvoidmain(String[]args)throwsIOException,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.StringprojectId="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-zonesStringzone="us-central1-a";// The name for your TPU.StringnodeName="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.StringtpuType="v5litepod-4";// Software version that specifies the version of the TPU runtime to install.// For more information see https://cloud.google.com/tpu/docs/runtimesStringtpuSoftwareVersion="v2-tpuv5-litepod";// The name for your Queued Resource.StringqueuedResourceId="QUEUED_RESOURCE_ID";createQueuedResource(projectId,zone,queuedResourceId,nodeName,tpuType,tpuSoftwareVersion);}// Creates a Queued Resource with --preemptible flag.publicstaticQueuedResourcecreateQueuedResource(StringprojectId,Stringzone,StringqueuedResourceId,StringnodeName,StringtpuType,StringtpuSoftwareVersion)throwsIOException,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(TpuClienttpuClient=TpuClient.create()){Stringparent=String.format("projects/%s/locations/%s",projectId,zone);StringresourceName=String.format("projects/%s/locations/%s/queuedResources/%s",projectId,zone,queuedResourceId);SchedulingConfigschedulingConfig=SchedulingConfig.newBuilder().setPreemptible(true).build();Nodenode=Node.newBuilder().setName(nodeName).setAcceleratorType(tpuType).setRuntimeVersion(tpuSoftwareVersion).setSchedulingConfig(schedulingConfig).setQueuedResource(resourceName).build();QueuedResourcequeuedResource=QueuedResource.newBuilder().setName(queuedResourceId).setTpu(QueuedResource.Tpu.newBuilder().addNodeSpec(QueuedResource.Tpu.NodeSpec.newBuilder().setParent(parent).setNode(node).setNodeId(nodeName).build()).build()).build();CreateQueuedResourceRequestrequest=CreateQueuedResourceRequest.newBuilder().setParent(parent).setQueuedResourceId(queuedResourceId).setQueuedResource(queuedResource).build();returntpuClient.createQueuedResourceAsync(request).get();}}}
fromgoogle.cloudimporttpu_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_versionnode_spec=tpu_v2alpha1.QueuedResource.Tpu.NodeSpec()node_spec.parent=f"projects/{project_id}/locations/{zone}"node_spec.node_id=tpu_namenode_spec.node=noderesource=tpu_v2alpha1.QueuedResource()resource.tpu=tpu_v2alpha1.QueuedResource.Tpu(node_spec=[node_spec])# Create a spot resourceresource.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
Request a queued resource to be allocated before or after a specified time
You can specify an optionalstart timeorend timein a queued resource request. The start
time or start duration specifies the earliest time in which to fill the request.
The end time or end duration specifies how long the request remains valid.
If a request hasn't been filled by the specified end time or within the
specified duration, the request expires. After the request has expired, it
remains in the queue but is no longer eligible for allocation.
You can also specify anallocation intervalby
specifying a start time or duration and an end time or duration.
For a list of supported timestamp and duration formats, seeDatetime.
Request a queued resource to be fulfilled after a specified time
In a queued resource request, you can specify a time or duration after which a
resource should be allocated.
gcloud
The following command requests a v5p-4096 TPU with to be allocated after 9AM on
December 14, 2022.
The accelerator type specifies the version and size of the Cloud TPU you want to create.
For more information about supported accelerator types for each TPU version, seeTPU versions.
The accelerator type specifies the version and size of the Cloud TPU you want to create.
For more information about supported accelerator types for each TPU version, seeTPU versions.
In theZonebox, select the zone where you want to create the TPU.
In theTPU typebox, select an accelerator type. The accelerator
type specifies the version and size of the Cloud TPU you want to
create. For more information about supported accelerator types for each
TPU version, seeTPU
versions.
In theTPU software versionbox, select a software version. When
creating a Cloud TPU VM, the TPU software version specifies the
version of the TPU runtime to install. For more information, seeTPU
software versions.
Click theEnable queueingtoggle.
In theQueued resource namefield, enter a name for your queued
resource request.
In theStart request onfield, enter the time after which the
resource should be allocated.
ClickCreateto create your queued resource request.
The following example requests a v5p-32 to be allocated after six hours.
The accelerator type specifies the version and size of the Cloud TPU you want to create.
For more information about supported accelerator types for each TPU version, seeTPU versions.
The accelerator type specifies the version and size of the Cloud TPU you want to create.
For more information about supported accelerator types for each TPU version, seeTPU versions.
importcom.google.cloud.tpu.v2alpha1.CreateQueuedResourceRequest;importcom.google.cloud.tpu.v2alpha1.Node;importcom.google.cloud.tpu.v2alpha1.QueuedResource;importcom.google.cloud.tpu.v2alpha1.TpuClient;importcom.google.protobuf.Duration;importjava.io.IOException;importjava.util.concurrent.ExecutionException;publicclassCreateTimeBoundQueuedResource{publicstaticvoidmain(String[]args)throwsIOException,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.StringprojectId="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-zonesStringzone="us-central2-b";// The name of your node.StringnodeId="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.StringacceleratorType="v2-8";// Software version that specifies the version of the TPU runtime to install.// For more information see https://cloud.google.com/tpu/docs/runtimesStringruntimeVersion="v2-tpuv5-litepod";// The name of your Queued Resource.StringqueuedResourceId="YOUR_QUEUED_RESOURCE_ID";createTimeBoundQueuedResource(projectId,nodeId,queuedResourceId,zone,acceleratorType,runtimeVersion);}// Creates a Queued Resource with time bound configuration.publicstaticQueuedResourcecreateTimeBoundQueuedResource(StringprojectId,StringnodeId,StringqueuedResourceId,Stringzone,StringacceleratorType,StringruntimeVersion)throwsIOException,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(TpuClienttpuClient=TpuClient.create()){Stringparent=String.format("projects/%s/locations/%s",projectId,zone);// Create a Duration object representing 6 hours.DurationvalidAfterDuration=Duration.newBuilder().setSeconds(6*3600).build();// You could also use timestamps like this:// Timestamp validAfterTime = Timestamps.parse("2024-10-14T09:00:00Z");Nodenode=Node.newBuilder().setName(nodeId).setAcceleratorType(acceleratorType).setRuntimeVersion(runtimeVersion).setQueuedResource(String.format("projects/%s/locations/%s/queuedResources/%s",projectId,zone,queuedResourceId)).build();QueuedResourcequeuedResource=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();CreateQueuedResourceRequestrequest=CreateQueuedResourceRequest.newBuilder().setParent(parent).setQueuedResource(queuedResource).setQueuedResourceId(queuedResourceId).build();returntpuClient.createQueuedResourceAsync(request).get();}}}
Request a queued resource that expires after a specified time
In a queued resource request, you can specify how long a queued resource request
remains valid. If the request hasn't been fulfilled by the time or duration you
specify, the request expires.
gcloud
The following command requests a v5p-4096 TPU. If the request isn't fulfilled by
December 14, 2022 at 9:00 AM, the request expires.
The accelerator type specifies the version and size of the Cloud TPU you want to create.
For more information about supported accelerator types for each TPU version, seeTPU versions.
The accelerator type specifies the version and size of the Cloud TPU you want to create.
For more information about supported accelerator types for each TPU version, seeTPU versions.
In theZonebox, select the zone where you want to create the TPU.
In theTPU typebox, select an accelerator type. The accelerator
type specifies the version and size of the Cloud TPU you want to
create. For more information about supported accelerator types for each
TPU version, seeTPU versions.
In theTPU software versionbox, select a software version. When
creating a Cloud TPU VM, the TPU software version specifies the
version of the TPU runtime to install. For more information, seeTPU software versions.
Click theEnable queueingtoggle.
In theQueued resource namefield, enter a name for your queued
resource request.
In theCancel request onfield, enter the time when the queued
resource request should expire if not filled.
ClickCreateto create your queued resource request.
The following example requests a v5p-32. The request expires if it's not filled
in six hours.
The accelerator type specifies the version and size of the Cloud TPU you want to create.
For more information about supported accelerator types for each TPU version, seeTPU versions.
The accelerator type specifies the version and size of the Cloud TPU you want to create.
For more information about supported accelerator types for each TPU version, seeTPU versions.
fromgoogle.cloudimporttpu_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_versionnode_spec=tpu_v2alpha1.QueuedResource.Tpu.NodeSpec()node_spec.parent=f"projects/{project_id}/locations/{zone}"node_spec.node_id=tpu_namenode_spec.node=noderesource=tpu_v2alpha1.QueuedResource()resource.tpu=tpu_v2alpha1.QueuedResource.Tpu(node_spec=[node_spec])# Use one of the following queueing policiesresource.queueing_policy=tpu_v2alpha1.QueuedResource.QueueingPolicy(# valid_after_duration = "6000s", # Duration after which a resource should be allocatedvalid_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
Request a queued resource to be allocated within a specified interval
You can specify an allocation interval by specifying both the start time or
duration and end time or duration.
gcloud
The following command requests a v5p-32 in 5 hours and 30 minutes from the
current time, to be created no later than December 14, 2022 at 9:00 AM.
The accelerator type specifies the version and size of the Cloud TPU you want to create.
For more information about supported accelerator types for each TPU version, seeTPU versions.
The accelerator type specifies the version and size of the Cloud TPU you want to create.
For more information about supported accelerator types for each TPU version, seeTPU versions.
In theZonebox, select the zone where you want to create the TPU.
In theTPU typebox, select an accelerator type. The accelerator
type specifies the version and size of the Cloud TPU you want to
create. For more information about supported accelerator types for each
TPU version, seeTPU versions.
In theTPU software versionbox, select a software version. When
creating a Cloud TPU VM, the TPU software version specifies the
version of the TPU runtime to install. For more information, seeTPU software versions.
Click theEnable queueingtoggle.
In theQueued resource namefield, enter a name for your queued
resource request.
In theStart request onfield, enter the time after which the
resource should be allocated.
In theCancel request onfield, enter the time when the queued
resource request should expire if not filled.
ClickCreateto create your queued resource request.
Request a queued resource with a startup script
You can specify a script to be run on a queued resource after it has been
provisioned.
gcloud
When using thegcloudcommand, you can use either the--metadataor--metadata-from-fileflag to specify a script command or a file
containing the script code, respectively. The following example creates a
queued resource request that will run thestartup-script.shscript.
The accelerator type specifies the version and size of the Cloud TPU you want to create.
For more information about supported accelerator types for each TPU version, seeTPU versions.
The time during which the request is valid after which the request is
canceled. For more information on duration formats, seeGoogle Cloud CLI topic datetime.
metadata-from-file
Specifies a file that contains metadata. If you don't specify a fully
qualified path to the metadata file, the command assumes it is located in the
current directory. In this example the file contains a startup script that
is run when the queued resource is provisioned.
metadata
Specifies metadata for the request. In this example the metadata is
a startup script command run when the queued resource is provisioned.
curl
When usingcurl, you must include the script code in the JSON content.
The following example includes an inline script in the JSON body.
The accelerator type specifies the version and size of the Cloud TPU you want to create.
For more information about supported accelerator types for each TPU version, seeTPU versions.
The time during which the request is valid after which the request is
canceled. For more information on duration formats, seeGoogle Cloud CLI topic datetime.
metadata-from-file
Specifies a file that contains metadata. If you don't specify a fully
qualified path to the metadata file, the command assumes it is located in the
current directory. In this example the file contains a startup script that
is run when the queued resource is provisioned.
metadata
Specifies metadata for the request. In this example the metadata is
a startup script command run when the queued resource is provisioned.
importcom.google.cloud.tpu.v2alpha1.CreateQueuedResourceRequest;importcom.google.cloud.tpu.v2alpha1.Node;importcom.google.cloud.tpu.v2alpha1.QueuedResource;importcom.google.cloud.tpu.v2alpha1.TpuClient;importjava.io.IOException;importjava.util.HashMap;importjava.util.Map;importjava.util.concurrent.ExecutionException;publicclassCreateQueuedResourceWithStartupScript{publicstaticvoidmain(String[]args)throwsIOException,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.StringprojectId="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-zonesStringzone="us-central1-a";// The name for your TPU.StringnodeName="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.StringtpuType="v5litepod-4";// Software version that specifies the version of the TPU runtime to install.// For more information see https://cloud.google.com/tpu/docs/runtimesStringtpuSoftwareVersion="v2-tpuv5-litepod";// The name for your Queued Resource.StringqueuedResourceId="QUEUED_RESOURCE_ID";createQueuedResource(projectId,zone,queuedResourceId,nodeName,tpuType,tpuSoftwareVersion);}// Creates a Queued Resource with startup script.publicstaticQueuedResourcecreateQueuedResource(StringprojectId,Stringzone,StringqueuedResourceId,StringnodeName,StringtpuType,StringtpuSoftwareVersion)throwsIOException,ExecutionException,InterruptedException{Stringparent=String.format("projects/%s/locations/%s",projectId,zone);StringstartupScriptContent="#!/bin/bash\necho \"Hello from the startup script!\"";// Add startup script to metadataMap<String,String>metadata=newHashMap<>();metadata.put("startup-script",startupScriptContent);StringqueuedResourceForTpu=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(TpuClienttpuClient=TpuClient.create()){Nodenode=Node.newBuilder().setName(nodeName).setAcceleratorType(tpuType).setRuntimeVersion(tpuSoftwareVersion).setQueuedResource(queuedResourceForTpu).putAllMetadata(metadata).build();QueuedResourcequeuedResource=QueuedResource.newBuilder().setName(queuedResourceId).setTpu(QueuedResource.Tpu.newBuilder().addNodeSpec(QueuedResource.Tpu.NodeSpec.newBuilder().setParent(parent).setNode(node).setNodeId(nodeName).build()).build()).build();CreateQueuedResourceRequestrequest=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.returntpuClient.createQueuedResourceAsync(request).get();}}}
fromgoogle.cloudimporttpu_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/bashecho "Hello World" > /var/log/hello.logsudo 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 numpynode.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_namenode_spec.node=noderesource=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 '}
Request a queued resource with a specified network and subnetwork
In a queued resource request, you can specify a network and subnetwork that
you want to connect your TPU to.
The accelerator type specifies the version and size of the Cloud TPU you want to create.
For more information about supported accelerator types for each TPU version, seeTPU versions.
The accelerator type specifies the version and size of the Cloud TPU you want to create.
For more information about supported accelerator types for each TPU version, seeTPU versions.
In theZonebox, select the zone where you want to create the TPU.
In theTPU typebox, select an accelerator type. The accelerator
type specifies the version and size of the Cloud TPU you want to
create. For more information about supported accelerator types for each
TPU version, seeTPU versions.
In theTPU software versionbox, select a software version. When
creating a Cloud TPU VM, the TPU software version specifies the
version of the TPU runtime to install. For more information, seeTPU software versions.
Click theEnable queueingtoggle.
In theQueued resource namefield, enter a name for your queued
resource request.
Expand theNetworksection.
In theNetworkandSubnetworkfields, select the network and
subnetwork you want to use.
ClickCreateto create your queued resource request.
importcom.google.api.gax.retrying.RetrySettings;importcom.google.cloud.tpu.v2alpha1.CreateQueuedResourceRequest;importcom.google.cloud.tpu.v2alpha1.NetworkConfig;importcom.google.cloud.tpu.v2alpha1.Node;importcom.google.cloud.tpu.v2alpha1.QueuedResource;importcom.google.cloud.tpu.v2alpha1.TpuClient;importcom.google.cloud.tpu.v2alpha1.TpuSettings;importjava.io.IOException;importjava.util.concurrent.ExecutionException;importorg.threeten.bp.Duration;publicclassCreateQueuedResourceWithNetwork{publicstaticvoidmain(String[]args)throwsIOException,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.StringprojectId="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-zonesStringzone="europe-west4-a";// The name for your TPU.StringnodeName="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.StringtpuType="v5litepod-4";// Software version that specifies the version of the TPU runtime to install.// For more information see https://cloud.google.com/tpu/docs/runtimesStringtpuSoftwareVersion="v2-tpuv5-litepod";// The name for your Queued Resource.StringqueuedResourceId="QUEUED_RESOURCE_ID";// The name of the network you want the node to connect to.// The network should be assigned to your project.StringnetworkName="YOUR_COMPUTE_TPU_NETWORK";createQueuedResourceWithNetwork(projectId,zone,queuedResourceId,nodeName,tpuType,tpuSoftwareVersion,networkName);}// Creates a Queued Resource with network configuration.publicstaticQueuedResourcecreateQueuedResourceWithNetwork(StringprojectId,Stringzone,StringqueuedResourceId,StringnodeName,StringtpuType,StringtpuSoftwareVersion,StringnetworkName)throwsIOException,ExecutionException,InterruptedException{// With these settings the client library handles the Operation's polling mechanism// and prevent CancellationException errorTpuSettings.BuilderclientSettings=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(TpuClienttpuClient=TpuClient.create(clientSettings.build())){Stringparent=String.format("projects/%s/locations/%s",projectId,zone);Stringregion=zone.substring(0,zone.length()-2);// Specify the network and subnetwork that you want to connect your TPU to.NetworkConfignetworkConfig=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 nodeNodenode=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 resourceQueuedResourcequeuedResource=QueuedResource.newBuilder().setName(queuedResourceId).setTpu(QueuedResource.Tpu.newBuilder().addNodeSpec(QueuedResource.Tpu.NodeSpec.newBuilder().setParent(parent).setNode(node).setNodeId(nodeName).build()).build()).build();CreateQueuedResourceRequestrequest=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.returntpuClient.createQueuedResourceAsync(request).get();}}}
fromgoogle.cloudimporttpu_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_typenode.runtime_version=runtime_version# Setting network configurationnode.network_config=tpu_v2alpha1.NetworkConfig(network=network,# Update if you want to use a specific networksubnetwork="default",# Update if you want to use a specific subnetworkenable_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_namenode_spec.node=noderesource=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
Delete a queued resource request
You can delete a queued resource request and the TPU associated with the request
by deleting the queued resource request:
gcloud
Pass the--forceflag to thequeued-resource deletecommand:
importcom.google.api.gax.retrying.RetrySettings;importcom.google.cloud.tpu.v2alpha1.DeleteQueuedResourceRequest;importcom.google.cloud.tpu.v2alpha1.TpuClient;importcom.google.cloud.tpu.v2alpha1.TpuSettings;importjava.io.IOException;importjava.util.concurrent.ExecutionException;importorg.threeten.bp.Duration;publicclassDeleteForceQueuedResource{publicstaticvoidmain(String[]args)throwsIOException,ExecutionException,InterruptedException{// TODO(developer): Replace these variables before running the sample.// Project ID or project number of the Google Cloud project.StringprojectId="YOUR_PROJECT_ID";// The zone in which the TPU was created.Stringzone="us-central1-f";// The name for your Queued Resource.StringqueuedResourceId="QUEUED_RESOURCE_ID";deleteForceQueuedResource(projectId,zone,queuedResourceId);}// Deletes a Queued Resource asynchronously with --force flag.publicstaticvoiddeleteForceQueuedResource(StringprojectId,Stringzone,StringqueuedResourceId)throwsExecutionException,InterruptedException,IOException{Stringname=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 errorTpuSettings.BuilderclientSettings=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(TpuClienttpuClient=TpuClient.create(clientSettings.build())){DeleteQueuedResourceRequestrequest=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);}}}
fromgoogle.cloudimporttpu_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.")exceptTypeErrorase:print(f"Error deleting resource:{e}")print(f"Queued resource '{queued_resource_name}' successfully deleted.")
If you delete the TPU directly, you also need to delete the queued resource, as
shown in the following example. When you delete the TPU, the queued resource
request transitions to theSUSPENDEDstate, after which the queued resource
request can be deleted.
When you delete your TPU, the associated queued resource goes into theSUSPENDINGstate, then theSUSPENDEDstate. When your queued
resource is in theSUSPENDEDstate, you can delete it:
When you delete your TPU, the associated queued resource goes into theSUSPENDINGstate, then theSUSPENDEDstate. When your queued
resource is in theSUSPENDEDstate, you can delete it:
When you delete your TPU, the associated queued resource goes into theSuspendingstate, then theSuspendedstate. When your queued resource is
in theSuspendedstate, you can delete it:
Click theQueued resourcestab.
Select the checkbox next to your queued resource request.
importcom.google.api.gax.longrunning.OperationTimedPollAlgorithm;importcom.google.api.gax.retrying.RetrySettings;importcom.google.cloud.tpu.v2.DeleteNodeRequest;importcom.google.cloud.tpu.v2.NodeName;importcom.google.cloud.tpu.v2.TpuClient;importcom.google.cloud.tpu.v2.TpuSettings;importjava.io.IOException;importjava.util.concurrent.ExecutionException;importorg.threeten.bp.Duration;publicclassDeleteTpuVm{publicstaticvoidmain(String[]args)throwsIOException,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.StringprojectId="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-zonesStringzone="europe-west4-a";// The name for your TPU.StringnodeName="YOUR_TPU_NAME";deleteTpuVm(projectId,zone,nodeName);}// Deletes a TPU VM with the specified name in the given project and zone.publicstaticvoiddeleteTpuVm(StringprojectId,Stringzone,StringnodeName)throwsIOException,ExecutionException,InterruptedException{// With these settings the client library handles the Operation's polling mechanism// and prevent CancellationException errorTpuSettings.BuilderclientSettings=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(TpuClienttpuClient=TpuClient.create(clientSettings.build())){Stringname=NodeName.of(projectId,zone,nodeName).toString();DeleteNodeRequestrequest=DeleteNodeRequest.newBuilder().setName(name).build();tpuClient.deleteNodeAsync(request).get();System.out.println("TPU VM deleted");}}}
When you delete your TPU, the associated queued resource goes into theSUSPENDINGstate, then theSUSPENDEDstate. When your queued resource is
in theSUSPENDEDstate, you can delete it:
importcom.google.cloud.tpu.v2alpha1.DeleteQueuedResourceRequest;importcom.google.cloud.tpu.v2alpha1.TpuClient;importjava.io.IOException;importjava.util.concurrent.ExecutionException;publicclassDeleteQueuedResource{publicstaticvoidmain(String[]args)throwsIOException,ExecutionException,InterruptedException{// TODO(developer): Replace these variables before running the sample.// Project ID or project number of the Google Cloud project.StringprojectId="YOUR_PROJECT_ID";// The zone in which the TPU was created.Stringzone="us-central1-f";// The name for your Queued Resource.StringqueuedResourceId="QUEUED_RESOURCE_ID";deleteQueuedResource(projectId,zone,queuedResourceId);}// Deletes a Queued Resource asynchronously.publicstaticvoiddeleteQueuedResource(StringprojectId,Stringzone,StringqueuedResourceId)throwsExecutionException,InterruptedException,IOException{Stringname=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(TpuClienttpuClient=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-vmDeleteQueuedResourceRequestrequest=DeleteQueuedResourceRequest.newBuilder().setName(name).build();tpuClient.deleteQueuedResourceAsync(request).get();}}}
fromgoogle.cloudimporttpu_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.")exceptExceptionase:print(e)
When you delete your TPU, the associated queued resource goes into theSUSPENDINGstate, then theSUSPENDEDstate. When your queued resource is
in theSUSPENDEDstate, you can delete it:
After your TPU has been provisioned, you can also view details about your
queued resource request by going to theTPUspage, finding your TPU, and
clicking on the name of the corresponding queued resource request.
importcom.google.cloud.tpu.v2alpha1.GetQueuedResourceRequest;importcom.google.cloud.tpu.v2alpha1.QueuedResource;importcom.google.cloud.tpu.v2alpha1.TpuClient;importjava.io.IOException;publicclassGetQueuedResource{publicstaticvoidmain(String[]args)throwsIOException{// TODO(developer): Replace these variables before running the sample.// Project ID or project number of the Google Cloud project.StringprojectId="YOUR_PROJECT_ID";// The zone in which the TPU was created.Stringzone="us-central1-f";// The name for your Queued Resource.StringqueuedResourceId="QUEUED_RESOURCE_ID";getQueuedResource(projectId,zone,queuedResourceId);}// Get a Queued Resource.publicstaticQueuedResourcegetQueuedResource(StringprojectId,Stringzone,StringqueuedResourceId)throwsIOException{Stringname=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(TpuClienttpuClient=TpuClient.create()){GetQueuedResourceRequestrequest=GetQueuedResourceRequest.newBuilder().setName(name).build();returntpuClient.getQueuedResource(request);}}}
fromgoogle.cloudimporttpu_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
If the request fails, the output will contain error information. For a request
that is waiting for resources, the output looks similar to the following:
importcom.google.cloud.tpu.v2alpha1.ListQueuedResourcesRequest;importcom.google.cloud.tpu.v2alpha1.QueuedResource;importcom.google.cloud.tpu.v2alpha1.TpuClient;importcom.google.cloud.tpu.v2alpha1.TpuClient.ListQueuedResourcesPage;importjava.io.IOException;publicclassListQueuedResources{publicstaticvoidmain(String[]args)throwsIOException{// TODO(developer): Replace these variables before running the sample.// Project ID or project number of the Google Cloud project.StringprojectId="YOUR_PROJECT_ID";// The zone in which the TPU was created.Stringzone="us-central1-a";listQueuedResources(projectId,zone);}// List Queued Resources.publicstaticListQueuedResourcesPagelistQueuedResources(StringprojectId,Stringzone)throwsIOException{Stringparent=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(TpuClienttpuClient=TpuClient.create()){ListQueuedResourcesRequestrequest=ListQueuedResourcesRequest.newBuilder().setParent(parent).build();ListQueuedResourcesPageresponse=tpuClient.listQueuedResources(request).getPage();for(QueuedResourcequeuedResource:response.iterateAll()){System.out.println(queuedResource.getName());}returnresponse;}}}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,[]]