Skip to content

Commit 3868b7a

Browse files
Joanna Grycziennae
authored andcommitted
feat: tpu_vm_delete
1 parent 8b32197 commit 3868b7a

File tree

4 files changed

+87
-24
lines changed

4 files changed

+87
-24
lines changed

compute/test/tpu.test.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,19 @@
1818

1919
const path = require('path');
2020
const assert = require('node:assert/strict');
21-
const {describe, xit} = require('mocha');
21+
const {describe, it} = require('mocha');
2222
const cp = require('child_process');
23-
// const {TpuClient} = require('@google-cloud/tpu').v2;
2423

2524
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2625
const cwd = path.join(__dirname, '..');
2726

2827
describe('Compute tpu', async () => {
2928
const nodeName = `node-name-2a2b3c${Math.floor(Math.random() * 1000 + 1)}`;
30-
const zone = 'us-central1-a';
31-
const tpuType = 'v2-32';
32-
const tpuSoftwareVersion = 'tpu-vm-base';
33-
// const tpuClient = new TpuClient();
34-
// let projectId;
29+
const zone = 'us-central1-b';
30+
const tpuType = 'v2-8';
31+
const tpuSoftwareVersion = 'tpu-vm-tf-2.14.1';
3532

36-
// before(async () => {
37-
// projectId = await tpuClient.getProjectId();
38-
// });
39-
40-
xit('should create a new tpu node', () => {
33+
it('should create a new tpu node', () => {
4134
const response = execSync(
4235
`node ./tpu/vmCreate.js ${nodeName} ${zone} ${tpuType} ${tpuSoftwareVersion}`,
4336
{
@@ -48,19 +41,29 @@ describe('Compute tpu', async () => {
4841
assert(response.includes(`TPU VM: ${nodeName} created.`));
4942
});
5043

51-
xit('should return tpu node', () => {
44+
it('should return tpu node', () => {
5245
const response = execSync(`node ./tpu/vmGet.js ${nodeName} ${zone}`, {
5346
cwd,
5447
});
5548

5649
assert(response.includes(`Node: ${nodeName} retrived.`));
5750
});
5851

59-
xit('should return list of tpu nodes', () => {
60-
const response = execSync(`node ./tpu/vmList.js ${zone}`, {
52+
it('should return list of tpu nodes', () => {
53+
const response = JSON.parse(
54+
execSync(`node ./tpu/vmList.js ${zone}`, {
55+
cwd,
56+
})
57+
);
58+
59+
assert(Array.isArray(response));
60+
});
61+
62+
it('should delete tpu node', () => {
63+
const response = execSync(`node ./tpu/vmDelete.js ${nodeName} ${zone}`, {
6164
cwd,
6265
});
6366

64-
assert(Array.isArray(response));
67+
assert(response.includes(`Node: ${nodeName} deleted.`));
6568
});
6669
});

compute/tpu/vmCreate.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ async function main(nodeName, zone, tpuType, tpuSoftwareVersion) {
3737
// The zone in which to create the TPU.
3838
// For more information about supported TPU types for specific zones,
3939
// see https://cloud.google.com/tpu/docs/regions-zones
40-
// zone = 'us-central1-a';
40+
// zone = 'us-central1-b';
4141

4242
// The accelerator type that specifies the version and size of the Cloud TPU you want to create.
4343
// For more information about supported accelerator types for each TPU version,
4444
// see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
45-
// tpuType = 'v2-32';
45+
// tpuType = 'v2-8';
4646

4747
// Software version that specifies the version of the TPU runtime to install. For more information,
4848
// see https://cloud.google.com/tpu/docs/runtimes
49-
// tpuSoftwareVersion = 'tpu-vm-base';
49+
// tpuSoftwareVersion = 'tpu-vm-tf-2.14.1';
5050

5151
async function callCreateTpuVM() {
5252
// Create a node
@@ -55,7 +55,7 @@ async function main(nodeName, zone, tpuType, tpuSoftwareVersion) {
5555
zone,
5656
apiVersion: tpu.Node.ApiVersion.V2,
5757
acceleratorType: tpuType,
58-
// Ensure that the tpuSoftwareVersion you're using (e.g., 'tpu-vm-base') is a valid and supported TensorFlow version for the selected tpuType and zone.
58+
// Ensure that the tpuSoftwareVersion you're using (e.g., 'tpu-vm-tf-2.14.1') is a valid and supported TensorFlow version for the selected tpuType and zone.
5959
// You can find a list of supported versions in the Cloud TPU documentation: https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions
6060
tensorflowVersion: tpuSoftwareVersion,
6161
networkConfig: tpu.NetworkConfig({enableExternalIps: true}),
@@ -66,12 +66,14 @@ async function main(nodeName, zone, tpuType, tpuSoftwareVersion) {
6666
const parent = `projects/${projectId}/locations/${zone}`;
6767
const request = {parent, node, nodeId: nodeName};
6868

69-
const [response] = await tpuClient.createNode(request);
69+
const [operation] = await tpuClient.createNode(request);
70+
71+
// Wait for the delete operation to complete.
72+
const [response] = await operation.promise();
7073

71-
console.log(`TPU VM: ${nodeName} created.`);
7274
console.log(JSON.stringify(response));
75+
console.log(`TPU VM: ${nodeName} created.`);
7376
}
74-
7577
await callCreateTpuVM();
7678
// [END tpu_vm_create]
7779
}

compute/tpu/vmDelete.js

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(nodeName, zone) {
20+
// [START tpu_vm_delete]
21+
// Import the TPU library
22+
const tpuLib = require('@google-cloud/tpu');
23+
24+
// Instantiate a tpuClient
25+
const tpuClient = new tpuLib.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 you want to delete a node.
31+
const projectId = await tpuClient.getProjectId();
32+
33+
// The name of TPU to delete.
34+
// nodeName = 'node-name-1';
35+
36+
// The zone, where the TPU is created.
37+
// zone = 'us-central1-a';
38+
39+
async function callDeleteTpuVM() {
40+
const request = {
41+
name: `projects/${projectId}/locations/${zone}/nodes/${nodeName}`,
42+
};
43+
44+
const [operation] = await tpuClient.deleteNode(request);
45+
46+
// Wait for the delete operation to complete.
47+
await operation.promise();
48+
49+
console.log(`Node: ${nodeName} deleted.`);
50+
}
51+
52+
await callDeleteTpuVM();
53+
// [END tpu_vm_delete]
54+
}
55+
56+
main(...process.argv.slice(2)).catch(err => {
57+
console.error(err);
58+
process.exitCode = 1;
59+
});

compute/tpu/vmList.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ async function main(zone) {
4040

4141
const [response] = await tpuClient.listNodes(request);
4242

43-
console.log('VM list retrived.');
4443
console.log(JSON.stringify(response));
4544
}
4645

0 commit comments

Comments
 (0)