🐶
Tensorflow

Keras & TensorFlow with AMD GPU: A Guide

By Ondřej Dolanský on 12/17/2024

Unlock the power of your AMD GPU for deep learning by leveraging Keras and TensorFlow for faster training and efficient model development.

Keras & TensorFlow with AMD GPU: A Guide

Table of Contents

Introduction

To harness the power of your AMD GPU for deep learning tasks with TensorFlow or Keras, you'll need to set up ROCm, AMD's open-source platform for GPU computing. This guide provides a concise walkthrough to get you started.

Step-by-Step Guide

To run TensorFlow or Keras on an AMD GPU, you need to use ROCm, AMD's open-source software platform for GPU computing.

  1. Install ROCm. Follow the instructions on the AMD ROCm website to install the correct version for your system.

    # Example ROCm installation command
    sudo apt install rocm-dkms-hip-kernels
  2. Install TensorFlow for ROCm. Use the provided wheel file for a streamlined installation.

    # Example TensorFlow for ROCm installation command
    pip install tensorflow-rocm
  3. Verify your installation. Run a simple TensorFlow code snippet to check if your GPU is detected.

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

If the output shows your AMD GPU, you're all set! If not, double-check your ROCm and TensorFlow installations.

Code Example

This code provides a step-by-step guide to install and verify TensorFlow for use with an AMD GPU using the ROCm platform. It first instructs on installing ROCm based on the user's Linux distribution, directing to the official AMD ROCm documentation for detailed instructions. Next, it guides the installation of the TensorFlow wheel file specifically designed for ROCm. Finally, it provides a Python script to verify the installation. This script checks and prints the TensorFlow version, confirms if it's built with ROCm, lists available GPUs, and provides information about physical and logical GPUs. The successful output should show the AMD GPU and confirm ROCm usage, indicating a successful TensorFlow installation configured for the AMD GPU.

This code example demonstrates how to install and verify TensorFlow for use with an AMD GPU using ROCm.

1. Install ROCm

First, you need to install ROCm. The specific installation command may vary depending on your Linux distribution and the ROCm version you need.

For Ubuntu/Debian:

sudo apt update
sudo apt install rocm-dkms-hip-kernels

For other distributions:

Refer to the official AMD ROCm documentation for detailed installation instructions: https://rocm.docs.amd.com/

2. Install TensorFlow for ROCm

Once ROCm is installed, you can install the TensorFlow wheel file specifically built for ROCm:

pip install tensorflow-rocm

3. Verify your installation

Finally, verify that TensorFlow can detect your AMD GPU by running a simple script:

import tensorflow as tf

# Check if TensorFlow is using ROCm
print("TensorFlow version:", tf.__version__)
print("Is built with CUDA:", tf.test.is_built_with_cuda())
print("Is built with ROCm:", tf.test.is_built_with_rocm())

# List available GPUs
gpus = tf.config.list_physical_devices('GPU')
print("Num GPUs Available: ", len(gpus))

if gpus:
  try:
    # Try to make sure that TensorFlow allocates all GPU memory
    for gpu in gpus:
      tf.config.experimental.set_memory_growth(gpu, True)
    logical_gpus = tf.config.list_logical_devices('GPU')
    print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
  except RuntimeError as e:
    # Virtual devices must be set before GPUs have been initialized
    print(e)

This script will print:

  • The installed TensorFlow version.
  • Whether TensorFlow is built with CUDA or ROCm.
  • The number of available GPUs.
  • Information about physical and logical GPUs.

If the output shows your AMD GPU and confirms ROCm usage, you have successfully installed and configured TensorFlow to run on your AMD GPU!

Additional Notes

  • ROCm (Radeon Open Compute Platform) is crucial for running TensorFlow or Keras on AMD GPUs. It serves as the counterpart to NVIDIA's CUDA platform.
  • Compatibility is key: Ensure you install the correct ROCm version compatible with your specific AMD GPU and Linux distribution. The AMD ROCm website provides comprehensive compatibility information.
  • TensorFlow-ROCm Wheel File: Opt for the tensorflow-rocm wheel file for a streamlined installation process tailored for AMD GPUs.
  • Verification is essential: After installation, always verify that TensorFlow recognizes your AMD GPU using the provided code snippet. This step confirms a successful setup.
  • Troubleshooting: If your GPU isn't detected, meticulously double-check your ROCm installation, ensuring all dependencies are met, and verify the TensorFlow installation.
  • Community Support: Online forums and communities dedicated to ROCm and TensorFlow on AMD GPUs can be invaluable resources for troubleshooting and seeking assistance.
  • Performance Optimization: Explore ROCm's performance tuning options to optimize your deep learning workloads on AMD GPUs.
  • Keep Updated: Regularly check for updates to ROCm and TensorFlow to benefit from the latest performance improvements and bug fixes.
  • Alternatives to ROCm: While ROCm is the recommended approach, you can explore alternative methods like compiling TensorFlow from source with OpenCL support, but this method can be more complex.
  • AMD GPU Ecosystem: AMD's commitment to the deep learning community is evident through its continuous development and support for ROCm, making AMD GPUs a viable option for deep learning tasks.

Summary

Step Action Details
1 Install ROCm Download and install the appropriate ROCm version for your system from the AMD ROCm website.
2 Install TensorFlow for ROCm Install the tensorflow-rocm package using pip.
3 Verify Installation Run a simple TensorFlow script to confirm your AMD GPU is recognized.

Conclusion

By following these steps, you can effectively utilize the computational power of AMD GPUs for your deep learning projects in TensorFlow and Keras. Remember to consult the official ROCm documentation for the most up-to-date information and compatibility details. With the right setup, AMD GPUs can be valuable assets in accelerating your deep learning endeavors.

References

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait