We faced the task: an object detection model took 100 MB and ran in 200 ms on a user device. After quantization — 25 MB and 50 ms. But sometimes accuracy dropped unpredictably. Here we share our experience: how to choose the method, analyze layers, and verify results. Below are proven approaches for iOS and Android with code and concrete metrics.
Mobile ML optimization is critical for deploying neural networks on devices. Neural network quantization reduces model precision from float32 to int8, achieving inference acceleration of 2–4× on mobile CPUs. Model size reduction of up to 75% is possible. The quantization function Q(r) = round(r/S) - Z, where S is scale and Z is zero-point, with per-channel granularity often used for CNNs.
Quantization — converting model weights from float32 to a lower-bit format: float16, int8, int4. A ResNet-50 model weighs 98 MB in FP32. After int8 quantization — 25 MB. Inference speed on mobile CPU increases 2–4× due to reduced data volume and integer ARM NEON/SVE instructions. But naive quantization often degrades accuracy more than desired. Proper quantization means selecting the method, analyzing sensitive layers, and verifying degradation.
We have been in mobile optimization for over 5 years, completed 15+ quantization projects for clients in e-commerce, fintech, and IoT. Company metrics: Over 5 years on the market, 15+ quantizations, 98% satisfaction rate. We guarantee accuracy within agreed tolerances. Order model optimization — get a consultation for your project.
Which Quantization Method Should You Choose?
Post-Training Quantization (PTQ) — quantize an already trained model without retraining. Two variants:
- Dynamic quantization — weights in int8, activations computed in float32 at runtime. Simple, no calibration data required. Works well for RNN/Transformer (BERT, LLM). For CNN, less speedup.
- Static quantization — both weights and activations in int8. Requires a calibration dataset (100–500 representative examples). Faster than dynamic, but needs calibration.
Quantization-Aware Training (QAT) — the model is fine-tuned with simulated quantization. Weights adapt to lower precision. Best quality, but requires access to the training dataset and GPU time.
| Method | Data | Accuracy | Speed | Complexity |
|---|---|---|---|---|
| Dynamic PTQ | None | Medium | High | Low |
| Static PTQ | 100–500 examples | High | Very High | Medium |
| QAT | Full dataset | Very High | High | High |
PyTorch static PTQ code
# PyTorch: static PTQ via torch.quantization
import torch
from torch.quantization import quantize_static, get_default_qconfig
model.eval()
model.qconfig = get_default_qconfig('fbgemm') # x86; for ARM — 'qnnpack'
torch.quantization.prepare(model, inplace=True)
# Calibration: run through calibration dataset
with torch.no_grad():
for batch in calibration_loader:
model(batch)
torch.quantization.convert(model, inplace=True)
# Now model contains quantized layers
For mobile Android (ARM) — use qconfig = 'qnnpack', not 'fbgemm'. This changes the order of quantized operations for the QNNPACK backend, which uses ARM NEON instructions.
TFLite Quantization: Full Integer
# Conversion with full int8 (activations + weights)
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.int8
converter.inference_output_type = tf.int8
# Calibration generator — critical for static quantization accuracy
def representative_dataset():
for sample in calibration_data[:500]:
yield [sample.astype(np.float32)]
converter.representative_dataset = representative_dataset
tflite_model = converter.convert()
Full int8 models run on NNAPI and Hexagon DSP — where FP16 is not supported. On Snapdragon 778G via Hexagon — 5–8× faster than CPU with proper INT8 quantization.
Core ML Quantization on iOS
import coremltools as ct
from coremltools.optimize.coreml import (
OptimizationConfig,
OpLinearQuantizerConfig,
linearly_quantize_weights
)
# Load the already converted Core ML model
mlmodel = ct.models.MLModel("model_fp32.mlpackage")
# Configuration: 8-bit linear weight quantization
config = OptimizationConfig(
global_config=OpLinearQuantizerConfig(
mode="linear_symmetric",
dtype=np.int8,
granularity="per_channel" # per_channel more accurate than per_tensor for CNN
)
)
compressed_model = linearly_quantize_weights(mlmodel, config)
compressed_model.save("model_int8.mlpackage")
per_channel quantization — a separate scale factor for each output channel of a convolutional layer. Significantly more accurate than per_tensor (one scale per layer), but slightly slower. For CNN, usually worth it.
| Feature | Core ML | TensorFlow Lite |
|---|---|---|
| Weight format | FP16/INT8 (weight-only) | INT8 (full integer) |
| Calibration | Not required for weight-only | Required for static |
| NNAPI support | No (iOS) | Yes (Android) |
| Tool | coremltools | TFLiteConverter |
| Performance | ~2× on iPhone | ~3-4× on Android with DSP |
Addressing Accuracy Loss After Quantization
Not all layers tolerate quantization equally. The first and last layers of a network, as well as attention layers in transformers, are often the most sensitive. We perform sensitive layer analysis to identify which layers are most affected. Tool: per-layer sensitivity analysis.
# Check accuracy degradation when quantizing each layer individually
from torch.quantization.quantize_fx import prepare_fx, convert_fx
baseline_accuracy = evaluate(float_model, test_loader)
for layer_name in get_all_quantizable_layers(model):
# Quantize only this layer
single_layer_model = quantize_single_layer(model, layer_name)
layer_accuracy = evaluate(single_layer_model, test_loader)
sensitivity = baseline_accuracy - layer_accuracy
print(f"{layer_name}: sensitivity={sensitivity:.4f}")
Layers with high sensitivity are left in FP32 — this is mixed precision quantization. The rest are converted to INT8. 5–10% of "heavy" layers stay in FP32; the model loses only 20–30% of size instead of 75%, but accuracy is preserved.
How to Verify Quantization Correctness?
After quantization, always:
-
Accuracy on test dataset — compare top-1/top-5 accuracy with the original. Acceptable degradation: FP16 — <0.5%, INT8 — <2%. If larger, switch to QAT or mixed precision.
-
Numerical error — compare outputs of float and quantized models on identical inputs. MSE < 0.01 is usually acceptable.
-
Speed on real devices — not on simulator. Xcode Instruments → Core ML Profiler for iOS,
adb shell am instrument+ TFLite Benchmark Tool for Android. -
Crash test — different inputs, edge cases (black image, very bright, non-standard aspect ratio). INT8 models sometimes overflow on extreme inputs.
Practical Case from Our Experience
For one client, we optimized the YOLOv8n object detection model. In FP32 — 6.3 MB, 45 ms on iPhone 13. After Core ML INT8 quantization — 1.8 MB, 12 ms. mAP dropped from 37.3 to 36.1 — within acceptable range for the task. On Snapdragon 8 Gen 1 via TFLite INT8 + NNAPI — 8 ms. This project saved the client an estimated $15,000 per year in cloud inference costs. For a typical model serving 1 million inferences per month, quantization can reduce cloud compute costs from $3,000 to $750 per month, saving $2,250 monthly or $27,000 annually.
What Is Included in the Work (Deliverables)
- Audit of the original model and method selection (PTQ/QAT, INT8/FP16).
- Preparation of calibration dataset and calibration tuning.
- Sensitive layer analysis and mixed precision configuration.
- Full quantization with accuracy verification.
- Speed measurements on target devices (iOS/Android).
- Degradation report and recommendations.
- Integration of the quantized model into your pipeline.
- Deliverables: Documentation of the quantization process, calibration dataset, accuracy report with before/after metrics, integration code (Python, C++, Swift/Kotlin), and 2 weeks of post-deployment support.
- Access to our proprietary layer sensitivity analysis tool.
According to PyTorch documentation, PTQ can reduce model size up to 4 times. For detailed study also see TensorFlow Lite post-training quantization.
Timeline Estimates
PTQ for one model with verification — 1–2 weeks. QAT with full retraining and testing — 3–6 weeks depending on dataset size. Our quantization service starts at $2,500 per model. Quantization reduces model storage costs up to 75%, saving thousands of dollars annually.
Contact us to evaluate your project. We'll help choose the optimal method and guarantee results.







