|
| 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 | +}); |
0 commit comments