Jatin Kishnani commited on
Commit
b2f2822
Β·
1 Parent(s): 9508e89

Publish 4-bit GGUF path; drop A16W4

Browse files

- Add gemma4-e2b-w4.gguf (2.44 GiB) and gemma4-e2b-w4-mtp.gguf (56 MiB),
tracked via a new *.gguf LFS rule.
- Remove the four gemma4_*_awq_pc_int4_v{79,81}.bin binaries and their
SHA256SUMS entries.
- README: rename A16W8 -> "8-bit quant" and W4 -> "4-bit quant" throughout,
and remove all A16W4 material. The group-32 vs per-channel granularity
finding is kept, stated on its own rather than as an A16W4 comparison
(~5.3 pp of MMLU, per the table's own numbers; the prose previously said
~4.8 pp, which its table did not support).
- SHA256SUMS: regroup by quant, with the 4-bit hashes added. The four int4
lines had also been filed under the "v81 UNVERIFIED" header despite two
of them being v79.

.gitattributes CHANGED
@@ -1,3 +1,4 @@
1
  *.bin filter=lfs diff=lfs merge=lfs -text
2
  *.bf16 filter=lfs diff=lfs merge=lfs -text
3
  tokenizer.json filter=lfs diff=lfs merge=lfs -text
 
 
1
  *.bin filter=lfs diff=lfs merge=lfs -text
2
  *.bf16 filter=lfs diff=lfs merge=lfs -text
3
  tokenizer.json filter=lfs diff=lfs merge=lfs -text
4
+ *.gguf filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -12,22 +12,26 @@ tags:
12
  library_name: qai-hub
13
  ---
14
 
15
- # Gemma-4-E2B-it β€” A16W8 and A16W4 for the Qualcomm Hexagon NPU
16
 
17
- `google/gemma-4-E2B-it` quantized to **A16W8** (int16 activations, int8 weights) and compiled
18
- to **QNN context binaries** that run **100% on the Hexagon NPU** β€” no CPU or GPU fallback.
19
 
20
- Most shipped mobile builds of this model use int8 activations (GGUF-style `q4_0`). Keeping
21
- activations at 16 bits costs some memory and holds onto accuracy: on a held-out 400-question
22
- MMLU slice this build is **statistically indistinguishable from the unquantized model**.
 
 
 
 
 
23
 
24
  > **Read the verification table before using a binary.** Not every target below has been run
25
- > on physical hardware, and this README states exactly which have. As of 2026-07-27 both
26
- > **prefill** binaries are verified on both v79 and v81 silicon; **v81 decode is not verified.**
27
 
28
  ---
29
 
30
- ## Verification status per target
31
 
32
  Everything below was measured on **physical hardware** β€” Snapdragon 8 Elite (SM8750, HTP v79)
33
  via Qualcomm Device Cloud, device `41c5710f`, 2026-07-26, and Snapdragon 8 Elite Gen 5
@@ -55,7 +59,7 @@ Two consecutive runs produced byte-identical output, so decoding is deterministi
55
  **Prefill** β€” 12 held-out chat prompts, each one forward pass, compared against float:
56
  **12/12 (100%)** next-token top-1, hidden cosine mean 0.992 / min 0.986.
57
 
58
- ### Speed ladder (v79, A16W8, AI Hub profile on real hardware)
59
 
60
  | decode graph | ms/step | tok/s | speedup |
61
  |---|---|---|---|
@@ -90,7 +94,7 @@ row deliberately re-ran only the harness-validation prompt and the one prompt v8
90
  The single v81 disagreement is `"Name a planet with rings."`, where float opens `'**'` and the
91
  device opens `'The'` at cos 0.9805 β€” a near-tie between two plausible sentence openings, not a
92
  degradation. **Re-running that exact prompt on the v79 binary reproduced the identical
93
- mismatch at an identical cosine of 0.9804982542991638**, so it is a property of the A16W8
94
  quantization, not of v81. On this evidence the two architectures are numerically
95
  indistinguishable on the trunk.
96
 
@@ -130,19 +134,19 @@ ships a `hexagon-v81` skel matching the compile.
130
 
131
  ---
132
 
133
- ## Accuracy
134
 
135
  Held-out MMLU, 0-shot, chat-formatted, 400 questions disjoint from all calibration data:
136
 
137
  | | accuracy |
138
  |---|---|
139
  | base model (float, ≑ HF) | 56.75% Β± 2.48 |
140
- | **A16W8 (this build)** | **59.25% Β± 2.46** |
141
  | delta | +2.50 pp |
142
  | random baseline | 25.00% |
143
 
144
  The +2.50 pp delta is about one standard error β€” **not** evidence that quantization improves
145
- the model. The correct reading is that A16W8 costs no measurable MMLU accuracy. Note the two
146
  models disagree on ~24% of individual questions; they match in aggregate, not per-question.
147
 
148
  ---
@@ -209,11 +213,14 @@ quantization of that graph.
209
  ## Files
210
 
