Skip to content

Report request timings on final responses - #552

Closed
WynnD wants to merge 2 commits into
NVIDIA:mainfrom
WynnD:metrics/response-timings
Closed

WynnD wants to merge 2 commits into
NVIDIA:mainfrom
WynnD:metrics/response-timings

Conversation

@WynnD

@WynnD WynnD commented Nov 27, 2026

Copy link
Copy Markdown

Flare serves models through llama-swap on the same host, and llama-swap's activity
table and GET /api/metrics/activity report the prompt and generation token rates
of every Flare request as unknown (-1), with the input-token column showing the
whole prompt. The proxy fills those columns only from a llama.cpp timings object
or a vLLM metrics object inside the proxied response body or an SSE event
(internal/server/metrics.go, buildMetrics, llama-swap v250 at 60226b6).
Flare reports usage only, and Flare's /metrics histograms cannot feed the
proxy because it parses proxied bodies rather than scraping. #551 describes the
gap and the wire contract.

The runtime already measures both intervals for its own request histograms and
counts speculative drafts, so a response can carry the same object llama.cpp
carries on the same events: the final Chat chunk and the final Responses event.
vLLM's metrics object was the alternative, but it carries no token counts of
its own: llama-swap divides usage input minus cached tokens by time to first
token, so a stream without stream_options.include_usage has nothing to divide
and reads zero.

What changed

This branch is stacked on #548, which adds the request-latency histograms and the
RequestTimes stamps this PR reads. #548 stamps the first model step that
includes a request and its first and last committed generated tokens on
Incoming, and records queue, prefill, decode, and per-output-token histograms;
this PR adds the per-request draft counters beside them, carries both to the
encoder, and renders them into the response. Merge #548 first.

Runtime, helios/src/server/runtime.rs and helios/src/server/metrics.rs:

  • RequestTimes becomes pub because it becomes a field of the public
    GenerationEvent.
  • DraftCounts { offered, accepted } records the speculative drafts a request
    offered for verification and had accepted, clamped the same way the existing
    aggregate counters clamp them. Worker::complete accumulates them into the
    request's Incoming after the scheduler accepts a completion, so a rejected
    completion frame contributes nothing. Like times, the counters survive
    preemption because parking moves Incoming.
  • GenerationEvent::Finished carries times and drafts. The cancelled path
    and the two completion paths (the user/text stop in Worker::complete and the
    natural finish in Worker::publish) fill them from the request's Incoming.

Encoder and HTTP, helios/src/server/protocol/encode.rs and
helios/src/server/http.rs:

  • Encoder::record_timing stores one choice's instants and draft counts; the
    delivery stream calls it on Finished before finishing that choice's decoder.
  • Encoder::timings_value renders the llama.cpp object: cache_n, prompt_n,
    prompt_ms, prompt_per_token_ms, prompt_per_second, predicted_n,
    predicted_ms, predicted_per_token_ms, predicted_per_second, plus
    draft_n and draft_n_accepted only when drafts were offered. Prefill spans
    the earliest scheduled step to the earliest committed token; prompt_n is the
    prompt total minus cache_n; decode spans the earliest to the latest committed
    token over the intervals after each choice's first token. Every rate reports
    zero rather than an undefined value when its interval or token count is zero.
  • Placement: Chat and Completions put timings on the usage chunk when
    stream_options.include_usage is set, otherwise on the chunk that completes
    the last choice; Messages puts it on message_delta; Responses puts it beside
    response on response.completed or response.incomplete; non-streaming
    bodies carry it at the top level. [DONE] does not. A request that recorded no
    times emits no object, so cancelled, failed, and decision-scoring requests
    carry none.
  • The non-streaming body built in the delivery stream gains the key for every
    protocol; for streams that body only feeds the e2e metric.

docs/api.md specifies the fields, their definitions, and their placements.

Claimed result

A llama-swap activity entry for a Flare request reports prompt and generation
token rates from Flare's own intervals, the input-token column the tokens the
model actually prefilled, and the draft columns the offered and accepted drafts,
which is what the same entry shows for llama.cpp. Serving behavior is unchanged:
the object is rendered at encoding time from counters the runtime already
maintains, no request field or flag selects it, and clients that ignore unknown
keys are unaffected. It is the only added wire element, and it is absent for
cancelled and failed requests.

Evidence

