This page explains how to use a Cloud TPU to accelerate specific TensorFlow machine learning workloads on Compute Engine.
For Google Kubernetes Engine, see the quick guide to setting up Cloud TPU.
For Cloud Machine Learning Engine see the guide for training your model using TPUs.
Need help deciding which TPU service is best for you?
Before you begin
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
Select or create a GCP project.
Make sure that billing is enabled for your project.
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.
Go to the Cloud Storage page on the GCP Console.
Create a new bucket, specifying the following options:
- A unique name of your choosing.
- Default storage class:
Open Cloud Shell and use the
This guide uses the Cloud TPU Provisioning
a simple tool for setting up and managing your Cloud TPU. The guide
ctpu from a Cloud Shell. For more advanced setup options, see the
ctpu tool is pre-installed in your Cloud Shell. Follow these steps to
Open a Cloud Shell window.
Type the following into your Cloud Shell, to check your
$ 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.
In the output message, the name is the name of your TPU resource (defaults to your username) and zone is the default geographic zone for your Compute Engine. You can change these when you run
ctpu upto create a Compute Engine VM and a Cloud TPU.
Take a look at the ctpu commands:
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 [optional: --name --zone]
You should see a message like this:
ctpu will use the following configuration:
Name: [your TPU's name] Zone: [your project's zone] GCP Project: [your project's name] TensorFlow Version: 1.9 VM: Machine Type: [your machine type] Disk Size: [your disk size] Preemptible: [true or false] Cloud TPU: Size: [your TPU size] Preemptible: [true or false]
OK to create your Cloud TPU resources with the above configuration? [Yn]:
Press y to create your Cloud TPU resources.
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:
- United States (US)
us-central1-f(TFRC program only)
- Europe (EU)
- Asia Pacific (APAC)
- United States (US)
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 (
- 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, run
ctpu up to restore the
--zone if you changed the default values.
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
ctpu up command has finished executing, verify that your shell
prompt has changed from
change shows that you are now logged into your Compute Engine VM.
Use the default or change the Cloud Storage access permissions
ctpu up command set up default permissions for your Cloud TPU
service account. If you want finer-grain permissions, review and update the
access level permissions.
Run a TensorFlow computation
Use Cloud TPU to execute a simple TensorFlow script which
Open a text editor and create a new Python script named
(vm)$ pico cloud-tpu.py
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!')
- Run the TensorFlow program. The program creates a
tf.Session()element with a
gRPCpointing 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!
To avoid incurring charges to your Google Cloud Platform account for the resources used in this quickstart:
Disconnect from the Compute Engine VM:
Your prompt should now be
user@projectname, showing you are in your Cloud Shell.
In your Cloud Shell, run the following command to delete your Compute Engine VM and your Cloud TPU:
$ ctpu delete
ctpu statusto make sure you have no instances allocated to avoid unnecessary charges for TPU usage. The deletion might take several minutes. 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. Compute Engine VM: -- Cloud TPU: --
When you no longer need the Cloud Storage bucket you created during this tutorial, use the
gsutilcommand to delete it. Replace
YOUR-BUCKET-NAMEwith 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.
- Learn more about
ctpu, including how to install it on a local machine.
- Follow the tutorial to run a TensorFlow MNIST sample on TPUs.
- Verify performance on a large-scale model by training a ResNet model on ImageNet.
- Explore the TPU tools in TensorBoard.
- Consider using Google Kubernetes Engine to manage your Cloud TPU clusters.