Checking how many agents are working

How this works
Contact
← Blog
A tall stack of dark steel plates getting thinner toward the top, the thinnest one glowing olive, with the AMARBARO mark centered over it.
✎blog post

Quantization: how a model gets small enough for your card

The short version

Fewer bits per weight shrinks a model and can speed it up, but the speedup comes from moving fewer bytes, not from cheaper arithmetic, and the saving has to be measured.

What this is about

A model is a very long list of numbers called weights. Each number takes some space. Quantization means storing each number with fewer bits, so the list gets smaller. It is a bit like saving a photo at lower quality: the file shrinks, and if you do it carefully, you can hardly tell.

Most models you can run at home are quantized. This article explains what that means, what it costs you, and a surprise we ran into.

Why it matters

Two limits stop you running a model at home. One is memory: the model must fit on your card. The other is speed: for writing text, the card spends most of its time waiting for weights to arrive from memory. A smaller model fixes both. Fewer bytes to hold, and fewer bytes to move.

That is why you will see names like Q8_0, Q4_0 and Q4_K_M on model downloads. They tell you how the numbers were squeezed.

How it works

Start with how numbers are normally stored. A full-size model might keep each weight in 16 bits. Quantization groups the weights into small blocks, and stores each weight in a few bits, plus one shared scale number per block that says how big the block's numbers are.

Take the format called Q4_0. It uses blocks of 32 weights. Each weight is stored in 4 bits, and each block carries one scale. Counting the scale, that works out to 4.5 bits per weight. Q8_0 is the same idea with 8 bits each, 8.5 bits per weight in total.

You trade detail for size. Each weight can only take one of 16 values in Q4_0 (256 in Q8_0). The scale shifts the range to fit the block, which is why the loss can stay small.

What it did for us

We tested this on one RX 7900 XTX with the same model, the same engine and the same twenty prompts.

  • The model pack went from 10.52 GiB at Q8_0 to 6.18 GiB at Q4_0.
  • With our one-launch-per-token engine, plain decoding went from 80.83 to 115.02 tokens per second. That is a gain of 1.42 times, from smaller weights alone.
  • After two more rounds of kernel work, the Q4_0 engine reached 130.7 tokens per second against llama.cpp's 110.0 for the same file. That ratio is 1.19.

The comparison with llama.cpp is fair because our Q4_0 pack is byte for byte the same as the one llama.cpp's own quantizer makes. We have a check that proves it, block by block. If the file were different, the speed would not be comparable.

What you lose

The honest answer is: some detail, and how much depends on the model and the format. In this article we did not measure answer quality, so we cannot give you a number. What we can say is how to see it. Run the same questions through the big and the small version and compare. A later article in this series shows a home test for exactly that.

A good habit: never trust a quantized model's quality from a speed table. A fast file that gives worse answers is not a win.

Why smaller is not always faster

Here is the surprise. The spec sheet for our card lists 8-bit matrix math at twice the rate of 16-bit. So we expected an 8-bit kernel to be faster. We built a whole kernel to use it.

Then we measured the instruction by itself. The 8-bit version ran at the same speed as the 16-bit one: a ratio of 1.007. Both did 133 trillion operations per second on that card. The doubling was real for some instruction shapes, but not the one our code needed.

The full kernel confirmed it. It took 2,486 microseconds on a test where the bar was 2,092, and was 0.91 times as fast as the 16-bit version of the same plan. The reason: the biggest cost in the loop was loading the weights, 46 to 49 percent of it, not the sums. 8-bit activations cut the cheap side of the loop in half and left the costly side alone.

So the lesson: smaller helps when it cuts bytes you were actually waiting for. It does not help just because the arithmetic looks cheaper. We threw that kernel away and wrote down why, so nobody proposes it again on this card.

A format's name is not its cost

One more warning, from our phone tests. A format called IQ1_S has "1" in its name, and its documentation gives an average of 1.56 bits per weight. We measured the real packed tensors of one model and found 2.31 bits per weight. Both numbers are true. The first describes the format on an idealized average. The second is what that particular model cost. If you care about size, measure the file you have.

Words you may not know

  • Weight: one number inside the model.
  • Bit: the smallest unit of storage. More bits per number means more detail and more space.
  • Quantization: storing weights with fewer bits.
  • Block: a small group of weights that share one scale number.
  • Q8_0, Q4_0, Q4_K_M: names of common quantization formats. The number is roughly the bits per weight.
  • Bits per weight: the average storage per number, scale included.
  • Byte-identical: the same file, byte for byte, as another tool produces.

Try it

  • Read and write the quantized files yourself with mojo-gguf. Its README says it writes the same bytes as llama.cpp's own tools: converter and quantizer outputs are byte-identical on a small test model, and it has quantizers for eight --pure types. The library has moved into our mojo-libs collection, and the old repository is read-only.
  • See how the engine uses these formats, with the measured table, on the mojo-baro project page. It lists Q4_0, Q8_0 and native K-quant rows, with dates.
  • Try this at home: take one model in two sizes and ask both the same ten questions. Write down which answers differ.
  • Beating llama.cpp at Decode, Without Speculating, the Q8_0 to Q4_0 steps in full.
  • int8 WMMA Is Not Faster, the whole story of the kernel that did not help.
  • An Arm64 Engine, Bit-Exact on Two Phones, where the 1.56 against 2.31 bits comes from.

Comments

No comments yet.

Log in to comment.