On-device & edge inference

In one sentence

Run the model on the device instead of the cloud: you win latency, privacy, offline use and near-zero cost per query, and pay with small models, tight memory and a fleet to update.

Analogy: A pocket calculator versus calling a mathematician: the calculator is instant, private and works in a tunnel, but for hard problems you still make the call.

Easy and private requests stay on device; hard ones escalate to a stateless cloud model. Models ship over the air like app releases: staged and reversible.

How it works

  • A flagship phone shares ~8–12 GB of RAM with the OS; a 3B model at 4-bit is ~1.5 GB.
  • Decode reads every weight per token, so tokens/s ≈ memory bandwidth ÷ model bytes.
  • Shrink the model: ~1–4B params, 4-bit quantization, distillation from a large teacher, LoRA adapters per task.
  • Go hybrid: a router keeps easy or private requests on device and escalates hard ones to the cloud.
  • Ship models over the air like code: versioned, staged, with rollback and a compatibility matrix per device class.

Check yourself

Answer to mark it learned; your answers aren't scored.

After an over-the-air update of your on-device model, decode on one Android device class is suddenly several times slower. Other device classes are fine and outputs are still correct. What's the most likely cause?

Go deeper

Full explanation

Edge inference runs the model where the data is born: phone, laptop, car or camera. Four reasons: latency (no network round trip), privacy (data stays local), offline operation and cost (the user paid for the chip, so a query costs you ~nothing).

The constraints are physical:

  • Memory: a flagship phone has ~8–12 GB of RAM shared with the OS and apps. A 3B model at 4-bit is ~1.5 GB of weights, plus the KV cache.
  • Bandwidth: decode reads every weight per token, so tokens/s ≈ memory bandwidth ÷ model bytes (GPU memory math).
  • Power and heat: sustained load throttles and drains the battery.
  • Accelerators: NPUs are fast only for the ops and formats they support.

Techniques: small models (~1–4B), Quantization (INT8, INT4, FP8) to 4 bits or below, Knowledge distillation from a large teacher, pruning, and small LoRA adapters per task over one shared base.

Runtimes: Core ML (Apple), LiteRT (ex-TensorFlow Lite) and AICore with Gemini Nano (Android), ONNX Runtime, ExecuTorch (PyTorch, 1.0 in Oct 2025), llama.cpp/GGUF (laptops), TensorRT (NVIDIA Jetson, cars).

Most products are hybrid: a router keeps easy or private requests on device and escalates hard ones, as Apple Intelligence does with Private Cloud Compute. Ship models over the air like code: versioned, staged, with rollback and a compatibility matrix per device class.

Code Local model first, cloud on escalation
pythonLocal model first, cloud on escalation
from llama_cpp import Llama   # llama.cpp bindings: GGUF on CPU, Metal or CUDA

# Any ~3B instruct model in GGUF. Q4_K_M averages ~4.8 bits/weight -> ~2 GB file.
local = Llama(
    model_path="models/slm-3b-instruct.Q4_K_M.gguf",
    n_ctx=4096,        # KV cache grows with context: keep it small on device
    n_gpu_layers=-1,   # offload all layers to the GPU when there is one
)

LOCAL_TASKS = {"classify", "rewrite", "summarize_short"}   # where evals say 3B is enough

def route(task: str, prompt_tokens: int, sensitive: bool, online: bool, cloud_ok: bool) -> str:
    if not online or (sensitive and not cloud_ok):
        return "local"                       # offline and privacy beat quality
    if task in LOCAL_TASKS and prompt_tokens < 2_000:
        return "local"                       # free for us, no network round trip
    return "cloud"                           # hard or long: escalate

