
TensorFlow can be used with AMD GPUs, but it requires some extra setup.
To get started, you'll need to install the ROCm (Radeon Open Compute) software, which allows TensorFlow to communicate with your AMD GPU.
This is a key step, as ROCm provides the necessary drivers and tools for TensorFlow to run on AMD hardware.
With ROCm installed, you can then use the TensorFlow ROCm build, which is specifically designed for use with AMD GPUs.
This build includes optimizations for AMD hardware and is a more efficient way to run TensorFlow on your system.
PyTorch is another popular deep learning framework that can also be used with AMD GPUs.
Check this out: Does Tensorflow Automatically Use Gpu
Installing ROCm
To install ROCm, start by running the following code to ensure your system is up to date: `sudo apt update`, `sudo apt dist-upgrade`, `sudo apt install libnuma-dev`, and `sudo reboot`. This will update your system and install the necessary libraries for ROCm.
You'll then need to add the ROCm apt repository by running `wget -q-O - http://repo.radeon.com/rocm/apt/debian/rocm.gpg.key | sudo apt-key add -`, followed by `echo 'deb [arch=amd64] http://repo.radeon.com/rocm/apt/debian/ xenial main' | sudo tee /etc/apt/sources.list.d/rocm.list`.
Recommended read: Tensorflow and Add
Here are the steps to install ROCm on Ubuntu in detail:
- Run the following code to ensure that your system is up to
- sudo apt update
- sudo apt dist-upgrade
- sudo apt install libnuma-dev
- sudo reboot
Add the ROCm apt repository:
- wget -q-O - http://repo.radeon.com/rocm/apt/debian/rocm.gpg.key | sudo apt-key add -
- echo 'deb [arch=amd64] http://repo.radeon.com/rocm/apt/debian/ xenial main' | sudo tee /etc/apt/sources.list.d/rocm.list
Install the ROCm meta-package:
- sudo apt update
- sudo apt install rocm-dkms
Set permissions:
- groups
- sudo usermod -a-G video $LOGNAME
- echo 'ADD_EXTRA_GROUPS=1' | sudo tee -a /etc/adduser.conf
- echo 'EXTRA_GROUPS=video' | sudo tee -a /etc/adduser.conf
Restart the systemVerify ROCm installation is successful:
- /opt/rocm/bin/rocminfo
- /opt/rocm/opencl/bin/x86_64/clinfo
Note: To run the ROCm programs more efficiently, add the ROCm binaries in your PATH by running `echo 'export PATH=$PATH:/opt/rocm/bin:/opt/rocm/profiler/bin:/opt/rocm/opencl/bin/x86_64' | sudo tee -a /etc/profile.d/rocm.sh`.
Install PyTorch on ROCm
To install PyTorch on ROCm, follow the instructions on the official ROCm PyTorch website.
If you're looking for a more general guide, you can also install PyTorch or TensorFlow on ROCm, but the official website is the recommended resource.
The ROCm ecosystem is maturing, and more GitHub codes will eventually contain ROCm/HIPified ports, so keep an eye out for those.
You can expect future posts to AMD lab notes to discuss the specifics of porting from CUDA to HIP, as well as guides to running popular community models from HuggingFace.
You might like: Pytorch and Tensorflow Interview Questions
Install ROCm on Ubuntu
To install ROCm on Ubuntu, you'll need to start by updating your system. This ensures you have the latest software packages and updates installed. Run the following code to update your system:
1. `sudo apt update`
2. `sudo apt dist-upgrade`
3. `sudo apt install libnuma-dev`
4. `sudo reboot`
After your system reboots, you can proceed with adding the ROCm apt repository. This is necessary for installing ROCm packages.
To add the ROCm apt repository, run the following code:
1. `wget -q-O - http://repo.radeon.com/rocm/apt/debian/rocm.gpg.key | sudo apt-key add -`
2. `echo 'deb [arch=amd64] http://repo.radeon.com/rocm/apt/debian/ xenial main' | sudo tee /etc/apt/sources.list.d/rocm.list`
Once the repository is added, you can install the ROCm meta-package. This will install the necessary dependencies and packages for ROCm.
To install the ROCm meta-package, run the following code:
1. `sudo apt update`
2. `sudo apt install rocm-dkms`
With the ROCm meta-package installed, you'll need to set permissions for your user account. This is necessary for running ROCm programs.
To set permissions, run the following code:
1. `groups`
2. `sudo usermod -a-G video $LOGNAME`
3. `echo 'ADD_EXTRA_GROUPS=1' | sudo tee -a /etc/adduser.conf`
4. `echo 'EXTRA_GROUPS=video' | sudo tee -a /etc/adduser.conf`
For another approach, see: Generative Ai with Python and Tensorflow 2
After setting permissions, you'll need to restart your system to apply the changes.
To verify that ROCm is installed successfully, run the following commands:
1. `/opt/rocm/bin/rocminfo`
2. `/opt/rocm/opencl/bin/x86_64/clinfo`
If ROCm is installed correctly, these commands should display information about your ROCm installation.
To run ROCm programs more efficiently, you can add the ROCm binaries to your system's PATH. To do this, run the following code:
1. `echo 'export PATH=$PATH:/opt/rocm/bin:/opt/rocm/profiler/bin:/opt/rocm/opencl/bin/x86_64' | sudo tee -a /etc/profile.d/rocm.sh`
Setting Up Environment
To get started with TensorFlow on your AMD GPU, you'll need to set up your environment correctly. Make sure your system is up to date by running the following commands in Terminal.
Before you begin, it's essential to install the dependency libnuma-dev for ROCm. This will ensure that your system has the necessary components to support ROCm.
Once libnuma-dev is installed, add the official ROCm repos to apt. This will provide your system with the necessary packages to install the ROCm kernel.
Suggestion: How to Install Tensorflow in Cmd
To install the ROCm kernel, follow the instructions provided by the ROCm documentation. You'll also need to open /etc/udev/rules.d/70-kfd.rules and add the necessary lines to enable GPU support.
In addition to the ROCm kernel, you'll also need to install libtinfo5. This package provides the necessary libraries for GPU support.
To verify that your installation was successful, run the commands to test your installation. If everything is set up correctly, you should see a list of supported GPUs installed on your system.
Before you start working with TensorFlow, create a virtualenv using python3. This will ensure that your environment is isolated and won't interfere with your system's packages.
Here's a summary of the steps you need to follow:
- Install libnuma-dev for ROCm
- Add the official ROCm repos to apt
- Install the ROCm kernel
- Open /etc/udev/rules.d/70-kfd.rules and add the necessary lines
- Install libtinfo5
- Test your installation
- Create a virtualenv using python3
GPU Support
GPU Support is a bit of a limitation for ROCm, but don't worry, I've got you covered.
Not all AMD GPUs can support ROCm, and as of now, only Vega, Polaris, Fiji, and Hawaii GPUs are supported.
I've personally used a Radeon VII GPU for this setup process, and it works like a charm.
You can check the full list of supported GPUs here, but unfortunately, Navi isn't included, and the reason is still a mystery!
To get started, you'll need to add ROCm binaries to your path, regardless of whether you use bash or zsh.
Operating System Support
TensorFlow on AMD GPU requires a specific operating system to work efficiently.
Currently, only Linux is supported for TensorFlow on AMD GPU.
The good news is that AMD has provided builds for popular Linux distributions, making it easier to get started.
Ubuntu, RHEL, and CentOS are the only distributions supported by AMD for now.
If you're planning to set up TensorFlow on Ubuntu, you're in luck, as it's one of the supported distributions.
Working with ROCm
Working with ROCm is a crucial step in setting up TensorFlow for AMD GPU. You can install ROCm on Ubuntu by running a series of commands, including updating the system, adding the ROCm apt repository, and installing the ROCm meta-package.
To verify that ROCm is installed successfully, you can run the `rocminfo` and `clinfo` commands. If you want to run ROCm programs more efficiently, you can add the ROCm binaries to your PATH by adding a line to your `/etc/profile.d/rocm.sh` file.
Here are the basic steps to install ROCm on Ubuntu:
- Update your system: `sudo apt update` and `sudo apt dist-upgrade`
- Add the ROCm apt repository: `wget -q-O - http://repo.radeon.com/rocm/apt/debian/rocm.gpg.key | sudo apt-key add -` and `echo 'deb [arch=amd64] http://repo.radeon.com/rocm/apt/debian/ xenial main' | sudo tee /etc/apt/sources.list.d/rocm.list`
- Install the ROCm meta-package: `sudo apt update` and `sudo apt install rocm-dkms`
- Set permissions: `groups`, `sudo usermod -a-G video $LOGNAME`, `echo 'ADD_EXTRA_GROUPS=1' | sudo tee-a /etc/adduser.conf`, and `echo 'EXTRA_GROUPS=video' | sudo tee-a /etc/adduser.conf`
Note: After installing ROCm, you need to restart your system.
Clone Git Source Code
To get started with working on ROCm, you'll need to clone the source code you want to run. This is the first step in setting up your code environment.
You can use Git to clone the source code, which is a version control system that helps you manage and track changes to your code.
The article mentions that more GitHub codes will eventually contain ROCm/HIPified ports, which means you may find pre-ported versions of the code you need.
Working with Docker Images
You can use a Docker image with TensorFlow pre-installed by following the recommended option through Docker. This provides portability and access to a prebuilt Docker image that has been rigorously tested within AMD.
A unique perspective: Tensorflow Docker
To get started, pull the latest public TensorFlow Docker image by running the command: `docker pull rocm/tensorflow:latest`.
Using Docker can save compilation time and mitigate potential installation issues. It also allows you to run a TensorFlow environment with just a few commands.
To run the Docker image, use the command: `docker run -it --network=host --device=/dev/kfd --device=/dev/dri --group-add video --cap-add=SYS_PTRACE --security-opt seccomp=unconfined rocm/tensorflow:latest`.
Additional software components required for TensorFlow include Ubuntu 24.04 and Tensorboard 2.19.0.
Find HIPified Library Source Code
If you're having trouble finding a HIPified library, you can search online or on GitHub for the library name followed by "ROCm". This often yields results.
Searching online for "library_name" + "ROCm" can be a straightforward way to find HIPified code. For instance, searching for "bitsandbytes ROCm" led to a fork that added ROCm support with a HIP compilation target.
You may need to adjust the installation location, as it may include the version number, such as /opt/rocm-5.5.0.
Running Code
If you can run your code without problems, then you have successfully created a code environment on AMD GPUs.
Running your code is a straightforward process. If it runs smoothly, it's a good sign that everything is set up correctly.
If not, it may be due to the additional packages in requirements.txt depending on CUDA, which needs to be HIPified to run on AMD GPUs.
You can try checking your requirements.txt file to see if it includes any packages that rely on CUDA. If it does, you'll need to take steps to HIPify them.
If you're able to run your code without issues, that's a great accomplishment!
Discover more: 파이썬 Tensorflow Cuda 버전
Featured Images: pexels.com

