When a model does not fit in VRAM, what can the CPU take on with the smallest hit to generation speed?

February 25, 2025

While looking into local DeepSeek deployment I also searched for KTransformers. Suppose a discrete graphics card is just 1 GB short of fitting the model, but the computer still has tens of gigabytes of free RAM. Leaving that 1 GB in system memory might get the model running. Yet two methods that save the same 1 GB can behave quite differently. One barely slows the answer, while another makes the model wait for every token. What accounts for the difference?

The processor on the graphics card is the GPU. It can perform many similar calculations at once and has high-speed memory nearby, called VRAM. The general-purpose CPU has direct access to system memory. Sharing data or computation that would otherwise reside on one side is called offloading. Before choosing a strategy, ask: the next time this data is needed, can the computation run where it already lives? If it has to move, how much moves and how often?

What needs to be read again when the model writes the next word?

Suppose you ask, “The key is in a blue box. The box is on the bookshelf. Where is the key?” The model first reads the whole question and computes its initial results. This is prefill. Then it generates the answer a little at a time, called decoding. Each unit of generated text is a token: a character, word, or part of a word. Waiting for the model to finish reading the question and waiting between tokens once the answer starts are two different experiences.

DataIn the key exampleWhere it fits best
Weights, numbers learned during trainingThey determine how inputs are processed. Someone else asking another question uses the same weights.Prioritize VRAM for weights needed every step that compute quickly on the GPU. When space is tight, look for parts whose computation can go to the CPU along with them.
KV cache, intermediate results for this conversationWhen answering, the model looks back at the key, blue box, and bookshelf. It precomputes two sets of numbers for preceding text: keys help identify relevant positions and values supply the associated information. Keeping them saves recomputing the prefix at every step.If attention over earlier text runs on the GPU, an active conversation uses the cache repeatedly, so prefer the GPU. With a paused conversation, temporarily moving it out is often easier to amortize.12
Intermediate results, outputs from one layer passed to the nextThe model processes the question through many layers. While generating one token at a time, the data passed between layers is often much smaller than a whole layer’s weights.If both devices share the work, group adjacent layers together and pass results only at their boundary to reduce handoffs.3

As the model answers “blue box” in Chinese, processing “蓝” adds a cache slot. Processing “盒” reuses the previous cache and adds another slot.

Figure 1. Each slot represents the keys and values computed for the corresponding text. Blue slots are reused. An orange slot marked with a plus is newly added at this step. During generation, each step reads the old cache.1

Consider transfers alone. Suppose measured bandwidth between a discrete GPU and system memory is 20 GB/s, and 1 GB of weights moves for every token generated. Each transfer takes about 50 milliseconds. If transfers and computation are fully serialized, this step alone caps generation at roughly 20 tokens per second. Prefetching and overlapping transfer with other computation can reduce the extra wait users notice. This is an illustrative calculation. My Radeon 780M integrated GPU shared system memory with the CPU, so the discrete GPU transfer figures cannot simply be applied there.

If weights are in system memory, how do llama.cpp and vLLM compute?

CPU and system memory on the left, discrete GPU on the right. Layer splitting in llama.cpp transfers intermediate results. vLLM weight offloading transfers the relevant weights every pass.

Figure 2. Follow the arrows through a generation step. The top panel shows only the boundary between adjacent groups of layers. The thick arrow below indicates the relevant weights moving back. Arrow widths do not represent measured proportions.34

llama.cpp divides the work by layer. The CPU computes its assigned layers locally. Data goes to the GPU at the boundary. As you add CPU layers, watch whether the CPU can finish them in time.3

vLLM v0.7.3 can store weights alone in system memory. --cpu-offload-gb specifies how many GiB of weights to offload per GPU (1 GiB is about 1.07 GB). When the GPU needs to perform the corresponding calculation, those weights have to move back. The documentation says “in each model forward pass”, meaning every pass incurs a transfer. The earlier 50-millisecond example applies to this scenario.4

vLLM also has --swap-space, which reserves swap space in system memory for each GPU. When VRAM is tight, the scheduler can pause a request, swap out its KV cache, and swap it back later. It can also choose to recompute upon resumption. This frees the space occupied by a particular conversation, with a cost to resume it.24

