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.
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")
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 offload=True,
11 torch_dtype=torch.float16, # Turing GPUs only support fp16 precision
12) # set offload to False if you want to disable offloading
13transformer.set_attention_impl("nunchaku-fp16") # Turing GPUs only support fp16 attention
14pipeline = FluxPipeline.from_pretrained(
15 "black-forest-labs/FLUX.1-dev", transformer=transformer, torch_dtype=torch.float16
16) # no need to set the device here
17pipeline.enable_sequential_cpu_offload() # diffusers' offloading
18image = pipeline("A cat holding a sign that says hello world", num_inference_steps=50, guidance_scale=3.5).images[0]
19image.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.float16in both the transformer and pipeline initializationUse
transformer.set_attention_impl("nunchaku-fp16")to enable FP16 attentionEnable offloading with
offload=Truein the transformer andpipeline.enable_sequential_cpu_offload()if you do not have enough VRAM.