Explain what optimizations are applied to inference pipelines in this repo.
- User
Explain what optimizations are applied to inference pipelines in this repo.
Assistant
This repository implements SVDQuant, a post-training quantization technique for 4-bit diffusion models, along with several system-level optimizations to accelerate inference and reduce memory usage.
1. SVDQuant & Low-Bit Quantization
The core optimization is SVDQuant, which decomposes 16-bit weights into a 4-bit component and a low-rank 16-bit component to maintain visual fidelity.
- 4-bit Linear Layers (
SVDQW4A4Linear):- Replaces standard
nn.Linearlayers. - Weights (
qweight): Stored as packed 4-bit integers (int8container). - Low-Rank Adapters (
proj_down,proj_up): Kept in 16-bit (BF16/FP16) to capture outliers and sensitivity. - Precision Support: Supports both INT4 (standard) and NVFP4 (NVIDIA 4-bit Floating Point for RTX 5090).
- Replaces standard
2. Kernel Fusion
To overcome the overhead of the low-rank branch and quantization, custom CUDA kernels (
nunchaku/csrc) are used to fuse operations:- Fused Quantization: Fuses Activation Quantization with the Low-Rank Down-Projection. This means the input is quantized to 4-bit and projected to the low-rank space in a single kernel pass.
- Fused GEMM: Fuses the 4-bit Matrix Multiplication (GEMM) with the Low-Rank Up-Projection, bias addition, scaling (
wscales,ascales), and activation functions (SiLU). - Other Fusions: RMSNorm, RoPE (Rotary Positional Embeddings), and fused QKV projections in attention layers.
3. Caching Mechanisms
Two types of caching are implemented to skip redundant computations:
- TeaCache (
nunchaku/caching/teacache.py): "Temporal Embedding Analysis Caching". It tracks the relative L1 distance of modulated inputs across timesteps. If the change is below a threshold, it skips the entire transformer block and reuses the previous residual. - First-Block Cache (FB Cache) (
nunchaku/caching/fbcache.py): Specifically optimizes the first transformer block. If the residual of the first block is similar to the previous step, it reuses cached results for subsequent blocks.
4. Memory Optimizations & Offloading
- 4-bit Linear Layers (
- User
Is 4-bit GEMM used in attention layers or non-attention linear layers?
Assistant
4-bit GEMM is used in both attention layers and non-attention linear layers (MLP/FeedForward) throughout the repository.
The implementation relies on the
SVDQW4A4Linearmodule, which replaces standard linear layers in both contexts:1. Attention Layers
In models like FLUX and Qwen-Image, the 4-bit GEMM is applied to the following projections:
- QKV Projections: The Query, Key, and Value projections are typically fused into a single
to_qkvlayer for efficiency. - Output Projections: The
to_out(orproj_out) layer that follows the attention computation. - Additional Projections: In complex blocks like Qwen-Image, it's also used for
add_qkv_projandto_add_out.
2. Non-Attention Linear Layers (MLP / FeedForward)
The 4-bit GEMM is also the core of the FeedForward networks (MLPs):
- This module patches the standard FeedForward block, replacing its layers with .
- QKV Projections: The Query, Key, and Value projections are typically fused into a single