How to get all possible GPU TPU using tensorflow

This recipe helps you get all possible GPU TPU using tensorflow

Recipe Objective

How to get all possible GPU/TPU using tensorflow?

The TPU here usually are on Cloud TPU worker where these are different from the local process running the user program in python. As there is need some initialization work which is to be done to connect to the remote cluster and then initialize the TPU. Here the argument of tpu to TPUClusterResolver is a special address just for Google Colab. If want to use colab for this then one thing to remember that our hardware accelerator is a TPU by checking the simple settings in Colab: Setting --> Runtime --> Change runtime type --> Hardware accelerator --> TPU

Access Avocado Machine Learning Project for Price Prediction

Step 1 - Import library

import tensorflow as tf import os import tensorflow_datasets as tfds

Step 2 - Initialize the TPU

TPU_Clust_Resolv = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='grpc://' + os.environ['COLAB_TPU_ADDR']) tf.config.experimental_connect_to_cluster(TPU_Clust_Resolv) tf.tpu.experimental.initialize_tpu_system(TPU_Clust_Resolv) print("All devices: ", tf.config.list_logical_devices('TPU'))

INFO:tensorflow:Initializing the TPU system: grpc://10.76.70.226:8470
INFO:tensorflow:Initializing the TPU system: grpc://10.76.70.226:8470
INFO:tensorflow:Clearing out eager caches
INFO:tensorflow:Clearing out eager caches
INFO:tensorflow:Finished initializing TPU system.
INFO:tensorflow:Finished initializing TPU system.
All devices:  [LogicalDevice(name='/job:worker/replica:0/task:0/device:TPU:7', device_type='TPU'), LogicalDevice(name='/job:worker/replica:0/task:0/device:TPU:6', device_type='TPU'), LogicalDevice(name='/job:worker/replica:0/task:0/device:TPU:5', device_type='TPU'), LogicalDevice(name='/job:worker/replica:0/task:0/device:TPU:4', device_type='TPU'), LogicalDevice(name='/job:worker/replica:0/task:0/device:TPU:3', device_type='TPU'), LogicalDevice(name='/job:worker/replica:0/task:0/device:TPU:0', device_type='TPU'), LogicalDevice(name='/job:worker/replica:0/task:0/device:TPU:1', device_type='TPU'), LogicalDevice(name='/job:worker/replica:0/task:0/device:TPU:2', device_type='TPU')]

Step 3 - Manual device placement

X_var = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) Y_var = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]) with tf.device('/TPU:0'): Z_var = tf.matmul(X_var, Y_var) print("Z_var device: ", Z_var.device) print("This is the output of matrix multiplication:",Z_var)

Z_var device:  /job:worker/replica:0/task:0/device:TPU:0
This is the output of matrix multiplication: tf.Tensor(
[[22. 28.]
 [49. 64.]], shape=(2, 2), dtype=float32)

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

Word2Vec and FastText Word Embedding with Gensim in Python
In this NLP Project, you will learn how to use the popular topic modelling library Gensim for implementing two state-of-the-art word embedding methods Word2Vec and FastText models.

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .

Deploy Transformer BART Model for Text summarization on GCP
Learn to Deploy a Machine Learning Model for the Abstractive Text Summarization on Google Cloud Platform (GCP)

PyTorch Project to Build a LSTM Text Classification Model
In this PyTorch Project you will learn how to build an LSTM Text Classification model for Classifying the Reviews of an App .

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.