nunchaku

repository·main·Indexed 26 days ago

https://github.com/nunchaku-ai/nunchaku

A high-performance inference engine optimized for 4-bit neural networks using SVDQuant. It provides significant memory savings and speedups for large models such as FLUX.1 and SANA while maintaining high visual fidelity. The engine utilizes outlier migration and low-rank decomposition to enable efficient quantization of diffusion models.

Tokens
14.7K
Snippets
32
Records
121
Agent score
86%

What's inside nunchaku

  1. Overview of Nunchaku Inference Engine

    main

    Nunchaku is a high-performance inference engine optimized for 4-bit neural networks using the SVDQuant post-training quantization technique. It is designed to maintain high visual fidelity while significantly reducing memory usage and increasing inference speed.

    Key benefits include:

    • Memory Reduction: Achieves up to 3.6× memory reduction (e.g., on 12B FLUX.1-dev compared to BF16).
    • Speedup: Offers significant speed improvements (e.g., 8.7× speedup over 16-bit models on specific hardware) by eliminating the need for CPU offloading.
    • High Fidelity: Maintains superior visual quality compared to standard W4A4 or W4A8 baselines.
  2. Explore nunchaku subpackages

    main

    The nunchaku package is organized into several functional subpackages. Depending on your task, you should import from the following modules:

    • nunchaku.models: Core model definitions and loading.
    • nunchaku.lora: LoRA (Low-Rank Adaptation) support and management.
    • nunchaku.pipeline: High-level inference pipelines.
    • nunchaku.ops: Specialized operations and kernels.
    • nunchaku.caching: Caching mechanisms for performance optimization.
    • nunchaku.utils: General utility functions.
  3. Understand SVDQuant Quantization

    main

    SVDQuant is the quantization method used by Nunchaku to enable efficient 4-bit quantization of diffusion models by managing outliers:

    1. Outlier Migration: Outliers are migrated from activations to weights. This makes the updated activation $\hat{\mathbf{X}}$ easier to quantize, while the updated weights $\hat{\mathbf{W}}$ become more difficult.
    2. Low-Rank Decomposition: The difficult weights $\hat{\mathbf{W}}$ are decomposed using SVD into a low-rank component $\mathbf{L}_1\mathbf{L}_2$ and a residual $\hat{\mathbf{W}}-\mathbf{L}_1\mathbf{L}_2$.
    3. Precision Handling: The low-rank branch runs at 16-bit precision to alleviate quantization difficulty.
  4. Nunchaku Engine Design and Optimization

    main

    The Nunchaku engine optimizes the overhead of running the low-rank branch through kernel fusion. Instead of running components naively (which introduces significant latency due to extra reads/writes), Nunchaku fuses kernels:

    • Down Projection & Quantize: These kernels are fused because they use the same input.
    • Up Projection & 4-Bit Compute: These kernels are fused because they share the same output.

    This reduces data movement overhead and improves inference speed.

  5. Enable CPU Offload for FLUX.1 models

    main

    To significantly reduce GPU memory usage when running FLUX.1 models, you can use Nunchaku's built-in CPU offload in combination with Diffusers' sequential offload.

    1. Nunchaku CPU Offload: Set offload=True during the transformer initialization. This offloads inactive model components to CPU memory.
    2. Diffusers Sequential Offload: Call pipeline.enable_sequential_cpu_offload() on your pipeline instance. This enables automatic device management.

    Important: When using pipeline.enable_sequential_cpu_offload(), do not manually move the pipeline to a device using .to('cuda'), as the sequential offload mechanism handles device placement automatically.

  6. Run Nunchaku unit tests

    main

    Nunchaku uses pytest for testing. You must provide a Hugging Face access token via the HF_TOKEN environment variable to download necessary models and datasets, unless you have already authenticated via huggingface-cli login.

    To speed up test runs that generate images, you can set the NUNCHAKU_TEST_CACHE_ROOT environment variable to cache results. If not set, images are saved to test_results/ref.

  7. Run Latency Benchmarks for SANA Models

    main

    Use latency.py to measure the end-to-end latency or individual DiT forward step latency for INT4 models.

    CLI Arguments:

    • -t <steps>: Number of inference steps (default: 20).
    • -g <scale>: Guidance scale (default: 5).
    • --pag-scale <scale>: PAG guidance scale (default: 2).
    • --mode <mode>: Set to step to measure the latency of a single DiT forward step instead of end-to-end generation.
    • --warmup-times <count>: Number of warmup runs (default: 2).
    • --test-times <count>: Number of test runs (default: 10).
    python latency.py
  8. Perform Command Line Inference with generate.py

    main

    Use the generate.py script to generate images from text prompts directly via the CLI. By default, the script uses the INT4 model and saves the result to output.png.

    CLI Arguments:

    • --prompt <text>: The text prompt for image generation.
    • -o or --output-path <path>: Specify a custom path for the generated image.
    • -p <precision>: Select model precision. Options include int4 (default) or bf16.
    • -t <steps>: Number of inference steps (default: 20).
    • -g <scale>: Classifier-free guidance scale (default: 5).
    • --pag-scale <scale>: Adjust the PAG guidance scale (default: 2).
    python generate.py --prompt "You Text Prompt"