Why does KTransformers choose experts for the CPU?

The full DeepSeek-R1 uses a mixture of experts (MoE). Think of experts as a set of optional computation modules within a layer. Each has its own weights, and each token is assigned only to some experts. The complete set is large, but only a subset computes for any particular token. That gives the CPU a meaningful share of the work. Common smaller distilled models such as R1-Distill-Qwen-1.5B are dense and do not have this pool of experts to choose from.5

One DeepSeek configuration in the version of KTransformers I saw took advantage of this property. During prefill, the GPU computed the selected experts. During token-by-token generation, the GPU still attended to earlier text and selected the experts. The CPU used expert weights kept in system memory to compute the results and returned them to the GPU. This was the division of work in one configuration. Others can choose differently.6

The GPU selects experts B and D and sends input to the CPU. The CPU computes only B and D and sends the results back to the GPU.

Figure 3. Follow the arrows around from the upper left. Choosing two experts out of four is a teaching example. The diagram depicts token-by-token decoding.6

Even if the next step selects different experts, they can still compute locally on the CPU. The cost is that the GPU waits for their results. If the CPU is already slow at reading memory or multiplying, freeing more VRAM will not automatically improve token speed. The project reported prefill and generation speeds separately precisely because these phases can have different bottlenecks.6 When running a 671B model on one 4090, what do fastllm and KTransformers each assign to the CPU?

What should I try first on my own computer?

  1. One user, with both the model and conversation fitting in discrete GPU memory. Measure all-GPU speed first as a baseline. If you only run out of memory with longer conversations, inspect the growing KV cache. Try reducing input length or the number of concurrent users to identify the bottleneck.12
  2. A dense model just exceeds available VRAM. In llama.cpp, start with a layer count that runs reliably, then gradually increase GPU layers while leaving room for the conversation cache. In vLLM, first try the smallest amount of weight offloading that lets the model fit. Compare with the same input and model precision.34
  3. Expert weights account for most of the model. Consider systems such as KTransformers that compute experts on the CPU. Check first that system memory can hold the weights, then measure tokens per second once generation starts. Fitting the weights and returning expert results quickly enough are separate requirements.6

For a practical comparison, load the model first and fix the prompt and output length. Record both time from sending the question to the first token and subsequent tokens generated per second. For multiuser serving, also measure aggregate generation speed across users. Choose the configuration with the shortest waits for your VRAM budget and usage pattern. Performance metrics and measurement in the local deployment tutorial

References

Footnotes

  1. Kwon, W., et al. (2023). Efficient memory management for large language model serving with PagedAttention. SOSP ’23. Section 2 on generation and the KV cache. ↩ ↩2 ↩3

  2. vLLM Project. (2025, February). Optimization and tuning (v0.7.3). vLLM documentation. Pausing and resuming requests under KV cache pressure. ↩ ↩2 ↩3

  3. llama.cpp contributors. (2025, January). llama.cpp (b4514). GitHub. CPU/GPU layer assignment. ↩ ↩2 ↩3 ↩4

  4. vLLM Project. (2025, February). Engine arguments (v0.7.3). vLLM documentation. Quotation from the second paragraph on --cpu-offload-gb and the adjacent --swap-space definition. See also implementation moving weights temporarily to the compute device. ↩ ↩2 ↩3 ↩4

  5. DeepSeek-AI. (2025, January). DeepSeek-R1. Hugging Face. Architecture of full and distilled models. ↩

  6. KTransformers contributors. (2025, February). KTransformers [Source code]. GitHub. Performance results available on February 25 and DeepSeek configuration in the same commit: device placement for routing, experts, and attention. ↩ ↩2 ↩3 ↩4

© 2025 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2025-02-25). When a model does not fit in VRAM, what can the CPU take on with the smallest hit to generation speed?. https://caiborui.com/blog/2025-02-25-zh-llm-inference-offloading-entrans

@misc{borui2025-e0356763,
  author = {borui},
  title = {{When a model does not fit in VRAM, what can the CPU take on with the smallest hit to generation speed?}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-02-25-zh-llm-inference-offloading-entrans}
}