Args: hidden_size (int): dimension of hidden layer. num_heads (int): number of attention heads. dropout_rate (float, optional): fraction of the input units to drop. Defaults to 0.0. qkv_bias (bool, optional): bias term for the qkv linear layer
(
self,
hidden_size: int,
num_heads: int,
dropout_rate: float = 0.0,
qkv_bias: bool = False,
save_attn: bool = False,
dim_head: int | None = None,
hidden_input_size: int | None = None,
causal: bool = False,
sequence_length: int | None = None,
rel_pos_embedding: str | None = None,
input_size: tuple | None = None,
attention_dtype: torch.dtype | None = None,
include_fc: bool = True,
use_combined_linear: bool = True,
use_flash_attention: bool = False,
)
| 30 | """ |
| 31 | |
| 32 | def __init__( |
| 33 | self, |
| 34 | hidden_size: int, |
| 35 | num_heads: int, |
| 36 | dropout_rate: float = 0.0, |
| 37 | qkv_bias: bool = False, |
| 38 | save_attn: bool = False, |
| 39 | dim_head: int | None = None, |
| 40 | hidden_input_size: int | None = None, |
| 41 | causal: bool = False, |
| 42 | sequence_length: int | None = None, |
| 43 | rel_pos_embedding: str | None = None, |
| 44 | input_size: tuple | None = None, |
| 45 | attention_dtype: torch.dtype | None = None, |
| 46 | include_fc: bool = True, |
| 47 | use_combined_linear: bool = True, |
| 48 | use_flash_attention: bool = False, |
| 49 | ) -> None: |
| 50 | """ |
| 51 | Args: |
| 52 | hidden_size (int): dimension of hidden layer. |
| 53 | num_heads (int): number of attention heads. |
| 54 | dropout_rate (float, optional): fraction of the input units to drop. Defaults to 0.0. |
| 55 | qkv_bias (bool, optional): bias term for the qkv linear layer. Defaults to False. |
| 56 | save_attn (bool, optional): to make accessible the attention matrix. Defaults to False. |
| 57 | dim_head (int, optional): dimension of each head. Defaults to hidden_size // num_heads. |
| 58 | hidden_input_size (int, optional): dimension of the input tensor. Defaults to hidden_size. |
| 59 | causal: whether to use causal attention (see https://arxiv.org/abs/1706.03762). |
| 60 | sequence_length: if causal is True, it is necessary to specify the sequence length. |
| 61 | rel_pos_embedding (str, optional): Add relative positional embeddings to the attention map. |
| 62 | For now only "decomposed" is supported (see https://arxiv.org/abs/2112.01526). 2D and 3D are supported. |
| 63 | input_size (tuple(spatial_dim), optional): Input resolution for calculating the relative |
| 64 | positional parameter size. |
| 65 | attention_dtype: cast attention operations to this dtype. |
| 66 | include_fc: whether to include the final linear layer. Default to True. |
| 67 | use_combined_linear: whether to use a single linear layer for qkv projection, default to True. |
| 68 | use_flash_attention: if True, use Pytorch's inbuilt flash attention for a memory efficient attention mechanism |
| 69 | (see https://pytorch.org/docs/2.2/generated/torch.nn.functional.scaled_dot_product_attention.html). |
| 70 | |
| 71 | Raises: |
| 72 | ValueError: if ``dropout_rate`` is not between 0 and 1. |
| 73 | ValueError: if ``hidden_size`` is not divisible by ``num_heads``. |
| 74 | ValueError: if ``causal`` is True and ``sequence_length`` is not provided. |
| 75 | ValueError: if both ``save_attn`` and ``use_flash_attention`` are True. |
| 76 | ValueError: if ``rel_pos_embedding`` is not None and ``use_flash_attention`` is True. |
| 77 | |
| 78 | """ |
| 79 | |
| 80 | super().__init__() |
| 81 | |
| 82 | if not (0 <= dropout_rate <= 1): |
| 83 | raise ValueError("dropout_rate should be between 0 and 1.") |
| 84 | |
| 85 | if hidden_size % num_heads != 0: |
| 86 | raise ValueError("hidden size should be divisible by num_heads.") |
| 87 | |
| 88 | if dim_head: |
| 89 | self.inner_dim = num_heads * dim_head |
nothing calls this directly
no test coverage detected