wavewait / Adreno 640 / OpenCL

Two Q4 kernels and a stream kernel on a OnePlus 7T Pro

The GPU counters said the Q4 matrix-vector kernels were arithmetic bound: 82 to 87 % of the busy time in the ALU bucket, before and after every change. A load ablation said otherwise. Issuing the cached loads was the cost, and the bucket that holds them is the one the counters call alu. Sharing the activation loads across four rows cut the GEMV time in half, and the energy per run with it. The failed arms are on the same tables with the same weight.

Commits f383017 (kernels) and 9b126d8 (docs) · measured 2026-11-24 · OpenCL 2.0 driver E031.37.12.05 · Android 12 · no root

-50 %
GEMV 4096 x 4096 time, 1302.5 to 654.7 us (best of three rounds)
-40 %
energy per GEMV, 3.16 to 1.90 mJ, 60 s sustained
-52 %
Q4_0 14336 x 4096 time, 4715.1 to 2271.3 us
-24 %
stream 4 MB time, 357.7 to 271.5 us; energy not resolved

1. The question

Can the inner loop of a GEMV kernel on this phone run faster and cheaper, and can we say why? The kernels are the ones a decode step needs: a 4096 x 4096 Q4 matrix-vector product, a 14336 x 4096 Q4_0 product in llama.cpp's flat layout, and a 4 MB stream kernel as the memory reference. The baselines were our v6 GEMV, our Q4_0 kernel and a one-float-per-thread stream.

The answer has three parts. Time is measured as OpenCL event time with the output checked by checksum and an FNV-1a hash in every arm. The cause is measured with the KGSL counters. Energy is measured from the battery over 60 seconds. Each part has its own failure mode, and each is reported for every arm.

2. The phone, and what it exposes

The phone is a OnePlus 7T Pro on Android 12, with an Adreno 640 GPU and OpenCL 2.0. Unlike the Adreno 840 phone in the sibling lane, this one does give us GPU counters without root. The KGSL counter path works, and the cause shares below come from it. One route is closed, and it shapes the energy method:

routewhat we got
KGSL performance counters (through the harness, SP busy cycles and cause shares)works; the cause shares name a bucket, not a lever
/sys/class/power_supply/*closed to the shell user
PMIC thermal zones pm8150b-ibat-lvl0 (battery mA, positive = discharge, 78 mA steps) and pm8150b-vbat-lvl0 (mV)readable without root; this is the energy source

3. How energy was measured, and when a row counts

The battery current is the system power only if the input from the charger is constant. So the phone stayed on its 500 mA charger, and every row carries a receipt that checks it. A row counts only if three things hold: the charger voltage reads the same in the idle window and in the run window; the idle power sits near the port limit (about -1.5 W, negative because the battery is taking charge from the port); and the run is the full 60 seconds, with a few battery samples per second (about 4 to 5 Hz). The power is battery mA times mV in the run window minus the idle window before it. Energy per run is that power divided by the runs per second over the same window.

Why the rule exists. A battery at full charge tapers its charge, so the idle window reads too high and the deltas come out up to 1 W low. The first series of this lane was taken that way. Those rows are in the raw data and are not used. The sibling lane ran with the cable out. Here the cable stays in, and the pinned input is what makes the battery delta equal the system delta.

The noise is real. Repeats of one kernel differ by 0.2 to 0.35 W in the run window, and the battery steps are 78 mA. A difference under about 10 % in energy is not resolved. A scrcpy panel attached to the phone on some runs adds load; each receipt counts those processes, and the rows with the panel attached are marked.

4. Every lever, with its verdict

The first table is the landed set, with the counter cause for each. The second is the rejected set. Both are in the same units and the same race, so they compare with each other.

4.1 Landed

kernelleverus best, before -> afterSP busy cyclescause shares alu / mem / dep / idleverdict
GEMV 4096 x 4096, gemv_q4_v6.cl -> gemv_q4_v9.clA. four rows per 64-lane group share the x loads (8 loads of x per weight load down to 2); scalar accumulators; one barrier1302.5 -> 891.6 (-32 %)34.39 M -> 23.50 M87/5/3/0 -> 85/6/2/0kept
same kernel, -DU8 removedB. nibbles from 32-bit words: one shrm per nibble instead of shrm, shr, and, and891.6 -> 654.7 (-27 %)23.50 M -> 16.80 M85/6/2/0 -> 81/9/6/0kept
GEMV A + Bthe pick, gemv_best.a6xx.cl -> v91302.5 -> 654.7 (-50 %)34.39 M -> 16.80 M87/5/3/0 -> 81/9/6/0kept
Q4_0 14336 x 4096, q4_0_ours.cl -> q4_0_ours_x4.clA (same idea, four rows) then B4715.1 -> 2271.3 (-52 %)43.34 M -> 24.19 M82/8/5/0 -> 83/7/6/0kept
stream 4 MB, stream.cl -> stream_v2.clC. float4 per thread: a quarter of the threads, one 16-byte load and store each357.7 -> 271.5 (-24 %)2.16 M -> 1.97 M33/33/4/22 -> 7/87/2/0kept (no energy claim)

4.2 Rejected, with the same weight

leverresult (us best)referencereading
B alone on the one-row kernel (no x sharing)1343.51302.2+3 %; the x loads bound the kernel, nibbles alone do not help
one barrier instead of the 7-barrier local tree1339.81343.50 %; barriers are not the cost
sub_group_reduce_add (needs -cl-std=CL2.0)1756.41381.1+27 %; slower than local memory on this driver
R rows as a row loop without sharing x, R = 4 / 8 / 16 / 32 / 641419.6 / 1367.0 / 1623.5 / 2761.9 / 4465.31381.1no gain without sharing; more local memory per group cuts the groups in flight
2 rows per group779.3654.7+19 %; four rows is the pick
8 rows unrolled, 2 KB local1043.1655.3+59 %; 1379 instructions
row-by-row reduce in 256 B local, R = 4 / 8 / 16791.2 / 1190.5 / 3323.1665.2two barriers per row; +19 % already at R = 4
fold the -8 offset into one term per block834.6661.3+26 %; 16 more live registers
all weight and scale loads issued before the math916.2660.7+39 %; live registers
fp16 products; fp16 products with the block sum854.7; 674.5661.3+29 %; +2 %; no fp16 gain here
32-bit offsets; -cl-mad-enable; -cl-fast-relaxed-math680.7; 667.3; 675.6661.3nothing
weights as an rgba32ui image1d_buffer, one texel per block825.2654.7+26 %; the image path loses here
stream: local size 128 / 256, one float per thread297.7 / 293.0357.3helps, less than float4 (271.5)
stream: float16 per thread, local 128378.1264.9loses
Adreno 840 pick, q4_0_ours_r4.cl, on this phone18128.64717.53.8x slower; float acc[R] goes to private memory on this compiler

Two more arms are not in the race. Integer dot product (cl_qcom_dot_product8) and cl_qcom_reqd_sub_group_size are not on this driver, so no arm could be built with them. Flash attention tiles were not tested because they are not a GEMV or stream kernel.

5. The numbers as bars

Every bar is a row from the raw race or the sustained logs. The scale is the largest value in each chart, and the bars are not scaled across charts.

gemv_q4 v6 (baseline)1302.5 us v9, -DU8 (A)891.6 us v9, 2 rows (R=2)779.3 us v9 (A + B)654.7 us
GEMV 4096 x 4096, best of three interleaved rounds. Source: .work/op/race-final/best.txt. Checksum equal in all four arms.
q4_0_ours (baseline)4715.1 us q4_0_ours_x4 (A + B)2271.3 us
Q4_0 14336 x 4096, best of three rounds. Source: .work/op/race-final/best.txt.
stream (baseline)357.7 us stream_v2 (float4)271.5 us
Stream 4 MB, best of three rounds. Source: .work/op/race-final/best.txt.
gemv v63.16 gemv v91.90 gemv v9, hint low1.11 q4_0 ours10.07 q4_0 x46.76 stream, idle corrected0.90 stream_v20.83
Energy per run, 60 s sustained, from .work/op/sus2/all*.log. The stream baseline's idle window was not at the port limit, so its bar uses the mean valid idle (-1.56 W) and is marked as corrected; the raw value is 0.70 mJ. The 1.11 mJ bar is the low-clock hint: 0.42x the throughput at 2.4x the time.
gemv v657.3 C gemv v958.0 C gemv v9, hint low43.0 C q4_0 ours54.2 C q4_0 x462.6 C stream55.3 C stream_v254.2 C
GPU temperature maximum in the 60 s run. Bars are on one absolute scale of 0 to 70 C. No run throttled: the last ten seconds were within 1 % of the first ten, except x4 (-7.3 %, see section 8).

6. The v9 GEMV kernel, step by step

The kernel computes y = W x for a 4096 x 4096 matrix in Q4 blocks of 32 weights. Each block has one float scale and 16 bytes of nibbles. The v6 baseline gave each row its own 64-lane group, and every group loaded the same 32 activations of x for its own row. For one lane step that is 16 bytes of weights against 128 bytes of x, which is what the ablation in section 7 found.

  1. Lever A: four rows share one x load. One 64-lane group now covers four rows (R = 4). For each block, the group loads the x values once, splits them into the even and odd lanes once, and uses them for all four rows. The eight x loads per weight load fall to two.
  2. Scalar accumulators. Each row has its own float accumulator, a0 to a3. A float acc[R] array looks the same, but on this compiler it goes to memory, which is why the Adreno 840 pick is 3.8x slower here.
  3. Lever B: nibbles from 32-bit words. The 16 weight bytes are read as four 32-bit words. Each nibble is one shrm instruction. The byte-wise form (-DU8) costs a shift, a shift, an and and an and per nibble instead.
  4. One barrier. Each row's four partial sums go to local memory. A single barrier, then lane i reduces row i in the same order as the six-round tree of v6, so the output bytes are equal.
// examples/phone/gemv_q4_v9.cl, lines 1 to 9 (header, shortened)
// Q4 GEMV for Adreno 640, R rows per 64-lane group (-DR=2|4, default 4), bit-identical to gemv_q4_v6.cl:
// 1. x for a 32-weight block is loaded and split once and used by R rows (v6: 8 x loads per weight load).
// 2. nibbles from 32-bit words: (w >> 4m) & 15 is one `shrm`, the uchar16 form (-DU8) costs four ops.
// 3. one barrier, lane i reduces row i from local memory in the order of v6's 6-round tree.
// wwk args: buf:8 buf:2 buf:0.016 buf:0.016 i32:4096 local:1024 k=gemv g=65536 l=64

// the main loop (lines 33 to 40): one block per lane step, four rows
for (int b = 16 * l; b < K / 2; b += 1024) {
  float16 xe = vload16(0, x + 2 * b), xo = vload16(1, x + 2 * b);
  float16 xl = (float16)(xe.even, xo.even), xh = (float16)(xe.odd, xo.odd);
  ROW(0) ROW(1)
#if R > 2
  ROW(2) ROW(3)
#endif
}
red[l] = a0; red[64 + l] = a1;
red[128 + l] = a2; red[192 + l] = a3;
barrier(CLK_LOCAL_MEM_FENCE);

Every row macro, ROW(i), unpacks the 32 nibbles of its block with the 32-bit form, multiplies them by the shared x halves, reduces the 16 products to one float with adds, and accumulates that times the block scale with mad. Local memory stays at R x 256 bytes: eight rows cost more code and more local bytes per group, which is why R = 8 lost.

7. The load ablation that found the cause

The counters said alu 87 % for v6, and the listing said the kernel was 218 instructions long. Neither says what to change, and the alu share does not fall when the cost goes away. So the kernel was run with one load stream at a time replaced by a constant. The timing changes, the output does not matter, and the arm is marked as timing only.

arm (one-row kernel, R = 1)us bestchangewhat it shows
v9 with every load1353.5referenceone row per group, x loaded per row
v9 without the x loads (constant x)589.4-56 %the x loads were 56 % of the time

The two races compare against a common arm, v9 with R = 4, which reads 655.1 us in one and 655.3 us in the other. After levers A and B, the same ablation on the four-row kernel gives:

arm (R = 4, after A + B)us bestwhat is left
all loads655.3the pick
without the x loads595.3-9 %
without the scale loads623.8-5 %
without the x and the scale loads506.0the weight stream plus arithmetic
without the weight loads228.2arithmetic and the x and scale loads (other race, reference 661.3)

So the final kernel is an additive sum, not an overlap. The 506 us arm holds about 280 us of weight stream, 8 MB at about 29 GB/s, and about 230 us of arithmetic. The remaining 149 us of the full kernel are the x and scale loads. Every way we tried to overlap the weight stream with the arithmetic cost more in live registers than it gained (section 4.2). A floor of about 330 us, 10 MB at 30 GB/s, would be the bound if the two overlapped.

8. The Q4_0 kernel, and the Adreno 840 pick

The Q4_0 kernel reads llama.cpp's flat layout, 14336 rows by 4096, which is 3.5x the rows of the GEMV. Lever A alone gave 4717.5 to 3246.7 us (-31 %). Lever B on top gave 2302.5 us (-29 % on that), and the final pick measured 2271.3 us best in the final race (-52 % from the baseline). The Adreno 840 phone's pick, q4_0_ours_r4.cl, keeps a float acc[R] array, and on this phone that array goes to memory. The arm ran 18128.6 us against 4717.5 us, 3.8x slower.

The energy follows the time. Q4_0 ours draws 1.90 W above idle over 189 runs per second, 10.07 mJ per run. The x4 pick draws 2.28 W over 337 runs per second, 6.76 mJ per run: -33 % energy, +78 % throughput. Its GPU reached 62.6 C, the hottest valid run in this lane, and its last ten seconds ran 7.3 % slower than its first ten. The first series rose 6.9 % on the same kernel, so we read that as drift, not a trend.

9. The stream kernel, at the memory ceiling

The stream kernel reads and writes 4 MB once. Its counters changed from idle 22 % and mem 33 % to idle 0 % and mem 87 % with float4 per thread. A 4 MB read and a 4 MB write in 265 us is about 30 GB/s, which is what this bus gives. Widening to 8 or 16 floats per thread was within 5 % of float4 (277.1 and 264.9 us), so the bus, not the instruction count, sets the time. In energy, the baseline's idle window was not at the port limit, so the corrected comparison gives 0.90 to 0.83 mJ per run (-8 %). That is inside the repeat noise. We make no energy claim for this kernel.

10. What was not done

11. Provenance

device OnePlus 7T Pro (Adreno 640) os Android 12, no root gpu driver OpenCL 2.0, driver E031.37.12.05 device strings cl_khr_subgroups, cl_khr_fp16, cl_qcom_perf_hint, cl_qcom_subgroup_shuffle no cl_qcom_reqd_sub_group_size, no integer dot product method race: OpenCL event time, interleaved rounds, best of three (sustained: 60 s) counters: KGSL SP busy cycles per 20 runs (GEMV) or per 8 runs (Q4_0), 5 for stream energy: PMIC thermal zones, charger held constant, 4 Hz sampler repo wavewait, commits f383017 (kernels) and 9b126d8 (docs), 49f2c19 (hash and receipt) receipts .work/result-oneplus-kernel.md, .work/op/race-final, .work/op/sus2/all*.log

12. How to prove this wrong

On the phone with the 500 mA charger, run the v6 and v9 GEMV races (scripts/ww-phone-race, three rounds, then the same kernels with scripts/ww-phone-sustain for 60 s each). This page is wrong if v9 is not at least 30 % faster than v6 in the best-of-three time (measured here: 50 %), if the checksums differ (both must read -4153.16), or if the energy-per-run ratio is above 0.8 with both receipts valid (measured here: 0.60).