Metadata-Version: 2.5
Name: sglang-kvblockd
Version: 0.2.0
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 with the same dtype, KV dtype and page
size the engine runs with:

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

It prints five fields (`model_revision`, `model_dtype`, `kv_dtype`,
`cache_layout`, `model_config_hash`). Put them in the storage extra config
next to the connection settings:

```sh
python -m sglang.launch_server --model-path Qwen/Qwen2.5-0.5B-Instruct \
  --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

- A Hugging Face model id is pinned to the commit it resolves to when you run
  the helper. A local directory needs `--revision` (a commit or a digest of
  the weights) unless it is a Hugging Face snapshot directory.
- Pass `--rope-override` with the JSON the engine applies on top of
  `config.json` (for example a YaRN `rope_scaling`), so it enters the hash.
- Missing or contradictory identity fields stop the backend from starting.
  They are declarations, not a check of the loaded weight bytes: regenerate
  them whenever the model, dtype, KV dtype, page size or SGLang version
  changes. A different identity gives different keys, so old pages are never
  read and simply expire.

## 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 layout `page_first` only. Other layouts 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/
