Sub topic 16
FP16 and INT8: Speed Gains and Accuracy Checks
Assume you know basic Python, NumPy, PyTorch, and image-model inference. You should also understand ONNX Runtime sessions and have access to an NVIDIA GPU for the TensorRT examples.
Reduced precision stores numbers with fewer bits than FP32. FP16 uses 16-bit floating-point values. INT8 uses 8-bit integer values. Smaller values reduce memory traffic and can use faster GPU instructions, but they can also change model outputs.
FP16: the low-risk speed option
FP16 usually requires little application code. TensorRT converts supported operations and runs them with half-precision arithmetic. Unsupported operations can remain in FP32.
import onnxruntime as ort
providers = [
(
"TensorrtExecutionProvider",
{
"trt_fp16_enable": True,
"trt_engine_cache_enable": True,
"trt_engine_cache_path": "./trt_cache",
},
),
"CUDAExecutionProvider",
"CPUExecutionProvider",
]
session = ort.InferenceSession(
"resnet50.onnx",
providers=providers,
)
print(session.get_providers())FP16 often gives a useful latency improvement and lowers GPU memory use. The actual gain depends on the GPU, model operators, tensor shapes, and data-transfer costs. Measure the complete request path, not only the neural-network kernel.
FP16 is a good first choice when:
- The model already meets its accuracy target in FP32.
- You need a quick deployment improvement.
- Your GPU has strong FP16 support.
- You want to avoid collecting calibration data.
INT8: more compression, more validation
INT8 represents values with integers. A quantized model needs a scale and zero point to map floating-point values into the integer range. Static quantization determines these values from representative calibration data.
Calibration data should resemble production images. Include different lighting, object sizes, backgrounds, and difficult examples. A poor calibration set can damage accuracy even when the validation set looks reasonable.
INT8 can outperform FP16, especially on hardware with dedicated integer tensor cores. It can also require more tuning. Some layers may remain in FP16 or FP32 because quantizing them causes too much error.
| Precision | Typical benefit | Main risk |
|---|---|---|
| FP32 | Reference accuracy | Higher latency and memory use |
| FP16 | Lower cost with small accuracy change | Unsupported or numerically sensitive operations |
| INT8 | Highest compression and possible speed | Calibration and accuracy loss |
Check numerical parity
Compare optimized outputs with the FP32 reference on a fixed test set. Use both numerical error and task-level metrics such as top-1 accuracy, mean average precision, or recall.
import numpy as np
import onnxruntime as ort
fp32 = ort.InferenceSession("resnet50.onnx", providers=["CPUExecutionProvider"])
fast = ort.InferenceSession(
"resnet50_fp16.onnx",
providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
)
x = np.random.rand(4, 3, 224, 224).astype(np.float32)
reference = fp32.run(["logits"], {"input": x})[0]
candidate = fast.run(["logits"], {"input": x})[0]
max_error = np.max(np.abs(reference - candidate))
same_top1 = np.array_equal(reference.argmax(1), candidate.argmax(1))
print("maximum absolute error:", max_error)
print("same top-1 predictions:", same_top1)Do not require exact equality. Different kernels and precisions produce small differences. Set tolerances from measured model behavior, then evaluate the candidate on a real labeled validation set.
How to choose
Exercise
Why should INT8 calibration images resemble production images?
Show answer
Calibration estimates the value ranges used for quantization. Unrepresentative images can produce poor scales and cause larger errors on production inputs.