Skip to content

Include spec stats in metrics#4625

Open
RunningLeon wants to merge 2 commits into
InternLM:mainfrom
RunningLeon:spec-logger
Open

Include spec stats in metrics#4625
RunningLeon wants to merge 2 commits into
InternLM:mainfrom
RunningLeon:spec-logger

Conversation

@RunningLeon
Copy link
Copy Markdown
Collaborator

Motivation

Include spec stats in metrics
get spec stats info from service

curl -s http://127.0.0.1:23333/metrics | grep 'lmdeploy:spec_decode'

Modification

Please briefly describe what modification is made in this PR.

BC-breaking (Optional)

Does the modification introduce changes that break the backward-compatibility of the downstream repositories?
If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR.

Use cases (Optional)

If this PR introduces a new feature, it is better to list some use cases here, and update the documentation.

Checklist

  1. Pre-commit or other linting tools are used to fix the potential lint issues.
  2. The modification is covered by complete unit tests. If not, please add more unit tests to ensure the correctness.
  3. If the modification has a dependency on downstream projects of a newer version, this PR should be tested with all supported versions of downstream projects.
  4. The documentation has been modified accordingly, like docstring or example tutorials.

Copilot AI review requested due to automatic review settings May 27, 2026 13:21
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds Prometheus metrics for speculative decoding (spec decode) so that spec decode statistics can be scraped from the service /metrics endpoint.

Changes:

  • Add spec decode Prometheus counters and gauges to PrometheusStatLogger.
  • Implement record_specdecode() to export spec decode totals and derived acceptance metrics.
  • Add a unit test validating that spec decode metrics are emitted as expected.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
lmdeploy/metrics/loggers.py Adds Prometheus metric definitions and record_specdecode() implementation to export spec decode stats.
tests/test_lmdeploy/test_metrics_loggers.py Adds a pytest test covering the newly exported spec decode metrics.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +206 to +210
# Speculative decoding counters. Acceptance rate and mean acceptance
# length are derived in PromQL from these raw counters.
# rate(accepted_tokens) / rate(draft_tokens)
# 1 + rate(accepted_tokens) / rate(drafts)
self.counter_spec_decode_num_drafts = prometheus_client.Counter(
Comment on lines +389 to +406
@staticmethod
def _get_counter_value(counter) -> float:
"""Get the current value from a prometheus counter child."""
return counter._value.get()

def record_specdecode(self, stats: SpeculativeDecodingStats) -> None:
pass
"""Report speculative decoding metrics to prometheus."""
if stats.num_drafts <= 0:
return

self.counter_spec_decode_num_drafts.inc(stats.num_drafts)
self.counter_spec_decode_num_draft_tokens.inc(stats.num_draft_tokens)
self.counter_spec_decode_num_accepted_tokens.inc(stats.num_accepted_tokens)

num_drafts = self._get_counter_value(self.counter_spec_decode_num_drafts)
num_draft_tokens = self._get_counter_value(self.counter_spec_decode_num_draft_tokens)
num_accepted_tokens = self._get_counter_value(self.counter_spec_decode_num_accepted_tokens)
mean_accept_rate = num_accepted_tokens / num_draft_tokens if num_draft_tokens > 0 else 0.0
@lvhan028 lvhan028 requested a review from CUHKSZzxy May 28, 2026 02:55
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