1+ /*
2+ * Copyright 2024 Google LLC
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package tpu ;
18+
19+ //[START tpu_vm_create_startup_script]
20+ import com .google .api .gax .longrunning .OperationTimedPollAlgorithm ;
21+ import com .google .api .gax .retrying .RetrySettings ;
22+ import com .google .cloud .tpu .v2 .CreateNodeRequest ;
23+ import com .google .cloud .tpu .v2 .Node ;
24+ import com .google .cloud .tpu .v2 .TpuClient ;
25+ import com .google .cloud .tpu .v2 .TpuSettings ;
26+ import java .io .IOException ;
27+ import java .util .HashMap ;
28+ import java .util .Map ;
29+ import java .util .concurrent .ExecutionException ;
30+ import org .threeten .bp .Duration ;
31+
32+ public class CreateTpuVmWithStartupScript {
33+ public static void main (String [] args )
34+ throws IOException , ExecutionException , InterruptedException {
35+ // TODO(developer): Replace these variables before running the sample.
36+ // Project ID or project number of the Google Cloud project you want to create a node.
37+ String projectId = "YOUR_PROJECT_ID" ;
38+ // The zone in which to create the TPU.
39+ // For more information about supported TPU types for specific zones,
40+ // see https://cloud.google.com/tpu/docs/regions-zones
41+ String zone = "europe-west4-a" ;
42+ // The name for your TPU.
43+ String nodeName = "YOUR_TPY_NAME" ;
44+ // The accelerator type that specifies the version and size of the Cloud TPU you want to create.
45+ // For more information about supported accelerator types for each TPU version,
46+ // see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
47+ String acceleratorType = "v2-8" ;
48+ // Software version that specifies the version of the TPU runtime to install.
49+ // For more information, see https://cloud.google.com/tpu/docs/runtimes
50+ String tpuSoftwareVersion = "tpu-vm-tf-2.14.1" ;
51+
52+ createTpuVmWithStartupScript (projectId , zone , nodeName , acceleratorType , tpuSoftwareVersion );
53+ }
54+
55+ // Create a TPU VM with a startup script.
56+ public static Node createTpuVmWithStartupScript (String projectId , String zone ,
57+ String nodeName , String acceleratorType , String tpuSoftwareVersion )
58+ throws IOException , ExecutionException , InterruptedException {
59+ // With these settings the client library handles the Operation's polling mechanism
60+ // and prevent CancellationException error
61+ TpuSettings .Builder clientSettings =
62+ TpuSettings .newBuilder ();
63+ clientSettings
64+ .createNodeOperationSettings ()
65+ .setPollingAlgorithm (
66+ OperationTimedPollAlgorithm .create (
67+ RetrySettings .newBuilder ()
68+ .setInitialRetryDelay (Duration .ofMillis (5000L ))
69+ .setRetryDelayMultiplier (1.5 )
70+ .setMaxRetryDelay (Duration .ofMillis (45000L ))
71+ .setInitialRpcTimeout (Duration .ZERO )
72+ .setRpcTimeoutMultiplier (1.0 )
73+ .setMaxRpcTimeout (Duration .ZERO )
74+ .setTotalTimeout (Duration .ofHours (24L ))
75+ .build ()));
76+
77+ // Initialize client that will be used to send requests. This client only needs to be created
78+ // once, and can be reused for multiple requests.
79+ try (TpuClient tpuClient = TpuClient .create (clientSettings .build ())) {
80+ String parent = String .format ("projects/%s/locations/%s" , projectId , zone );
81+
82+ // Create metadata map
83+ Map <String , String > metadata = new HashMap <>();
84+ metadata .put ("startup-script" , "your-script-here" ); // Script content here
85+
86+ Node tpuVm =
87+ Node .newBuilder ()
88+ .setName (nodeName )
89+ .setAcceleratorType (acceleratorType )
90+ .setRuntimeVersion (tpuSoftwareVersion )
91+ .putAllLabels (metadata )
92+ .build ();
93+
94+ CreateNodeRequest request =
95+ CreateNodeRequest .newBuilder ()
96+ .setParent (parent )
97+ .setNodeId (nodeName )
98+ .setNode (tpuVm )
99+ .build ();
100+
101+ Node response = tpuClient .createNodeAsync (request ).get ();
102+
103+ System .out .printf ("TPU VM created: %s\n " , response .getName ());
104+ return response ;
105+ }
106+ }
107+ }
108+ //[END tpu_vm_create_startup_script]
0 commit comments