def answer(task: str, prompt: str, **ctx) -> str:
    target = route(task, len(prompt) // 4, **ctx)   # ~4 characters per token
    log_route(task, target)                         # escalation rate is your key metric
    if target == "local":
        out = local.create_chat_completion(
            messages=[{"role": "user", "content": prompt}], max_tokens=256)
        return out["choices"][0]["message"]["content"]
    return cloud_complete(prompt)                   # via your gateway
Real example

Tesla's FSD computer is edge inference where the cloud is not an option: at highway speed a 100 ms network round trip is ~3 m of travel, and coverage drops in tunnels. The 2019 board (HW3) carries two independent FSD chips, each with its own power and memory, ~144 TOPS in total. Each chip runs the networks on all camera feeds and produces a driving plan; the plans are cross-checked before the car acts, so it can keep going if one chip fails. New networks reach the fleet over the air after shadow-mode validation. The datacenter's job is training, not driving.

Trade-offs +3 · −4

+ You gain

  • Latency with no network round trip, and it keeps working offline.
  • Data stays on the device: a simpler privacy and compliance story.
  • Marginal cost per query is ~zero for you; the user's hardware pays.

− You pay

  • Small models reason and know far less than frontier cloud models.
  • Fragmented hardware: each device class needs its own build, quantization and tests.
  • Slow, partial updates: a long tail of devices keeps running old model versions.
  • Battery, heat and app-size budgets: a 2 GB model download is a product decision.

Use it when

Latency-critical or offline paths (cars, robots, AR, keyboards), data that shouldn't leave the device, or very high-volume simple tasks where cloud cost per query would dominate.

Avoid it when

Tasks that need frontier reasoning, fresh knowledge or long context; low-volume features where cloud cost is trivial; or teams that can't maintain device-specific model builds.

Numbers to know 5
  • Apple (2024): ~3B on-device model, mixed 2/4-bit at ~3.7 bits/weight; ~0.6 ms per prompt token and ~30 tokens/s on iPhone 15 Pro.
  • Weights ≈ params × bits ÷ 8: 3B at 4-bit ≈ 1.5 GB; at 16-bit ≈ 6 GB, too much for most phones.
  • Rule of thumb: decode tokens/s ≲ memory bandwidth ÷ weight bytes; ~50 GB/s ÷ 1.5 GB ≈ ~30 tokens/s.
  • Apple's per-task LoRA adapters (rank 16) on the 3B model weigh tens of MB, so one base serves many features.
  • Tesla HW3 (2019): two independent FSD chips, ~72 TOPS each, ~144 TOPS per computer.
Common pitfalls 4
  • Benchmarking on a cool, plugged-in dev device. Measure sustained speed on a warm phone running on battery.
  • Ignoring the KV cache: long contexts can need as much memory as the weights.
  • No escalation telemetry: without logging how often and why the router escalates, you can't see the local model failing.
  • OTA updates without a device compatibility matrix: an op the NPU lacks silently falls back to the much slower CPU.
Interview questions 3

Answer out loud first, then reveal.

When would you put an LLM feature on device instead of in the cloud?Show answer

When one of four drivers is strong: latency the network can't meet, data that must not leave the device, offline use, or volume so high that cloud cost per query hurts. Then the real test is whether a ~3B quantized model passes my evals for that task on the weakest device I support, measured warm and on battery. If yes, I go hybrid: on device by default, escalate hard cases to the cloud, and track the escalation rate. The trade-off is a lower quality ceiling and a fleet of model builds to maintain.

Design the hybrid edge–cloud architecture for a phone assistant.Show answer

On device: a small base model with per-feature LoRA adapters, plus a router that uses task type, length and a confidence check. Hard requests escalate to a cloud model. For privacy I'd follow Apple's Private Cloud Compute principles: stateless processing, no privileged admin access, and server images published so researchers can verify them. Models ship through an OTA registry with staged rollout by device class, a kill switch and rollback. Telemetry is aggregated and privacy-preserving: escalation rate, latency, crash rate per model version. Offline, the assistant degrades gracefully instead of failing.

What changes when edge inference is safety-critical, like Tesla's FSD?Show answer

Three things. Availability: the model can't depend on a network, so perception and planning run in the car within a fixed compute and power budget. Redundancy: Tesla's HW3 has two independent chips that each compute a plan and cross-check it, so one failure doesn't stop the car. Validation: new networks run in shadow mode on the fleet, compared against human drivers, before they're allowed to act, and rollouts are staged over the air. The fixed hardware budget also forces efficiency work that cloud teams can postpone.