2026-11-27 16:19 UTC

llama-sleep parks llama.cpp model state to speed local GPU swaps

state: expiredheat: mediumuncertainty: mediumrelevance: mediumllama-cpp local-inference model-switchingRobert Zaage

What is this?

On 2026-11-05, developer Robert Zaage published llama-sleep, an MIT-licensed wrapper for llama.cpp deployments managed by llama-swap. It parks an idle model’s CUDA state—including weights and KV cache—in host RAM rather than terminating the process, then restores it to the GPU when needed; the wrapper holds requests until the model is ready. Zaage reports that on one Linux/NVIDIA RTX 3060 setup, a swap between two GPU-filling models fell from 10.3 seconds to 2.1 seconds, or 1.7 seconds with a Linux multi-size transparent huge-page setting, while preserving cached prompts. These are the author’s measurements on a single machine, not independent benchmarks; the evidence does not establish broad compatibility or adoption.

Why it matters

This is a practical infrastructure improvement for local inference, not a new model: keeping model state and prompt caches warm could make a single-GPU server more responsive when different tasks need different models. That may help home assistants and small deployments offer model choice without paying a full reload and recomputing long prompts each time. The trade-offs matter: parked state consumes host RAM, performance depends on idle time and hardware/software configuration, and the reported results come from one setup.

Current assessment

New: Zaage’s 2026-11-05 article describes and links the MIT-licensed llama-sleep wrapper. In the author’s test, llama-swap’s baseline two-26B-model swap took 10.3 seconds; cumulative changes reduced it to 2.1 seconds, and enabling mTHP for 16 KB–1 MB pages reduced it to 1.7 seconds. A 26B-to-12B swap fell from 5.1 to 1.1 seconds. Zaage also reports that a 9,589-token prompt retained its cache across a swap and required only one token to be processed on return. The mechanism is to checkpoint CUDA process state into host RAM while idle, then restore it and gate requests until ready. The reported improvement combines wrapper behavior, configuration changes, and a kernel setting; it is not a controlled cross-platform comparison. Tests used one RTX 3060 12 GB system, Linux/Fedora, NVIDIA drivers, and specified quantized Gemma models. Limitations include host-RAM use comparable to parked GPU state, slower swaps if requests arrive while parking is underway, and NVIDIA/Linux requirements (driver 570 or later). Further hardware, workload, reliability, compatibility, and adoption evidence is not supplied. If the approach generalizes, it could reduce latency and preserve prompt-cache work for local multi-model services, but current evidence supports a promising individual implementation rather than a general performance guarantee.

Measured heat

now 0 pts/hpeak 0 pts/hcomments 0/hpeers p36momentum: steady2 platformsage 418h
points/hour across evidence · reading as of 2026-11-21 10:22:47.297091+11:00 · deterministic, not a model opinion

How the heat travelled

11-03 13:00⭐ origin echo-reconstructedThe original primary artifact is Robert Zaage’s llama-sleep repository, introduced in commit b48a327 on 2026-11-04. Its README says: “Instea
Robert Zaage on github (echo) · attributed from hn.story.50399642
—
11-06 22:59first on hacker news · published · +82.0hSwapping LLMs in Under Two Seconds: A Sleep Mode for Llama.cpp
phirdev
—
11-06 22:59amplified on hacker news 👑hn.story.50399642
phirdev
peak 2 · 0 comments · 104% of case engagement
11-06 23:22our radar first saw it · +82.4hdiscovery anchor: hn.story.50399642—

Evidence (2) — ⭐ canonical anchor

sourceobjectauthorscorecomments
🟧 hnSwapping LLMs in Under Two Seconds: A Sleep Mode for Llama.cpp
Retrieved article excerpt

Open article · Retrieved 2026-11-06T23:24:04.927719+00:00

# Swapping LLMs in Under Two Seconds: A Sleep Mode for llama.cpp