211
  ```
212
- gemma4_decode_wgqa_int8kv_a16w8_v79.bin 1.9 GB decode, v79 (recommended)
213
- gemma4_decode_wgqa_a16w8_v79.bin 1.9 GB decode, v79, no int8-KV
214
- gemma4_trunk_a16w8_v79.bin 1.9 GB prefill, v79, fixed SEQ=128
215
- gemma4_decode_wgqa_int8kv_a16w8_v81.bin 1.9 GB decode, v81 [UNVERIFIED - see above]
216
- gemma4_trunk_a16w8_v81.bin 1.9 GB prefill, v81 (verified on v81 silicon)
 
 
 
217
 
218
  host-model/embed_tokens_weight.bf16 769 MB token embeddings
219
  host-model/embed_tokens_per_layer_weight.bf16 4.4 GB per-layer embeddings
@@ -235,7 +242,7 @@ The three `host-model` tensors are ~5.2 GB and stay on the host by design β€” pu
235
 
236
  ---
237
 
238
- # How to run
239
 
240
  ## What you need that is NOT in this repo
241
 
@@ -330,139 +337,293 @@ part of normal use.
330
 
331
  ---
332
 
333
- # A16W4 (int16 activations, int4 weights)
334
 
335
- A second precision in the same repo: **AWQ per-channel int4 weights** with the same int16
336
- activations and the same WGQA decode graph. It is **half the size and ~2Γ— the NPU compute
337
- speed of A16W8 β€” and it costs real accuracy.** Unlike the A16W8 build, this one is *not*
338
- statistically indistinguishable from the unquantized model.
339
 
340
- | | A16W4 | A16W8 |
 
 
 
341
  |---|---|---|
342
- | decode + prefill binaries | **1.80 GiB** | 3.54 GiB |
343
- | decode NPU compute | **33.5 ms/step Β· 29.9 tok/s** | 65.3 ms/step Β· 15.3 tok/s |
344
- | held-out MMLU (n=400) | **48.50% Β±2.50** | 59.25% Β±2.46 |
345
- | vs unquantized base (56.75% Β±2.48) | **βˆ’8.25 pp β€” a real loss (~2.3 SE)** | +2.50 pp (~1 SE, not significant) |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
346
 
347
- **Choose A16W4 only if you are memory-constrained.** If you can afford 3.5 GiB, A16W8 is
348
- float-exact on device and this one is not.
 
 
349
 
350
- ## A16W4 verification status
351
 
352
- ### v79 (SM8750 / Snapdragon 8 Elite) β€” verified on hardware
 
353
 
354
- Measured on a physical Snapdragon 8 Elite via Qualcomm Device Cloud, device `2a38935c`.
355
 
356
- | binary | role | on hardware |
357
  |---|---|---|
358
- | `gemma4_decode_awq_pc_int4_v79.bin` | decode | βœ… runs, deterministic; token-exact on some prompts, **not all** |
359
- | `gemma4_trunk_awq_pc_int4_v79.bin` | prefill, fixed SEQ=128 | βœ… 10/12 next-token, cos mean 0.818 / min 0.740 |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
360
 
361
- **Decode**, free-running with the chat template, against the float reference:
362
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
363
  ```
364
- "The capital of France is" -> 'The capital of France is **Paris**.' exact 8/8
365
- "Who wrote Hamlet?" -> '**William Shakespeare** wrote *Hamlet*.' exact 8/8
366
- "What color is grass?" -> 'Grass is **green**.' 2/6
367
- (float: 'Grass is typically **green**.')
368
- "Name a country in South America." -> 'Name a country in South America.' 0/7
369
- (float: 'Brazil')
 
 
370
  ```
371
 
372
- Two consecutive runs of the first prompt were byte-identical, so decoding is deterministic.
373
 
374
- **Read the last row honestly.** On that prompt the model echoed the input instead of
375
- answering. That is a genuine failure, not a paraphrase, and it is the kind of degradation the
376
- βˆ’8.25 pp MMLU predicts. Two of four prompts are token-exact with float; one differs only in
377
- wording; one fails outright.
 
378
 
379
- **Prefill** β€” 12 held-out chat prompts vs float: **10/12 (83.3%)** next-token top-1, hidden
380
- cosine mean 0.818 / min 0.740. (A16W8 on the same harness: 12/12, cos 0.992 / 0.986.) The
381
- device result matches this build's CPU simulation (11/12, cos 0.813) within noise, i.e. the
382
- hardware reproduces the quantization faithfully β€” the loss is in the quantization, not the port.
383
 
384
- ### v81 (SM8850 / 8 Elite Gen 5) β€” prefill verified, decode never run
385
 
386
- Measured on **real Snapdragon 8 Elite Gen 5 silicon** via AI Hub inference, 2026-07-27.
 
387
 
388
- | binary | role | on hardware |
389
- |---|---|---|
390
- | `gemma4_trunk_awq_pc_int4_v81.bin` | prefill, fixed SEQ=128 | βœ… 9/12 next-token, cos mean 0.809 / min 0.610 β€” **bit-identical to v79** |
391
- | `gemma4_decode_awq_pc_int4_v81.bin` | decode | ⚠️ **never run on hardware** |
392
 
393
- **The v81 and v79 trunks produced bit-identical output.** Both binaries were run through the
394
- same harness on their own silicon β€” v81 on SM8850, v79 on SM8750 β€” over the same 12 held-out
395
- prompts. Every one of the 12 hidden cosines agreed **to the full float32 bit pattern**
396
- (maximum absolute difference across all 12: exactly `0.0`), and all 12 device tokens matched,
397
- including the same three disagreements with float on the same three prompts:
 
398
 
399
- | trunk binary | device | next-token top-1 | hidden cos (mean / min) |
400
- |---|---|---|---|
401
- | `gemma4_trunk_awq_pc_int4_v81.bin` | SM8850 (v81) | 9/12 | 0.80949 / 0.61016 |
402
- | `gemma4_trunk_awq_pc_int4_v79.bin` | SM8750 (v79) | 9/12 | 0.80949 / 0.61016 |
403
 
404
- HTP consumes per-channel int4 natively on both architectures and, on this graph, does so
405
- deterministically and identically. **The v81 trunk carries exactly the A16W4 accuracy loss
406
- documented above β€” no more, and no less.**
 
 
407
 
408
- These 9/12 figures come from the AI Hub inference path; the 10/12 (cos 0.818 / 0.740) quoted
409
- for v79 above was measured over adb on Device Cloud with the same prompts and harness logic.
410
- The one-prompt difference is the near-tie on `"Who painted the Mona Lisa?"` resolving
411
- differently between the two measurement paths, not an architecture effect β€” which is precisely
412
- why the v79 control was re-run here rather than compared across paths.
413
 
414
- **Decode on v81 has still never been run.** Same reason as A16W8: AI Hub bills one farm job
415
- per decode step, and Device Cloud has no v81 part. Given that the trunk is bit-identical
416
- across the two architectures, decode is *likely* fine too β€” but that is an inference, not a
417
- measurement, and A16W4 decode is the path where this precision's real failures show up (it
418
- echoes the prompt instead of answering on at least one prompt, on verified v79 silicon).
419
 
420
- ## Reproducing the A16W4 quantization
 
 
 
421
 
422
- AIMET `QuantizationSimModel`, `param_type=int4`, `activation_type=int16`,
423
- `quant_scheme=min_max`, `NEG=-1e4`, calibrated on real activations captured from
424
- **chat-formatted** decode loops β€” every caveat in the A16W8 section applies here unchanged.
425
 
426
- Weights additionally use **AWQ per-input-channel scaling folded into the preceding RMSNorm**,
427
- so it costs nothing at inference. Sanity check: the AWQ *float* trunk scores exactly the same
428
- MMLU as the unquantized base (56.75%), confirming the fold is numerically exact.
429
 
430
- ### Two things that will waste your time
431
 
432
- **IO dtype depends on who compiled the binary.** AI Hub builds expose
433
- `QNN_DATATYPE_FLOAT_32` graph inputs; binaries produced locally by
434
- `qnn-context-binary-generator` keep the DLC's `QNN_DATATYPE_UFIXED_POINT_16`. The reference
435
- harness writes fp32, so it is correct only for AI Hub builds β€” feed fp32 into a uint16-IO
436
- binary and you get fluent-looking garbage. Check before debugging anything else:
437
 
438
  ```bash
