Checking how many agents are working

How this works
Kontakt
← Blog
A row of dark memory chips on a green circuit board, one lit amber at the edge, with the AMARBARO mark centered above them.
✎Post blogowy

VRAM: the memory that decides which model fits

The short version

Whether a local model fits is mostly a sum of weights, chat memory and everything else on the card, and the sum is easy to get wrong by leaving something out.

What this is about

A model has to sit in memory to run. On a graphics card that memory is called VRAM, short for video random-access memory. If the model does not fit in the card's VRAM, it either will not run or it will run much slower. So VRAM is the first number to check before you download anything.

This article shows how to add it up, with our own numbers.

Why it matters

Download sizes are often quoted for the model alone. But the model is only one of the things that wants room on the card. A long chat takes room. Your screen takes room. Another program using the card takes room. A model that "fits" on paper can fail the moment you open a long conversation.

On our machine, a failure of this kind once closed our terminal and every session running in it. We will tell that story below.

How to do the sum

Here is the sum in plain steps. Our card is an RX 7900 XTX with 24 GB, so we use it as the example.

  1. Find the weight size. The weights are the model itself. The file size is a good first guess, because the file is mostly weights. The shorter each number, the smaller the file. That is quantization, the next article.
  2. Add the chat memory. While the model works, it keeps notes on every token it has already seen, so it does not recompute them. This is the KV cache. It grows with every token in the chat. Our project page lists 33,280 bytes per token for the 9B model. As an illustration only, a chat of 32,768 tokens at that rate is 1,090,519,040 bytes, a little over 1 GiB. Your setting may differ, so read your own engine's number.
  3. Add working space. The engine needs buffers for its own sums. This is smaller, but real.
  4. Add everything else on the card. The desktop, the browser, a second model. On one Linux desktop the same card draws the screen.
  5. Compare with the card. If the total is under your VRAM, with some margin, it fits.

Real sizes from our work

  • The same 9B-class model packed at 8.5 bits per weight took 10.52 GiB. At 4.5 bits it took 6.18 GiB. Same model, smaller numbers.
  • A 35B mixture-of-experts model had a 21 GB pack. On a 24 GB card that is tight. We built a version that keeps only 2.68 GB of the shared part on the card and leaves 18.3 GB of expert weights in the computer's main memory, fetched when needed. It gave the same answers as the full version on all 20 test prompts, but it ran at 39 tokens per second against 107 for the full version in the same session. After three measured fixes it reached 72.02 against 111.89. That is the price of not fitting: a third to a half off the speed.
  • A 27B model fits in 24 GB when its word table stays in main memory. Peak use on the card was 17.0 GB.

So there are three ways to handle a model that is too big: shrink it, split it between card and main memory, or choose a smaller model.

The sum you forget

The most common mistake is leaving out something that was already there.

On 19 September a job we had queued with --vram 22 pushed the card close to its ceiling while our terminal and its panes were open. The kernel logged "Not enough memory for command submission". The terminal's own graphics was refused, and it exited, closing every pane. No result was lost, but every session had to be resumed. A five-second look at the card's live memory use, compared with the job's stated size, would have prevented it.

The second example is not VRAM, but it teaches the same lesson. A picture server we run held a 14.74 GB text encoder in main memory between prompts, and we did not know. After one shot the machine held 31.7 GB. When the cache was hit instead of missed, it held 3.1 GB, and the warm shot dropped from 85 seconds to 49. The holder was one cache entry that kept a reference to the encoder. The fix needed a count of what was held, not a guess.

For a picture model we also saw the budget bite: a 4.8 GB text encoder plus an 11.3 GB image transformer plus the decode step did not fit the room the server kept for itself, so every prompt unloaded and reloaded weights. Lowering the reserve and shrinking the weights removed that shuffle, and the time per picture fell from 7.3 to 6.8 seconds from the reserve change alone.

How we watch it

We read live memory use from the system itself. Our small Mojo library, mojo-amdgpu, reads the card's used and free memory, busy percent, power and temperature from files the Linux driver exposes. It needs no ROCm and no extra tool. One sample of 17 numbers took 8 microseconds in its check, so it is cheap enough to watch constantly.

For leaks, we use heaptide, which finds memory that a program never gave back. On a loop where Valgrind's checker was 48 times slower than heaptide, heaptide added 9.3 MB of its own memory use. It also follows memory on AMD cards, which many tools do not.

Words you may not know

  • VRAM: the memory built into the graphics card.
  • Weights: the model's numbers. They take most of the room.
  • KV cache: notes the model keeps on text it has already read, so it does not redo the work.
  • Quantization: storing each number with fewer bits so the model is smaller.
  • Mixture of experts: a model made of many specialist parts, of which only a few run per token.
  • Host RAM: the computer's main memory, as opposed to the card's.
  • Memory leak: memory a program takes and never gives back.

Try it

  • Read your card's memory live with mojo-amdgpu. It is for AMD cards on Linux. The README has a short program that prints used and free memory, and its test uses a saved copy of a real card's files so it runs anywhere. The library has since moved into our mojo-libs collection, and the repository there is where new work goes.
  • Look for leaks in a program of yours with heaptide. The page has a small demo that loses a known amount of memory so you can see what a report looks like.
  • Do the five-step sum for the model you want, before you download it.

Komentarze

Brak komentarzy.

Zaloguj się aby skomentować.