# Measuring a Model's GPU Memory

> Load a model, benchmark it, and measure the GPU memory it really holds at its peak (weights, drafters, CUDA graphs, overhead, KV cache); compare the peak with the profile's hardware booking, and write the measurement into the profile.

A deploy profile states two amounts of GPU memory for each GPU a replica runs on:

- **The hardware booking** (`hardware.gpu.memory_bytes_per_device`): the memory the
  replica holds. Planning places a replica only on GPUs with this much free, and an alert
  fires when a replica uses more.
- **The engine booking** (`runtime.engine_memory_booking_bytes_per_device`, optional):
  the memory the engine is told to use. For vLLM and PyTorch engines the deploy writes
  the engine's own memory option from it and each GPU's memory; without it, from the
  hardware booking. SGLang's `--mem-fraction-static` is used as the profile writes it.

Two amounts, because engines take more than they are told: under load, activations and
buffers outside the engine's own budget add to what it holds. vLLM told 83.58 GiB was
measured at 86.3 GiB at its peak. The hardware booking must cover that overshoot, so it
is above the engine booking.

The measurement shows where the memory goes at the peak, and whether the peak stays
within the hardware booking. The engine booking and the measurement are fields of
version 1.1.0 of the profile format, so a profile that writes them names it in its first
line; a profile written for an older version leaves them out:

```yaml
$schema: https://schemas.onprem.ai/ai-deploy-profiles/v1.1.0/ai-deploy.schema.json

hardware:
  gpu:
    product: nvidia-rtx-pro-6000-blackwell
    count: 1
    memory_bytes_per_device: 94489280512            # hardware booking: 88 GiB
runtime:
  engine: vllm
  compose: compose.yaml
  engine_memory_booking_bytes_per_device: 90194313216   # engine booking: 84 GiB
  measured_gpu_memory_bytes_per_device:             # at the peak, while benchmarked
    model_weights: 23761906565                      # 22.13 GiB
    cuda_graphs: 762356695                          #  0.71 GiB
    runtime_overhead: 5599983043                    #  5.22 GiB
    kv_cache: 62513248993                           # 58.22 GiB
  measured_kv_cache_tokens_per_replica: 1671168
```

The parts add up to the peak, 86.28 GiB. Two figures follow from it and are not
written: the headroom, the hardware booking minus the peak (1.72 GiB), and the
overshoot, the peak minus the engine booking (2.28 GiB).

## The parts

| Part | What it holds |
|---|---|
| `model_weights` | the model's weights loaded on the GPU; with several GPUs, this GPU's share. A drafter whose layers sit in the main weights' files is counted here |
| `drafters` | every speculative-decoding drafter together whose memory can be told apart (MTP layers in files of their own, EAGLE heads, a separate draft model): its weights, its own KV cache and its buffers |
| `cuda_graphs` | the captured CUDA graphs |
| `runtime_overhead` | the activation peak, the CUDA context, the buffers between GPUs, and what the engine took beyond its booking under load |
| `kv_cache` | the KV cache pool the engine allocated |

- Every value is a whole number of bytes. A part the profile does not have is left out,
  never written as 0. An image model has no KV cache and no drafter.
- The parts add up to the peak, which stays at or below the hardware booking. When it
  does not, the profile check warns.
- `measured_kv_cache_tokens_per_replica` is how many tokens the KV cache holds, across
  all the replica's GPUs, as the engine reported it. It is written together with
  `kv_cache`, and only with it.
- With more than one GPU, write the GPU that uses the most memory.
- Measure the profile exactly as it is written. A change to `compose.yaml` needs a new
  measurement in the same commit.

## Before you measure: the engine booking

For vLLM and PyTorch engines the deploy writes the engine's memory share from the engine
booking and each GPU's memory; the share written in `compose.yaml` is replaced. Without
an engine booking it is written from the hardware booking: the engine is then told all
of it, and its overshoot under load goes above the hardware booking.

So a profile you measure states an engine booking below its hardware booking. When it
has none, set one before you measure: the amount the engine ran with so far (the
`Desired GPU memory utilization is (…, … GiB)` line of an earlier deployment log, or the
journal), keeping the hardware booking. Then the measurement shows the overshoot on top
of it, and whether the hardware booking covers it.

The profile check says where this matters: the hint `engine.gpu-memory-share-written`
appears when the share in `compose.yaml` is not the one the deploy writes from the
booking, and names the value to write there. Keep the setting in `compose.yaml`: the
deploy replaces only a setting that is there, and without one the engine has no limit
(the warning `engine.gpu-memory-limit`). Check the profile before you measure and after
every change.

## The procedure

1. **Load the model.** Deploy the profile as a test service and wait until its replica
   is ready. Its deployment log holds the engine's start-up lines; the time from deploy
   to ready is its loading time.
2. **Load it.** `inference_services_change`, operation `replicas_benchmark_start`, then
   `inference_services_read`, operation `replicas_benchmark_get`. Its workloads raise the
   concurrency step by step. The peak comes under many long answers at once (`max-decode`
   at high concurrency drove it in the measurements so far), and the engine keeps memory
   it took, so the peak does not fall back. Two loads:
   - **Memory only**, when only the bookings change: run only `max-decode/1.1.0`, and
     stop the run (`replicas_benchmark_cancel`) once it has reached concurrency 32 (or
     the profile's highest, when lower) and the peak has not grown for 2 minutes; at the
     latest after 15 minutes. It writes no benchmark file; the journal says how it ended.
   - **Benchmark and memory**, when the model card's figures change too: every workload,
     to its end (minutes to an hour), and its benchmark file is committed with the
     measurement.
3. **Read the peak after the benchmark.** `time_series_read`, operation `query`, in one
   call, with the replica's id and a window that covers the run (`[2h]` for a run
   that started at most two hours ago):

   ```
   max_over_time(sum by (inference_service_replica_id, gpu_uuid) (
     onprem_ai_inference_service_replica_gpu_vram_used_bytes{inference_service_replica_id="<id>"}
   )[2h:])
   ```

   The series has one sample per process of the replica on a GPU. A replica with
   several processes on one GPU (two containers, or an engine with helper processes)
   holds their sum, so the query takes the highest sum at one time, never the sum of
   each process's highest value. The answer has one series per GPU; for a replica on
   several GPUs, the highest one is the peak per GPU.
   When the query answers no series (the node's metrics do not arrive), read
   `inference_services_read`, operation `get`, every half minute while the benchmark
   runs: each replica's GPU allocations carry `vram_used_bytes`, what its processes
   hold on that GPU now, and the highest value is the peak.
   `inference_nodes_compute_devices_read`, operation `inspect_processes`, shows which
   processes hold the memory.
4. **Compare the peak with the hardware booking**, and work out the parts (below).

## Where the numbers are

**The deployment log.** `logs_read`, operation `list`, with the replica's
`inference_service_replica_id`, names its deployment log. Search it with operation
`read` and a `pattern`, like `grep -i`; search again with other words when a line is
not there. Engines change their wording between versions: read the lines around a
match (`context_lines`) rather than trust one exact phrase.

| Part | vLLM: search for | SGLang: search for |
|---|---|---|
| `model_weights` | `Model loading took` (`... took 18.5 GiB memory`) | `Load weight end` (`... mem usage=18.50 GB`) |
| `cuda_graphs` | `CUDA graph pool memory` (`... GiB (actual)`), `Graph capturing finished` (`... took 0.98 GiB`), or `CUDAGraph memory` | `Capture cuda graph end` (`... mem usage=0.98 GB`) |
| `kv_cache` | `Available KV cache memory`, or `kv cache memory in use` | `KV Cache is allocated` (`K size`, `V size`) |
| tokens | `GPU KV cache size: N tokens` | `max_total_num_tokens=N` |
| the engine's budget | `Desired GPU memory utilization is (0.88, 83.58 GiB)` | `--mem-fraction-static` times the memory free when the engine started |
| `drafters` | `drafter`, `draft model`, `speculative`, `EAGLE`, `MTP` | `draft`, `EAGLE`, `NEXTN` |

vLLM also writes one line with most of it: `Actual usage is ... GiB for consumed memory
(weights + non-torch), ... GiB for peak activation, and ... GiB for CUDAGraph memory`.
Search for `Actual usage is`.

Other engines (llama.cpp, TensorRT-LLM, image servers) word it their own way: search
for `memory`, `MiB`, `GiB`, `cache`, `graph`, `weights`, `allocated` and read what is
there.

## Working out the parts

1. Convert what the log says to bytes: 1 GiB is 1073741824 bytes; SGLang's `GB` is
   GiB as well.
2. `model_weights`, `cuda_graphs` and `kv_cache` come from the log.
3. `drafters`: when the engine reports the drafter's memory apart, take it. When it
   counts the drafter inside the weights (vLLM's `Model loading took` does with MTP)
   and the drafter has weight files of its own (an entry of its own in
   `model.weights`, such as `draft`), take their size and subtract it from
   `model_weights`. When the drafter's layers sit inside the main weights' files (many
   MTP models), nothing tells them apart: leave `drafters` out, keep the memory in
   `model_weights`, and say so in the journal.
4. `runtime_overhead` = the peak minus every other part. It holds what the engine does
   not report on its own: the CUDA context, the activation peak, and its overshoot.
5. Write each number's source in the journal entry: the log line, the peak's time, or
   the calculation. Write the engine's budget from the log, the peak, the headroom and
   the overshoot there too.

## When the peak does not fit

The peak must stay below the hardware booking: a replica that holds more takes memory its
neighbours on the GPU were promised.

- **Peak at or below the hardware booking:** write the measurement.
- **Peak above it:** the hardware booking is too small. Raise
  `hardware.gpu.memory_bytes_per_device` above the peak with a margin, write the
  measurement, and propose the change to the catalog (a pull request for a git catalog
  with an upstream). The other way is to lower the engine booking, which shrinks the KV
  cache, and measure again. Never write a measurement whose peak is above the hardware
  booking without raising the booking: the profile check warns about it.
- **The engine's own budget already above the hardware booking:** the engine is told
  more than the replica may hold. Report it: the engine booking, or the share in
  `compose.yaml`, is wrong (see [Tuning a model's performance](/en/platform/docs/guides/models-and-deployments/tuning-model-performance/index.md)).