439
- qnn-context-binary-utility --context_binary X.bin --json_file io.json # -> graphInputs[].dataType
 
440
  ```
441
 
442
- **Pair each binary with the QAIRT version that built it.** A 2.48-compiled context binary on
443
- a 2.42 runtime fails with `Could not create context from binary` β€” the same message as an HTP
444
- arch mismatch, but a completely different cause.
445
 
446
- ### A note on LPBQ / group-64 int4
 
 
 
 
 
 
447
 
448
- We built and measured it; **do not use it.** Low-Power Blockwise Quantization stores 4-bit
449
- values inside an int8 container, so it saves no memory at all (1.78 GiB β€” *larger* than
450
- A16W8's 1.77 GiB), scores 6 pp worse than A16W8 on MMLU, and runs **9.3Γ— slower**
451
- (609 ms/step) because HTP cannot consume blockwise weights natively and inserts ~1079
452
- dequantize ops per step. It is dominated by A16W8 on every axis. Per-channel int4 is the only
453
- 4-bit form worth shipping on this graph.
454
 
455
- ## A16W4 limitations
456
 
457
- Everything in the A16W8 limitations section applies, plus:
 
458
 
459
- - **Not float-equivalent.** It fails some prompts outright (see the table above).
460
- - **The speed figure is NPU compute per step, not TPOT.** Real end-to-end throughput is
461
- unmeasured and requires a resident-KV runtime. Our file-based `qnn-net-run` harness measures
462
- 598 ms/step for A16W4 and 592 ms/step for A16W8 β€” nearly identical *despite A16W8 having
463
- twice the compute* β€” because ~126 MB of KV file traffic per step dominates everything. Do
464
- not quote either number as throughput.
465
- - **v81 prefill is verified** and bit-identical to v79; **v81 decode has never been run.**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
466
 
467
  ---
468
 
@@ -509,10 +670,10 @@ concatenating, or you will silently assemble a truncated file.
509
  | Fluent but wrong answer | binary/mask mismatch β€” the host `NEG` must be `-1e4`, matching calibration |
510
  | Garbage tokens, hidden norm β‰ˆ 0 | wrong context binary, or KV buffers not zeroed before position 0 |
511
  | Different output across identical runs | a KV buffer was corrupted mid-push; re-seed (`dd` on device) and retry |
512
- | Fluent garbage from an A16W4 binary you compiled yourself | local `qnn-context-binary-generator` emits `UFIXED_POINT_16` IO; the harness writes fp32. Use the AI Hub build, or check `graphInputs[].dataType` |
513
  | `Could not create context from binary` on a binary that used to work | QAIRT runtime version does not match the version that compiled it |
514
 
515
- ## Reproducing the quantization
516
 
517
  AIMET `QuantizationSimModel`, `param_type=int8`, `activation_type=int16`,
518
  `quant_scheme=min_max`, calibrated on **real activations captured from chat-formatted decode
@@ -530,7 +691,7 @@ Both of those details matter and each caused a distinct on-device failure:
530
  matters more than outlier clipping, and `tf_enhanced` mis-estimated the range badly enough to
531
  inflate hidden norms ~10Γ—.
532
 
533
- ## Limitations
534
 
535
  - Fixed 4096 context.
536
  - Prefill is not included in this repo; the decode graph can prefill token-by-token, which is
 
12
  library_name: qai-hub
13
  ---
14
 
15
+ # Gemma-4-E2B-it β€” 8-bit and 4-bit quantization for the Qualcomm Hexagon NPU
16
 
17
+ `google/gemma-4-E2B-it` quantized two ways, both running on the Hexagon NPU:
 
18
 
19
+ - **8-bit quant** β€” int8 weights with int16 activations, compiled to **QNN context binaries**
20
+ that run **100% on the Hexagon NPU** with no CPU or GPU fallback. Needs the QAIRT SDK.
21
+ - **4-bit quant** β€” `q4_0` group-32 weights as a **GGUF executed on the NPU through llama.cpp's
22
+ `ggml-hexagon` backend**. No QAIRT SDK, smaller on disk, and faster on decode.
23
+
24
+ Most shipped mobile builds of this model use int8 activations. Keeping activations at 16 bits
25
+ costs some memory and holds onto accuracy: on a held-out 400-question MMLU slice the 8-bit
26
+ build is **statistically indistinguishable from the unquantized model**.
27
 
28
  > **Read the verification table before using a binary.** Not every target below has been run
29
+ > on physical hardware, and this README states exactly which have. As of 2026-07-27 the 8-bit
30
+ > **prefill** binary is verified on both v79 and v81 silicon; **v81 decode is not verified.**
31
 
32
  ---
33
 
34
+ ## 8-bit β€” verification status per target
35
 
36
  Everything below was measured on **physical hardware** β€” Snapdragon 8 Elite (SM8750, HTP v79)
37
  via Qualcomm Device Cloud, device `41c5710f`, 2026-07-26, and Snapdragon 8 Elite Gen 5
 
59
  **Prefill** β€” 12 held-out chat prompts, each one forward pass, compared against float:
60
  **12/12 (100%)** next-token top-1, hidden cosine mean 0.992 / min 0.986.
61
 
62
+ ### Speed ladder (v79, 8-bit, AI Hub profile on real hardware)
63
 
64
  | decode graph | ms/step | tok/s | speedup |
65
  |---|---|---|---|
 
94
  The single v81 disagreement is `"Name a planet with rings."`, where float opens `'**'` and the
95
  device opens `'The'` at cos 0.9805 β€” a near-tie between two plausible sentence openings, not a
96
  degradation. **Re-running that exact prompt on the v79 binary reproduced the identical
97
+ mismatch at an identical cosine of 0.9804982542991638**, so it is a property of the 8-bit
98
  quantization, not of v81. On this evidence the two architectures are numerically
99
  indistinguishable on the trunk.
100
 
 
134
 
135
  ---
136
 
137
+ ## 8-bit accuracy
138
 
139
  Held-out MMLU, 0-shot, chat-formatted, 400 questions disjoint from all calibration data:
140
 
141
  | | accuracy |
142
  |---|---|
143
  | base model (float, ≑ HF) | 56.75% Β± 2.48 |
144
+ | **8-bit (this build)** | **59.25% Β± 2.46** |
145
  | delta | +2.50 pp |
146
  | random baseline | 25.00% |
147
 
148
  The +2.50 pp delta is about one standard error β€” **not** evidence that quantization improves
149
+ the model. The correct reading is that 8-bit costs no measurable MMLU accuracy. Note the two
150
  models disagree on ~24% of individual questions; they match in aggregate, not per-question.
151
 
152
  ---
 
213
  ## Files
214
 
215
  ```
