Repository navigation
feat: add EncoderCacheManager to TRT-LLM PrefillHandler #5714
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
Merged
Merged
Changes from all commits
Commits
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
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
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
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
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
44 changes: 44 additions & 0 deletions
44
components/src/dynamo/trtllm/tests/request_handlers/test_trtllm_prefill_handler.py
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,44 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Unit tests for PrefillHandler.""" | ||
|
|
||
| from unittest.mock import MagicMock | ||
|
|
||
| import pytest | ||
|
|
||
| from dynamo.trtllm.request_handlers.handlers import PrefillHandler | ||
| from dynamo.trtllm.tests.utils import create_mock_request_handler_config | ||
|
|
||
| pytestmark = [ | ||
| pytest.mark.pre_merge, | ||
| pytest.mark.trtllm, | ||
| pytest.mark.gpu_0, | ||
| ] | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_config(): | ||
| """Create a mock RequestHandlerConfig.""" | ||
| return create_mock_request_handler_config(disaggregation_mode="prefill") | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_encoder_cache(): | ||
| """Create a mock EncoderCacheManager.""" | ||
| cache = MagicMock() | ||
| cache.get = MagicMock(return_value=None) | ||
| cache.set = MagicMock(return_value=True) | ||
| cache.stats = {"hits": 0, "misses": 0, "entries": 0} | ||
| return cache | ||
|
|
||
|
|
||
| class TestPrefillHandlerInit: | ||
| """Tests for PrefillHandler initialization.""" | ||
|
|
||
| def test_init_with_encoder_cache(self, mock_config, mock_encoder_cache): | ||
| """Test PrefillHandler can be initialized with encoder_cache.""" | ||
| handler = PrefillHandler(mock_config, encoder_cache=mock_encoder_cache) | ||
|
|
||
| assert handler.engine == mock_config.engine | ||
| assert handler._encoder_cache == mock_encoder_cache | ||
77 changes: 77 additions & 0 deletions
77
components/src/dynamo/trtllm/tests/request_handlers/test_trtllm_request_handler_factory.py
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,77 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Unit tests for RequestHandlerFactory.""" | ||
|
|
||
| import pytest | ||
|
|
||
| from dynamo.common.memory.encoder_cache_manager import EncoderCacheManager | ||
| from dynamo.trtllm.request_handlers.handlers import ( | ||
| AggregatedHandler, | ||
| PrefillHandler, | ||
| RequestHandlerFactory, | ||
| ) | ||
| from dynamo.trtllm.tests.utils import create_mock_request_handler_config | ||
|
|
||
| pytestmark = [ | ||
| pytest.mark.pre_merge, | ||
| pytest.mark.trtllm, | ||
| pytest.mark.gpu_0, | ||
| ] | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_config(): | ||
| """Create a mock RequestHandlerConfig.""" | ||
| return create_mock_request_handler_config() | ||
|
|
||
|
|
||
| class TestRequestHandlerFactory: | ||
| """Tests for RequestHandlerFactory.""" | ||
|
|
||
| def test_creates_aggregated_handler(self, mock_config): | ||
| """Test factory creates AggregatedHandler for prefill_and_decode mode.""" | ||
| factory = RequestHandlerFactory() | ||
| handler = factory.get_request_handler(mock_config) | ||
|
|
||
| assert isinstance(handler, AggregatedHandler) | ||
|
|
||
| def test_creates_prefill_handler(self, mock_config): | ||
| """Test factory creates PrefillHandler for prefill mode.""" | ||
| mock_config.disaggregation_mode.value = "prefill" | ||
| factory = RequestHandlerFactory() | ||
| handler = factory.get_request_handler(mock_config) | ||
|
|
||
| assert isinstance(handler, PrefillHandler) | ||
|
|
||
| def test_invalid_mode_raises(self, mock_config): | ||
| """Test factory raises ValueError for invalid disaggregation_mode.""" | ||
| mock_config.disaggregation_mode.value = "invalid_mode" | ||
| factory = RequestHandlerFactory() | ||
|
|
||
| with pytest.raises(ValueError, match="Invalid disaggregation_mode"): | ||
| factory.get_request_handler(mock_config) | ||
|
|
||
| def test_prefill_handler_with_encoder_cache(self): | ||
| """Test factory creates PrefillHandler with EncoderCacheManager when capacity > 0.""" | ||
| mock_config = create_mock_request_handler_config( | ||
| disaggregation_mode="prefill", | ||
| encoder_cache_capacity_gb=1.0, | ||
| ) | ||
| factory = RequestHandlerFactory() | ||
| handler = factory.get_request_handler(mock_config) | ||
|
|
||
| assert isinstance(handler, PrefillHandler) | ||
| assert isinstance(handler._encoder_cache, EncoderCacheManager) | ||
|
|
||
| def test_prefill_handler_without_encoder_cache(self): | ||
| """Test factory creates PrefillHandler with no cache when capacity is 0.""" | ||
| mock_config = create_mock_request_handler_config( | ||
| disaggregation_mode="prefill", | ||
| encoder_cache_capacity_gb=0, | ||
| ) | ||
| factory = RequestHandlerFactory() | ||
| handler = factory.get_request_handler(mock_config) | ||
|
|
||
| assert isinstance(handler, PrefillHandler) | ||
| assert handler._encoder_cache is None |
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,36 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Shared test utilities for dynamo.trtllm tests.""" | ||
|
|
||
| from unittest.mock import MagicMock | ||
|
|
||
|
|
||
| def create_mock_request_handler_config( | ||
| disaggregation_mode: str = "prefill_and_decode", | ||
| encoder_cache_capacity_gb: float = 0, | ||
|
furionw marked this conversation as resolved.
|
||
| ) -> MagicMock: | ||
| """Create a mock RequestHandlerConfig for testing. | ||
|
|
||
| Args: | ||
| disaggregation_mode: The disaggregation mode value. | ||
| encoder_cache_capacity_gb: Encoder cache capacity in GB. | ||
|
|
||
| Returns: | ||
| MagicMock configured as a RequestHandlerConfig. | ||
| """ | ||
| config = MagicMock() | ||
| config.disaggregation_mode.value = disaggregation_mode | ||
| config.engine = MagicMock() | ||
| config.component = MagicMock() | ||
| config.default_sampling_params = MagicMock() | ||
| config.publisher = MagicMock() | ||
| config.metrics_collector = None | ||
| config.encode_client = None | ||
| config.multimodal_processor = None | ||
| config.connector = None | ||
| config.runtime = None | ||
| config.kv_block_size = 32 | ||
| config.shutdown_event = None | ||
| config.encoder_cache_capacity_gb = encoder_cache_capacity_gb | ||
| return config | ||
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
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.