-
-
Notifications
You must be signed in to change notification settings - Fork 19.1k
[Core] Add register_model() to KVConnectorBase_V1 for CacheBlend #37339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zbennett10
wants to merge
9
commits into
vllm-project:main
Choose a base branch
from
WorldFlowAI:semblend/register-model-hook
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+242
−2
Open
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4d04e09
fix: add register_model() to KVConnectorBase_V1 for CacheBlend
zbennett10 4a29898
refactor: delegate register_model to _lmcache_engine in LMCacheConnec…
zbennett10 86d9f44
style: fix E501 lint errors in register_model files
zbennett10 7fe344e
refactor: replace register_model with WorkerConnectorInitializationDa…
zbennett10 531cfa2
fix: remove unnecessary string quote from type annotation
zbennett10 bc551dc
Merge branch 'main' into semblend/register-model-hook
NickLucche d21c342
refactor: address review feedback on initialize_worker_connector
zbennett10 7e7a460
test: restore LMCache connector initialize_worker_connector coverage
zbennett10 45732eb
test: move LMCache tests from test_multi_connector to test_lmcache_co…
zbennett10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,346 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
| """Tests for initialize_worker_connector() on KVConnectorBase_V1. | ||
|
|
||
| Validates PR: replace register_model() with WorkerConnectorInitializationData | ||
| pattern for extensible connector initialization. | ||
| """ | ||
|
|
||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import torch | ||
|
|
||
|
|
||
| class TestWorkerConnectorInitializationData: | ||
| """Test the WorkerConnectorInitializationData dataclass.""" | ||
|
|
||
| def test_dataclass_default_model_is_none(self): | ||
| """WorkerConnectorInitializationData.model defaults to None.""" | ||
| from vllm.distributed.kv_transfer.kv_connector.v1.base import ( | ||
| WorkerConnectorInitializationData, | ||
| ) | ||
|
|
||
| data = WorkerConnectorInitializationData() | ||
| assert data.model is None | ||
|
|
||
| def test_dataclass_accepts_model(self): | ||
| """WorkerConnectorInitializationData accepts an nn.Module.""" | ||
| from vllm.distributed.kv_transfer.kv_connector.v1.base import ( | ||
| WorkerConnectorInitializationData, | ||
| ) | ||
|
|
||
| model = MagicMock(spec=torch.nn.Module) | ||
| data = WorkerConnectorInitializationData(model=model) | ||
| assert data.model is model | ||
|
|
||
| def test_response_dataclass_exists(self): | ||
| """WorkerConnectorInitializationResponse is importable.""" | ||
| from vllm.distributed.kv_transfer.kv_connector.v1.base import ( | ||
| WorkerConnectorInitializationResponse, | ||
| ) | ||
|
|
||
| resp = WorkerConnectorInitializationResponse() | ||
| assert resp is not None | ||
|
|
||
|
|
||
| class TestKVConnectorBaseInitializeWorkerConnector: | ||
| """Test the base class initialize_worker_connector default behavior.""" | ||
|
|
||
| def test_base_class_has_initialize_worker_connector(self): | ||
| """initialize_worker_connector exists on KVConnectorBase_V1.""" | ||
| from vllm.distributed.kv_transfer.kv_connector.v1.base import ( | ||
| KVConnectorBase_V1, | ||
| ) | ||
|
|
||
| assert hasattr(KVConnectorBase_V1, "initialize_worker_connector") | ||
|
|
||
| def test_base_class_is_noop_returns_response(self): | ||
| """Base class initialize_worker_connector returns a response object.""" | ||
| from vllm.distributed.kv_transfer.kv_connector.v1.base import ( | ||
| KVConnectorBase_V1, | ||
| WorkerConnectorInitializationData, | ||
| WorkerConnectorInitializationResponse, | ||
| ) | ||
|
|
||
| connector = KVConnectorBase_V1.__new__(KVConnectorBase_V1) | ||
| data = WorkerConnectorInitializationData(model=MagicMock(spec=torch.nn.Module)) | ||
| result = connector.initialize_worker_connector(data) | ||
| assert isinstance(result, WorkerConnectorInitializationResponse) | ||
|
|
||
|
|
||
| class TestActiveKVConnectorInitializeWorkerConnector: | ||
| """Test that ActiveKVConnector forwards initialization data.""" | ||
|
|
||
| def test_initialize_worker_connector_called_with_model(self): | ||
| """ActiveKVConnector calls initialize_worker_connector with model.""" | ||
| mock_connector = MagicMock() | ||
| mock_model = MagicMock(spec=torch.nn.Module) | ||
|
|
||
| with ( | ||
| patch( | ||
| "vllm.v1.worker.gpu.kv_connector.get_kv_transfer_group", | ||
| return_value=mock_connector, | ||
| ), | ||
| patch( | ||
| "vllm.v1.worker.gpu.kv_connector.has_kv_transfer_group", | ||
| return_value=True, | ||
| ), | ||
| ): | ||
| from vllm.v1.worker.gpu.kv_connector import ActiveKVConnector | ||
|
|
||
| _connector = ActiveKVConnector( | ||
| vllm_config=MagicMock(), | ||
| kv_caches_dict={"layer.0": torch.zeros(1)}, | ||
| model=mock_model, | ||
| ) | ||
|
|
||
| mock_connector.initialize_worker_connector.assert_called_once() | ||
| call_args = mock_connector.initialize_worker_connector.call_args | ||
| init_data = call_args[0][0] | ||
| assert init_data.model is mock_model | ||
|
|
||
| def test_initialize_worker_connector_called_with_none_model(self): | ||
| """ActiveKVConnector still calls initialize_worker_connector when model=None.""" | ||
| mock_connector = MagicMock() | ||
|
|
||
| with ( | ||
| patch( | ||
| "vllm.v1.worker.gpu.kv_connector.get_kv_transfer_group", | ||
| return_value=mock_connector, | ||
| ), | ||
| patch( | ||
| "vllm.v1.worker.gpu.kv_connector.has_kv_transfer_group", | ||
| return_value=True, | ||
| ), | ||
| ): | ||
| from vllm.v1.worker.gpu.kv_connector import ActiveKVConnector | ||
|
|
||
| _connector = ActiveKVConnector( | ||
| vllm_config=MagicMock(), | ||
| kv_caches_dict={"layer.0": torch.zeros(1)}, | ||
| model=None, | ||
| ) | ||
|
|
||
| mock_connector.initialize_worker_connector.assert_called_once() | ||
| call_args = mock_connector.initialize_worker_connector.call_args | ||
| init_data = call_args[0][0] | ||
| assert init_data.model is None | ||
|
|
||
|
|
||
| class TestGetKVConnectorInitializeWorkerConnector: | ||
| """Test that get_kv_connector passes initialization data through.""" | ||
|
|
||
| def test_passes_model_via_initialization_data(self): | ||
| """get_kv_connector forwards model in WorkerConnectorInitializationData.""" | ||
| mock_connector = MagicMock() | ||
| mock_model = MagicMock(spec=torch.nn.Module) | ||
|
|
||
| with ( | ||
| patch( | ||
| "vllm.v1.worker.gpu.kv_connector.get_kv_transfer_group", | ||
| return_value=mock_connector, | ||
| ), | ||
| patch( | ||
| "vllm.v1.worker.gpu.kv_connector.has_kv_transfer_group", | ||
| return_value=True, | ||
| ), | ||
| ): | ||
| from vllm.v1.worker.gpu.kv_connector import get_kv_connector | ||
|
|
||
| _kv_connector = get_kv_connector( | ||
| vllm_config=MagicMock(), | ||
| kv_caches_dict={"layer.0": torch.zeros(1)}, | ||
| model=mock_model, | ||
| ) | ||
|
|
||
| mock_connector.initialize_worker_connector.assert_called_once() | ||
| call_args = mock_connector.initialize_worker_connector.call_args | ||
| init_data = call_args[0][0] | ||
| assert init_data.model is mock_model | ||
|
|
||
| def test_noop_when_no_transfer_group(self): | ||
| """get_kv_connector returns NoOp when no transfer group.""" | ||
| with patch( | ||
| "vllm.v1.worker.gpu.kv_connector.has_kv_transfer_group", | ||
| return_value=False, | ||
| ): | ||
| from vllm.v1.worker.gpu.kv_connector import ( | ||
| NO_OP_KV_CONNECTOR, | ||
| get_kv_connector, | ||
| ) | ||
|
|
||
| result = get_kv_connector( | ||
| vllm_config=MagicMock(), | ||
| kv_caches_dict={}, | ||
| model=MagicMock(), | ||
| ) | ||
| assert result is NO_OP_KV_CONNECTOR | ||
|
|
||
|
|
||
| class TestLMCacheConnectorInitializeWorkerConnector: | ||
| """Test LMCache connector initialize_worker_connector delegation.""" | ||
|
|
||
| def test_initialize_worker_connector_delegates_model_to_engine(self): | ||
| """LMCacheConnectorV1 forwards model to engine.register_model.""" | ||
| from vllm.distributed.kv_transfer.kv_connector.v1.base import ( | ||
| WorkerConnectorInitializationData, | ||
| ) | ||
| from vllm.distributed.kv_transfer.kv_connector.v1.lmcache_connector import ( | ||
| LMCacheConnectorV1, | ||
| ) | ||
|
|
||
| mock_engine = MagicMock() | ||
| mock_model = MagicMock(spec=torch.nn.Module) | ||
|
|
||
| connector = LMCacheConnectorV1.__new__(LMCacheConnectorV1) | ||
| connector._lmcache_engine = mock_engine | ||
| connector.initialize_worker_connector( | ||
| WorkerConnectorInitializationData(model=mock_model) | ||
| ) | ||
|
|
||
| mock_engine.register_model.assert_called_once_with(mock_model) | ||
|
|
||
| def test_initialize_worker_connector_skips_engine_without_method(self): | ||
| """No-op when engine lacks register_model.""" | ||
| from vllm.distributed.kv_transfer.kv_connector.v1.base import ( | ||
| WorkerConnectorInitializationData, | ||
| ) | ||
| from vllm.distributed.kv_transfer.kv_connector.v1.lmcache_connector import ( | ||
| LMCacheConnectorV1, | ||
| ) | ||
|
|
||
| mock_engine = MagicMock(spec=[]) # empty spec — no attributes | ||
| mock_model = MagicMock(spec=torch.nn.Module) | ||
|
|
||
| connector = LMCacheConnectorV1.__new__(LMCacheConnectorV1) | ||
| connector._lmcache_engine = mock_engine | ||
| # Should not raise | ||
| connector.initialize_worker_connector( | ||
| WorkerConnectorInitializationData(model=mock_model) | ||
| ) | ||
|
|
||
| def test_initialize_worker_connector_skips_when_model_none(self): | ||
| """Does not call register_model when model is None.""" | ||
| from vllm.distributed.kv_transfer.kv_connector.v1.base import ( | ||
| WorkerConnectorInitializationData, | ||
| ) | ||
| from vllm.distributed.kv_transfer.kv_connector.v1.lmcache_connector import ( | ||
| LMCacheConnectorV1, | ||
| ) | ||
|
|
||
| mock_engine = MagicMock() | ||
| connector = LMCacheConnectorV1.__new__(LMCacheConnectorV1) | ||
| connector._lmcache_engine = mock_engine | ||
| connector.initialize_worker_connector(WorkerConnectorInitializationData()) | ||
| mock_engine.register_model.assert_not_called() | ||
|
|
||
|
|
||
| class TestLMCacheConnectorV1ImplInitializeWorkerConnector: | ||
| """Test LMCacheConnectorV1Impl initialize_worker_connector logic.""" | ||
|
|
||
| def test_registers_model_with_vllm_model_tracker(self): | ||
| """Impl registers model with VLLMModelTracker when model provided.""" | ||
| from vllm.distributed.kv_transfer.kv_connector.v1.base import ( | ||
| WorkerConnectorInitializationData, | ||
| ) | ||
|
|
||
| mock_tracker = MagicMock() | ||
| mock_model = MagicMock(spec=torch.nn.Module) | ||
|
|
||
| with patch.dict( | ||
| "sys.modules", | ||
| { | ||
| "lmcache": MagicMock(), | ||
| "lmcache.v1": MagicMock(), | ||
| "lmcache.v1.compute": MagicMock(), | ||
| "lmcache.v1.compute.models": MagicMock(), | ||
| "lmcache.v1.compute.models.utils": MagicMock( | ||
| VLLMModelTracker=mock_tracker | ||
| ), | ||
| }, | ||
| ): | ||
| from vllm.distributed.kv_transfer.kv_connector.v1.lmcache_integration.vllm_v1_adapter import ( # noqa: E501 | ||
| LMCacheConnectorV1Impl, | ||
| ) | ||
|
|
||
| impl = LMCacheConnectorV1Impl.__new__(LMCacheConnectorV1Impl) | ||
| impl.initialize_worker_connector( | ||
| WorkerConnectorInitializationData(model=mock_model) | ||
| ) | ||
|
|
||
| mock_tracker.register_model.assert_called_once() | ||
| call_args = mock_tracker.register_model.call_args | ||
| assert call_args[0][1] is mock_model | ||
|
|
||
| def test_graceful_on_import_error(self): | ||
| """Doesn't crash if LMCache CacheBlend not installed.""" | ||
| from vllm.distributed.kv_transfer.kv_connector.v1.base import ( | ||
| WorkerConnectorInitializationData, | ||
| ) | ||
| from vllm.distributed.kv_transfer.kv_connector.v1.lmcache_integration.vllm_v1_adapter import ( # noqa: E501 | ||
| LMCacheConnectorV1Impl, | ||
| ) | ||
|
|
||
| impl = LMCacheConnectorV1Impl.__new__(LMCacheConnectorV1Impl) | ||
|
|
||
| with patch.dict( | ||
| "sys.modules", | ||
| {"lmcache.v1.compute.models.utils": None}, | ||
| ): | ||
| impl.initialize_worker_connector( | ||
| WorkerConnectorInitializationData(model=MagicMock(spec=torch.nn.Module)) | ||
| ) | ||
|
|
||
| def test_skips_tracker_when_model_none(self): | ||
| """Does not call VLLMModelTracker when model is None.""" | ||
| from vllm.distributed.kv_transfer.kv_connector.v1.base import ( | ||
| WorkerConnectorInitializationData, | ||
| ) | ||
|
|
||
| mock_tracker = MagicMock() | ||
|
|
||
| with patch.dict( | ||
| "sys.modules", | ||
| { | ||
| "lmcache": MagicMock(), | ||
| "lmcache.v1": MagicMock(), | ||
| "lmcache.v1.compute": MagicMock(), | ||
| "lmcache.v1.compute.models": MagicMock(), | ||
| "lmcache.v1.compute.models.utils": MagicMock( | ||
| VLLMModelTracker=mock_tracker | ||
| ), | ||
| }, | ||
| ): | ||
| from vllm.distributed.kv_transfer.kv_connector.v1.lmcache_integration.vllm_v1_adapter import ( # noqa: E501 | ||
| LMCacheConnectorV1Impl, | ||
| ) | ||
|
|
||
| impl = LMCacheConnectorV1Impl.__new__(LMCacheConnectorV1Impl) | ||
| impl.initialize_worker_connector(WorkerConnectorInitializationData()) | ||
|
|
||
| mock_tracker.register_model.assert_not_called() | ||
|
|
||
|
|
||
| class TestMultiConnectorInitializeWorkerConnector: | ||
| """Test MultiConnector delegates initialize_worker_connector to sub-connectors.""" | ||
|
|
||
| def test_delegates_to_all_sub_connectors(self): | ||
| """MultiConnector.initialize_worker_connector calls each sub-connector.""" | ||
| from vllm.distributed.kv_transfer.kv_connector.v1.base import ( | ||
| WorkerConnectorInitializationData, | ||
| ) | ||
| from vllm.distributed.kv_transfer.kv_connector.v1.multi_connector import ( | ||
| MultiConnector, | ||
| ) | ||
|
|
||
| mock_model = MagicMock(spec=torch.nn.Module) | ||
| sub1 = MagicMock() | ||
| sub2 = MagicMock() | ||
|
|
||
| connector = MultiConnector.__new__(MultiConnector) | ||
| connector._connectors = [sub1, sub2] | ||
| data = WorkerConnectorInitializationData(model=mock_model) | ||
| connector.initialize_worker_connector(data) | ||
|
|
||
| sub1.initialize_worker_connector.assert_called_once_with(data) | ||
| sub2.initialize_worker_connector.assert_called_once_with(data) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.