> ## Documentation Index
> Fetch the complete documentation index at: https://cerebrium-mintlify-a2e78336.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Memory and GPU Checkpointing (Beta)

> Snapshot CPU and GPU memory to skip imports, model loading, and CUDA kernel compilation, drastically cutting Cerebrium container cold start times.

## Introduction

Memory checkpointing takes a snapshot of a container's CPU memory and GPU memory, and uses it to speed up the startup of future containers. Applications that perform a large amount of work at container start time benefit the most from this process.

This is useful for both CPU-only and GPU workloads. For CPU applications, checkpointing can preserve expensive initialization work such as imports, dependency loading, configuration setup, and in-memory state. For GPU applications, it can also preserve model weights, CUDA state, and compiled kernels.

For example, ML and LLM frameworks often load large model weights and compile CUDA kernels at container start time, which can take many seconds or minutes. Loading from a checkpoint that already contains this initialized state can skip most of that delay.

Since this feature is still in beta, please report all issues to the team via our [Discord Community](https://discord.gg/ATj6USmeE2) or via [Email](mailto:support@cerebrium.ai).

## How to use

Checkpointing is available in early beta to our customer base. Add the following to your `cerebrium.toml` to use it.

```toml theme={null}
[cerebrium.experimental]
checkpointing = true
```

To create a checkpoint, your application sends a trigger to our runtime once it has finished initialization and is ready. When this trigger is received, the runtime verifies whether a new checkpoint is required. To save resources, the system will not create a new checkpoint if:

1. A checkpoint already exists for the current build version.
2. Another container instance is already undergoing the checkpointing process.

If a checkpoint should occur, your container is frozen for the duration of the process. GPU memory is copied to CPU memory, and then all of the container memory is written to storage. This saved checkpoint is then distributed to run throughout the region.

Send a POST request to `http://169.254.169.253:8234/checkpoint` from inside your container when the container is ready to checkpoint.

If successful, subsequent containers are restored from this checkpoint. You can tell that a container was restored from a checkpoint if it has `CEREBRIUM_RESTORED: container restored from checkpoint` as the first log line in the container.

A checkpoint is tightly coupled to a single deployment. To disable restoring from checkpoints, remove the POST request and redeploy your application.

You can find several implementations in our [Examples repository on GitHub](https://github.com/CerebriumAI/examples).

### vLLM example

```python theme={null}
from vllm import AsyncLLMEngine
from vllm.engine.arg_utils import AsyncEngineArgs
import http
import urllib

# Init vLLM engine
engine_args = AsyncEngineArgs(
    model="Qwen/Qwen2.5-0.5B-Instruct",
    async_scheduling=False,
    sleep_mode=True
)
engine = AsyncLLMEngine.from_engine_args(engine_args)

# Drop KV cache for reduced GPU memory footprint.
engine.sleep(level=1)
# Trigger checkpoint
try:
    import json
    req = urllib.request.Request("http://169.254.169.253:8234/checkpoint", method="POST")
    with urllib.request.urlopen(req, timeout=300) as response:
        result = response.read()
        print(json.loads(result))
except http.client.RemoteDisconnected:
    # TCP connections disconnect on restore and throw remote
    pass

# Restore KV cache
engine.wake_up()
```

## Limitations

**Memory overhead:** The container memory allocation must be large enough to contain the GPU memory dump in addition to your regular memory use.

**Execution lifecycle:** When a container is restored from a checkpoint, execution continues from the point where the HTTP request was sent. Any environment variables read before this point remain the same as they were at the time of the checkpoint.

**Network connections:** Any TCP connections made before the checkpoint will have disconnected. For example, if you connected to a database before the checkpoint, you must reestablish that connection after restore.

**Ephemeral filesystem:** Any files written to disk before the checkpoint are not copied to the restored container. Only memory is checkpointed.

**Provider Availability:** Checkpointing is only available on the <b>AWS provider</b>. More coming soon.

## Platform-specific recommendations

### vLLM

vLLM checkpointing support is not complete but is still possible. See [vllm-project/vllm#34303](https://github.com/vllm-project/vllm/issues/34303) and related issues.

The larger the size of the memory checkpoint the slower the restore is. We can reduce the size of the snapshot substantially and improve startup times by dropping the KV Cache before checkpoint and recreating it after restore. vLLM has functionality that does this built in as part of [vLLM Sleep Mode](https://docs.vllm.ai/en/latest/features/sleep_mode/).
