Skip to content

feat(metrics): /health's figures on /metrics: running tokens, cached prompt tokens, rounds, prefill time - #407

Closed
MiaAI-Lab wants to merge 1 commit into
ashhart:mainfrom
MiaAI-Lab:pr/health-metrics
Closed

MiaAI-Lab wants to merge 1 commit into
ashhart:mainfrom
MiaAI-Lab:pr/health-metrics

Conversation

@MiaAI-Lab

Copy link
Copy Markdown
Contributor

/health reports figures that /metrics leaves out, so a Prometheus scraper that wants them has to poll /health
beside it and parse JSON. This puts them on /metrics, on both HTTP layers (the CUDA server and the Mac server),
under the tensorfold: prefix and with the vLLM mirror where vLLM has a family for the same reading. It builds on the
/metrics module from #110 (@ashhart), its vLLM names from #156 (@jeidbugs404) and request_decode_seconds from
#269 (@sxuff), and follows their rule: a family the server cannot count is left out, never sent as a zero.

What /metrics gains

Family Type CUDA server Mac server /health field it matches
generation_tokens_running gauge the running replies' tokens so far the same, per HTTP thread completion_tokens_total minus the finished replies' tokens
prompt_tokens_cached_total counter engine stats cached the job's cached_tokens cached_tokens_total
decode_rounds_total counter engine stats rounds the stream's rounds rounds_total
request_prefill_seconds (mirror request_prefill_time_seconds) histogram engine stats prefill_s the job's started_at to prefilled_at (the done line's prefill=) prefill_seconds_total is its _sum
requests_running_max gauge the concurrent scheduler's max_streams; left out on one-request engines max_batch_size (the lane count) streams.max, max_batch_size

