Checking how many agents are working

How this works
Contact
← Blog
A steel stopwatch laid on a dark bench with a thin teal beam of light crossing one section of a brass gear train, one gear lit warm amber, the AMARBARO mark and wordmark centered over it.
✎post de blog

Finding the slow part of a program

The short version

Measuring where time goes before changing code finds fixes that guessing misses, and the cheapest test of a hypothesis often removes the need for a big build.

Part 9 in Measuring things honestly. Earlier parts include what a benchmark is.

What this is about

A slow program has a slow part. Often one part takes most of the time, and the rest is small. If you speed up the wrong part, you gain nothing.

Profiling is how you find the right part. You run the program with a stopwatch on every piece, and you read the list. This article shows how, using a real case where the answer was not what we first thought.

Why it matters

Our instincts about speed are poor. We guess that the maths is slow, or that the disk is slow, and we start work. Hours later we have a faster piece of code that makes no difference to the whole.

A profile costs minutes. It tells you where to spend your hours.

How it works

Step 1: Time the whole thing

Run the job, and write down the total time and what you ran. We timed four clips of a video model. Each made 81 frames at 480 by 832 pixels. A warm clip took about four minutes, and the sampling step was 78 to 80 percent of it.

Step 2: Break the time into parts

Use a profiler, a tool that lists where time went. For the video model we used torch.profiler over one step. The top lines:

Part Share of the step
Attention (one routine) 74.9 percent
Matrix multiplication 23.6 percent
Small pieces together 1.6 percent
A weight-unpacking step 0.09 percent

The last line killed an idea at no cost. We had planned to fuse the unpacking into the matrix multiplication. It took 0.09 percent of the step, so there was nothing to win.

Step 3: Admit when your first answer is wrong

We first worked out that the card could give at most 1.6 times more speed. We had written down a peak of 61 TFLOPS for the card. That was wrong. The real peak is about 123 TFLOPS, and attention was three quarters of the step, not half. We said so in our notes and corrected them. A profile helps most when you let it overrule you.

Step 4: Find the cheapest test

Attention was running at about 20 percent of the card's peak, while the matrix multiplication ran at 67 percent. It looked like a job for a new, hand-written kernel.

Before building anything, we ran the same attention call on the same data in two ways. ComfyUI passes its data to attention in non-adjacent slices, not one continuous block. We made a copy that is continuous first:

  • Data as passed (strided): 1701 ms per call.
  • The same data made continuous: 278.5 ms per call.

The attention routine was never slow. It was being fed data in a layout it handles badly. Think of reading a book where the pages of each chapter are scattered through the shelf, against reading it in order.

Step 5: Make the small fix

A small custom node that makes the data continuous before the call costs 299.7 ms per call including the copies. On a full clip, sampling went from 226.7 seconds to 122.6 seconds, 1.85 times faster. The pictures matched the stock run to within the difference between two stock runs.

Step 6: Check the big idea against the small fix

A new kernel now had to beat 278.5 ms. We tried one in Mojo and got 294 ms against 279 ms for the built-in. A tie is not a win. We published it as a negative result and did not build more.

The same method on CPU code

For CPU programs, our tool corewait goes a step further than a list of busy functions. It records hardware counters while the program runs, joins them to the machine instructions, and gives each instruction a cause: memory, branch, dependency, front end or busy.

In one worked case from ComfyUI, one routine in a library took 98.2 percent of the cycles inside that library, which was 23.5 percent of the run. A second finding was larger: a spin loop in the maths library (OpenBLAS), waiting for work, took 58 percent of the run's cycles. Setting one environment variable, OPENBLAS_NUM_THREADS=1, removes it. This was measured on an offline workload, not on a live server.

The rules

  1. Measure before you change.
  2. Look at the biggest share first.
  3. Test the cheapest hypothesis before the expensive build.
  4. Correct your notes when the data disagrees.
  5. Say when your own fix ties.

Glossary

  • Profiler: a tool that shows where a program spends its time.
  • Attention: the part of a language or video model that relates every piece of the input to every other.
  • Memory layout: how data is arranged in memory.
  • Strided: data spread in steps, with gaps, and not packed side by side.
  • Contiguous: data packed side by side.
  • TFLOPS: trillions of arithmetic operations per second.
  • Kernel: a small program that runs on a GPU.

Try it

Pick a script you use that feels slow. Run it with Python's built-in profiler, or any profiler for your language, and read the top three lines.

For CPU code, read the corewait page and the repository at github.com/amarbarosys/corewait. It runs on Linux and on Android phones, and each row of its table has a cause.

The video story is in our post The Slow Part Was a Memory Layout.

What to remember

Measure first. The slow part is often one small thing in an unexpected place.

Comentarii

Niciun comentariu încă.

Conectare pentru a comenta.