216
+ gemma4_decode_wgqa_int8kv_a16w8_v79.bin 1.9 GB 8-bit decode, v79 (recommended)
217
+ gemma4_decode_wgqa_a16w8_v79.bin 1.9 GB 8-bit decode, v79, no int8-KV
218
+ gemma4_trunk_a16w8_v79.bin 1.9 GB 8-bit prefill, v79, fixed SEQ=128
219
+ gemma4_decode_wgqa_int8kv_a16w8_v81.bin 1.9 GB 8-bit decode, v81 [UNVERIFIED - see above]
220
+ gemma4_trunk_a16w8_v81.bin 1.9 GB 8-bit prefill, v81 (verified on v81 silicon)
221
+
222
+ gemma4-e2b-w4.gguf 2.6 GB 4-bit, runs via ggml-hexagon
223
+ gemma4-e2b-w4-mtp.gguf 57 MB 4-bit MTP drafter (speculative decoding)
224
 
225
  host-model/embed_tokens_weight.bf16 769 MB token embeddings
226
  host-model/embed_tokens_per_layer_weight.bf16 4.4 GB per-layer embeddings
 
242
 
243
  ---
244
 
245
+ # How to run the 8-bit build
246
 
247
  ## What you need that is NOT in this repo
248
 
 
337
 
338
  ---
339
 
340
+ # 4-bit β€” q4_0 weights on the Hexagon NPU
341
 
342
+ The 4-bit path uses a different runtime. It is not a QNN context binary: it is a **GGUF executed
343
+ on the Hexagon NPU through llama.cpp's `ggml-hexagon` backend**, which drives the DSP directly
344
+ over FastRPC instead of through QNN. It needs no QAIRT SDK.
 
345
 
346
+ On the same physical v79 silicon it is **2.5Γ— faster on decode than the 8-bit build**, fits
347
+ entirely in one 2.44 GiB file with no host-side tensors, and costs ~0.4 pp of MMLU:
348
+
349
+ | | **4-bit / ggml-hexagon** | 8-bit / QNN |
350
  |---|---|---|
