Skip to content

Commit 0daddad

Browse files
author
Joanna Grycz
committed
feat: tpu_queued_resources_create
1 parent e587ab9 commit 0daddad

File tree

9 files changed

+383
-0
lines changed

9 files changed

+383
-0
lines changed

.github/workflows/tpu.yaml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
name: tpu
16+
on:
17+
push:
18+
branches:
19+
- main
20+
paths:
21+
- 'tpu/**'
22+
- '.github/workflows/tpu.yaml'
23+
- '.github/workflows/test.yaml'
24+
pull_request:
25+
types:
26+
- opened
27+
- reopened
28+
- synchronize
29+
- labeled
30+
paths:
31+
- 'tpu/**'
32+
- '.github/workflows/tpu.yaml'
33+
- '.github/workflows/test.yaml'
34+
schedule:
35+
- cron: '0 0 * * 0'
36+
jobs:
37+
test:
38+
# Ref: https://github.com/google-github-actions/auth#usage
39+
permissions:
40+
contents: 'read'
41+
id-token: 'write'
42+
if: github.event.action != 'labeled' || github.event.label.name == 'actions:force-run'
43+
uses: ./.github/workflows/test.yaml
44+
with:
45+
name: 'tpu'
46+
path: 'tpu'
47+
flakybot:
48+
# Ref: https://github.com/google-github-actions/auth#usage
49+
permissions:
50+
contents: 'read'
51+
id-token: 'write'
52+
if: github.event_name == 'schedule' && always() # always() submits logs even if tests fail
53+
uses: ./.github/workflows/flakybot.yaml
54+
needs: [test]

.github/workflows/utils/workflows.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
"speech",
9191
"talent",
9292
"texttospeech",
93+
"tpu",
9394
"translate",
9495
"video-intelligence",
9596
"vision/productSearch",

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ recaptcha_enterprise @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/nodejs-
2424
recaptcha_enterprise/demosite @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/recaptcha-customer-obsession-reviewers @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
2525
secret-manager @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/cloud-secrets-team
2626
service-directory @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
27+
tpu @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
2728
webrisk @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
2829

2930
# SoDa teams

