11# SPDX-License-Identifier: Apache-2.0
22# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
33
4+ import logging
5+
46import pytest
7+ import regex as re
58
69from vllm import LLM
710from vllm .entrypoints .chat_utils import ChatCompletionMessageParam
11+ from vllm .v1 .metrics import loggers as stat_loggers
812from vllm .v1 .metrics .reader import Counter , Metric
913
1014from ..openai .test_vision import TEST_IMAGE_ASSETS
@@ -37,12 +41,27 @@ def _get_mm_cache_stats(metrics: list[Metric]):
3741 return mm_cache_queries , mm_cache_hits
3842
3943
44+ def _get_mm_cache_log (llm : LLM , caplog_vllm : pytest .LogCaptureFixture ) -> float :
45+ caplog_vllm .clear ()
46+ with caplog_vllm .at_level (logging .INFO , logger = stat_loggers .__name__ ):
47+ llm .llm_engine .do_log_stats ()
48+
49+ assert len (caplog_vllm .records ) == 1
50+ msg = caplog_vllm .records [0 ].getMessage ()
51+
52+ assert "MM cache hit rate" in msg
53+ match = re .search (r"MM cache hit rate: ([0-9.]+)%" , msg )
54+ assert match is not None
55+ return float (match .group (1 ))
56+
57+
4058@pytest .mark .parametrize ("image_urls" , [TEST_IMAGE_ASSETS [:2 ]], indirect = True )
4159@pytest .mark .parametrize ("mm_processor_cache_type" , ["lru" , "shm" ])
4260def test_mm_cache_stats (
4361 num_gpus_available ,
4462 image_urls ,
4563 mm_processor_cache_type ,
64+ caplog_vllm ,
4665):
4766 llm = LLM (
4867 model = "llava-hf/llava-1.5-7b-hf" ,
@@ -56,19 +75,24 @@ def test_mm_cache_stats(
5675
5776 llm .chat (_make_messages (image_urls [0 ]))
5877 assert _get_mm_cache_stats (llm .get_metrics ()) == (1 , 0 )
78+ assert _get_mm_cache_log (llm , caplog_vllm ) == pytest .approx (0.0 )
5979
6080 llm .chat (_make_messages (image_urls [1 ]))
6181 assert _get_mm_cache_stats (llm .get_metrics ()) == (2 , 0 )
82+ assert _get_mm_cache_log (llm , caplog_vllm ) == pytest .approx (0.0 )
6283
6384 llm .chat (_make_messages (image_urls [0 ]))
6485 assert _get_mm_cache_stats (llm .get_metrics ()) == (3 , 1 )
86+ assert _get_mm_cache_log (llm , caplog_vllm ) == pytest .approx (33.3 )
6587
6688 # NOTE: This only resets hit rate stats in CachingMetrics
6789 # The raw queries and hits counts remain unaffected
6890 llm .reset_mm_cache ()
6991
7092 llm .chat (_make_messages (image_urls [0 ]))
7193 assert _get_mm_cache_stats (llm .get_metrics ()) == (4 , 1 )
94+ assert _get_mm_cache_log (llm , caplog_vllm ) == pytest .approx (0.0 )
7295
7396 llm .chat (_make_messages (image_urls [1 ]))
7497 assert _get_mm_cache_stats (llm .get_metrics ()) == (5 , 1 )
98+ assert _get_mm_cache_log (llm , caplog_vllm ) == pytest .approx (0.0 )
0 commit comments