[[https://pctresearch.com/|{{:banner_image.jpg?nolink&800|}}]] ====== Stable Diffusion Version 2 - Run locally. Abandon if this doesn't work. ====== - [[stable-diffusion#Windows11 Installation|Windows11 Installation]] ===== Windows11 Installation ===== - [[stable-diffusion#Prerequisites|Prerequisites]] - [[stable-diffusion#git|git]] - [[stable-diffusion#python3.10.6|python3.10.6]] - [[stable-diffusion#Anaconda3|Anaconda3]] - [[stable-diffusion#Make conda environment|Make conda environment]] - [[stable-diffusion#Download & Installation|Download & Installation]] - [[stable-diffusion#Installing stablediffusion|Installing stablediffusion]] - [[stable-diffusion#Installing NVIDIA CUDA Toolkit|Installing NVIDIA CUDA Toolkit]] - [[stable-diffusion#Installing PyTorch|Installing PyTorch]] - [[stable-diffusion#Installing xFormers|Installing xFormers]] - [[stable-diffusion#Installing diffusers|Installing diffusers]] - [[stable-diffusion#about triton|about triton]] - [[stable-diffusion#Operation Confirmation|Operation Confirmation]] - [[stable-diffusion#scripts/txt2img.py|scripts/txt2img.py]] - [[stable-diffusion#scripts/img2img.py|scripts/img2img.py]] - [[stable-diffusion#Installing stable-diffusion-webui|Installing stable-diffusion-webui]] - [[stable-diffusion#Corresponding Windows System Environments|Corresponding Windows System Environments]] - [[stable-diffusion#Trouble shooting|Trouble shooting]] - [[stable-diffusion#Out of memory|Out of memory]] - [[stable-diffusion#venv folder|venv folder]] - [[stable-diffusion#Locate files in the right places|Locate files in the right places]] ==== Prerequisites ==== === git ===
Official site Installation Guide
Git for Windows Git for Windows Installation
TortoiseGit(optional) TortoiseGit Installation
=== python3.10.6 === python3.10.6 (3.10.0, 3.11.4 has also been confirmed to work) [[https://www.python.org/|python.org]] :!: python3.10.6 was recommended, nevertheless 2023/09/03 Operation has been confirmed in python3.11.4 [[python-installation|Python Installation]] === Anaconda3 === [[https://www.anaconda.com/download|Anaconda]] [[anaconda-installation|Anaconda Installation]] ==== Make conda environment ==== Make conda environment for stable diffusion :!: to activate conda environment, you must use ''Anaconda Prompt''. Here we use environment name ''webui_env''. E:\> conda create --name webui_env python==3.10.6 Location of envs are C:\Users\%USERNAME%\anaconda3\envs activate env E:\> conda activate webui_env for reference (webui_env) E:> conda list # packages in environment at C:\Users\kan\anaconda3\envs\webui_env: # # Name Version Build Channel bzip2 1.0.8 he774522_0 ca-certificates 2023.05.30 haa95532_0 libffi 3.4.4 hd77b12b_0 openssl 1.1.1v h2bbff1b_0 pip 23.2.1 py310haa95532_0 python 3.10.0 h96c0403_3 setuptools 68.0.0 py310haa95532_0 sqlite 3.41.2 h2bbff1b_0 tk 8.6.12 h2bbff1b_0 tzdata 2023c h04d1e81_0 vc 14.2 h21ff451_1 vs2015_runtime 14.27.29016 h5e58377_2 wheel 0.38.4 py310haa95532_0 xz 5.4.2 h8cc25b3_0 zlib 1.2.13 h8cc25b3_0 (webui_env) E:\stable_diffusion>pip list Package Version ---------- ------- pip 23.2.1 setuptools 68.0.0 wheel 0.38.4 and maybe followings are needed. > conda config --append channels conda-forge > conda config --append channels nvidia ==== download & installation ==== === installing stablediffusion === installing stablediffusion using requirements.txt (webui_env) E:\> mkdir stable_diffusion (webui_env) E:\> cd stable_diffusion (webui_env) E:\stable_diffusion> git clone https://github.com/Stability-AI/stablediffusion.git (webui_env) E:\stable_diffusion> cd stablediffusion (webui_env) E:\stable_diffusion\stablediffusion> pip install -r requirements.txt :!: if you got following error ... running build_rust error: can't find Rust compiler ... You need to Install Rust from [[https://www.rust-lang.org/tools/install|rust-lang.org]] === Installing NVIDIA CUDA Toolkit === [[https://developer.nvidia.com/cuda-downloads|NVIDIA CUDA Toolkit downloads]] maybe 11.8 is recommended [[https://developer.nvidia.com/cuda-11-8-0-download-archive|CUDA Toolkit 11.8 Downloads]] and execute following command (webui_env) E:\stable_diffusion\stablediffusion> conda install cudatoolkit === Installing PyTorch === There are two method. - Method 1: Installing PyTorch with Conda - Method 2: Installing PyTorch with Pip Both methods are straightforward and can be done. [[https://pytorch.org/get-started/locally|pytorch.org]] says Anaconda is our recommended package manager since it installs all dependencies. But we choice pip install. {{:pytorch_pip.png?direct&400|}} It says > pip3 install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cu121 But we execute more precisely, (webui_env) E:\stable_diffusion\stablediffusion> pip install --pre torch==2.1.0.dev20230817+cu121 torchvision==0.16.0.dev20230818+cu121 torchaudio==2.1.0.dev20230818+cu121 --index-url https://download.pytorch.org/whl/nightly/cu121 :!: This is one of the most important checkpoints. At this point torch.cuda should be available. Check it! (webui_env) E:\stable_diffusion\stablediffusion> python >>> import torch >>> print(torch.__version__) 2.1.0.dev20230817+cu121 >>> torch.cuda.is_available() True >>> exit() for reference (webui_env) E:\stable_diffusion\stablediffusion> conda list | findstr torch open-clip-torch 2.7.0 pypi_0 pypi pytorch-lightning 1.4.2 pypi_0 pypi torch 2.1.0.dev20230817+cu121 pypi_0 pypi torchaudio 2.1.0.dev20230818+cu121 pypi_0 pypi torchmetrics 0.6.0 pypi_0 pypi torchvision 0.16.0.dev20230818+cu121 pypi_0 pypi === Installing xFormers === :!: You don't need to do this, if you only use with webui. (webui_env) E:\stable_diffusion\stablediffusion> pip install git+https://github.com/facebookresearch/xformers Successfully installed xformers-0.0.22+cfad52d.d2023090 Upon successful installation, the code will automatically default to memory efficient attention for the self- and cross-attention layers in the U-Net and autoencoder. === Installing diffusers === (webui_env) E:\stable_diffusion\stablediffusion> pip install git+https://github.com/huggingface/transformers Successfully uninstalled transformers-4.19.2 Successfully installed safetensors-0.3.3 transformers-4.33.0.dev0 === about triton === Set Windows environment ''XFORMERS_FORCE_DISABLE_TRITON=1'' I Installied from vcpkg but xformers can't recognize triton. ==== Operation Confirmation ==== === scripts/txt2img.py === download ''v2-1_768-ema-pruned.ckpt'' from [[https://huggingface.co/stabilityai/stable-diffusion-2-1/blob/main/v2-1_768-ema-pruned.ckpt|stabilityai/stable-diffusion-2-1]] (webui_env) E:\stable_diffusion\stablediffusion> python scripts/txt2img.py --prompt "a professional photograph of an astronaut riding a horse" --ckpt "E:\difwork\SD2.1-v\v2-1_768-ema-pruned.ckpt" --config "E:\stable_diffusion\stablediffusion\configs\stable-diffusion\v2-inference-v.yaml" --H 768 --W 768 --device cuda :!: txt2img.py makes 9 images. So it takes so much time and large memory. [[stable-diffusion#Out of memory|''Trouble shooting of Out of memory'']]. If you can't fix this error,skip this. === scripts/img2img.py === we need to download [[https://huggingface.co/stabilityai/stable-diffusion-2-1-base/blob/main/v2-1_512-ema-pruned.ckpt|SD2.1-base\v2-1_512-ema-pruned.ckpt]] (webui_env) E:\stable_diffusion\stablediffusion> python scripts/img2img.py --prompt "To create a commentary on modern technology and classical art, use an art style like Pop Art that often addresses cultural themes. Clearly mention the juxtaposition of elements. Pop Art painting of a modern smartphone with classic art pieces appearing on the screen." --init-img "E:\difwork\SD2.1-base\sample.png" --strength 0.8 --ckpt "E:\difwork\SD2.1-base\v2-1_512-ema-pruned.ckpt" outputs/img2img-samples Enjoy. ==== installing stable-diffusion-webui ==== [[https://github.com/AUTOMATIC1111/stable-diffusion-webui|stable-diffusion-webui]] (webui_env) E:\stable_diffusion\stablediffusion> cd .. (webui_env) E:\stable_diffusion> git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git (webui_env) E:\stable_diffusion> cd stable-diffusion-webui Edit the webui-user.bat to include ''--xformers'' (''--reinstall-xformers'') for the command line arguments as well as the desired package version. Then run the ''webui-user.bat'' as normal. ''webui-user.bat'': @echo off set PYTHON= set GIT= set VENV_DIR= set XFORMERS_PACKAGE=xformers==0.0.20 set COMMANDLINE_ARGS=--xformers call webui.bat :!: We need to install xformers for webui.And It should be 0.0.20 not 0.0.22 at current condition. then execute ''webui-user.bat'' (webui_env) E:\stable_diffusion\stable-diffusion-webui> webui-user.bat Running on local URL: http://127.0.0.1:7860 Applying attention optimization: xformers... done. ... ** Gooooal!!! ** {{:00257-104118679.png?nolink&400|}} for reference (webui_env) E:\webui\stable-diffusion-webui>python >>> import torch >>> print(torch.__version__) 2.1.0.dev20230817+cu121 >>> torch.cuda.is_available() True >>> exit() (webui_env) E:\webui\stable-diffusion-webui>conda list | findstr torch open-clip-torch 2.7.0 pypi_0 pypi pytorch-lightning 1.4.2 pypi_0 pypi torch 2.1.0.dev20230817+cu121 pypi_0 pypi torchaudio 2.1.0.dev20230818+cu121 pypi_0 pypi torchmetrics 0.6.0 pypi_0 pypi torchvision 0.16.0.dev20230818+cu121 pypi_0 pypi ==== Corresponding Windows System Environment ==== followings are checked by programs CUDA_HOME = C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.2 CUDA_PATH = C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8 PYTORCH_CUDA_ALLOC_CONF = garbage_collection_threshold:0.6,max_split_size_mb:4096 PYTORCH_NO_CUDA_MEMORY_CACHING = 1 XFORMERS_FORCE_DISABLE_TRITON = 1 maybe not correnponding CUDA_PATH_V11_8 = C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8 CUDA_PATH_V12_2 = C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.2 ==== Trouble shooting ==== === Out of memory === If you got error like following, torch.cuda.OutOfMemoryError: CUDA out of memory. edit windows system environment variables set PYTORCH_NO_CUDA_MEMORY_CACHING=1 set PYTORCH_CUDA_ALLOC_CONF=garbage_collection_threshold:0.6,max_split_size_mb:32 set PYTORCH_CUDA_ALLOC_CONF=garbage_collection_threshold:0.6,max_split_size_mb:1024 set PYTORCH_CUDA_ALLOC_CONF=garbage_collection_threshold:0.6,max_split_size_mb:4096 :!: About ''max_split_size_mb'' ''max_split_size_mb'' prevents the native allocator from splitting blocks larger than this size (in MB). Default value is unlimited.So easily cause Out of Memory. So,set ''max_split_size_mb'' to a maximum integer value smaller than the minimum size of the video memory request when Out of Memory occurs. That can ensure the feasibility of running large images while maximizing performance. ==== venv folder ==== If you change some environments and use the same directory, you should delete the ''venv'' folder completely. stable-diffusion-webui\venv webui will create new venv again. === Locate files in the right places === ''.ckpt'', ''.safetensors'', ''.pt''', ''.bin'', ''.pth'' There are many types of extension files that correspond to webui. You must place these files in the correct locations. Otherwise, it will cause many warnings or even errors. See [[https://github.com/jianwu13/PCTResearch/wiki/Webui-Tips#webui.folder|Webui-Tips]]