Stay organized with collectionsSave and categorize content based on your preferences.
Manage TPU resources
This page describes how to create, list, stop, start, delete, and connect to
Cloud TPUs using the Create Node API. The Create Node API is called when you
run thegcloud compute tpus tpu-vm createcommand using the Google Cloud CLI
and when you create a TPU using the Google Cloud console. When you use the Create
Node API, your request is processed immediately. If there is not enough capacity
to fulfill your request, then the request will fail.
The best practice is to create TPUs usingqueued resourcesinstead of the Create Node API.
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. For
more information, seeManage queued resources.
When using Multislice, you must used queued resources. For more
information, seeMultislice introduction.
If you want to use Google Kubernetes Engine (GKE) to manage TPU resources,
you first have to create a GKE cluster. You then add node pools
containing TPU slices to your cluster. For more information, seeAbout TPUs in GKE.
Prerequisites
Before you run these procedures, 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:
To avoid setting the--projectflag in every gcloud CLI command,
use thegcloud config
setcommand to set
the project ID in your active configuration:
gcloudconfigsetprojectproject-id
Replaceproject-idwith the name of your Google Cloud project.
Create a Cloud TPU using the Create Node API
You can create a Cloud TPU usinggcloud, the Google Cloud console,
or the Cloud TPU API.
When creating a Cloud TPU, you must specify the TPU software version (also
called runtime version). To determine which software version you should use, seeTPU software versions.
You also need to specify the TPU configuration in terms of TensorCores or TPU
chips. For more information, see the section for the TPU version you are using
inSystem architecture.
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.
Specifies that the TPU instances are created with secure boot enabled.
This implicitly makes them Shielded VM instances.
SeeWhat is Shielded VM?for more details.
In theZonebox, select the zone in which 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.
importcom.google.api.gax.longrunning.OperationTimedPollAlgorithm;importcom.google.api.gax.retrying.RetrySettings;importcom.google.cloud.tpu.v2.CreateNodeRequest;importcom.google.cloud.tpu.v2.Node;importcom.google.cloud.tpu.v2.TpuClient;importcom.google.cloud.tpu.v2.TpuSettings;importjava.io.IOException;importjava.util.concurrent.ExecutionException;importorg.threeten.bp.Duration;publicclassCreateTpuVm{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="v2-8";// 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";createTpuVm(projectId,zone,nodeName,tpuType,tpuSoftwareVersion);}// Creates a TPU VM with the specified name, zone, accelerator type, and version.publicstaticNodecreateTpuVm(StringprojectId,Stringzone,StringnodeName,StringtpuType,StringtpuSoftwareVersion)throwsIOException,ExecutionException,InterruptedException{// With these settings the client library handles the Operation's polling mechanism// and prevent CancellationException errorTpuSettings.BuilderclientSettings=TpuSettings.newBuilder();clientSettings.createNodeOperationSettings().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())){Stringparent=String.format("projects/%s/locations/%s",projectId,zone);NodetpuVm=Node.newBuilder().setName(nodeName).setAcceleratorType(tpuType).setRuntimeVersion(tpuSoftwareVersion).build();CreateNodeRequestrequest=CreateNodeRequest.newBuilder().setParent(parent).setNodeId(nodeName).setNode(tpuVm).build();returntpuClient.createNodeAsync(request).get();}}}
// Import the TPUClient// TODO(developer): Uncomment below line before running the sample.// const {TpuClient} = require('@google-cloud/tpu').v2;const{Node,NetworkConfig}=require('@google-cloud/tpu').protos.google.cloud.tpu.v2;// Instantiate a tpuClient// TODO(developer): Uncomment below line before running the sample.// tpuClient = new TpuClient();// TODO(developer): Update below line before running the sample.// Project ID or project number of the Google Cloud project you want to create a node.constprojectId=awaittpuClient.getProjectId();// The name of the network you want the TPU node to connect to. The network should be assigned to your project.constnetworkName='compute-tpu-network';// The region of the network, that you want the TPU node to connect to.constregion='europe-west4';// The name for your TPU.constnodeName='node-name-1';// 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-zonesconstzone='europe-west4-a';// 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.consttpuType='v5litepod-4';// Software version that specifies the version of the TPU runtime to install. For more information,// see https://cloud.google.com/tpu/docs/runtimesconsttpuSoftwareVersion='v2-tpuv5-litepod';asyncfunctioncallCreateTpuVM(){// Create a nodeconstnode=newNode({name:nodeName,zone,acceleratorType:tpuType,runtimeVersion:tpuSoftwareVersion,// Define networknetworkConfig:newNetworkConfig({enableExternalIps:true,network:`projects/${projectId}/global/networks/${networkName}`,subnetwork:`projects/${projectId}/regions/${region}/subnetworks/${networkName}`,}),});constparent=`projects/${projectId}/locations/${zone}`;constrequest={parent,node,nodeId:nodeName};const[operation]=awaittpuClient.createNode(request);// Wait for the create operation to complete.const[response]=awaitoperation.promise();console.log(`TPU VM:${nodeName}created.`);returnresponse;}returnawaitcallCreateTpuVM();
fromgoogle.cloudimporttpu_v2# 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"# Create a TPU nodenode=tpu_v2.Node()node.accelerator_type=tpu_type# To see available runtime version use command:# gcloud compute tpus versions list --zone={ZONE}node.runtime_version=runtime_versionrequest=tpu_v2.CreateNodeRequest(parent=f"projects/{project_id}/locations/{zone}",node_id=tpu_name,node=node,)# Create a TPU clientclient=tpu_v2.TpuClient()operation=client.create_node(request=request)print("Waiting for operation to complete...")response=operation.result()print(response)# Example response:# name: "projects/[project_id]/locations/[zone]/nodes/my-tpu"# accelerator_type: "v5litepod-4"# state: READY# ...
Run a startup script
gcloud
You can run a startup script on each TPU VM by specifying the--metadata startup-scriptflag when creating the TPU VM. The following
command creates a TPU VM using a startup script.
importcom.google.cloud.tpu.v2.CreateNodeRequest;importcom.google.cloud.tpu.v2.Node;importcom.google.cloud.tpu.v2.TpuClient;importjava.io.IOException;importjava.util.HashMap;importjava.util.Map;importjava.util.concurrent.ExecutionException;publicclassCreateTpuVmWithStartupScript{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.StringacceleratorType="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";createTpuVmWithStartupScript(projectId,zone,nodeName,acceleratorType,tpuSoftwareVersion);}// Create a TPU VM with a startup script.publicstaticNodecreateTpuVmWithStartupScript(StringprojectId,Stringzone,StringnodeName,StringacceleratorType,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);StringstartupScriptContent="#!/bin/bash\necho \"Hello from the startup script!\"";// Add startup script to metadataMap<String,String>metadata=newHashMap<>();metadata.put("startup-script",startupScriptContent);NodetpuVm=Node.newBuilder().setName(nodeName).setAcceleratorType(acceleratorType).setRuntimeVersion(tpuSoftwareVersion).putAllMetadata(metadata).build();CreateNodeRequestrequest=CreateNodeRequest.newBuilder().setParent(parent).setNodeId(nodeName).setNode(tpuVm).build();returntpuClient.createNodeAsync(request).get();}}}
// Import the TPUClient// TODO(developer): Uncomment below line before running the sample.// const {TpuClient} = require('@google-cloud/tpu').v2;const{Node,NetworkConfig}=require('@google-cloud/tpu').protos.google.cloud.tpu.v2;// Instantiate a tpuClient// TODO(developer): Uncomment below line before running the sample.// tpuClient = new TpuClient();// TODO(developer): Update these variables before running the sample.// Project ID or project number of the Google Cloud project you want to create a node.constprojectId=awaittpuClient.getProjectId();// The name of the network you want the TPU node to connect to. The network should be assigned to your project.constnetworkName='compute-tpu-network';// The region of the network, that you want the TPU node to connect to.constregion='europe-west4';// The name for your TPU.constnodeName='node-name-1';// 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-zonesconstzone='europe-west4-a';// 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.consttpuType='v5litepod-4';// Software version that specifies the version of the TPU runtime to install. For more information,// see https://cloud.google.com/tpu/docs/runtimesconsttpuSoftwareVersion='v2-tpuv5-litepod';asyncfunctioncallCreateTpuVMStartupScript(){// Create a nodeconstnode=newNode({name:nodeName,zone,acceleratorType:tpuType,runtimeVersion:tpuSoftwareVersion,// Define networknetworkConfig:newNetworkConfig({enableExternalIps:true,network:`projects/${projectId}/global/networks/${networkName}`,subnetwork:`projects/${projectId}/regions/${region}/subnetworks/${networkName}`,}),metadata:{// The script updates numpy to the latest version and logs the output to a file.'startup-script':`#!/bin/bashecho "Hello World" > /var/log/hello.logsudo pip3 install --upgrade numpy >> /var/log/hello.log 2>&1`,},});constparent=`projects/${projectId}/locations/${zone}`;constrequest={parent,node,nodeId:nodeName};const[operation]=awaittpuClient.createNode(request);// Wait for the create operation to complete.const[response]=awaitoperation.promise();console.log(JSON.stringify(response));returnresponse;}returnawaitcallCreateTpuVMStartupScript();
fromgoogle.cloudimporttpu_v2# 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"node=tpu_v2.Node()node.accelerator_type=tpu_typenode.runtime_version=runtime_version# This startup script updates numpy to the latest version and logs the output to a file.metadata={"startup-script":"""#!/bin/bashecho "Hello World" > /var/log/hello.logsudo pip3 install --upgrade numpy >> /var/log/hello.log 2>&1"""}# Adding metadata with startup script to the TPU node.node.metadata=metadata# Enabling external IPs for internet access from the TPU node.node.network_config=tpu_v2.NetworkConfig(enable_external_ips=True)request=tpu_v2.CreateNodeRequest(parent=f"projects/{project_id}/locations/{zone}",node_id=tpu_name,node=node,)client=tpu_v2.TpuClient()operation=client.create_node(request=request)print("Waiting for operation to complete...")response=operation.result()print(response.metadata)# Example response:# {'startup-script': '#!/bin/bash\n echo "Hello World" > /var/log/hello.log\n# ...
Connect to a Cloud TPU
You can connect to a Cloud TPU using SSH.
If you fail to connect to a TPU VM using SSH, it might be because the TPU VM
doesn't have an external IP address. To access a TPU VM without an external IP
address, follow the instructions inConnect to a TPU VM without a public IP
address.
gcloud
Connect to your Cloud TPU using SSH:
$gcloudcomputetpustpu-vmsshtpu-name--zone=zone
When you request a slice larger than a single host, Cloud TPU creates a
TPU VM for each host. The number of TPU chips per host depends on theTPU version.
To install binaries or run code, connect to each TPU VM using
thetpu-vm ssh command.
$gcloudcomputetpustpu-vmsshtpu-name--zone=zone
To connect to a specific TPU VM
using SSH, use the--workerflag which follows a 0-based index:
For Multislice, you can run a command on a single VM using the
enumerated TPU name, with each slice prefix and the number appended to it. To
run a command on all TPU VMs in all slices, use the--node=all,--worker=all,
and--commandflags, with an optional--batch-sizeflag.
importcom.google.cloud.tpu.v2.ListNodesRequest;importcom.google.cloud.tpu.v2.TpuClient;importjava.io.IOException;publicclassListTpuVms{publicstaticvoidmain(String[]args)throwsIOException{// TODO(developer): Replace these variables before running the sample.// Project ID or project number of the Google Cloud project you want to use.StringprojectId="YOUR_PROJECT_ID";// The zone where the TPUs are located.// For more information about supported TPU types for specific zones,// see https://cloud.google.com/tpu/docs/regions-zonesStringzone="us-central1-f";listTpuVms(projectId,zone);}// Lists TPU VMs in the specified zone.publicstaticTpuClient.ListNodesPagelistTpuVms(StringprojectId,Stringzone)throwsIOException{// 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);ListNodesRequestrequest=ListNodesRequest.newBuilder().setParent(parent).build();returntpuClient.listNodes(request).getPage();}}}
// Import the TPUClient// TODO(developer): Uncomment below line before running the sample.// const {TpuClient} = require('@google-cloud/tpu').v2;// Instantiate a tpuClient// TODO(developer): Uncomment below line before running the sample.// tpuClient = new TpuClient();// TODO(developer): Update these variables before running the sample.// Project ID or project number of the Google Cloud project you want to retrive a list of TPU nodes.constprojectId=awaittpuClient.getProjectId();// The zone from which the TPUs are retrived.constzone='europe-west4-a';asyncfunctioncallTpuVMList(){constrequest={parent:`projects/${projectId}/locations/${zone}`,};const[response]=awaittpuClient.listNodes(request);returnresponse;}returnawaitcallTpuVMList();
importcom.google.cloud.tpu.v2.GetNodeRequest;importcom.google.cloud.tpu.v2.Node;importcom.google.cloud.tpu.v2.NodeName;importcom.google.cloud.tpu.v2.TpuClient;importjava.io.IOException;publicclassGetTpuVm{publicstaticvoidmain(String[]args)throwsIOException{// TODO(developer): Replace these variables before running the sample.// Project ID or project number of the Google Cloud project you want to use.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";getTpuVm(projectId,zone,nodeName);}// Describes a TPU VM with the specified name in the given project and zone.publicstaticNodegetTpuVm(StringprojectId,Stringzone,StringnodeName)throwsIOException{// 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()){Stringname=NodeName.of(projectId,zone,nodeName).toString();GetNodeRequestrequest=GetNodeRequest.newBuilder().setName(name).build();returntpuClient.getNode(request);}}}
// Import the TPUClient// TODO(developer): Uncomment below line before running the sample.// const {TpuClient} = require('@google-cloud/tpu').v2;// Instantiate a tpuClient// TODO(developer): Uncomment below line before running the sample.// tpuClient = new TpuClient();// TODO(developer): Update these variables before running the sample.// Project ID or project number of the Google Cloud project you want to retrive a node.constprojectId=awaittpuClient.getProjectId();// The name of TPU to retrive.constnodeName='node-name-1';// The zone, where the TPU is created.constzone='europe-west4-a';asyncfunctioncallGetTpuVM(){constrequest={name:`projects/${projectId}/locations/${zone}/nodes/${nodeName}`,};const[response]=awaittpuClient.getNode(request);console.log(`Node:${nodeName}retrived.`);returnresponse;}returnawaitcallGetTpuVM();
importcom.google.cloud.tpu.v2.Node;importcom.google.cloud.tpu.v2.NodeName;importcom.google.cloud.tpu.v2.StopNodeRequest;importcom.google.cloud.tpu.v2.TpuClient;importjava.io.IOException;importjava.util.concurrent.ExecutionException;publicclassStopTpuVm{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 use.StringprojectId="YOUR_PROJECT_ID";// The zone where the TPU is located.// For more information about supported TPU types for specific zones,// see https://cloud.google.com/tpu/docs/regions-zonesStringzone="us-central1-f";// The name for your TPU.StringnodeName="YOUR_TPU_NAME";stopTpuVm(projectId,zone,nodeName);}// Stops a TPU VM with the specified name in the given project and zone.publicstaticNodestopTpuVm(StringprojectId,Stringzone,StringnodeName)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()){Stringname=NodeName.of(projectId,zone,nodeName).toString();StopNodeRequestrequest=StopNodeRequest.newBuilder().setName(name).build();returntpuClient.stopNodeAsync(request).get();}}}
// Import the TPUClient// TODO(developer): Uncomment below line before running the sample.// const {TpuClient} = require('@google-cloud/tpu').v2;// Instantiate a tpuClient// TODO(developer): Uncomment below line before running the sample.// tpuClient = new TpuClient();// TODO(developer): Update these variables before running the sample.// Project ID or project number of the Google Cloud project you want to stop a node.constprojectId=awaittpuClient.getProjectId();// The name of TPU to stop.constnodeName='node-name-1';// The zone, where the TPU is created.constzone='europe-west4-a';asyncfunctioncallStopTpuVM(){constrequest={name:`projects/${projectId}/locations/${zone}/nodes/${nodeName}`,};const[operation]=awaittpuClient.stopNode(request);// Wait for the operation to complete.const[response]=awaitoperation.promise();console.log(`Node:${nodeName}stopped.`);returnresponse;}returnawaitcallStopTpuVM();
fromgoogle.cloudimporttpu_v2# TODO(developer): Update and un-comment below lines# project_id = "your-project-id"# zone = "us-central1-a"# tpu_name = "tpu-name"client=tpu_v2.TpuClient()request=tpu_v2.StopNodeRequest(name=f"projects/{project_id}/locations/{zone}/nodes/{tpu_name}",)try:operation=client.stop_node(request=request)print("Waiting for stop operation to complete...")response=operation.result()print(f"This TPU{tpu_name}has been stopped")print(response.state)# Example response:# State.STOPPEDexceptExceptionase:print(e)raisee
importcom.google.cloud.tpu.v2.Node;importcom.google.cloud.tpu.v2.NodeName;importcom.google.cloud.tpu.v2.StartNodeRequest;importcom.google.cloud.tpu.v2.TpuClient;importjava.io.IOException;importjava.util.concurrent.ExecutionException;publicclassStartTpuVm{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 use.StringprojectId="YOUR_PROJECT_ID";// The zone where the TPU is located.// For more information about supported TPU types for specific zones,// see https://cloud.google.com/tpu/docs/regions-zonesStringzone="us-central1-f";// The name for your TPU.StringnodeName="YOUR_TPU_NAME";startTpuVm(projectId,zone,nodeName);}// Starts a TPU VM with the specified name in the given project and zone.publicstaticNodestartTpuVm(StringprojectId,Stringzone,StringnodeName)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()){Stringname=NodeName.of(projectId,zone,nodeName).toString();StartNodeRequestrequest=StartNodeRequest.newBuilder().setName(name).build();returntpuClient.startNodeAsync(request).get();}}}
// Import the TPUClient// TODO(developer): Uncomment below line before running the sample.// const {TpuClient} = require('@google-cloud/tpu').v2;// Instantiate a tpuClient// TODO(developer): Uncomment below line before running the sample.// tpuClient = new TpuClient();// TODO(developer): Update these variables before running the sample.// Project ID or project number of the Google Cloud project you want to start a node.constprojectId=awaittpuClient.getProjectId();// The name of TPU to start.constnodeName='node-name-1';// The zone, where the TPU is created.constzone='europe-west4-a';asyncfunctioncallStartTpuVM(){constrequest={name:`projects/${projectId}/locations/${zone}/nodes/${nodeName}`,};const[operation]=awaittpuClient.startNode(request);// Wait for the operation to complete.const[response]=awaitoperation.promise();console.log(`Node:${nodeName}started.`);returnresponse;}returnawaitcallStartTpuVM();
fromgoogle.cloudimporttpu_v2# TODO(developer): Update and un-comment below lines# project_id = "your-project-id"# zone = "us-central1-a"# tpu_name = "tpu-name"client=tpu_v2.TpuClient()request=tpu_v2.StartNodeRequest(name=f"projects/{project_id}/locations/{zone}/nodes/{tpu_name}",)try:operation=client.start_node(request=request)print("Waiting for start operation to complete...")response=operation.result()print(f"TPU{tpu_name}has been started")print(response.state)# Example response:# State.READYexceptExceptionase:print(e)raisee
Delete a Cloud TPU
Delete your TPU VM slices at the end of your session.
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");}}}
// Import the TPUClient// TODO(developer): Uncomment below line before running the sample.// const {TpuClient} = require('@google-cloud/tpu').v2;// Instantiate a tpuClient// TODO(developer): Uncomment below line before running the sample.// tpuClient = new TpuClient();// TODO(developer): Update these variables before running the sample.// Project ID or project number of the Google Cloud project you want to delete a node.constprojectId=awaittpuClient.getProjectId();// The name of TPU to delete.constnodeName='node-name-1';// The zone, where the TPU is created.constzone='europe-west4-a';asyncfunctioncallDeleteTpuVM(){constrequest={name:`projects/${projectId}/locations/${zone}/nodes/${nodeName}`,};const[operation]=awaittpuClient.deleteNode(request);// Wait for the delete operation to complete.const[response]=awaitoperation.promise();console.log(`Node:${nodeName}deleted.`);returnresponse;}returnawaitcallDeleteTpuVM();
[[["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,[]]