The Ultimate Guide to Using Stable Diffusion on Linux with AMD GPU

The Ultimate Guide to Using Stable Diffusion on Linux with AMD GPU

Table of Contents

  1. Introduction
  2. Preparing Your Ubuntu System
    • Checking Ubuntu Version
    • Installing Rock M Libraries
    • Rebooting Your System
    • Assigning Groups to Current User
    • Verifying Rock M Installation
  3. Installing Miniconda
    • Downloading Miniconda
    • Installing Miniconda
    • Creating a Python Environment
  4. Installing PyTorch and Rock M
    • Installing PyTorch
    • Setting Environment Variables
    • testing PyTorch Installation
  5. Setting Up Git
    • Installing Git
    • Checking Git Version
  6. Installing Transformers and Accelerate
    • Upgrading Transformers
    • Installing Accelerate
  7. Testing Stable Diffusion
    • Launching Python Console
    • Importing Torch and Diffusion
    • Loading the Model
    • Generating an Image
    • Saving and Displaying the Image
  8. Conclusion

Installing Stable Diffusion on Linux (Ubuntu)

In this step-by-step guide, we will walk you through the process of installing and running Stable Diffusion on Linux (Ubuntu) using the Rock M Package. Stable Diffusion is a powerful tool for deep learning with AMD GPUs, similar to Nvidia's Cuda package. By following this guide, you will be able to harness the full potential of your AMD GPU. So let's get started!

2. Preparing Your Ubuntu System

Before we begin, let's make sure that your Ubuntu system is ready for Stable Diffusion. Please follow the steps below:

Checking Ubuntu Version

The first thing you need to do is check your Ubuntu version. The Rock M libraries are only available for specific Ubuntu versions. Open your terminal and type the following command to check your Ubuntu version:

lsb_release -a

You will see the Ubuntu version displayed on the output. Make sure it is one of the following supported versions: 20.04 LTS or 22.04.

Installing Rock M Libraries

To install the Rock M libraries, we need to install a few packages first. Open your terminal and run the following command to update your system and install the necessary packages:

sudo apt-get update
sudo apt-get install wget gpg2 gawk -y

After the installation completes, you can proceed to install the Rock M libraries.

Rebooting Your System

Once the Rock M libraries are installed, it is recommended to reboot your system to ensure that the changes take effect. You can reboot your system by running the following command:

sudo reboot

After the reboot, you can proceed to the next step.

Assigning Groups to Current User

To enable stable diffusion on your GPU, your current user needs to be a member of the "render" and "video" groups. To check if your user is already a member of these groups, run the following commands:

groups $(whoami)

If your user is not a member of the "render" group, run the following command to add it:

sudo usermod -aG render $(whoami)

Similarly, if your user is not a member of the "video" group, run the following command:

sudo usermod -aG video $(whoami)

After adding your user to the necessary groups, you need to reboot your system once again for the changes to take effect.

Verifying Rock M Installation

After the reboot, open your terminal and run the following command to check if the Rock M libraries are successfully installed:

rocminfo

You should see the information about your AMD GPU displayed on the output.

3. Installing Miniconda

Miniconda is a lightweight version of Anaconda, a popular Python distribution. We recommend installing Miniconda to manage Python environments for Stable Diffusion. Follow the steps below to install Miniconda:

Downloading Miniconda