tpu/package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "nodejs-docs-samples-tpu",
3+
"license": "Apache-2.0",
4+
"author": "Google Inc.",
5+
"engines": {
6+
"node": ">=16.0.0"
7+
},
8+
"repository": "googleapis/nodejs-tpu",
9+
"private": true,
10+
"files": [
11+
"*.js"
12+
],
13+
"scripts": {
14+
"test": "c8 mocha -p -j 2 test --timeout 1200000"
15+
},
16+
"dependencies": {
17+
"@google-cloud/tpu": "^3.5.0"
18+
},
19+
"devDependencies": {
20+
"c8": "^10.0.0",
21+
"mocha": "^10.0.0"
22+
}
23+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
* https://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+
'use strict';
18+
19+
async function main(
20+
nodeName,
21+
queuedResourceName,
22+
zone,
23+
tpuType,
24+
tpuSoftwareVersion
25+
) {
26+
// [START tpu_queued_resources_create]
27+
// Import the TPU library
28+
const {TpuClient} = require('@google-cloud/tpu').v2alpha1;
29+
const {Node, NetworkConfig, QueuedResource} =
30+
require('@google-cloud/tpu').protos.google.cloud.tpu.v2alpha1;
31+
32+
// Instantiate a tpuClient
33+
const tpuClient = new TpuClient();
34+
35+
/**
36+
* TODO(developer): Update/uncomment these variables before running the sample.
37+
*/
38+
// Project ID or project number of the Google Cloud project, where you want to create queued resource.
39+
const projectId = await tpuClient.getProjectId();
40+
41+
// The name of the network you want the node to connect to. The network should be assigned to your project.
42+
const networkName = 'compute-tpu-network';
43+
44+
// The region of the network, that you want the node to connect to.
45+
const region = 'europe-west4';
46+
47+
// The name for your queued resource.
48+
// queuedResourceName = 'queued-resource-1';
49+
50+
// The name for your node.
51+
// nodeName = 'node-name-1';
52+
53+
// The zone in which to create the node.
54+
// For more information about supported TPU types for specific zones,
55+
// see https://cloud.google.com/tpu/docs/regions-zones
56+
// zone = 'europe-west4-a';
57+
58+
// The accelerator type that specifies the version and size of the node you want to create.
59+
// For more information about supported accelerator types for each TPU version,
60+
// see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
61+
// tpuType = 'v2-8';
62+
63+
// Software version that specifies the version of the node runtime to install. For more information,
64+
// see https://cloud.google.com/tpu/docs/runtimes
65+
// tpuSoftwareVersion = 'tpu-vm-tf-2.14.1';
66+
67+
async function callCreateQueuedResource() {
68+
// Create a node
69+
const node = new Node({
70+
name: nodeName,
71+
zone,
72+
acceleratorType: tpuType,
73+
runtimeVersion: tpuSoftwareVersion,
74+
// Define network
75+
networkConfig: new NetworkConfig({
76+
enableExternalIps: true,
77+
network: `projects/${projectId}/global/networks/${networkName}`,
78+
subnetwork: `projects/${projectId}/regions/${region}/subnetworks/${networkName}`,
79+
}),
80+
queuedResource: `projects/${projectId}/locations/${zone}/queuedResources/${queuedResourceName}`,
81+
});
82+
83+
// Define parent for requests
84+
const parent = `projects/${projectId}/locations/${zone}`;
85+
86+
// Create queued resource
87+
const queuedResource = new QueuedResource({
88+
name: queuedResourceName,
89+
tpu: {
90+
nodeSpec: [
91+
{
92+
parent,
93+
node,
94+
nodeId: nodeName,
95+
},
96+
],
97+
},
98+
// TODO(developer): Uncomment next line if you want to specify reservation.
99+
// reservationName: 'reservation-name'
100+
});
101+
102+
const request = {
103+
parent: `projects/${projectId}/locations/${zone}`,
104+
queuedResource,
105+
queuedResourceId: queuedResourceName,
106+
};
107+
108+
const [operation] = await tpuClient.createQueuedResource(request);
109+
110+
// Wait for the create operation to complete.
111+
const [response] = await operation.promise();
112+
113+
console.log(JSON.stringify(response));
114+
console.log(`Queued resource ${queuedResourceName} created.`);
115+
}
116+
await callCreateQueuedResource();
117+
// [END tpu_queued_resources_create]
118+
}
119+
120+
main(...process.argv.slice(2)).catch(err => {
121+
console.error(err);
122+
process.exitCode = 1;
123+
});
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
* https://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+
'use strict';
18+
19+
async function main(queuedResourceName, zone) {
20+
// [START tpu_queued_resources_delete]
21+
// Import the TPU library
22+
const {TpuClient} = require('@google-cloud/tpu').v2alpha1;
23+
24+
// Instantiate a tpuClient
25+
const tpuClient = new TpuClient();
26+
27+
/**
28+
* TODO(developer): Update/uncomment these variables before running the sample.
29+
*/
30+
// Project ID or project number of the Google Cloud project, where you want to delete node.
31+
const projectId = await tpuClient.getProjectId();
32+
33+
// The name of queued resource.
34+
// queuedResourceName = 'queued-resource-1';
35+
36+
// The zone of your queued resource.
37+
// zone = 'europe-west4-a';
38+
39+
async function callDeleteQueuedResource() {
40+
const request = {
41+
name: `projects/${projectId}/locations/${zone}/queuedResources/${queuedResourceName}`,
42+
};
43+
44+
const [operation] = await tpuClient.deleteQueuedResource(request);
45+
46+
// Wait for the delete operation to complete.
47+
const [response] = await operation.promise();
48+
49+
console.log(JSON.stringify(response));
50+
console.log(`Queued resource ${queuedResourceName} deleted.`);
51+
}
52+
await callDeleteQueuedResource();
53+
// [END tpu_queued_resources_delete]
54+
}
55+
56+
main(...process.argv.slice(2)).catch(err => {
57+
console.error(err);
58+
process.exitCode = 1;
59+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
* https://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+
'use strict';
18+
19+
async function main(queuedResourceName, zone) {
20+
// [START tpu_queued_resources_delete_force]
21+
// Import the TPU library
22+
const {TpuClient} = require('@google-cloud/tpu').v2alpha1;
23+
24+
// Instantiate a tpuClient
25+
const tpuClient = new TpuClient();
26+
27+
/**
28+
* TODO(developer): Update/uncomment these variables before running the sample.
29+
*/
30+
// Project ID or project number of the Google Cloud project, where you want to delete node.
31+
const projectId = await tpuClient.getProjectId();
32+
33+
// The name of queued resource.
34+
// queuedResourceName = 'queued-resource-1';
35+
36+
// The zone of your queued resource.
37+
// zone = 'europe-west4-a';
38+
39+
async function callForceDeleteQueuedResource() {
40+
const request = {
41+
name: `projects/${projectId}/locations/${zone}/queuedResources/${queuedResourceName}`,
42+
force: true,
43+
};
44+
45+
const [operation] = await tpuClient.deleteQueuedResource(request);
46+
47+
// Wait for the delete operation to complete.
48+
const [response] = await operation.promise();
49+
50+
console.log(JSON.stringify(response));
51+
console.log(`Queued resource ${queuedResourceName} deletion forced.`);
52+
}
53+
await callForceDeleteQueuedResource();
54+
// [END tpu_queued_resources_delete_force]
55+
}
56+
57+
main(...process.argv.slice(2)).catch(err => {
58+
console.error(err);
59+
process.exitCode = 1;
60+
});

tpu/test/.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
env:
3+
mocha: true

0 commit comments

Comments
 (0)