On-Device AI Deployment: INT8 and BF16 Quantization Explained
This article covers how to deploy AI models on local devices. It discusses the trade-offs of on-device inference, how different model types affect optimization, and how to choose between INT8 and BF16 precision. It also walks through the deployment pipeline, accuracy validation, performance tuning, and common engineering problems.
1. The Value and Limits of On-Device Inference
Running model inference on a local device offers three main benefits. First, response latency is lower because requests do not need a network round trip. Second, raw data never has to leave the device, which helps with privacy protection and compliance requirements. Third, applications keep working when the network is unstable or offline. These benefits come at a cost: compute, memory, and power on the device are limited, so model size and runtime efficiency must be balanced together.
On-device hardware typically includes a central processing unit (CPU), a graphics processing unit (GPU), and a neural processing unit (NPU). The CPU is the most general-purpose and suits control logic and irregular computation. The GPU has high parallelism and suits large matrix operations. The NPU is designed specifically for matrix multiplication and convolution. It usually delivers higher throughput per unit of power, which makes it well suited to long-running inference tasks. A common engineering practice is to assign operators according to task characteristics, so that each type of computation runs on the most suitable hardware.
2. Model Types and Workload Characteristics
Different models have very different computation patterns, and these patterns directly affect the optimization strategy.
- Large language models: During generation, the model decodes one token at a time, and each step reads a large amount of weights. Memory bandwidth is often the bottleneck in this case, so the storage and bandwidth gains from weight quantization are most noticeable.
- Natural language processing (NLP) models: Examples include text classification, sequence labeling, and vector encoding. These models require less computation, but their input lengths vary widely. Dynamic input shapes increase the complexity of compilation and scheduling, so the input range must be defined in advance.
- Convolutional neural networks: Models for image classification and object detection have highly regular computation patterns. They suit whole-graph compilation and operator fusion, and they usually achieve stable acceleration.
To decide which optimization approach fits a model, first check whether its computational density, parameter scale, and input shapes are fixed. These three factors determine the potential gains from quantization and the difficulty of compilation.
3. Numerical Precision: INT8 and BF16
Choosing a numerical precision is one of the most important decisions in on-device deployment. The two common options have different characteristics.
INT8 is an 8-bit integer format. During inference, weights and activation values are mapped to the integer range of -128 to 127. A scale factor and a zero point convert them back into approximate floating-point values. INT8 storage takes only one quarter of the space needed by FP32, and integer arithmetic runs efficiently on hardware. The main risk is quantization error. If the estimated range of activation values is inaccurate, some channels may be truncated, and accuracy can drop noticeably. For this reason, INT8 usually requires a representative calibration dataset to collect the activation range of each layer.
BF16 is a 16-bit floating-point format. It keeps the same exponent width as FP32, so its dynamic range matches FP32, with reduced mantissa precision. Because it does not need a complex calibration process, migrating to BF16 costs less. It is usually a safer choice for numerically sensitive models. The trade-off is that its storage and bandwidth savings are smaller than those of INT8.
The main differences between the two formats are summarized below.
| Dimension | INT8 | BF16 |
|---|---|---|
| Storage footprint | Smallest | About half of FP32 |
| Accuracy risk | Depends on calibration quality | Lower |
| Engineering complexity | Requires quantization calibration and error analysis | Simple migration |
| Typical use cases | Large model weights, convolutional networks, latency-sensitive tasks | Accuracy-sensitive models, rapid validation, cases with insufficient calibration data |
The recommended approach is to establish an accuracy baseline with BF16 first. Then evaluate whether INT8 can deliver enough benefit within an acceptable accuracy loss. The two precisions can also be mixed. For example, sensitive layers can stay at higher precision while the rest are quantized.
4. Deployment Pipeline
A complete on-device deployment pipeline typically includes the following steps.
- Model export: Convert the model from its training framework into a general intermediate representation, fix the input shapes, and check for unsupported operators.
- Graph optimization: Perform operator fusion, constant folding, and removal of redundant nodes to reduce scheduling overhead at runtime.
- Quantization calibration: Run the model on representative samples, collect the activation distribution of each layer, and compute the quantization parameters. Calibration samples should cover the types of inputs found in real business use.
- Compilation: Compile the optimized graph into an artifact that runs on the target device, and generate the corresponding memory plan.
- Inference and verification: Run the model on the target device and compare its outputs against the floating-point baseline.
The following pseudocode illustrates the data flow between these steps. The function names only describe each step. They are not specific interfaces.
# Illustrative flow: function names only describe each step and must be replaced with the actual interfaces of the toolchain in usemodel = load_model("model_fp32")graph = export_to_intermediate_repr(model, input_shape=(1, 3, 224, 224))graph = optimize_graph(graph) # operator fusion, constant foldingcalib = load_calibration_set(sample_count) # representative samplesqgraph = quantize(graph, dtype="int8", calibration=calib)artifact = compile(qgraph, target="npu")result = run(artifact, sample_input)
In practice, the quality of calibration samples matters more than their quantity. If the samples cover only simple scenarios, the activation ranges for complex inputs may be underestimated. This causes saturation and truncation.
5. Accuracy Validation and Performance Tuning
After any quantization or precision change, the model needs systematic validation. Checking only whether the model runs is not enough.
Accuracy validation should work on two levels. The first level is output consistency. Compare the quantized model’s outputs with the floating-point baseline on the same batch of inputs, using metrics such as cosine similarity and mean squared error. The second level is task metrics, such as classification accuracy, retrieval recall, or human evaluation of generated content. Both levels must pass before the deployed version can be considered usable.
When accuracy drops more than expected, you can locate the sources of error layer by layer. A common method is to replace layers with higher-precision versions one at a time, moving from the network output back toward the input, and observe how the metrics change. This identifies the layers most sensitive to error. Those layers can then be tuned with different quantization parameters or kept at higher precision.
Performance tuning focuses on three metrics. Throughput measures how many samples are processed per unit of time. Latency has two parts: the first response time and the steady-state time per request. Latency matters most for interactive applications. Peak memory determines whether the device can run other tasks at the same time. Measurements should be taken over multiple rounds after the device has stabilized, to avoid interference from cold starts and temperature changes.
Common problems include the following.
- Some operators are not supported by the target device. The runtime then falls back to the general-purpose processor, which splits the graph into several segments. Frequent data transfers between segments can cancel out the acceleration. The fix is to replace operators with equivalent ones or rewrite the subgraph before export.
- Dynamic input shapes cause repeated compilation or cache invalidation. You can pad inputs to a few fixed lengths, or precompile for commonly used shapes.
- Calibration data does not match the distribution of live data, so quantization error grows in production. Update the calibration set regularly using sampled live data.
6. Engineering Recommendations
For team collaboration, the following practices help reduce deployment risk.
First, version-control model versions, quantization configurations, and calibration data so that every result can be reproduced.
Second, use an isolated runtime environment with pinned dependency versions. This reduces accuracy or performance variation caused by environment differences.
Third, validate the full pipeline on a smaller model before scaling up to larger ones. Debugging several stages at once on a complex problem makes failures harder to isolate.
Fourth, add accuracy regression tests to continuous integration. Key metrics are then compared automatically after each model or toolchain update.
7. Summary
The core of on-device deployment is finding the right balance among accuracy, latency, memory, and power consumption. Developers should first understand the computational characteristics of the model. Then they should choose INT8 or BF16 based on task requirements, and confirm the results through a complete process of calibration, compilation, and validation. Following this approach turns on-device inference from a one-time experiment into a maintainable, reproducible engineering capability.
