"""INT8 weight-only compression (NNCF) of the converted OpenVINO IRs; activations stay f16. Usage: python int8_compress.py SRC_DIR DST_DIR [--files dit_a.xml,dit_b.xml,...] [--mode INT8_ASYM|INT8_SYM] [--keep-down] --keep-down: leave every ffn_down projection (MatMul + weight) in fp16 (the shipped sft-int8 DiT: models/dit_sft_int8_keepdown, 6.1 GB instead of 5.0 GB, the velocity error of dit_c drops from 4.8 to 3.6 percent on the hard test case). Copies every non-IR file of SRC_DIR (host_weights.npz, dataspace.npz) next to the result. CPU only (RAM: about the size of the source IR x 3). """ import argparse import glob import os import shutil import sys import time sys.path.insert(0, __import__("os").path.dirname(__import__("os").path.abspath(__file__))) from common import hwm_gb # noqa: E402 def main(): ap = argparse.ArgumentParser() ap.add_argument("src") ap.add_argument("dst") ap.add_argument("--files", default="") ap.add_argument("--mode", default="INT8_ASYM", choices=["INT8_ASYM", "INT8_SYM"]) ap.add_argument("--keep-down", action="store_true", help="keep the ffn_down projections in fp16") a = ap.parse_args() import nncf import openvino as ov os.makedirs(a.dst, exist_ok=True) files = a.files.split(",") if a.files else sorted(os.path.basename(x) for x in glob.glob(f"{a.src}/*.xml")) for f in os.listdir(a.src): if not f.endswith((".xml", ".bin")) and os.path.isfile(f"{a.src}/{f}") and not os.path.exists(f"{a.dst}/{f}"): if f.endswith(".npz"): shutil.copy(f"{a.src}/{f}", f"{a.dst}/{f}") core = ov.Core() for f in files: t = time.time() m = core.read_model(f"{a.src}/{f}") t_read = time.time() - t t = time.time() extra = {"ignored_scope": nncf.IgnoredScope(patterns=[".*ffn_down.*"])} if a.keep_down else {} mc = nncf.compress_weights(m, mode=getattr(nncf.CompressWeightsMode, a.mode), **extra) t_comp = time.time() - t out = f"{a.dst}/{f}" ov.save_model(mc, out, compress_to_fp16=True) sz = os.path.getsize(out.replace(".xml", ".bin")) / 1e9 src_sz = os.path.getsize(f"{a.src}/{f}".replace(".xml", ".bin")) / 1e9 print(f"[int8] {f}: {src_sz:.2f} GB -> {sz:.2f} GB (read {t_read:.1f}s compress {t_comp:.1f}s) hwm {hwm_gb():.1f} GB", flush=True) del m, mc if __name__ == "__main__": main()