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
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.
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
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:
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, 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
ctpu up command has finished executing, check the following:
Verify that your shell prompt has changed from
username@username. This change shows that you are now logged into your Compute Engine VM.
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:
Run a TensorFlow computation
Use the 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!')
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.
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
You can run
ctpu statusto 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: --
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.