| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | import torch |
| 4 | from transformers import PreTrainedConfig |
| 5 | |
| 6 | from vllm.config import ( |
| 7 | VllmConfig, |
| 8 | ) |
| 9 | from vllm.distributed import ( |
| 10 | get_tensor_model_parallel_rank, |
| 11 | get_tensor_model_parallel_world_size, |
| 12 | ) |
| 13 | from vllm.model_executor.custom_op import PluggableLayer |
| 14 | from vllm.model_executor.layers.mamba.abstract import MambaBase |
| 15 | from vllm.model_executor.layers.mamba.mamba_utils import ( |
| 16 | MambaStateDtypeCalculator, |
| 17 | ) |
| 18 | from vllm.model_executor.models.utils import extract_layer_index |
| 19 | from vllm.v1.attention.backends.registry import MambaAttentionBackendEnum |
| 20 | |
| 21 | |
| 22 | class GatedDeltaNetAttention(PluggableLayer, MambaBase): |
| 23 | """Base class for GatedDeltaNet attention layer.""" |
| 24 | |
| 25 | def __init__( |
| 26 | self, |
| 27 | config: PreTrainedConfig, |
| 28 | vllm_config: VllmConfig, |
| 29 | prefix: str = "", |
| 30 | ) -> None: |
| 31 | super().__init__() |
| 32 | self.prefix = prefix |
| 33 | self.tp_size = get_tensor_model_parallel_world_size() |
| 34 | self.tp_rank = get_tensor_model_parallel_rank() |
| 35 | self.layer_idx = extract_layer_index(prefix) |
| 36 | self.hidden_size = config.hidden_size |
| 37 | self.activation = config.hidden_act |
| 38 | self.layer_norm_epsilon = config.rms_norm_eps |
| 39 | self.model_config = vllm_config.model_config |
| 40 | self.cache_config = vllm_config.cache_config |
| 41 | self.quant_config = vllm_config.quant_config |
| 42 | self.speculative_config = vllm_config.speculative_config |
| 43 | self.num_spec = ( |
| 44 | self.speculative_config.num_speculative_tokens |
| 45 | if self.speculative_config |
| 46 | else 0 |
| 47 | ) |
| 48 | |
| 49 | @property |
| 50 | def mamba_type(self) -> MambaAttentionBackendEnum: |
| 51 | return MambaAttentionBackendEnum.GDN_ATTN |
| 52 | |
| 53 | def get_state_dtype(self) -> tuple[torch.dtype, ...]: |
| 54 | return MambaStateDtypeCalculator.gated_delta_net_state_dtype( |
| 55 | self.model_config.dtype, |
| 56 | self.cache_config.mamba_cache_dtype, |
| 57 | self.cache_config.mamba_ssm_cache_dtype, |
| 58 | ) |