Real-ESRGAN-OpenVINO / export_openvino_int4.py
HelloSun's picture
Document Intel GPU / NPU / AUTO device support; add --target-device gpu to the export tool
e8e4ce6 verified
Raw History Blame Contribute Delete
12.6 kB
#!/usr/bin/env python3
"""Export Real-ESRGAN (RealESRGAN_x4plus) to OpenVINO IR and quantize it to INT4.
Pipeline
--------
1. Rebuild the RRDBNet generator from the original PyTorch checkpoint.
2. Export to ONNX (opset 17, dynamic H/W so that tiled inference works).
3. Convert the ONNX graph to an OpenVINO FP32 / FP16 model.
4. (optional) Post-training-quantize the OpenVINO model with NNCF to INT4.
Two INT4 flavours are produced:
* ``*_int4_w4a16.xml`` - INT4 weights (SmoothQuant) + FP16 activations.
Best quality / best PSNR, smallest model, slightly slower than INT8 compute.
* ``*_int4_w4a8.xml`` - INT4 weights + INT8 activations (W4A8).
Fastest on CPU (AMX/VNNI), still close in quality.
Usage
-----
python export_openvino_int4.py \
--weights RealESRGAN_x4plus.pth \
--data-dir inputs \
--out-dir models
"""
import argparse
import glob
import os
import pathlib
import sys
import cv2
import numpy as np
import torch
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
from rrdbnet_arch import RRDBNet # noqa: E402
import openvino as ov # noqa: E402
def build_model(weights_path):
"""Recreate RealESRGAN_x4plus and load the checkpoint."""
net = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
ckpt = torch.load(weights_path, map_location='cpu', weights_only=True)
if isinstance(ckpt, dict) and 'params' in ckpt:
state = ckpt['params']
elif isinstance(ckpt, dict) and 'params_ema' in ckpt:
state = ckpt['params_ema']
else:
state = ckpt
net.load_state_dict(state, strict=True)
net.eval()
return net
def export_onnx(net, onnx_path):
dummy = torch.randn(1, 3, 64, 64)
dynamic_axes = {
'input': {2: 'height', 3: 'width'},
'output': {2: 'out_height', 3: 'out_width'},
}
torch.onnx.export(
net,
dummy,
onnx_path,
opset_version=17,
input_names=['input'],
output_names=['output'],
dynamic_axes=dynamic_axes,
do_constant_folding=True,
)
print(f'[export] wrote {onnx_path}')
def convert_onnx(onnx_path):
"""ONNX -> OpenVINO IR in true FP32.
`openvino.convert_model()` compresses every weight to FP16 by default
(the legacy `--compress_to_fp16=True` default of the OpenVINO Model
Converter). For a 23-block RRDBNet that rounding error accumulates to
~25 dB PSNR, so the conversion is done with compression disabled here.
"""
from openvino.tools.ovc.cli_parser import get_all_cli_parser
from openvino.tools.ovc.convert_impl import _convert
cli_parser = get_all_cli_parser()
cli_parser.set_defaults(compress_to_fp16=False)
params = dict(input_model=str(onnx_path), input=None, output=None, example_input=None,
extension=None, verbose=False, share_weights=True, dynamo=False)
model, _ = _convert(cli_parser, params, True)
return model
def calibration_dataset(data_dir, tile, samples, num_crops=1, seed=0):
"""Collect real LR crops from the given folder for the calibration pass."""
paths = sorted(
p for ext in ('png', 'jpg', 'jpeg', 'bmp', 'webp')
for p in glob.glob(os.path.join(data_dir, f'*.{ext}'))
)
if not paths:
raise RuntimeError(f'no images found in {data_dir}')
rng = np.random.default_rng(seed)
crops = []
for path in paths[:samples]:
img = cv2.imread(path, cv2.IMREAD_COLOR)
if img is None:
continue
img = cv2.resize(img, (tile, tile), interpolation=cv2.INTER_AREA) if img.shape[0] != tile or img.shape[1] != tile else img
for _ in range(num_crops):
crops.append(img)
# a couple of synthetic (noise/gradient) tiles keep the statistics stable
for _ in range(max(2, len(crops) // 4)):
base = rng.integers(0, 256, size=(tile, tile, 3), dtype=np.uint8)
crops.append(cv2.GaussianBlur(base, (5, 5), 0))
print(f'[calib] {len(crops)} crops of {tile}x{tile} from {len(paths)} images')
return crops
class CalibDataset:
def __init__(self, images):
self.images = images
def __len__(self):
return len(self.images)
def __getitem__(self, index):
img = self.images[index]
rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
return {'input': np.transpose(rgb, (2, 0, 1))[np.newaxis, ...]}
def enable_int4_weights_in_cpu_hw_config():
"""The stock NNCF CPU hardware configuration only allows 8/16-bit weights.
Real-ESRGAN needs 4-bit convolutions, so the `q4_w` (symmetric 4-bit,
per-channel) space is added to the Convolution descriptor. The produced
model contains `u4` constants, which the OpenVINO CPU plugin supports.
"""
import nncf.common.hardware.config as hw_config
from nncf.common.hardware.defines import HWOpName, OpDesc
from nncf.common.hardware.qspaces import q4_w
patched = []
for desc in hw_config.CPU_SETUP:
if desc.type == HWOpName.CONVOLUTION:
desc = OpDesc(type=desc.type, activations=desc.activations,
weights=tuple(desc.weights) + (q4_w,))
patched.append(desc)
hw_config.CPU_SETUP = tuple(patched)
def quantize_ptq_int4(model, dataset, out_dir, stem, smooth_alpha=0.5, num_bits=4, name='int4_w4a8',
target_device='cpu'):
"""Post-training quantization: {num_bits}-bit weights + INT8 activations."""
import nncf
from nncf.quantization.advanced_parameters import QuantizationParameters
if not isinstance(dataset, nncf.Dataset):
dataset = nncf.Dataset(dataset)
device = nncf.TargetDevice.GPU if str(target_device).lower().startswith('gpu') else nncf.TargetDevice.CPU
if num_bits < 8 and device == nncf.TargetDevice.CPU:
# the stock NNCF CPU hardware config only allows 8/16-bit weights; add the 4-bit space.
# For target_device=GPU NNCF already allows 4-bit weight compression out of the box.
enable_int4_weights_in_cpu_hw_config()
advanced = nncf.AdvancedQuantizationParameters(
weights_quantization_params=QuantizationParameters(num_bits=num_bits, per_channel=True),
smooth_quant_alpha=smooth_alpha,
backend_params={'backend_algorithm_accuracy_checking': False},
)
print(f'[nncf] PTQ -> W{num_bits}A8 (target_device={device.value})')
quantized = nncf.quantize(model, dataset, target_device=device,
subset_size=100000, advanced_parameters=advanced)
path = os.path.join(out_dir, f'{stem}_{name}.xml')
ov.save_model(quantized, path, compress_to_fp16=True)
print(f'[nncf] wrote {path}')
return path
def enable_int4_for_convolutions():
"""Allow NNCF's weight compression to quantize Convolution layers to INT4.
Out of the box `nncf.compress_weights(..., mode=int4_*)` only applies the
4-bit precision to MatMul layers - every Convolution falls back to the INT8
backup precision (see `WeightCompression._get_ratio_defining_params`).
Real-ESRGAN is a pure convolutional network, so without this patch the model
would silently end up being INT8. The OpenVINO CPU plugin can execute
`u4`/`i4` compressed weight constants, therefore extending the 4-bit
precision to convolutions is safe.
"""
from nncf.quantization.algorithms.weight_compression.algorithm import WeightCompression
original = WeightCompression._get_ratio_defining_params
def _get_ratio_defining_params(self, all_weight_params, is_last_layer_skipped):
if self._mode in (nncf.CompressWeightsMode.INT8_SYM, nncf.CompressWeightsMode.INT8_ASYM):
return original(self, all_weight_params, is_last_layer_skipped)
return list(all_weight_params)
WeightCompression._get_ratio_defining_params = _get_ratio_defining_params
import nncf # noqa: F401 (already imported by the caller, kept for clarity)
def quantize_weights_int4(model, out_dir, stem, dataset=None, mode='int4_asym', ratio=1.0,
name='int4_w4a16', scale_estimation=False, awq=False, group_size=-1):
"""Weight-only INT4 compression (LWC): 4-bit weights, FP16 activations."""
import nncf
enable_int4_for_convolutions()
print(f'[nncf] weight compression -> INT4 ({mode}, ratio={ratio}, scale_estimation={scale_estimation}, awq={awq})')
compressed = nncf.compress_weights(model, mode=nncf.CompressWeightsMode(mode), ratio=ratio,
group_size=group_size, all_layers=True, dataset=dataset,
scale_estimation=scale_estimation, awq=awq)
path = os.path.join(out_dir, f'{stem}_{name}.xml')
ov.save_model(compressed, path, compress_to_fp16=True)
print(f'[nncf] wrote {path}')
return path
def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('--weights', default='RealESRGAN_x4plus.pth')
parser.add_argument('--data-dir', default='inputs', help='folder with images used for calibration')
parser.add_argument('--out-dir', default='models')
parser.add_argument('--calib-tile', type=int, default=128)
parser.add_argument('--calib-samples', type=int, default=6)
parser.add_argument('--stem', default='realesrgan_x4plus')
parser.add_argument('--smooth-alpha', type=float, default=0.5)
parser.add_argument('--group-size', type=int, default=-1)
parser.add_argument('--scale-estimation', action='store_true',
help='data-aware scale search (NNCF) before the 4-bit rounding')
parser.add_argument('--awq', action='store_true', help='activation-aware weight quantization scaling')
parser.add_argument('--a8-bits', type=int, default=4, help='weight bits for the PTQ (W?A8) pass')
parser.add_argument('--target-device', default='cpu', choices=['cpu', 'gpu'],
help='NNCF target device; use "gpu" to produce an IR tuned for the Intel GPU plugin')
parser.add_argument('--a8-name', default='int4_w4a8')
parser.add_argument('--lwc-dataset', action='store_true',
help='pass the calibration set to the weight-compression pass (AWQ / scale estimation)')
parser.add_argument('--weight-ratio', type=float, default=1.0,
help='fraction of weights quantized to INT4 by the weight-only pass')
parser.add_argument('--skip-onnx', action='store_true')
parser.add_argument('--skip-quant', action='store_true')
parser.add_argument('--skip-fp16', action='store_true')
parser.add_argument('--skip-fp32', action='store_true')
parser.add_argument('--no-w4a16', action='store_true')
parser.add_argument('--no-w4a8', action='store_true')
args = parser.parse_args()
os.makedirs(args.out_dir, exist_ok=True)
onnx_path = os.path.join(args.out_dir, f'{args.stem}.onnx')
if not args.skip_onnx and not os.path.exists(onnx_path):
net = build_model(args.weights)
print('[export] loaded checkpoint with strict=True (architecture matches RealESRGAN_x4plus)')
export_onnx(net, onnx_path)
model = convert_onnx(onnx_path)
print('[openvino] converted ONNX -> OV (FP32)')
if not args.skip_fp32:
fp32_path = os.path.join(args.out_dir, f'{args.stem}_fp32.xml')
ov.save_model(model.clone(), fp32_path, compress_to_fp16=False)
print(f'[openvino] wrote {fp32_path}')
if not args.skip_fp16:
fp16_path = os.path.join(args.out_dir, f'{args.stem}_fp16.xml')
ov.save_model(model.clone(), fp16_path, compress_to_fp16=True)
print(f'[openvino] wrote {fp16_path}')
if args.skip_quant:
return
crops = calibration_dataset(args.data_dir, args.calib_tile, args.calib_samples, num_crops=2)
dataset = CalibDataset(crops)
import nncf
calib = nncf.Dataset(dataset)
if not args.no_w4a16:
quantize_weights_int4(model, args.out_dir, args.stem,
dataset=calib if args.lwc_dataset else None, ratio=args.weight_ratio,
scale_estimation=args.scale_estimation, awq=args.awq,
group_size=args.group_size,
name='int4_mixed_w4a16' if args.weight_ratio != 1.0 else 'int4_w4a16')
if not args.no_w4a8:
quantize_ptq_int4(model, calib, args.out_dir, args.stem, smooth_alpha=args.smooth_alpha,
num_bits=args.a8_bits, name=args.a8_name, target_device=args.target_device)
if __name__ == '__main__':
main()