351
+ | model on disk | **2.44 GiB total** | 3.54 GiB + 5.2 GB host tensors |
352
+ | decode, sustained on device | **37.8 – 38.5 tok/s** | 15.3 tok/s |
353
+ | prefill, NPU | **1145 tok/s** | β€” |
354
+ | peak device RSS | **3.84 GiB** | not measured |
355
+ | held-out MMLU | **57.76% Β±0.99** | 59.25% Β±2.46 |
356
+ | vs unquantized base | **βˆ’0.44 pp** | +2.50 pp |
357
+ | GSM8K (5-shot, chat) | **0.5200 / 0.6467** | not measured |
358
+ | embeddings | **inside the file** | 5.2 GB on host |
359
+ | QAIRT SDK required | **no** | yes |
360
+
361
+ **Granularity, not bit-width, is what decides accuracy here.** This build is group-32; at the
362
+ same 4 bits a per-channel grid costs ~5.3 pp of MMLU, measured by quantizing the same parent
363
+ checkpoint both ways on the same harness:
364
+
365
+ | grid, same parent checkpoint | MMLU (n=2280) |
366
+ |---|---|
367
+ | unquantized parent | 57.68% Β±0.99 |
368
+ | group-32 (this build) | **57.76% Β±0.99** |
369
+ | per-channel int4 | 52.46% Β±1.01 |
370
 
371
+ Group-wise 4-bit was previously ruled out on this graph because LPBQ stores 4-bit values in an
372
+ int8 container and runs 9.3Γ— slower. **That conclusion still holds for QNN** β€” it does not apply
373
+ here, because `ggml-hexagon` consumes `q4_0` blocks natively with its own HTP kernels rather
374
+ than going through QNN's blockwise path.
375
 
376
+ ## 4-bit verification status
377
 
378
+ Measured on **physical Snapdragon 8 Elite (SM8750, HTP v79)** via Qualcomm Device Cloud, device
379
+ `87b3a4aa`, 2026-08-05. Every run started from 42 Β°C.
380
 
381
+ ### Throughput
382
 
383
+ | dev | prefill pp128 | decode tg32 |
384
  |---|---|---|
385
+ | **HTP0 (NPU)** | **1145.39 Β± 7.89 tok/s** | 28.50 Β± 0.06 tok/s |
386
+ | CPU (6 threads) | 225.17 Β± 0.46 tok/s | 42.48 Β± 0.08 tok/s |
387
+
388
+ **NPU prefill is 5.1Γ— CPU; CPU decode is 1.5Γ— NPU.** Decode is weight-bandwidth-bound, so the
389
+ NPU's compute advantage does not help it β€” prefill is compute-bound, where it does. A hybrid
390
+ placement (NPU prefill, CPU decode) is the strongest configuration on this hardware.
391
+
392
+ ### Sustained decode β€” and a measurement discrepancy you should know about
393
+
394
+ Five consecutive 128-token requests through `llama-server` on HTP0:
395
+
396
+ | run | tok/s | peak RSS | hottest thermal zone |
397
+ |---|---|---|---|
398
+ | 1 | 38.47 | 4,029,912 kB | 79.2 Β°C |
399
+ | 2 | 38.27 | 4,031,796 kB | 83.4 Β°C |
400
+ | 3 | 38.13 | 4,031,796 kB | 83.8 Β°C |
401
+ | 4 | 38.06 | 4,031,796 kB | 86.1 Β°C |
402
+ | 5 | 37.79 | 4,031,796 kB | 87.2 Β°C |
403
+
404
+
405
+ ### Latency and memory
406
+
407
+ - **TTFT 61.19 ms** for an 8-token prompt. Not comparable to context-512 TTFT figures.
408
+ - **Peak RSS 3.84 GiB** (`VmHWM` 4,031,796 kB) for a 2.44 GiB file β€” ~1.4 GiB of host-side
409
+ buffers on top of the weights.
410
+ - The `ggml-hex` log reports the DSP session mapping budget as `vmem 3355443200` =
411
+ **3.125 GiB**. RSS exceeds it, so not everything is resident in the session. A model whose
412
+ weights alone approach 3.1 GiB will need multi-device layer splitting.
413
+
414
+ ### Thermals
415
+
416
+ 42 Β°C β†’ **87.2 Β°C** peak across 5Γ—128-token bursts, with **1.8% decode decay**. Hot but not
417
+ throttling within that window. A multi-minute soak has **not** been run β€” do not assume this
418
+ holds for continuous generation.
419
 
420
+ ### Generation quality on device
421
 
422
+ Greedy, raw completion (no chat template):
423
+
424
+ | prompt | output |
425
+ |---|---|
426
+ | "The capital of France is" | " Paris." |
427
+ | "Who wrote Hamlet?" | "The answer is William Shakespeare." |
428
+ | "What color is grass?" | *(empty)* |
429
+ | "Name a country in South America." | **"Brazil"** |
430
+
431
+ The empty response on "What color is grass?" is a **prompt artifact, not a quantization
432
+ defect**: raw completion emits an immediate end-of-turn, and Google's own `q4_0` GGUF returns the
433
+ identical empty string on that prompt. Use the chat template.
434
+
435
+ ### Accuracy β€” measured on CPU, not on device
436
+
437
+ | benchmark | 4-bit | parent (unquantized) | base fp32 |
438
+ |---|---|---|---|
439
+ | MMLU, 0-shot, n=2280 | **57.76% Β±0.99** | 57.68% Β±0.99 | 58.20% Β±0.99 |
440
+ | GSM8K 5-shot chat, strict | **0.5200 Β±0.041** | 0.5200 Β±0.041 | β€” |
441
+ | GSM8K 5-shot chat, flexible | **0.6467 Β±0.039** | 0.6467 Β±0.039 | β€” |
442
+ | top-1 agreement vs parent | **100.00%** | β€” | β€” |
443
+ | KL divergence vs parent | **0.00007** | β€” | β€” |
444
+
445
+ GSM8K is **identical to the unquantized parent to the digit**, standard errors included. MMLU is
446
+ +0.08 pp β€” noise. Token-level agreement is exactly 100%, KL is 7Γ—10⁻⁡.
447
+
448
+ **How this was measured, and its limit:** the GGUF was dequantized and its tensors substituted
449
+ into the parent architecture, then evaluated with `lm-evaluation-harness` on CPU. That measures
450
+ the **weight grid**, which is what quantization changes β€” it does **not** exercise the HTP
451
+ kernels. On-device accuracy at scale is unverified; the four coherence prompts above are the only
452
+ device-side quality evidence.
453
+
454
+ Two harness details that otherwise waste time: MMLU needs `tokenizer.add_bos_token=True` or
455
+ loglikelihood-MC scores at chance; GSM8K needs `apply_chat_template=True` **and**
456
+ `add_bos_token` left off, because the template already emits `<bos>` and forcing it
457
+ double-prefixes. GSM8K run 0-shot raw scores `strict-match = 0.0` β€” the model never emits the
458
+ `#### N` format, so that is a broken measurement, not a bad model.
459
+
460
+ ### What is not verified
461
+
462
+ - **v81.** `libggml-htp-v81.so` builds and is auto-selected by arch, but has never been run β€”
463
+ Device Cloud provisions v79 only. Qualcomm's published figures for a `q4_0` GGUF on v81 show
464
+ NPU decode *lower* than v79 (17.61 vs 26.66 tok/s at ctx 512) with prefill 36% higher, so do
465
+ not assume v81 is an upgrade for decode.
466
+ - **Long context.** Nothing beyond depth 4096 exercised. The `tg32 @d4096` figure was itself
467
+ unstable across sessions (21.90 Β±0.97 vs 27.47 previously), so it is not quoted here.
468
+ - **Sustained multi-minute load**, batch > 1, and on-device accuracy at scale.
469
+
470
+ ## How to run the 4-bit build
471
+
472
+ ### What you need that is not in this repo
473
+
474
+ 1. **A recent llama.cpp** β€” Hexagon support and the `gemma4` architecture both require a build
475
+ from 2026-06 or later. Verified against upstream `0ef6e55`.
476
+ 2. **The snapdragon toolchain container** for the Android NDK and Hexagon SDK:
477
+ `ghcr.io/snapdragon-toolchain/arm64-android:v0.7` (NDK r28b, Hexagon SDK 6.6). **No QAIRT/QNN
478
+ SDK is needed for this path.**
479
+ 3. **A device**: SM8750 (v79) or SM8850 (v81) over `adb`.
480
+ 4. **~3 GB free on the device**, ~4 GB RAM headroom for the process.
481
+
482
+ ### Step 1 β€” build for Android + Hexagon
483
+
484
+ ```bash
485
+ git clone https://github.com/ggml-org/llama.cpp && cd llama.cpp
486
+ cp docs/backend/snapdragon/CMakeUserPresets.json .
487
+
488
+ docker run --rm -u $(id -u):$(id -g) -v $(pwd):/workspace --platform linux/amd64 \
489
+ ghcr.io/snapdragon-toolchain/arm64-android:v0.7 \
490
+ bash -lc 'cd /workspace \
491
+ && cmake --preset arm64-android-snapdragon-release -B build-snapdragon \
492
+ && cmake --build build-snapdragon -j 32 \
493
+ && cmake --install build-snapdragon --prefix pkg-snapdragon/llama.cpp'
494
  ```
495
+
496
+ This produces `libggml-hexagon.so` (host side) plus `libggml-htp-v73/v75/v79/v81.so` (DSP side).
497
+ The right HTP library is chosen automatically from the detected arch β€” unlike QNN, there is no
498
+ version pinning to get right. Confirm you got real DSP objects:
499
+
500
+ ```bash
501
+ file pkg-snapdragon/llama.cpp/lib/libggml-htp-v79.so
502
+ # ELF 32-bit LSB shared object, QUALCOMM DSP6, version 1 (SYSV)
503
  ```
504
 
505
+ ### Step 2 β€” push
506
 
507
+ ```bash
508
+ adb push pkg-snapdragon/llama.cpp /data/local/tmp/
509
+ adb shell mkdir -p /data/local/tmp/gguf
510
+ adb push gemma4-e2b-w4.gguf /data/local/tmp/gguf/
511
+ ```
512
 
513
+ Over a QDC tunnel expect 2.6–7.6 MB/s, so 10–20 min for the model. The *Slow adb* section below
514
+ applies here too.
 
 
515
 
516
+ ### Step 3 β€” the one non-obvious requirement
517
 
518
+ **`ADSP_LIBRARY_PATH` is mandatory.** Without it the DSP loader cannot find the skel, and the
519
+ error looks like a hardware or arch problem but is not:
520
 
521
+ ```
522
+ ggml-hex: failed to open session 0 : error 0x80000406
523
+ ```
 
524
 
525
+ ```bash
526
+ adb shell
527
+ cd /data/local/tmp/llama.cpp
528
+ export LD_LIBRARY_PATH=/data/local/tmp/llama.cpp/lib
529
+ export ADSP_LIBRARY_PATH="/data/local/tmp/llama.cpp/lib;/system/lib/rfsa/adsp;/vendor/lib/rfsa/adsp"
530
+ ```
531
 
532
+ A healthy session logs:
 
 
 
533
 
534
+ ```
535
+ ggml-hex: Hexagon Arch version v79
536
+ ggml-hex: HTP0 hwinfo: threads 6, hvx 6, hmx 1, vtcm 8 MB
537
+ ggml-hex: HTP0 new session : ... file:///libggml-htp-v79.so ... _dom=cdsp
538
+ ```
539
 
540
+ ### Step 4 β€” run
 
 
 
 
541
 
542
+ Benchmark NPU and CPU in one invocation:
 
 
 
 
543
 
544
+ ```bash
545
+ ./bin/llama-bench -m /data/local/tmp/gguf/gemma4-e2b-w4.gguf \
546
+ -p 128 -n 32 -t 6 -r 2 -fa 1 -dev HTP0,none
547
+ ```
548
 
549
+ Interactive generation on the NPU:
 
 
550
 
551
+ ```bash
552
+ ./bin/llama-cli -m /data/local/tmp/gguf/gemma4-e2b-w4.gguf -dev HTP0 -t 6 -c 4096 -fa on
553
+ ```
554
 
555
+ Server β€” the path the quoted decode figures come from:
556
 
557
+ ```bash
558
+ ./bin/llama-server -m /data/local/tmp/gguf/gemma4-e2b-w4.gguf \
559
+ -dev HTP0 -t 6 -c 4096 -fa on --parallel 1 --no-cont-batching --port 8099
560
+ ```
 
561
 
562
  ```bash
563
+ curl -s localhost:8099/completion -H 'Content-Type: application/json' \
564
+ -d '{"prompt":"Write a paragraph about on-device inference.","n_predict":128,"temperature":0}'
565
  ```
566
 
567
+ The `timings` block carries `predicted_per_second`, `prompt_ms` (TTFT) and, with a drafter
568
+ loaded, `draft_n` / `draft_n_accepted`.
 
569
 
570
+ | flag | why |
571
+ |---|---|
572
+ | `-dev HTP0` | run on the NPU. `-dev none` = CPU. `-dev HTP0,none` benchmarks both |
573
+ | `-fa on` | flash attention; required for the windowed-KV path |
574
+ | `-t 6` | matches the HTP's 6 HVX contexts |
575
+ | `--parallel 1 --no-cont-batching` | single-stream, matching the quoted figures |
576
+ | `-c 4096` | context; nothing beyond this is verified |
577
 
578
+ Quantized KV (`-ctk q8_0 -ctv q8_0`) is **not supported** on this backend β€” it aborts with
579
+ `GGML_ASSERT(offset == 0)` in `ggml-hexagon.cpp`. On CPU it works but is slower than `f16`
580
+ (25.66 vs 34.38 tok/s at depth 4096).
 
 
 
581
 
582
+ ### Step 5 β€” speculative decoding with the MTP drafter
583
 
584
+ `gemma4-e2b-w4-mtp.gguf` is a Multi-Token Prediction drafter (~57 MB) that shares the target's
585
+ KV cache. It needs a llama.cpp build newer than 2026-06-08 (arch `gemma4-assistant`).
586
 
