Basic Usage

Basic Usage#

The following is a minimal script for running 4-bit FLUX.1 using Nunchaku. Nunchaku provides the same API as Diffusers, so you can use it in a familiar way.

Running FLUX.1-dev (examples/flux.1-dev.py)#
 1import torch
 2from diffusers import FluxPipeline
 3
 4from nunchaku import NunchakuFluxTransformer2dModel
 5from nunchaku.utils import get_precision
 6
 7precision = get_precision()  # auto-detect your precision is 'int4' or 'fp4' based on your GPU
 8transformer = NunchakuFluxTransformer2dModel.from_pretrained(
 9    f"nunchaku-tech/nunchaku-flux.1-dev/svdq-{precision}_r32-flux.1-dev.safetensors"
10)
11pipeline = FluxPipeline.from_pretrained(
12    "black-forest-labs/FLUX.1-dev", transformer=transformer, torch_dtype=torch.bfloat16
13).to("cuda")
14image = pipeline("A cat holding a sign that says hello world", num_inference_steps=50, guidance_scale=3.5).images[0]
15image.save(f"flux.1-dev-{precision}.png")

The key difference when using Nunchaku is replacing the standard FluxTransformer2dModel with NunchakuFluxTransformer2dModel. The from_pretrained() method loads quantized models and accepts either Hugging Face remote file paths or local file paths.

Note

The get_precision() function automatically detects whether your GPU supports INT4 or FP4 quantization. Use FP4 models for Blackwell GPUs (RTX 50-series) and INT4 models for other architectures.

Note

For Turing GPUs (e.g., NVIDIA 20-series), additional configuration is required:

  • Set torch_dtype=torch.float16 in both the transformer and pipeline initialization

  • Use transformer.set_attention_impl("nunchaku-fp16") to enable FP16 attention

  • Enable offloading with offload=True in the transformer and pipeline.enable_sequential_cpu_offload() if you do not have enough VRAM.