2026-11-05
by [Robert Zaage](https://zaage.it/about/)
10 min read
[#note](https://zaage.it/tags/note/) [#linux](https://zaage.it/tags/linux/) [#machine learning](https://zaage.it/tags/machine-learning/)

Contents

## Abstract[#](https://zaage.it/writing/llama-sleep/#abstract)

Different tasks want different language models, and with a single GPU a proxy such as llama-swap has to swap them, often several times an hour.
A swap between two GPU-filling models took 10.3 seconds and threw away the cached prompts of the model it replaced. One configuration change and a small wrapper, *llama-sleep*, which parks a model’s GPU memory in host RAM instead of stopping it, bring it down to 2.1 seconds with cached prompts intact.
What remains is the copy between host RAM and GPU, held back mainly by 4 KB pages rather than by PCIe; with multi-size transparent huge pages, a kernel setting, the swap takes 1.7 seconds, about six times faster than at the start.

## 1 Introduction[#](https://zaage.it/writing/llama-sleep/#1-introduction)

On my home server, a Gemma 4 12B model answers a voice assistant, and a Gemma 4 26B model handles long inputs, many of them from automations. Each fills the GPU alone, so every automation job costs two swaps: to the 26B and back.
[llama-swap](https://github.com/mostlygeek/llama-swap) [1], a proxy in front of llama.cpp’s `llama-server` [2], does the swapping: it stops the running model and starts the requested one. That costs a full model load, and the voice model loses its cached system prompt of about 8,000 tokens; recomputing it delayed the first spoken word by another 6–7 seconds.

vLLM has a *sleep mode* for this [3], and llama-swap ships a `vllm-wrapper` that uses it [4, 5]. llama-server’s `--sleep-idle-seconds` unloads a model after idle time [6], but it frees the KV cache with it and reloads the weights on the next request.
The question of this note: how fast can a swap behind llama-swap get, with llama-swap unchanged and as little extra software as possible?

## 2 Setup and method[#](https://zaage.it/writing/llama-sleep/#2-setup-and-method)

- **Hardware:** NVIDIA RTX 3060 12 GB on PCIe 3.0 x16 (the Ryzen 5 PRO 5650G limits the slot to gen 3), 125 GB DDR4, Fedora 44, driver 615.71.
- **Software:** llama-swap v263 and llama.cpp b11552 (CUDA 13) in one container (`ghcr.io/mostlygeek/llama-swap:v263-cuda13-b11552-non-root`). All model files are in the page cache.
- **Models:** Gemma 4 12B QAT and Gemma 4 26B A4B QAT, both UD-Q4\_K\_XL. The 12B uses 7.9 GiB of GPU memory, the 26B 11.1–11.3 GiB with part of its expert weights on the CPU. For a worst case, two separate 26B processes are swapped against each other.
- **Metric:** time from sending a request for the other model until its first token arrives (`max_tokens` 1), measured at the client. Each value is the mean of two steady-state rounds of the cycle 26B → 26B → 12B.
- **Breakdowns:** llama-swap logged with microsecond timestamps and the wrapper logged every step; client, proxy and wrapper events were merged into one timeline.

## 3 Where the ten seconds go[#](https://zaage.it/writing/llama-sleep/#3-where-the-ten-seconds-go)

A baseline swap between the two 26B models takes 10.3 seconds: stopping the old process, starting the new one, loading the weights and waiting until llama-swap’s health check succeeds. Two parts of that are pure overhead.

- **Probing memory.** llama.cpp’s `--fit` finds out how many layers fit on the GPU by creating trial contexts, again at every start. A fixed split (`--n-cpu-moe 14` for the 26B) halves the swap to 5.5 seconds. The catch: the split is tuned by hand for one model on one GPU. llama-sleep’s wake does not depend on it, but every cold start does.
- **Polling.** llama-swap checks the new model’s health 250 ms after starting it and then once a second [1], so a model that is ready just after a check waits up to a second.

Skipping llama.cpp’s warmup run (`--no-warmup`) changes nothing measurable. What remains is loading weights that the machine already holds in RAM, and that is what parking avoids.

## 4 Parking instead of stopping[#](https://zaage.it/writing/llama-sleep/#4-parking-instead-of-stopping)

### 4.1 What cuda-checkpoint can do[#](https://zaage.it/writing/llama-sleep/#41-what-cuda-checkpoint-can-do)

Since driver 550, NVIDIA’s `cuda-checkpoint` tool can suspend the CUDA state of a running process [7]. It copies the process’s device memory into host memory and frees the GPU; a second call copies it back to the same addresses.
On the running 12B this took 3.8 seconds out and 1.5 seconds back in, and the model answered correctly afterwards.

Two observations shaped the design:

- **A request to a parked model crashed it.** A suspended llama-server still answers `/health` with 200, so llama-swap considers it ready and forwards the request while the restore is still running. The process then segfaulted inside `libcuda`. NVIDIA’s documentation says such calls may block until CUDA is resumed [7]; with llama-server they did not.
- **A frozen process cannot be woken.** Freezing the container would keep requests away, but the restore then hangs: it runs on a CUDA restore thread of the target process (`cuda-checkpoint --get-restore-tid` reports its ID) [7, 8], and the cgroup freezer stops that thread too.

### 4.2 llama-sleep[#](https://zaage.it/writing/llama-sleep/#42-llama-sleep)

[llama-sleep](https://git.zaage.it/robert/llama-sleep) [9], MIT-licensed, follows the pattern of llama-swap’s `vllm-wrapper` [4]: a small program that is the model’s `cmd` and `cmdStop`. Figure 1 shows how it swaps two models.

```
macros:
  "serve": llama-sleep serve --name ${MODEL_ID} --listen 127.0.0.1:${PORT} --idle 5s --
  "sleep": llama-sleep sleep --name ${MODEL_ID} --stop-pid ${PID}
models:
  "gemma-26b": { cmd: "${serve} llama-server ...", cmdStop: "${sleep}" }
```

Animated diagram of two models, A and B, each behind its own llama-sleep wrapper, sharing one GPU. Model A has been idle for five seconds, so its GPU state, weights and KV cache, is copied to host RAM in the background and the GPU is free. A request for B arrives through llama-swap and is held at B's closed gate while B's GPU state is copied from host RAM back to the GPU; then the gate opens and B answers. The same then happens in reverse.

Figure 1. A swap with llama-sleep: the idle model is parked in host RAM before anyone needs the GPU, and the request for the other model waits at its gate until that model is awake.

- **It holds requests.** llama-server runs as a detached daemon that only llama-sleep can reach, through a Unix socket or an internal container port. llama-sleep is the reverse proxy on llama-swap’s port and holds every request, the health check included, until the model is awake and healthy. This prevents the crashes and replaces llama-swap’s polling: the health check returns the moment the model is ready.
- **It parks in the background.** With `--idle 5s`, a model that has been idle for five seconds is parked while nobody waits. A later swap then only has to wake the new model.
- **It wakes on start.** llama-swap starts `serve` because a request is waiting, so the wake begins at once instead of at the first health check.
- **It is one file.** llama-sleep is a single Go binary of about 7 MB, built without cgo. It calls the checkpoint functions of the CUDA driver API (`cuCheckpointProcess*`) [7, 8] through `dlopen` [10] and needs neither `cuda-checkpoint` nor the CUDA toolkit.
- **It fails safe.** A changed command line replaces a parked daemon instead of resuming it; a failed restore or a crashed daemon ends `serve`, so llama-swap notices and starts the model fresh on the next request.

## 5 Results[#](https://zaage.it/writing/llama-sleep/#5-results)

Horizontal bar chart of swap time until the first token for six steps. Swap between two 26B models: baseline 10.3 seconds, fixed layer split 5.5, held health check 4.5, parked in the background 2.5, wake on start 2.1, huge pages (mTHP) 1.7. Swap from the 26B to the 12B: 5.1, 4.0, 3.5, 2.0, 1.9 and 1.1 seconds.

Figure 2. Swap time until the first token after each step, between two GPU-filling 26B models and from the 26B back to the 12B voice model. The last step, multi-size huge pages, is explained in section 6.

The steps are cumulative: the fixed layer split from section 3; holding requests, parking in the background and waking on start from section 4.2; and huge pages from section 6. Each removes a visible part of the swap, and parking brings the largest gain (Figures 2 and 3). The direction not shown, from the 12B to the 26B, behaves like the 26B pair: 10.1 seconds at the start, 1.6 at the end.

Animation of two swaps on a shared time axis. Top lane, baseline: the old model stops, the new one probes memory, loads weights, waits for the health check and answers after 10.3 seconds. Bottom lane, llama-sleep: the old model is already parked, the new one is copied from RAM to the GPU and answers after 1.7 seconds.

Figure 3. The same swap between two 26B models without and with llama-sleep (with huge pages, section 6), played in real time. The baseline lane’s phases are reconstructed from separate measurements; both totals are measured.

Cached prompts survive. A 9,589-token prompt sent to the 12B again after a swap to the 26B and back was answered with **one** token processed and 9,588 taken from the cache; after a cold start, all 9,589 had to be processed.

Stacked horizontal bars of one swap from the 26B to the 12B, in milliseconds. Baseline cold start, about 5,100: stop, model load, health-check gap and first token. Parked during the swap, 6,770: copy GPU to RAM 4,950, health-check gap 250, copy RAM to GPU 1,490. Parked in the background, 2,000: health-check gap 250, copy RAM to GPU 1,673. Wake on start, 1,866: copy RAM to GPU 1,784, everything else 82.

Figure 4. Where the time goes in one 26B → 12B swap, with 4 KB pages. Except for the baseline, whose split is approximate, the segments come from millisecond-timestamped traces.

Parking pays off only in the background. Parked *during* a swap, the slower copy direction sits on the critical path: with the fixed split, 26B → 12B then takes 6.8 seconds against 3.5 seconds for stopping the old model.

With parking in the background and the wake on start, everything around the copy shrinks to 82 ms: llama-swap, process start, unlock, health check, prompt and first token. The other 1,784 ms are the copy back to the GPU (Figure 4).

## 6 The copy: page faults, not the bus[#](https://zaage.it/writing/llama-sleep/#6-the-copy-page-faults-not-the-bus)

Both copies, out of the GPU when parking and back in when waking, are far slower than the link. A small benchmark with the CUDA driver API (1 GiB copies, best of five) shows what the link can do: 13.4 GB/s from pinned host memory to the GPU and 13.2 GB/s back, and about 12–13 GB/s with ordinary memory that has been touched before. Copied from the GPU into a freshly allocated buffer, the same data moves at only 2.2 GB/s, which is exactly llama-sleep’s park speed.
Parking is that case: the driver allocates the host buffer when it parks a model, so every 4 KB page has to be faulted in and zeroed during the copy, about two million of them for 8 GB. The faults, not PCIe, set the pace.

Larger pages mean fewer faults [11]. Classic 2 MB transparent huge pages helped, but only while the kernel had free 2 MB blocks: on this host, which had been running for a while, the first two models got them and the third did not, and forcing them with synchronous compaction made parking slower. Multi-size transparent huge pages for anonymous memory (mTHP, Linux 6.8 and later) add sizes between 4 KB and 2 MB, and the kernel kept finding those in the same fragmented memory:

```
# as root; not persistent, so run it at every boot (for example from tmpfiles.d)
cd /sys/kernel/mm/transparent_hugepage
for s in 16 32 64 128 256 
phirdev20
🟧 echo.github ⭐The original primary artifact is Robert Zaage’s llama-sleep repository, introduced in commit b48a327 on 2026-11-04. Its README says: “InsteaRobert Zaage——

Interpretation history

Decision trace