|
| 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