Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.From a recipe to a kitchen
A trained model is like a recipe written by a chef in their own notation. To cook it in a different kitchen, someone rewrites it in a standard format, then the kitchen’s staff reorganize it for their equipment: combining steps that can be done together, skipping steps whose results nobody uses, measuring with coarser cups where precision does not matter, and planning which bowls to reuse. Inference runtimes do exactly this for neural networks, and good ones make the model faster and smaller without changing its answers.
After exported: 14 operators, 16 initializers, 23,136 bytes of parameters; activations for a batch of 32: 70,784 bytes with one buffer per tensor, 26,624 with buffer reuse. Outputs are unchanged (largest difference below 10⁻¹²).
Dead-code elimination removed a debugging branch that kept a tensor alive; batch normalization and input normalization fold into the next layer's weights; fusion turns MatMul, Add and Relu into one kernel with no intermediate tensors. Runtimes such as TensorRT, TensorFlow Lite and ONNX Runtime apply these passes when they build an engine.
Folding a batch-normalization layer into the weights before it removes the layer entirely: the network computes the same function with fewer operations.
- 1. An exported model is stored as…
- 2. Operator fusion…
- 3. A good graph optimization changes…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- ONNX: an open graph format of typed operators with versioned semantics, produced by exporters from PyTorch, TensorFlow and others.
- ONNX Runtime: applies graph optimizations and runs subgraphs on execution providers (CPU, CUDA, TensorRT, NNAPI, Core ML and others).
- TensorRT: builds an optimized engine for a specific NVIDIA GPU: layer and tensor fusion, kernel auto-tuning, fp16 and int8 with calibration.
- TensorFlow Lite (LiteRT) and TFLite Micro: a flatbuffer model with integer-only kernels and delegates for accelerators; the micro variant runs from a static memory arena without dynamic allocation.
- Graph passes: dead-code elimination, constant folding, batch-norm folding, operator fusion.
- Integer-only quantization: uint8 or int8 activations with zero points, int8 weights, int32 accumulation, and fixed-point requantization.
- Calibration: choosing activation ranges from representative data (max, percentile, entropy).
- Memory planning: assigning intermediate tensors to reusable buffers from their lifetimes.
- 1. TensorRT's int8 entropy calibration chooses ranges by minimizing…
- 2. ONNX Runtime runs subgraphs on…
- 3. TFLite Micro allocates memory…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.A runtime in miniature
Exercise 1.
- An 8-64-64-8 network with input standardization and batch normalization, trained on raw sensor values, exported as a 14-node graph with a debugging branch.
- A reference interpreter, dead-code elimination, batch-norm folding, input-normalization folding and MatMul–Add–Relu fusion, each checked for unchanged outputs.
- A memory planner that reuses buffers once a tensor’s last reader has run.
Practice.
- Export with a fixed opset and test the exported model against the framework’s outputs before optimizing.
- Check every runtime’s output against a reference with tolerances measured for its precision (fp32, fp16, int8).
- Calibrate with clean, representative data and enough of it; inspect the chosen ranges.
- Prefer operators the target supports, or the graph will be split between devices with transfers in between.
- Profile on the device at the deployed batch size, including memory: embedded runtimes need a static arena that fits.
def fold_batchnorm(W, b, gamma, beta, mean, var, eps=1e-5):
s = gamma / sqrt(var + eps)
return W * s, (b - mean) * s + beta
def requantize(acc, M0, shift, zp_out): # integer-only: acc int32, M0 a 31-bit integer
n = 31 + shift
return clip(((acc * M0 + (1 << (n - 1))) >> n) + zp_out, 0, 255)- 1. Before optimizing an exported model, you should…
- 2. Unsupported operators on an accelerator cause…
- 3. Calibration data should be…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Batch-norm folding. At inference batch normalization is the affine map z ↦ γ(z − μ)/√(σ² + ε) + β with fixed statistics. Applied to z = xW + b it gives x(W·s) + ((b − μ)·s + β) with s = γ/√(σ² + ε) per output, so the folded layer computes the same function exactly, up to rounding. Input standardization (x − m)/d before a layer folds the same way: W’ = diag(1/d)W and b’ = b − (m/d)W.
Integer-only arithmetic. With real values r = s(q − z) for scale s and zero point z, a layer y = xW + b becomes s_y(q_y − z_y) = s_x s_w Σ(q_x − z_x)q_w + b. Storing the bias as an int32 with scale s_x s_w, the accumulator is an integer, and q_y = z_y + M·acc with M = s_x s_w / s_y. Writing M = M₀·2^(−31−k) with M₀ a 31-bit integer lets the whole layer run with integer multiplies and a rounding shift, as TensorFlow Lite’s kernels do. A ReLU folds into the clamp at the zero point.
Calibration. The clipping range trades rounding error (step = range/255) against clipping error for values beyond it. The maximum never clips calibration data but is sensitive to outliers; a high percentile ignores a few extremes; entropy calibration picks the threshold whose quantized histogram is closest in KL divergence to the original, which suits long-tailed activations and can clip bounded ones too aggressively.
Memory planning. Each intermediate tensor is live from the node that writes it to its last reader. Buffers can be shared by tensors whose lifetimes do not overlap; the minimum arena is at least the largest total size of tensors live at any one node, and greedy assignment by lifetime usually reaches it for chain-like graphs.
- 1. Folded batch norm scales each output's weights by…
- 2. The requantization multiplier M equals…
- 3. The smallest possible activation arena is at least…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Does an optimized, integer-only int8 version of an exported network give the same answers as the original, and how does the calibration method matter?
History.
- Google released TensorFlow Lite in 2017; B. Jacob and colleagues (2018) published its integer-only quantization scheme; TensorFlow Lite Micro followed in 2019 (R. David and colleagues, 2021).
- NVIDIA released TensorRT in 2016; S. Migacz (2017) described its int8 entropy calibration.
- Microsoft and Facebook introduced ONNX in 2017; ONNX Runtime was open-sourced in 2018.
- T. Chen and colleagues (2018) built TVM, a compiler that tunes kernels for each target; MLIR (C. Lattner and colleagues, 2021) underlies several newer toolchains.
Exercise 2.
- 4,000 training and 2,000 test points; the graph is optimized with all four passes.
- int8 conversion: uint8 activations, per-channel int8 weights, int32 biases, 31-bit fixed-point requantization; activation ranges from 500 training inputs by maximum, 99.99th percentile or entropy calibration.
Seed 0 (fp32: accuracy 0.9235, 21,024 bytes of parameters):
| calibration | accuracy | agreement with fp32 | largest logit error | bytes |
|---|---|---|---|---|
| maximum | 0.9230 | 0.9970 | 1.312 | 6,752 |
| 99.99th percentile | 0.9245 | 0.9980 | 1.726 | 6,752 |
| entropy (KL) | 0.9205 | 0.9875 | 10.607 | 6,752 |
Seeds 1 and 2 (fp32 0.9325 and 0.9355): maximum 0.9340 and 0.9345 (agreement 0.9960 and 0.9965); percentile 0.9350 and 0.9345; entropy 0.9285 and 0.9215 (agreement 0.9835 and 0.9825).
The graph passes reduced 14 nodes to 4 and 16 initializers to 6, with outputs unchanged to within 10⁻¹²; activation memory for a batch of 32 fell from 70,784 bytes (one buffer per tensor) to 16,384.
Findings.
- With maximum calibration the integer-only model was within 0.0015 of fp32 accuracy in every seed and agreed with fp32 on 99.6% to 99.7% of inputs, at less than a third of the size.
- Entropy calibration clipped the hidden activations at about half their maximum and cost 0.3 to 1.4 points: these activations are bounded, not long-tailed.
- Dead-code elimination also cut planned memory (from 26,624 to 18,432 bytes), because the unused debugging branch kept an intermediate tensor alive.
Inference runtimes in practice
- Robots and drones with NVIDIA Jetson modules deploy TensorRT engines, often through ONNX; engines are built on the target device for its GPU.
- Phones and microcontrollers run TensorFlow Lite (LiteRT), Core ML or vendor NPU runtimes; TFLite Micro runs within tens to hundreds of kilobytes.
- Spacecraft payloads use vendor toolchains for FPGAs and accelerators (Vitis AI, OpenVINO for Myriad-class VPUs), with the same steps: export, optimize, quantize, calibrate, verify (topic M4-5).
References
- B. Jacob and colleagues (2018). “Quantization and training of neural networks for efficient integer-arithmetic-only inference.” CVPR.
- S. Migacz (2017). “8-bit inference with TensorRT.” NVIDIA GPU Technology Conference.
- R. David and colleagues (2021). “TensorFlow Lite Micro: embedded machine learning for TinyML systems.” MLSys.
- T. Chen and colleagues (2018). “TVM: an automated end-to-end optimizing compiler for deep learning.” OSDI.
- ONNX (2017–). “Open Neural Network Exchange: operator specifications.” onnx.ai.
- H. Wu, P. Judd, X. Zhang, M. Isaev and P. Micikevicius (2020). “Integer quantization for deep learning inference: principles and empirical evaluation.” arXiv:2004.09602.