Page 1 of 1

AUTOMATIC1111/stable-diffusion-webui SETUP

Posted: Tue Nov 08, 2022 7:14 am
by jstenner

This set of instructions will get you up and running the latest and greatest Stable Diffusion WebUI on HiPerGator.

Stable Diffusion Web UI by AUTOMATIC1111

To run Web UI on HiPerGator is pretty straight forward, but it's a sort of hybrid between running something "local" and running it "remote." We want to use our wonderful access to an array of NVIDIA A100 GPUs on HiPerGator, but we need to use the graphical interface afforded Web UI. Here's what to do:

First, we'll install everything, then quit and relaunch it with a SLURM batch script using the same technique as running a Jupyter Notebook locally. Once installed, in day-to-day use, you'll use the SLURM batch script to launch Web UI.

Move to your working directory on "blue." Substitute the group name (art4612, art4630, cota-dept, etc.) for <groupname> as well as your username for <gatorlink> in the command below:

Code: Select all

cd /blue/<groupname>/share/<gatorlink>/

Code: Select all

git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
Move into our cloned directory and edit webui-user.sh to reflect our installation path:

Code: Select all

cd stable-diffusion-webui
nano webui-user.sh
Notice that I've uncommented (removed the #) on particular lines below:
Screen Shot 2022-10-17 at 9.49.52 AM.png
Modify "install_dir" and "clone_dir" to reflect your paths. Note that $(whoami) is a variable substitution that will automatically substitute your gator username, so you don't need to modify that element.

Next, we need to modify the TORCH_COMMAND to install specific versions in order to work with HiPerGator. The appropriate versions will change with time, but as of 01.2023, below is correct:

Code: Select all

export TORCH_COMMAND="pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118"
Save your changes to webui-user.sh the normal way (Ctrl + o, Enter, Ctrl + x).

Grab a GPU. Modify this based on how long you want and be sure to substitute the appropriate HiPerGator groupname (art4612, art4630, cota-dept, etc.) for your job:

Code: Select all

srun -p gpu --nodes=1 --gpus=a100:1 --time=2:00:00 --mem=16gb --ntasks=1 --cpus-per-task=1 --pty -u -A <groupname> -q <groupname> bash -i
Load up some modules and save the grouping as "webui":
[These are the best versions as of Fall 2023]

Code: Select all

module load cuda/11.4.3
module load gcc
module load git
module load python/3.10
module load ffmpeg/4.3.1
module save webui
Once you've saved your module list, in the future, we'll restore these modules in our batch script with:

Code: Select all

module r webui
You can list the modules you have loaded with:

Code: Select all

module list
At some point in the future you may need to change version of the software you're loading via the module system. Use "spider" to see what versions are available. For example:

Code: Select all

module spider ffmpeg
For the next step, we need to get a basic stable diffusion model downloaded and placed inside the "models/Stable-diffusion" directory. I have placed a selection of models in "/blue/<groupname>/share/models/". To copy a model into your stable-diffusion-webui directory, do this:

Code: Select all

cd models/Stable-diffusion
cp ../../../../models/Stable-diffusion/sd-v1-4.ckpt .
"ls" to check if you did that properly (the number of ../ depends on your particular setup). You should see your model.

UPDATE: of course, you may want to get the latest model yourself or use a model I haven't provided. SDXL 1.0 was recently released. You would get that here:
https://huggingface.co/stabilityai/stab ... l-base-1.0
Navigate to your models directory, into the Stable-diffusion directory and run:

Code: Select all

wget https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/sd_xl_base_1.0.safetensors
Here's a good tutorial using SDXL:


Now we can proceed with installing the necessary dependencies and run webui.
Change directories from your current location (models/Stable-diffusion) to the root install directory (stable-diffusion-webui):

Code: Select all

cd ../../
Run webui:

Code: Select all

bash webui.sh
Everything should install properly (be patient) and the application/server should launch. You won't have any way to access the server, though, unless we set up a SSH Tunnel. You'll know the installation has finished and the app is waiting to be used because the terminal will present you with a localhost (or 127.0.0.1) URL. Let's kill it now by typing "Ctrl + c".

Okay, everything is installed. Let's get remote access to the application going. I have written a shell script that does that for us. It is adapted from the shell script that launches Jupyter Lab. We'll grab the SLURM batch script from my GitHub repository.

Move out of our current working directory (stable-diffusion-webui) and clone my "slurm_examples":

Code: Select all

cd ..
git clone https://github.com/jstenner/slurm_examples.git
Inside "slurm_examples" there is a batch script called "webui_sbatch.sh". Copy that file to your "stable-diffusion-webui" directory. From now on, that's the script you'll launch to run AUTOMATIC1111/stable-diffusion-webui.

By now, you should know how to edit SLURM batch scripts to customize them for your purposes (e.g. run time, commandline arguments, etc.). Move to your webui working directory and edit the batch script:

Code: Select all

cd stable-diffusion-webui
nano webui_sbatch.sh
Change <gatorusername> to your own username so you get notifications.
Set the time you want the job to run. Set your gradio-auth USERNAME:PASSWD in the python command on the last line.
Save your changes: Ctrl + o, Enter, Ctrl + x.

Now LAUNCH:

Code: Select all

sbatch webui_sbatch.sh
The batch script will output a log file called "webui_<jobid>.out". We need to look at this log file to see how to connect to the application and to monitor its operation. Let's use "tail". Remember to substitute your actual filename with the proper job id in place of "webui_<jobid>" below:

Code: Select all

tail -f webui_<jobid>.out
Notice two lines in the output. You'll see "SSH tunnel command:" and "Local browser URI:" Copy/paste the "ssh" line into a NEW Terminal window and authenticate. That creates a secure shell tunnel to HiPerGator allowing the web server (https) running Stable Diffusion to be accessible on your local machine.

Finally, copy/paste the "Local browser URI" into your web browser. It may not work immediately because the application is still loading the model and starting up. After a minute or two, refresh the page and the Gradio login dialog should appear and you're ready to login and get started!
Screen Shot 2022-11-08 at 8.04.18 AM.png
Notice that as you render, you'll get feedback about what's happening in your main terminal window (the one with "tail" running). At first webui needs to download certain models and support files so it may be a bit slow. Once everything is available to you, it's very quick.

HAVE FUN!

To quit, type "Ctrl + c" in your shell tunnel terminal window to close that down.
Type "Ctrl + c" in your main terminal window to close "tail". Then type:

Code: Select all

squeuemine 
and notice the Job ID. Use that number to close down your SLURM session:

Code: Select all

scancel <JOBID>
End your session with:

Code: Select all

exit
:-)