[Metrics] Expose cached prompt tokens by cache tier (#56318)
Signed-off-by: Cam Quilici <cjquilici@gmail.com> Signed-off-by: Cam Quilici <cameron@semianalysis.com> Co-authored-by: Cam Quilici <cameron@semianalysis.com> Co-authored-by: Nick Hill <nickhill123@gmail.com> Co-authored-by: Yifan Qiao <yifanqiao@inferact.ai>
This commit is contained in:
co-authored by
Cam Quilici
Nick Hill
Yifan Qiao
parent
ad0f67a7ec
commit
e37e51dd24
@@ -1,10 +1,13 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
from vllm.v1.core.sched.output import ScheduledEncoderInputStats, SchedulerOutput
|
||||
from vllm.v1.engine import EngineCoreOutputs, FinishReason
|
||||
from vllm.v1.metrics.cache_hit_source import CacheHitSource
|
||||
from vllm.v1.metrics.stats import (
|
||||
IterationStats,
|
||||
PrefillStats,
|
||||
PrefixCacheStats,
|
||||
PromptTokenStats,
|
||||
RequestStateStats,
|
||||
SchedulerIterationDetails,
|
||||
@@ -45,6 +48,96 @@ def test_scheduler_iteration_details_serialization():
|
||||
assert decoded.scheduler_stats.iteration_details == iteration_details
|
||||
|
||||
|
||||
def test_prefix_cache_stats_hits_by_source_accumulate():
|
||||
stats = PrefixCacheStats()
|
||||
stats.record(
|
||||
num_tokens=8,
|
||||
num_hits=5,
|
||||
preempted=False,
|
||||
hits_by_source={CacheHitSource.HOST: 3, CacheHitSource.DISK: 2},
|
||||
)
|
||||
stats.record(
|
||||
num_tokens=8,
|
||||
num_hits=6,
|
||||
preempted=False,
|
||||
hits_by_source={CacheHitSource.P2P: 5, CacheHitSource.HOST: 1},
|
||||
)
|
||||
# Zero counts never create an entry.
|
||||
stats.record(
|
||||
num_tokens=8,
|
||||
num_hits=4,
|
||||
preempted=False,
|
||||
hits_by_source={CacheHitSource.DISK: 4, CacheHitSource.P2P: 0},
|
||||
)
|
||||
|
||||
assert stats.hits_by_source == {
|
||||
CacheHitSource.HOST: 4,
|
||||
CacheHitSource.DISK: 6,
|
||||
CacheHitSource.P2P: 5,
|
||||
}
|
||||
assert sum(stats.hits_by_source.values()) == stats.hits == 15
|
||||
|
||||
|
||||
def test_prefix_cache_stats_record_hits_by_source():
|
||||
stats = PrefixCacheStats()
|
||||
stats.record(num_tokens=32, num_hits=16, preempted=False)
|
||||
stats.record(
|
||||
num_tokens=32,
|
||||
num_hits=24,
|
||||
preempted=False,
|
||||
hits_by_source={CacheHitSource.HOST: 16, CacheHitSource.DISK: 8},
|
||||
)
|
||||
# Preempted re-admissions never contribute to hits or the split.
|
||||
stats.record(
|
||||
num_tokens=32,
|
||||
num_hits=32,
|
||||
preempted=True,
|
||||
hits_by_source={CacheHitSource.P2P: 32},
|
||||
)
|
||||
# A connector miscount is reported as unspecified, not dropped or raised.
|
||||
stats.record(
|
||||
num_tokens=8,
|
||||
num_hits=8,
|
||||
preempted=False,
|
||||
hits_by_source={CacheHitSource.HOST: 4},
|
||||
)
|
||||
|
||||
assert stats.requests == 3
|
||||
assert stats.hits == 48
|
||||
assert stats.preempted_hits == 32
|
||||
assert stats.hits_by_source == {
|
||||
CacheHitSource.HOST: 16,
|
||||
CacheHitSource.DISK: 8,
|
||||
CacheHitSource.EXTERNAL_UNSPECIFIED: 8,
|
||||
}
|
||||
assert sum(stats.hits_by_source.values()) == 48 - 16
|
||||
|
||||
|
||||
def test_prefix_cache_stats_hits_by_source_serialization():
|
||||
connector_stats = PrefixCacheStats()
|
||||
connector_stats.record(
|
||||
num_tokens=16,
|
||||
num_hits=8,
|
||||
preempted=False,
|
||||
hits_by_source={CacheHitSource.P2P: 4, CacheHitSource.HOST: 4},
|
||||
)
|
||||
outputs = EngineCoreOutputs(
|
||||
scheduler_stats=SchedulerStats(
|
||||
connector_prefix_cache_stats=connector_stats,
|
||||
)
|
||||
)
|
||||
|
||||
encoded = MsgpackEncoder().encode(outputs)
|
||||
decoded = MsgpackDecoder(EngineCoreOutputs).decode(encoded)
|
||||
|
||||
assert decoded.scheduler_stats is not None
|
||||
assert decoded.scheduler_stats.connector_prefix_cache_stats is not None
|
||||
assert decoded.scheduler_stats.connector_prefix_cache_stats.hits_by_source == {
|
||||
CacheHitSource.P2P: 4,
|
||||
CacheHitSource.HOST: 4,
|
||||
}
|
||||
|
||||
|
||||
def test_compute_iteration_details_includes_encoder_stats():
|
||||
scheduler_output = SchedulerOutput.make_empty()
|
||||
scheduler_output.scheduled_encoder_input_stats = ScheduledEncoderInputStats(
|
||||
|
||||
Reference in New Issue
Block a user