Why each one:

  • generation_tokens_running. generation_tokens_total counts finished requests only, so a long reply shows up
    as one jump when it ends, and a decode-rate panel reads zero for minutes. generation_tokens_total + generation_tokens_running counts tokens as they are generated, as the CUDA /health's completion_tokens_total
    does (the TUI's telemetry reads /health for exactly this today). It is a gauge, so the existing counter keeps its
    meaning.
  • prompt_tokens_cached_total, as @Acermax asked for on feat: the CUDA server publishes live token totals on /health #79, under that name. It is also vLLM's own name
    (vllm:prompt_tokens_cached), so no mirror is needed. Over prompt_tokens_total it is the prompt-cache hit rate.
  • decode_rounds_total. generation_tokens_total over it is the tokens a round, the draft payoff that
    mtp_accepted_total alone does not give.
  • request_prefill_seconds. prompt_tokens_total minus the cached tokens, over its _sum, is the prompt rate.
    vLLM's dashboard reads request_prefill_time_seconds_sum, which the mirror fills.
  • requests_running_max, so a saturation panel (requests_running / requests_running_max) needs no
    configuration.

Left out on purpose: requests_total (every histogram's _count already counts finished requests),
decode_seconds_total (request_decode_seconds_sum), the drafted and accepted totals (mtp_*_total), the context
window (configuration, not a reading), and the Mac /health's MLX memory split (active, cache, peak): that is
Metal-only, which we could not run.

How

  • server/metrics.py (383 to 455 lines): Metrics keeps the cached-token and round counters and a prefill
    histogram, and a small table of the Mac server's running requests' tokens by HTTP thread (begin and tokens write
    it, finish_request drops it). note takes cached, rounds and prefill. On the CUDA server, render sums the
    live requests' tokens from /health's own Health.live, under its lock.
  • cuda/health.py: the fold that feeds /health hands the engine's cached, rounds and prefill_s to note.
  • docs/api.md: the new families.

No engine, scheduler or request path changes: the new code runs when a request ends, when a Mac request's token chunk
arrives (one dictionary write under the metrics lock), and at scrape time.

Related

#367 (@juliankang4, a TPOT histogram) also changes server/metrics.py, cuda/health.py and tests/test_metrics.py.
The two add different families, so whichever lands second may need a small merge where both extend metrics.note.

Receipt

Environment. TensorFold 0.6.5 (609ca41) against this branch on it. NGC PyTorch 26.07 container (PyTorch
2.13.0a0+9186a08, CUDA 13.3, Python 3.12), pip install "tensorfold[test] @ git+...@609ca41", the branch's
src/tensorfold mounted over the installed package. Two DGX Sparks (GB10, 128 GB each), --tp 2 over their CX7
cable (NCCL on both RoCE rails, NCCL_IB_HCA as the RUNBOOK says). Checkpoint Mia-AiLab/GLM-5.3-Flash-EXL3-4bpw-TensorFold
at 078455ff; serve arguments --tp 2 --context 131072, otherwise the defaults (--drafter auto picked the MTP head
and incoai/GLM-5.3-Flash-DFlash2 at bf582e4 from the cache). Before and after ran on the same two machines in the
same session, one boot each.

/metrics against /health, live (a 600-token greedy reply, scraped 4 s in and after it; the same counters on
0.6.5 for contrast):

0.6.5 /metrics this branch /metrics /health
during: tokens generated so far generation_tokens_total 19,770 (finished only) 19,770 + generation_tokens_running 259 = 20,029 completion_tokens_total 20,029
after: cached prompt tokens (none) prompt_tokens_cached_total 1,501 cached_tokens_total 1,501
after: decode rounds (none) decode_rounds_total 10,946 rounds_total 10,946
after: prefill seconds (none) request_prefill_seconds_sum 811.775115 (_count 119) prefill_seconds_total 811.775115 (requests_total 119)

request_prefill_time_seconds carried the same sum and count. requests_running_max is left out here, as it should
be: GLM on CUDA serves one request at a time and has no limit to read.

Exactness (python3 tools/bench_concurrent.py http://127.0.0.1:8877 glm-flash --alone --serial, 4 streams,
256 tokens, 3 reps): every concurrent reply equal to its solo run and every solo run equal to "draft": false, on
both trees.

cell 0.6.5 alone / serial this branch alone / serial
code, T=1.0 12/12, 4/4 12/12, 4/4
chat, T=1.0 12/12, 4/4 12/12, 4/4
code, T=0 12/12, 1/1 12/12, 1/1
chat, T=0 12/12, 1/1 12/12, 1/1

A greedy 64-token reply had the same token_sha (ba2f337c82d8) on both trees.

Decode (python3 tools/bench_openai.py http://127.0.0.1:8877 glm-flash, 64 tokens, 5 reps, median tok/s):

Boots in this order: 0.6.5, this branch, a third tree that does not touch this code (#301's branch, twice), 0.6.5
again.

prompt 0.6.5 (1st) this branch other tree other tree 0.6.5 (2nd)
code, T=1.0 34.90 35.31 35.34 35.19 35.23
chat, T=1.0 26.08 26.17 26.24 26.16 26.13
code, T=0 41.41 41.56 41.28 41.40 41.35
chat, T=0 23.86 23.84 23.79 23.72 23.77

The sampled cells' five reps have the same shape in every run (code, T=1.0: 36.1-36.4, 26.1-26.3, 34.9-35.3,
35.4-35.6, 31.8-32.2), the drafts' acceptance on those seeds, so the medians compare directly. The first 0.6.5 run had
CPU tests running beside rank 1; the rest did not.

Prompt speed (tools/prefill_cold.py, cold prompts, median of 3, tok/s):

prompt 0.6.5 this branch
2k 490.7 479.6
8k 461.8 463.8
16k 472.6 460.0
32k 476.9 460.2
64k 491.4 469.1

Per rep (time to first token, s), with the third tree's boot (#301's branch, which changes no prompt code either):

prompt 0.6.5 this branch third tree
16k 34.66, 34.49, 35.08 35.89, 34.65, 35.60 35.23, 34.54, 35.11
32k 68.69, 68.78, 68.53 68.97, 71.94, 71.19 69.73, 70.11, 69.57
64k 131.28, 138.02, 133.35 132.40, 144.62, 139.69 131.43, 138.42, 133.47

This branch's medians read 0-4.5% below 0.6.5 at 16k-64k, from two or three slow reps (32k: 71.9 and 71.2 s,
64k: 144.6 s); its fastest reps match 0.6.5's. Nothing in this change runs during a prompt pass (the new code runs
when a request ends and at scrape time, and prefill_cold scrapes nothing), so we read it as boot-to-boot noise, but
we had one boot of each and could not run the alternating order the contributing guide asks for at a few percent.
Please rerun if it matters.

