HelloSun commited on
Commit
b13fca7
·
verified ·
1 Parent(s): 1616bdd

Add README.md

Browse files
Files changed (1) hide show
  1. README.md +182 -0
README.md ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ library_name: optimum
4
+ tags:
5
+ - openvino
6
+ - int4
7
+ - nncf
8
+ - flux
9
+ - text-to-image
10
+ - photorealistic
11
+ - black-forest-labs
12
+ - cpu-inference
13
+ pipeline_tag: text-to-image
14
+ base_model: black-forest-labs/FLUX.1-schnell
15
+ ---
16
+
17
+ # FLUX.1-schnell · OpenVINO INT4 (CPU)
18
+
19
+ [black-forest-labs/FLUX.1-schnell](https://huggingface.co/black-forest-labs/FLUX.1-schnell) 轉成 OpenVINO IR,
20
+ `transformer`、`text_encoder`、`text_encoder_2` 以 NNCF 做 **weight-only INT4**(asymmetric, group_size 128),
21
+ 其餘組件 INT8。**不需要 GPU**,純 CPU 即可生圖。
22
+
23
+ - 權重體積 **31.4 GB → 8.3 GB(-73.5%)**
24
+ - 1024×1024 / 4 steps / CFG 0.0:**~67 s 出一張(16.3 s/step)**(Xeon Platinum 8559C, 16 vCPU)
25
+ - 512×512 / 4 steps:**~20 s 出一張(4.9 s/step)**
26
+ - load + compile:**11.8 s**
27
+
28
+ 轉換環境:openvino 2026.4.0 / nncf 3.4.0 / optimum 2.3.0 / optimum-intel 2.2.0 / diffusers 0.39.0 /
29
+ transformers 5.5.4 / tokenizers 0.22.2 / huggingface-hub 1.21.0 / torch 2.14.1 / pillow 12.3.0 / psutil 7.2.2
30
+
31
+ ---
32
+
33
+ ## 模型頁展示(10 張)
34
+
35
+ ### 1024 × 1024(4 steps, CFG 0.0, max_sequence_length=256)
36
+
37
+ | seed 42 · 01_hanfu | seed 43 · 02_astronaut |
38
+ | --- | --- |
39
+ | ![](examples/01_hanfu.png) | ![](examples/02_astronaut.png) |
40
+
41
+ | seed 44 · 03_taipei | seed 45 · 04_shiba |
42
+ | --- | --- |
43
+ | ![](examples/03_taipei.png) | ![](examples/04_shiba.png) |
44
+
45
+ | seed 46 · 05_ink | |
46
+ | --- | --- |
47
+ | ![](examples/05_ink.png) | |
48
+
49
+ ### 512 × 512 對照組(同 prompt / 同 seed)
50
+
51
+ | 01_hanfu_512 | 02_astronaut_512 | 03_taipei_512 |
52
+ | --- | --- | --- |
53
+ | ![](examples/01_hanfu_512.png) | ![](examples/02_astronaut_512.png) | ![](examples/03_taipei_512.png) |
54
+
55
+ | 04_shiba_512 | 05_ink_512 |
56
+ | --- | --- |
57
+ | ![](examples/04_shiba_512.png) | ![](examples/05_ink_512.png) |
58
+
59
+ Prompts 見 [`outputs/prompts.txt`](outputs/prompts.txt)。
60
+
61
+ ---
62
+
63
+ ## 安裝
64
+
65
+ ```bash
66
+ pip install -U optimum optimum-intel openvino nncf \
67
+ diffusers transformers tokenizers huggingface-hub \
68
+ torch pillow psutil
69
+ ```
70
+
71
+ ## 用法
72
+
73
+ ```python
74
+ import torch
75
+ from optimum.intel import OVFluxPipeline
76
+
77
+ pipe = OVFluxPipeline.from_pretrained(
78
+ "HelloSun/FLUX.1-schnell-OpenVINO-INT4",
79
+ compile=True,
80
+ device="CPU",
81
+ )
82
+
83
+ prompt = (
84
+ "Young Chinese woman in red Hanfu, intricate embroidery, impeccable makeup, "
85
+ "red floral forehead pattern, elaborate high bun, golden phoenix headdress, "
86
+ "soft-lit outdoor night background, silhouetted tiered pagoda, blurred colorful "
87
+ "distant lights, photorealistic, ultra detailed, 8k"
88
+ )
89
+ generator = torch.Generator(device="cpu").manual_seed(42)
90
+
91
+ image = pipe(
92
+ prompt=prompt,
93
+ negative_prompt="",
94
+ width=1024, height=1024,
95
+ num_inference_steps=4,
96
+ guidance_scale=0.0,
97
+ max_sequence_length=256,
98
+ generator=generator,
99
+ ).images[0]
100
+ image.save("hanfu.png")
101
+ ```
102
+
103
+ 完整腳本:
104
+
105
+ | 檔案 | 用途 |
106
+ | --- | --- |
107
+ | [`inference_int4_flux.py`](inference_int4_flux.py) | 單張 txt2img 推論 |
108
+ | [`quantize_int4_flux.py`](quantize_int4_flux.py) | 由 FP16 pipeline 產生 INT4 pipeline(OVQuantizer + NNCF) |
109
+ | [`generate5_flux.py`](generate5_flux.py) | 5 組固定 seed 批次生成,callback 記錄每 step 時間與 RSS |
110
+ | [`REPORT.md`](REPORT.md) | 完整轉換與實驗報告 |
111
+ | [`outputs/benchmark.json`](outputs/benchmark.json) | 機器資訊 + 每 step 時間/RSS 原始數據 |
112
+ | [`outputs/prompts.txt`](outputs/prompts.txt) | 全部 prompt / negative prompt / seed |
113
+
114
+ ### 轉換步驟(重現本 repo)
115
+
116
+ ```bash
117
+ # 1) 匯出 FP16
118
+ optimum-cli export openvino -m black-forest-labs/FLUX.1-schnell \
119
+ --task text-to-image --library diffusers --weight-format fp16 \
120
+ ./flux-schnell-ov-fp16
121
+
122
+ # 2) NNCF weight-only INT4(transformer + text_encoder + text_encoder_2 INT4,其餘 INT8)
123
+ python quantize_int4_flux.py \
124
+ --model_path ./flux-schnell-ov-fp16 \
125
+ --output_path ./flux-schnell-ov-int4
126
+ ```
127
+
128
+ 量化設定(`quantize_int4_flux.py` 內):
129
+
130
+ ```python
131
+ int4 = dict(bits=4, sym=False, group_size=128, group_size_fallback="adjust", ratio=1.0)
132
+ OVPipelineQuantizationConfig(
133
+ quantization_configs={
134
+ "transformer": OVWeightQuantizationConfig(**int4),
135
+ "text_encoder": OVWeightQuantizationConfig(**int4),
136
+ "text_encoder_2": OVWeightQuantizationConfig(**int4),
137
+ },
138
+ default_config=OVWeightQuantizationConfig(bits=8),
139
+ )
140
+ ```
141
+
142
+ ## 實驗數據
143
+
144
+ 環境:Intel Xeon Platinum 8559C(2 socket / 96 core / 192 thread,容器 cgroup 限 16 vCPU)、
145
+ AVX-512、2 TB RAM、無 GPU。`lscpu CPU(s)=192`,`os.cpu_count()=192`,OpenVINO 2026.4.0。
146
+
147
+ | 項目 | 數值 |
148
+ | --- | --- |
149
+ | load + compile(`compile=True`) | **11.82 s**(結束時 RSS 3423 MB) |
150
+ | 1024×1024 / 4 steps 單張總時間 | 63.59 – 76.69 s(平均 **66.96 s**) |
151
+ | 1024×1024 單步時間 | 平均 **16.28 s**、中位數 15.71 s、範圍 14.76 – 27.06 s(暖機除外) |
152
+ | 512×512 / 4 steps 單張總時間 | 19.84 – 20.49 s(平均 **20.22 s**) |
153
+ | 512×512 單步時間 | 平均 **4.93 s**、中位數 4.78 s |
154
+ | 峰值 RSS(連續生成) | **~36.9 GB** |
155
+ | FP16 pipeline 大小 | 32175.7 MB |
156
+ | INT4 pipeline 大小 | **8511.4 MB(-73.5%)** |
157
+ | 量化時間(weight-only, data-free) | 97.6 s |
158
+
159
+ 逐張明細:
160
+
161
+ | 影像 | seed | 解析度 | steps | CFG | 總時間 | 單步平均 | 峰值 RSS |
162
+ | --- | ---: | --- | ---: | ---: | ---: | ---: | ---: |
163
+ | 01_hanfu | 42 | 1024² | 4 | 0.0 | 76.69 s | 18.665 s | 34481 MB |
164
+ | 02_astronaut | 43 | 1024² | 4 | 0.0 | 65.74 s | 16.014 s | 36888 MB |
165
+ | 03_taipei | 44 | 1024² | 4 | 0.0 | 63.59 s | 15.471 s | 36892 MB |
166
+ | 04_shiba | 45 | 1024² | 4 | 0.0 | 64.95 s | 15.764 s | 36897 MB |
167
+ | 05_ink | 46 | 1024² | 4 | 0.0 | 63.83 s | 15.484 s | 36899 MB |
168
+ | 01_hanfu_512 | 42 | 512² | 4 | 0.0 | 20.39 s | 4.947 s | 36903 MB |
169
+ | 02_astronaut_512 | 43 | 512² | 4 | 0.0 | 20.09 s | 4.921 s | 36912 MB |
170
+ | 03_taipei_512 | 44 | 512² | 4 | 0.0 | 19.84 s | 4.831 s | 36912 MB |
171
+ | 04_shiba_512 | 45 | 512² | 4 | 0.0 | 20.31 s | 4.973 s | 36912 MB |
172
+ | 05_ink_512 | 46 | 512² | 4 | 0.0 | 20.49 s | 5.001 s | 36912 MB |
173
+
174
+ ## 建議參數
175
+
176
+ 沿用上游模型卡建議:steps **1–4**、CFG **0.0**(FLUX 為 rectified flow,不使用 CFG)、`max_sequence_length=256`。
177
+ 本 repo 範例採 4 steps。
178
+
179
+ ## Credits
180
+
181
+ - 模型:[black-forest-labs/FLUX.1-schnell](https://huggingface.co/black-forest-labs/FLUX.1-schnell) by Black Forest Labs(Apache-2.0)
182
+ - 轉換與範例:[HelloSun/FLUX.1-schnell-OpenVINO-INT4](https://huggingface.co/HelloSun/FLUX.1-schnell-OpenVINO-INT4)