onnx_quantize() compresses 32-bit floating point (FP32) ONNX neural network models
to 8-bit integer (INT8) precision. Dynamic quantization reduces model file sizes
by up to 75% (e.g. from 24 MB to 6 MB) and accelerates CPU inference throughput
by 2–3x with negligible accuracy trade-off.
The speed-up is hardware dependent: dynamic INT8 kernels target CPUs (best on CPUs
with VNNI/AVX-512 or ARM dot-product instructions). With engine = "gpu"
(DirectML), quantized operators may fall back to slower kernels or the CPU, so INT8
models can be slower than FP32 on the GPU. Measure with yolo_benchmark().
Usage
onnx_quantize(
model_path,
output_path = NULL,
weight_type = c("QUInt8", "QInt8"),
verbose = TRUE
)Arguments
- model_path
Character string. File path to the input FP32
.onnxmodel.- output_path
Optional file path for the quantized INT8 model. If
NULL(default), saves the model inpliman_model_dir()appending"_int8.onnx"to the original model name.- weight_type
Precision type:
"QUInt8"(unsigned 8-bit, default) or"QInt8"(signed 8-bit).- verbose
Logical. Display compression metrics and file sizes. Defaults to
TRUE.
Examples
if (FALSE) { # interactive()
library(pliman)
# Quantize classification model
q_model <- onnx_quantize("dataset_flores_yolo_cls.onnx")
# Run inference with 3x speedup on CPU
res <- image_classify_dl("rose.jpg", model = q_model)
}
