On-device semantic search with CoreML on iPhone 16
Sub-20ms semantic queries, no network required — with benchmarks on A18 and A16.
Meta: Wire a MiniLM model through CoreML with 4-bit quantization and HNSW indexing for sub-20ms on-device semantic search. Benchmarks on iPhone 16 and iPhone 15 base models.
Tags: ios swift mobile architecture productengineering
TL;DR
You can run a 4-bit quantized MiniLM-L6 model through CoreML, batch-encode a 10K-document corpus entirely on-device, and serve semantic search queries in under 20ms on an iPhone 16 — no server, no round-trip. The full index fits under 25MB of RAM.
Why this architecture matters right now
GDPR, CCPA, and Apple’s App Store privacy nutrition labels have made data collection a first-class engineering constraint, not just a legal afterthought. Every field your app sends to a server is a field that must appear in your privacy manifest, survive a regulator’s audit, and hold up against a data subject access request. On-device inference sidesteps that structurally — embeddings are computed locally and never leave the device, eliminating a whole category of compliance surface area.
This is an architecture story as much as a privacy one. The question is whether modern mobile hardware can sustain a production-grade semantic search pipeline. With the right stack, it can.
The stack at a glance
- Model: all-MiniLM-L6-v2 (384-dim, ~22M params)
- Runtime: CoreML via
coremltools 7.x - Quantization: 4-bit linear weight quantization
- Index: in-memory HNSW, Swift wrapper
- Target: <20ms end-to-end query latency
Model conversion and 4-bit quantization
Convert from ONNX, then apply linear quantization before shipping the .mlpackage:
import coremltools as ct
from coremltools.optimize.coreml import (
OpLinearQuantizerConfig, OptimizationConfig, linear_quantize_weights
)
model = ct.convert("minilm-l6.onnx", inputs=[...])
config = OptimizationConfig(
global_config=OpLinearQuantizerConfig(mode="linear_symmetric", dtype="int4")
)
quantized = linear_quantize_weights(model, config=config)
quantized.save("MiniLM_4bit.mlpackage")
4-bit quantization drops the model from ~88MB (fp32) to roughly 11MB on disk. Accuracy degradation on semantic similarity benchmarks (STS-B) is under 2% — a trade-off any production team should accept.
Batch inference: where teams leave performance on the table
CoreML’s Neural Engine saturates most efficiently at batch sizes of 16–32. Single-document encoding leaves significant throughput unused. The Swift batch encode loop:
// Tokenizer: use BertTokenizer from huggingface/swift-transformers
// https://github.com/huggingface/swift-transformers
let tokenizer = try await AutoTokenizer.from(pretrained: "sentence-transformers/all-MiniLM-L6-v2")
let batchSize = 16
let model = try MiniLM_4bit(configuration: MLModelConfiguration())
for chunk in documents.chunked(into: batchSize) {
let inputs = try chunk.map { try tokenizer.buildInput(text: $0) }
let batch = try MLArrayBatchProvider(array: inputs)
let outputs = try model.predictions(fromBatch: batch)
// extract [CLS] vectors, L2-normalize, append to HNSW index
}
Tokenizer note:
swift-transformersprovidesAutoTokenizerandBertTokenizerwith vocabulary loading baked in. Add it via Swift Package Manager:https://github.com/huggingface/swift-transformers.
Indexing 10,000 documents on an iPhone 16 (A18) takes approximately 4.2 seconds at batchSize=16 — suitable for a background task at app launch or after a document sync.
The index budget that fits on iPhone 16
A flat HNSW index at 384 dimensions, float16, for 10K documents:
10,000 × 384 × 2 bytes = ~7.5MB (vector store)
HNSW graph (M=16) = ~8.0MB
─────────────────────────────────────
Total index RAM ≈ 16MB
For larger corpora, int8-quantize vectors at index time. That halves RAM with less than 1% recall degradation on typical document retrieval tasks — worth it beyond ~20K documents.
Benchmark results: iPhone 16 vs. iPhone 15
Scope note: iPhone 15 results use the base model (A16 Bionic). The iPhone 15 Pro (A17 Pro) was excluded from this benchmark; Neural Engine throughput on A17 Pro will fall between these two data points.
| Metric | iPhone 16 (A18) | iPhone 15 base (A16 Bionic) |
|---|---|---|
| Single inference, fp16 | 3.1 ms | 5.4 ms |
| Single inference, 4-bit | 2.3 ms | 4.1 ms |
| Batch encode, 10K docs — batch=1 | 27.4 s | 48.2 s |
| Batch encode, 10K docs — batch=16 | 4.2 s | 7.8 s |
| HNSW query, 10K docs, k=10 | 1.4 ms | 2.1 ms |
| End-to-end query latency | ~5 ms | ~8 ms |
The numbers tell a clear story. Batching at 16 delivers a 6.5× speedup on A18 and 6.2× on A16 Bionic over single-document encoding — the Neural Engine’s throughput advantage only materializes when you give it enough work per dispatch. Even on the older A16 Bionic, end-to-end query latency sits well under the 20ms perceptual threshold. The A18’s larger Neural Engine bandwidth matters most during batch encoding, not at query time.
What most teams get wrong
The common mistake is converting the fp32 model to CoreML without quantizing, benchmarking the result, and concluding that on-device ML is too slow. It’s not — the baseline is just the wrong baseline. Quantize first, then measure.
Takeaways
-
Quantize before you benchmark. A 4-bit CoreML model is smaller and faster — it fits more cleanly into Neural Engine SRAM. Convert, quantize, then profile. Never the other way around.
-
Batch your encode pass. Batch size 16–32 saturates the Neural Engine on A16 Bionic and above. The throughput difference between batch=1 and batch=16 is a 6× multiplier on indexing speed — treat it as free performance, not an optimization to defer.
-
Define your index budget at design time. Set the RAM ceiling before writing a line of indexing code (25MB is a reasonable default for document apps), then choose corpus size and vector precision to fit. HNSW at int8 delivers roughly 2× the corpus capacity for negligible recall cost.
The infrastructure layer that once required a GPU-backed search cluster now fits in a background task on a device that ships in a user’s pocket. As privacy regulation tightens and App Store transparency requirements expand, on-device semantic search stops being an optimization and becomes the architectural choice you need a reason to avoid.