Skip to content

Commit 54683f2

Browse files
author
Joanna Grycz
committed
feat: tpu_vm_create_startup_script
1 parent 18cbdf3 commit 54683f2

File tree

5 files changed

+173
-8
lines changed

5 files changed

+173
-8
lines changed

tpu/createStartupScriptVM.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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, tpuType, tpuSoftwareVersion) {
20+
// [START tpu_vm_create_startup_script]
21+
// Import the TPU library
22+
const {TpuClient} = require('@google-cloud/tpu').v2;
23+
const {Node, NetworkConfig} =
24+
require('@google-cloud/tpu').protos.google.cloud.tpu.v2;
25+
26+
// Instantiate a tpuClient
27+
const tpuClient = new TpuClient();
28+
29+
/**
30+
* TODO(developer): Update/uncomment these variables before running the sample.
31+
*/
32+
// Project ID or project number of the Google Cloud project you want to create a node.
33+
const projectId = await tpuClient.getProjectId();
34+
35+
// The name of the network you want the TPU node to connect to. The network should be assigned to your project.
36+
const networkName = 'compute-tpu-network';
37+
38+
// The region of the network, that you want the TPU node to connect to.
39+
const region = 'europe-west4';
40+
41+
// The name for your TPU.
42+
// nodeName = 'node-name-1';
43+
44+
// The zone in which to create the TPU.
45+
// For more information about supported TPU types for specific zones,
46+
// see https://cloud.google.com/tpu/docs/regions-zones
47+
// zone = 'europe-west4-a';
48+
49+
// The accelerator type that specifies the version and size of the Cloud TPU you want to create.
50+
// For more information about supported accelerator types for each TPU version,
51+
// see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
52+
// tpuType = 'v2-8';
53+
54+
// Software version that specifies the version of the TPU runtime to install. For more information,
55+
// see https://cloud.google.com/tpu/docs/runtimes
56+
// tpuSoftwareVersion = 'tpu-vm-tf-2.17.0-pod-pjrt';
57+
58+
async function callCreateTpuVMStartupScript() {
59+
// Create a node
60+
const node = new Node({
61+
name: nodeName,
62+
zone,
63+
acceleratorType: tpuType,
64+
runtimeVersion: tpuSoftwareVersion,
65+
// Define network
66+
networkConfig: new NetworkConfig({
67+
enableExternalIps: true,
68+
network: `projects/${projectId}/global/networks/${networkName}`,
69+
subnetwork: `projects/${projectId}/regions/${region}/subnetworks/${networkName}`,
70+
}),
71+
metadata: {
72+
// The script updates numpy to the latest version and logs the output to a file.
73+
'startup-script': `#!/bin/bash
74+
echo "Hello World" > /var/log/hello.log
75+
sudo pip3 install --upgrade numpy >> /var/log/hello.log 2>&1`,
76+
},
77+
});
78+
79+
const parent = `projects/${projectId}/locations/${zone}`;
80+
const request = {parent, node, nodeId: nodeName};
81+
82+
const [operation] = await tpuClient.createNode(request);
83+
84+
// Wait for the create operation to complete.
85+
const [response] = await operation.promise();
86+
87+
console.log(JSON.stringify(response));
88+
console.log(`TPU VM: ${nodeName} with start-up script created.`);
89+
}
90+
await callCreateTpuVMStartupScript();
91+
// [END tpu_vm_create_startup_script]
92+
}
93+
94+
main(...process.argv.slice(2)).catch(err => {
95+
console.error(err);
96+
process.exitCode = 1;
97+
});

