Text-to-Image
Diffusers
OpenVINO
ZImagePipeline
int4
optimum
optimum-intel
nncf
z-image
transformer
cpu
Instructions to use HelloSun/Z-Image-Turbo-OpenVINO-INT4 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use HelloSun/Z-Image-Turbo-OpenVINO-INT4 with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("HelloSun/Z-Image-Turbo-OpenVINO-INT4", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- Draw Things
- DiffusionBee
Add quantize_int4.py
Browse files- quantize_int4.py +49 -0
quantize_int4.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Quantize Z-Image-Turbo OV FP16 -> INT4 using optimum (NNCF weight-only)."""
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from optimum.intel import OVDiffusionPipeline, OVQuantizer
|
| 4 |
+
from optimum.intel.openvino.configuration import (
|
| 5 |
+
OVConfig,
|
| 6 |
+
OVWeightQuantizationConfig,
|
| 7 |
+
OVPipelineQuantizationConfig,
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
fp16_dir = "/home/user/app/z-image-turbo-ov-fp16"
|
| 11 |
+
int4_dir = "/home/user/app/z-image-turbo-ov-int4"
|
| 12 |
+
|
| 13 |
+
# INT4 config for transformer + text_encoder (main weights)
|
| 14 |
+
# group_size=128, sym=False is NNCF default sweet spot for quality/size
|
| 15 |
+
int4_config = OVWeightQuantizationConfig(
|
| 16 |
+
bits=4,
|
| 17 |
+
sym=False,
|
| 18 |
+
group_size=128,
|
| 19 |
+
group_size_fallback="adjust",
|
| 20 |
+
ratio=1.0,
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# Keep VAE in FP16 (don't quantize) by using default FP32? Actually set bits=8 for others,
|
| 24 |
+
# but we will explicitly only quantize transformer+text_encoder and copy rest as-is.
|
| 25 |
+
# Using OVPipelineQuantizationConfig:
|
| 26 |
+
pipeline_config = OVPipelineQuantizationConfig(
|
| 27 |
+
quantization_configs={
|
| 28 |
+
"transformer": int4_config,
|
| 29 |
+
"text_encoder": int4_config,
|
| 30 |
+
},
|
| 31 |
+
# default: 8-bit weight-only for any other submodel that gets quantized,
|
| 32 |
+
# but we skip vae by not including it? OVQuantizer quantizes only listed?
|
| 33 |
+
# To be safe, set default 8-bit; vae will be copied if not quantized?
|
| 34 |
+
default_config=OVWeightQuantizationConfig(bits=8, sym=True),
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
print(f"Loading FP16 OV pipeline from {fp16_dir} ...")
|
| 38 |
+
pipe = OVDiffusionPipeline.from_pretrained(fp16_dir)
|
| 39 |
+
print(f"Components: {list(pipe.components.keys())}")
|
| 40 |
+
print(f"OV submodels: {list(pipe.ov_submodels) if hasattr(pipe, 'ov_submodels') else 'n/a'}")
|
| 41 |
+
|
| 42 |
+
quantizer = OVQuantizer.from_pretrained(pipe)
|
| 43 |
+
print("Quantizing transformer+text_encoder to INT4 (NNCF, optimum)...")
|
| 44 |
+
ov_config = OVConfig(quantization_config=pipeline_config)
|
| 45 |
+
quantizer.quantize(
|
| 46 |
+
save_directory=int4_dir,
|
| 47 |
+
ov_config=ov_config,
|
| 48 |
+
)
|
| 49 |
+
print(f"Saved INT4 model to {int4_dir}")
|