Metadata-Version: 2.5
Name: sglang-kvblockd
Version: 0.2.1
Summary: SGLang HiCache storage backend (v1 interface) backed by kvblockd
Project-URL: Homepage, https://kvstash.com
Project-URL: Documentation, https://kvstash.com/integrations/sglang/
Project-URL: Get the daemon, https://kvstash.com/docs/install/
Author-email: Rudray Mehra <rudraymehra@gmail.com>
License-Expression: Apache-2.0
Requires-Python: >=3.10
Requires-Dist: kvblockd>=0.2.0
Provides-Extra: redknot
Requires-Dist: safetensors>=0.5; extra == 'redknot'
Requires-Dist: torch>=2.6; extra == 'redknot'
Provides-Extra: sglang
Requires-Dist: sglang>=0.4; extra == 'sglang'
Provides-Extra: test
Requires-Dist: pytest>=8; extra == 'test'
Requires-Dist: safetensors>=0.5; extra == 'test'
Requires-Dist: torch; extra == 'test'
Description-Content-Type: text/markdown

# sglang-kvblockd

SGLang HiCache storage backend for [KVStash](https://kvstash.com). KVStash
becomes SGLang's third cache tier, under GPU and host memory: a restarted
SGLang server, or a second one serving the same model, loads prompt KV from the
KVStash daemon instead of recomputing it.

- Get the daemon (free download): https://kvstash.com/docs/install/
- Full guide: https://kvstash.com/integrations/sglang/

Tested with SGLang 0.5.23 (the official `lmsysorg/sglang:v0.5.23` image) and
0.5.15.post1, tensor parallel 1.

## Install

```sh
pip install sglang-kvblockd
```

## Configure

The backend only shares KV between engines that declare the same weights and
cache encoding. Generate that identity in the engine's environment, passing
the same settings the engine runs with:

```sh
python -m sglang_kvblockd.identity Qwen/Qwen2.5-0.5B-Instruct --page-size 64
```

It prints five fields (`model_revision`, `model_dtype`, `kv_dtype`,
`cache_layout`, `model_config_hash`) on stdout and, for a hub id, the commit it
pinned on stderr. Launch SGLang with that same `--revision` and put the fields
in the storage extra config next to the connection settings:

```sh
python -m sglang.launch_server --model-path Qwen/Qwen2.5-0.5B-Instruct --revision COMMIT \
  --page-size 64 \
  --enable-hierarchical-cache --hicache-size 48 \
  --hicache-mem-layout page_first --hicache-io-backend kernel \
  --hicache-storage-backend dynamic \
  --hicache-storage-backend-extra-config '{
    "backend_name": "kvblockd",
    "module_path": "sglang_kvblockd.backend",
    "class_name": "KvblockdHiCacheStorage",
    "interface_v1": 1,
    "endpoint": "kvblockd://STORE_HOST:9440",
    "namespace": "sglang",
    "token": "YOUR_TOKEN",
    "model_revision": "...", "model_dtype": "...", "kv_dtype": "...",
    "cache_layout": "...", "model_config_hash": "..."
  }'
```

`STORE_HOST:9440` is the daemon's data port; keep it on a private network.
The namespace and token must match an entry in the daemon's
`namespaces.yaml`.

## Cache identity

Pass the identity command every cache-affecting engine setting:

| Engine flag | Identity flag |
|---|---|
| `--dtype` (default `auto`: follows the checkpoint) | `--dtype` (default `auto`, resolved the same way) |
| `--kv-cache-dtype` | `--kv-dtype` (default: the dtype) |
| `--page-size` | `--page-size` (default 64) |
| `--hicache-mem-layout`, `--hicache-io-backend direct` | `--layout` (`page_first`, `page_first_direct`, `page_head`) |
| `--quantization` | `--quantization` |
| `--quantization-param-path` (KV scales) | `--quantization-param-path` (its contents are hashed) |
| `--json-model-override-args` | `--config-override` |

- A Hugging Face model id is pinned to the commit it resolves to now; launch
  the engine with that `--revision`. A local directory needs `--revision` (a
  commit or a 64-hex weights digest) or `--hash-weights`, which digests the
  weight files, unless it is a Hugging Face snapshot directory.
- Missing or contradictory identity fields stop the backend from starting.
  They are declarations, not a check of the loaded weight bytes: regenerate
  them whenever a setting above or the SGLang version changes. A different
  identity gives different keys, so old pages are never read and simply
  expire.
- The backend also folds the host pool's real layout and dtype into its keys
  when SGLang registers the pool, so pages written in one byte order or 2-byte
  format are never read as another, whatever was declared. SGLang keeps every
  fp8 format as uint8 on the host, so for fp8 the declared `--kv-dtype` is what
  separates e4m3 from e5m2: a 2-byte declaration over an fp8 pool makes every
  operation miss, and fp8 pools also bind CUDA vs ROCm. Upgrading from 0.2.0
  starts with a cold cache.

## Tuning

`streams` (default 16) sets the connections to the daemon and `get_fanout`
(default `min(16, streams)`, from 1 through `streams`) how many of them a load
uses in parallel. On an L40S with SGLang 0.5.15.post1 and a 110k-token prefix,
4 shards gave 12x faster time to first token than a cold prefill, 16 gave
18.7x and 32 no more. Measure on your own hosts; machines with fewer CPU cores
may do better with smaller values.

Size SGLang's host-memory pool (`--hicache-size`) for your traffic: every
page read from KVStash passes through it.

## Limits

- Host layouts `page_first`, `page_first_direct` and `page_head`. Others
  (e.g. `layer_first`) log an error and return misses, so SGLang recomputes.
- Decode context parallel (`dcp_size` above 1) is refused at start-up.
- Tensor parallel above 1 is not tested yet.
- If the daemon is unreachable, requests recompute at normal speed.

## Experimental modules

`sglang_kvblockd.redknot` (RedKnot fork integration) and
`sglang_kvblockd.dca_readout` (Qwen DCA final-prompt readout) are opt-in and
not enabled by importing the package. Ask before using them in production:
https://kvstash.com/contact/
