Repository navigation
feat(metrics): count prompt tokens restored from kept prompt states - #531
Merged
Merged
Conversation
An engine that keeps prompt states between requests (Flash Next and GLM-5.3-Flash with a prompt cache) now says so in Info.prompt_cache, and each finished request adds the prompt tokens its prefilled event reported as restored. /metrics then carries tensorfold:prompt_tokens_cached_total (vLLM's name) and repeats it with prompt_tokens_total as vLLM's prefix_cache_hits_total and prefix_cache_queries_total, so a vLLM dashboard's prefix cache hit rate fills. An engine that keeps no prompt states leaves all three out, as before.
Owner
|
Thank you @juliankang4. Merged into main (733c4f8), with one follow-up of ours (b29db35): the Qwen3.8-27B host, whose prompt cache is new in 1.0.3, sets the same flag, so its restored tokens count too. |
This was referenced Oct 10, 2026
jschmied
pushed a commit
to jschmied/TensorFold
that referenced
this pull request
Oct 10, 2026
ashhart#531 marks Flash Next's and GLM's kept prompt states; the 27B's cache, new in 1.0.3, now says so as well.
jschmied
pushed a commit
to jschmied/TensorFold
that referenced
this pull request
Oct 10, 2026
Closed
BlivionIaG
pushed a commit
to BlivionIaG/TensorFold
that referenced
this pull request
Oct 20, 2026
…rompt states Thanks @juliankang4. Fixes ashhart#530.
BlivionIaG
pushed a commit
to BlivionIaG/TensorFold
that referenced
this pull request
Oct 20, 2026
ashhart#531 marks Flash Next's and GLM's kept prompt states; the 27B's cache, new in 1.0.3, now says so as well.
BlivionIaG
pushed a commit
to BlivionIaG/TensorFold
that referenced
this pull request
Oct 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this changes
Engines that keep prompt states between requests now publish
tensorfold:prompt_tokens_cached_totalon/metrics.It's the number of prompt tokens finished requests got from a kept state, the same figure each reply already reports
in
usage.prompt_tokens_details.cached_tokens. Divide it byprompt_tokens_totaland you have the hit rate.This family was held back in 8debb38 (from #407) because the Zig engine had no prompt cache yet. Flash Next and
GLM-5.3-Flash have one since 1.0.2, so they get the family now. Engines without a prompt cache still leave it out.
Closes #530.
How it works
Infohas a newprompt_cacheflag.flashnext_hostsets it when it creates its prompt store, andglm_hostsetsit when it attaches its store to the lane host. Neither creates a store on rank 1 of speed-up mode or with
--prompt-cache-gib 0, so the flag stays off there. Nemotron and Qwen3.5 don't have a store at all.chat.generatealready keeps the restored token count from theprefilledevent forusage. It now passes thatcount to
noteRequesttoo, andMetrics.noteadds it up next toprompt.renderwritesprompt_tokens_cached_totalright afterprompt_tokens_total. Like the TPOThistogram, the name is already vLLM's (
vllm:prompt_tokens_cached).prefix_cache_queries_totalandprefix_cache_hits_total, because vLLM's dashboards compute the hit rate from those. One difference: vLLM countsqueries when a request is scheduled, and here both counters move when a request finishes, like
prompt_tokens_total. If you'd rather not have this mirror, drop the secondif (keeps)block inrender, its twotest lines and one sentence in the docs.
docs/api.mddescribes the three families and when they show up.Nothing in the engines, the scheduler or the prompt pass changes. Each finished request does one more addition, and
each scrape reads
Infoonce.Receipt
I built
mainatf8fe17dand this branch the same way:zig build native -Dnvcc=/usr/local/cuda/bin/nvcc -Dsm=121 -Dversion=1.0.2, with nvcc 13.0.88 from thelmsysorg/sglang:v0.5.21image. Both used the 1.0.2 release'sshare/tensorfold/cuda/sm121throughTENSORFOLD_CUDA_KERNELS.TensorFold/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-MLX-4bit@d9d758fb83953437f7263256b0d96157e2a348b8,served with
--parallel 4 --context 16384 --temperature 0 --no-thinking --no-update-check, MTP drafts on.tools/bench_concurrent.py --alone --serialortoken_shaagainst"draft": false:--levels 1,4 --tokens 256 --reps 3 --temperatures 0 --alone --serialtwice on each binary. Everyconcurrent reply matched its solo run (30/30 each time), every solo run I checked matched
"draft": false(4/4),and nothing failed.
sha=values, each the same number of times.tools/bench_openai.py:--tokens 256 --reps 3 --temperatures 0, median tok/s. I ran main then the branch, then the branch then main.The four runs of each prompt are within 1.1% of each other. The 4-stream totals from
bench_concurrent.pyjumpedaround between 296 and 370 tok/s on both binaries, so I wouldn't read anything into them.
tools/prefill_cold.py:tools/prefill_cold.pyis no longer onmain, and this change doesn't touch the prompt pass. Median TTFTfrom
bench_openai.pywas 36 to 37 ms and 52 to 53 ms on both binaries.zig build test test-golden -Dcpu=apple_m1: 95/95 steps, 385/387 tests passed, 2 skipped. Goldens: "312/319equal to the frozen Python answers, 0 unexpected differences", same as
main.zig build test-native test-flashnext-host -Dcpu=apple_m1: 45/45 steps, 6/6 tests passed.python3 tools/zig/lean_check.py: 512 problems, same asmain. The one existingmetrics.zigmulti-linecomment finding moved from line 288 to 315.
render counts restored prompt tokens where the engine keeps prompt states, fails ifrenderleavesthe family out and passes with this change. The existing render test also checks now that
prefix_cache_hits_totalisn't there when the engine keeps no prompt states./metricshad the same families andprompt_tokens_total2032. Neither had the newfamilies, which is right, since Nemotron keeps no prompt states. The same 466-token request sent twice came back
with
cached_tokens: 0both times.have one. So I haven't seen the counter go above zero on a live server. It counts the same number those engines
already report as
cached_tokensinusageand ascached=in the finish log. On one of those machines you cancheck it by sending the same conversation twice and comparing
prompt_tokens_cached_totalwith the secondreply's
cached_tokens.Checklist
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.Drafted and concurrent were checked on GB10 Nemotron. This change doesn't touch the resume path.