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_queued_resources_create]
20
+ import com .google .cloud .tpu .v2alpha1 .CreateQueuedResourceRequest ;
21
+ import com .google .cloud .tpu .v2alpha1 .Node ;
22
+ import com .google .cloud .tpu .v2alpha1 .QueuedResource ;
23
+ import com .google .cloud .tpu .v2alpha1 .TpuClient ;
24
+ import java .io .IOException ;
25
+ import java .util .concurrent .ExecutionException ;
26
+ import java .util .concurrent .TimeUnit ;
27
+ import java .util .concurrent .TimeoutException ;
28
+
29
+ public class CreateQueuedResource {
30
+ public static void main (String [] args )
31
+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
32
+ // TODO(developer): Replace these variables before running the sample.
33
+ // Project ID or project number of the Google Cloud project you want to create a node.
34
+ String projectId = "YOUR_PROJECT_ID" ;
35
+ // The zone in which to create the TPU.
36
+ // For more information about supported TPU types for specific zones,
37
+ // see https://cloud.google.com/tpu/docs/regions-zones
38
+ String zone = "us-central1-f" ;
39
+ // The name for your TPU.
40
+ String nodeName = "YOUR_NODE_ID" ;
41
+ // The accelerator type that specifies the version and size of the Cloud TPU you want to create.
42
+ // For more information about supported accelerator types for each TPU version,
43
+ // see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
44
+ String tpuType = "v2-8" ;
45
+ // Software version that specifies the version of the TPU runtime to install.
46
+ // For more information see https://cloud.google.com/tpu/docs/runtimes
47
+ String tpuSoftwareVersion = "tpu-vm-tf-2.14.1" ;
48
+ // The name for your Queued Resource.
49
+ String queuedResourceId = "QUEUED_RESOURCE_ID" ;
50
+
51
+ createQueuedResource (
52
+ projectId , zone , queuedResourceId , nodeName , tpuType , tpuSoftwareVersion );
53
+ }
54
+
55
+ // Creates a Queued Resource
56
+ public static QueuedResource createQueuedResource (String projectId , String zone ,
57
+ String queuedResourceId , String nodeName , String tpuType , String tpuSoftwareVersion )
58
+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
59
+ String resource = String .format ("projects/%s/locations/%s/queuedResources/%s" ,
60
+ projectId , zone , queuedResourceId );
61
+ // Initialize client that will be used to send requests. This client only needs to be created
62
+ // once, and can be reused for multiple requests.
63
+ try (TpuClient tpuClient = TpuClient .create ()) {
64
+ String parent = String .format ("projects/%s/locations/%s" , projectId , zone );
65
+ Node node =
66
+ Node .newBuilder ()
67
+ .setName (nodeName )
68
+ .setAcceleratorType (tpuType )
69
+ .setRuntimeVersion (tpuSoftwareVersion )
70
+ .setQueuedResource (resource )
71
+ .build ();
72
+
73
+ QueuedResource queuedResource =
74
+ QueuedResource .newBuilder ()
75
+ .setName (queuedResourceId )
76
+ .setTpu (
77
+ QueuedResource .Tpu .newBuilder ()
78
+ .addNodeSpec (
79
+ QueuedResource .Tpu .NodeSpec .newBuilder ()
80
+ .setParent (parent )
81
+ .setNode (node )
82
+ .setNodeId (nodeName )
83
+ .build ())
84
+ .build ())
85
+ .build ();
86
+
87
+ CreateQueuedResourceRequest request =
88
+ CreateQueuedResourceRequest .newBuilder ()
89
+ .setParent (parent )
90
+ .setQueuedResourceId (queuedResourceId )
91
+ .setQueuedResource (queuedResource )
92
+ .build ();
93
+
94
+ return tpuClient .createQueuedResourceAsync (request ).get (1 , TimeUnit .MINUTES );
95
+ }
96
+ }
97
+ }
98
+ //[END tpu_queued_resources_create]
0 commit comments