The question
We wanted a faster Q4 matrix-vector kernel for a phone GPU, and we wanted to know why it was faster. The phone is a OnePlus 7T Pro with an Adreno 640 GPU, Android 12 and OpenCL 2.0. The kernel is the one a decode step needs: a 4096 by 4096 Q4 product. We also timed a Q4_0 product in llama.cpp's flat layout, 14336 by 4096, and a 4 MB stream kernel as a memory reference.
Speed and energy are separate questions, so we measured them separately. Speed is OpenCL event time per run, with a checksum and an FNV-1a hash of the output checked in every arm. Cause is the KGSL counters. Energy is battery current over a 60 second run. Every arm reports all three, and the failed arms carry the same weight as the winners.
The counters said the wrong thing
On this phone the counters work without root, which the sibling lane on the Xiaomi phone could not do. The counters gave the cause shares for each kernel. For the baseline GEMV the ALU bucket held 87 % of the busy time. After the first lever it still held 85 %, and after the second it held 81 %. The busy cycles fell by half, but the share named the same bucket.
The reason is that on this GPU issuing a cached load is charged to the ALU bucket. The counter tells us where the cycles are charged. It does not tell us which instruction is the cost. For that we needed an experiment.
The load ablation
We ran the one-row kernel, one row per 64-lane group, with the x loads replaced by a constant. The output changes on purpose, so this arm is for timing only. The time fell from 1353.5 to 589.4 microseconds, a 56 % drop. The x loads were the cost. Each lane step read 16 bytes of weights and 128 bytes of activations, so the kernel was bound by how many loads it issued, not by its arithmetic.
That pointed at the fix. If four rows share one set of x loads, the eight loads of x per weight load fall to two.
The kernel that worked
The kernel we kept, gemv_q4_v9.cl, makes two changes.
The first is that four rows share the activation loads. One 64-lane group covers four rows. For each 32-weight block the group loads x once, splits it into its even and odd halves once, and reuses it for all four rows. Each row keeps a scalar accumulator. We tried a float array for the four accumulators, and on this compiler the array goes to memory, so the scalars are part of the lever, not a detail.

The second is that the nibbles come out of 32-bit words. Each nibble is one shift instruction. The byte-wise form needs a shift, a shift, an and and an and for each nibble, and the 32-bit form does not.
The reduction ends with one barrier. Lane i reduces row i from local memory in the same order as the old six-round tree, so the output bytes match the baseline exactly. The checksum is -4153.16 in every arm, and the hash is the same.
On its own, the first change gave 1302.5 to 891.6 microseconds, a 32 % drop. The second gave 891.6 to 654.7, a further 27 %. Together they give a 50 % drop. The Q4_0 kernel took the same two steps and went from 4715.1 to 2271.3 microseconds, a 52 % drop.
What failed, with the numbers
Every rejected lever was measured in the same race as the pick, so each number compares with the reference beside it. The list below is abbreviated; the full set is in the repository.
- Nibbles from words on the one-row kernel alone: 1343.5 against 1302.2 microseconds, +3 %. Without the shared loads, the nibble change does not help.
- One barrier instead of the seven-barrier tree: 1339.8 against 1343.5, no change. Barriers were not the cost.
- The subgroup reduction: 1756.4 against 1381.1, +27 %. Local memory was faster on this driver.
- A row loop without shared x, for 4, 8, 16, 32 and 64 rows: 1419.6, 1367.0, 1623.5, 2761.9 and 4465.3 microseconds, against 1381.1.
- Two rows per group: 779.3 against 654.7, +19 %.
- Eight rows unrolled with 2 KB of local memory: 1043.1 against 655.3, +59 %.
- Folding the offset into one term per block: 834.6 against 661.3, +26 %.
- Issuing all loads before the math: 916.2 against 660.7, +39 %. The live registers cost more than the overlap gained.
- Half precision products: 854.7 against 661.3, +29 %. Half precision and a half block sum: 674.5, +2 %.
- 32-bit offsets,
-cl-mad-enableand fast relaxed math: 680.7, 667.3 and 675.6 against 661.3, each within 3 % of the reference, so no gain. - Weights as an rgba32ui image buffer: 825.2 against 654.7, +26 %.
Two more levers were not testable. The driver does not expose the Qualcomm subgroup-size extension or an integer dot product, so no arm could be built with them.
The Adreno 840 phone's best Q4_0 kernel, q4_0_ours_r4.cl, is the other example that matters here. On this phone it ran 18128.6 microseconds against 4717.5, 3.8x slower, because of the private array.
The final GEMV, and what is left
Removing the weight loads leaves 228.2 microseconds, which is the arithmetic and the x and scale loads together. Removing the x and scale loads leaves 506.0 microseconds. From that we read the final kernel as about 280 microseconds of weight stream, 8 MB at about 29 GB/s, plus about 230 microseconds of arithmetic. The full kernel takes 655.3 microseconds, so the x and scale loads add the remaining 149 microseconds.
The weight stream and the arithmetic do not overlap. Every attempt to overlap them cost more in registers than it gained. If they did overlap, the bound would be about 330 microseconds, 10 MB at 30 GB/s.
Energy, and the rule that decides a row
The energy method is different from the sibling lane's. Here the cable stays in. The phone sits on its 500 mA charger, and the battery current is read from the PMIC thermal zones in milliamps and millivolts, because the power supply nodes are closed to the shell user.
The battery delta equals the system power only if the input is constant. So a row counts only when three things hold. The charger voltage is the same in the idle window and the run window. The idle power sits near the port limit, about -1.5 W, which says the input is already at its ceiling. And the run lasts the full 60 seconds.
A battery at full charge tapers, and then the idle window reads too high. Our first series was taken that way, and its deltas came out up to 1 W low. We kept those rows in the raw data and left them out of the result.
With the rule applied, the energy per run falls with the time. The GEMV draws about 2.2 to 2.4 W above idle whatever the kernel, so the energy follows the speed. The baseline was 3.16 millijoules per run and the pick is 1.90, a 40 % drop. Over the 60 seconds the baseline ran 705 times per second and the pick 1217, so throughput rose 73 %.
The low-clock hint is the one lever that saves energy by slowing down. At the low hint the GEMV ran 509 times per second and used 1.11 millijoules per run. That is 42 % less energy than the pick, at 0.42 times the throughput.
The phone's GPU reached 58.0 C under the pick and 43.0 C under the low hint. No run throttled in its 60 seconds, except the Q4_0 pick, which ended 7.3 % slower than it started. Its first series rose by 6.9 % on the same kernel, so we read it as drift.
The stream kernel
The stream kernel reads and writes 4 MB. With one float per thread it took 357.7 microseconds. With float4 per thread it took 271.5, a 24 % drop. Its counters moved from 22 % idle and 33 % memory to 0 % idle and 87 % memory. A 4 MB read and a 4 MB write in 265 microseconds is about 30 GB/s, and widening to 8 or 16 floats per thread did not change that. The bus sets the time.

On energy we make no claim. The baseline's idle window was not at the port limit, so its corrected energy is 0.90 millijoules per run against 0.83 for the new kernel. That difference is inside the repeat noise.
Limits
- One phone, one driver, one day. The results may not hold on another Adreno 640 or after a driver update.
- The energy numbers rest on battery current in steps of 78 mA. A bench supply on the input would tighten them. Differences under about 10 % in energy are not resolved.
- The stream baseline's energy needs a correction, and we say so in the table. One more stream baseline run had its idle window out of range and is excluded.
- The fp16 speedup promised by the research notes did not appear for these kernels.
- Integer dot product and the Qualcomm subgroup-size extension are absent on this driver, so two levers stayed untested.
- We did not test flash-attention tiles.
Falsifier
On the phone, with the 500 mA charger, run the v6 and v9 GEMV races and then sixty-second sustained runs of each. This writeup is wrong if v9 is not at least 30 % faster than v6 in the best-of-three time, which we measured at 50 %. It is also wrong if the checksums differ, since both must read -4153.16, or if the energy-per-run ratio is above 0.8 with both receipts valid. We measured 0.60.
Receipts: the kernels, picks and scripts are in commit f383017 of the wavewait repository, the cause and ablation notes in 9b126d8, and the output hash and in-run charger receipt in 49f2c19.
The companion post on the Xiaomi 17 Ultra, a phone GPU with no counters, covers the same method on a phone that gives us no counters at all.

Commenti
Ancora nessun commento.
Accedi per commentare.