tpu/vmCreateTopology.js renamed to tpu/createTopologyVM.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ async function main(nodeName, zone, tpuSoftwareVersion) {
8787
const [response] = await operation.promise();
8888

8989
console.log(JSON.stringify(response));
90-
console.log(`TPU VM: ${nodeName} created.`);
90+
console.log(`TPU VM: ${nodeName} with topology created.`);
9191
}
9292
await callCreateTpuVMTopology();
9393
// [END tpu_vm_create_topology]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
const path = require('path');
20+
const assert = require('node:assert/strict');
21+
const {after, describe, it} = require('mocha');
22+
const cp = require('child_process');
23+
const {getStaleNodes, deleteNode} = require('./util');
24+
25+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
26+
const cwd = path.join(__dirname, '..');
27+
28+
describe('Compute tpu', async () => {
29+
const nodePrefix = 'node-name-startup-script-2a2b3c';
30+
const nodeName = `${nodePrefix}${Math.floor(Math.random() * 1000 + 1)}`;
31+
const zone = 'us-central1-a';
32+
const tpuType = 'v3-8';
33+
const tpuSoftwareVersion = 'tpu-vm-tf-2.17.0-pod-pjrt';
34+
35+
before(async () => {
36+
// Clean-up resources
37+
const nodes = await getStaleNodes(nodePrefix, zone);
38+
await Promise.all(nodes.map(node => deleteNode(zone, node.nodeName)));
39+
});
40+
41+
after(async () => {
42+
// Delete node
43+
await deleteNode(zone, nodeName);
44+
});
45+
46+
it('should create a new tpu using startup script', () => {
47+
const metadata = {
48+
'startup-script':
49+
'#!/bin/bash\n echo "Hello World" > /var/log/hello.log\n sudo pip3 install --upgrade numpy >> /var/log/hello.log 2>&1',
50+
};
51+
52+
const response = execSync(
53+
`node ./createStartupScriptVM.js ${nodeName} ${zone} ${tpuType} ${tpuSoftwareVersion}`,
54+
{
55+
cwd,
56+
}
57+
);
58+
59+
assert(
60+
response.includes(`TPU VM: ${nodeName} with start-up script created.`)
61+
);
62+
assert(response.includes(JSON.stringify(metadata)));
63+
});
64+
});

tpu/test/vmTopology.test.js renamed to tpu/test/createTopologyVM.test.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
const path = require('path');
2020
const assert = require('node:assert/strict');
21-
const {before, after, describe, it} = require('mocha');
21+
const {after, describe, it} = require('mocha');
2222
const cp = require('child_process');
2323
const {getStaleNodes, deleteNode} = require('./util');
2424

@@ -28,11 +28,11 @@ const cwd = path.join(__dirname, '..');
2828
describe('Compute tpu with topology', async () => {
2929
const nodePrefix = 'topology-node-name-2a2b3c';
3030
const nodeName = `${nodePrefix}${Math.floor(Math.random() * 1000 + 1)}`;
31-
const zone = 'europe-west4-a';
31+
const zone = 'asia-east1-c';
3232
const tpuSoftwareVersion = 'tpu-vm-tf-2.17.0-pod-pjrt';
3333

3434
before(async () => {
35-
// Cleanup resources
35+
// Clean-up resources
3636
const nodes = await getStaleNodes(nodePrefix, zone);
3737
await Promise.all(nodes.map(node => deleteNode(zone, node.nodeName)));
3838
});
@@ -42,13 +42,17 @@ describe('Compute tpu with topology', async () => {
4242
await deleteNode(zone, nodeName);
4343
});
4444

45-
it('should create a new tpu', () => {
45+
it('should create a new tpu with topology', () => {
46+
const acceleratorConfig = {type: 'V2', topology: '2x2'};
47+
4648
const response = execSync(
47-
`node ./vmCreateTopology.js ${nodeName} ${zone} ${tpuSoftwareVersion}`,
49+
`node ./createTopologyVM.js ${nodeName} ${zone} ${tpuSoftwareVersion}`,
4850
{
4951
cwd,
5052
}
5153
);
52-
assert(response.includes(`TPU VM: ${nodeName} created.`));
54+
55+
assert(response.includes(`TPU VM: ${nodeName} with topology created.`));
56+
assert(response.includes(JSON.stringify(acceleratorConfig)));
5357
});
5458
});

tpu/test/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ async function deleteNode(zone, nodeName) {
5252
name: `projects/${projectId}/locations/${zone}/nodes/${nodeName}`,
5353
};
5454

55-
console.log('Deleting node: ', nodeName);
55+
console.log('Deleting node:', nodeName);
5656

5757
const [operation] = await tpuClient.deleteNode(request);
5858

0 commit comments

Comments
 (0)