CSC3119 Β· Module 7 of 9

Inference Performance: ONNX, Pruning & INT8 Quantization

Accelerate prediction throughput, shrink model sizes, and reduce server operational costs

~56 min to study properly

Training throughput focuses on processing millions of samples in parallel batches across days. Inference latency, however, is about serving a single patient prediction in under 15 milliseconds.

Checkpoint Β· the examiner asks

Explain the mathematical principle of post-training quantization from FP32 to INT8. Why does it shrink memory by 4x, and what hardware acceleration does INT8 leverage?

In one minute

Inference is about single-request latency. ONNX Runtime compiles the model graph to optimised C++ kernels, removing Python overhead. INT8 quantisation stores 1 byte per weight instead of 4 (75% smaller) and SIMD/VNNI instructions process four times more INT8 values per cycle. Combined with pruning, you get big speedups for a tiny accuracy cost β€” always re-measure AUC after.

Traps examiners look for
  • Saying INT8 saves 25% β€” it saves 75% (4Γ— smaller).
  • Quantising without re-validating clinical metrics.
  • Optimising training throughput when the problem is inference latency.
Read

Study notes

01Latency is a different goal from training throughput

Training is about throughput: process millions of samples per hour in big batches, and it's fine if a single step takes a second. Serving a patient is about latency: one prediction, now β€” typically a budget of ~10–15 ms for the model inside a ~100 ms end-to-end request.

Where inference time goes:

  • Python overhead β€” interpreting each operation, creating objects.
  • Memory traffic β€” moving weights from RAM to the CPU caches; often the real bottleneck.
  • Arithmetic β€” the multiply-adds themselves.

Optimisation attacks each one: compiled runtimes remove Python overhead, smaller number formats cut memory traffic, and SIMD instructions do more arithmetic per cycle.

02ONNX and ONNX Runtime

ONNX (Open Neural Network Exchange) is a standard file format for model computation graphs. You can export from scikit-learn (skl2onnx), XGBoost, PyTorch or TensorFlow into one .onnx file.

ONNX Runtime executes that graph with optimised C++ kernels: it fuses operations, plans memory up front and uses hardware-specific instructions β€” and no Python interpreter runs inside the hot loop. Typical result: the same model, same outputs, noticeably lower latency. A bonus: the .onnx file can be served from C#, Java or JavaScript, not just Python.

sess = onnxruntime.InferenceSession("stroke.onnx")
probs = sess.run(None, {"input": X.astype(np.float32)})[1]

03Quantisation β€” FP32 to INT8

Weights are normally 32-bit floats (FP32, 4 bytes). Quantisation stores them in fewer bits:

FormatBytes per weightSize of a 100 M-param model
FP324400 MB
FP16 / BF162200 MB
INT81100 MB

INT8 is 4Γ— smaller (a 75% saving). Speed improves too: less data to move from memory, more of the model fits in cache, and CPU vector units (AVX-512 VNNI) process four times as many 8-bit values per instruction as 32-bit ones.

Affine quantisation maps a float range [min, max] onto 0–255: scale s = (max βˆ’ min)/255, zero-point z = round(βˆ’min/s), q = round(x/s) + z, and dequantised xΜ‚ = (q βˆ’ z)Β·s. The rounding error is at most s/2 β€” which is why outliers that stretch the range hurt accuracy.

04Pruning β€” and always re-validating

Pruning removes weights (or whole neurons, trees, branches) that contribute little β€” e.g. weights near zero. Sparse models can be smaller and faster, especially with structured pruning that removes entire channels or layers the runtime can skip.

Every optimisation is lossy or at least risky, so the rule is: re-run the full evaluation on the optimised artifact β€” ROC-AUC, recall at the clinical threshold, calibration β€” and compare with the original. In the course example INT8 + pruning cost less than 0.5% ROC-AUC, an excellent trade for a 4Γ— smaller, several-times-faster model. If recall at the triage threshold dropped noticeably, you'd ship the FP32 model instead.

Watch it done

Worked examples

Worked example 1

Memory saved by INT8

A 64 M-parameter model is stored in FP32. How many MB does INT8 quantisation save?

    Worked example 2

    Quantise one weight by hand

    A tensor's values span [βˆ’1.0, 1.0]. Quantise x = 0.42 to INT8 with affine quantisation and compute the error.

      Vocabulary

      Key terms

      Inference is Memory-Bound, Not Compute-Bound

      For small-batch or single-instance model inference, modern CPUs spend over 70% of time waiting for weights to be fetched from DRAM into CPU cache. Shrinking weights with INT8 quantization directly cuts DRAM bus traffic by 4Γ—, creating massive real-world speedups even on budget cloud VMs.

      Practise

      Quick check

      0 of 8 correct so far. Wrong answers come with an explanation β€” and the buddy can unpack them.

      Q1A deep learning ensemble model has a serialized file size of 240 MB in standard FP32. What will its approximate size be after INT8 quantization?

      Q2What is the primary benefit of exporting a scikit-learn or PyTorch model to ONNX format?

      Q3How many bytes does an INT8 weight use?

      Q4FP32 β†’ INT8 reduces weight memory by…

      Q5What does ONNX Runtime mainly remove from the inference hot path?

      Q6Why does INT8 run faster on modern CPUs?

      Q7After quantising the stroke model, what must you do before deploying?

      Q8Why do outlier weights hurt INT8 accuracy?

      Exam-style

      Written practice

      Short answer5 marks

      Explain how INT8 quantisation reduces memory and speeds up CPU inference.

      Short answer5 marks

      What are ONNX and ONNX Runtime, and why do they reduce latency?

      Long answer12 marks

      The stroke API must answer in under 15 ms on a CPU-only server, but the current FP32 Python model takes 40 ms. Describe a step-by-step optimisation plan, how you would measure each step, and the acceptance criteria before deployment.

      Watch

      Videos

      Fresh picks are chosen by AI every day.
      Hands-on

      ONNX & INT8 Quantization Speedup Simulator

      4 guided tasks: predict, experiment, explain β€” then challenge yourself. Plus 1 coding exercise you run right in the browser.

      Start the lab