mamba_cache.py 2.83 KB
Newer Older
1
2
# SPDX-License-Identifier: Apache-2.0

3
from dataclasses import dataclass
4
5
6

import torch

7
from vllm.attention.backends.utils import PAD_SLOT_ID
8
from vllm.config import VllmConfig
9
from vllm.model_executor.models.constant_size_cache import ConstantSizeCache
10
11
12
13
14
15
16
17
18
19
20
21


@dataclass
class MambaCacheParams:
    conv_state: torch.Tensor = torch.Tensor()
    ssm_state: torch.Tensor = torch.Tensor()
    state_indices_tensor: torch.Tensor = torch.Tensor()

    def at_layer_idx(self, layer_idx):
        return MambaCacheParams(self.conv_state[layer_idx],
                                self.ssm_state[layer_idx],
                                self.state_indices_tensor)
22
23


24
class MambaCacheManager(ConstantSizeCache):
25

26
    def __init__(self, vllm_config: VllmConfig, dtype: torch.dtype,
27
28
                 num_mamba_layers: int, conv_state_shape: tuple[int, int],
                 temporal_state_shape: tuple[int, int]):
29
30
31
32
33

        # Determine max batch size to set size of MambaCache
        max_batch_size = vllm_config.scheduler_config.max_num_seqs
        if not vllm_config.model_config.enforce_eager:
            max_batch_size = vllm_config.pad_for_cudagraph(max_batch_size)
34

35
36
37
        # Initialize parent class
        super().__init__(max_batch_size)

38
39
40
41
42
43
44
45
46
        conv_state = torch.empty(size=(num_mamba_layers, max_batch_size) +
                                 conv_state_shape,
                                 dtype=dtype,
                                 device="cuda")
        temporal_state = torch.empty(size=(num_mamba_layers, max_batch_size) +
                                     temporal_state_shape,
                                     dtype=dtype,
                                     device="cuda")

47
48
49
50
51
        self._mamba_cache = (conv_state, temporal_state)

    @property
    def cache(self):
        return self._mamba_cache
52

53
54
55
56
    def _copy_cache(self, from_index: int, to_index: int):
        for cache_t in self.cache:
            cache_t[:, to_index].copy_(cache_t[:, from_index],
                                       non_blocking=True)
57

Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
58
    def current_run_tensors(self, **kwargs) -> MambaCacheParams:
59
60
61
        """
        Return the tensors for the current run's conv and ssm state.
        """
62
63
64
        cache_tensors, state_indices_tensor = super().current_run_tensors(
            **kwargs)
        return MambaCacheParams(cache_tensors[0], cache_tensors[1],
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
65
                                state_indices_tensor)
66
67
68
69
70
71
72

    def get_seqlen_agnostic_capture_inputs(self, batch_size: int):
        """
        Provide the CUDA graph capture runs with a buffer in adjusted size.
        The buffer is used to maintain the Mamba Cache during the CUDA graph
        replay runs.
        """
73
74
75
        return self._mamba_cache, torch.as_tensor([PAD_SLOT_ID] * batch_size,
                                                  dtype=torch.int32,
                                                  device="cuda")