Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/features/speculative_decoding/acceptance_metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ A `summary` response's `metrics` looks like:
"mean_acceptance_length": 1.2325581395348837,
"draft_acceptance_rate": 0.07751937984496124,
"acceptance_histogram": [39, 1, 0, 3],
"acceptance_histogram_by_draft_length": {"3": [39, 1, 0, 3]},
"num_spec_steps": 43,
"num_accepted_draft_tokens": 10,
"num_draft_tokens": 129,
Expand All @@ -62,11 +63,18 @@ A `summary` response's `metrics` looks like:
| `mean_acceptance_length` | Mean tokens emitted per verification step, including the bonus token: `1 + num_accepted_draft_tokens / num_spec_steps`. Ranges from `1.0` (nothing accepted) to `num_spec_tokens + 1`. |
| `draft_acceptance_rate` | Fraction of proposed draft tokens accepted: `num_accepted_draft_tokens / num_draft_tokens`. |
| `acceptance_histogram` | Dense list of length `num_spec_tokens + 1`; index `j` is the number of steps that accepted exactly `j` draft tokens. Excludes the always-accepted bonus token. |
| `acceptance_histogram_by_draft_length` | Maps each observed proposed draft length `k` to a dense list of `k + 1` accepted-count buckets. The proposed length excludes grammar-invalidated drafts; JSON object keys are strings. |
| `num_spec_steps` | Number of verification steps for this request (the sum of the histogram). |
| `num_accepted_draft_tokens` | Total accepted draft tokens, excluding bonus tokens. |
| `num_draft_tokens` | Total proposed draft tokens, after subtracting drafts invalidated by structured-output constraints. |
| `num_spec_tokens` | Configured `num_speculative_tokens` (`k`), i.e. the maximum draft length per step. |

For variable-length drafting, `acceptance_histogram_by_draft_length[k][j]`
counts steps that accepted `j` of `k` proposed drafts. This preserves whether
acceptance reached the proposal limit at each budget without retaining every
step. Only observed draft lengths allocate rows, so storage is bounded by the
configured draft length rather than the number of verification steps.

With `detailed`, two ordered arrays are added, one entry per verification step:

