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:

  1. 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 .

  2. If you're using one of the Cloud Client Libraries , follow the setup instructions for the language you're using:

  3. To avoid setting the --project flag in every gcloud CLI command, use the gcloud config set command to set the project ID in your active configuration:

    gcloud  
    config  
     set 
      
    project  
     project-id 
    

    Replace 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:

 $  
 
gcloud  
compute  
tpus  
tpu-vm  
create  
 tpu-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

  1. In the Google Cloud console, go to the TPUspage:

    Go to TPUs

  2. Click Create TPU.

  3. In the Namefield, enter a name for your TPU.

  4. In the Zonebox, select the zone in which to create the TPU.

  5. In the TPU 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, see TPU versions .

  6. In the TPU 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, see TPU software versions .

  7. Click Createto create your resources.

curl

The following command uses curl to create a TPU.

 $  
 
curl  
-X  
POST  
-H  
 "Authorization: Bearer 
 $( 
gcloud  
auth  
print-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 .

  import 
  
 com.google.api.gax.longrunning. OperationTimedPollAlgorithm 
 
 ; 
 import 
  
 com.google.api.gax.retrying. RetrySettings 
 
 ; 
 import 
  
 com.google.cloud.tpu.v2. CreateNodeRequest 
 
 ; 
 import 
  
 com.google.cloud.tpu.v2. Node 
 
 ; 
 import 
  
 com.google.cloud.tpu.v2. TpuClient 
 
 ; 
 import 
  
 com.google.cloud.tpu.v2. TpuSettings 
 
 ; 
 import 
  
 java.io.IOException 
 ; 
 import 
  
 java.util.concurrent.ExecutionException 
 ; 
 import 
  
 org.threeten.bp.Duration 
 ; 
 public 
  
 class 
 CreateTpuVm 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 throws 
  
 IOException 
 , 
  
 ExecutionException 
 , 
  
 InterruptedException 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 // Project ID or project number of the Google Cloud project you want to create a node. 
  
 String 
  
 projectId 
  
 = 
  
 "YOUR_PROJECT_ID" 
 ; 
  
 // The zone in which to create the TPU. 
  
 // For more information about supported TPU types for specific zones, 
  
 // see https://cloud.google.com/tpu/docs/regions-zones 
  
 String 
  
 zone 
  
 = 
  
 "europe-west4-a" 
 ; 
  
 // The name for your TPU. 
  
 String 
  
 nodeName 
  
 = 
  
 "YOUR_TPU_NAME" 
 ; 
  
 // The accelerator type that specifies the version and size of the Cloud TPU you want to create. 
  
 // For more information about supported accelerator types for each TPU version, 
  
 // see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions. 
  
 String 
  
 tpuType 
  
 = 
  
 "v2-8" 
 ; 
  
 // Software version that specifies the version of the TPU runtime to install. 
  
 // For more information see https://cloud.google.com/tpu/docs/runtimes 
  
 String 
  
 tpuSoftwareVersion 
  
 = 
  
 "v2-tpuv5-litepod" 
 ; 
  
 createTpuVm 
 ( 
 projectId 
 , 
  
 zone 
 , 
  
 nodeName 
 , 
  
 tpuType 
 , 
  
 tpuSoftwareVersion 
 ); 
  
 } 
  
 // Creates a TPU VM with the specified name, zone, accelerator type, and version. 
  
 public 
  
 static 
  
  Node 
 
  
 createTpuVm 
 ( 
  
 String 
  
 projectId 
 , 
  
 String 
  
 zone 
 , 
  
 String 
  
 nodeName 
 , 
  
 String 
  
 tpuType 
 , 
  
 String 
  
 tpuSoftwareVersion 
 ) 
  
 throws 
  
 IOException 
 , 
  
 ExecutionException 
 , 
  
 InterruptedException 
  
 { 
  
 // With these settings the client library handles the Operation's polling mechanism 
  
 // and prevent CancellationException error 
  
  TpuSettings 
 
 . 
 Builder 
  
 clientSettings 
  
 = 
  
  TpuSettings 
 
 . 
 newBuilder 
 (); 
  
 clientSettings 
  
 . 
 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 
 ())) 
  
 { 
  
 String 
  
 parent 
  
 = 
  
 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 
 (); 
  
 return 
  
 tpuClient 
 . 
  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. 
 const 
  
 projectId 
  
 = 
  
 await 
  
 tpuClient 
 . 
 getProjectId 
 (); 
 // The name of the network you want the TPU node to connect to. The network should be assigned to your project. 
 const 
  
 networkName 
  
 = 
  
 'compute-tpu-network' 
 ; 
 // The region of the network, that you want the TPU node to connect to. 
 const 
  
 region 
  
 = 
  
 'europe-west4' 
 ; 
 // The name for your TPU. 
 const 
  
 nodeName 
  
 = 
  
 '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 
 const 
  
 zone 
  
 = 
  
 '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. 
 const 
  
 tpuType 
  
 = 
  
 'v5litepod-4' 
 ; 
 // Software version that specifies the version of the TPU runtime to install. For more information, 
 // see https://cloud.google.com/tpu/docs/runtimes 
 const 
  
 tpuSoftwareVersion 
  
 = 
  
 'v2-tpuv5-litepod' 
 ; 
 async 
  
 function 
  
 callCreateTpuVM 
 () 
  
 { 
  
 // Create a node 
  
 const 
  
 node 
  
 = 
  
 new 
  
 Node 
 ({ 
  
 name 
 : 
  
 nodeName 
 , 
  
 zone 
 , 
  
 acceleratorType 
 : 
  
 tpuType 
 , 
  
 runtimeVersion 
 : 
  
 tpuSoftwareVersion 
 , 
  
 // Define network 
  
 networkConfig 
 : 
  
 new 
  
 NetworkConfig 
 ({ 
  
 enableExternalIps 
 : 
  
 true 
 , 
  
 network 
 : 
  
 `projects/ 
 ${ 
 projectId 
 } 
 /global/networks/ 
 ${ 
 networkName 
 } 
 ` 
 , 
  
 subnetwork 
 : 
  
 `projects/ 
 ${ 
 projectId 
 } 
 /regions/ 
 ${ 
 region 
 } 
 /subnetworks/ 
 ${ 
 networkName 
 } 
 ` 
 , 
  
 }), 
  
 }); 
  
 const 
  
 parent 
  
 = 
  
 `projects/ 
 ${ 
 projectId 
 } 
 /locations/ 
 ${ 
 zone 
 } 
 ` 
 ; 
  
 const 
  
 request 
  
 = 
  
 { 
 parent 
 , 
  
 node 
 , 
  
 nodeId 
 : 
  
 nodeName 
 }; 
  
 const 
  
 [ 
 operation 
 ] 
  
 = 
  
 await 
  
 tpuClient 
 . 
 createNode 
 ( 
 request 
 ); 
  
 // Wait for the create operation to complete. 
  
 const 
  
 [ 
 response 
 ] 
  
 = 
  
 await 
  
 operation 
 . 
 promise 
 (); 
  
 console 
 . 
 log 
 ( 
 `TPU VM: 
 ${ 
 nodeName 
 } 
 created.` 
 ); 
  
 return 
  
 response 
 ; 
 } 
 return 
  
 await 
  
 callCreateTpuVM 
 (); 
 

Python

To authenticate to Cloud TPU, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  from 
  
 google.cloud 
  
 import 
 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.

 $  
 
gcloud  
compute  
tpus  
tpu-vm  
create  
 tpu-name 
  
 \ 
  
--zone = 
 us-central2-b 
  
 \ 
  
--accelerator-type = 
 v5litepod-8 
  
 \ 
  
--version = 
 tpu-software-version 
  
 \ 
  
--metadata  
startup-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 .

  import 
  
 com.google.cloud.tpu.v2. CreateNodeRequest 
 
 ; 
 import 
  
 com.google.cloud.tpu.v2. Node 
 
 ; 
 import 
  
 com.google.cloud.tpu.v2. TpuClient 
 
 ; 
 import 
  
 java.io.IOException 
 ; 
 import 
  
 java.util.HashMap 
 ; 
 import 
  
 java.util.Map 
 ; 
 import 
  
 java.util.concurrent.ExecutionException 
 ; 
 public 
  
 class 
 CreateTpuVmWithStartupScript 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 throws 
  
 IOException 
 , 
  
 ExecutionException 
 , 
  
 InterruptedException 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 // Project ID or project number of the Google Cloud project you want to create a node. 
  
 String 
  
 projectId 
  
 = 
  
 "YOUR_PROJECT_ID" 
 ; 
  
 // The zone in which to create the TPU. 
  
 // For more information about supported TPU types for specific zones, 
  
 // see https://cloud.google.com/tpu/docs/regions-zones 
  
 String 
  
 zone 
  
 = 
  
 "us-central1-a" 
 ; 
  
 // The name for your TPU. 
  
 String 
  
 nodeName 
  
 = 
  
 "YOUR_TPU_NAME" 
 ; 
  
 // The accelerator type that specifies the version and size of the Cloud TPU you want to create. 
  
 // For more information about supported accelerator types for each TPU version, 
  
 // see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions. 
  
 String 
  
 acceleratorType 
  
 = 
  
 "v5litepod-4" 
 ; 
  
 // Software version that specifies the version of the TPU runtime to install. 
  
 // For more information, see https://cloud.google.com/tpu/docs/runtimes 
  
 String 
  
 tpuSoftwareVersion 
  
 = 
  
 "v2-tpuv5-litepod" 
 ; 
  
 createTpuVmWithStartupScript 
 ( 
 projectId 
 , 
  
 zone 
 , 
  
 nodeName 
 , 
  
 acceleratorType 
 , 
  
 tpuSoftwareVersion 
 ); 
  
 } 
  
 // Create a TPU VM with a startup script. 
  
 public 
  
 static 
  
  Node 
 
  
 createTpuVmWithStartupScript 
 ( 
 String 
  
 projectId 
 , 
  
 String 
  
 zone 
 , 
  
 String 
  
 nodeName 
 , 
  
 String 
  
 acceleratorType 
 , 
  
 String 
  
 tpuSoftwareVersion 
 ) 
  
 throws 
  
 IOException 
 , 
  
 ExecutionException 
 , 
  
 InterruptedException 
  
 { 
  
 // Initialize client that will be used to send requests. This client only needs to be created 
  
 // once, and can be reused for multiple requests. 
  
 try 
  
 ( 
  TpuClient 
 
  
 tpuClient 
  
 = 
  
  TpuClient 
 
 . 
 create 
 ()) 
  
 { 
  
 String 
  
 parent 
  
 = 
  
 String 
 . 
 format 
 ( 
 "projects/%s/locations/%s" 
 , 
  
 projectId 
 , 
  
 zone 
 ); 
  
 String 
  
 startupScriptContent 
  
 = 
  
 "#!/bin/bash\necho \"Hello from the startup script!\"" 
 ; 
  
 // Add startup script to metadata 
  
 Map<String 
 , 
  
 String 
>  
 metadata 
  
 = 
  
 new 
  
 HashMap 
<> (); 
  
 metadata 
 . 
 put 
 ( 
 "startup-script" 
 , 
  
 startupScriptContent 
 ); 
  
  Node 
 
  
 tpuVm 
  
 = 
  
  Node 
 
 . 
 newBuilder 
 () 
  
 . 
 setName 
 ( 
 nodeName 
 ) 
  
 . 
 setAcceleratorType 
 ( 
 acceleratorType 
 ) 
  
 . 
 setRuntimeVersion 
 ( 
 tpuSoftwareVersion 
 ) 
  
 . 
  putAllMetadata 
 
 ( 
 metadata 
 ) 
  
 . 
 build 
 (); 
  
  CreateNodeRequest 
 
  
 request 
  
 = 
  
  CreateNodeRequest 
 
 . 
 newBuilder 
 () 
  
 . 
 setParent 
 ( 
 parent 
 ) 
  
 . 
 setNodeId 
 ( 
 nodeName 
 ) 
  
 . 
 setNode 
 ( 
 tpuVm 
 ) 
  
 . 
 build 
 (); 
  
 return 
  
 tpuClient 
 . 
  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. 
 const 
  
 projectId 
  
 = 
  
 await 
  
 tpuClient 
 . 
 getProjectId 
 (); 
 // The name of the network you want the TPU node to connect to. The network should be assigned to your project. 
 const 
  
 networkName 
  
 = 
  
 'compute-tpu-network' 
 ; 
 // The region of the network, that you want the TPU node to connect to. 
 const 
  
 region 
  
 = 
  
 'europe-west4' 
 ; 
 // The name for your TPU. 
 const 
  
 nodeName 
  
 = 
  
 '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 
 const 
  
 zone 
  
 = 
  
 '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. 
 const 
  
 tpuType 
  
 = 
  
 'v5litepod-4' 
 ; 
 // Software version that specifies the version of the TPU runtime to install. For more information, 
 // see https://cloud.google.com/tpu/docs/runtimes 
 const 
  
 tpuSoftwareVersion 
  
 = 
  
 'v2-tpuv5-litepod' 
 ; 
 async 
  
 function 
  
 callCreateTpuVMStartupScript 
 () 
  
 { 
  
 // Create a node 
  
 const 
  
 node 
  
 = 
  
 new 
  
 Node 
 ({ 
  
 name 
 : 
  
 nodeName 
 , 
  
 zone 
 , 
  
 acceleratorType 
 : 
  
 tpuType 
 , 
  
 runtimeVersion 
 : 
  
 tpuSoftwareVersion 
 , 
  
 // Define network 
  
 networkConfig 
 : 
  
 new 
  
 NetworkConfig 
 ({ 
  
 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` 
 , 
  
 }, 
  
 }); 
  
 const 
  
 parent 
  
 = 
  
 `projects/ 
 ${ 
 projectId 
 } 
 /locations/ 
 ${ 
 zone 
 } 
 ` 
 ; 
  
 const 
  
 request 
  
 = 
  
 { 
 parent 
 , 
  
 node 
 , 
  
 nodeId 
 : 
  
 nodeName 
 }; 
  
 const 
  
 [ 
 operation 
 ] 
  
 = 
  
 await 
  
 tpuClient 
 . 
 createNode 
 ( 
 request 
 ); 
  
 // Wait for the create operation to complete. 
  
 const 
  
 [ 
 response 
 ] 
  
 = 
  
 await 
  
 operation 
 . 
 promise 
 (); 
  
 console 
 . 
 log 
 ( 
 JSON 
 . 
 stringify 
 ( 
 response 
 )); 
  
 return 
  
 response 
 ; 
 } 
 return 
  
 await 
  
 callCreateTpuVMStartupScript 
 (); 
 

Python

To authenticate to Cloud TPU, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  from 
  
 google.cloud 
  
 import 
 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:

 $  
 
gcloud  
compute  
tpus  
tpu-vm  
ssh  
 tpu-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 .

 $  
 
gcloud  
compute  
tpus  
tpu-vm  
ssh  
 tpu-name 
  
--zone = 
 zone 

To connect to a specific TPU VM using SSH, use the --worker flag which follows a 0-based index:

 $  
 
gcloud  
compute  
tpus  
tpu-vm  
ssh  
 tpu-name 
  
--zone = 
 zone 
  
--worker = 
 1 

To run a command on all TPU VMs with a single command, use the --worker=all and --command flags:

 $  
 
gcloud  
compute  
tpus  
tpu-vm  
ssh  
 tpu-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.

 $  
 
gcloud  
compute  
tpus  
queued-resources  
ssh  
 ${ 
 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:

  1. In the Google Cloud console, go to the TPUspage:

    Go to TPUs

  2. In the list of TPU VMs, click SSHin 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

 $  
 
gcloud  
compute  
tpus  
tpu-vm  
list  
--zone = 
 zone 

Console

In the Google Cloud console, go to the TPUspage:

Go to TPUs

Java

To authenticate to Cloud TPU, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  import 
  
 com.google.cloud.tpu.v2. ListNodesRequest 
 
 ; 
 import 
  
 com.google.cloud.tpu.v2. TpuClient 
 
 ; 
 import 
  
 java.io.IOException 
 ; 
 public 
  
 class 
 ListTpuVms 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 throws 
  
 IOException 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 // Project ID or project number of the Google Cloud project you want to use. 
  
 String 
  
 projectId 
  
 = 
  
 "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 
  
 String 
  
 zone 
  
 = 
  
 "us-central1-f" 
 ; 
  
 listTpuVms 
 ( 
 projectId 
 , 
  
 zone 
 ); 
  
 } 
  
 // Lists TPU VMs in the specified zone. 
  
 public 
  
 static 
  
  TpuClient 
 
 . 
  ListNodesPage 
 
  
 listTpuVms 
 ( 
 String 
  
 projectId 
 , 
  
 String 
  
 zone 
 ) 
  
 throws 
  
 IOException 
  
 { 
  
 // Initialize client that will be used to send requests. This client only needs to be created 
  
 // once, and can be reused for multiple requests. 
  
 try 
  
 ( 
  TpuClient 
 
  
 tpuClient 
  
 = 
  
  TpuClient 
 
 . 
 create 
 ()) 
  
 { 
  
 String 
  
 parent 
  
 = 
  
 String 
 . 
 format 
 ( 
 "projects/%s/locations/%s" 
 , 
  
 projectId 
 , 
  
 zone 
 ); 
  
  ListNodesRequest 
 
  
 request 
  
 = 
  
  ListNodesRequest 
 
 . 
 newBuilder 
 (). 
 setParent 
 ( 
 parent 
 ). 
 build 
 (); 
  
 return 
  
 tpuClient 
 . 
 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. 
 const 
  
 projectId 
  
 = 
  
 await 
  
 tpuClient 
 . 
 getProjectId 
 (); 
 // The zone from which the TPUs are retrived. 
 const 
  
 zone 
  
 = 
  
 'europe-west4-a' 
 ; 
 async 
  
 function 
  
 callTpuVMList 
 () 
  
 { 
  
 const 
  
 request 
  
 = 
  
 { 
  
 parent 
 : 
  
 `projects/ 
 ${ 
 projectId 
 } 
 /locations/ 
 ${ 
 zone 
 } 
 ` 
 , 
  
 }; 
  
 const 
  
 [ 
 response 
 ] 
  
 = 
  
 await 
  
 tpuClient 
 . 
 listNodes 
 ( 
 request 
 ); 
  
 return 
  
 response 
 ; 
 } 
 return 
  
 await 
  
 callTpuVMList 
 (); 
 

Python

To authenticate to Cloud TPU, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  from 
  
 google.cloud 
  
 import 
 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

 $  
 
gcloud  
compute  
tpus  
tpu-vm  
describe  
 tpu-name 
  
 \ 
  
--zone = 
 zone 

Console

  1. In the Google Cloud console, go to the TPUspage:

    Go to TPUs

  2. 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 .

  import 
  
 com.google.cloud.tpu.v2. GetNodeRequest 
 
 ; 
 import 
  
 com.google.cloud.tpu.v2. Node 
 
 ; 
 import 
  
 com.google.cloud.tpu.v2. NodeName 
 
 ; 
 import 
  
 com.google.cloud.tpu.v2. TpuClient 
 
 ; 
 import 
  
 java.io.IOException 
 ; 
 public 
  
 class 
 GetTpuVm 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 throws 
  
 IOException 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 // Project ID or project number of the Google Cloud project you want to use. 
  
 String 
  
 projectId 
  
 = 
  
 "YOUR_PROJECT_ID" 
 ; 
  
 // The zone in which to create the TPU. 
  
 // For more information about supported TPU types for specific zones, 
  
 // see https://cloud.google.com/tpu/docs/regions-zones 
  
 String 
  
 zone 
  
 = 
  
 "europe-west4-a" 
 ; 
  
 // The name for your TPU. 
  
 String 
  
 nodeName 
  
 = 
  
 "YOUR_TPU_NAME" 
 ; 
  
 getTpuVm 
 ( 
 projectId 
 , 
  
 zone 
 , 
  
 nodeName 
 ); 
  
 } 
  
 // Describes a TPU VM with the specified name in the given project and zone. 
  
 public 
  
 static 
  
  Node 
 
  
 getTpuVm 
 ( 
 String 
  
 projectId 
 , 
  
 String 
  
 zone 
 , 
  
 String 
  
 nodeName 
 ) 
  
 throws 
  
 IOException 
  
 { 
  
 // Initialize client that will be used to send requests. This client only needs to be created 
  
 // once, and can be reused for multiple requests. 
  
 try 
  
 ( 
  TpuClient 
 
  
 tpuClient 
  
 = 
  
  TpuClient 
 
 . 
 create 
 ()) 
  
 { 
  
 String 
  
 name 
  
 = 
  
  NodeName 
 
 . 
 of 
 ( 
 projectId 
 , 
  
 zone 
 , 
  
 nodeName 
 ). 
 toString 
 (); 
  
  GetNodeRequest 
 
  
 request 
  
 = 
  
  GetNodeRequest 
 
 . 
 newBuilder 
 (). 
 setName 
 ( 
 name 
 ). 
 build 
 (); 
  
 return 
  
 tpuClient 
 . 
 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. 
 const 
  
 projectId 
  
 = 
  
 await 
  
 tpuClient 
 . 
 getProjectId 
 (); 
 // The name of TPU to retrive. 
 const 
  
 nodeName 
  
 = 
  
 'node-name-1' 
 ; 
 // The zone, where the TPU is created. 
 const 
  
 zone 
  
 = 
  
 'europe-west4-a' 
 ; 
 async 
  
 function 
  
 callGetTpuVM 
 () 
  
 { 
  
 const 
  
 request 
  
 = 
  
 { 
  
 name 
 : 
  
 `projects/ 
 ${ 
 projectId 
 } 
 /locations/ 
 ${ 
 zone 
 } 
 /nodes/ 
 ${ 
 nodeName 
 } 
 ` 
 , 
  
 }; 
  
 const 
  
 [ 
 response 
 ] 
  
 = 
  
 await 
  
 tpuClient 
 . 
 getNode 
 ( 
 request 
 ); 
  
 console 
 . 
 log 
 ( 
 `Node: 
 ${ 
 nodeName 
 } 
 retrived.` 
 ); 
  
 return 
  
 response 
 ; 
 } 
 return 
  
 await 
  
 callGetTpuVM 
 (); 
 

Python

To authenticate to Cloud TPU, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  from 
  
 google.cloud 
  
 import 
 tpu_v2 
 # TODO(developer): Update and un-comment below lines 
 # project_id = "your-project-id" 
 # zone = "us-central1-b" 
 # tpu_name = "tpu-name" 
 client 
 = 
 tpu_v2 
 . 
 TpuClient 
 () 
 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

 $  
 
gcloud  
compute  
tpus  
tpu-vm  
stop  
 tpu-name 
  
 \ 
  
--zone = 
 zone 

Console

  1. In the Google Cloud console, go to the TPUspage:

    Go to TPUs

  2. Select the checkbox next to your Cloud TPU.

  3. 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 .

  import 
  
 com.google.cloud.tpu.v2. Node 
 
 ; 
 import 
  
 com.google.cloud.tpu.v2. NodeName 
 
 ; 
 import 
  
 com.google.cloud.tpu.v2. StopNodeRequest 
 
 ; 
 import 
  
 com.google.cloud.tpu.v2. TpuClient 
 
 ; 
 import 
  
 java.io.IOException 
 ; 
 import 
  
 java.util.concurrent.ExecutionException 
 ; 
 public 
  
 class 
 StopTpuVm 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 throws 
  
 IOException 
 , 
  
 ExecutionException 
 , 
  
 InterruptedException 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 // Project ID or project number of the Google Cloud project you want to use. 
  
 String 
  
 projectId 
  
 = 
  
 "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 
  
 String 
  
 zone 
  
 = 
  
 "us-central1-f" 
 ; 
  
 // The name for your TPU. 
  
 String 
  
 nodeName 
  
 = 
  
 "YOUR_TPU_NAME" 
 ; 
  
 stopTpuVm 
 ( 
 projectId 
 , 
  
 zone 
 , 
  
 nodeName 
 ); 
  
 } 
  
 // Stops a TPU VM with the specified name in the given project and zone. 
  
 public 
  
 static 
  
  Node 
 
  
 stopTpuVm 
 ( 
 String 
  
 projectId 
 , 
  
 String 
  
 zone 
 , 
  
 String 
  
 nodeName 
 ) 
  
 throws 
  
 IOException 
 , 
  
 ExecutionException 
 , 
  
 InterruptedException 
  
 { 
  
 // Initialize client that will be used to send requests. This client only needs to be created 
  
 // once, and can be reused for multiple requests. 
  
 try 
  
 ( 
  TpuClient 
 
  
 tpuClient 
  
 = 
  
  TpuClient 
 
 . 
 create 
 ()) 
  
 { 
  
 String 
  
 name 
  
 = 
  
  NodeName 
 
 . 
 of 
 ( 
 projectId 
 , 
  
 zone 
 , 
  
 nodeName 
 ). 
 toString 
 (); 
  
  StopNodeRequest 
 
  
 request 
  
 = 
  
  StopNodeRequest 
 
 . 
 newBuilder 
 (). 
 setName 
 ( 
 name 
 ). 
 build 
 (); 
  
 return 
  
 tpuClient 
 . 
  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. 
 const 
  
 projectId 
  
 = 
  
 await 
  
 tpuClient 
 . 
 getProjectId 
 (); 
 // The name of TPU to stop. 
 const 
  
 nodeName 
  
 = 
  
 'node-name-1' 
 ; 
 // The zone, where the TPU is created. 
 const 
  
 zone 
  
 = 
  
 'europe-west4-a' 
 ; 
 async 
  
 function 
  
 callStopTpuVM 
 () 
  
 { 
  
 const 
  
 request 
  
 = 
  
 { 
  
 name 
 : 
  
 `projects/ 
 ${ 
 projectId 
 } 
 /locations/ 
 ${ 
 zone 
 } 
 /nodes/ 
 ${ 
 nodeName 
 } 
 ` 
 , 
  
 }; 
  
 const 
  
 [ 
 operation 
 ] 
  
 = 
  
 await 
  
 tpuClient 
 . 
 stopNode 
 ( 
 request 
 ); 
  
 // Wait for the operation to complete. 
  
 const 
  
 [ 
 response 
 ] 
  
 = 
  
 await 
  
 operation 
 . 
 promise 
 (); 
  
 console 
 . 
 log 
 ( 
 `Node: 
 ${ 
 nodeName 
 } 
 stopped.` 
 ); 
  
 return 
  
 response 
 ; 
 } 
 return 
  
 await 
  
 callStopTpuVM 
 (); 
 

Python

To authenticate to Cloud TPU, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  from 
  
 google.cloud 
  
 import 
 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

 $  
 
gcloud  
compute  
tpus  
tpu-vm  
start  
 tpu-name 
  
 \ 
  
--zone = 
 zone 

Console

  1. In the Google Cloud console, go to the TPUspage:

    Go to TPUs

  2. Select the checkbox next to your Cloud TPU.

  3. 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 .

  import 
  
 com.google.cloud.tpu.v2. Node 
 
 ; 
 import 
  
 com.google.cloud.tpu.v2. NodeName 
 
 ; 
 import 
  
 com.google.cloud.tpu.v2. StartNodeRequest 
 
 ; 
 import 
  
 com.google.cloud.tpu.v2. TpuClient 
 
 ; 
 import 
  
 java.io.IOException 
 ; 
 import 
  
 java.util.concurrent.ExecutionException 
 ; 
 public 
  
 class 
 StartTpuVm 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 throws 
  
 IOException 
 , 
  
 ExecutionException 
 , 
  
 InterruptedException 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 // Project ID or project number of the Google Cloud project you want to use. 
  
 String 
  
 projectId 
  
 = 
  
 "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 
  
 String 
  
 zone 
  
 = 
  
 "us-central1-f" 
 ; 
  
 // The name for your TPU. 
  
 String 
  
 nodeName 
  
 = 
  
 "YOUR_TPU_NAME" 
 ; 
  
 startTpuVm 
 ( 
 projectId 
 , 
  
 zone 
 , 
  
 nodeName 
 ); 
  
 } 
  
 // Starts a TPU VM with the specified name in the given project and zone. 
  
 public 
  
 static 
  
  Node 
 
  
 startTpuVm 
 ( 
 String 
  
 projectId 
 , 
  
 String 
  
 zone 
 , 
  
 String 
  
 nodeName 
 ) 
  
 throws 
  
 IOException 
 , 
  
 ExecutionException 
 , 
  
 InterruptedException 
  
 { 
  
 // Initialize client that will be used to send requests. This client only needs to be created 
  
 // once, and can be reused for multiple requests. 
  
 try 
  
 ( 
  TpuClient 
 
  
 tpuClient 
  
 = 
  
  TpuClient 
 
 . 
 create 
 ()) 
  
 { 
  
 String 
  
 name 
  
 = 
  
  NodeName 
 
 . 
 of 
 ( 
 projectId 
 , 
  
 zone 
 , 
  
 nodeName 
 ). 
 toString 
 (); 
  
  StartNodeRequest 
 
  
 request 
  
 = 
  
  StartNodeRequest 
 
 . 
 newBuilder 
 (). 
 setName 
 ( 
 name 
 ). 
 build 
 (); 
  
 return 
  
 tpuClient 
 . 
  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. 
 const 
  
 projectId 
  
 = 
  
 await 
  
 tpuClient 
 . 
 getProjectId 
 (); 
 // The name of TPU to start. 
 const 
  
 nodeName 
  
 = 
  
 'node-name-1' 
 ; 
 // The zone, where the TPU is created. 
 const 
  
 zone 
  
 = 
  
 'europe-west4-a' 
 ; 
 async 
  
 function 
  
 callStartTpuVM 
 () 
  
 { 
  
 const 
  
 request 
  
 = 
  
 { 
  
 name 
 : 
  
 `projects/ 
 ${ 
 projectId 
 } 
 /locations/ 
 ${ 
 zone 
 } 
 /nodes/ 
 ${ 
 nodeName 
 } 
 ` 
 , 
  
 }; 
  
 const 
  
 [ 
 operation 
 ] 
  
 = 
  
 await 
  
 tpuClient 
 . 
 startNode 
 ( 
 request 
 ); 
  
 // Wait for the operation to complete. 
  
 const 
  
 [ 
 response 
 ] 
  
 = 
  
 await 
  
 operation 
 . 
 promise 
 (); 
  
 console 
 . 
 log 
 ( 
 `Node: 
 ${ 
 nodeName 
 } 
 started.` 
 ); 
  
 return 
  
 response 
 ; 
 } 
 return 
  
 await 
  
 callStartTpuVM 
 (); 
 

Python

To authenticate to Cloud TPU, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  from 
  
 google.cloud 
  
 import 
 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

 $  
 
gcloud  
compute  
tpus  
tpu-vm  
delete  
 tpu-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

  1. In the Google Cloud console, go to the TPUspage:

    Go to TPUs

  2. Select the checkbox next to your Cloud TPU.

  3. 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 .

  import 
  
 com.google.api.gax.longrunning. OperationTimedPollAlgorithm 
 
 ; 
 import 
  
 com.google.api.gax.retrying. RetrySettings 
 
 ; 
 import 
  
 com.google.cloud.tpu.v2. DeleteNodeRequest 
 
 ; 
 import 
  
 com.google.cloud.tpu.v2. NodeName 
 
 ; 
 import 
  
 com.google.cloud.tpu.v2. TpuClient 
 
 ; 
 import 
  
 com.google.cloud.tpu.v2. TpuSettings 
 
 ; 
 import 
  
 java.io.IOException 
 ; 
 import 
  
 java.util.concurrent.ExecutionException 
 ; 
 import 
  
 org.threeten.bp.Duration 
 ; 
 public 
  
 class 
 DeleteTpuVm 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 throws 
  
 IOException 
 , 
  
 ExecutionException 
 , 
  
 InterruptedException 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 // Project ID or project number of the Google Cloud project you want to create a node. 
  
 String 
  
 projectId 
  
 = 
  
 "YOUR_PROJECT_ID" 
 ; 
  
 // The zone in which to create the TPU. 
  
 // For more information about supported TPU types for specific zones, 
  
 // see https://cloud.google.com/tpu/docs/regions-zones 
  
 String 
  
 zone 
  
 = 
  
 "europe-west4-a" 
 ; 
  
 // The name for your TPU. 
  
 String 
  
 nodeName 
  
 = 
  
 "YOUR_TPU_NAME" 
 ; 
  
 deleteTpuVm 
 ( 
 projectId 
 , 
  
 zone 
 , 
  
 nodeName 
 ); 
  
 } 
  
 // Deletes a TPU VM with the specified name in the given project and zone. 
  
 public 
  
 static 
  
 void 
  
 deleteTpuVm 
 ( 
 String 
  
 projectId 
 , 
  
 String 
  
 zone 
 , 
  
 String 
  
 nodeName 
 ) 
  
 throws 
  
 IOException 
 , 
  
 ExecutionException 
 , 
  
 InterruptedException 
  
 { 
  
 // With these settings the client library handles the Operation's polling mechanism 
  
 // and prevent CancellationException error 
  
  TpuSettings 
 
 . 
 Builder 
  
 clientSettings 
  
 = 
  
  TpuSettings 
 
 . 
 newBuilder 
 (); 
  
 clientSettings 
  
 . 
 deleteNodeOperationSettings 
 () 
  
 . 
 setPollingAlgorithm 
 ( 
  
  OperationTimedPollAlgorithm 
 
 . 
 create 
 ( 
  
  RetrySettings 
 
 . 
 newBuilder 
 () 
  
 . 
  setInitialRetryDelay 
 
 ( 
 Duration 
 . 
 ofMillis 
 ( 
 5000L 
 )) 
  
 . 
  setRetryDelayMultiplier 
 
 ( 
 1.5 
 ) 
  
 . 
  setMaxRetryDelay 
 
 ( 
 Duration 
 . 
 ofMillis 
 ( 
 45000L 
 )) 
  
 . 
  setInitialRpcTimeout 
 
 ( 
 Duration 
 . 
 ZERO 
 ) 
  
 . 
  setRpcTimeoutMultiplier 
 
 ( 
 1.0 
 ) 
  
 . 
  setMaxRpcTimeout 
 
 ( 
 Duration 
 . 
 ZERO 
 ) 
  
 . 
  setTotalTimeout 
 
 ( 
 Duration 
 . 
 ofHours 
 ( 
 24L 
 )) 
  
 . 
 build 
 ())); 
  
 // Initialize client that will be used to send requests. This client only needs to be created 
  
 // once, and can be reused for multiple requests. 
  
 try 
  
 ( 
  TpuClient 
 
  
 tpuClient 
  
 = 
  
  TpuClient 
 
 . 
 create 
 ( 
 clientSettings 
 . 
 build 
 ())) 
  
 { 
  
 String 
  
 name 
  
 = 
  
  NodeName 
 
 . 
 of 
 ( 
 projectId 
 , 
  
 zone 
 , 
  
 nodeName 
 ). 
 toString 
 (); 
  
  DeleteNodeRequest 
 
  
 request 
  
 = 
  
  DeleteNodeRequest 
 
 . 
 newBuilder 
 (). 
 setName 
 ( 
 name 
 ). 
 build 
 (); 
  
 tpuClient 
 . 
  deleteNodeAsync 
 
 ( 
 request 
 ). 
  get 
 
 (); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "TPU VM deleted" 
 ); 
  
 } 
  
 } 
 } 
 

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. 
 const 
  
 projectId 
  
 = 
  
 await 
  
 tpuClient 
 . 
 getProjectId 
 (); 
 // The name of TPU to delete. 
 const 
  
 nodeName 
  
 = 
  
 'node-name-1' 
 ; 
 // The zone, where the TPU is created. 
 const 
  
 zone 
  
 = 
  
 'europe-west4-a' 
 ; 
 async 
  
 function 
  
 callDeleteTpuVM 
 () 
  
 { 
  
 const 
  
 request 
  
 = 
  
 { 
  
 name 
 : 
  
 `projects/ 
 ${ 
 projectId 
 } 
 /locations/ 
 ${ 
 zone 
 } 
 /nodes/ 
 ${ 
 nodeName 
 } 
 ` 
 , 
  
 }; 
  
 const 
  
 [ 
 operation 
 ] 
  
 = 
  
 await 
  
 tpuClient 
 . 
 deleteNode 
 ( 
 request 
 ); 
  
 // Wait for the delete operation to complete. 
  
 const 
  
 [ 
 response 
 ] 
  
 = 
  
 await 
  
 operation 
 . 
 promise 
 (); 
  
 console 
 . 
 log 
 ( 
 `Node: 
 ${ 
 nodeName 
 } 
 deleted.` 
 ); 
  
 return 
  
 response 
 ; 
 } 
 return 
  
 await 
  
 callDeleteTpuVM 
 (); 
 

Python

To authenticate to Cloud TPU, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  from 
  
 google.cloud 
  
 import 
 tpu_v2 
 # TODO(developer): Update and un-comment below lines 
 # project_id = "your-project-id" 
 # zone = "us-central1-b" 
 # tpu_name = "tpu-name" 
 client 
 = 
 tpu_v2 
 . 
 TpuClient 
 () 
 try 
 : 
 client 
 . 
  delete_node 
 
 ( 
 name 
 = 
 f 
 "projects/ 
 { 
 project_id 
 } 
 /locations/ 
 { 
 zone 
 } 
 /nodes/ 
 { 
 tpu_name 
 } 
 " 
 ) 
 print 
 ( 
 "The TPU node was deleted." 
 ) 
 except 
 Exception 
 as 
 e 
 : 
 print 
 ( 
 e 
 ) 
 
Create a Mobile Website
View Site in Mobile | Classic
Share by: