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.
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:
| route | what 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.
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
| kernel | lever | us best, before -> after | SP busy cycles | cause shares alu / mem / dep / idle | verdict |
|---|---|---|---|---|---|
GEMV 4096 x 4096, gemv_q4_v6.cl -> gemv_q4_v9.cl | A. four rows per 64-lane group share the x loads (8 loads of x per weight load down to 2); scalar accumulators; one barrier | 1302.5 -> 891.6 (-32 %) | 34.39 M -> 23.50 M | 87/5/3/0 -> 85/6/2/0 | kept |
| same kernel, -DU8 removed | B. nibbles from 32-bit words: one shrm per nibble instead of shrm, shr, and, and | 891.6 -> 654.7 (-27 %) | 23.50 M -> 16.80 M | 85/6/2/0 -> 81/9/6/0 | kept |
| GEMV A + B | the pick, gemv_best.a6xx.cl -> v9 | 1302.5 -> 654.7 (-50 %) | 34.39 M -> 16.80 M | 87/5/3/0 -> 81/9/6/0 | kept |
Q4_0 14336 x 4096, q4_0_ours.cl -> q4_0_ours_x4.cl | A (same idea, four rows) then B | 4715.1 -> 2271.3 (-52 %) | 43.34 M -> 24.19 M | 82/8/5/0 -> 83/7/6/0 | kept |
stream 4 MB, stream.cl -> stream_v2.cl | C. float4 per thread: a quarter of the threads, one 16-byte load and store each | 357.7 -> 271.5 (-24 %) | 2.16 M -> 1.97 M | 33/33/4/22 -> 7/87/2/0 | kept (no energy claim) |
4.2 Rejected, with the same weight
| lever | result (us best) | reference | reading |
|---|---|---|---|
| B alone on the one-row kernel (no x sharing) | 1343.5 | 1302.2 | +3 %; the x loads bound the kernel, nibbles alone do not help |
| one barrier instead of the 7-barrier local tree | 1339.8 | 1343.5 | 0 %; barriers are not the cost |
sub_group_reduce_add (needs -cl-std=CL2.0) | 1756.4 | 1381.1 | +27 %; slower than local memory on this driver |
| R rows as a row loop without sharing x, R = 4 / 8 / 16 / 32 / 64 | 1419.6 / 1367.0 / 1623.5 / 2761.9 / 4465.3 | 1381.1 | no gain without sharing; more local memory per group cuts the groups in flight |
| 2 rows per group | 779.3 | 654.7 | +19 %; four rows is the pick |
| 8 rows unrolled, 2 KB local | 1043.1 | 655.3 | +59 %; 1379 instructions |
| row-by-row reduce in 256 B local, R = 4 / 8 / 16 | 791.2 / 1190.5 / 3323.1 | 665.2 | two barriers per row; +19 % already at R = 4 |
| fold the -8 offset into one term per block | 834.6 | 661.3 | +26 %; 16 more live registers |
| all weight and scale loads issued before the math | 916.2 | 660.7 | +39 %; live registers |
| fp16 products; fp16 products with the block sum | 854.7; 674.5 | 661.3 | +29 %; +2 %; no fp16 gain here |
32-bit offsets; -cl-mad-enable; -cl-fast-relaxed-math | 680.7; 667.3; 675.6 | 661.3 | nothing |
weights as an rgba32ui image1d_buffer, one texel per block | 825.2 | 654.7 | +26 %; the image path loses here |
| stream: local size 128 / 256, one float per thread | 297.7 / 293.0 | 357.3 | helps, less than float4 (271.5) |
| stream: float16 per thread, local 128 | 378.1 | 264.9 | loses |
Adreno 840 pick, q4_0_ours_r4.cl, on this phone | 18128.6 | 4717.5 | 3.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.
.work/op/race-final/best.txt. Checksum equal in all four arms..work/op/race-final/best.txt..work/op/race-final/best.txt..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.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.
- 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. - Scalar accumulators. Each row has its own float accumulator,
a0toa3. Afloat 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. - Lever B: nibbles from 32-bit words. The 16 weight bytes are read as four 32-bit words. Each nibble is one
shrminstruction. The byte-wise form (-DU8) costs a shift, a shift, an and and an and per nibble instead. - One barrier. Each row's four partial sums go to local memory. A single barrier, then lane
ireduces rowiin 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 best | change | what it shows |
|---|---|---|---|
| v9 with every load | 1353.5 | reference | one 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 best | what is left |
|---|---|---|
| all loads | 655.3 | the pick |
| without the x loads | 595.3 | -9 % |
| without the scale loads | 623.8 | -5 % |
| without the x and the scale loads | 506.0 | the weight stream plus arithmetic |
| without the weight loads | 228.2 | arithmetic 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
- No Qualcomm
cl_qcom_reqd_sub_group_sizeand no integer dot product: the driver does not expose them. Two levers from the research notes stayed untested for this reason. - The fp16 2x claim of the research notes is not confirmed for these kernels (+2 % to +29 % slower, section 4.2).
- Only one phone and one driver. The results may not hold on another Adreno 640 or after a driver update.
- The energy numbers rest on battery current in 78 mA steps. A bench supply on the USB input would tighten them. Differences under about 10 % in energy are not resolved.
- One stream baseline run in the second series had an idle window out of range (1.470 mJ, idle -0.62 W). It is excluded, and the baseline energy is the corrected value. The first series is also excluded (battery taper).
- Flash attention tiles were not tested.
11. Provenance
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).