diff --git a/vllm/v1/core/single_type_kv_cache_manager.py b/vllm/v1/core/single_type_kv_cache_manager.py index 19240e68a8..cf3c9c1b61 100644 --- a/vllm/v1/core/single_type_kv_cache_manager.py +++ b/vllm/v1/core/single_type_kv_cache_manager.py @@ -1722,6 +1722,17 @@ class MambaManager(SingleTypeKVCacheManager): self.block_pool.hash_block_size, self.block_size, ) + if ( + self.mamba_cache_mode == "align" + and self.enable_caching + and self.block_size > self.block_pool.hash_block_size + ): + logger.info_once( + "Mamba align: a prompt tail key that lands on a block end is " + "hashed too (hash unit %d, block %d)", + self.block_pool.hash_block_size, + self.block_size, + ) def _retire_state_blocks( self, request_id: str, retired: list[tuple[int, KVCacheBlock]] @@ -2384,6 +2395,14 @@ class MambaManager(SingleTypeKVCacheManager): replay_boundaries: Sequence[int], ) -> None: num_cached_blocks_before = self.num_cached_block.get(request.request_id, 0) + tail = self._block_aligned_prompt_tail(request) + if tail: + # With a hash unit finer than the block, the prompt's tail + # key (where the next turn resumes; `_cache_partial_tail_block`) + # can land exactly on a block end. It is then a full-block key, + # not a partial one, and the replay-boundary mask alone (one block + # below it under the eagle drop) would never hash that state. + replay_boundaries = (*replay_boundaries, tail) super().cache_blocks( request, num_tokens, @@ -2425,6 +2444,22 @@ class MambaManager(SingleTypeKVCacheManager): def new_step_starts(self) -> None: self.cached_blocks_this_step.clear() + def _block_aligned_prompt_tail(self, request: Request) -> int: + """The prompt's tail key when it falls on a block end, else 0. + + Only with a hash unit finer than the block (at the block unit the + tail IS the replay boundary, which the mask already keeps). The key + is placed exactly as `_cache_partial_tail_block` places a sub-block + tail, eagle shift included. + """ + hash_block_size = self.block_pool.hash_block_size + if self.mamba_cache_mode != "align" or self.block_size <= hash_block_size: + return 0 + tail = request.num_prompt_tokens // hash_block_size * hash_block_size + if self.drop_eagle_checkpoint_block: + tail = max(tail - hash_block_size, 0) + return tail if tail % self.block_size == 0 else 0 + def _cache_partial_tail_block( self, request: Request,