Skip to content

Commit 684a880

Browse files
Fixed samples and tests
1 parent 3035ff6 commit 684a880

File tree

6 files changed

+153
-133
lines changed

6 files changed

+153
-133
lines changed

tpu/src/main/java/tpu/CreateQueuedResourceWithNetwork.java

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
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+
117
package tpu;
218

319
//[START tpu_queued_resources_network]
4-
520
import com.google.api.gax.retrying.RetrySettings;
621
import com.google.cloud.tpu.v2alpha1.CreateQueuedResourceRequest;
722
import com.google.cloud.tpu.v2alpha1.NetworkConfig;
@@ -14,22 +29,17 @@
1429
import org.threeten.bp.Duration;
1530

1631
public class CreateQueuedResourceWithNetwork {
17-
18-
private final TpuClient tpuClient;
19-
public CreateQueuedResourceWithNetwork(TpuClient tpuClient) {
20-
this.tpuClient = tpuClient;
21-
}
2232
public static void main(String[] args)
2333
throws IOException, ExecutionException, InterruptedException {
2434
// TODO(developer): Replace these variables before running the sample.
2535
// Project ID or project number of the Google Cloud project you want to create a node.
26-
String projectId = "tyaho-softserve-project";//"your-project";
36+
String projectId = "YOUR_PROJECT_ID";
2737
// The zone in which to create the TPU.
2838
// For more information about supported TPU types for specific zones,
2939
// see https://cloud.google.com/tpu/docs/regions-zones
3040
String zone = "europe-west4-a";
3141
// The name for your TPU.
32-
String nodeName = "your-node-id";
42+
String nodeName = "YOUR_TPY_NAME";
3343
// The accelerator type that specifies the version and size of the Cloud TPU you want to create.
3444
// For more information about supported accelerator types for each TPU version,
3545
// see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
@@ -38,22 +48,24 @@ public static void main(String[] args)
3848
// For more information see https://cloud.google.com/tpu/docs/runtimes
3949
String tpuSoftwareVersion = "tpu-vm-tf-2.14.1";
4050
// The name for your Queued Resource.
41-
String queuedResourceName = "your-queued-resource-id";
42-
TpuClient tpuClient = TpuClient.create();
43-
CreateQueuedResourceWithNetwork creator = new CreateQueuedResourceWithNetwork(tpuClient);
44-
45-
creator.createQueuedResourceWithNetwork(projectId, zone, queuedResourceName, nodeName, tpuType, tpuSoftwareVersion);
51+
String queuedResourceId = "QUEUED_RESOURCE_ID";
52+
// The name of the network you want the node to connect to.
53+
// The network should be assigned to your project.
54+
String networkName = "YOUR_COMPUTE_TPU_NETWORK";
4655

56+
createQueuedResourceWithNetwork(projectId, zone, queuedResourceId, nodeName,
57+
tpuType, tpuSoftwareVersion, networkName);
4758
}
4859

4960
// Creates a Queued Resource with network configuration.
50-
public static void createQueuedResourceWithNetwork(
61+
public static QueuedResource createQueuedResourceWithNetwork(
5162
String projectId,
5263
String zone,
53-
String queuedResourceName,
64+
String queuedResourceId,
5465
String nodeName,
5566
String tpuType,
56-
String tpuSoftwareVersion)
67+
String tpuSoftwareVersion,
68+
String networkName)
5769
throws IOException, ExecutionException, InterruptedException {
5870
// With these settings the client library handles the Operation's polling mechanism
5971
// and prevent CancellationException error
@@ -75,10 +87,8 @@ public static void createQueuedResourceWithNetwork(
7587
try (TpuClient tpuClient = TpuClient.create(clientSettings.build())) {
7688
String parent = String.format("projects/%s/locations/%s", projectId, zone);
7789
String region = zone.substring(0, zone.length() - 2);
78-
// The name of the network you want the node to connect to. The network should be assigned to your project.
79-
String networkName = "compute-tpu-network";
80-
// Specify the network and subnetwork that you want to connect your TPU to.
8190

91+
// Specify the network and subnetwork that you want to connect your TPU to.
8292
NetworkConfig networkConfig =
8393
NetworkConfig.newBuilder()
8494
.setEnableExternalIps(true)
@@ -98,13 +108,13 @@ public static void createQueuedResourceWithNetwork(
98108
.setQueuedResource(
99109
String.format(
100110
"projects/%s/locations/%s/queuedResources/%s",
101-
projectId, zone, queuedResourceName))
111+
projectId, zone, queuedResourceId))
102112
.build();
103113

104114
// Create queued resource
105115
QueuedResource queuedResource =
106116
QueuedResource.newBuilder()
107-
.setName(queuedResourceName)
117+
.setName(queuedResourceId)
108118
.setTpu(
109119
QueuedResource.Tpu.newBuilder()
110120
.addNodeSpec(
@@ -120,15 +130,14 @@ public static void createQueuedResourceWithNetwork(
120130
CreateQueuedResourceRequest.newBuilder()
121131
.setParent(parent)
122132
.setQueuedResource(queuedResource)
123-
.setQueuedResourceId(queuedResourceName)
133+
.setQueuedResourceId(queuedResourceId)
124134
.build();
125135

126136
QueuedResource response = tpuClient.createQueuedResourceAsync(request).get();
127137
// You can wait until TPU Node is READY,
128138
// and check its status using getTpuVm() from "tpu_vm_get" sample.
129-
System.out.printf(
130-
"Queued resource %s with specified network created.\n", queuedResourceName);
131-
System.out.println(response);
139+
System.out.println("Queued Resource created: " + queuedResourceId);
140+
return response;
132141
}
133142
}
134143
}

tpu/src/main/java/tpu/CreateQueuedResourceWithStartupScript.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
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+
117
package tpu;
218

319
import com.google.api.gax.retrying.RetrySettings;
@@ -6,25 +22,24 @@
622
import com.google.cloud.tpu.v2alpha1.QueuedResource;
723
import com.google.cloud.tpu.v2alpha1.TpuClient;
824
import com.google.cloud.tpu.v2alpha1.TpuSettings;
9-
import org.threeten.bp.Duration;
10-
1125
import java.io.IOException;
1226
import java.util.HashMap;
1327
import java.util.Map;
1428
import java.util.concurrent.ExecutionException;
29+
import org.threeten.bp.Duration;
1530

1631
public class CreateQueuedResourceWithStartupScript {
1732
public static void main(String[] args)
1833
throws IOException, ExecutionException, InterruptedException {
1934
// TODO(developer): Replace these variables before running the sample.
2035
// Project ID or project number of the Google Cloud project you want to create a node.
21-
String projectId = "tyaho-softserve-project";//"your-project";
36+
String projectId = "YOUR_PROJECT_ID";
2237
// The zone in which to create the TPU.
2338
// For more information about supported TPU types for specific zones,
2439
// see https://cloud.google.com/tpu/docs/regions-zones
2540
String zone = "europe-west4-a";
2641
// The name for your TPU.
27-
String nodeName = "your-node-id";
42+
String nodeName = "YOUR_TPY_NAME";
2843
// The accelerator type that specifies the version and size of the Cloud TPU you want to create.
2944
// For more information about supported accelerator types for each TPU version,
3045
// see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
@@ -33,14 +48,14 @@ public static void main(String[] args)
3348
// For more information see https://cloud.google.com/tpu/docs/runtimes
3449
String tpuSoftwareVersion = "tpu-vm-tf-2.14.1";
3550
// The name for your Queued Resource.
36-
String queuedResourceId = "your-queued-resource-id";
51+
String queuedResourceId = "QUEUED_RESOURCE_ID";
3752

3853
createQueuedResource(
3954
projectId, zone, queuedResourceId, nodeName, tpuType, tpuSoftwareVersion);
4055
}
4156

4257
// Creates a Queued Resource
43-
public static void createQueuedResource(
58+
public static QueuedResource createQueuedResource(
4459
String projectId,
4560
String zone,
4661
String queuedResourceId,
@@ -110,6 +125,7 @@ public static void createQueuedResource(
110125
// You can wait until TPU Node is READY,
111126
// and check its status using getTpuVm() from "tpu_vm_get" sample.
112127
System.out.printf("Queued Resource created: %s\n", queuedResourceId);
128+
return response;
113129
}
114130
}
115131
}

tpu/src/main/java/tpu/CreateSpotQueuedResource.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
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+
117
package tpu;
218

319
import com.google.api.gax.retrying.RetrySettings;
@@ -7,19 +23,16 @@
723
import com.google.cloud.tpu.v2alpha1.SchedulingConfig;
824
import com.google.cloud.tpu.v2alpha1.TpuClient;
925
import com.google.cloud.tpu.v2alpha1.TpuSettings;
10-
import org.threeten.bp.Duration;
11-
1226
import java.io.IOException;
13-
import java.util.HashMap;
14-
import java.util.Map;
1527
import java.util.concurrent.ExecutionException;
28+
import org.threeten.bp.Duration;
1629

1730
public class CreateSpotQueuedResource {
1831
public static void main(String[] args)
1932
throws IOException, ExecutionException, InterruptedException {
2033
// TODO(developer): Replace these variables before running the sample.
2134
// Project ID or project number of the Google Cloud project you want to create a node.
22-
String projectId = "tyaho-softserve-project";//"your-project";
35+
String projectId = "YOUR_PROJECT_ID";
2336
// The zone in which to create the TPU.
2437
// For more information about supported TPU types for specific zones,
2538
// see https://cloud.google.com/tpu/docs/regions-zones
@@ -41,7 +54,7 @@ public static void main(String[] args)
4154
}
4255

4356
// Creates a Queued Resource
44-
public static void createQueuedResource(
57+
public static QueuedResource createQueuedResource(
4558
String projectId,
4659
String zone,
4760
String queuedResourceId,
@@ -109,6 +122,7 @@ public static void createQueuedResource(
109122
// You can wait until TPU Node is READY,
110123
// and check its status using getTpuVm() from "tpu_vm_get" sample.
111124
System.out.printf("Queued Resource created: %s\n", queuedResourceId);
125+
return response;
112126
}
113127
}
114128
}

0 commit comments

Comments
 (0)