Deploy Stable Diffusion on an AMD CPU instance
This topic shows how to use an Alibaba Cloud AMD CPU ECS instance (g8a) and an Anolis container image to deploy a text-to-image service with the Stable Diffusion model.
Background
Stable Diffusion is a text-to-image latent diffusion model that generates images from text prompts. It works by progressively denoising a random Gaussian noise input to create a target image. Compared to traditional generative models, Stable Diffusion can produce high-fidelity, detailed images without requiring complex training processes or massive datasets. It is now widely used in various fields, including computer vision, digital art, and video games.
Create an ECS instance
Go to the instance creation page.
-
On the instance creation page, configure the parameters to create an ECS instance.
Configure the following key parameters. For information about other parameters, see Create an instance by using the wizard.
-
Instance: Stable Diffusion model inference is memory-intensive. To ensure stable operation, select at least the ecs.g8a.16xlarge instance type (256 GiB of memory).
-
Images: Alibaba Cloud Linux 3.2104 LTS 64-bit.
-
Public IP Address: Select Assign Public IPv4 Address. Set the bandwidth billing method to Pay-by-traffic and the peak bandwidth to 100 Mbps. This helps accelerate model downloads.
-
System Disk: Running Stable Diffusion requires significant storage space for model files. To ensure smooth operation, set the system disk size to at least 100 GiB.
-
-
Add security group rules.
Add an inbound rule to the security group of the ECS instance to allow traffic on ports 22, 443, and 7860 (for WebUI access). For more information, see Add a security group rule.
-
After the instance is created, obtain its public IP address from the ECS instance page.
NoteUse the public IP address to access the WebUI service and generate images.
Create the Docker runtime environment
-
Remotely connect to the ECS instance.
For more information, see Connect to a Linux instance by using Workbench.
-
Install Docker.
For more information, see Install Docker on an Alibaba Cloud Linux 3 instance.
-
Create and run the PyTorch AI container.
The Anolis community provides a rich selection of container images based on Anolis OS, including a PyTorch image optimized for AMD. You can use this image to create a PyTorch runtime environment.
The following commands pull the container image, create a container named
pytorch-amdthat runs in detached mode, and map your home directory to the container to preserve your development files.sudo docker pull registry.openanolis.cn/openanolis/pytorch-amd:1.13.1-23-zendnn4.1 sudo docker run -d -it --name pytorch-amd --net host -v $HOME:/root registry.openanolis.cn/openanolis/pytorch-amd:1.13.1-23-zendnn4.1
Deploy Stable Diffusion
Manual
Step 1: Install required software
-
Enter the container environment.
sudo docker exec -it -w /root pytorch-amd /bin/bashImportantSubsequent commands must be run in the container environment. If you exit unexpectedly, run the preceding command to re-enter. To check if you are in a container, run
cat /proc/1/cgroup | grep docker. If the command returns output, you are in the container. -
Install the software required for deploying Stable Diffusion.
yum install -y git git-lfs wget tmux mesa-libGL gperftools-libs -
Enable Git LFS.
Git LFS is required to download the pre-trained models.
git lfs install
Step 2: Download source code and models
-
Create a tmux session.
tmuxNoteDownloading the pre-trained models can be time-consuming and its success depends on network conditions. Run the download in a tmux session to prevent interruptions from a lost connection.
-
Download the Stable Diffusion WebUI project source code.
git clone -b v1.5.2 https://github.com/AUTOMATIC1111/stable-diffusion-webui.git -
Create a repositories directory and download the project's dependencies.
mkdir stable-diffusion-webui/repositories && cd $_ git clone https://github.com/Stability-AI/stablediffusion.git stable-diffusion-stability-ai git clone https://github.com/Stability-AI/generative-models.git generative-models git clone https://github.com/crowsonkb/k-diffusion.git k-diffusion git clone https://github.com/sczhou/CodeFormer.git CodeFormer git clone https://github.com/salesforce/BLIP.git BLIP -
Check out the specific commits for the dependency repositories to ensure stable generation results.
git -C stable-diffusion-stability-ai checkout cf1d67a6fd5ea1aa600c4df58e5b47da45f6bdbf git -C generative-models checkout 5c10deee76adad0032b412294130090932317a87 git -C k-diffusion checkout c9fe758757e022f05ca5a53fa8fac28889e4f1cf git -C CodeFormer checkout c5b4593074ba6214284d6acd5f1719b6c5d739af git -C BLIP checkout 48211a1594f1321b00f14c9f7a5b4813144b2fb9 -
Download the Stable Diffusion pre-trained model.
cd ~ && mkdir -p stable-diffusion-webui/models/Stable-diffusion wget "https://www.modelscope.cn/models/AI-ModelScope/stable-diffusion-v1-5/resolve/master/v1-5-pruned-emaonly.safetensors" -O stable-diffusion-webui/models/Stable-diffusion/v1-5-pruned-emaonly.safetensors mkdir -p ~/stable-diffusion-webui/models/clip git clone --depth=1 https://gitee.com/modelee/clip-vit-large-patch14.git ~/stable-diffusion-webui/models/clip/clip-vit-large-patch14 -
Modify the script so that Stable Diffusion uses the local ViT multimodal model at runtime.
sed -i "s?openai/clip-vit-large-patch14?${HOME}/stable-diffusion-webui/models/clip/clip-vit-large-patch14?g" ~/stable-diffusion-webui/repositories/stable-diffusion-stability-ai/ldm/modules/encoders/modules.py
Step 3: Deploy runtime environment
-
Change the pip download source.
Before you install dependencies, change the pip download source to accelerate the installation.
-
Create the pip directory.
mkdir -p ~/.config/pip -
Configure the pip installation mirror.
cat > ~/.config/pip/pip.conf <<EOF [global] index-url=http://mirrors.cloud.aliyuncs.com/pypi/simple/ [install] trusted-host=mirrors.cloud.aliyuncs.com EOF
-
-
Install Python runtime dependencies.
pip install cython gfpgan open-clip-torch==2.8.0 httpx==0.24.1 pip install git+https://github.com/openai/CLIP.git@d50d76daa670286dd6cacf3bcd80b5e4823fc8e1 -
Set the
OMP_NUM_THREADSandGOMP_CPU_AFFINITYenvironment variables.Set the
OMP_NUM_THREADSandGOMP_CPU_AFFINITYenvironment variables, which are required by the ZenDNN runtime library for your hardware platform.cat > /etc/profile.d/env.sh <<EOF export OMP_NUM_THREADS=\$(nproc --all) export GOMP_CPU_AFFINITY=0-\$(( \$(nproc --all) - 1 )) EOF source /etc/profile -
Run the script to automatically deploy the Stable Diffusion runtime environment.
cd ~/stable-diffusion-webui venv_dir="-" ./webui.sh -f --skip-torch-cuda-test --exit
Step 4: Generate images
-
Run the following command to start the WebUI service.
export LD_PRELOAD=/usr/lib64/libtcmalloc.so.4 export venv_dir="-" python3 launch.py -f --skip-torch-cuda-test --skip-version-check --no-half --precision full --use-cpu all --listenThe WebUI service has started successfully when the following output appears:
Python 3.10.12 (main, Jun 7 2023, 00:00:00) [GCC 12.2.1 20221121 (Anolis OS 12.2.1-2)] Version: v1.5.2 Commit hash: c9c8485bc1e8720aba70f029d25cba1c4abf2b5c Launching Web UI with arguments: -f --skip-torch-cuda-test --skip-version-check --no-half --precision full --use-cpu all --listen no module 'xformers'. Processing without... No SDP backend available, likely because you are running in pytorch versions < 2.0. In fact, you are in PyTorch 1.13.1. You might want to consider upgrading. no module 'xformers'. Processing without... No module 'xformers'. Proceeding without it. Warning: caught exception 'Torch not compiled with CUDA enabled', memory monitor disabled Calculating sha256 for /root/stable-diffusion-webui/models/Stable-diffusion/v1-5-pruned-emaonly.safetensors: Running on local URL: http://0.0.0.0:7860 To create a public link, set `share=True` in `launch()`. Startup time: 4.1s (launcher: 0.2s, import torch: 1.7s, import gradio: 0.6s, setup paths: 0.4s, other imports: 0.5s, load scripts: 0.3s, create ui: 0.3s, gradio launch: 0.1s). 6ce0161689b3853acaa03779ec93eafe75a02f4ced659bee03f50797806fa2fa Loading weights [6ce0161689] from /root/stable-diffusion-webui/models/Stable-diffusion/v1-5-pruned-emaonly.safetensors Creating model from config: /root/stable-diffusion-webui/configs/v1-inference.yaml LatentDiffusion: Running in eps-prediction mode DiffusionWrapper has 859.52 M params. Applying attention optimization: InvokeAI... done. Model loaded in 3.8s (calculate hash: 2.8s, load config: 0.1s, create model: 0.3s, apply weights to model: 0.3s, calculate empty prompt: 0.1s). -
In your browser's address bar, enter
http://<ECS_public_IP_address>:7860to access the web interface. -
In the prompt text box, enter a prompt (English only), for example,
Urban portrait of a skateboarder in mid-jump, graffiti walls background, high shutter speed. Click Generate to create the image.NoteYou can also try other prompts to explore more capabilities of Stable Diffusion.
Automated
Alibaba Cloud provides an automated deployment script for deploying and running the Stable Diffusion model with a single command.
-
Download the automated deployment script.
wget https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/en-US/20231213/bild/deploy_stable-diffusion_amd-docker.sh -
Enter the container environment.
sudo docker exec -it -w /root pytorch-amd /bin/bashImportantSubsequent commands must be run in the container environment. If you exit unexpectedly, run the preceding command to re-enter. To check if you are in a container, run
cat /proc/1/cgroup | grep docker. If the command returns output, you are in the container. -
Install tmux and create a tmux session.
yum install -y tmux tmuxNoteThis script downloads model files and can take a long time to run. Run it in a tmux session to prevent interruptions from a lost connection.
-
Make the automated deployment script executable.
chmod +x deploy_stable-diffusion_amd-docker.sh -
Run the automated deployment script.
./deploy_stable-diffusion_amd-docker.shThe automated deployment is complete when the following output appears:
############################################################## # # # Deploy success! Service is running now. # # # ############################################################## [ @iZuf6fwy177fk xxx ~]# nohup: ignoring input and redirecting stderr to stdout Python 3.10.12 (main, Jun 7 2023, 00:00:00) [GCC 12.2.1 20221121 (Anolis OS 12.2.1-2)] Version: v1.5.2 Commit hash: c9c8485bc1e8720aba70f029d25cba1c4abf2b5c Launching Web UI with arguments: -f --skip-torch-cuda-test --skip-version-check --no-half --precision full --use-cpu all --listen no module 'xformers'. Processing without... No SDP backend available, likely because you are running in pytorch versions < 2.0. In fact, you are using PyTorch 1.13.1. You might want to consider upgrading. no module 'xformers'. Processing without... No module 'xformers'. Proceeding without it. Warning: caught exception 'Torch not compiled with CUDA enabled', memory monitor disabled Calculating sha256 for /root/stable-diffusion-webui/models/Stable-diffusion/v1-5-pruned-emaonly.safetensors: Running on local URL: http://0.0.0.0:7860 To create a public link, set `share=True` in `launch()`. Startup time: 4.7s (launcher: 0.2s, import torch: 2.2s, import gradio: 0.6s, setup paths: 0.4s, other imports: 0.4s, setup codeformer: 0.2s, load scripts: 0.3s, create ui: 0.3s, gradio launch: 0.1s). 6ce0161689b3853acaa03779ec93eafe75a02f4ced659bee03f50797806fa2fa Loading weights [6ce0161689] from /root/stable-diffusion-webui/models/Stable-diffusion/v1-5-pruned-emaonly.safetensors Creating model from config: /root/stable-diffusion-webui/configs/v1-inference.yaml LatentDiffusion: Running in eps-prediction mode DiffusionWrapper has 859.52 M params. Applying attention optimization: InvokeAI... done. Model loaded in 3.8s (calculate hash: 2.8s, create model: 0.4s, apply weights to model: 0.3s, calculate empty prompt: 0.1s).NoteThe script takes a long time to run. If you lose your connection to the ECS instance during deployment in a tmux session, you can reconnect to the instance and run
tmux attachto resume the session and check the script's progress. -
In your browser's address bar, enter
http://<ECS_public_IP_address>:7860to access the web interface. -
In the prompt text box, enter a prompt (English only), for example,
Urban portrait of a skateboarder in mid-jump, graffiti walls background, high shutter speed. Click Generate to create the image.NoteYou can also try other prompts to explore more capabilities of Stable Diffusion.