Skip to content

Commit 2d79b73

Browse files
Joanna Grycziennae
authored andcommitted
feat: tpu_vm_create
1 parent 0469e1f commit 2d79b73

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

compute/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
},
1616
"dependencies": {
1717
"@google-cloud/compute": "^4.0.0",
18+
"@google-cloud/tpu": "^3.5.0",
1819
"@sendgrid/mail": "^8.0.0",
1920
"nodemailer": "^6.0.0",
2021
"nodemailer-smtp-transport": "^2.7.4",

compute/test/tpu.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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('chai');
21+
const {describe, xit} = require('mocha');
22+
const cp = require('child_process');
23+
// const {TpuClient} = require('@google-cloud/tpu').v2;
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 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;
35+
36+
// before(async () => {
37+
// projectId = await tpuClient.getProjectId();
38+
// });
39+
40+
xit('should create a new tpu node', () => {
41+
const response = execSync(
42+
`node ./tpu/vmCreate.js ${nodeName} ${zone} ${tpuType} ${tpuSoftwareVersion}`,
43+
{
44+
cwd,
45+
}
46+
);
47+
48+
assert.include(response, `TPU VM: ${nodeName} created.`);
49+
});
50+
});

compute/tpu/vmCreate.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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]
21+
// Import the TPU library
22+
const tpuLib = require('@google-cloud/tpu');
23+
const tpu = tpuLib.protos.google.cloud.tpu.v2;
24+
25+
// Instantiate a tpuClient
26+
const tpuClient = new tpuLib.TpuClient();
27+
28+
/**
29+
* TODO(developer): Update/uncomment these variables before running the sample.
30+
*/
31+
// Project ID or project number of the Google Cloud project you want to create a node.
32+
const projectId = await tpuClient.getProjectId();
33+
34+
// The name for your TPU.
35+
// nodeName = 'node-name-1';
36+
37+
// The zone in which to create the TPU.
38+
// For more information about supported TPU types for specific zones,
39+
// see https://cloud.google.com/tpu/docs/regions-zones
40+
// zone = 'us-central1-a';
41+
42+
// The accelerator type that specifies the version and size of the Cloud TPU you want to create.
43+
// For more information about supported accelerator types for each TPU version,
44+
// see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
45+
// tpuType = 'v2-32';
46+
47+
// Software version that specifies the version of the TPU runtime to install. For more information,
48+
// see https://cloud.google.com/tpu/docs/runtimes
49+
// tpuSoftwareVersion = 'tpu-vm-base';
50+
51+
async function callCreateTpuVM() {
52+
// Create a node
53+
const node = new tpu.Node({
54+
name: nodeName,
55+
zone,
56+
apiVersion: tpu.Node.ApiVersion.V2,
57+
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.
59+
// You can find a list of supported versions in the Cloud TPU documentation: https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions
60+
tensorflowVersion: tpuSoftwareVersion,
61+
networkConfig: tpu.NetworkConfig({enableExternalIps: true}),
62+
shieldedInstanceConfig: tpu.ShieldedInstanceConfig({
63+
enableSecureBoot: true,
64+
}),
65+
});
66+
const parent = `projects/${projectId}/locations/${zone}`;
67+
const request = {parent, node, nodeId: nodeName};
68+
69+
const [response] = await tpuClient.createNode(request);
70+
71+
console.log(`TPU VM: ${nodeName} created.`);
72+
console.log(JSON.stringify(response));
73+
}
74+
75+
await callCreateTpuVM();
76+
// [END tpu_vm_create]
77+
}
78+
79+
main(...process.argv.slice(2)).catch(err => {
80+
console.error(err);
81+
process.exitCode = 1;
82+
});

0 commit comments

Comments
 (0)