wavewait / Adreno 840 / OpenCL

A kernel judged without counters

On the Xiaomi 17 Ultra the GPU gives us no performance counters and no power readings while the cable is in. So we judged a Q4_0 matrix-vector kernel by three numbers only: GPU event time, a checksum, and battery energy with the cable out. Two levers were faster: subgroup reductions, and an integer dot product that was also lower in energy. Most others were not. This page shows all of them, with the failed arms at the same weight as the one that won.

Commit 01e78ef · measured 2026-11-24 · driver OpenCL 3.0 QUALCOMM build 0842.44.1 · Android 17

-10 %
GEMV time, 4096 x 4096, d8 and s8 vs r4 (144.4 / 144.7 vs 160.8 us)
-16 %
energy per GEMV, d8 vs r4, 25 s run (0.310 vs 0.371 mJ)
9 C
cooler after 60 s, d8 (70.5 C) than r4 (79.5 C)
0
shader counters readable on this phone without root

1. The question

Does a change to the inner loop of a GEMV kernel make a phone's GPU faster and cheaper to run, and can we tell? The baseline was q4_0_ours_r4.cl, a local-memory tree with six barriers. The candidates were subgroup reductions, a packed integer dot product, image-backed weights, and a few build options. The shapes were the ones that matter for a decode step: 4096 x 4096, 1024 x 4096 and 151936 x 4096 (the output head).

The answer has two parts. Speed is measured directly. Energy is measured from the battery, and only with the cable out. Both are reported for every arm.

2. The phone, and why it has no GPU counters

The phone is a Xiaomi 17 Ultra with a Snapdragon 8 Elite class SoC (SM8850) and an Adreno 840 GPU, on Android 17. Without root, the usual routes to hardware counters are all closed on this device:

routeresult
KGSL performance counters (kgsl-3d0)read returns EPERM (the driver flag perfcounter is 0). Turning the flag on power-cycles the GPU, so we do not touch it.
VK_KHR_performance_querynot offered by the driver
power monitor rails (dumpsys powerstats lists a gpu channel)0 uWs on every rail, idle and under load
/sys/class/power_supply/battery/*permission denied for the shell
PMIC thermal zones (pmih010x-ibat-lvl0 current, vbat voltage)readable, but only useful with the cable out (next section)

So a kernel change is judged by GPU event time, by a checksum that every arm must match, and by the energy the whole phone draws while the kernel runs. The energy number contains the GPU, the memory traffic and the CPU thread that enqueues work. It is not a GPU-only number.

3. How energy was measured

The charger hides the load. On USB, the battery current reads about 0.65 W for the stream kernel, and a duty sweep (25, 50, 75, 100 %) gives 0.07, 0.17, 0.54 and 0.65 W with no knee. The charger simply feeds the phone. With the cable out and the phone on its battery (dumpsys battery status 3, wireless adb), the same zones read an idle of about 0.14 W and 1.42 W over idle for the stream kernel.

The method for each arm:

1. wait until the GPU is below 34 C, then 6 s idle, sampling the battery current and voltage at 5 Hz 2. run the kernel for 25 s or 60 s 3. dW = battery power during the run minus the idle power 4. mJ per op = dW / (enqueues per second)

Enqueues per second are taken from the first and the last 10 s of the same run. Run minus idle is therefore the whole phone's extra draw for that kernel. Two runs of one kernel differed by 1 % (d8) and 5 % (s8). We treat an energy difference under 5 % as no result.

The timing is separate. Each arm is run as a warm, interleaved set: the first second of every batch is dropped, and the best per-second GPU event time is kept, best of five rounds. A timing batch that starts right after the OpenCL build sees idle GPU clocks: the s8 arm took 173 us cold and 143 us after one second of work. That is why the first second is dropped.

4. Every lever, with its verdict

Baseline = q4_0_ours_r4.cl. Times are us per GEMV at 4096 x 4096, best of five warm rounds. Energy is mJ per GEMV from the battery. Checksums matched (23897.2) for every arm of the shape except the llama.cpp arm, which computes a different output (see the callout).

leverarmus, 4096us, 1024 rowsus, 151936 rowsmJ 25 smJ 60 sC after 60 sverdict
baseline: local tree, 6 barriersr4160.843.657550.3710.40079.5reference
subgroup reduce, 8 rows per waves8144.7 (-10 %)40.0 (-8 %)5115 (-11 %)0.3620.38585.3faster; energy within noise
block dot as packed int8 dotsd8144.4 (-10 %)42.1 (-4 %)5117 (-11 %)0.310 (-16 %)0.335 (-16 %)70.5faster and lower energy; int8 caveat
products and block sums in fp16s8 -DH16147.542.850920.344n/an/ano gain: not ALU bound
local size 1024, rows by subgroups8 -DNSG155.1 (sustained)n/an/a0.362n/an/aabout 8 % slower than local 64
full-wave subgroup, local 128s8 -DW=128141.8 (not valid)41.3n/an/an/an/ainvalid: checksum is half (11923 vs 23897.2); the reduce still covers 64 lanes
-cl-fast-relaxed-maths8144.440.5n/an/an/an/a0 %
image weights (llama.cpp noshuffle)ns139.2 (-13 %)72.0 (+65 %)8557 (+49 %)0.326 (one run)0.38486.4wins only at 4096 x 4096; different output, see callout
perf hint normals8 / d8144.5 / 144.4n/an/a0.344 / 0.313n/an/asame as the default (high)
perf hint lowd8691.5 (4.8x)n/an/a0.187 (-40 % vs d8, -50 % vs r4)n/a36lowest energy per GEMV, five times the time
perf hint lows8751.8 (5.2x)n/an/a0.214 (-41 % vs s8)n/a36energy for latency

W over idle, 60 s run: r4 2.33, s8 2.46, d8 2.18, ns 2.57. No arm throttled within 60 s: the last 10 s ran at 0.996 to 1.026 of the first 10 s.

The llama.cpp arm does not compute the same output. Its checksum differs from the others (-5116.83 at 4096 rows, against 23897.2), so its timing and energy are not a like-for-like comparison. It is a useful reference for shape dependence: it wins at 4096 x 4096 and loses at the two other shapes. We report it, and we do not claim it as a faster correct kernel.
One energy sample is missing from the result. A fourth 25 s run of the llama.cpp arm gave 0.372 mJ per GEMV against 0.326 in the reported run. Its idle baseline read -1.1 W, which is not physical, so that run is excluded. The llama.cpp energy figure therefore rests on one usable sample, and a 14 % spread is unexplained.

5. The numbers as bars

Each bar is computed from the raw rows in the result, not from memory. The winning arm is in green.

r4 baseline160.8 s8 subgroup144.7 d8 int dot144.4 s8 fp16 (H16)147.5 s8 local 1024155.1 ns llama.cpp139.2* us per GEMV, lower is better
GEMV 4096 x 4096, us per enqueue, best of five warm rounds. The s8 local 1024 bar is the sustained figure. * different output (see callout).
r4 baseline0.400 s8 subgroup0.385 d8 int dot0.335 ns llama.cpp0.384* d8, perf hint low0.187 (25 s) s8, perf hint low0.214 (25 s) mJ per GEMV from the battery, cable out
Energy per GEMV, 60 s run, except the two hint rows (25 s). * one usable sample (see callout).
r4 baseline79.5 C s8 subgroup85.3 C d8 int dot70.5 C ns llama.cpp86.4 C d8, perf hint low36 C GPU C after 60 s of GEMV
GPU temperature at the end of the 60 s run. The d8 arm ends 9 C cooler than r4.
stream, 1 float1124.5 local 10241119.4 4 floats1128.6 16 floats1144.7 16 floats, local 10241192.4 us per enqueue, 32 MiB in place, slow state
Stream kernel, timing best of five. No variant is faster. Two speed states exist (section 8).

6. The d8 kernel, step by step

d8 keeps the structure of s8: each lane takes blocks, shares the activation vector across eight rows, and finishes with one subgroup reduction per row. The change is in the block dot product. Instead of converting nibbles to float and multiplying, d8 quantizes the activation block to int8 once, and does eight packed 4 x 8 integer dots per block.

  1. Quantize x once per block and lane. The 32 floats of the block become 32 int8 values with a scale of 64 (x * XS, saturating, round to nearest even). They are kept in two uint4 registers, shared by the eight rows.
  2. Precompute the offset term. Q4_0 stores nibbles n in 0..15 with an offset of 8. The sum of (n - 8) x x equals (sum of n x xq minus 8 times the sum of xq) divided by XS. The sum of xq is computed once per block with the dot builtin against a vector of ones, so the offset costs no extra loop.
  3. Unpack the weights as bytes. A Q4_0 block is 32 weights in 16 bytes. lo = v & 0x0F0F0F0Fu and hi = (v >> 4) & 0x0F0F0F0Fu give the low and high nibbles as bytes, one 16-byte half each.
  4. Eight integer dots per block. D4 is four dot_4x8packed_us_int calls, one per 4-byte group, so one call does four multiply-adds. Two D4 per row cover the block.
  5. One conversion per block. The integer sum is turned into float once, with the block scale and 1/XS, and added to the row accumulator with mad.
// examples/phone/q4_0_ours_d8.cl, lines 26 to 35 of the file (indentation of the nested loop shortened)
for (int b = l; b < nb; b += 64) {
  uint4 xl = as_uint4(convert_char16_sat_rte(vload16(0, x + 32 * b) * XS)), xh = as_uint4(convert_char16_sat_rte(vload16(1, x + 32 * b) * XS));
  int sx = 8 * (D4(one, xl) + D4(one, xh));
  for (int i = 0; i < R; i++) {
    size_t blk = (size_t)(r0 + i) * nb + b;
    uint4 v = vload4(0, (__global const uint *)(q + 16 * blk));
    uint4 lo = v & 0x0F0F0F0Fu, hi = (v >> 4) & 0x0F0F0F0Fu;
    acc[i] = mad((float)(D4(lo, xl) + D4(hi, xh) - sx), vload_half(0, d + blk) * (1.0f / XS), acc[i]);
  }
}

The reduction at the end is the same as s8: sub_group_reduce_add per row, no local memory and no barrier. The kernel compiles with qcom_reqd_sub_group_size("half"), which pins the subgroup to 64 lanes.

7. The int8 caveat

d8 changes the output on real activations. It quantizes x to int8. The harness fills x with values where x times 64 is an integer in the int8 range, so quantization is exact and the checksum matches (23897.2). On real activations the rounding is not exact, so the output changes. An engine would need its own quality gate before using d8, and a caller that already holds int8 activations saves the conversion inside the loop, which we did not measure.

8. The qcom_dot8_acc build failure, and the builtins that work

Qualcomm exposes a packed 8-bit dot with saturating accumulate as qcom_dot8_acc. We wrote a probe (examples/phone/dot8_probe.cl). The frontend accepts the extension with -cl-std=CL3.0, but the build fails with error CL -6 on driver 0842.44.1. So this route is not usable on this phone.

The Khronos packed builtins build and match their spec on the same device:

builtininputresult
dot_4x8packed_us_intunsigned x signed, 0xFF against 0xFF-255
dot_4x8packed_ss_intsigned x signed, 0xFF against 0xFF1
dot_4x8packed_uu_uintunsigned x unsigned, 0xFF against 0xFF65025
dot_acc_sat_4x8packed_us_int1+2+3-4 plus accumulator 100102

d8 uses only the first builtin, the mixed unsigned by signed form, which is the one the probe checks against the spec.

9. The stream kernel is at the ceiling, or above it

The stream kernel reads and writes 32 MiB in place, once per enqueue. Wider loads (4 or 16 floats per thread) and a local size of 1024 are within 2 % of it, or slower. No stream variant is faster.

The bandwidth figure needs care. Taking the buffer as built, 32 MiB read and 32 MiB written in 941 us is 71.3 GB/s. Taking 32 MB as 32 million bytes, it is 68.0 GB/s. The ceiling we compare against, 65.5 GB/s, comes from a read-only pattern (8 MiB in 128 us). A read-plus-write kernel at 71 GB/s is therefore not proof that it sits at a read ceiling. The honest statement is: the stream kernel is at the bandwidth this phone delivers for this pattern. A read-only pass over the same 32 MiB would settle it.

Over 60 s the stream kernel ran at 822 enqueues per second, 1.41 W over idle, and the GPU reached 54.3 C.

10. Two speed states we cannot explain

The stream kernel ran at two different speeds for the same code. In the first runs, on the cable and right after the cable came out, one enqueue took 941 to 947 us. About ten minutes later, on battery and back on the cable, every run took 1124 to 1137 us, at the same 1.41 W. A third set, a cold timing batch, read 1098 us. We do not know what switches the state. The GEMV did not move: it ran at 143 to 145 us throughout.

What this means for the table: the timing rows in section 4 and 9 were taken together in one interleaved set in the slow state, so they compare with each other. The energy rows are tagged by state. Two 25 s stream energy numbers exist: 1.450 mJ per enqueue in the fast state, and 1.723 mJ in the slow state from a hint-normal run. The stream energy in the result (1.450 mJ) is the fast-state number. The state of the hint-low stream run (0.976 mJ) is unknown: against the slow state's 1.712 mJ it would be 43 % lower, against the fast state's 1.450 mJ it is 33 % lower.

11. What was not done

12. Provenance

device Xiaomi 17 Ultra (Snapdragon 8 Elite class, SM8850) gpu Adreno 840 driver OpenCL 3.0 QUALCOMM build: 0842.44.1 Compiler E031.50.19.29 os Android 17, no root method ww-phone-ab (timing: 4 s warm batches, best of 5 rounds) battery energy: cable out, dumpsys battery status 3, 5 Hz PMIC zones repo wavewait, commit 01e78ef receipts .work/result-xiaomi-kernel.md, .work/xk/ (raw rows)

13. How to prove this wrong

Build examples/phone/q4_0_ours_r4.cl and examples/phone/q4_0_ours_d8.cl on an attached Xiaomi 17 Ultra with the cable out. Run scripts/ww-phone-ab OUT ARMS 5 for timing and scripts/ww-phone-ab OUT ARMS 0 60 for energy. This page is wrong if, in the 60 s energy run, d8 is not at least 5 % lower in mJ per GEMV than r4, or if the d8 checksum on the harness fill at 4096 x 4096 is not 23897.2.