Document Intel GPU / NPU / AUTO device support; add --target-device gpu to the export tool
Browse files- README.md +26 -0
- export_openvino_int4.py +11 -5
README.md
CHANGED
|
@@ -198,6 +198,32 @@ Real-ESRGAN 是純卷積的深層殘差網路(23 個 RRDB,每層輸出 `x*0.
|
|
| 198 |
**INT4 在 CPU 上不會更快**(CPU plugin 執行時會把 4-bit 權重解壓回 FP32/FP16),
|
| 199 |
它的優勢是模型只有 11 MB;若要在 4-bit 尺寸下兼顧畫質,請用 `int4-mixed`。
|
| 200 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
---
|
| 202 |
|
| 203 |
## 2. 安裝
|
|
|
|
| 198 |
**INT4 在 CPU 上不會更快**(CPU plugin 執行時會把 4-bit 權重解壓回 FP32/FP16),
|
| 199 |
它的優勢是模型只有 11 MB;若要在 4-bit 尺寸下兼顧畫質,請用 `int4-mixed`。
|
| 200 |
|
| 201 |
+
### Intel GPU(NPU / AUTO 同理)可以執行嗎?
|
| 202 |
+
|
| 203 |
+
可以。這些都是**標準 OpenVINO IR**(動態 shape),不綁定 plugin,`-d GPU` / `-d NPU` / `-d AUTO` / `-d HETERO` 都能跑:
|
| 204 |
+
|
| 205 |
+
```bash
|
| 206 |
+
python realesrgan_ov.py -i input.jpg -d GPU -m int8 -t 128 # Intel GPU 上推薦 INT8
|
| 207 |
+
python realesrgan_ov.py -i input.jpg -d AUTO -m fp16 # 自動挑最快裝置
|
| 208 |
+
```
|
| 209 |
+
|
| 210 |
+
| 模型 | Intel GPU 支援 | 說明 |
|
| 211 |
+
|---|---|---|
|
| 212 |
+
| `fp16` | ✅ | Xe / Arc 上 FP16 有硬體加速;更舊的 UHD/Iris 會以 f32 執行 |
|
| 213 |
+
| `int8` (W8A8) | ✅ **GPU 上最快** | GPU plugin 對 INT8 卷積有最佳路徑(Xe-LP+ / Arc / Alchemist) |
|
| 214 |
+
| `int4-mixed` / `int4` / `int4-w4a8` | ⚠️ 可執行、通常不快 | GPU plugin 支援 4-bit 權重,但**卷積**沒有 4-bit 加速路徑,會在 runtime 解壓成 FP16/FP32;收益只有 VRAM。純 4-bit 的模型還多了 dequantize 節點,GPU 上反而變慢 |
|
| 215 |
+
| `fp32` | ✅ | 相容但最慢,僅供對照 |
|
| 216 |
+
|
| 217 |
+
若要給 Intel GPU 專用的量化模型(INT8 的 activation 範圍與 SmoothQuant 會依 GPU 特性不同而調整):
|
| 218 |
+
|
| 219 |
+
```bash
|
| 220 |
+
python export_openvino_int4.py --weights RealESRGAN_x4plus.pth --target-device gpu \
|
| 221 |
+
--a8-bits 8 --a8-name int8_w8a8_gpu --no-w4a16
|
| 222 |
+
```
|
| 223 |
+
|
| 224 |
+
備註:本 repo 的模型是在**沒有 GPU 的機器**上驗證的(`available_devices = ['CPU']`),
|
| 225 |
+
以上 Intel GPU 的說明是依 OpenVINO plugin 的能力整理,不是本機實測數據。
|
| 226 |
+
|
| 227 |
---
|
| 228 |
|
| 229 |
## 2. 安裝
|
export_openvino_int4.py
CHANGED
|
@@ -150,7 +150,8 @@ def enable_int4_weights_in_cpu_hw_config():
|
|
| 150 |
hw_config.CPU_SETUP = tuple(patched)
|
| 151 |
|
| 152 |
|
| 153 |
-
def quantize_ptq_int4(model, dataset, out_dir, stem, smooth_alpha=0.5, num_bits=4, name='int4_w4a8'
|
|
|
|
| 154 |
"""Post-training quantization: {num_bits}-bit weights + INT8 activations."""
|
| 155 |
import nncf
|
| 156 |
from nncf.quantization.advanced_parameters import QuantizationParameters
|
|
@@ -158,15 +159,18 @@ def quantize_ptq_int4(model, dataset, out_dir, stem, smooth_alpha=0.5, num_bits=
|
|
| 158 |
if not isinstance(dataset, nncf.Dataset):
|
| 159 |
dataset = nncf.Dataset(dataset)
|
| 160 |
|
| 161 |
-
if
|
|
|
|
|
|
|
|
|
|
| 162 |
enable_int4_weights_in_cpu_hw_config()
|
| 163 |
advanced = nncf.AdvancedQuantizationParameters(
|
| 164 |
weights_quantization_params=QuantizationParameters(num_bits=num_bits, per_channel=True),
|
| 165 |
smooth_quant_alpha=smooth_alpha,
|
| 166 |
backend_params={'backend_algorithm_accuracy_checking': False},
|
| 167 |
)
|
| 168 |
-
print(f'[nncf] PTQ -> W{num_bits}A8')
|
| 169 |
-
quantized = nncf.quantize(model, dataset, target_device=
|
| 170 |
subset_size=100000, advanced_parameters=advanced)
|
| 171 |
path = os.path.join(out_dir, f'{stem}_{name}.xml')
|
| 172 |
ov.save_model(quantized, path, compress_to_fp16=True)
|
|
@@ -228,6 +232,8 @@ def main():
|
|
| 228 |
help='data-aware scale search (NNCF) before the 4-bit rounding')
|
| 229 |
parser.add_argument('--awq', action='store_true', help='activation-aware weight quantization scaling')
|
| 230 |
parser.add_argument('--a8-bits', type=int, default=4, help='weight bits for the PTQ (W?A8) pass')
|
|
|
|
|
|
|
| 231 |
parser.add_argument('--a8-name', default='int4_w4a8')
|
| 232 |
parser.add_argument('--lwc-dataset', action='store_true',
|
| 233 |
help='pass the calibration set to the weight-compression pass (AWQ / scale estimation)')
|
|
@@ -279,7 +285,7 @@ def main():
|
|
| 279 |
name='int4_mixed_w4a16' if args.weight_ratio != 1.0 else 'int4_w4a16')
|
| 280 |
if not args.no_w4a8:
|
| 281 |
quantize_ptq_int4(model, calib, args.out_dir, args.stem, smooth_alpha=args.smooth_alpha,
|
| 282 |
-
num_bits=args.a8_bits, name=args.a8_name)
|
| 283 |
|
| 284 |
|
| 285 |
|
|
|
|
| 150 |
hw_config.CPU_SETUP = tuple(patched)
|
| 151 |
|
| 152 |
|
| 153 |
+
def quantize_ptq_int4(model, dataset, out_dir, stem, smooth_alpha=0.5, num_bits=4, name='int4_w4a8',
|
| 154 |
+
target_device='cpu'):
|
| 155 |
"""Post-training quantization: {num_bits}-bit weights + INT8 activations."""
|
| 156 |
import nncf
|
| 157 |
from nncf.quantization.advanced_parameters import QuantizationParameters
|
|
|
|
| 159 |
if not isinstance(dataset, nncf.Dataset):
|
| 160 |
dataset = nncf.Dataset(dataset)
|
| 161 |
|
| 162 |
+
device = nncf.TargetDevice.GPU if str(target_device).lower().startswith('gpu') else nncf.TargetDevice.CPU
|
| 163 |
+
if num_bits < 8 and device == nncf.TargetDevice.CPU:
|
| 164 |
+
# the stock NNCF CPU hardware config only allows 8/16-bit weights; add the 4-bit space.
|
| 165 |
+
# For target_device=GPU NNCF already allows 4-bit weight compression out of the box.
|
| 166 |
enable_int4_weights_in_cpu_hw_config()
|
| 167 |
advanced = nncf.AdvancedQuantizationParameters(
|
| 168 |
weights_quantization_params=QuantizationParameters(num_bits=num_bits, per_channel=True),
|
| 169 |
smooth_quant_alpha=smooth_alpha,
|
| 170 |
backend_params={'backend_algorithm_accuracy_checking': False},
|
| 171 |
)
|
| 172 |
+
print(f'[nncf] PTQ -> W{num_bits}A8 (target_device={device.value})')
|
| 173 |
+
quantized = nncf.quantize(model, dataset, target_device=device,
|
| 174 |
subset_size=100000, advanced_parameters=advanced)
|
| 175 |
path = os.path.join(out_dir, f'{stem}_{name}.xml')
|
| 176 |
ov.save_model(quantized, path, compress_to_fp16=True)
|
|
|
|
| 232 |
help='data-aware scale search (NNCF) before the 4-bit rounding')
|
| 233 |
parser.add_argument('--awq', action='store_true', help='activation-aware weight quantization scaling')
|
| 234 |
parser.add_argument('--a8-bits', type=int, default=4, help='weight bits for the PTQ (W?A8) pass')
|
| 235 |
+
parser.add_argument('--target-device', default='cpu', choices=['cpu', 'gpu'],
|
| 236 |
+
help='NNCF target device; use "gpu" to produce an IR tuned for the Intel GPU plugin')
|
| 237 |
parser.add_argument('--a8-name', default='int4_w4a8')
|
| 238 |
parser.add_argument('--lwc-dataset', action='store_true',
|
| 239 |
help='pass the calibration set to the weight-compression pass (AWQ / scale estimation)')
|
|
|
|
| 285 |
name='int4_mixed_w4a16' if args.weight_ratio != 1.0 else 'int4_w4a16')
|
| 286 |
if not args.no_w4a8:
|
| 287 |
quantize_ptq_int4(model, calib, args.out_dir, args.stem, smooth_alpha=args.smooth_alpha,
|
| 288 |
+
num_bits=args.a8_bits, name=args.a8_name, target_device=args.target_device)
|
| 289 |
|
| 290 |
|
| 291 |
|