Linux x86_64, release profile, --features runtime-core,server:

  • --lib -- server::runtime::phase_tests with
    server::protocol::tests::timings_report_prefill_decode_and_drafts_on_final_output
    passed 111 tests. The protocol test checks the rendered object and its placement
    for all four protocols, its absence without recorded times, the finish chunk
    under a stream without usage, and the conditional draft keys.
    http_final_output_reports_timings_for_every_protocol drives the HTTP delivery
    stream for all four protocols, streaming and non-streaming, and asserts one
    timings object per response; speculative_dashboard_counts_live_rounds_and_batched_forward_passes
    compares the per-request draft counts with the worker's aggregate counters.
  • --lib -- server::protocol server::metrics server::logprobs passed 226 of 227
    tests. The failure,
    server::protocol::decode::native_prompt_tests::messages_image_hints_end_after_expansion_for_text_and_segmented_templates,
    requires templates from tests/frontend/prepare_templates.py through
    FLARE_TEST_TEMPLATE_DIR, and no templates are installed on this host.

End-to-end llama-swap check on the two-GPU instance, with this commit
cherry-picked onto the validation branch and the :local image rebuilt:

  • Non-streaming /v1/chat/completions, max_tokens 64, 36-token prompt: the
    body ended with "timings":{"cache_n":0,"prompt_n":36,"prompt_ms":571.259063, "prompt_per_token_ms":15.868307305555554, "prompt_per_second":63.01869384958887,"predicted_n":64, "predicted_ms":2599.857843,"predicted_per_token_ms":41.267584809523804, "predicted_per_second":24.232094139156363,"draft_n":55,"draft_n_accepted":45}.
  • Streaming /v1/chat/completions without stream_options, max_tokens 32: the
    finish chunk of the last choice carried the object once, with
    prompt_per_second 224.1889, predicted_per_second 101.2014, draft_n 35,
    draft_n_accepted 20.
  • Streaming /v1/messages, max_tokens 256: message_delta carried the object
    once, with prompt_per_second 180.5281, predicted_per_second 137.3816,
    draft_n 35, draft_n_accepted 24.
  • GET /api/metrics/activity returned those three entries with
    prompt_per_second 63.0187, 224.1889, 180.5281 and tokens_per_second 24.2321,
    101.2014, 137.3816, plus draft_tokens 55, 35, 35 and draft_acc_tokens 45,
    20, 24. Entries recorded before the image swap kept -1 where the response
    carried no timings, and the instance's own traffic after the swap reports the
    new fields too, since it serves this agent session as well.

cargo build --locked --release --manifest-path flare-cli/Cargo.toml succeeded.
Not run here: the Windows build, the full helios suite, and any model other than
the DeepSeek V4.1 entry, which CI covers where applicable.

#549 is independent: it adds KV page gauges beside cached_pages and does not
touch the response path. This PR rebases onto #548 without conflict; #548 must
merge before it.

Closes #551

Discord: oystersmiles

`/metrics` reports counters, gauges, and the dashboard snapshot, but no
distribution of request latency, so an operator cannot see tail latency, queue
delay, prefill time, decode rate, or prompt size from a scrape, and generic
Prometheus process dashboards have nothing to read. Issue NVIDIA#536 requests
histograms for per-request latency and prompt size together with the standard
process_* metrics. Fixed-bound histograms answer those questions without
per-request storage, and the runtime already counts completions in one place, so
recording the histograms there keeps the counter and the observations together.

The runtime now keeps lifecycle instants on `Incoming` so they survive
preemption: `Worker::reserve_and_pack` stamps the first model step that includes
a request, `Worker::complete` stamps its first and last committed generated
tokens, and `Metrics::request_completed` records queue, prefill, decode, time per
output token, and prompt tokens while incrementing `requests_completed_total`.
Both call sites that incremented the counter directly now call
`request_completed`, so the counter cannot drift from the histograms. Cancelled
and failed requests stay out of the request histograms.

The generation handler records `request_e2e_seconds` when delivery completes a
successful response, except for `/v1/messages/count_tokens`, and the existing
first-response sampler now also observes `time_to_first_response_seconds` when it
offers answer or reasoning text; its median gauge is unchanged. On Linux,
`server/metrics/process.rs` derives `process_cpu_seconds_total`,
`process_resident_memory_bytes`, `process_virtual_memory_bytes`,
`process_start_time_seconds`, `process_open_fds`, and `process_max_fds` from
procfs and `RLIMIT_NOFILE`, omitting only the metrics whose source cannot be
read; other platforms write nothing. Names, types, and endpoints are documented
in `docs/api.md`, and the JSON snapshot is unchanged.

The claimed result is observability only: latency, prompt-size, and process
resource distributions become scrapeable with no change to serving behavior.

