Skip to content

feat: add cache salt support to KV cache events - #30827

Merged
ishandhanani merged 4 commits into
sgl-project:mainfrom
jthomson04:jthomson04/sglang-cache-salt-events
Aug 12, 2026
Merged

ishandhanani merged 4 commits into
sgl-project:mainfrom
jthomson04:jthomson04/sglang-cache-salt-events

Conversation

@jthomson04

@jthomson04 jthomson04 commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

External KV-aware routers need request cache namespaces to remain distinct from SGLang's caller-defined extra_key. Concatenating the two values can collide, and the existing KV-event hashes and payloads do not expose a typed cache namespace.

Modifications

  • Add an explicit scalar/batched cache_salt contract to Engine.generate, Engine.async_generate, request normalization, tokenization, scheduling, sessions, and the supported Python radix-cache variants.
  • Keep cache_salt separate from extra_key and index salted radix keys with the structured pair (extra_key, cache_salt) while preserving existing unsalted key behavior.
  • Namespace salted external KV-event hashes by seeding the root chain with SHA256(b"sglang-cache-salt-v1\\0" + cache_salt.encode()); internal cache hashes and unsalted event hashes remain unchanged.
  • Append typed { "cache_salt": "..." } metadata to salted BlockStored events while preserving the legacy unsalted positional wire layout.
  • Add unit coverage for normalization, OpenAI forwarding, collision resistance, radix isolation, event hashing/removal, and msgspec wire compatibility.

Cache-salt-plus-LoRA routing semantics are outside this PR.

Accuracy Tests

Not applicable; this does not modify model computation or generated outputs.

Speed Tests and Profiling

Not run. Unsalted radix keys and KV-event hashes retain their existing paths. Namespace hashing is limited to salted requests with KV events enabled.

Validation

  • python3 -m compileall -q python/sglang/srt ... for all changed source and test modules
  • Black 26.1.0, isort 7.0.0, and Ruff 0.15.1 checks on all changed Python files
  • Direct msgspec checks for legacy 7-slot unsalted events, typed 8-slot salted events, and existing typed KVEventBatch consumers
  • Full SGLang unit/GPU suites were not available in this local checkout; the focused tests are included for CI

Checklist

  • Format the changed code with the repository-pinned tools.
  • Add focused unit tests.
  • No documentation update is required for this internal routing/event contract.
  • Accuracy and speed benchmarks are not applicable to model computation.
  • Follow the SGLang code style guidance.

CI States

Latest PR Test (Base): ✅ Run #31618650333
Latest PR Test (Extra): ❌ Run #31618650207

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@jthomson04 jthomson04 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the full diff. Overall well-scoped: the collision fix is real, the wire-compat strategy (separate struct, same tag, trailing element tolerated by array-like decoders) is sound and tested, and the call-site sweep is nearly complete. Two findings that don't anchor to changed lines:

1. Isolation regression in the experimental C++ radix tree. python/sglang/srt/mem_cache/radix_cache_cpp.py (~lines 188/222/230) still builds RadixKey(token_ids, req.extra_key) with no cache_salt. Before this PR the salt reached that backend embedded in extra_key (via _compute_extra_key), so salted requests were isolated there; now the salt is a separate field that backend silently drops. With SGLANG_EXPERIMENTAL_CPP_RADIX_TREE=1, two requests with different salts and identical tokens share cache entries. Suggest propagating cache_salt at those three sites, or rejecting salted requests when that backend is active.

2. L3 storage tiers are not salt-namespaced (pre-existing; worth documenting). Internal hashes (get_native_hash) cover token ids only, and hicache storage / LMCache / FlexKV key remote blocks off those unsalted chains, so a salted request that misses the device/host tree can still prefetch blocks written under a different salt. Not a regression (extra_key has the same gap), but since this PR formalizes the salt-isolation contract, the limitation deserves a sentence in the PR body or a docstring — especially if cache_salt is meant to close prefix-probing timing side channels.

Also: the red checks are all pr-gate/*-finish jobs failing within seconds — the run gate never opened, so the new unit tests have no CI signal yet.

Inline comments below for the per-file items.

🤖 Generated with Claude Code

Comment thread python/sglang/srt/mem_cache/utils.py Resolved
Comment thread python/sglang/srt/mem_cache/events.py Resolved
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@jthomson04
jthomson04 force-pushed the jthomson04/sglang-cache-salt-events branch from bcaca2d to 369a759 Compare July 11, 2026 21:28
@ishandhanani

Copy link
Copy Markdown
Collaborator

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Jul 13, 2026
@jthomson04
jthomson04 force-pushed the jthomson04/sglang-cache-salt-events branch 4 times, most recently from 9e7c958 to 8ce7347 Compare July 15, 2026 17:01
@ishandhanani

Copy link
Copy Markdown
Collaborator

/rerun-group disaggregation radix_cache hicache

@jthomson04
jthomson04 force-pushed the jthomson04/sglang-cache-salt-events branch from 0122f20 to 12e354f Compare August 12, 2026 16:39
@jthomson04

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

3 similar comments
@jthomson04

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@jthomson04

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@jthomson04

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@ishandhanani

Copy link
Copy Markdown
Collaborator
Screenshot 2026-08-12 at 4 13 45 PM

@ishandhanani
ishandhanani merged commit 385903b into sgl-project:main Aug 12, 2026
405 of 498 checks passed
saturn-acc pushed a commit to saturn-acc/sglang that referenced this pull request Aug 16, 2026
Jialin added a commit to Jialin/sglang that referenced this pull request Aug 28, 2026
Atituiset pushed a commit to Atituiset/sglang that referenced this pull request Sep 10, 2026
wenxuewuhd added a commit to wenxuewuhd/sglang-dllm that referenced this pull request Nov 19, 2026
RadixKey now reads req.cache_salt in cache_finished_req (sgl-project#30827, after this
branch's base). The SimpleNamespace stand-in did not carry the field, so the
test passes on this branch and raises AttributeError once rebased or merged
onto current main. Found by cherry-picking the series onto main and running
the suite there.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants