Checking how many agents are working

How this works
Kontakt
← Blog
A row of small olive dials with needles, one needle pointing to a long bar while a dark gap beside it glows faintly, on a near-black navy ground, the AMARBARO mark centred over it.
✎Blogbeitrag

Reading the counters on a graphics card

The short version

Sharing the activation loads across four rows cut a Q4 matrix-vector kernel on a OnePlus 7T Pro from 1302.5 to 654.7 microseconds and the energy per run from 3.16 to 1.90 millijoules, while the ALU counter share stayed between 81 and 87 percent.

What this is about

A graphics card keeps count of what it does. It counts cycles spent on math, on memory, on waiting. These counts are called performance counters. They look like an answer to "why is my kernel slow?"

They are a clue, not an answer. This article shows a case where the counters said one thing and the cause was another. The setting was a phone, not a desktop card, but the lesson is the same.

Why it matters

When a kernel is slow, you want to know what to change. If the counters say "math units busy", you might try to cut the math. If the real cost is loads, that effort is wasted. Reading counters well saves days.

What a counter is

Think of a factory floor with a log. Every minute the log says which station was busy: cutting, welding or moving parts. The log tells you where the minutes were charged. It does not tell you which job was the bottleneck. Two stations might both be "busy" because one is waiting on the other.

A card's counters work like that log. They say how many cycles went to each bucket, such as arithmetic, memory or idle.

The case: a phone kernel

We wanted a faster Q4 matrix-vector kernel for a phone GPU. The phone was a OnePlus 7T Pro with an Adreno 640 GPU. The kernel multiplies a 4096 by 4096 table of 4-bit weights by one vector, the shape a decode step needs.

We measured three things for every version, and the failed ones counted as much as the winners:

  • Speed: event time per run, with a checksum and a hash of the output to prove nothing changed.
  • Cause: the counters, which worked here without root.
  • Energy: battery current over a 60 second run.

What the counters said

For the starting kernel, the arithmetic bucket held 87 percent of busy time. After our first change it held 85 percent, and after the second 81 percent. The busy cycles fell by half. The share named the same bucket every time.

The reason: on this GPU, issuing a cached load is charged to the arithmetic bucket. So "arithmetic busy" included the loads. The counter said where cycles were charged. It did not say which instruction was the cost.

The experiment that told us

To find out, we ran the kernel with its loads of the vector replaced by a constant. The output changes on purpose, so that run is for timing only. The time fell from 1353.5 to 589.4 microseconds, a drop of 56 percent. The loads were the cost.

Each step of a lane read 16 bytes of weights and 128 bytes of the vector. So the kernel was limited by how many loads it issued, not by its math.

The fix

If four rows share one set of vector loads, the eight loads of the vector per weight load fall to two. We built a kernel where one group of 64 lanes covers four rows. For each block of 32 weights, it loads the vector once and reuses it for all four rows.

Two changes in total:

  1. Share the loads across four rows. 1302.5 to 891.6 microseconds, a drop of 32 percent.
  2. Pull the 4-bit values out of 32-bit words. 891.6 to 654.7, a further 27 percent.

Together that is a 50 percent drop, with the same output bytes. The checksum read -4153.16 in every version.

What failed

We measured the rejected ideas in the same race, so each compares to a reference beside it. A few, from our list:

  • Using a subgroup reduction: 1756.4 against 1381.1 microseconds, 27 percent worse.
  • Two rows per group: 779.3 against 654.7, 19 percent worse.
  • Eight rows with local memory: 1043.1 against 655.3, 59 percent worse.
  • Half precision products: 854.7 against 661.3, 29 percent worse.
  • Issuing all loads before the math: 916.2 against 660.7, 39 percent worse. The live registers cost more than the overlap gained.

Energy

Energy followed speed. The kernel drew about 2.2 to 2.4 watts above idle whatever the version, so a faster kernel used less energy per run. The starting kernel used 3.16 millijoules a run and the final one 1.90, a 40 percent drop. We took care with the rule for counting a row: the charger voltage must match between the idle and run windows, and the run must last the full 60 seconds. Our first series broke that rule, and we kept it out of the result.

What this does not show

One phone, one driver, one day. Another phone or a driver update may change the picture. Our energy numbers rest on battery current in steps of 78 milliamps, so differences under about 10 percent are not resolved. We did not test every idea: two were not possible on this driver.

The honest limit: counters are only as good as your knowledge of what each bucket includes. Always follow a counter with an experiment that changes one thing.

Words you may not know

  • Counter: a hardware count of cycles spent on one kind of work.
  • ALU: the arithmetic unit of the card.
  • Kernel: a small program that runs on the graphics card.
  • Load: reading a number from memory into a register.
  • Ablation: removing one part to see what it was worth.
  • Checksum: a short number computed from the output to prove it did not change.
  • Microsecond: one millionth of a second.
  • Lane: one worker inside a group that runs the same step together.
  • Register: the fastest, smallest store a worker has.
  • Local memory: a small shared store that a group of workers can use together.

Try it

For a desktop AMD card, wavewait reads the hardware thread trace and gives a per-instruction wait table with a cause on every row. It is for the gfx1100 family, such as the RX 7900 XTX, and not for the phone in this article. The code is on GitHub.

Our engine, mojo-baro, keeps its kernel measurements with their protocols. Questions about counters are welcome at gllm.forum.

The Counters Said ALU, and the Loads Were the Cost has the full list of ideas, the energy method and the falsifier.

Kommentare

Noch keine Kommentare.

Anmelden um zu kommentieren.