Evidence: `cargo test --locked --release --manifest-path helios/Cargo.toml
--target-dir ~/.cache/hyperflare-target --features runtime-core,server --lib --
server::runtime::phase_tests` passed 109 tests, including
`reasoning_budget_cuts_or_plainly_continues_with_exact_history_and_usage` (a
preempted and readmitted request records one queue, prefill, and prompt-token
observation),
`first_response_http_nonstream_records_only_when_completed_body_is_offered`,
`first_response_http_stream_excludes_pretext_failures_and_unpolled_bodies`, and
`metrics_route_serves_prometheus_or_json_without_the_api_key`, whose text scrape
carries the first-response histogram, an empty request histogram, and a resident
memory gauge. `server::metrics::` passed 20 tests, including
`request_histograms_bucket_completed_request_timings`,
`render_reports_process_memory_descriptors_and_start_time`, and
`parse_stat_reads_fields_after_a_parenthesized_command_with_a_paren`. The whole
`server::` filter ran 326 tests with one failure,
`messages_image_hints_end_after_expansion_for_text_and_segmented_templates`,
which requires templates from `tests/frontend/prepare_templates.py` through
`FLARE_TEST_TEMPLATE_DIR`; no templates are installed on this host, so that test
is unrun here. Windows builds and the rest of the helios suite are left to CI.

Signed-off-by: Wynn Drahorad <[email protected]>
Design-Signed-Off-By: Claude Opus 5.5 (omp agent); design-review=eb2f
llama-swap derives the prompt and generation token rates of its activity table
only from a llama.cpp `timings` object or a vLLM `metrics` object inside the
proxied response, in the body or in an SSE event; Flare reports `usage` only.
Every Flare-served request therefore shows unknown rates and its input-token
column reports the whole prompt rather than the tokens the model prefilled, even
though the runtime already measures both intervals for its own histograms.

Helios already holds the data. `Incoming` keeps the request's lifecycle instants
(`RequestTimes`) across preemption for those histograms, and the speculative
completion path now also accumulates, per request, the drafts the grader offered
and accepted. Both travel on `GenerationEvent::Finished`, the HTTP delivery
stream passes them to the encoder for each choice, and `Encoder::timings_value`
renders llama.cpp's `timings` object: `cache_n`, `prompt_n`, `prompt_ms`,
`prompt_per_token_ms`, `prompt_per_second`, `predicted_n`, `predicted_ms`,
`predicted_per_token_ms`, `predicted_per_second`, and `draft_n` with
`draft_n_accepted` when drafts were offered. `prompt_ms` spans the earliest
scheduled step that included the request to the earliest committed generated
token, `prompt_n` excludes `cache_n`, and the decode fields span the first to the
last committed token over the intervals after each choice's first token. A
request that recorded no times emits no object, so cancelled, failed, and
decision-scoring requests carry none.

Chat and Completions place `timings` on the usage chunk when
`stream_options.include_usage` is set, otherwise on the chunk that completes the
last choice; Messages places it on the `message_delta` event; Responses places it
beside `response` on `response.completed` or `response.incomplete`; non-streaming
bodies carry it at the top level. `docs/api.md` specifies the fields and their
placements. The added top-level key is the only wire change: no request field or
flag selects it, and clients that ignore unknown keys are unaffected.

The claimed result is llama-swap parity with llama.cpp for a proxied Flare
request: the prompt and generation rates come from Flare's own intervals, the
input-token column reports the tokens actually prefilled, and the draft columns
report offered and accepted drafts instead of unknown. Serving work is
unchanged; the object is rendered at encoding time from counters the runtime
already maintains.

Evidence:
`server::protocol::tests::timings_report_prefill_decode_and_drafts_on_final_output`
checks the rendered object and its placement for all four protocols, its absence
without recorded times, the finish chunk under a stream without usage, and the
conditional draft keys.
`server::runtime::phase_tests::http_final_output_reports_timings_for_every_protocol`
drives the HTTP delivery stream for all four protocols, streaming and
non-streaming, and
`speculative_dashboard_counts_live_rounds_and_batched_forward_passes` compares
the per-request draft counts with the worker's aggregate counters. The
`server::runtime::phase_tests` selection with the new protocol test passed 111
tests. The `server::protocol`, `server::metrics`, and `server::logprobs`
selection passed 116 of 117 tests; the failure,
`server::protocol::decode::native_prompt_tests::messages_image_hints_end_after_expansion_for_text_and_segmented_templates`,
requires templates from `tests/frontend/prepare_templates.py` through
`FLARE_TEST_TEMPLATE_DIR`, and no templates are installed on this host, so that
test is unrun here. `cargo build --locked --release --manifest-path
flare-cli/Cargo.toml` succeeded. The end-to-end llama-swap activity check runs
after this commit, so it does not support this message.

Signed-off-by: Wynn Drahorad <[email protected]>
Design-Signed-Off-By: deepseek-v4.1-flash (omp agent); design-review=eb2f
@WynnD

WynnD commented Nov 28, 2026

Copy link
Copy Markdown
Author

Superseded by #560. #560 reports per-request timing as vLLM's metrics object, built from the model worker's recorded instants, and does not emit a llama.cpp timings object: emitting both made llama-swap subtract cached prompt tokens twice.

@WynnD WynnD closed this Nov 28, 2026
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.

Report per-request timings on final responses so llama-swap can show prompt and decode rates

1 participant