|
| 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 | + |
| 15 | +import os |
| 16 | + |
| 17 | +from google.cloud.tpu_v2 import Node |
| 18 | + |
| 19 | + |
| 20 | +def create_cloud_tpu_with_topology( |
| 21 | + project_id: str, |
| 22 | + zone: str, |
| 23 | + tpu_name: str, |
| 24 | + runtime_version: str = "tpu-vm-tf-2.17.0-pjrt", |
| 25 | +) -> Node: |
| 26 | + """Creates a Cloud TPU node with a specific topology. |
| 27 | + Args: |
| 28 | + project_id (str): The ID of the Google Cloud project. |
| 29 | + zone (str): The zone where the TPU node will be created. |
| 30 | + tpu_name (str): The name of the TPU node. |
| 31 | + runtime_version (str, optional): The runtime version for the TPU. |
| 32 | + Returns: |
| 33 | + Node: The created TPU node. |
| 34 | + """ |
| 35 | + # [START tpu_vm_create_topology] |
| 36 | + from google.cloud import tpu_v2 |
| 37 | + |
| 38 | + # TODO(developer): Update and un-comment below lines |
| 39 | + # project_id = "your-project-id" |
| 40 | + # zone = "us-central1-b" |
| 41 | + # tpu_name = "tpu-name" |
| 42 | + # runtime_version = "tpu-vm-tf-2.17.0-pjrt" |
| 43 | + |
| 44 | + node = tpu_v2.Node() |
| 45 | + # Here we are creating a TPU v3-8 with 2x2 topology. |
| 46 | + node.accelerator_config = tpu_v2.AcceleratorConfig( |
| 47 | + type_=tpu_v2.AcceleratorConfig.Type.V3, |
| 48 | + topology="2x2", |
| 49 | + ) |
| 50 | + node.runtime_version = runtime_version |
| 51 | + |
| 52 | + request = tpu_v2.CreateNodeRequest( |
| 53 | + parent=f"projects/{project_id}/locations/{zone}", |
| 54 | + node_id=tpu_name, |
| 55 | + node=node, |
| 56 | + ) |
| 57 | + |
| 58 | + client = tpu_v2.TpuClient() |
| 59 | + operation = client.create_node(request=request) |
| 60 | + print("Waiting for operation to complete...") |
| 61 | + |
| 62 | + response = operation.result() |
| 63 | + print(response.accelerator_config) |
| 64 | + # Example response: |
| 65 | + # type_: V3 |
| 66 | + # topology: "2x2" |
| 67 | + |
| 68 | + # [END tpu_vm_create_topology] |
| 69 | + return response |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") |
| 74 | + ZONE = "us-central1-a" |
| 75 | + create_cloud_tpu_with_topology(PROJECT_ID, ZONE, "tpu-name") |
0 commit comments