Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/source/en/_toctree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
title: bitsandbytes
- local: quantization/bfl
title: BFL (fp8r / nvfp4)
- local: quantization/entropack
title: EntroPack
- local: quantization/gguf
title: gguf
- local: quantization/nunchaku
Expand Down
4 changes: 4 additions & 0 deletions docs/source/en/api/quantization.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ Quantization techniques reduce memory and computational costs by representing we

[[autodoc]] quantizers.quantization_config.BitsAndBytesConfig

## EntroPackConfig

[[autodoc]] quantizers.quantization_config.EntroPackConfig

## GGUFQuantizationConfig

[[autodoc]] quantizers.quantization_config.GGUFQuantizationConfig
Expand Down
173 changes: 173 additions & 0 deletions docs/source/en/quantization/entropack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<!--Copyright 2026 The HuggingFace Team. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

-->

# EntroPack

[EntroPack](https://github.com/modelscope/entropack) compresses `nn.Linear` weights with lattice quantization and entropy coding. It supports fractional target rates, such as 3.5 bits per parameter (bpp), and does not require a calibration dataset.

Install EntroPack with the extra that matches your CUDA version. Diffusers requires EntroPack 0.1.9 or newer.

```bash
pip install --upgrade diffusers "entropack[cuda12]"
# CUDA 13
pip install --upgrade diffusers "entropack[cuda13]"
```

## Quantize a model

Pass an [`EntroPackConfig`] to [`~ModelMixin.from_pretrained`]. The linear weights are compressed as the checkpoint is loaded.

```py
import torch
from diffusers import EntroPackConfig, ZImageTransformer2DModel

model = ZImageTransformer2DModel.from_pretrained(
"Tongyi-MAI/Z-Image-Turbo",
subfolder="transformer",
quantization_config=EntroPackConfig(target_bpp=4.0),
torch_dtype=torch.bfloat16,
)
```

Only `torch.nn.Linear` weights are compressed. Embeddings and biases are left uncompressed; use `modules_to_not_convert` to keep specific projected layers in their original precision.

### Linear implementations

Set `linear_type` to choose how the decoded weight is used.

| `linear_type` | Layer | Computation | Minimum hardware |
|---|---|---|---|
| `"default"` | `CompressedLinear` | Reconstruct the weight and apply a regular linear operation | CUDA GPU |
| `"fp8"` | `CompressedFP8Linear` | FP8 E4M3FN weights and activations | CUDA SM8.9+ |
| `"int8"` | `CompressedINT8Linear` | Symmetric INT8 weights and activations | CUDA SM8.0+ |

```py
fp8_config = EntroPackConfig(target_bpp=4.0, linear_type="fp8")
int8_config = EntroPackConfig(target_bpp=4.0, linear_type="int8")
```

FP8 and INT8 checkpoints also store one FP32 scale per output channel.

### INT8 rotation (ConvRot)
Comment thread
sayakpaul marked this conversation as resolved.
Resolved

With `linear_type="int8"`, weights and activations are rotated with a block Hadamard rotation before
quantization by default (`use_convrot=True`). The rotation spreads outlier magnitudes across each block,
which improves reconstruction quality at low bitrates. `convrot_groupsize` requests the rotation block size
(a power of four, reduced by factors of four until it divides the layer's input dimension):

```py
int8_config = EntroPackConfig(target_bpp=4.0, linear_type="int8", use_convrot=True, convrot_groupsize=64)
```

The rotation setting is not part of the packed weights: it is saved in `config.json` and read back when
reloading. Pass `use_convrot=False` to disable rotation. Both settings are ignored for other linear types.

### Compression settings

`target_bpp` defaults to 4.0. The default linear implementation accepts rates from 0.001 through 11 bpp. The FP8 and INT8 implementations require a rate below 8 bpp.

Use `entropack_lossy_config` for additional settings accepted by EntroPack's lossy compression backend. See the [EntroPack configuration reference](https://github.com/modelscope/entropack/blob/main/docs/en/Usage/Configuration.md#entropacklossyconfig) for the available fields.

```py
quantization_config = EntroPackConfig(
target_bpp=4.0,
entropack_lossy_config={"row_rdo_iterations": 1},
)
```

Use `modules_to_not_convert` to keep selected modules uncompressed. A name matches the module itself and everything under it.

```py
quantization_config = EntroPackConfig(
target_bpp=4.0,
modules_to_not_convert=["all_final_layer", "t_embedder"],
)
```

## Quantize a pipeline

Use [`~quantizers.PipelineQuantizationConfig`] to quantize one or more pipeline components.

```py
import torch
from diffusers import EntroPackConfig, ZImagePipeline
from diffusers.quantizers import PipelineQuantizationConfig

quantization_config = PipelineQuantizationConfig(
quant_mapping={"transformer": EntroPackConfig(target_bpp=4.0)},
)
pipe = ZImagePipeline.from_pretrained(
"Tongyi-MAI/Z-Image-Turbo",
quantization_config=quantization_config,
torch_dtype=torch.bfloat16,
device_map="cuda",
)
image = pipe(
prompt="A cat holding a sign that says hello world",
num_inference_steps=8,
guidance_scale=0.0,
).images[0]
```

`device_map="cuda"` quantizes on the GPU during loading: fast, but temporarily needs extra GPU memory for the original plus quantized weights. If GPU memory is tight, drop `device_map` and call `pipeline.to("cuda")` after loading:

```py
pipe = ZImagePipeline.from_pretrained(
"Tongyi-MAI/Z-Image-Turbo",
quantization_config=quantization_config,
torch_dtype=torch.bfloat16,
)
pipe.to("cuda")
```

Without `device_map`, the layers are quantized on the CPU, which is slower but avoids the temporary GPU-memory spike.

## Speed, memory, and quality trade-offs

Lower target rates save memory but cost reconstruction quality. The entropy-coded (`default`) path reconstructs weights on every forward, adding a per-step decode overhead (within ~10% of bf16 on the setup below); the `fp8`/`int8` paths decode 8-bit codes and run low-precision tensor-core GEMMs, which can be faster than bf16 at some quality cost. Measured on Z-Image-Turbo (single H20, 1024x1024, 8 steps, fixed seed; PSNR against the bf16 output):

| config | PSNR vs bf16 | per-step latency | peak VRAM |
|---|---|---|---|
| bf16 | — | 536.8 ms | 21.69 GiB |
| `target_bpp=8.0` | 31.89 dB | 587.6 ms | 15.99 GiB |
| `target_bpp=4.0` | 21.61 dB | 576.6 ms | 13.01 GiB |
| `target_bpp=3.0` | 13.66 dB | 572.4 ms | 12.33 GiB |
| `target_bpp=4.0, linear_type="int8"` | 18.50 dB | 440.5 ms | 13.01 GiB |
| `target_bpp=4.0, linear_type="fp8"` | 23.46 dB | 460.0 ms | 13.01 GiB |

## Save and reload
Comment thread
sayakpaul marked this conversation as resolved.
Resolved

Save an EntroPack model with [`~ModelMixin.save_pretrained`]. The packed weights are saved in safetensors format, and the EntroPack settings are included in `config.json`.

```py
model.save_pretrained("zimage-entropack-4bpp")
```

Load the saved model without passing another [`EntroPackConfig`]. Diffusers reads the quantization settings from `config.json`.

```py
model = ZImageTransformer2DModel.from_pretrained(
"zimage-entropack-4bpp",
torch_dtype=torch.bfloat16,
)
```

## Dequantize

Call [`~ModelMixin.dequantize`] to restore dense `nn.Linear` layers from a quantized model, for example before merging adapters:

```py
model = model.dequantize()
```

EntroPack compression is lossy: dequantize returns the reconstructed weights, not the original pre-quantization weights.
1 change: 1 addition & 0 deletions docs/source/en/quantization/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ The following backends support loading prequantized checkpoints out of the box.
|---|---|
| [bitsandbytes](./bitsandbytes) | Config is saved in `config.json`; no extra arguments needed. Supports quantizing on the fly as well as loading prequantized checkpoints. |
| [torchao](./torchao) | Same as above. |
| [EntroPack](./entropack) | Config is saved in `config.json`; supports online quantization and loading prequantized checkpoints. |
| [GGUF](./gguf) | Uses `from_single_file` with Model classes; pipeline-level loading is not supported. |
| [BFL](./bfl) | Loads Black Forest Labs `fp8r` and `nvfp4` single-file checkpoints with `from_single_file`; pipeline-level loading is not supported. |
| [AutoRound](./autoround) | Only loading is supported; quantize first with the AutoRound CLI or Python API. |
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
"bitsandbytes>=0.43.3",
"nvidia_modelopt[hf]>=0.33.1",
"sdnq>=0.2.2",
"entropack>=0.1.9",
"regex!=2019.12.17",
"requests",
"tensorboard",
Expand Down Expand Up @@ -252,6 +253,7 @@ def run(self):
extras["torchao"] = deps_list("torchao", "accelerate")
extras["nvidia_modelopt"] = deps_list("nvidia_modelopt[hf]")
extras["sdnq"] = deps_list("sdnq")
extras["entropack"] = deps_list("entropack", "accelerate")
extras["flashpack"] = deps_list("flashpack")

extras["dev"] = extras["quality"] + extras["test"] + extras["training"] + extras["docs"] + extras["torch"]
Expand Down
5 changes: 4 additions & 1 deletion src/diffusers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
is_accelerate_available,
is_auto_round_available,
is_bitsandbytes_available,
is_entropack_available,
is_gguf_available,
is_librosa_available,
is_note_seq_available,
Expand Down Expand Up @@ -43,10 +44,11 @@
"modular_pipelines": [],
"pipelines": [],
"quantizers.pipe_quant_config": ["PipelineQuantizationConfig"],
"quantizers.quantization_config": [],
"quantizers.quantization_config": ["EntroPackConfig"],
"schedulers": [],
"utils": [
"OptionalDependencyNotAvailable",
"is_entropack_available",
"is_inflect_available",
"is_invisible_watermark_available",
"is_librosa_available",
Expand Down Expand Up @@ -1036,6 +1038,7 @@
from .utils.dummy_bitsandbytes_objects import *
else:
from .quantizers.quantization_config import BitsAndBytesConfig
from .quantizers.quantization_config import EntroPackConfig

try:
if not is_gguf_available():
Expand Down
1 change: 1 addition & 0 deletions src/diffusers/dependency_versions_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"bitsandbytes": "bitsandbytes>=0.43.3",
"nvidia_modelopt[hf]": "nvidia_modelopt[hf]>=0.33.1",
"sdnq": "sdnq>=0.2.2",
"entropack": "entropack>=0.1.9",
"regex": "regex!=2019.12.17",
"requests": "requests",
"tensorboard": "tensorboard",
Expand Down
4 changes: 4 additions & 0 deletions src/diffusers/quantizers/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
from .autoround import AutoRoundQuantizer
from .bfl import BFLQuantizer
from .bitsandbytes import BnB4BitDiffusersQuantizer, BnB8BitDiffusersQuantizer
from .entropack import EntroPackQuantizer
from .gguf import GGUFQuantizer
from .modelopt import NVIDIAModelOptQuantizer
from .nunchaku import NunchakuLiteQuantizer
from .quantization_config import (
AutoRoundConfig,
BFLQuantizationConfig,
BitsAndBytesConfig,
EntroPackConfig,
GGUFQuantizationConfig,
NunchakuLiteQuantizationConfig,
NVIDIAModelOptConfig,
Expand All @@ -46,6 +48,7 @@
"bfl": BFLQuantizer,
"bitsandbytes_4bit": BnB4BitDiffusersQuantizer,
"bitsandbytes_8bit": BnB8BitDiffusersQuantizer,
"entropack": EntroPackQuantizer,
"gguf": GGUFQuantizer,
"quanto": QuantoQuantizer,
"torchao": TorchAoHfQuantizer,
Expand All @@ -59,6 +62,7 @@
"bfl": BFLQuantizationConfig,
"bitsandbytes_4bit": BitsAndBytesConfig,
"bitsandbytes_8bit": BitsAndBytesConfig,
"entropack": EntroPackConfig,
"gguf": GGUFQuantizationConfig,
"quanto": QuantoConfig,
"torchao": TorchAoConfig,
Expand Down
4 changes: 4 additions & 0 deletions src/diffusers/quantizers/entropack/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .entropack_quantizer import EntroPackQuantizer


__all__ = ["EntroPackQuantizer"]
Loading
Loading