Quickstart

You can use a Cloud TPU to accelerate specific TensorFlow machine learning workloads. This page explains how to create a Cloud TPU and use it to run a basic TensorFlow program.

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. This walkthrough 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.

Create a Cloud Storage bucket

You need a Cloud Storage bucket to store the data that you use to train your machine learning model and the results of the training.

  1. Go to the Cloud Storage page on the GCP Console.

    Go to the Cloud Storage page

  2. Create a new bucket, specifying the following options:

    • A unique name of your choosing.
    • Default storage class: Regional
    • Location: us-central1

Open Cloud Shell and use the ctpu tool

This guide uses the Cloud TPU Provisioning Utility (ctpu) as a simple tool for setting up and managing your Cloud TPU. The guide runs ctpu from a Cloud Shell. For more advanced setup options, see the custom setup.

The ctpu tool is pre-installed in your Cloud Shell. Follow these steps to check your ctpu configuration:

  1. Open a Cloud Shell window.

    Open Cloud Shell

  2. Type the following into your Cloud Shell, to check your ctpu configuration:

    $ ctpu print-config
    

    You should see a message like this:

    2018/04/29 05:23:03 WARNING: Setting zone to "us-central1-b"
    ctpu configuration:
            name: [your TPU's name]
            project: [your-project-name]
            zone: us-central1-b
    If you would like to change the configuration for a single command invocation, please use the command line flags.
    

  3. Take a look at the ctpu commands:

    $ ctpu

    You should see a usage guide, including a list of subcommands and flags with a brief description of each one.

Create a Compute Engine VM and a Cloud TPU

Run the following command to set up a Compute Engine virtual machine (VM) and a Cloud TPU with associated services. This combination of resources and services is called a Cloud TPU flock:

$ ctpu up

The ctpu up command performs the following tasks:

  • Enables the Compute Engine and Cloud TPU services.
  • Creates a Compute Engine VM with the latest stable TensorFlow version pre-installed. The default zone is us-central1-b. For reference, Cloud TPU is available in the following zones:

    • us-central1-b
    • us-central1-c

  • Creates a Cloud TPU with the corresponding version of TensorFlow, and passes the name of the Cloud TPU to the Compute Engine VM as an environment variable (TPU_NAME).

  • Ensures your Cloud TPU has access to resources it needs from your GCP project, by granting specific IAM roles to your Cloud TPU service account.
  • Performs a number of other checks.
  • Logs you in to your new Compute Engine VM.

You can run ctpu up as often as you like. For example, if you lose the SSH connection to the Compute Engine VM, just run ctpu up to restore it.

From this point on, a prefix of (vm)$ means you should run the command on the Compute Engine VM instance.

Verify your Compute Engine VM

When the ctpu up command has finished executing, check the following:

  1. Verify that your shell prompt has changed from username@project to username@username. This change shows that you are now logged into your Compute Engine VM.

  2. Execute the following command to check your TensorFlow installation:

    (vm)$ python -c "import tensorflow; print(tensorflow.VERSION)"
    

    You should see some text printed, with the last line showing a TensorFlow version number. For example: 1.8.0.

Run a TensorFlow computation

Use the Cloud TPU to execute a simple TensorFlow script which performs A*X+Y:

  1. Open a text editor and create a new Python script named cloud-tpu.py:

    (vm)$ pico cloud-tpu.py
    

  2. Copy the contents of the following sample script into the file, then save the file.

    import os
    import tensorflow as tf
    from tensorflow.contrib import tpu
    from tensorflow.contrib.cluster_resolver import TPUClusterResolver
    
    def axy_computation(a, x, y):
      return a * x + y
    
    inputs = [
        3.0,
        tf.ones([3, 3], tf.float32),
        tf.ones([3, 3], tf.float32),
    ]
    
    tpu_computation = tpu.rewrite(axy_computation, inputs)
    
    tpu_grpc_url = TPUClusterResolver(
        tpu=[os.environ['TPU_NAME']]).get_master()
    
    with tf.Session(tpu_grpc_url) as sess:
      sess.run(tpu.initialize_system())
      sess.run(tf.global_variables_initializer())
      output = sess.run(tpu_computation)
      print(output)
      sess.run(tpu.shutdown_system())
    
    print('Done!')
    

    Note: This example code is for testing that you are properly configured and can run computation on the Cloud TPU. To train your models, the recommended method is to use the TPUEstimator framework. Read the tutorial for running a residual network on Cloud TPU to see an example of training using TPUEstimator.

  3. Run the TensorFlow program. The program creates a tf.Session() element with a gRPC pointing to your Cloud TPU endpoint and runs a simple computation:

    (vm)$ python cloud-tpu.py
    [array([[4., 4., 4.],
           [4., 4., 4.],
           [4., 4., 4.]], dtype=float32)]
    Done!
    

Clean up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this quickstart:

  1. Disconnect from the Compute Engine VM:

    (vm)$ exit
    

    Your prompt should now be user@projectname, showing you are in your Cloud Shell.

  2. In your Cloud Shell, run the following command to delete your Compute Engine VM and your Cloud TPU:

    $ ctpu delete
    

  3. You can run ctpu status to make sure you have no instances allocated, although note that deletion may take a minute or two. A response like the one below indicates there are no more allocated instances:

    2018/04/28 16:16:23 WARNING: Setting zone to "us-central1-b"
    No instances currently exist.
            GCE VM:     --
            Cloud TPU:  --
    

  4. When you no longer need the Cloud Storage bucket you created during this tutorial, use the gsutil command to delete it. 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

Send feedback about...