Tests (CPU, the NGC 26.07 container, no GPU):

  • tests/test_metrics.py tests/test_metrics_footprint_host.py tests/test_cuda_server_health.py tests/test_health_memory.py: 34 passed, 1 skipped, 1 failed on this branch. The failure is
    test_health_memory.py::test_health_reports_the_budget_and_the_process_footprint, which reads macOS's process
    footprint and fails the same way on 0.6.5 on Linux.
  • tests/test_metrics.py against 0.6.5's source: 6 of its tests fail there (the five new ones, and the existing
    scrape test whose list of families now names the new ones), and all pass here.
  • the full suite, python -m pytest -q -p no:randomly --timeout 120 tests in the NGC 26.07 container on an aarch64 CPU (no GPU, no MLX): 4,052 passed, 135 failed, 11 errors, 172 skipped on this branch, against 4,047 passed and the same 135 failed, 11 errors and 172 skipped on 0.6.5. The failures and errors are the same tests on both trees: they need MLX or macOS (test_lane_server.py, test_request_reasoning.py, test_prompt_fill.py and the like), and one, test_cancellation.py::test_streaming_socket_disconnect_during_decode_releases_the_active_row, loops on recv there until pytest-timeout's 120 s stops it (without the timeout the suite hangs at that test). The five more passing are the new tests.

New tests: both servers' new families from their own counters, the Mac server's running tokens across two threads,
the CUDA HTTP layer's /metrics against its /health during and after a request, the limit only where an engine has
one, and the prefill mirror.

Not run. Metal: we have no Mac. The Mac server's new families are covered by the CPU tests with stand-in jobs
and streams, not by a Mac chat; test_a_mac_chat_counts_the_reply_the_http_thread_returns (MLX) skips here. The
concurrent CUDA engines (--parallel N > 1 on Qwen and Flash Next), where requests_running_max and several running
streams show up, ran only in the tests, not on a server.

…prompt tokens, rounds, prefill time

/health reports figures /metrics left out, so a Prometheus scraper had to poll /health beside it. Both HTTP
layers now publish them, where each server counts them:

- generation_tokens_running (gauge): the running replies' tokens so far. Added to generation_tokens_total it
  counts tokens as they are generated, as /health's completion_tokens_total does on CUDA.
