Skip to content

Instantly share code, notes, and snippets.

@geerlingguy
Last active March 15, 2024 06:52
  • Star 21 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save geerlingguy/ff3c3cbcf4416be2c0c1e0f836a8183d to your computer and use it in GitHub Desktop.
Install Stable Diffusion on an AMD GPU PC running Ubuntu 20.04
# Note: This will only work on Navi21 GPUs (6800/6900+).
# See: https://github.com/RadeonOpenCompute/ROCm/issues/1668#issuecomment-1043994570
# Install Conda (latest from https://docs.conda.io/en/latest/miniconda.html#linux-installers)
wget https://repo.anaconda.com/miniconda/Miniconda3-py39_4.12.0-Linux-x86_64.sh
bash Miniconda3-py39_4.12.0-Linux-x86_64.sh
# follow the prompts to install it, and run `conda` to make sure it's working.
# Install git and curl, and clone the stable-diffusion repo
sudo apt install -y git curl
cd Downloads
git clone https://github.com/CompVis/stable-diffusion.git
# Install dependencies and activate environment
cd stable-diffusion
conda env create -f environment.yaml
conda activate ldm
# Download Stable Diffusion weights
curl https://www.googleapis.com/storage/v1/b/aai-blog-files/o/sd-v1-4.ckpt?alt=media > sd-v1-4.ckpt
# Symlink the weights into place
mkdir -p models/ldm/stable-diffusion-v1/
ln -s -r sd-v1-4.ckpt models/ldm/stable-diffusion-v1/model.ckpt
# Install AMD ROCm support
wget https://repo.radeon.com/amdgpu-install/22.10/ubuntu/focal/amdgpu-install_22.10.50100-1_all.deb
sudo apt-get install ./amdgpu-install_22.10.50100-1_all.deb
sudo amdgpu-install --usecase=dkms,graphics,rocm,lrt,hip,hiplibsdk
# make sure you see your GPU by running rocm-smi
# Make AMD GPU work with ROCm
cd stable-diffusion/
conda remove cudatoolkit -y
pip3 uninstall torch torchvision -y
# Install PyTorch ROCm
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/rocm5.1.0
pip3 install transformers==4.19.2 scann kornia==0.6.4 torchmetrics==0.6.0
# Generate an image
python scripts/txt2img.py --prompt "a photograph of an astronaut riding a horse" --plms
@MHBZHY
Copy link

MHBZHY commented Oct 16, 2022

perhaps macOS compatible script can help https://github.com/dylancl/stable-diffusion-webui-mps/blob/master/setup_mac.sh
I found them very similar about installing miniconda and PyTorch , but I don't have enough time to test this.

@insat-
Copy link

insat- commented Oct 20, 2022

Hi @clasen, were you able to make it work with a 6700 XT? I've 32gb of RAM, so might give it a try.

Based @kcnqwe tip to HSA_OVERRIDE_GFX_VERSION=10.3.0 in front of python scripts/txt2img.py

Could this override have some risk of ruining the GPU?

@mgturner
Copy link

mgturner commented Oct 24, 2022

Hi, thanks for this. When I run the final python code to generate the image I get the following errors...

Traceback (most recent call last):
  File "scripts/txt2img.py", line 344, in <module>
    main()

  File "scripts/txt2img.py", line 240, in main
    model = load_model_from_config(config, f"{opt.ckpt}")

  File "scripts/txt2img.py", line 50, in load_model_from_config
    pl_sd = torch.load(ckpt, map_location="cpu")

  File "/home/renrut/miniconda3/envs/ldm/lib/python3.8/site-packages/torch/serialization.py", line 713, in load
    return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)

  File "/home/renrut/miniconda3/envs/ldm/lib/python3.8/site-packages/torch/serialization.py", line 920, in _legacy_load
    magic_number = pickle_module.load(f, **pickle_load_args)

_pickle.UnpicklingError: invalid load key, 'A'.

just wondered if you have any pointers on this?

Update
This was caused by the weights link not working, switching to curl https://f004.backblazeb2.com/file/aai-blog-files/sd-v1-4.ckpt > sd-v1-4.ckpt
fixes it

@SkyyySi
Copy link

SkyyySi commented Oct 26, 2022

Please change the file extension from .txt to .sh. It's a shell script, and GitHub will add syntax highlighting based on the extension.

@Jake-Jensen
Copy link

Hi there, thanks for this toutorial.

I have a RIG and I want to try execute stable difusion inside the rig (with x8 AMD 6700xt)

GPU Temp AvgPwr SCLK MCLK Fan Perf PwrCap VRAM% GPU%

0    29.0c  29.0W   1450Mhz  1074Mhz  77.65%  manual  211.0W    0%   0%    
1    30.0c  29.0W   1450Mhz  1074Mhz  77.65%  manual  211.0W    0%   0%    
2    31.0c  29.0W   1450Mhz  1074Mhz  77.65%  manual  211.0W    0%   0%    
3    28.0c  28.0W   1450Mhz  1074Mhz  77.65%  manual  211.0W    0%   0%    
4    31.0c  29.0W   1450Mhz  1074Mhz  77.65%  manual  211.0W    0%   0%    
5    28.0c  28.0W   1450Mhz  1074Mhz  77.65%  manual  211.0W    0%   0%    
6    31.0c  28.0W   1450Mhz  1074Mhz  77.65%  manual  211.0W    0%   0%    
7    30.0c  30.0W   1450Mhz  1074Mhz  77.65%  manual  211.0W    0%   0%    

After follow your instructions, it show me an unexplain error that says "Killed"

(ldm) root@Adam:~/amd/stable-diffusion# python scripts/txt2img.py --prompt "a photograph of an astronaut riding a horse" --plms
Global seed set to 42
Loading model from models/ldm/stable-diffusion-v1/model.ckpt
Global Step: 470000
LatentDiffusion: Running in eps-prediction mode
Killed

I have 8GB of RAM.

thanks,

8GB of RAM? aahaahahaaa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment