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.
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:
| route | result |
|---|---|
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_query | not 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:
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).
| lever | arm | us, 4096 | us, 1024 rows | us, 151936 rows | mJ 25 s | mJ 60 s | C after 60 s | verdict |
|---|---|---|---|---|---|---|---|---|
| baseline: local tree, 6 barriers | r4 | 160.8 | 43.6 | 5755 | 0.371 | 0.400 | 79.5 | reference |
| subgroup reduce, 8 rows per wave | s8 | 144.7 (-10 %) | 40.0 (-8 %) | 5115 (-11 %) | 0.362 | 0.385 | 85.3 | faster; energy within noise |
| block dot as packed int8 dots | d8 | 144.4 (-10 %) | 42.1 (-4 %) | 5117 (-11 %) | 0.310 (-16 %) | 0.335 (-16 %) | 70.5 | faster and lower energy; int8 caveat |
| products and block sums in fp16 | s8 -DH16 | 147.5 | 42.8 | 5092 | 0.344 | n/a | n/a | no gain: not ALU bound |
| local size 1024, rows by subgroup | s8 -DNSG | 155.1 (sustained) | n/a | n/a | 0.362 | n/a | n/a | about 8 % slower than local 64 |
| full-wave subgroup, local 128 | s8 -DW=128 | 141.8 (not valid) | 41.3 | n/a | n/a | n/a | n/a | invalid: checksum is half (11923 vs 23897.2); the reduce still covers 64 lanes |
-cl-fast-relaxed-math | s8 | 144.4 | 40.5 | n/a | n/a | n/a | n/a | 0 % |
| image weights (llama.cpp noshuffle) | ns | 139.2 (-13 %) | 72.0 (+65 %) | 8557 (+49 %) | 0.326 (one run) | 0.384 | 86.4 | wins only at 4096 x 4096; different output, see callout |
| perf hint normal | s8 / d8 | 144.5 / 144.4 | n/a | n/a | 0.344 / 0.313 | n/a | n/a | same as the default (high) |
| perf hint low | d8 | 691.5 (4.8x) | n/a | n/a | 0.187 (-40 % vs d8, -50 % vs r4) | n/a | 36 | lowest energy per GEMV, five times the time |
| perf hint low | s8 | 751.8 (5.2x) | n/a | n/a | 0.214 (-41 % vs s8) | n/a | 36 | energy for latency |
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.
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.
- 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 twouint4registers, shared by the eight rows. - 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.
- Unpack the weights as bytes. A Q4_0 block is 32 weights in 16 bytes.
lo = v & 0x0F0F0F0Fuandhi = (v >> 4) & 0x0F0F0F0Fugive the low and high nibbles as bytes, one 16-byte half each. - Eight integer dots per block.
D4is fourdot_4x8packed_us_intcalls, one per 4-byte group, so one call does four multiply-adds. TwoD4per row cover the block. - 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
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:
| builtin | input | result |
|---|---|---|
dot_4x8packed_us_int | unsigned x signed, 0xFF against 0xFF | -255 |
dot_4x8packed_ss_int | signed x signed, 0xFF against 0xFF | 1 |
dot_4x8packed_uu_uint | unsigned x unsigned, 0xFF against 0xFF | 65025 |
dot_acc_sat_4x8packed_us_int | 1+2+3-4 plus accumulator 100 | 102 |
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
- The shipped alias
q4_0_best.a8xx.clstill points at r4. s8 is a drop-in with the same output, but it takes a different launch shape (g=M/8*64), and the launch script is being edited by another lane. The switch waits for that commit. - Recordable queues and
cl_qcom_ml_opswere not tried. Both are the vendor routes to lower dispatch cost, which is the part of the energy that our method includes. - Energy per arm has at most two samples, and the phone was on battery for about half an hour in total. Only one usable energy sample exists for the llama.cpp arm.
- One phone, one driver, one day. The results may not hold on another Adreno 8xx device or after a driver update.
- int8 quality was not measured on real activations, so d8 is not yet usable in an engine.
- The two speed states are not explained.
12. Provenance
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.