Case · On-device AI · llama.cpp · Snapdragon 7 Gen 4

The phone's NPU returned garbage. Now it reads prompts 3.1× faster than the CPU.

On a Motorola Edge 70, llama.cpp's NPU backend produced nonsense. The chip's matrix unit has no FP16, and the software assumed it did. Instead of waiting for a fix, we asked what the unit can do: integer math. Three days later the NPU is the fastest way to read a prompt on that phone, with the same accuracy as the CPU.

0×
prompt speed vs the best CPU setting (Qwen3.5-4B, 512 tokens, current llama.cpp)
0×
vs the phone's GPU (Adreno 722, OpenCL)
10.84
perplexity on the NPU, 10.85 on the CPU: the same accuracy
0
Snapdragon chips checked for FP16; 5 lack it
How we got here

The reasoning, step by step.

I worked with an AI coding agent. I set the direction and the checkpoints; the agent ran the probes, wrote the kernels and measured. Every claim had to hold on the real phone before it counted. The dead ends stay in the picture: they are where most of the learning was.

Me · direction
Agent · probes, code, numbers
26.09 Question

The phone has an NPU. Can llama.cpp use it?

Motorola Edge 70, Snapdragon 7 Gen 4. The goal: local models that are actually fast.

26.09 Test

437 tokens/s on the prompt. The answer is gibberish.

The speed was fake: the matrix unit wrote zeros and infinities. Attention and the Qwen3.5 layers failed their tests too. Filed upstream as #29473.

26.09 Hypothesis

A hunch: maybe nobody has solved this yet.

"That could be our winning case."

26.09 Evidence

The maintainer: same models run clean on IQ-9075.

Same Hexagon generation (v73), different result. The difference had to be in the chip, not the code.

27.09 Test Evidence

A probe model on 21 chipsets through Qualcomm AI Hub.

Qualcomm's runtime on this chip: fp16=false. Five of 21 Snapdragons have no FP16 in the matrix unit. llama.cpp picks FP16 by generation, so the two v73 parts among them are exposed.

27.09 Decision

Turn the error into a resource.

"If we teach the matrix unit to count in integers, that's the breakthrough."

And a condition before investing: show what is actually new.

27.09 Test

Map the undocumented integer mode on the SDK emulator.

Then the same inputs on the phone: 12 of 12 output hashes match. An emulator is a hypothesis until hardware agrees.

27.09 Build

First integer matmul. First correct answer from Qwen on this NPU.

Weights to int8, activations as two 8-bit planes, the exact 32-bit sum stitched back on the vector unit. 118 t/s, already 2.3× the CPU.

27.09 Decision

Take it end to end, not just a bug report.

Correct first, then fast, then clean enough for upstream. Publish code and numbers; keep the SDK lab private (its licence).

27.09 Measure

Pipeline the units; attention goes integer too.

Matrix unit computes tile i while the vector unit finishes i−1 and prepares i+1. 118 → 126 → 147 t/s.

27.09 Dead end

Speculative decoding with the NPU as verifier

Checking 8 draft tokens costs the NPU more than the CPU. Measured, dropped.

27.09 Build Bug

Qwen3.5's recurrent layers, eight tokens per pass. Perplexity: 69.5.

All unit tests passed. Only the full quality run caught it: a decay product underflowed to 0/0 on fast-forgetting heads. Moved to log space.

27.09 Measure

172 tokens/s. Perplexity 10.84 vs 10.85 on the CPU.

3.3× the CPU, 2.3× the GPU, byte-identical greedy answers on the test prompt. The chip is detected at start-up; no settings needed.

27.09 Dead end

8-bit activations for 2× matrix speed

The matrix unit got faster, the vector unit became the wall. Quality cost +0.3–1.6%. Not worth it.

28.09 Dead end

Decode on CPU and NPU at once

Both share one memory bus. Together: +15% over the best alone. Generation is limited by memory, not compute.

28.09 Build

Two slow copies found in decode. Fixed.

8192 rows of 12 bytes each, one memory wait per row. Now: one transfer in, one vector gather, one out. Decode 8.31 → 8.99 t/s.

28.09 Ship

Public repo, upstream discussion, PR prepared.

Side result: a CPU kernel for PrismML's ternary models, 1.9× faster prompts on the same phone, opened as PR #290.

30.09–07.10 Upstream

Cause confirmed upstream. One PR merged.

Qualcomm's Hexagon maintainer confirmed it in #29473: some lower-end v73 chips have no FP16 in the matrix unit. The integer path is in review as llama.cpp PR #29740. The ternary CPU kernel is merged into PrismML (PR #290). A re-measure of an upstream copy rewrite showed a decode slowdown on this chip; the maintainer is taking another look at our faster version.

11–12.10 Upstream

Merged into llama.cpp, with a co-author credit.

The Hexagon maintainer folded the short-row copy path into his rewrite of the copy ops, kept the original commit as co-authored, and merged it as #30341 (commit). On this phone the two conv-state copies went from 142 and 258 µs to 18 and 15 µs per call; decode 8.5 → 9.2 t/s.

04.11 Upstream

Rebased onto current llama.cpp. A second chip has the same bug.

