Custom Setup

This document shows you how to perform a full setup of a Cloud TPU using the individual commands or the Google Cloud Platform Console at each stage of the process.

In most cases, you can use the Cloud TPU Provisioning Utility (ctpu) as an alternative, simple tool for setting up and managing your Cloud TPU. The quickstart guide and tutorials use ctpu. But there may be times when you need more control over the setup and management of your Cloud TPU. The instructions below are useful for those situations.

Before you begin

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Select or create a GCP project.

    Go to the Manage resources page

  3. Make sure that billing is enabled for your project.

    Learn how to enable billing

  4. When you use Cloud TPU, your project uses billable components of Google Cloud Platform. Check the Cloud TPU pricing page to estimate your costs, and follow the instructions to clean up resources when you've finished with them.

  5. Visit the Cloud TPU page on the GCP Console and enable the Cloud TPU API.

    Go to the Cloud TPU API page

  6. Visit the Compute Engine page on the GCP Console and enable the Compute Engine API.

    Go to the Compute Engine API page

Install the gcloud command-line tool

The Cloud SDK is a set of tools that you can use to interact with GCP from the command line.

  1. Install the gcloud command-line tool via the Cloud SDK.

    Install the Cloud SDK

Create a Compute Engine VM and a Cloud TPU resource

This section shows you how to create a Compute Engine VM and a Cloud TPU resource from your local machine.

  1. Use the gcloud command-line tool to specify your Cloud Platform project:

    $ gcloud config set project YOUR-CLOUD-PROJECT
    

    where YOUR-CLOUD-PROJECTis the name of your GCP project that has access to Cloud TPU quota.

  2. Specify the zone where you plan to create your Compute Engine VM and Cloud TPU resource. For this example, use the us-central1-b zone:

    $ gcloud config set compute/zone us-central1-b
    

    For reference, Cloud TPU is available in the following zones:

    • United States (US)
    • Europe (EU)
      • europe-west4-a
    • Asia Pacific (APAC)
      • asia-east1-c

  3. Create a Compute Engine VM to interact with your Cloud TPU:

    $ gcloud compute instances create tpu-demo-vm \
      --machine-type=n1-standard-2 \
      --image-project=ml-images \
      --image-family=tf-1-9 \
      --scopes=cloud-platform
    

    where:

    • tpu-demo-vm is a name for identifying the VM instance that you're creating.

    • --machine-type=n1-standard-2 is a standard machine type with 4 virtual CPUs and 15 GB of memory. See the available machine types.

    • --image-project=ml-images is a shared collection of images that makes the tf-1-9 image available for your use.

    • --image-family=tf-1-9 is an image with the required pip package for TensorFlow.

    • --scopes=cloud-platform allows the VM to access GCP APIs.

  4. Create a new Cloud TPU resource using the gcloud compute tpus create command. For this example, name the resource demo-tpu. Keep in mind that billing begins as soon as the Cloud TPU is created, until the time it is deleted. Check the Cloud TPU pricing page to estimate your costs. If you are using a dataset that requires a substantial download and processing phase, hold off on running this command until you are ready to use the Cloud TPU:

    $ gcloud compute tpus create demo-tpu \
      --network=[YOUR_NETWORK_ID] \
      --range=10.240.1.0/29 \
      --version=1.9
    

    where:

    • demo-tpu is a name for identifying the TPU that you're creating.
    • --network specifies the name of the network that your Compute Engine VM instance uses. You must be able to connect to instances on this network over SSH. For most situations, you can use the default network that your Google Cloud Platform project created automatically. However, an error results if the default network is a legacy network.
    • --range specifies the address of the created Cloud TPU resource and can be any value in 10.240.*.*/29. For this example, use 10.240.1.0/29.
    • --version specifies the TensorFlow version to use with the TPU.

Remotely connect to the Compute Engine VM

You can use the following command to remotely connect to your Compute Engine VM:

$ gcloud compute ssh tpu-demo-vm

Alternatively, you can SSH into your Compute Engine VM from the GCP Console. Go to Compute Engine > VM instances. Find the instance with your VM name, and click SSH to connect to it.

Prepare to run a machine learning model on Cloud TPU

Run the following commands on your Compute Engine VM:

  1. Use the gcloud command-line tool to specify your GCP project:

    (vm)$ gcloud config set project YOUR-CLOUD-PROJECT
    

    where YOUR-CLOUD-PROJECT is the name of the your GCP project with access to TPU quota.

  2. Specify the zone where you plan to create your VM and TPU resource. For this tutorial, use the us-central1-b zone:

    (vm)$ gcloud config set compute/zone us-central1-b
    

    For reference, Cloud TPU is available in the following zones:

    • United States (US)
    • Europe (EU)
      • europe-west4-a
    • Asia Pacific (APAC)
      • asia-east1-c

  3. List your available TPUs:

    (vm)$ gcloud compute tpus list
    

    The above command prints the details of the TPUs you've created. For example:

    NAME       ZONE           ACCELERATOR_TYPE  NETWORK_ENDPOINT   NETWORK  RANGE          STATUS
    demo-tpu   us-central1-b  v2-8              10.240.1.2:8470    default  10.240.1.0/29  READY
    

  4. Create an environment variable containing the name of your TPU:

    (vm)$ export TPU_NAME='demo-tpu'
    

You're ready to run your model

You can now run your own machine learning workload on your Cloud TPU.

Clean up

When you've finished with the Compute Engine VM and the Cloud TPU, clean up the resources to avoid incurring extra charges to your Google Cloud Platform account.

If you haven't set the project and zone for this session, do so now. Then follow this cleanup procedure:

  1. Use the gcloud command-line tool to delete your Cloud TPU resource:

    (vm)$ gcloud compute tpus delete demo-tpu
    

  2. Disconnect from the Compute Engine VM instance:

    (vm)$ exit
    

  3. Use the gcloud command-line tool to delete your Compute Engine instance:

    $ gcloud compute instances delete tpu-demo-vm
    

  4. Go to the VPC Networking page in the Google Cloud Platform Console.

    Go to the VPC Networking page

  5. Select the VPC network that Google automatically created as part of the Cloud TPU setup. The peering entry starts with cp-to-tp-peering in the ID.

  6. At the top of the VPC Networking page, click Delete to delete the selected VPC network.

  7. Go to the Network Routes page in the Google Cloud Platform Console.

    Go to the Network Routes page

  8. Select the route that Google automatically created as part of the Cloud TPU setup. The peering entry starts with peering-route in the ID.

  9. At the top of the Network Routes page, click Delete to delete the selected route.

  10. When you've finished finished examining the data, use the gsutil command to delete any Cloud Storage buckets you created during this tutorial. Replace YOUR-BUCKET-NAME with the name of your Cloud Storage bucket:

    $ gsutil rm -r gs://YOUR-BUCKET-NAME
    

    See the Cloud Storage pricing guide for free storage limits and other pricing information.

What's next

Was this page helpful? Let us know how we did:

Send feedback about...