SD.Next Documentation

repository·master·Indexed 27 days ago

https://github.com/vladmandic/sdnext

An all-in-one open-source WebUI for AI generative image and video creation based on Stable Diffusion. It features SDNQ quantization for VRAM reduction, Balanced Offload for memory management, and support for a wide range of hardware including nVidia CUDA, AMD ROCm/ZLUDA, Intel Arc OneAPI, OpenVINO, DirectML, and Apple Silicon MPS. The platform supports workflows for generation, editing, guidance (ControlNet, IPAdapters, LoRA), and processing, and provides a comprehensive API for captioning, object detection, and control operations.

Tokens
21.2K
Snippets
14
Records
135
Agent score
92%

What's inside SD.Next

  1. Overview of SD.Next features

    master

    SD.Next is an all-in-one WebUI for AI image and video generation. Key features include:

    • Automatic model download: Select reference models to download them automatically, or add your own for auto-detection.
    • SDNQ: A quantization engine providing up to 4x VRAM reduction.
    • Balanced Offload: Dynamically balances CPU and GPU memory for large models on limited hardware.
    • Caption and Enhance: Built-in support for 25+ LLM and VLM models, OpenCLiP, and Taggers (WaifuDiffusion/DeepDanbooru).
    • Image Processing: Includes a color-grading suite of tools.
    • Multi-platform: Optimized for various hardware via platform-specific auto-detection during installation.
  2. Run MiDaS via Docker

    master

    To run MiDaS in a containerized environment, ensure you have Docker and the NVIDIA Docker runtime installed.

    1. Build the image: docker build -t midas .

    2. Run inference (mounts input, output, and weights directories): docker run --rm --gpus all -v $PWD/input:/opt/MiDaS/input -v $PWD/output:/opt/MiDaS/output -v $PWD/weights:/opt/MiDaS/weights midas

    docker build -t midas .
    
    docker run --rm --gpus all -v $PWD/input:/opt/MiDaS/input -v $PWD/output:/opt/MiDaS/output -v $PWD/weights:/opt/MiDaS/weights midas
  3. Load a custom pipeline using DiffusionPipeline

    master

    To load a custom pipeline in diffusers, pass the custom_pipeline argument to the DiffusionPipeline.from_pretrained method. The value should correspond to one of the files located in diffusers/examples/community.

    pipe = DiffusionPipeline.from_pretrained(
        "Alpha-VLLM/Lumina-DiMOO",
        vqvae=vqvae,
        tokenizer=tokenizer,
        torch_dtype=torch.bfloat16,
        custom_pipeline="lumina_dimoo",
    )
  4. Setup MiDaS environment

    master

    To use MiDaS for monocular depth estimation, follow these steps to prepare your environment and weights:

    1. Download Weights: Pick one or more models and download the corresponding .pt or .xml/.bin files into a weights folder.
    2. Install Dependencies: Use Conda to create and activate the environment.

    Optional Steps:

    • For Next-ViT models: Add the submodule https://github.com/isl-org/Next-ViT to midas/external/next_vit.
    • For OpenVINO models: Install via pip install openvino.
    conda env create -f environment.yaml
    conda activate midas-py310
  5. Quick Start installation of SD.Next

    master

    To install and run SD.Next quickly, clone the repository and execute the appropriate startup script for your operating system. The built-in installer handles dependency management and automatic updates.

    git clone https://github.com/vladmandic/sdnext
    cd sdnext
    ./webui.sh # Linux/Mac
    webui.bat  # Windows
    webui.ps1  # PowerShell
  6. Upscale an image via SD.Next API using the CLI

    master

    The api-upscale.py script allows you to perform image upscaling by sending a request to a running SD.Next instance via its API. It supports specifying the input image, the desired upscaler, and the scaling factor.

    Environment Variables

    The script uses the following environment variables for connection and authentication:

    • SDAPI_URL: The base URL of the SD.Next API (defaults to http://127.0.0.1:7860).
    • SDAPI_USR: Username for HTTP Basic Authentication.
    • SDAPI_PWD: Password for HTTP Basic Authentication.

    CLI Arguments

    • --input (required): Path to the input image file.
    • --output (optional): Path where the upscaled image will be saved.
    • --upscaler (optional): The name of the upscaler to use (defaults to Nearest). You can find available upscalers by querying the /sdapi/v1/upscalers endpoint.
    • --scale (optional): The upscaling scale factor (defaults to 2).
  7. Use the api-grid CLI to generate image grids

    master

    The api-grid.py script is a CLI tool that automates the generation of image grids by interacting with the SD.Next API. It reads parameter variations from two text files (representing the X and Y axes) and generates a grid of images, optionally including labels and saving the results to a specified folder.

    To use it, provide files containing key:value pairs. For example, a line like prompt:a cat will append that prompt to the generation request, while lora:my_lora will append <lora:my_lora> to the prompt. Other keys will attempt to set corresponding attributes in the API request (e.g., steps:30).

  8. Configure SDNQ Attention Settings

    master

    After running the benchmark, you can apply the recommended settings in the SD.Next WebUI under Compute Settings -> SDNQ Attention.

    Key configuration options include:

    • sdnq_attention_matmul_type: Controls the quantization type for attention MatMuls (e.g., enabled resolves to int8, float8_e4m3fn for FP8 support).
    • sdnq_attention_pv_matmul_type: Controls the quantization type for PV MatMuls.
    • sdnq_attention_smooth_k: Enables/disables smooth K buybacks to reduce error.
    • sdnq_attention_use_hadamard: Enables/disables Hadamard buybacks.
    • sdnq_attention_hadamard_group_size: Sets the group size for Hadamard (values above head dim are clamped; non-pow2 values floor to the nearest power of 2).

    Important: Changes to SDNQ attention SDP overrides require a restart of the application to take effect.

  9. Run SD attention and dequantization benchmarks

    master

    Use the sdnq-attention-benchmark.py script to benchmark attention mechanisms and dequantization settings for SD.Next. The tool evaluates performance (latency) and accuracy (weight error) across various configurations to provide recommended settings for the WebUI.

    Key Benchmark Sections:

    • attention: Benchmarks attention kernels and shapes (e.g., wan22, ltx2, krea2).
    • dequant: Benchmarks weight dequantization, including MatMul backends, group sizes, SVD ranks, and Hadamard groups.
    • block: Benchmarks block-level configurations.
    • conv: Benchmarks convolutional layer quantization.

    Note on VRAM: Benchmarks require significant free VRAM. The script will skip sections if insufficient VRAM is detected (e.g., ~2GB for dequant, ~3GB for blocks, and up to 12GB for certain attention shapes like wan22-cfg).

  10. Install StableFast optimizations via CLI

    master

    Use the install-stablefast.py script to install StableFast optimizations. The script automatically detects your Python version, PyTorch version, and CUDA version to determine whether to install a pre-built wheel from the nightly releases or build from source.

    Supported Environments

    • Python Versions: 3.9, 3.10, 3.11
    • PyTorch Versions: 2.1.1, 2.1.2, 2.2.0, 2.2.1, 2.2.2, 2.3.0
    • CUDA Versions: cu118, cu121
    • Platforms: linux (manylinux2014_x86_64) and win32 (win_amd64)

    Installation Logic

    1. Wheel Installation: If your environment matches supported versions, the script downloads and installs a specific nightly wheel from the chengzeyi/stable-fast repository.
    2. Source Installation: If your PyTorch or CUDA versions are unsupported, or if a nightly version cannot be determined, the script falls back to installing from the GitHub source (git+https://github.com/chengzeyi/stable-fast.git@main#egg=stable-fast).
    3. Dependencies: The script automatically attempts to install triton via pip if the StableFast installation succeeds.
  11. Monitor API progress and automate system shutdown

    master

    The api-progress.py script monitors the SD.Next API progress endpoint to track the status of the last job. It is designed to automatically execute a system command (e.g., sudo shutdown now) if the time elapsed since the last job's timestamp exceeds a specified timeout.

    Configuration Options

    The script uses a Dot configuration object with the following keys:

    • url: The base URL of the SD.Next instance (default: http://127.0.0.1:7860).
    • timeout: Maximum allowed seconds since the last job before the action is triggered (default: 3600).
    • frequency: Polling interval in seconds (default: 1).
    • action: The shell command to execute upon timeout (default: sudo shutdown now).
    • user: Username for HTTP Basic Authentication.
    • password: Password for HTTP Basic Authentication.

    API Endpoint Used

    The script polls the following endpoint: GET {url}/sdapi/v1/progress?skip_current_image=true

    # Example of how the configuration is structured within the script
    opts = Dot({
        "timeout": 3600,
        "frequency": 1,
        "action": "sudo shutdown now",
        "url": "http://127.0.0.1:7860",
        "user": "",
        "password": "",
    })