Update README.md
Browse files
README.md
CHANGED
|
@@ -1,191 +1,213 @@
|
|
| 1 |
-
---
|
| 2 |
-
language:
|
| 3 |
-
- en
|
| 4 |
-
license: other
|
| 5 |
-
library_name: diffusers
|
| 6 |
-
pipeline_tag: text-to-image
|
| 7 |
-
tags:
|
| 8 |
-
- text-to-image
|
| 9 |
-
- diffusers
|
| 10 |
-
- quanto
|
| 11 |
-
- int8
|
| 12 |
-
- z-image
|
| 13 |
-
- transformer-quantization
|
| 14 |
-
base_model:
|
| 15 |
-
- Tongyi-MAI/Z-Image
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
-
|
| 23 |
-
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
- **
|
| 31 |
-
- **
|
| 32 |
-
- **
|
| 33 |
-
- **
|
| 34 |
-
- **
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
- `
|
| 41 |
-
- `
|
| 42 |
-
- `
|
| 43 |
-
- `
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
#
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
-
|
| 134 |
-
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
|
| 163 |
-
|
| 164 |
-
|
|
| 165 |
-
|
|
| 166 |
-
|
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
license: other
|
| 5 |
+
library_name: diffusers
|
| 6 |
+
pipeline_tag: text-to-image
|
| 7 |
+
tags:
|
| 8 |
+
- text-to-image
|
| 9 |
+
- diffusers
|
| 10 |
+
- quanto
|
| 11 |
+
- int8
|
| 12 |
+
- z-image
|
| 13 |
+
- transformer-quantization
|
| 14 |
+
base_model:
|
| 15 |
+
- Tongyi-MAI/Z-Image
|
| 16 |
+
---
|
| 17 |
+
|
| 18 |
+
# Z-Image INT8 (Quanto)
|
| 19 |
+
|
| 20 |
+
This repository provides an INT8-quantized variant of [Tongyi-MAI/Z-Image](https://huggingface.co/Tongyi-MAI/Z-Image):
|
| 21 |
+
- **Only** the `transformer` is quantized with **Quanto weight-only INT8**.
|
| 22 |
+
- `text_encoder`, `vae`, `scheduler`, and `tokenizer` remain unchanged.
|
| 23 |
+
- Inference API stays compatible with `diffusers.ZImagePipeline`.
|
| 24 |
+
|
| 25 |
+
> Please follow the original upstream model license and usage terms. `license: other` means this repo inherits upstream licensing constraints.
|
| 26 |
+
|
| 27 |
+
## Model Details
|
| 28 |
+
|
| 29 |
+
- **Base model**: `Tongyi-MAI/Z-Image`
|
| 30 |
+
- **Quantization method**: `optimum-quanto` (weight-only INT8)
|
| 31 |
+
- **Quantized part**: `transformer`
|
| 32 |
+
- **Compute dtype**: `bfloat16`
|
| 33 |
+
- **Pipeline**: `diffusers.ZImagePipeline`
|
| 34 |
+
- **Negative prompt support**: Yes (same pipeline API as the base model)
|
| 35 |
+
|
| 36 |
+
## Files
|
| 37 |
+
|
| 38 |
+
Key files in this repository:
|
| 39 |
+
- `model_index.json`
|
| 40 |
+
- `transformer/diffusion_pytorch_model.safetensors` (INT8-quantized weights)
|
| 41 |
+
- `text_encoder/*`, `vae/*`, `scheduler/*`, `tokenizer/*` (not quantized)
|
| 42 |
+
- `zimage_quanto_bench_results/*` (benchmark metrics and baseline-vs-int8 images)
|
| 43 |
+
- `test_outputs/*` (generated examples)
|
| 44 |
+
|
| 45 |
+
## Installation
|
| 46 |
+
|
| 47 |
+
Python 3.10+ is recommended.
|
| 48 |
+
|
| 49 |
+
```bash
|
| 50 |
+
# Create env (optional)
|
| 51 |
+
python -m venv .venv
|
| 52 |
+
|
| 53 |
+
# Windows
|
| 54 |
+
.venv\Scripts\activate
|
| 55 |
+
|
| 56 |
+
# Linux/macOS
|
| 57 |
+
# source .venv/bin/activate
|
| 58 |
+
|
| 59 |
+
python -m pip install --upgrade pip
|
| 60 |
+
|
| 61 |
+
# PyTorch (NVIDIA CUDA, example)
|
| 62 |
+
pip install torch --index-url https://download.pytorch.org/whl/cu128
|
| 63 |
+
|
| 64 |
+
# PyTorch (macOS / CPU-only example)
|
| 65 |
+
# pip install torch
|
| 66 |
+
|
| 67 |
+
# Inference dependencies
|
| 68 |
+
pip install diffusers transformers accelerate safetensors sentencepiece optimum-quanto pillow
|
| 69 |
+
|
| 70 |
+
# Recommended minimum versions (helps avoid backend compatibility issues)
|
| 71 |
+
pip install -U "torch>=2.4" "diffusers>=0.36.0" "accelerate>=0.33"
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
## Quick Start (Diffusers)
|
| 75 |
+
|
| 76 |
+
This repo already stores quantized weights, so you do **not** need to re-run quantization during loading.
|
| 77 |
+
|
| 78 |
+
```python
|
| 79 |
+
import torch
|
| 80 |
+
from diffusers import ZImagePipeline
|
| 81 |
+
|
| 82 |
+
model_id = "ixim/Z-Image-INT8"
|
| 83 |
+
|
| 84 |
+
if torch.cuda.is_available():
|
| 85 |
+
device = "cuda"
|
| 86 |
+
dtype = torch.bfloat16
|
| 87 |
+
elif torch.backends.mps.is_available():
|
| 88 |
+
# Apple Silicon
|
| 89 |
+
device = "mps"
|
| 90 |
+
dtype = torch.float16
|
| 91 |
+
else:
|
| 92 |
+
# Intel Mac / CPU-only
|
| 93 |
+
device = "cpu"
|
| 94 |
+
dtype = torch.float32
|
| 95 |
+
|
| 96 |
+
pipe = ZImagePipeline.from_pretrained(
|
| 97 |
+
model_id,
|
| 98 |
+
torch_dtype=dtype,
|
| 99 |
+
low_cpu_mem_usage=True,
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
if device == "cuda":
|
| 103 |
+
pipe.enable_model_cpu_offload()
|
| 104 |
+
else:
|
| 105 |
+
pipe = pipe.to(device)
|
| 106 |
+
|
| 107 |
+
prompt = "A cinematic portrait of a young woman, soft lighting, high detail"
|
| 108 |
+
negative_prompt = "blurry, low quality, distorted face, extra limbs, artifacts"
|
| 109 |
+
# Use CPU generator for best cross-device compatibility (cpu/mps/cuda)
|
| 110 |
+
generator = torch.Generator(device="cpu").manual_seed(42)
|
| 111 |
+
|
| 112 |
+
image = pipe(
|
| 113 |
+
prompt=prompt,
|
| 114 |
+
negative_prompt=negative_prompt,
|
| 115 |
+
height=1024,
|
| 116 |
+
width=1024,
|
| 117 |
+
num_inference_steps=28,
|
| 118 |
+
guidance_scale=4.0,
|
| 119 |
+
generator=generator,
|
| 120 |
+
).images[0]
|
| 121 |
+
|
| 122 |
+
image.save("zimage_int8_sample.png")
|
| 123 |
+
print("Saved: zimage_int8_sample.png")
|
| 124 |
+
```
|
| 125 |
+
|
| 126 |
+
## macOS Notes & Troubleshooting
|
| 127 |
+
|
| 128 |
+
- `AttributeError: module 'torch' has no attribute 'xpu'` is usually a backend/version compatibility issue in the local environment, not a model issue.
|
| 129 |
+
- Fix it by upgrading to recent versions:
|
| 130 |
+
- `pip install -U "torch>=2.4" "diffusers>=0.36.0" "accelerate>=0.33"`
|
| 131 |
+
- On Apple Silicon, warnings like `CUDA not available` and `Disabling autocast` are expected in non-CUDA execution paths.
|
| 132 |
+
- Slow speed on Mac is expected compared with high-end NVIDIA GPUs. To improve speed on Apple Silicon:
|
| 133 |
+
- Ensure the script uses `mps` (as in the example above), not `cpu`.
|
| 134 |
+
- Start from `height=512`, `width=512`, and fewer steps (e.g., `20~28`) before scaling up.
|
| 135 |
+
|
| 136 |
+
## Additional Generated Samples (INT8)
|
| 137 |
+
|
| 138 |
+
These two images are generated with this quantized model:
|
| 139 |
+
|
| 140 |
+
### 1) `en_portrait_1024x1024.png`
|
| 141 |
+
|
| 142 |
+
- **Prompt**: `A cinematic portrait of a young woman standing by the window, golden hour sunlight, shallow depth of field, film grain, ultra-detailed skin texture, photorealistic`
|
| 143 |
+
|
| 144 |
+
<div align="center"><img src="test_outputs/en_portrait_1024x1024.png" width="512" /></div>
|
| 145 |
+
|
| 146 |
+
### 2) `cn_scene_1024x1024.png`
|
| 147 |
+
|
| 148 |
+
- **Prompt**: `一只橘猫趴在堆满旧书的木桌上打盹,午后阳光透过窗帘洒进来,暖色调,胶片风格,细腻毛发纹理,超高清`
|
| 149 |
+
|
| 150 |
+
<div align="center"><img src="test_outputs/cn_scene_1024x1024.png" width="512" /></div>
|
| 151 |
+
|
| 152 |
+
## Benchmark & Performance
|
| 153 |
+
|
| 154 |
+
Test environment:
|
| 155 |
+
- GPU: NVIDIA GeForce RTX 5090
|
| 156 |
+
- Framework: PyTorch 2.10.0+cu130
|
| 157 |
+
- Inference setting: 1024×1024, 28 steps, guidance=4.0, CPU offload enabled
|
| 158 |
+
- Cases: 4 prompts (`portrait_01`, `portrait_02`, `scene_01`, `night_01`)
|
| 159 |
+
|
| 160 |
+
### Aggregate Comparison (Baseline vs INT8)
|
| 161 |
+
|
| 162 |
+
| Metric | Baseline | INT8 | Delta |
|
| 163 |
+
|---|---:|---:|---:|
|
| 164 |
+
| Avg elapsed / image (s) | 51.7766 | 39.5662 | **-23.6%** |
|
| 165 |
+
| Avg sec / step | 1.8492 | 1.4131 | **-23.6%** |
|
| 166 |
+
| Avg peak CUDA alloc (GB) | 12.5195 | 7.7470 | **-38.1%** |
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
> Results may vary across hardware, drivers, and PyTorch/CUDA versions.
|
| 170 |
+
|
| 171 |
+
### Per-Case Results
|
| 172 |
+
|
| 173 |
+
| Case | Baseline (s) | INT8 (s) | Speedup |
|
| 174 |
+
|---|---:|---:|---:|
|
| 175 |
+
| portrait_01 | 99.9223 | 60.6768 | 1.65x |
|
| 176 |
+
| portrait_02 | 37.4116 | 32.8863 | 1.14x |
|
| 177 |
+
| scene_01 | 34.9946 | 32.2035 | 1.09x |
|
| 178 |
+
| night_01 | 34.7780 | 32.4981 | 1.07x |
|
| 179 |
+
|
| 180 |
+
## Visual Comparison (Baseline vs INT8)
|
| 181 |
+
|
| 182 |
+
Left: Baseline. Right: INT8. (Same prompt/seed/steps.)
|
| 183 |
+
|
| 184 |
+
| Case | Base | INT8 |
|
| 185 |
+
|---|---|---|
|
| 186 |
+
| portrait_01 |  |  |
|
| 187 |
+
| portrait_02 |  |  |
|
| 188 |
+
| scene_01 |  |  |
|
| 189 |
+
| night_01 |  |  |
|
| 190 |
+
|
| 191 |
+
## Limitations
|
| 192 |
+
|
| 193 |
+
- This is **weight-only INT8** quantization; activation precision is unchanged.
|
| 194 |
+
- Minor visual differences may appear on some prompts.
|
| 195 |
+
- `enable_model_cpu_offload()` can change latency distribution across pipeline stages.
|
| 196 |
+
- For extreme resolutions / very long step counts, validate quality and stability first.
|
| 197 |
+
|
| 198 |
+
## Intended Use
|
| 199 |
+
|
| 200 |
+
Recommended for:
|
| 201 |
+
- Running Z-Image with lower VRAM usage.
|
| 202 |
+
- Improving throughput while keeping quality close to baseline.
|
| 203 |
+
|
| 204 |
+
Not recommended as-is for:
|
| 205 |
+
- Safety-critical decision workflows.
|
| 206 |
+
- High-risk generation use cases without additional review/guardrails.
|
| 207 |
+
|
| 208 |
+
## Citation
|
| 209 |
+
|
| 210 |
+
If you use this model, please cite/reference the upstream model and toolchain:
|
| 211 |
+
- Tongyi-MAI/Z-Image
|
| 212 |
+
- Hugging Face Diffusers
|
| 213 |
+
- optimum-quanto
|