| Field | Description |
Expand Down
33 changes: 33 additions & 0 deletions tests/v1/spec_decode/test_request_acceptance.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
response as ``metrics.speculative_decoding``.
"""

from collections import Counter

import msgspec
import pytest

Expand Down Expand Up @@ -56,6 +58,7 @@ def test_to_dict_summary_omits_per_step_arrays():
"mean_acceptance_length": pytest.approx(1 + 9 / 5), # j+1
"draft_acceptance_rate": pytest.approx(9 / 15),
"acceptance_histogram": [1, 1, 1, 2], # dense, index j = step count
"acceptance_histogram_by_draft_length": {3: [1, 1, 1, 2]},
"num_spec_steps": 5,
"num_accepted_draft_tokens": 9,
"num_draft_tokens": 15,
Expand Down Expand Up @@ -124,6 +127,7 @@ def test_engine_core_output_round_trips_spec_decode_metrics():
assert decoded.spec_decode_metrics.histogram == [1, 0, 1, 1]
assert decoded.spec_decode_metrics.num_draft_tokens == 9
assert decoded.spec_decode_metrics.per_step_accepted == [0, 3, 2]
assert decoded.spec_decode_metrics.histogram_by_draft_length == {3: [1, 0, 1, 1]}

without = EngineCoreOutput(request_id="r2", new_token_ids=[1])
decoded_without = decoder.decode(msgspec.msgpack.encode(without))
Expand All @@ -148,3 +152,32 @@ def test_completion_output_carries_spec_decode_metrics():
out = _completion_output(spec_decode_metrics=metrics)
assert out.spec_decode_metrics is metrics
assert _completion_output().spec_decode_metrics is None


def test_summary_distinguishes_draft_budgets_with_equal_aggregate_counts():
first = _metrics([(1, 1), (3, 2)]).to_dict()
second = _metrics([(2, 1), (2, 2)]).to_dict()
assert first["acceptance_histogram"] == second["acceptance_histogram"]
assert first["num_draft_tokens"] == second["num_draft_tokens"] == 4
assert first["acceptance_histogram_by_draft_length"] == {
1: [0, 1],
3: [0, 0, 1, 0],
}
assert second["acceptance_histogram_by_draft_length"] == {2: [0, 1, 1]}


def test_budget_histograms_match_detailed_pairs_without_storing_steps():
pairs = [(0, 0), (1, 0), (1, 1), (3, 2), (3, 3)] * 100
summary = _metrics(pairs)
detailed = _metrics(pairs, detailed=True)
expected = Counter(zip(detailed.per_step_drafted, detailed.per_step_accepted))
actual = {
(k, j): count
for k, counts in summary.histogram_by_draft_length.items()
for j, count in enumerate(counts)
if count
}
assert actual == expected
assert summary.histogram_by_draft_length == detailed.histogram_by_draft_length
assert sum(map(len, summary.histogram_by_draft_length.values())) == 7
assert summary.per_step_accepted == summary.per_step_drafted == []
2 changes: 2 additions & 0 deletions vllm/entrypoints/generate/base/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class SpeculativeDecodingMetrics(OpenAIBaseModel):
# exactly j draft tokens (length num_spec_tokens + 1). Excludes the
# always-accepted bonus token.
acceptance_histogram: list[int]
# Actual proposed length k maps to a dense accepted-count histogram of k + 1.
acceptance_histogram_by_draft_length: dict[int, list[int]]
num_spec_steps: int
num_accepted_draft_tokens: int
num_draft_tokens: int
Expand Down
11 changes: 11 additions & 0 deletions vllm/v1/metrics/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ class RequestSpecDecodeMetrics:
also the histogram's upper bound.
histogram: Dense counts indexed by accepted draft tokens ``j``
(length ``num_spec_tokens + 1``).
histogram_by_draft_length: Accepted-count histograms keyed by actual
proposed draft length, after the grammar-invalidated adjustment.
num_draft_tokens: Total proposed draft tokens, after the
grammar-invalidated (``num_invalid_spec_tokens``) adjustment.
per_step_accepted: Ordered accepted-draft count per verify step
Expand All @@ -330,6 +332,7 @@ class RequestSpecDecodeMetrics:
num_draft_tokens: int = 0
per_step_accepted: list[int] = field(default_factory=list)
per_step_drafted: list[int] = field(default_factory=list)
histogram_by_draft_length: dict[int, list[int]] = field(default_factory=dict)

@classmethod
def new(cls, num_spec_tokens: int) -> "RequestSpecDecodeMetrics":
Expand All @@ -343,6 +346,11 @@ def observe(
) -> None:
self.histogram[num_accepted] += 1
self.num_draft_tokens += num_draft_tokens
histogram = self.histogram_by_draft_length.get(num_draft_tokens)
if histogram is None:
histogram = [0] * (num_draft_tokens + 1)
self.histogram_by_draft_length[num_draft_tokens] = histogram
histogram[num_accepted] += 1
if detailed:
self.per_step_accepted.append(num_accepted)
self.per_step_drafted.append(num_draft_tokens)
Expand All @@ -364,6 +372,9 @@ def to_dict(self) -> dict[str, Any]:
"mean_acceptance_length": mean_al,
"draft_acceptance_rate": rate,
"acceptance_histogram": list(self.histogram),
"acceptance_histogram_by_draft_length": {
k: list(counts) for k, counts in self.histogram_by_draft_length.items()
},
"num_spec_steps": num_spec_steps,
"num_accepted_draft_tokens": num_accepted,
"num_draft_tokens": self.num_draft_tokens,
Expand Down
Loading