- prompt_tokens_cached_total: prompt tokens read from a kept prompt state (vLLM's name too).
- decode_rounds_total: decode rounds; generation_tokens_total over it is the tokens a round.
- request_prefill_seconds, mirrored as vLLM's request_prefill_time_seconds: each finished request's prompt
  pass (the CUDA engine's prefill_s; on the Mac server the job's start to its prefill's end).
- requests_running_max (gauge), where the engine has a limit to read: the concurrent CUDA scheduler's
  max_streams, the Mac server's lane count.

CUDA reads them from the engine stats /health already folds and from /health's own live requests; the Mac
server from its job and stream. Tests: tests/test_metrics.py (both servers, the HTTP layer, the mirror).
ashhart added a commit that referenced this pull request Oct 6, 2026
…367, #407)

/metrics gains four readings. request_time_per_output_token_seconds (vLLM's
TPOT) is, for each finished reply, the server's time from its first generated
token to its last over its tokens less one, in buckets from 2.5 ms to 1 s; a
one-token reply adds no sample. generation_tokens_running counts what live
replies have generated so far, from a snapshot the engine thread takes between
rounds, so a scrape never reads a stream the engine is changing.
decode_rounds_total counts the rounds finished replies used.
request_prefill_seconds, repeated as vLLM's request_prefill_time_seconds, is
each finished reply's prompt pass as the engine measured it, from the job's
start to the end of its prefill. The reply's tensorfold.prefill_seconds now
reports that same span, so it no longer includes time spent waiting for a lane.
The cached-prompt counter stays out until an engine counts kept-prefix hits.

Tests: the TPOT arithmetic, the bucket a sample lands in, and render() through
a stub engine (the new families present, no cached family). The scripted test
engine reports a prefill span, and the server parity check lists the four
/metrics cases as expected differences, since the Python server does not have
these families.

Co-authored-by: juliankang4 <[email protected]>
Co-authored-by: MiaAI-Lab <[email protected]>
@ashhart

ashhart commented Oct 6, 2026

Copy link
Copy Markdown
Owner

Thank you, @MiaAI-Lab. This landed in the Zig engine as 8debb38, with your credit.

The Zig /metrics now has generation_tokens_running, decode_rounds_total and the request_prefill_seconds histogram, mirrored as request_prefill_time_seconds for vLLM's dashboards. prompt_tokens_cached_total stays out under your own rule. The Zig engine has no prompt cache yet, so there is nothing to count, and that family comes with the cache.

The Python engine is frozen (#286), so this PR closes with that. Thanks for tying every family to the /health figure it matches.

@ashhart ashhart closed this Oct 6, 2026
@ashhart ashhart mentioned this pull request Oct 6, 2026
grearjake-star added a commit to grearjake-star/TensorFold that referenced this pull request Oct 8, 2026
… tokens, rounds, prefill time

/health reported figures that /metrics left out, so a Prometheus scraper had to poll /health beside it
and parse JSON. /metrics now carries generation_tokens_running (the running replies' tokens so far),
prompt_tokens_cached_total (prompt tokens read from a kept prompt state), decode_rounds_total,
request_prefill_seconds (mirrored as vLLM's request_prefill_time_seconds) and, where the engine has a
limit to read, requests_running_max. A family the server cannot count is left out, never sent as zero.
Merged beside this tree's TPOT histogram (ashhart#367); the macOS footprint gauge stays out with the Mac
server. Observability only.

Upstream: ashhart#407, closed unmerged with the Python engine frozen; the maintainer landed
the same families in the Zig server.

Co-authored-by: MiaAI-Lab <[email protected]>
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…shhart#367, ashhart#407)

/metrics gains four readings. request_time_per_output_token_seconds (vLLM's
TPOT) is, for each finished reply, the server's time from its first generated
token to its last over its tokens less one, in buckets from 2.5 ms to 1 s; a
one-token reply adds no sample. generation_tokens_running counts what live
replies have generated so far, from a snapshot the engine thread takes between
rounds, so a scrape never reads a stream the engine is changing.
decode_rounds_total counts the rounds finished replies used.
request_prefill_seconds, repeated as vLLM's request_prefill_time_seconds, is
each finished reply's prompt pass as the engine measured it, from the job's
start to the end of its prefill. The reply's tensorfold.prefill_seconds now
reports that same span, so it no longer includes time spent waiting for a lane.
The cached-prompt counter stays out until an engine counts kept-prefix hits.

Tests: the TPOT arithmetic, the bucket a sample lands in, and render() through
a stub engine (the new families present, no cached family). The scripted test
engine reports a prefill span, and the server parity check lists the four
/metrics cases as expected differences, since the Python server does not have
these families.

Co-authored-by: juliankang4 <[email protected]>
Co-authored-by: MiaAI-Lab <[email protected]>
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.

2 participants