HelloSun's picture
docs: restructure README/REPORT and clean metadata
eab6841
|
Raw History Blame Contribute Delete
8.52 kB

Z-Image-Turbo — OpenVINO INT4:轉換與實測報告

項目 內容
來源模型 Tongyi-MAI/Z-Image-Turbo
Pipeline 類型 ZImagePipeline
Scheduler FlowMatchEulerDiscreteScheduler(shift=3.0)
目標格式 OpenVINO IR;Transformer 與 text encoder 為 INT4,VAE 為 INT8
發布位置 HelloSun/Z-Image-Turbo-OpenVINO-INT4

目錄

  1. 環境與版本
  2. 轉換流程
  3. 模型檔案與體積
  4. 實測結果
  5. 測試 Prompt
  6. 觀察與結論
  7. 復現步驟

1. 環境與版本

diffusers==0.37.1 transformers==4.57.6 tokenizers==0.22.0 huggingface-hub==0.35.1 optimum==2.3.0 optimum-intel==2.2.0 openvino==2026.4.0 nncf==3.4.0 torch pillow psutil

2. 轉換流程

步驟 1 — 匯出 OpenVINO FP16

optimum-cli export openvino -m Tongyi-MAI/Z-Image-Turbo --task text-to-image --library diffusers --weight-format fp16 ./z-image-turbo-ov-fp16

步驟 2 — NNCF weight-only 量化

由 quantize_int4.py 執行。

from optimum.intel.openvino.configuration import (
    OVConfig, OVWeightQuantizationConfig, OVPipelineQuantizationConfig,
)

int4_config = OVWeightQuantizationConfig(
    bits=4, sym=False, group_size=128,
    group_size_fallback="adjust", ratio=1.0,
)

pipeline_config = OVPipelineQuantizationConfig(
    quantization_configs={
        "transformer":  int4_config,
        "text_encoder": int4_config,
    },
    default_config=OVWeightQuantizationConfig(bits=8, sym=True),
)

重點說明

  • bits=4、sym=False → 非對稱 4-bit weight-only 量化。
  • group_size=128 → 每 128 個權重共用一組 scale / zero-point。
  • group_size_fallback="adjust" → 分組不整除時自動調整群組大小,避免部分層被跳過。
  • ratio=1.0 → 全部權重都量化,不做混合精度保留。
  • VAE 沿用 default_config 的 INT8 對稱量化。
  • 注意:openvino_config.json 的 default_config 只記錄了 quant_method,沒有寫入 bits=8;「VAE 為 INT8」這項資訊只能從 quantize_int4.py 得知。

3. 模型檔案與體積

元件 精度 說明
transformer INT4 ZImageTransformer2DModel(6B,dim 3840 / 30 層)
text_encoder INT4 Qwen3ForCausalLM 語言模型(hidden 2560 / 36 層)
vae_decoder INT8 由 latent 解回 RGB
vae_encoder INT8 image encoder(推論時未實際使用)
tokenizer — Qwen2Tokenizer,未量化
scheduler — FlowMatchEulerDiscreteScheduler,shift=3.0

實際體積

下表為 repo 內 openvino_model.bin 的實際位元組數(Git LFS 記錄值),可直接對照下載量。

元件 位元組 GB GiB
transformer 3,199,343,400 3.20 GB 2.98 GiB
text_encoder 2,236,949,085 2.24 GB 2.08 GiB
vae_decoder 49,612,856 0.05 GB 0.05 GiB
vae_encoder 34,325,660 0.03 GB 0.03 GiB
合計 5,520,231,001 5.52 GB 5.14 GiB

FP16 匯出模型約 20.32 GB(transformer 12.31 GB + text_encoder 7.84 GB + vae_decoder 99 MB + vae_encoder 69 MB)——取自 examples/benchmark.json 的 sysinfo.model_sizes_GB。 ⚠️ 早期說明中的 19 GB / 5.2 GB 與實測不符:實際為 20.32 GB / 5.52 GB,本表為準。 INT4 後的 text_encoder 仍有 2.24 GB,因為它是 Qwen3-4B 級別的語言模型,即使 4-bit 量化體積依然可觀。

4. 實測結果

項目 數值
樣本數 5
推論設定 num_inference_steps=9、guidance_scale=0.0、1024×1024 + 512×512 對照
from_pretrained + compile=True 7.72 s
平均總耗時 111.18 s
平均單步耗時 12.15 s
512px 對照組平均 33.46 s(單步 3.67 s)
1024 / 512 時間比 3.32×

逐張結果

# Prompt Seed steps CFG 總耗時 (s) 平均單步 (s) 逐 step 耗時 (s)
01_hanfu_portrait 42 116.19 12.696 17.97, 12.20, 11.94, 12.37, 11.71, 12.20, 12.08, 12.28, 11.50 38.72 16.31
02_astronaut_jungle 43 110.82 12.112 12.93, 12.16, 11.53, 11.87, 11.92, 12.33, 12.16, 12.03, 12.08 33.04 16.31
03_taipei_cyberpunk 44 110.31 12.066 13.60, 12.21, 12.00, 12.13, 12.00, 11.88, 11.50, 11.60, 11.68 31.81 16.32
04_shiba_astronaut 45 109.30 11.943 12.61, 11.89, 11.92, 11.79, 11.79, 11.99, 12.01, 11.59, 11.89 31.05 16.32
05_ink_landscape 46 109.26 11.941 13.35, 11.92, 11.67, 11.51, 11.70, 11.40, 11.92, 12.10, 11.91 32.69 16.32
平均 111.18 12.152 33.46 16.32

逐 step 耗時(主測組)

# Prompt Seed 總耗時 (s) 平均單步 (s) 逐 step 耗時 (s)
01 hanfu_portrait 42 116.19 12.696 17.97, 12.20, 11.94, 12.37, 11.71, 12.20, 12.08, 12.28, 11.50
02 astronaut_jungle 43 110.82 12.112 12.93, 12.16, 11.53, 11.87, 11.92, 12.33, 12.16, 12.03, 12.08
03 taipei_cyberpunk 44 110.31 12.066 13.60, 12.21, 12.00, 12.13, 12.00, 11.88, 11.50, 11.60, 11.68
04 shiba_astronaut 45 109.30 11.943 12.61, 11.89, 11.92, 11.79, 11.79, 11.99, 12.01, 11.59, 11.89
05 ink_landscape 46 109.26 11.941 13.35, 11.92, 11.67, 11.51, 11.70, 11.40, 11.92, 12.10, 11.91

「總耗時」包含文字編碼與 VAE decode;「平均單步」= 逐 step 耗時加總 ÷ 步數, 因此會略小於 總耗時 ÷ 步數。

5. 測試 Prompt

# Seed Prompt
01 42 Young Chinese woman in red Hanfu, intricate embroidery, impeccable makeup, red floral forehead pattern, elaborate high bun, golden phoenix headdress, soft-lit outdoor night background, silhouetted tiered pagoda, blurred colorful distant lights, photorealistic, ultra detailed, 8k
02 43 Astronaut in a jungle, cold color palette, muted colors, detailed, 8k, photorealistic, cinematic lighting
03 44 Cyberpunk street in Taipei at night, heavy rain, neon signs with text 'TAIPEI' and Chinese characters '台北', reflections on wet asphalt, crowded night market, cinematic, ultra detailed
04 45 Cute Shiba Inu wearing a tiny astronaut helmet, sitting in a field of sunflowers under a starry sky, dreamy illustration, vibrant colors, high quality
05 46 Traditional Chinese ink wash landscape, misty mountains, a small pagoda on a cliff, cranes flying, minimalist, elegant, high aesthetic quality

完整內容另見 examples/prompts.txt。

6. 觀察與結論

  1. 首圖首步開銷明顯:第一張的第 1 個 step 為 17.97 s,之後各張的首步落在 12.6–13.6 s,後續步驟穩定在 11.4–12.4 s。首圖包含 text_encoder 首次執行與 Transformer warmup。
  2. 1024px 約為 512px 的 3.3× 時間:略低於像素數的 4×,固定 overhead 在高解析度下被攤薄。
  3. 記憶體平台期:RSS 於第一張圖後為 16.306 GB,其後四張在 16.313–16.325 GB 之間,無持續成長,未觀察到洩漏。
  4. 總耗時比逐 step 加總多約 1.7–1.9 s:為文字編碼與 VAE decode 的固定成本。
  5. INT4 後 text_encoder 仍佔 2.24 GB:Qwen3 級別的語言模型即使 4-bit 量化體積依然可觀,是整體壓縮率(~73%)的主要限制來源。

7. 復現步驟

# 1. 匯出 FP16 OpenVINO 模型
optimum-cli export openvino \
  -m Tongyi-MAI/Z-Image-Turbo \
  --task text-to-image \
  --library diffusers \
  --weight-format fp16 \
  ./z-image-turbo-ov-fp16

# 2. NNCF weight-only INT4 量化
python quantize_int4.py --fp16-dir ./z-image-turbo-ov-fp16 \
                          --int4-dir  ./z-image-turbo-ov-int4

# 3. 單張推論
python inference_int4.py

# 4. 批次生成 5 組 + 512px 對照 + benchmark
python generate5.py --outdir examples

量化腳本會另外輸出:


原始機器紀錄為 examples/benchmark.json。