TensorFlow Single and Multiple GPU

Last updated on Oct 24 2021
Ashutosh Wakiroo

Table of Contents

TensorFlow Single and Multiple GPU

Our usual system can comprise multiple devices for computation, and as we already know TensorFlow, supports both CPU and GPU, which we represent a string.
For example:
• If we have a CPU, it may be addressed as “/cpu:0”.
• TensorFlow GPU strings have index starting from zero.
• Similarly, the second GPU is “/device:GPU:1”.

tensorFlow 43
tensorFlow

Device Placement Logging

We can find out which devices handle the particular operations by creating a session where the log_device_placementconfiguration option is preset.

1. # Graph creation. 
2. x = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='x') 
3. y = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='y') 
4. z = tf.matmul(x, y) 
5. sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) 
6. # Running the operation. 
7. print(sess.run(z)) 
The output of TensorFlow GPU device placement logging shown as below:
1. /log:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Tesla K40c, pci bus 
2. id: 0000:05:00.0 
3. b: /job:localhost/replica:0/task:0/device:GPU:0 
4. a: /job:localhost/replica:0/task:0/device:GPU:0 
5. MatMul: /job:localhost/replica:0/task:0/device:GPU:0 
6. [[ 22. 28.] 
7. [ 49. 64.]]

Manual Device Placement

At times we can want to decide on which device our operation should be running and we can do this by creating a context with tf.device wherein we assign the specific device, like. CPU or a GPU that should do the computation, as given below:

1. # Graph Creation.
2. with tf.device('/cpu:0'):
3. x = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='x')
4. y = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='y')
5. z = tf.matmul(x, y)
6. sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
7. # Running the operation.
8. print(sess.run(z))

The above code of TensorFlow GPU assigns the constants a and b to cpu:o. In the second part of the code, since there is no explicit declaration of which device is to perform the task, a GPU by default is chosen if available and it copies the multi-dimensional arrays between devices.

1. Device mapping:
2. /job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Tesla K40c, pci bus
3. id: 0000:05:00.0
4. y: /job:localhost/replica:0/task:0/cpu:0
5. x: /job:localhost/replica:0/task:0/cpu:0
6. MatMul: /job:localhost/replica:0/task:0/device:GPU:0
7. [[ 22. 28.]
8. [ 49. 64.]]

Optimizing TensorFlow GPU Memory

Memory fragmentation is done to optimize memory resources by mapping almost all of the TensorFlow GPUs memory that is visible to the processor, thus saving a lot of potential resources. TensorFlow GPU offers two configuration options to control the allocation of memory if and when required by the processor to save memory and these TensorFlow GPU optimizations are described below:
ConfigProto is used for this purpose:

1. config = tf.ConfigProto()
2. config.gpu_options.allow_growth = True
3. session = tf.Session(config=config, ...)
per_process_gpu_memory_fraction is the second choice, and it decides the segment of the total memory should be allocated for each GPU in use. Given below is an example which will be used in tensorflow to allocate 40% of the memory:
1. config = tf.ConfigProto()
2. config.gpu_options.per_process_gpu_memory_fraction = 0.4
3. session = tf.Session(config=config, ...)

It is using only in cases where we already specify the computation and are sure that we will not change during processing.

Single GPU in Multi-GPU system

In multi TensorFlow GPU systems, the device with the lowest identity is selected by default user does not need it.

1. # Creates a graph.
2. with tf.device('/device:GPU:2'):
3. x = tf.constant([1, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='x')
4. y = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='y')
5. z= tf.matmul(x, y)
6. # Creates a session with log_device_placement set to True.
7. sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
8. # Runs the op.
9. print(sess.run(c=z))
The InvalidArgumentError is obtained when the TensorFlow GPU specified by the user does not exist as shown below:
1. InvalidArgumentError: Invalid argument: Cannot assign a device to node 'y':
2. Could not satisfy explicit device specification '/device:GPU:2'
3. [[Node: b = Const[dtype=DT_FLOAT, value=Tensor<type: float shape: [3,2]
4. values: 1 2 3...>, _device="/device:GPU:2"]()]]
if we want to specify the default

Using Multiple GPU in TensorFlow

We are already aware of the towers in TensorFlow and each tower we can assign to a GPU, making a multi tower structural model for working with TensorFlow multiple GPUs.

1. z= [] 
2. for d in ['/device:GPU:2', '/device:GPU:3']: 
3. with tf.device(d): 
4. x = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3]) 
5. y = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2]) 
6. z.append(tf.matmul(x, y)) 
7. with tf.device('/cpu:0'): 
8. sum = tf.add_n(z) 
9. sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) 
10. # Running the operations. 
11. print(sess.run(sum)) 
The output of TensorFlow GPU is as follows:
1. /job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Tesla K20m, pci bus 
2. id: 0000:02:00.0 
3. /job:localhost/replica:0/task:0/device:GPU:1 -> device: 1, name: Tesla K20m, pci bus 
4. id: 0000:03:00.0 
5. /job:localhost/replica:0/task:0/device:GPU:2 -> device: 2, name: Tesla K20m, pci bus 
6. id: 0000:83:00.0 
7. /job:localhost/replica:0/task:0/device:GPU:3 -> device: 3, name: Tesla K20m, pci bus 
8. id: 0000:84:00.0 
9. Const_3: /job:localhost/replica:0/task:0/device:GPU:3 
10. Const_2: /job:localhost/replica:0/task:0/device:GPU:3 
11. MatMul_1: /job:localhost/replica:0/task:0/device:GPU:3 
12. Const_1: /job:localhost/replica:0/task:0/device:GPU:2 
13. Const: 2/job:localhost/replica:0/task:0/device:GPU:2 
14. MatMul: /job:localhost/replica:0/task:0/device:GPU:2 
15. AddN: /job:localhost/replica:0/task:0/cpu:0 
16. [[ 44. 56.] 
17. [ 98. 128.]]

