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 the gcloud compute tpus tpu-vm create command 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 using queued resources instead 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, see Manage queued resources.
When using Multislice, you must used queued resources. For more information, see Multislice 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, see About 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, see Set up the Cloud TPU environment.
If you're using one of the Cloud 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-idReplace project-id with the name of your Google Cloud project.
Create a Cloud TPU using the Create Node API
You can create a Cloud TPU using gcloud, 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, see TPU 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 in System architecture.
gcloud
To create a TPU using the Create Node API, use
the gcloud compute tpus tpu-vm create command.
To configure specific internal or external IP addresses, see the instructions in
External and internal IP addresses.
The following command uses a v5litepod-8 (v5e) TPU:
$gcloudcomputetpustpu-vmcreatetpu-name\ --zone=us-central2-b\ --accelerator-type=v5litepod-8\ --version=tpu-software-version
Command flag descriptions
zone- The zone where you plan to create your Cloud TPU.
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, see TPU versions.
version- The TPU software version.
shielded-secure-boot(optional)- Specifies that the TPU instances are created with secure boot enabled. This implicitly makes them Shielded VM instances. See What is Shielded VM? for more details.
Console
In the Google Cloud console, go to the TPUs page:
Click Create TPU.
In the Name field, enter a name for your TPU.
In the Zone box, select the zone in which to create the TPU.
In the TPU type box, 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, see TPU versions.
In the TPU software version box, 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, see TPU software versions.
Click Create to create your resources.
curl
The following command uses curl to create a TPU.
$curl-XPOST-H"Authorization: Bearer $(gcloudauthprint-access-token)"-H"Content-Type: application/json"-d"{accelerator_type: 'v5litepod-8', \ runtime_version:'tpu-vm-tf-2.17.1-pjrt', \ network_config: {enable_external_ips: true}, \ shielded_instance_config: { enable_secure_boot: true }}"\ https://tpu.googleapis.com/v2/projects/project-id/locations/us-central2-b/nodes?node_id=node_name
Required fields
runtime_version- The Cloud TPU runtime version that you want to use.
project-id- The name of your enrolled Google Cloud project.
zone- The zone where you're creating your Cloud TPU.
node_name- The name of the TPU VM you're creating.
Java
To authenticate to Cloud TPU, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
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;
publicclass CreateTpuVm{
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-zones
Stringzone="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/runtimes
StringtpuSoftwareVersion="v2-tpuv5-litepod";
createTpuVm(projectId,zone,nodeName,tpuType,tpuSoftwareVersion);
}
// Creates a TPU VM with the specified name, zone, accelerator type, and version.
publicstaticNode createTpuVm(
StringprojectId,Stringzone,StringnodeName,StringtpuType,StringtpuSoftwareVersion)
throwsIOException,ExecutionException,InterruptedException{
// With these settings the client library handles the Operation's polling mechanism
// and prevent CancellationException error
TpuSettings .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(TpuClient tpuClient=TpuClient .create(clientSettings.build())){
Stringparent=String.format("projects/%s/locations/%s",projectId,zone);
Node tpuVm=Node .newBuilder()
.setName(nodeName)
.setAcceleratorType(tpuType)
.setRuntimeVersion(tpuSoftwareVersion)
.build();
CreateNodeRequest request=CreateNodeRequest .newBuilder()
.setParent(parent)
.setNodeId(nodeName)
.setNode(tpuVm)
.build();
returntpuClient.createNodeAsync (request).get ();
}
}
}
Node.js
To authenticate to Cloud TPU, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
// 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-zones
constzone='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/runtimes
consttpuSoftwareVersion='v2-tpuv5-litepod';
asyncfunctioncallCreateTpuVM(){
// Create a node
constnode=newNode({
name:nodeName,
zone,
acceleratorType:tpuType,
runtimeVersion:tpuSoftwareVersion,
// Define network
networkConfig: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();Python
To authenticate to Cloud TPU, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
fromgoogle.cloudimport tpu_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 node
node = 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_version
request = tpu_v2.CreateNodeRequest(
parent=f"projects/{project_id}/locations/{zone}",
node_id=tpu_name,
node=node,
)
# Create a TPU client
client = 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-script flag when creating the TPU VM. The following
command creates a TPU VM using a startup script.
$gcloudcomputetpustpu-vmcreatetpu-name\ --zone=us-central2-b\ --accelerator-type=v5litepod-8\ --version=tpu-software-version\ --metadatastartup-script='#! /bin/bash pip3 install numpy EOF'
Java
To authenticate to Cloud TPU, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
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;
publicclass CreateTpuVmWithStartupScript{
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-zones
Stringzone="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/runtimes
StringtpuSoftwareVersion="v2-tpuv5-litepod";
createTpuVmWithStartupScript(projectId,zone,nodeName,acceleratorType,tpuSoftwareVersion);
}
// Create a TPU VM with a startup script.
publicstaticNode createTpuVmWithStartupScript(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(TpuClient tpuClient=TpuClient .create()){
Stringparent=String.format("projects/%s/locations/%s",projectId,zone);
StringstartupScriptContent="#!/bin/bash\necho \"Hello from the startup script!\"";
// Add startup script to metadata
Map<String,String>metadata=newHashMap<>();
metadata.put("startup-script",startupScriptContent);
Node tpuVm=
Node .newBuilder()
.setName(nodeName)
.setAcceleratorType(acceleratorType)
.setRuntimeVersion(tpuSoftwareVersion)
.putAllMetadata (metadata)
.build();
CreateNodeRequest request=
CreateNodeRequest .newBuilder()
.setParent(parent)
.setNodeId(nodeName)
.setNode(tpuVm)
.build();
returntpuClient.createNodeAsync (request).get();
}
}
}
Node.js
To authenticate to Cloud TPU, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
// 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-zones
constzone='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/runtimes
consttpuSoftwareVersion='v2-tpuv5-litepod';
asyncfunctioncallCreateTpuVMStartupScript(){
// Create a node
constnode=newNode({
name:nodeName,
zone,
acceleratorType:tpuType,
runtimeVersion:tpuSoftwareVersion,
// Define network
networkConfig: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/bash
echo "Hello World" > /var/log/hello.log
sudo 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();Python
To authenticate to Cloud TPU, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
fromgoogle.cloudimport tpu_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_type
node.runtime_version = runtime_version
# This startup script updates numpy to the latest version and logs the output to a file.
metadata = {
"startup-script": """#!/bin/bash
echo "Hello World" > /var/log/hello.log
sudo 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 in Connect 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 the TPU version.
To install binaries or run code, connect to each TPU VM using
the tpu-vm ssh command .
$gcloudcomputetpustpu-vmsshtpu-name--zone=zone
To connect to a specific TPU VM
using SSH, use the --worker flag which follows a 0-based index:
$gcloudcomputetpustpu-vmsshtpu-name--zone=zone--worker=1
To run a command on all TPU VMs with a single command, use the
--worker=all and --command flags:
$gcloudcomputetpustpu-vmsshtpu-name\ --zone=zone\ --worker=all\ --command='pip install "jax[tpu]==0.4.20" -f https://storage.googleapis.com/jax-releases/libtpu_releases.html'
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 --command flags, with an optional --batch-size
flag.
$gcloudcomputetpusqueued-resourcesssh${QUEUED_RESOURCE_ID}\ --zone=zone\ --node=all\ --worker=all\ --command='pip install jax[tpu] -f https://storage.googleapis.com/jax-releases/libtpu_releases.html'\ --batch-size=4
Console
To connect to your TPUs in the Google Cloud console, use SSH-in-browser:
In the Google Cloud console, go to the TPUs page:
In the list of TPU VMs, click SSH in the row of the TPU VM that you want to connect to.
List your Cloud TPU resources
You can list all of your Cloud TPUs in a specified zone.
gcloud
$gcloudcomputetpustpu-vmlist--zone=zone
Console
In the Google Cloud console, go to the TPUs page:
Java
To authenticate to Cloud TPU, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
importcom.google.cloud.tpu.v2.ListNodesRequest ;
importcom.google.cloud.tpu.v2.TpuClient ;
importjava.io.IOException;
publicclass ListTpuVms{
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-zones
Stringzone="us-central1-f";
listTpuVms(projectId,zone);
}
// Lists TPU VMs in the specified zone.
publicstaticTpuClient .ListNodesPage listTpuVms(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(TpuClient tpuClient=TpuClient .create()){
Stringparent=String.format("projects/%s/locations/%s",projectId,zone);
ListNodesRequest request=ListNodesRequest .newBuilder().setParent(parent).build();
returntpuClient.listNodes(request).getPage();
}
}
}
Node.js
To authenticate to Cloud TPU, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
// 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();Python
To authenticate to Cloud TPU, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
fromgoogle.cloudimport tpu_v2
# TODO(developer): Update and un-comment below lines
# project_id = "your-project-id"
# zone = "us-central1-b"
client = tpu_v2.TpuClient()
nodes = client.list_nodes (parent=f"projects/{project_id}/locations/{zone}")
for node in nodes:
print(node.name)
print(node.state)
print(node.accelerator_type)
# Example response:
# projects/[project_id]/locations/[zone]/nodes/node-name
# State.READY
# v2-8
Retrieve information about your Cloud TPU
You can retrieve information about a specified Cloud TPU.
gcloud
$gcloudcomputetpustpu-vmdescribetpu-name\ --zone=zone
Console
In the Google Cloud console, go to the TPUs page:
Click the name of your Cloud TPU. The console displays the Cloud TPU detail page.
Java
To authenticate to Cloud TPU, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
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;
publicclass GetTpuVm{
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-zones
Stringzone="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.
publicstaticNode getTpuVm(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(TpuClient tpuClient=TpuClient .create()){
Stringname=NodeName .of(projectId,zone,nodeName).toString();
GetNodeRequest request=GetNodeRequest .newBuilder().setName(name).build();
returntpuClient.getNode(request);
}
}
}
Node.js
To authenticate to Cloud TPU, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
// 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();Python
To authenticate to Cloud TPU, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
fromgoogle.cloudimport 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()
node = client.get_node (
name=f"projects/{project_id}/locations/{zone}/nodes/{tpu_name}"
)
print(node)
# Example response:
# name: "projects/[project_id]/locations/[zone]/nodes/tpu-name"
# state: "READY"
# runtime_version: ...
Stop your Cloud TPU resources
You can stop a single Cloud TPU to stop incurring charges without losing your VM's configuration and software.
gcloud
$gcloudcomputetpustpu-vmstoptpu-name\ --zone=zone
Console
In the Google Cloud console, go to the TPUs page:
Select the checkbox next to your Cloud TPU.
Click Stop.
Java
To authenticate to Cloud TPU, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
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;
publicclass StopTpuVm{
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-zones
Stringzone="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.
publicstaticNode stopTpuVm(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(TpuClient tpuClient=TpuClient .create()){
Stringname=NodeName .of(projectId,zone,nodeName).toString();
StopNodeRequest request=StopNodeRequest .newBuilder().setName(name).build();
returntpuClient.stopNodeAsync (request).get();
}
}
}
Node.js
To authenticate to Cloud TPU, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
// 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();Python
To authenticate to Cloud TPU, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
fromgoogle.cloudimport tpu_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.STOPPED
except Exception as e:
print(e)
raise e
Start your Cloud TPU resources
You can start a Cloud TPU when it is stopped.
gcloud
$gcloudcomputetpustpu-vmstarttpu-name\ --zone=zone
Console
In the Google Cloud console, go to the TPUs page:
Select the checkbox next to your Cloud TPU.
Click Start.
Java
To authenticate to Cloud TPU, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
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;
publicclass StartTpuVm{
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-zones
Stringzone="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.
publicstaticNode startTpuVm(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(TpuClient tpuClient=TpuClient .create()){
Stringname=NodeName .of(projectId,zone,nodeName).toString();
StartNodeRequest request=StartNodeRequest .newBuilder().setName(name).build();
returntpuClient.startNodeAsync (request).get();
}
}
}
Node.js
To authenticate to Cloud TPU, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
// 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();Python
To authenticate to Cloud TPU, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
fromgoogle.cloudimport tpu_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.READY
except Exception as e:
print(e)
raise e
Delete a Cloud TPU
Delete your TPU VM slices at the end of your session.
gcloud
$gcloudcomputetpustpu-vmdeletetpu-name\ --zone=zone\ --quiet
Command flag descriptions
zone- The zone where you plan to delete your Cloud TPU.
quiet- Disables all interactive prompts when running gcloud CLI commands.
Console
In the Google Cloud console, go to the TPUs page:
Select the checkbox next to your Cloud TPU.
Click Delete.
Java
To authenticate to Cloud TPU, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
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;
publicclass DeleteTpuVm{
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-zones
Stringzone="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 error
TpuSettings .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(TpuClient tpuClient=TpuClient .create(clientSettings.build())){
Stringname=NodeName .of(projectId,zone,nodeName).toString();
DeleteNodeRequest request=DeleteNodeRequest .newBuilder().setName(name).build();
tpuClient.deleteNodeAsync (request).get ();
System.out.println("TPU VM deleted");
}
}
}
Node.js
To authenticate to Cloud TPU, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
// 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();Python
To authenticate to Cloud TPU, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
fromgoogle.cloudimport 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)