Innovations & Integrations (Community of Practice)

Wednesday 17 January 2024

Installing Python 3.10.11, Python Packages, Visual Studio, CUDA and cuDNN for Stable Diffusion

If you are running on Anaconda 3.9.13, you will need to create a new Environment, e.g. py3106, then activate py3106, and PIP install Python==3.10.6, you will need to update your Environment Variables by adding the Path to your Python 3.10.6 location path. 

I notice, Python 3.10.11 was needed after creating py3106 environment therefore I did a reinstallation for Python 3.10.11, but kept the py3106 environment.

  1. (py3106) C:\Users\username> cd C:\Users\username\stable-diffusion-webui #go to directory
  2. (py3106) C:\Users\username> webui.bat #to call out stable diffusion
  3. (py3106) C:\Users\username> pip install --upgrade --force-reinstall xformers==0.0.23.post1 #need this xformers package
  4. (py3106) C:\Users\username> pip install torch==2.1.0 torchvision==0.16.0 torchaudio==2.1.0 #need to update dependency package
  5. Install visual studio 2022: with
    1. Desktop development with C++: This is crucial for CUDA development as CUDA uses C++ for kernel definitions and host code. It typically includes MSVC (Microsoft Visual C++ Compiler), which is required for compiling C++ code.
    2. Game development with C++ (optional): If you're planning to do graphics-intensive work or game development, this workload includes graphics tools that can be helpful.
    3. Linux development with C++ (optional): If you intend to target Linux systems or cross-compile for Linux from Windows, this could be useful.
    4. Data science and analytical applications workload (optional): If you're working with data science applications that may benefit from GPU acceleration.
  6. Update latest Windows SDK:
    1. MSVC v142 - VS 2019 C++ x64/x86 build tools (or later) - this is particularly important for native C++ compilation.
    2. C++ CMake tools for Windows - if you are planning to use CMake as your build system.
    3. C++/CLI support - if you are planning to mix managed (.NET) and native (C++) code.
  7. Downloaded and install cuda_12.3.2_546.12_windows.exe (or later version) from the website here:
  8. (py3106) C:\Users\username> nvcc --version #check CUDA version
  9. (py3106) C:\Users\username> nvidia-smi #check GPU driver version (for Nvidia, download your driver from
  10. (py3106) C:\Users\username> conda install cudnn #install deep learning frameworks for faster performance
  11. (py3106) C:\Users\usernamepip install chardet
  12. (py3106) C:\Users\username> python -m pip --version
  13. (py3106) C:\Users\usernameconda install torchaudio
  14. (py3106) C:\Users\usernamepip install --upgrade --force-reinstall torchvision charset_normalizer
  15. (py3106) C:\Users\usernameconda update --all
  16. Goto Environment Variables > add relevant paths
    • C:\Users\username\anaconda3\envs\py3106\python.exe
    • %PATH%;C:\Users\username\anaconda3\envs\py3106\Scripts
    • C:\Users\username\anaconda3\envs\Scripts
    • etc.
  17. Edit webui-user.bat saved under C:\Users\username\stable-diffusion-webui, go to anaconda prompt, activated env, go to folder and run it, e.g. (py3106) C:\Users\username\stable-diffusion-webui>webui-user.bat
    • set PYTHON=C:\Users\username\anaconda3\envs\py3106\python.exe
    • set GIT=C:\Program Files\Git\cmd\git.exe ; 
    • set VENV_DIR=C:\Users\username\anaconda3\envs\py3106
    • set COMMANDLINE_ARGS=--xformers --autolaunch --medvram
username above refers to your own naming setup.

xformers is a Python package or library that extends the capabilities of PyTorch for working with transformer-based models. Transformers are a class of deep learning models that have shown remarkable performance in various natural language processing tasks. xformers provides additional tools and functionalities for working with transformers and simplifies their usage in different applications.

torch (PyTorch) is an open-source machine learning library that provides a flexible framework for building and training deep neural networks. It is widely used for various machine learning tasks, including computer vision, natural language processing, and more. PyTorch is known for its dynamic computation graph and is popular among researchers and developers in the deep learning community.

CUDA stands for "Compute Unified Device Architecture," and it is a parallel computing platform and API (Application Programming Interface) developed by NVIDIA. CUDA is designed to leverage the computational power of NVIDIA GPUs (Graphics Processing Units) for general-purpose computing tasks, beyond just graphics processing.

CUDA is a technology that allows developers to tap into the immense parallel processing power of NVIDIA GPUs for a wide range of computational tasks. It has become a fundamental tool for accelerating scientific and data-intensive applications and is particularly important in the field of machine learning and deep learning where training large neural networks can be significantly accelerated using GPUs with CUDA support.

After installing the CUDA Toolkit, it's a good idea to also install cuDNN (CUDA Deep Neural Network library) if you plan to work with deep learning frameworks like PyTorch or TensorFlow. cuDNN is an optimized library for deep neural networks, and it can significantly improve the training and inference performance of deep learning models when used with CUDA-compatible GPUs.

Step 5 & 6:
Please take note that Visual Studio Installation is needed if you don't have it.
Follow the steps below to have the latest MSVC compiler and standard C++ libraries under Visual Studio Installation, which are often required for compiling CUDA applications.

1. Windows SDK and MSVC Update: 
  • To update the Windows SDK and MSVC v142 (or later) build tools, use the Visual Studio Installer. These components are required for native C++ compilation and are necessary for CUDA development.
  • From the start page of Visual Studio 2022, close the window or click on Continue without code to access the menus.
  • Go to Tools -> Get Tools and Features... which will open the Visual Studio Installer.
  • In the installer, find Visual Studio 2022 and click Modify.
  • Under "Workloads", ensure "Desktop development with C++" is checked.
  • Switch to the "Individual components" tab and check the latest Windows SDK and the MSVC build tools (e.g., MSVC v142 - VS 2019 C++ x64/x86 build tools or later).
  • Click "Modify" or "Install" to apply the updates.
2. CUDA Toolkit Installation:
  • Download the CUDA Toolkit installer from the NVIDIA website.
  • Run the installer and select the components you need. The installer should automatically detect Visual Studio 2022 and install the necessary integrations.
  • The installer typically sets the necessary environment variables, but if it doesn't, you may need to manually add the CUDA Toolkit's bin directory to your system's PATH.
3. NVIDIA Nsight Visual Studio Edition Update:
After completing these steps, restart your computer to ensure all changes take effect. Then, you can try running the webui.bat of Stable Diffusion again for it to work. 

No comments:

Post a Comment