587
+ ```bash
588
+ adb push gemma4-e2b-w4-mtp.gguf /data/local/tmp/gguf/
589
+ ```
590
+
591
+ ```bash
592
+ ./bin/llama-server -m /data/local/tmp/gguf/gemma4-e2b-w4.gguf \
593
+ -md /data/local/tmp/gguf/gemma4-e2b-w4-mtp.gguf \
594
+ --spec-type draft-mtp --spec-draft-n-max 4 \
595
+ -dev HTP0 -t 6 -c 4096 -fa on --port 8099
596
+ ```
597
+
598
+ The drafter cannot be instantiated standalone (`failed to create context with model` β€” it
599
+ requires the target's context), and the startup warning `[spec] failed to measure draft model
600
+ memory` is benign; the drafter loads afterwards and `common_speculative_init_result` confirms it.
601
+
602
+ ### 4-bit troubleshooting
603
+
604
+ | symptom | cause |
605
+ |---|---|
606
+ | `failed to open session 0 : error 0x80000406` | `ADSP_LIBRARY_PATH` not set β€” the DSP cannot find the skel |
607
+ | `GGML_ASSERT(offset == 0)` in `ggml-hexagon.cpp` | quantized KV cache (`-ctk`/`-ctv`) unsupported on HTP |
608
+ | `failed to create context with model 'gemma4-e2b-w4-mtp.gguf'` | the drafter needs the target's context; do not load it alone |
609
+ | `503 {"error":"Loading model"}` | server not ready. `/health` returns 503 while loading, so poll for HTTP 200, not just any response |
610
+ | decode slower on NPU than CPU | expected β€” decode is bandwidth-bound. Use the NPU for prefill |
611
+ | `llama-cli` idles at a `>` prompt | it entered conversation mode; use `llama-server`, or check this build's single-turn flag |
612
+ | arch `gemma4` / `gemma4-assistant` unknown | llama.cpp too old |
613
+
614
+ ## 4-bit limitations
615
+
616
+ - **Decode figure is unsettled** β€” 28.50 (llama-bench) vs ~38 tok/s (llama-server), cause
617
+ unresolved. See above.
618
+ - **On-device accuracy is unverified at scale.** MMLU/GSM8K were measured on CPU against the
619
+ weight grid, not through the HTP kernels.
620
+ - **v81 has never been run**, and Qualcomm's own v81 figures suggest decode is *worse* there.
621
+ - **No compiled artifact.** Unlike the 8-bit context binaries, there is no AOT compile
622
+ step β€” `ggml-hexagon` reads the GGUF at runtime. Converting this to a QNN context binary is
623
+ not a shortcut: QAIRT's `gguf_builder` dequantizes rather than preserving the group-32 grid,
624
+ which is the property doing the work.
625
+ - **Quantized KV cache unsupported** on the HTP backend.
626
+ - **Beyond 4096 context, batch > 1, and multi-minute sustained load** are all untested.
627
 
628
  ---
629
 
 
670
  | Fluent but wrong answer | binary/mask mismatch β€” the host `NEG` must be `-1e4`, matching calibration |
671
  | Garbage tokens, hidden norm β‰ˆ 0 | wrong context binary, or KV buffers not zeroed before position 0 |
672
  | Different output across identical runs | a KV buffer was corrupted mid-push; re-seed (`dd` on device) and retry |
673
+ | Fluent garbage from a binary you compiled yourself | local `qnn-context-binary-generator` emits `UFIXED_POINT_16` IO; the harness writes fp32. Use the AI Hub build, or check `graphInputs[].dataType` |
674
  | `Could not create context from binary` on a binary that used to work | QAIRT runtime version does not match the version that compiled it |
675
 
676
+ ## Reproducing the 8-bit quantization
677
 
678
  AIMET `QuantizationSimModel`, `param_type=int8`, `activation_type=int16`,
679
  `quant_scheme=min_max`, calibrated on **real activations captured from chat-formatted decode
 
691
  matters more than outlier clipping, and `tf_enhanced` mis-estimated the range badly enough to
692
  inflate hidden norms ~10Γ—.
693
 
694
+ ## 8-bit limitations
695
 
696
  - Fixed 4096 context.
697
  - Prefill is not included in this repo; the decode graph can prefill token-by-token, which is
SHA256SUMS CHANGED
@@ -1,12 +1,13 @@
 
1
  # v79 -- hardware verified on SM8750 (device 41c5710f, 2026-07-26)
2
  9e784f7dd2240dacaf721a7f82101730f614f0b96330643a4ad25225cdf2c722 gemma4_decode_wgqa_a16w8_v79.bin
3
  b92006525c92b154314072097299762a0df95ce01d3cc5e6d838ecc6215a500c gemma4_decode_wgqa_int8kv_a16w8_v79.bin
4
  10c044403c4d221ade9974cc242910c6359647385d8f91c625138e388a724373 gemma4_trunk_a16w8_v79.bin
5
 
6
- # v81 -- UNVERIFIED, see README. Published for investigation only.
7
  1d7d4b297bf9e67d74fdb41ec1dc04d14e297d22f923dd482dcaed8a83ec91bd gemma4_decode_wgqa_int8kv_a16w8_v81.bin
8
  c21f53e917cc4a2c51958fdebea5586bb91bc584f801ef181d69c982c643e27c gemma4_trunk_a16w8_v81.bin
9
- 633efc8a22a915341bd46653ad76b7d739062807014a46c60cd4ea00caed35a5 gemma4_decode_awq_pc_int4_v79.bin
10
- 08ba1cc27188367028702c5dd65f85b43ac5dfd85f4afd15f34de7e2ddf51843 gemma4_trunk_awq_pc_int4_v79.bin
11
- 55747f2e04a2fcd12b034b765fa74fdc5a11a415ded5221d40224048dc18afaa gemma4_decode_awq_pc_int4_v81.bin
12
- 0c85c6053f5c962140316288259bae2b03207922da867e3f913f0a9c2d0de1f6 gemma4_trunk_awq_pc_int4_v81.bin
 
1
+ # 8-bit (QNN context binaries)
2
  # v79 -- hardware verified on SM8750 (device 41c5710f, 2026-07-26)
3
  9e784f7dd2240dacaf721a7f82101730f614f0b96330643a4ad25225cdf2c722 gemma4_decode_wgqa_a16w8_v79.bin
4
  b92006525c92b154314072097299762a0df95ce01d3cc5e6d838ecc6215a500c gemma4_decode_wgqa_int8kv_a16w8_v79.bin
5
  10c044403c4d221ade9974cc242910c6359647385d8f91c625138e388a724373 gemma4_trunk_a16w8_v79.bin
6
 
7
+ # v81 -- decode UNVERIFIED, see README. Published for investigation only.
8
  1d7d4b297bf9e67d74fdb41ec1dc04d14e297d22f923dd482dcaed8a83ec91bd gemma4_decode_wgqa_int8kv_a16w8_v81.bin
9
  c21f53e917cc4a2c51958fdebea5586bb91bc584f801ef181d69c982c643e27c gemma4_trunk_a16w8_v81.bin
10
+
11
+ # 4-bit (GGUF, ggml-hexagon) -- v79 hardware verified on SM8750 (device 87b3a4aa, 2026-08-05)
12
+ e531007218dfab990486a5de7676a6932d6ea8dea233d1f698d7c21cf8a16889 gemma4-e2b-w4.gguf
13
+ 586f2460b909008640981ec34060aa864e03c144fbabfb3173c4335087e4aae0 gemma4-e2b-w4-mtp.gguf
gemma4_decode_awq_pc_int4_v79.bin β†’ gemma4-e2b-w4-mtp.gguf RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:633efc8a22a915341bd46653ad76b7d739062807014a46c60cd4ea00caed35a5
3
- size 964219720
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:586f2460b909008640981ec34060aa864e03c144fbabfb3173c4335087e4aae0
3
+ size 59235648
gemma4_decode_awq_pc_int4_v81.bin β†’ gemma4-e2b-w4.gguf RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:55747f2e04a2fcd12b034b765fa74fdc5a11a415ded5221d40224048dc18afaa
3
- size 965043016
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e531007218dfab990486a5de7676a6932d6ea8dea233d1f698d7c21cf8a16889
3
+ size 2620370976
gemma4_trunk_awq_pc_int4_v79.bin DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:08ba1cc27188367028702c5dd65f85b43ac5dfd85f4afd15f34de7e2ddf51843
3
- size 966970264
 
 
 
 
gemma4_trunk_awq_pc_int4_v81.bin DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:0c85c6053f5c962140316288259bae2b03207922da867e3f913f0a9c2d0de1f6
3
- size 973065112