Skip to content

feat: add FlashStates hybrid prefix caching - #1806

Merged
AlpinDale merged 1 commit into
mainfrom
feat/flashstates
Nov 13, 2026
Merged

AlpinDale merged 1 commit into
mainfrom
feat/flashstates

Conversation

@AlpinDale

@AlpinDale AlpinDale commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Summary

Integrates GPU-resident FlashStates into Sonar for supported attention/recurrent hybrid models. Context parallelism and custom host-cache tier support through Mooncake will arrive in later PRs.

It's recommended to use this for multi-turn agentic workloads, especially when many long conversations compete for cache space. As an example, this was tested on 2x B300 GPUs:

sonar serve nvidia/GLM-5.3-Flash-NVFP4 \
  --tensor-parallel-size 2 \
  --enable-prefix-caching \
  --mamba-frontier-checkpoint \
  --mamba-prefill-export \
  --prefix-cache-eviction gds \
  --prefill-cost-coefficients 1.862e-5 3.33e-6 \
  --prefix-cache-leaf-last \
  --prefix-cache-leaf-idle-seconds 60

Performance

Sessions Baseline output tok/s/GPU FlashStates output tok/s/GPU Speedup Baseline p90 TTFT FlashStates p90 TTFT
24 445.90 443.82 1.00× 0.494 s 0.720 s
48 229.70 632.92 2.76× 14.925 s 1.605 s

What does each option do?

  • --mamba-frontier-checkpoint: Saves recurrent state at completed request boundaries, including positions between cache-block boundaries.
  • --mamba-prefill-export: Saves additional checkpoints during prefill.
  • --prefix-cache-eviction gds: Chooses eviction candidates using estimated recomputation cost relative to cache space.
  • --prefill-cost-coefficients: Supplies the model’s prefill-cost estimate. The values above are for GLM, not universal defaults.
  • --prefix-cache-leaf-last: Prefers retaining the newest checkpoint for a conversation.
  • --prefix-cache-leaf-idle-seconds 60: Removes that preference after a checkpoint has been idle for 60 seconds. It does not forcibly expire it.

If you're not sure what to set the coefficients to, --prefill-cost-coefficients 1 0 will provide you with a linear, although untuned, estimate. More details below.

How to tune the cost coefficients

Only the ratio of the two coefficients matter, so users can tune one param:

--prefill-cost-coefficients 1 R

R represents how much more expensive processing a token becomes as its context grows. Sonar currently has no automatic calibration command.

For practical tuning, start with 1 0, which estimates cost purely from token count. Then sweep a few ratios on a representative multi-turn workload:

1 0
1 0.1
1 0.2
1 0.5
1 1

Above aren't actual measured recommendations, so try as many variations as you can to further tune this. Compare output throughput and p90 TTFT under cache pressure. Cache hit rate helps explain the result, but should not be the sole optimization target.

Fitting coefficients from timings

The implemented estimate for prefilling positions [a, b) is:

$$ T(a,b) = D(b-a) + E\frac{b^2-a^2}{200000} $$

  • D: context-independent processing cost per token.
  • E: additional per-token cost per 100,000 tokens of context.
  • Increasing E/D gives more weight to avoiding recomputation at longer contexts.

For an empirical fit:

  1. Warm the model and kernels.
  2. Measure uncached prefill across several prompt lengths covering the deployment’s range.
  3. Use repeated measurements without queueing.
  4. Fit:

$$ T(L) = C + DL + E\frac{L^2}{200000} $$

Include an intercept C for fixed overhead, with constraints D > 0 and E ≥ 0.

  1. Pass D E, or equivalently 1 E/D, and validate against the actual workload. Measured prefill execution time is preferable. Single-request TTFT with one output token is a usable approximation, but includes additional overhead.

Signed-off-by: AlpinDale <alpindale@gmail.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T11:58:06.757833Z f140599 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@AlpinDale
AlpinDale added this pull request to stack #1810 November 13, 2026 16:50
@AlpinDale
AlpinDale merged commit 33a0078 into main Nov 13, 2026
2 checks passed
@AlpinDale
AlpinDale deleted the feat/flashstates branch November 22, 2026 11:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant