Skip to content

feat(metrics): count prompt tokens restored from kept prompt states - #531

Merged
ashhart merged 1 commit into
ashhart:mainfrom
juliankang4:feat/zig-metrics-cached-tokens
Oct 10, 2026
Merged

ashhart merged 1 commit into
ashhart:mainfrom
juliankang4:feat/zig-metrics-cached-tokens

Conversation

@juliankang4

Copy link
Copy Markdown
Contributor

What this changes

Engines that keep prompt states between requests now publish tensorfold:prompt_tokens_cached_total on /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 by prompt_tokens_total and 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

  • Info has a new prompt_cache flag. flashnext_host sets it when it creates its prompt store, and glm_host sets
    it 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.generate already keeps the restored token count from the prefilled event for usage. It now passes that
    count to noteRequest too, and Metrics.note adds it up next to prompt.
  • When the flag is on, render writes prompt_tokens_cached_total right after prompt_tokens_total. Like the TPOT
    histogram, the name is already vLLM's (vllm:prompt_tokens_cached).
  • In the same case it also writes the two counters as vLLM's prefix_cache_queries_total and
    prefix_cache_hits_total, because vLLM's dashboards compute the hit rate from those. One difference: vLLM counts
    queries 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 second if (keeps) block in render, its two
    test lines and one sentence in the docs.
  • docs/api.md describes 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 Info once.

Receipt

  • Environment, with the TensorFold commit, MLX or PyTorch version, machine and GPU, checkpoint and revision:
    • Host tests on an M4 Pro, macOS 27.0.1, Zig 0.17.0, Xcode Metal toolchain 27A266a.
    • CUDA on one GB10 (DGX Spark class), driver 595.84, kernel 6.17.0-1032-nvidia, GPU clock locked at 2000 MHz.
      I built main at f8fe17d and 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 the
      lmsysorg/sglang:v0.5.21 image. Both used the 1.0.2 release's share/tensorfold/cuda/sm121 through
      TENSORFOLD_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.
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false:
    • I ran --levels 1,4 --tokens 256 --reps 3 --temperatures 0 --alone --serial twice on each binary. Every
      concurrent reply matched its solo run (30/30 each time), every solo run I checked matched "draft": false (4/4),
      and nothing failed.
    • The finish logs of both binaries have the same sha= values, each the same number of times.
  • Decode speed before and after, from tools/bench_openai.py:
    • --tokens 256 --reps 3 --temperatures 0, median tok/s. I ran main then the branch, then the branch then main.

      Prompt main, run 1 branch, run 1 branch, run 2 main, run 2
      fibonacci-raw 219.8 219.0 219.5 221.0
      gpu-chat-no-think 162.4 162.0 162.9 163.7
    • The four runs of each prompt are within 1.1% of each other. The 4-stream totals from bench_concurrent.py jumped
      around between 296 and 370 tok/s on both binaries, so I wouldn't read anything into them.

  • Prompt speed before and after, from tools/prefill_cold.py:
    • Not run. tools/prefill_cold.py is no longer on main, and this change doesn't touch the prompt pass. Median TTFT
      from bench_openai.py was 36 to 37 ms and 52 to 53 ms on both binaries.
  • Tests run, with pass, fail and skip counts:
    • zig build test test-golden -Dcpu=apple_m1: 95/95 steps, 385/387 tests passed, 2 skipped. Goldens: "312/319
      equal 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 as main. The one existing metrics.zig multi-line
      comment finding moved from line 288 to 315.
    • The new test, render counts restored prompt tokens where the engine keeps prompt states, fails if render leaves
      the family out and passes with this change. The existing render test also checks now that
      prefix_cache_hits_total isn't there when the engine keeps no prompt states.
    • On the GB10, both binaries' /metrics had the same families and prompt_tokens_total 2032. Neither had the new
      families, which is right, since Nemotron keeps no prompt states. The same 466-token request sent twice came back
      with cached_tokens: 0 both times.
  • Not run, and why:
    • Flash Next and GLM-5.3-Flash, the two engines that turn the flag on. They need one and two M5 Ultras, and I don't
      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_tokens in usage and as cached= in the finish log. On one of those machines you can
      check it by sending the same conversation twice and comparing prompt_tokens_cached_total with the second
      reply's cached_tokens.
    • Metal Nemotron on a Mac. It goes through the same server code as CUDA Nemotron and has no prompt cache either.

Checklist

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
  • Drafted output equals "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.
  • No precision traded for speed. If the bits change, the description says which sums change and why.
  • Prompt processing is no slower than the last release.
  • The description says which platforms I ran, Metal M1 to M5 and CUDA, and which I could not.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths. No AI attribution lines.

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.
@ashhart
ashhart merged commit 733c4f8 into ashhart:main Oct 10, 2026
@ashhart

ashhart commented Oct 10, 2026

Copy link
Copy Markdown
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. /metrics now fills a vLLM dashboard's prefix cache hit rate. Closes #530; it ships in the next release.

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.
@ashhart ashhart mentioned this pull request Oct 10, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
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.
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.

/metrics: no cached prompt token counter now that Flash Next and GLM-5.3-Flash keep prompt states

2 participants