|
| 1 | +package tpu; |
| 2 | + |
| 3 | +//[START tpu_queued_resources_network] |
| 4 | + |
| 5 | +import com.google.api.gax.retrying.RetrySettings; |
| 6 | +import com.google.cloud.tpu.v2alpha1.CreateQueuedResourceRequest; |
| 7 | +import com.google.cloud.tpu.v2alpha1.NetworkConfig; |
| 8 | +import com.google.cloud.tpu.v2alpha1.Node; |
| 9 | +import com.google.cloud.tpu.v2alpha1.QueuedResource; |
| 10 | +import com.google.cloud.tpu.v2alpha1.TpuClient; |
| 11 | +import com.google.cloud.tpu.v2alpha1.TpuSettings; |
| 12 | +import java.io.IOException; |
| 13 | +import java.util.concurrent.ExecutionException; |
| 14 | +import org.threeten.bp.Duration; |
| 15 | + |
| 16 | +public class CreateQueuedResourceWithNetwork { |
| 17 | + |
| 18 | + private final TpuClient tpuClient; |
| 19 | + public CreateQueuedResourceWithNetwork(TpuClient tpuClient) { |
| 20 | + this.tpuClient = tpuClient; |
| 21 | + } |
| 22 | + public static void main(String[] args) |
| 23 | + throws IOException, ExecutionException, InterruptedException { |
| 24 | + // TODO(developer): Replace these variables before running the sample. |
| 25 | + // Project ID or project number of the Google Cloud project you want to create a node. |
| 26 | + String projectId = "tyaho-softserve-project";//"your-project"; |
| 27 | + // The zone in which to create the TPU. |
| 28 | + // For more information about supported TPU types for specific zones, |
| 29 | + // see https://cloud.google.com/tpu/docs/regions-zones |
| 30 | + String zone = "europe-west4-a"; |
| 31 | + // The name for your TPU. |
| 32 | + String nodeName = "your-node-id"; |
| 33 | + // The accelerator type that specifies the version and size of the Cloud TPU you want to create. |
| 34 | + // For more information about supported accelerator types for each TPU version, |
| 35 | + // see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions. |
| 36 | + String tpuType = "v2-8"; |
| 37 | + // Software version that specifies the version of the TPU runtime to install. |
| 38 | + // For more information see https://cloud.google.com/tpu/docs/runtimes |
| 39 | + String tpuSoftwareVersion = "tpu-vm-tf-2.14.1"; |
| 40 | + // The name for your Queued Resource. |
| 41 | + String queuedResourceName = "your-queued-resource-id"; |
| 42 | + TpuClient tpuClient = TpuClient.create(); |
| 43 | + CreateQueuedResourceWithNetwork creator = new CreateQueuedResourceWithNetwork(tpuClient); |
| 44 | + |
| 45 | + creator.createQueuedResourceWithNetwork(projectId, zone, queuedResourceName, nodeName, tpuType, tpuSoftwareVersion); |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + // Creates a Queued Resource with network configuration. |
| 50 | + public static void createQueuedResourceWithNetwork( |
| 51 | + String projectId, |
| 52 | + String zone, |
| 53 | + String queuedResourceName, |
| 54 | + String nodeName, |
| 55 | + String tpuType, |
| 56 | + String tpuSoftwareVersion) |
| 57 | + throws IOException, ExecutionException, InterruptedException { |
| 58 | + // With these settings the client library handles the Operation's polling mechanism |
| 59 | + // and prevent CancellationException error |
| 60 | + TpuSettings.Builder clientSettings = |
| 61 | + TpuSettings.newBuilder(); |
| 62 | + clientSettings |
| 63 | + .createQueuedResourceSettings() |
| 64 | + .setRetrySettings( |
| 65 | + RetrySettings.newBuilder() |
| 66 | + .setInitialRetryDelay(Duration.ofMillis(5000L)) |
| 67 | + .setRetryDelayMultiplier(2.0) |
| 68 | + .setInitialRpcTimeout(Duration.ZERO) |
| 69 | + .setRpcTimeoutMultiplier(1.0) |
| 70 | + .setMaxRetryDelay(Duration.ofMillis(45000L)) |
| 71 | + .setTotalTimeout(Duration.ofHours(24L)) |
| 72 | + .build()); |
| 73 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 74 | + // once, and can be reused for multiple requests. |
| 75 | + try (TpuClient tpuClient = TpuClient.create(clientSettings.build())) { |
| 76 | + String parent = String.format("projects/%s/locations/%s", projectId, zone); |
| 77 | + String region = zone.substring(0, zone.length() - 2); |
| 78 | + // The name of the network you want the node to connect to. The network should be assigned to your project. |
| 79 | + String networkName = "compute-tpu-network"; |
| 80 | + // Specify the network and subnetwork that you want to connect your TPU to. |
| 81 | + |
| 82 | + NetworkConfig networkConfig = |
| 83 | + NetworkConfig.newBuilder() |
| 84 | + .setEnableExternalIps(true) |
| 85 | + .setNetwork(String.format("projects/%s/global/networks/%s", projectId, networkName)) |
| 86 | + .setSubnetwork( |
| 87 | + String.format( |
| 88 | + "projects/%s/regions/%s/subnetworks/%s", projectId, region, networkName)) |
| 89 | + .build(); |
| 90 | + |
| 91 | + // Create a node |
| 92 | + Node node = |
| 93 | + Node.newBuilder() |
| 94 | + .setName(nodeName) |
| 95 | + .setAcceleratorType(tpuType) |
| 96 | + .setRuntimeVersion(tpuSoftwareVersion) |
| 97 | + .setNetworkConfig(networkConfig) |
| 98 | + .setQueuedResource( |
| 99 | + String.format( |
| 100 | + "projects/%s/locations/%s/queuedResources/%s", |
| 101 | + projectId, zone, queuedResourceName)) |
| 102 | + .build(); |
| 103 | + |
| 104 | + // Create queued resource |
| 105 | + QueuedResource queuedResource = |
| 106 | + QueuedResource.newBuilder() |
| 107 | + .setName(queuedResourceName) |
| 108 | + .setTpu( |
| 109 | + QueuedResource.Tpu.newBuilder() |
| 110 | + .addNodeSpec( |
| 111 | + QueuedResource.Tpu.NodeSpec.newBuilder() |
| 112 | + .setParent(parent) |
| 113 | + .setNode(node) |
| 114 | + .setNodeId(nodeName) |
| 115 | + .build()) |
| 116 | + .build()) |
| 117 | + .build(); |
| 118 | + |
| 119 | + CreateQueuedResourceRequest request = |
| 120 | + CreateQueuedResourceRequest.newBuilder() |
| 121 | + .setParent(parent) |
| 122 | + .setQueuedResource(queuedResource) |
| 123 | + .setQueuedResourceId(queuedResourceName) |
| 124 | + .build(); |
| 125 | + |
| 126 | + QueuedResource response = tpuClient.createQueuedResourceAsync(request).get(); |
| 127 | + // You can wait until TPU Node is READY, |
| 128 | + // and check its status using getTpuVm() from "tpu_vm_get" sample. |
| 129 | + System.out.printf( |
| 130 | + "Queued resource %s with specified network created.\n", queuedResourceName); |
| 131 | + System.out.println(response); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | +//[END tpu_queued_resources_network |
0 commit comments