Go to the Miniconda website (https://docs.conda.io/en/latest/miniconda.html) and download the Linux installer for Python 3.9.

Once the installer is downloaded, open your terminal and navigate to the directory where the installer is located.

Installing Miniconda

To install Miniconda, run the following command in your terminal:

bash Miniconda3-latest-Linux-x86_64.sh

Follow the prompts to complete the installation. Make sure to agree to the license terms and choose the default installation options.

Creating a Python Environment

After installing Miniconda, you can create a Python environment specifically for Stable Diffusion. Open your terminal and run the following command to create the environment:

conda create -n stable-diffusion python=3.9

You can replace "stable-diffusion" with your preferred environment name. Press "y" when prompted to proceed with the installation.

Once the environment is created, activate it by running the following command:

conda activate stable-diffusion

Your Python environment is now set up and ready for Stable Diffusion.

4. Installing PyTorch and Rock M

In this step, we will install PyTorch and the Rock M package to enable Stable Diffusion on your Linux system.

Installing PyTorch

To install PyTorch, ensure that your stable-diffusion environment is activated. Run the following command in your terminal:

pip install torch==1.9.1+rocm5.2 -f https://download.pytorch.org/whl/torch_stable.html

This command will install PyTorch with the Rock M 5.2 version. Please note that the Rock M version may be different from the one Mentioned here, but it should still work.

Setting Environment Variables

Next, we need to set an environment variable to ensure the proper usage of Rock M. Run the following command in your terminal:

export HSA_TOOLS_LIB=libhsakmt.so.1.0.0

This command will set the necessary environment variable for Rock M to function correctly.

Testing PyTorch Installation

To verify that PyTorch is installed correctly, run the following command in your terminal:

python -c "import torch; print(torch.__version__)"

You should see the PyTorch version displayed on the output.

5. Setting Up Git

To access the Stable Diffusion repository and easily manage code, we recommend installing Git on your system.

Installing Git

Git is officially supported by Ubuntu, making it easy to install. Open your terminal and run the following command:

sudo apt-get update
sudo apt-get install git -y

After the installation is complete, you can verify the Git version by running the following command:

git --version

You should see the Git version displayed on the output.

6. Installing Transformers and Accelerate

To complete the setup, we need to install the Transformers and Accelerate packages, which are essential for Stable Diffusion.

Upgrading Transformers

To upgrade Transformers to the latest version, run the following command in your terminal:

pip install --upgrade transformers

This command will ensure that you have the most recent version of Transformers installed.

Installing Accelerate

To install Accelerate, run the following command in your terminal:

pip install accelerate

Accelerate is a high-level library built on top of PyTorch that optimizes training and inference processes, providing faster performance.

7. Testing Stable Diffusion

Now that everything is set up, let's test Stable Diffusion to ensure it works correctly.

Launching Python Console

Open your terminal and run the following command to launch the Python console:

python

Importing Torch and Diffusion

In the Python console, import the necessary packages by running the following commands:

import torch
import diffusion

Loading the Model

To load a pre-trained Stable Diffusion Model, use the following command:

model_path = "path/to/your/model"
model = diffusion.load_model(model_path)

Make sure to replace "path/to/your/model" with the path to your own Stable Diffusion model.

Generating an Image

To generate an image using Stable Diffusion, use the following command:

Prompt = "Your prompt goes here"
image = diffusion.generate_image(model, prompt, dtype=torch.float16)

Replace "Your prompt goes here" with the text prompt you want to use for image generation.

Saving and Displaying the Image

To save the generated image, use the following command:

image.save("path/to/save/image.png")

Replace "path/to/save/image.png" with the desired path and image name.

To display the image, you can use any image viewer compatible with your system.

8. Conclusion

Congratulations! You have successfully installed and run Stable Diffusion on your Linux (Ubuntu) system. With Stable Diffusion, you can now generate high-quality images using your AMD GPU. Enjoy exploring the possibilities of Stable Diffusion and feel free to share your results and ask any questions in the comments. Happy generating!

Pros:

  • Detailed step-by-step guide for installing Stable Diffusion on Linux (Ubuntu)
  • Clear instructions and commands provided for each step
  • Includes necessary prerequisites like installing Miniconda and setting up Git
  • Covers testing Stable Diffusion with a pre-trained model
  • Provides guidance on saving and displaying the generated images

Cons:

  • Could benefit from additional explanations or examples for some commands
  • Some steps may require more technical knowledge, especially for beginners

Highlights:

  • Comprehensive guide for installing and running Stable Diffusion on Linux (Ubuntu)
  • Covers system preparation, Miniconda installation, PyTorch and Rock M setup, Git installation, and package installation
  • Includes testing instructions and tips for saving and displaying generated images
  • Offers clear, concise steps for users to follow with confidence

FAQ

Q: Can I install Stable Diffusion on other Linux distributions besides Ubuntu? A: Stable Diffusion is specifically designed for Ubuntu. While it may be possible to install it on other Linux distributions, it is recommended to use Ubuntu for optimal compatibility.

Q: Do I need an AMD GPU to run Stable Diffusion? A: Yes, Stable Diffusion is specifically designed to work with AMD GPUs. Nvidia GPUs use the Cuda package instead.

Q: Can I use Stable Diffusion with other deep learning frameworks besides PyTorch? A: Stable Diffusion is primarily built for PyTorch. It is recommended to use PyTorch for optimal compatibility and performance.

Q: How long does it typically take to generate an image with Stable Diffusion? A: The generation time can vary depending on the complexity of the model and the specifications of your GPU. However, with the Rock M package and an AMD GPU, the generation time can be significantly reduced compared to other setups.

Q: Are there any additional resources or tutorials available for Stable Diffusion? A: Yes, you can find more resources and tutorials on the official Stable Diffusion documentation website (https://diffusion.ai/).

Find AI tools in Toolify

Join TOOLIFY to find the ai tools

Get started

Sign Up
App rating
4.9
AI Tools
20k+
Trusted Users
5000+
No complicated
No difficulty
Free forever
Browse More Content