TensorFlow
GPU
Machine Learning
Deep Learning
Neural Networks

How do I use TensorFlow GPU?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Using TensorFlow with a GPU can significantly accelerate the performance of machine learning models by leveraging the parallel processing power of modern GPUs. This guide will walk you through the process of configuring TensorFlow to use GPU resources, examining technical aspects, providing examples, and summarizing key points in a table.

Requirements

Before diving into TensorFlow GPU usage, ensure that your system meets the following requirements:

  1. Supported GPU: Confirm that you have an NVIDIA GPU compatible with CUDA-supported applications. Check the NVIDIA website for a list of supported GPUs.
  2. CUDA Toolkit: Install the appropriate version of the CUDA Toolkit. The version must be compatible with the TensorFlow release you plan to use.
  3. CuDNN Library: Install the cuDNN library that matches the CUDA Toolkit version. This library provides efficient implementations for standard neural network operations.
  4. NVIDIA Drivers: Ensure your system has the latest NVIDIA graphics drivers installed for optimal performance and compatibility.
  5. TensorFlow: Make sure you have TensorFlow installed, preferring the GPU version for harnessing CUDA capabilities.

Installation Steps

1. Install NVIDIA Drivers

Download and install the latest NVIDIA drivers from the NVIDIA website. These drivers ensure your GPU is recognized and fully utilized by the operating system.

2. Install CUDA Toolkit

Visit the CUDA Toolkit archive and download the appropriate version. Follow the installation guide provided on the website. Set environment variables for CUDA paths in your shell configuration file (e.g., .bashrc, .zshrc):

bash
export PATH=/usr/local/cuda-11.x/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-11.x/lib64\
                         ${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

3. Install cuDNN

Download the cuDNN library from the NVIDIA Developer site. Extract the files and copy them to the CUDA installation directory. After copying, ensure the LD_LIBRARY_PATH is updated:

bash
export LD_LIBRARY_PATH=/usr/local/cuda/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

4. Install TensorFlow with GPU Support

To install TensorFlow with GPU support, use pip:

bash
pip install tensorflow-gpu

Ensure that you install a version of TensorFlow compatible with your CUDA and cuDNN versions.

Configuring and Running TensorFlow on GPU

Checking GPU Availability in TensorFlow

After installation, confirm that TensorFlow recognizes your GPU. Start a Python environment and run:

python
import tensorflow as tf

print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))

You should see output indicating one or more GPUs are available.

Example: Basic TensorFlow GPU Usage

Here is a simple example illustrating TensorFlow’s ability to utilize a GPU for matrix multiplication:

python
1import tensorflow as tf
2import time
3
4# Create two random matrices
5matrix1 = tf.random.uniform((5000, 5000))
6matrix2 = tf.random.uniform((5000, 5000))
7
8# Function to perform matrix multiplication
9def matrix_multiply():
10    return tf.matmul(matrix1, matrix2)
11
12# Using GPU
13start = time.time()
14gpu_result = tf.function(matrix_multiply)()
15print("Time taken on GPU: ", time.time() - start)
16
17# Disabling the GPU
18tf.config.set_visible_devices([], 'GPU')
19start = time.time()
20cpu_result = tf.function(matrix_multiply)()
21print("Time taken on CPU: ", time.time() - start)
22
23# Re-enable GPU
24tf.config.experimental.set_visible_devices()
25

Configuring TensorFlow for Efficient GPU Usage

Memory Growth

TensorFlow, by default, allocates all memory of the GPU. It can be adjusted to allocate memory as required:

python
1gpus = tf.config.experimental.list_physical_devices('GPU')
2if gpus:
3    try:
4        for gpu in gpus:
5            tf.config.experimental.set_memory_growth(gpu, True)
6    except RuntimeError as e:
7        print(e)

Logical Device Placement

TensorFlow can run operations on multiple GPUs by placing logical devices. Here's an example for splitting operations across multiple GPUs:

python
1strategy = tf.distribute.MirroredStrategy()
2
3with strategy.scope():
4    # Define and compile model, which will be mirrored across available GPUs
5    model = create_model()

Key Points Summary

ComponentDescription
NVIDIA GPURequired for leveraging GPU capabilities in TensorFlow.
CUDA ToolkitMust be installed; ensure correct version compatibility with TensorFlow.
cuDNNProvides optimized implementations for NN operations and should match CUDA version.
NVIDIA DriversLatest drivers are necessary for compatibility and performance.
TensorFlow-GPU PackageTensorFlow version specifically optimized for running on GPUs.
GPU Availability CheckUse tf.config.experimental.list_physical_devices('GPU') to ensure TensorFlow recognizes the GPU.
Memory Growth OptionEnable memory growth to avoid allocation of all GPU memory: tf.config.experimental.set_memory_growth(gpu, True).
Distributed StrategyUse tf.distribute.Strategy to scale operations across multiple devices.

In conclusion, using TensorFlow with a GPU involves setting up the necessary hardware and software requirements, configuring the environment, and leveraging TensorFlow's built-in capabilities for optimal GPU resource utilization. Whether performing simple operations or scaling across multiple GPUs, TensorFlow offers robust tools to harness GPU power effectively.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.