|
| 1 | +# Copyright 2024 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import os |
| 15 | + |
| 16 | +from google.cloud.tpu_v2 import Node |
| 17 | + |
| 18 | + |
| 19 | +def create_cloud_tpu_with_script( |
| 20 | + project_id: str, |
| 21 | + zone: str, |
| 22 | + tpu_name: str, |
| 23 | + tpu_type: str = "v2-8", |
| 24 | + runtime_version: str = "tpu-vm-tf-2.17.0-pjrt", |
| 25 | +) -> Node: |
| 26 | + # [START tpu_vm_create_startup_script] |
| 27 | + from google.cloud import tpu_v2 |
| 28 | + |
| 29 | + # TODO(developer): Update and un-comment below lines |
| 30 | + # project_id = "your-project-id" |
| 31 | + # zone = "us-central1-b" |
| 32 | + # tpu_name = "tpu-name" |
| 33 | + # tpu_type = "v2-8" |
| 34 | + # runtime_version = "tpu-vm-tf-2.17.0-pjrt" |
| 35 | + |
| 36 | + node = tpu_v2.Node() |
| 37 | + node.accelerator_type = tpu_type |
| 38 | + node.runtime_version = runtime_version |
| 39 | + |
| 40 | + # This startup script updates numpy to the latest version and logs the output to a file. |
| 41 | + metadata = { |
| 42 | + "startup-script": """#!/bin/bash |
| 43 | + echo "Hello World" > /var/log/hello.log |
| 44 | + sudo pip3 install --upgrade numpy >> /var/log/hello.log 2>&1 |
| 45 | + """ |
| 46 | + } |
| 47 | + |
| 48 | + # Adding metadata with startup script to the TPU node. |
| 49 | + node.metadata = metadata |
| 50 | + # Enabling external IPs for internet access from the TPU node. |
| 51 | + node.network_config = tpu_v2.NetworkConfig(enable_external_ips=True) |
| 52 | + |
| 53 | + request = tpu_v2.CreateNodeRequest( |
| 54 | + parent=f"projects/{project_id}/locations/{zone}", |
| 55 | + node_id=tpu_name, |
| 56 | + node=node, |
| 57 | + ) |
| 58 | + |
| 59 | + client = tpu_v2.TpuClient() |
| 60 | + operation = client.create_node(request=request) |
| 61 | + print("Waiting for operation to complete...") |
| 62 | + |
| 63 | + response = operation.result() |
| 64 | + print(response.metadata) |
| 65 | + # Example response: |
| 66 | + # {'startup-script': '#!/bin/bash\n echo "Hello World" > /var/log/hello.log\n |
| 67 | + # ... |
| 68 | + |
| 69 | + # [END tpu_vm_create_startup_script] |
| 70 | + return response |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") |
| 75 | + ZONE = "us-central1-b" |
| 76 | + create_cloud_tpu_with_script(PROJECT_ID, ZONE, "tpu-name") |
0 commit comments