Skip to content

Commit 777644e

Browse files
author
Felipe Sztutman
committed
feat: add Krea 2 W4A4 support
Adds SVDQuant W4A4 support for krea/Krea-2-Turbo through the existing fused low-bit kernels. No new CUDA was required; the current kernels already serve this shape, so the port is pure Python. The attention processor expands the grouped-query key/value heads explicitly rather than passing enable_gqa=True, because Krea 2 always supplies an attention mask and SDPA will not serve that flag together with a mask on the flash backend. It falls back to the math backend silently, at roughly 3x the cost end to end. Also adds the example script, usage and API docs, and an LPIPS regression test following the Z-Image layout.
1 parent 8f41840 commit 777644e

16 files changed

Lines changed: 411 additions & 2 deletions

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Join our user groups on [**Discord**](https://discord.gg/Wk6PnwX9Sm) and [**WeCh
1717

1818
## News
1919

20+
- **[2026-07-20]** 🔥 Release **4-bit [krea/Krea-2-Turbo](https://huggingface.co/krea/Krea-2-Turbo)**! W4A4 checkpoint on [Hugging Face](https://huggingface.co/felipesztutman/Krea-2-Turbo-W4A4-Nunchaku), and try it with our [example script](examples/v1/krea-2-turbo.py).
2021
- **[2026-01-12]** 🚀 **v1.2.0 Released!** Enjoy a **20–30% Z-Image performance boost**, seamless **LoRA support with native ComfyUI nodes**, **INT4 support for 20-series GPUs**, and **compatibility with ComfyUI 0.7**!
2122
- **[2025-12-20]** 🔥 Release **4-bit [Tongyi-MAI/Z-Image-Turbo](https://huggingface.co/Tongyi-MAI/Z-Image-Turbo)**! Download on [Hugging Face](https://huggingface.co/nunchaku-ai/nunchaku-z-image-turbo) or [ModelScope](https://modelscope.cn/models/nunchaku-tech/nunchaku-z-image-turbo), and try it with our [example script](examples/v1/z-image-turbo.py).
2223
- **[2025-09-25]** 🔥 Release **4-bit [4/8-step lightning Qwen-Image-Edit-2509](https://huggingface.co/lightx2v/Qwen-Image-Lightning)**! Download on [Hugging Face](https://huggingface.co/nunchaku-ai/nunchaku-qwen-image-edit-2509) or [ModelScope](https://modelscope.cn/models/nunchaku-tech/nunchaku-qwen-image-edit-2509), and try it with our [example script](examples/v1/qwen-image-edit-2509-lightning.py).

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Check out `DeepCompressor <github_deepcompressor_>`_ for the quantization librar
2828
usage/cache.rst
2929
usage/pulid.rst
3030
usage/ip_adapter.rst
31+
usage/krea2.rst
3132
usage/zimage.rst
3233

3334
.. toctree::
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
nunchaku.models.attention\_processors.krea2
2+
===========================================
3+
4+
.. automodule:: nunchaku.models.attention_processors.krea2
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:

docs/source/python_api/nunchaku.models.attention_processors.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ nunchaku.models.attention_processors
66

77
nunchaku.models.attention_processors.flux
88
nunchaku.models.attention_processors.qwenimage
9+
nunchaku.models.attention_processors.krea2
910
nunchaku.models.attention_processors.zimage

docs/source/python_api/nunchaku.models.transformers.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ nunchaku.models.transformers
77
nunchaku.models.transformers.transformer_flux
88
nunchaku.models.transformers.transformer_flux_v2
99
nunchaku.models.transformers.transformer_qwenimage
10+
nunchaku.models.transformers.transformer_krea2
1011
nunchaku.models.transformers.transformer_zimage
1112
nunchaku.models.transformers.transformer_sana
1213
nunchaku.models.transformers.utils
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
nunchaku.models.transformers.transformer\_krea2
2+
===============================================
3+
4+
.. automodule:: nunchaku.models.transformers.transformer_krea2
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:

docs/source/usage/krea2.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Krea 2
2+
======
3+
4+
The following is the example of running Nunchaku version of Krea 2 text-to-image pipeline.
5+
6+
.. tabs::
7+
8+
.. tab:: Krea-2-Turbo
9+
10+
.. literalinclude:: ../../../examples/v1/krea-2-turbo.py
11+
:language: python
12+
:caption: Running Krea-2-Turbo (`examples/v1/krea-2-turbo.py <https://github.com/nunchaku-tech/nunchaku/blob/main/examples/v1/krea-2-turbo.py>`__)
13+
:linenos:
14+
15+
.. note::
16+
17+
Krea 2 uses grouped-query attention and always passes an attention mask, because text and
18+
image share one sequence. PyTorch SDPA will not serve ``enable_gqa=True`` together with a
19+
mask on the flash backend and falls back to the math backend silently, which costs roughly
20+
3x. The processor therefore expands the key/value heads explicitly.
21+
22+
For more details, see :class:`~nunchaku.models.transformers.transformer_krea2.NunchakuKrea2Transformer2DModel`.

examples/v1/krea-2-turbo.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import torch
2+
from diffusers import Krea2Pipeline
3+
4+
from nunchaku import NunchakuKrea2Transformer2DModel
5+
from nunchaku.utils import get_precision, is_turing
6+
7+
if __name__ == "__main__":
8+
precision = get_precision() # auto-detect your precision is 'int4' or 'fp4' based on your GPU
9+
rank = 32 # Rank of the SVDQuant low-rank branch that absorbs the outlier activations
10+
dtype = torch.float16 if is_turing() else torch.bfloat16 # Use float16 when Turing (20- series) GPU is used.
11+
transformer = NunchakuKrea2Transformer2DModel.from_pretrained(
12+
f"felipesztutman/Krea-2-Turbo-W4A4-Nunchaku/svdq-{precision}_r{rank}-krea-2-turbo.safetensors",
13+
torch_dtype=dtype,
14+
)
15+
16+
pipe = Krea2Pipeline.from_pretrained(
17+
"krea/Krea-2-Turbo", transformer=transformer, torch_dtype=dtype, low_cpu_mem_usage=False
18+
)
19+
pipe.enable_sequential_cpu_offload() # enable sequential CPU offload for low vram
20+
# pipe = pipe.to("cuda") # or else comment the line above and uncomment this line to put all components to GPU
21+
22+
prompt = "a red fox in deep snow at dusk, backlit, shallow depth of field"
23+
24+
image = pipe(
25+
prompt=prompt,
26+
height=1024,
27+
width=1024,
28+
num_inference_steps=8,
29+
guidance_scale=0.0, # Guidance should be 0 for the Turbo models
30+
generator=torch.Generator().manual_seed(1000),
31+
).images[0]
32+
33+
image.save(f"krea-2-turbo-{precision}_r{rank}_{str(dtype)}.png")

nunchaku/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from .models import (
22
NunchakuFluxTransformer2dModel,
33
NunchakuFluxTransformer2DModelV2,
4+
NunchakuKrea2Transformer2DModel,
45
NunchakuQwenImageTransformer2DModel,
56
NunchakuSanaTransformer2DModel,
67
NunchakuT5EncoderModel,
@@ -13,5 +14,6 @@
1314
"NunchakuT5EncoderModel",
1415
"NunchakuFluxTransformer2DModelV2",
1516
"NunchakuQwenImageTransformer2DModel",
17+
"NunchakuKrea2Transformer2DModel",
1618
"NunchakuZImageTransformer2DModel",
1719
]

nunchaku/merge_safetensors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def merge_safetensors(
4848
pretrained_model_name_or_path : str or os.PathLike
4949
Path to the model directory or HuggingFace repo.
5050
model_class : str
51-
Specify model class. E.g. NunchakuFluxTransformer2dModel or NunchakuZImageTransformer2DModel
51+
Specify model class. E.g. NunchakuFluxTransformer2dModel, NunchakuZImageTransformer2DModel or NunchakuKrea2Transformer2DModel
5252
**kwargs
5353
Additional keyword arguments for subfolder, comfy_config_path, and HuggingFace download options.
5454
@@ -163,7 +163,7 @@ def merge_safetensors(
163163
"--model-class",
164164
type=str,
165165
required=True,
166-
help="Specify model class. E.g. NunchakuFluxTransformer2dModel or NunchakuZImageTransformer2DModel",
166+
help="Specify model class. E.g. NunchakuFluxTransformer2dModel, NunchakuZImageTransformer2DModel or NunchakuKrea2Transformer2DModel",
167167
)
168168
parser.add_argument("-o", "--output-path", type=Path, required=True, help="Path to output path")
169169
args = parser.parse_args()

0 commit comments

Comments
 (0)