From c528b9006a2d8b9a7905658b6d38121eea61f846 Mon Sep 17 00:00:00 2001 From: usberkeley <150880684+usberkeley@users.noreply.github.com> Date: Fri, 24 Oct 2025 13:00:01 +0800 Subject: [PATCH] Fix EventPublisherFactory logic for disabled KV cache events (#27419) Signed-off-by: Bradley --- tests/distributed/test_events.py | 49 ++++++++++++++++++++++++++++++++ vllm/distributed/kv_events.py | 6 +++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/tests/distributed/test_events.py b/tests/distributed/test_events.py index f06f6771a..f17b7997c 100644 --- a/tests/distributed/test_events.py +++ b/tests/distributed/test_events.py @@ -263,3 +263,52 @@ def test_data_parallel_rank_tagging(publisher_config): pub_1.shutdown() sub_0.close() sub_1.close() + + +def test_event_publisher_factory(): + """Test event publisher factory creation behavior under different configurations""" + from vllm.config.kv_events import KVEventsConfig + from vllm.distributed.kv_events import ZmqEventPublisher + + # test config is None + publisher = EventPublisherFactory.create(None, DP_RANK) + assert isinstance(publisher, NullEventPublisher) + publisher.shutdown() + + # test disable kv cache events + config = KVEventsConfig( + enable_kv_cache_events=False, + publisher="zmq", # Even if zmq is specified, should return NullEventPublisher + endpoint="tcp://localhost:5557", + ) + publisher = EventPublisherFactory.create(config, DP_RANK) + assert isinstance(publisher, NullEventPublisher) + publisher.shutdown() + + # test zmq publisher + config = KVEventsConfig( + enable_kv_cache_events=True, + publisher="zmq", + endpoint="inproc://test-factory-true", + ) + publisher = EventPublisherFactory.create(config, DP_RANK) + assert isinstance(publisher, ZmqEventPublisher) + publisher.shutdown() + + # test unknown publisher + with pytest.raises(ValueError, match="Input should be"): + KVEventsConfig( + enable_kv_cache_events=True, + publisher="unknown_publisher", + endpoint="tcp://localhost:5557", + ) + + # test publisher not specified + config = KVEventsConfig( + enable_kv_cache_events=True, + # publisher not specified, should default to "zmq" + endpoint="tcp://localhost:5557", + ) + publisher = EventPublisherFactory.create(config, DP_RANK) + assert isinstance(publisher, ZmqEventPublisher) + publisher.shutdown() diff --git a/vllm/distributed/kv_events.py b/vllm/distributed/kv_events.py index 2fd46b232..7b5cb94cf 100644 --- a/vllm/distributed/kv_events.py +++ b/vllm/distributed/kv_events.py @@ -353,7 +353,11 @@ class EventPublisherFactory: cls, config: KVEventsConfig | None, data_parallel_rank: int = 0 ) -> EventPublisher: """Create publisher from a config mapping.""" - if config is None or config.publisher == "null": + if ( + config is None + or not config.enable_kv_cache_events + or config.publisher == "null" + ): return NullEventPublisher() config_dict = asdict(config)