We can test this multiple GPU model with a simple dataset such as CIFAR10 to experiment and understand working with GPUs.
So, this brings us to the end of blog. This Tecklearn ‘TensorFlow Single and Multiple GPU’ blog helps you with commonly asked questions if you are looking out for a job in Artificial Intelligence. If you wish to learn Artificial Intelligence and build a career in AI or Machine Learning domain, then check out our interactive, Artificial Intelligence and Deep Learning with TensorFlow Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:
https://www.tecklearn.com/course/artificial-intelligence-and-deep-learning-with-tensorflow/

Artificial Intelligence and Deep Learning with TensorFlow Training

About the Course

Tecklearn’s Artificial Intelligence and Deep Learning with Tensor Flow course is curated by industry professionals as per the industry requirements & demands and aligned with the latest best practices. You’ll master convolutional neural networks (CNN), TensorFlow, TensorFlow code, transfer learning, graph visualization, recurrent neural networks (RNN), Deep Learning libraries, GPU in Deep Learning, Keras and TFLearn APIs, backpropagation, and hyperparameters via hands-on projects. The trainee will learn AI by mastering natural language processing, deep neural networks, predictive analytics, reinforcement learning, and more programming languages needed to shine in this field.

Why Should you take Artificial Intelligence and Deep Learning with Tensor Flow Training?

• According to Paysa.com, an Artificial Intelligence Engineer earns an average of $171,715, ranging from $124,542 at the 25th percentile to $201,853 at the 75th percentile, with top earners earning more than $257,530.
• Worldwide Spending on Artificial Intelligence Systems Will Be Nearly $98 Billion in 2023, According to New IDC Spending Guide at a GACR of 28.5%.
• IBM, Amazon, Apple, Google, Facebook, Microsoft, Oracle and almost all the leading companies are working on Artificial Intelligence to innovate future technologies.

What you will Learn in this Course?

Introduction to Deep Learning and AI
• What is Deep Learning?
• Advantage of Deep Learning over Machine learning
• Real-Life use cases of Deep Learning
• Review of Machine Learning: Regression, Classification, Clustering, Reinforcement Learning, Underfitting and Overfitting, Optimization
• Pre-requisites for AI & DL
• Python Programming Language
• Installation & IDE
Environment Set Up and Essentials
• Installation
• Python – NumPy
• Python for Data Science and AI
• Python Language Essentials
• Python Libraries – Numpy and Pandas
• Numpy for Mathematical Computing
More Prerequisites for Deep Learning and AI
• Pandas for Data Analysis
• Machine Learning Basic Concepts
• Normalization
• Data Set
• Machine Learning Concepts
• Regression
• Logistic Regression
• SVM – Support Vector Machines
• Decision Trees
• Python Libraries for Data Science and AI
Introduction to Neural Networks
• Creating Module
• Neural Network Equation
• Sigmoid Function
• Multi-layered perception
• Weights, Biases
• Activation Functions
• Gradient Decent or Error function
• Epoch, Forward & backword propagation
• What is TensorFlow?
• TensorFlow code-basics
• Graph Visualization
• Constants, Placeholders, Variables
Multi-layered Neural Networks
• Error Back propagation issues
• Drop outs
Regularization techniques in Deep Learning
Deep Learning Libraries
• Tensorflow
• Keras
• OpenCV
• SkImage
• PIL
Building of Simple Neural Network from Scratch from Simple Equation
• Training the model
Dual Equation Neural Network
• TensorFlow
• Predicting Algorithm
Introduction to Keras API
• Define Keras
• How to compose Models in Keras
• Sequential Composition
• Functional Composition
• Predefined Neural Network Layers
• What is Batch Normalization
• Saving and Loading a model with Keras
• Customizing the Training Process
• Using TensorBoard with Keras
• Use-Case Implementation with Keras
GPU in Deep Learning
• Introduction to GPUs and how they differ from CPUs
• Importance of GPUs in training Deep Learning Networks
• The GPU constituent with simpler core and concurrent hardware
• Keras Model Saving and Reusing
• Deploying Keras with TensorBoard
Keras Cat Vs Dog Modelling
• Activation Functions in Neural Network
Optimization Techniques
• Some Examples for Neural Network
Convolutional Neural Networks (CNN)
• Introduction to CNNs
• CNNs Application
• Architecture of a CNN
• Convolution and Pooling layers in a CNN
• Understanding and Visualizing a CNN
RNN: Recurrent Neural Networks
• Introduction to RNN Model
• Application use cases of RNN
• Modelling sequences
• Training RNNs with Backpropagation
• Long Short-Term memory (LSTM)
• Recursive Neural Tensor Network Theory
• Recurrent Neural Network Model
Application of Deep Learning in image recognition, NLP and more
Real world projects in recommender systems and others
Got a question for us? Please mention it in the comments section and we will get back to you.

 

0 responses on "TensorFlow Single and Multiple GPU"

Leave a Message

Your email address will not be published. Required fields are marked *