Upstream reorganised the host code; the FP16 checks moved with it and now also cover two newer FP16 users (conv2d and the lightning indexer). On current llama.cpp the CPU reads prompts faster than in September (Qwen3.5-4B: 57 t/s, was 52), while the NPU holds at 177 t/s, so the gap is now 3.1×. A developer with a Snapdragon 6 Gen 4 (SM6650) reported the same garbage output in #29473. Its limits (2 MB of fast memory, 2 vector threads), simulated on this phone, pass the same tests: Llama-3.2-1B reads prompts at 304 t/s, against 49 without the matrix unit.

Results

Faster than the CPU and the GPU, on the same phone.

Measured on v1.4, 27 September. Motorola Edge 70, 12 GB. llama-bench, 512-token prompt. CPU is the best of 2/4/6/8 threads; GPU is the stock OpenCL backend. After the rebase onto current llama.cpp (4 November) the CPU is faster (Qwen3.5-4B 57.1 t/s) and the NPU holds (177.0 t/s): 3.1×.

Prompt processing, tokens per second

Higher is better
NPU, integer HMXGPUCPU
Table view
ModelNPUGPUCPUvs CPU
Qwen3.5-4B Q4_0171.973.951.73.33×
Llama-3.2-1B Q4_0550.6307.2226.82.43×
Qwen3.5-4B Q4_0, mixed quants105.976.247.12.25×

Qwen3.5-4B prompt speed by version

Tokens per second; the dashed line is the CPU. v1.4 kept the prompt speed and made generation 8% faster (8.31 → 8.99 t/s).
Quality, wikitext-2, 20 × 512PerplexityTime
CPU10.85334:02
NPU10.83551:08

Liquid AI's LFM2.5 on the same NPU

Measured on 7 November 2026 on the same Motorola Edge 70 with the rebased branch of #29740 (commit 08c83734f). llama-bench, 512-token prompt and 64-token generation, 3 runs; the CPU is the best of 4 and 8 threads. Accuracy is the mean KL divergence against the CPU on wikitext-2, 10 × 512 tokens. Raw log.
LFM2.5, Q4_0Prompt, NPU / CPUvs CPUGeneration, NPU / CPUKLD vs CPU
1.2B-Instruct865 / 2453.5×33 / 380.004
2.6B400 / 1083.7×16 / 170.006
Tokens per second. The 1.2B model's greedy answer to a test prompt matches the CPU word for word. The stock FP16 path on this chip returns garbage for both models (perplexity 65,536 and 128,000). Generation stays slightly faster on the CPU. The KL divergence is higher than for Qwen and Llama (0.0013–0.0031); not investigated yet.

Side contribution, merged into PrismML

PrismML compresses models so that every weight is −1, 0 or +1. A 27-billion-parameter model then fits in about 6 GB and runs on a phone. On phone and Mac processors (ARM) their format only had a slow generic code path. The new ARM path decodes the weights in 8-bit lanes and uses the processor's int8 dot-product and matrix instructions. The output is the same bit for bit; their maintainer checked it on an M5 Pro. Merged on 7 October as PR #290.
Ternary Bonsai 2 27B, Snapdragon 7 Gen 4 CPUGenericNew ARM path
Reading the prompt, tokens/s1.222.33 (1.9×)
Writing the answer, tokens/s1.091.31 (+20%)
How it works

Integer math, exact sums.

The matrix unit (HMX) on this chip multiplies 8-bit integers. Everything else is arranged so the answer comes back exact.

01 · weights

q4_0 → int8

Re-quantized per column against the largest block scale in each K chunk. That scale rides in the unit's output conversion.

02 · activations

16-bit as two 8-bit planes

Fed through the uh:2x1 mode; a second bias word cancels the unsigned offset exactly.

03 · accumulate

Coarse + fine, stitched

Two 16-bit stores per chunk, joined on the vector unit (HVX) into the exact 32-bit sum, then rescaled.

04 · pipeline

Both units busy

HMX computes tile i while HVX reduces i−1 and converts weights for i+1. Attention runs the same way.

Who needs this

Five of 21 Snapdragons have no FP16 matrix math.

Qualcomm AI Hub, QNN 2.50, one probe model per chipset. On the two v73 parts without FP16, stock llama.cpp takes the FP16 path anyway. 7 Gen 4 is confirmed on a real phone; QCM6690 is untested.

no FP16: needs the integer pathFP16 available
Working with an AI agent

Who did what.

Me

  • Picked the goal and the bet: a phone NPU as a real LLM engine
  • Reframed the bug as the opportunity: integers, not a workaround
  • Asked for the novelty case before investing time
  • Set the bar: correct on hardware, same quality as the CPU
  • Chose what to publish, and what to keep private for licence reasons

Agent (Claude Code)

  • Probed the chip: AI Hub runs, emulator, hash checks on the phone
  • Wrote the integer kernels, the pipeline and the auto-detection
  • Measured every version; kept the logs and the dead ends
  • Drafted the write-ups; I edit and publish them

Honest limits

  • The matrix unit speeds up prompts. Generation is memory-bound; the CPU is still slightly faster (Qwen3.5-4B: 9.7 vs 9.0 tokens/s).
  • Only q4_0 weights run on the matrix unit, so mixed-quant models gain less (2.25×).
  • On a 300-token prompt the greedy text follows the CPU for about 10 tokens, then picks a different word. Perplexity stays the same.
  • Tested on one phone; the Snapdragon 6 Gen 4 numbers are a simulation on it. Not merged into llama.cpp yet: the integer path is in review as #29740.