|
| 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(tpuClient) { |
| 20 | + // [START tpu_vm_create] |
| 21 | + // Import the TPUClient |
| 22 | + // TODO(developer): Uncomment below line before running the sample. |
| 23 | + // const {TpuClient} = require('@google-cloud/tpu').v2; |
| 24 | + const {Node, NetworkConfig} = |
| 25 | + require('@google-cloud/tpu').protos.google.cloud.tpu.v2; |
| 26 | + |
| 27 | + // Instantiate a tpuClient |
| 28 | + // TODO(developer): Uncomment below line before running the sample. |
| 29 | + // tpuClient = new TpuClient(); |
| 30 | + |
| 31 | + // TODO(developer): Update below line before running the sample. |
| 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 | + const 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 | + const 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 | + const 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 | + const tpuSoftwareVersion = 'tpu-vm-tf-2.14.1'; |
| 57 | + |
| 58 | + async function callCreateTpuVM() { |
| 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 | + }); |
| 72 | + |
| 73 | + const parent = `projects/${projectId}/locations/${zone}`; |
| 74 | + const request = {parent, node, nodeId: nodeName}; |
| 75 | + |
| 76 | + const [operation] = await tpuClient.createNode(request); |
| 77 | + |
| 78 | + // Wait for the create operation to complete. |
| 79 | + const [response] = await operation.promise(); |
| 80 | + |
| 81 | + console.log(`TPU VM: ${nodeName} created.`); |
| 82 | + return response; |
| 83 | + } |
| 84 | + return await callCreateTpuVM(); |
| 85 | + // [END tpu_vm_create] |
| 86 | +} |
| 87 | + |
| 88 | +module.exports = main; |
| 89 | + |
| 90 | +// TODO(developer): Uncomment below lines before running the sample. |
| 91 | +// main(...process.argv.slice(2)).catch(err => { |
| 92 | +// console.error(err); |
| 93 | +// process.exitCode = 1; |
| 94 | +// }); |
0 commit comments