Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Kaapiii/9ee7680a8b387f55ed564e25fa26e8b0 to your computer and use it in GitHub Desktop.
Save Kaapiii/9ee7680a8b387f55ed564e25fa26e8b0 to your computer and use it in GitHub Desktop.
Use Fooocus with AMD Radeon 7900 XT / 7900 XTX on Ubuntu 22.04 LTS

Use Fooocus with AMD Radeon 7900 XT / 7900 XTX on Ubuntu 22.04 LTS

This document contains the setup instructions for running the image-generating software Fooocus with AMD GPUs on Ubuntu 22.04 LTS. The original instructions are scattered in different places, and it was a pain to find all the required pieces and configure them correctly. So here is a guide.

TOC

  1. Prerequisites
  2. Install ROCm Drivers
  3. Verify Driver Installation
  4. Clone Fooocus
  5. Install compatible PyTorch Versions

Prerequisites for 22.04 LTS

  • Latest Ubuntu LTS version (22.04.3) installed

  • Installed Radeon 7900 XT / XTX

  • Python Version 3.10 (I've not tested Python v3.11)

  • Check that ROCm (Readon Open Compute platform) user groups are set up correctly. ROCm instructions

    Add the current user to the listed groups

    sudo usermod -a -G render,video $LOGNAME

    Check User Groups and Permissions

    groups
    # youruser adm cdrm sudo render video ...
  • Disable the iGPU (integrated GPU in the CPU). All newer AMD Ryzen 7000 CPUs have integrated iGPUs. The GPU needs to be disabled in the UEFI/Bios. On most of the mainboards the setting can be found under:

    "Settings" -> "AMD CBS" -> "NBIO (Common Options)" -> "GFX configuration" -> "iGPU" and set it to "Disabled".

Install the Newest ROCm 5.7 Driver

For newer versions, follow the original instructions for installing ROCm via Package Manager Ignore the other installation options "AMDGPU". It will get you in trouble.

The instructions are issued with the ROCm version 5.7.1 and copied straight from the ROCm Linux Quick Start Guide.

Add Package Signing Keys

# Make the directory if it doesn't exist yet.
# This location is recommended by the distribution maintainers.
sudo mkdir --parents --mode=0755 /etc/apt/keyrings
# Download the key, convert the signing-key to a full
# keyring required by apt and store in the keyring directory
wget https://repo.radeon.com/rocm/rocm.gpg.key -O - | \
    gpg --dearmor | sudo tee /etc/apt/keyrings/rocm.gpg > /dev/null

Define a ROCm Version

# version
ver=5.7.1

Add Package Repositories

# Kernel driver repository for jammy
sudo tee /etc/apt/sources.list.d/amdgpu.list <<'EOF'
deb [arch=amd64 signed-by=/etc/apt/keyrings/rocm.gpg] https://repo.radeon.com/amdgpu/$ver/ubuntu jammy main
EOF
# ROCm repository for jammy
sudo tee /etc/apt/sources.list.d/rocm.list <<'EOF'
deb [arch=amd64 signed-by=/etc/apt/keyrings/rocm.gpg] https://repo.radeon.com/rocm/apt/debian jammy main
EOF
# Prefer packages from the rocm repository over system packages
echo -e 'Package: *\nPin: release o=repo.radeon.com\nPin-Priority: 600' | sudo tee /etc/apt/preferences.d/rocm-pin-600

Update Package List

sudo apt update

Install Drivers and Runtimes

sudo apt install amdgpu-dkms
sudo apt install rocm-hip-libraries
sudo reboot

Verify Driver Installation

Run the post-install checks original Instructions.

Check if the amdgpu kernel driver is installed.

dkms status
# amdgpu/6.2.4-1664922.22.04, 6.2.0-37-generic, x86_64: installed

Check if your GPU is recognized as an agent/device.

rocminfo

Output: (The important part is the "Agent 2")

ROCk module is loaded
=====================    
HSA System Attributes    
=====================    
Runtime Version:         1.1
System Timestamp Freq.:  1000.000000MHz
Sig. Max Wait Duration:  18446744073709551615 (0xFFFFFFFFFFFFFFFF) (timestamp count)
Machine Model:           LARGE                              
System Endianness:       LITTLE                             
Mwaitx:                  DISABLED
DMAbuf Support:          YES

==========               
HSA Agents               
==========               
*******                  
Agent 1                  
*******   
  Name:                    AMD Ryzen 9 7900X3D 12-Core Processor
  ....

*******                  
Agent 2                  
*******                  
  Name:                    gfx1100                            
  Uuid:                    GPU-c444df7f5062ac2e               
  Marketing Name:          Radeon RX 7900 XT                  
  Vendor Name:             AMD                                
  Feature:                 KERNEL_DISPATCH                    
  Profile:                 BASE_PROFILE                       
  Float Round Mode:        NEAR                

Check if GPU Is Listed

The original Instructions lists an additional check with "clinfo". If this check does NOT return anything, the installation can still work.

In my case, the "clinfo" listed the following.

sudo apt install clinfo && info
# Number of platforms                               0

Clone Fooocus

Clone Foccous from the Github Repository.

The installation is done using Python Venv. Install dependencies if needed.

sudo apt install python3.10-venv python3-pip git -y

Clone Repository

Clone the repo to the desired destination directory.

git clone https://github.com/lllyasviel/Fooocus.git
cd Fooocus
python3 -m venv fooocus_env
source fooocus_env/bin/activate
pip install -r requirements_versions.txt

Important: As of the time of writing, custom PyTorch installation instructions for "Linux (AMD GPUs)" from the Fooocus docs are not accurate and should be ignored.

Install the tested PyTorch Version from ROCm

Official Pytorch installation documentation (ROCm Version) can be found here.

Update wheel

pip3 install --upgrade pip wheel

Install Torch & Torchvision for ROCm AMD GPUs

wget https://repo.radeon.com/rocm/manylinux/rocm-rel-5.7/torch-2.0.1%2Brocm5.7-cp310-cp310-linux_x86_64.whl
wget https://repo.radeon.com/rocm/manylinux/rocm-rel-5.7/torchvision-0.15.2%2Brocm5.7-cp310-cp310-linux_x86_64.whl
pip3 install --force-reinstall torch-2.0.1+rocm5.7-cp310-cp310-linux_x86_64.whl torchvision-0.15.2+rocm5.7-cp310-cp310-linux_x86_64.whl 

Verify PyTorch Installation

Instructions were taken from here

Check if PyTorch detects the GPU

python3 -c 'import torch' 2> /dev/null && echo 'Success' || echo 'Failure'
# Expected ouput: Success

Check if GPU is available for PyTorch

python3 -c 'import torch; print(torch.cuda.is_available())'
# Expected output: True

Display installed GPU device name

python3 -c "import torch; print(f'device name [0]:', torch.cuda.get_device_name(0))"
# Example output: device name [0]: Radeon RX 7900 XT

Display PyTorch environment details

python3 -m torch.utils.collect_env

Run Fooocus

Go to your Fooocus git directory and run:

source fooocus_env/bin/activate
python3 entry_with_update.py --preset realistic

Troubleshooting

Error After First Run

If your first image generation fails and you face this error, ...

RuntimeError: HIP error: invalid device function
HIP kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect.
For debugging consider passing HIP_LAUNCH_BLOCKING=1.
Compile with `TORCH_USE_HIP_DSA` to enable device-side assertions.

... the Fooocus has chosen the wrong GPU, which doesn't contain any CUDA cores. Lukily there is a flag, so Fooocus can be instructed to use the correct one.

Fix "HIP error: invalid device function"

In order to find the correct flag value, we need to rerun the "rocinfo" command:

rocinfo

This will produce a similar output (Ouput was shortened):

ROCk module is loaded
=====================    
HSA System Attributes    
=====================    
Runtime Version:         1.1
...

==========               
HSA Agents               
==========               
*******                  
Agent 1                  
*******                  
  Name:                    AMD Ryzen 9 7900X3D 12-Core Processor                    
  ...
  
*******                  
Agent 2                  
*******                  
  Name:                    gfx1100                            
  ...

*******                  
Agent 3                  
*******                  
  Name:                    gfx1036                            
  ...
  
*** Done *** 

Note the agent names. In order to force Fooocus to use the GPU agent 2, we need to set the flag HSA_OVERRIDE_GFX_VERSION.

Name: gfx1100 translates to "11.0.0" Name: gfx1036 translates to "10.3.6" ... (other example) ... Name: gfx1030 translates to "10.3.0"

Run Fooocus With a Specifc GPU Agent.

HSA_OVERRIDE_GFX_VERSION=<gfx-name-translation> python3 entry_with_update.py --preset realistic

Example Preset "realistic":

HSA_OVERRIDE_GFX_VERSION=11.0.0 python3 entry_with_update.py --preset realistic

Example Preset "anime":

HSA_OVERRIDE_GFX_VERSION=11.0.0 python entry_with_update.py --preset anime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment