Skip to content

Memory and performance

NVserve keeps a model on disk and streams it through memory, so it needs far less RAM than the model's size. This page explains where the memory goes, how to set a budget, and what decides the speed.

Setting a memory budget

nvserve run model.nvpack --mem-limit 4G ...

--mem-limit does two things:

  1. Plans memory before anything loads. NVserve works out what it needs, prints the plan, and refuses to start, with the reason, if the model cannot fit:

    Llama-3.1-70B (39.6 GiB on disk) under --mem-limit 4G:

    [memory plan]
      runtime                             192.0 MiB
      weights (2 slots)                     1.6 GiB
      activations (256 rows)              401.2 MiB
      KV cache (RAM, f16, 1 × 2048 tokens)  656.0 MiB
      total                                 2.8 GiB of 4.0 GiB
    

    Optional extras are fitted in order, and dropped rather than failing: the draft model, then the KV cache in RAM (else on disk), then the expert cache for mixture-of-experts models.

  2. Enforces the cap on the process: a Job Object on Windows and RLIMIT_DATA on Linux. This is also how to check that a model really fits a smaller machine.

Leave the operating system its share: on an 8 GB system, --mem-limit 4G–5G is a sensible budget for NVserve. Without --mem-limit there is no plan and no cap, and the defaults apply.

Where the memory goes

Part Size Set by
Weight slots 2 × the largest block, e.g. 2 × 822 MiB for Llama-3.1-70B (its output layer) --slots
Activations grows with --prefill-chunk and --ctx --prefill-chunk
KV cache grows with context length and the number of requests --ctx, --kv, --max-batch
Expert cache (MoE only) most of what is left --expert-cache
Draft model (optional) the whole small model --draft

KV cache

The KV cache keeps every token's attention state. It is stored in f16 (2 bytes per value):

Model KV cache per 1000 tokens
Llama-3.1-70B 313 MiB
Llama-3.1-8B 125 MiB
Qwen3-30B-A3B 94 MiB
DeepSeek-V2-Lite (MLA) 30 MiB

With --kv auto, the default, it stays in RAM when the budget allows and otherwise goes to disk, where only the layer being computed is in RAM. --kv ram and --kv disk force one or the other. Disk KV files are temporary and deleted when NVserve exits. They go to the system temp directory, or /var/tmp on Linux, unless you set --kv-dir.

Under a 4 GiB cap, Llama-3.1-70B peaks at 2.3 GiB with a 2048-token context in RAM, and at 1.7 GiB with an 8192-token context on disk.

Expert cache

Mixture-of-experts models (Qwen3 MoE, DeepSeek) choose a few experts per token. NVserve reads only those experts and keeps recently used ones in RAM. A bigger cache means fewer reads:

Qwen3-30B-A3B Expert cache Experts from cache s/token
--mem-limit 4G 2.1 GiB 48% 0.47
--mem-limit 8G 5.1 GiB 69% 0.35–0.42

--expert-cache auto, the default, uses most of what --mem-limit leaves, or 1 GiB without a limit. Give a size (2G) to fix it, or 0 to turn it off.

NVserve also predicts the next layer's experts and reads them ahead (--expert-prefetch, default 8). 80–87% of those guesses are used.

What decides the speed

  • The disk, for most models. Each token reads the model, or its active experts, from disk. Time per token is about the bytes read divided by the disk speed. nvserve inspect shows the bytes read per pass. A PCIe 4.0 NVMe SSD reads 4–7 GB/s, PCIe 3.0 about 3 GB/s, and SATA about 0.5 GB/s.
  • The CPU, for prompts and small models. Prompt tokens are processed many at a time, which is compute-bound. Small models fit the page cache and are limited by matrix multiplication speed.

The summary at the end of nvserve run shows which one is the limit (see Reading the summary).

Making it faster

  • Mixture-of-experts models read only a fraction of their weights per token.
  • Speculative decoding verifies several drafted tokens in one pass. It makes dense 70B models 2–3× faster. See Speculative decoding.
  • Batching shares each pass between several prompts or server requests.
  • Smaller quantizations read less per token.
  • More memory grows the expert cache and keeps the KV cache in RAM.

Threads

NVserve uses half the CPU cores for computation by default, and at least 4. Every matrix multiplication waits for its slowest thread, so using all cores is often slower, especially on CPUs with efficiency cores and in virtual machines. On the 16-core test system, Moonlight-16B decoded at 0.31–0.34 s/token with 8 threads and 0.38–0.65 s/token with 14.

--threads N overrides the default, and so does the CANDLE_NUM_THREADS environment variable.

Prompts are different: their matrix multiplications are large enough to keep more cores busy. So the Q4_K and Q6_K matmuls of prompt chunks (16 tokens or more) run on a second pool with all cores but two. That makes prompts about 10% faster on the 16-core test system. --prompt-threads N (or NVSERVE_PROMPT_THREADS) sets its size. A value no larger than --threads turns it off.

Disk options

Option Default Use
--buffered off read through the OS page cache instead of direct I/O; needed on file systems without direct I/O, and fastest for small models that fit the page cache
--slots 2 streaming buffers; 2 overlaps reading the next layer with computing this one
--readers, --chunk-mb 1, 64 split each layer read across threads; one whole read was fastest on NVMe under Windows
--resident off load every weight into RAM instead of streaming (needs RAM for the whole model)