mask_utils
¶
Functions¶
fastvideo.models.mask_utils.and_masks
¶
Returns a mask function that is the intersection of provided mask functions
Source code in fastvideo/models/mask_utils.py
fastvideo.models.mask_utils.causal_mask_function
¶
fastvideo.models.mask_utils.padding_mask_function
¶
padding_mask_function(padding_mask: Tensor) -> Callable
This return the mask_function function corresponding to a 2D padding mask.
Source code in fastvideo/models/mask_utils.py
fastvideo.models.mask_utils.prepare_padding_mask
¶
prepare_padding_mask(attention_mask: Optional[Tensor], kv_length: int, kv_offset: int) -> Optional[Tensor]
From the 2D attention mask, prepare the correct padding mask to use by potentially padding it.
Source code in fastvideo/models/mask_utils.py
fastvideo.models.mask_utils.sdpa_mask
¶
sdpa_mask(batch_size: int, cache_position: Tensor, kv_length: int, kv_offset: int = 0, mask_function: Callable = causal_mask_function, attention_mask: Optional[Tensor] = None, local_size: Optional[int] = None, allow_is_causal_skip: bool = True, allow_is_bidirectional_skip: bool = False, allow_torch_fix: bool = True, use_vmap: bool = False, **kwargs) -> Optional[Tensor]
Create a 4D boolean mask of shape (batch_size, 1, query_length, kv_length) where a value of True indicates that
the element should take part in the attention computation, and False that it should not.
This function can only be used with torch>=2.5, as the context manager is otherwise not available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
`int`
|
The batch size of the input sequence. |
required |
cache_position
|
`torch.Tensor`
|
A tensor of shape (query_length,) indicating the current indices of the input sequence elements. |
required |
kv_length
|
`int`
|
The size that the key and value states will have during the attention computation. |
required |
kv_offset
|
`int`
|
An optional offset to indicate at which first position the key and values states will refer to. |
0
|
mask_function
|
`Callable`
|
The mask factory function describing the mask pattern. |
causal_mask_function
|
attention_mask
|
`torch.Tensor`
|
The 2D attention mask corresponding to padded tokens of shape (batch_size, number_of_seen_tokens+q_length) |
None
|
local_size
|
`int`
|
The size of the local attention, if we do not use full attention. This is used only if |
None
|
allow_is_causal_skip
|
`bool`
|
Whether to allow to return |
True
|
allow_is_bidirectional_skip
|
`bool`
|
Whether to allow to return |
False
|
allow_torch_fix
|
`bool`
|
Whether to update the mask in case a query is not attending to any tokens, to solve a bug in torch's older
versions. We need an arg to skip it when using eager. By default |
True
|
use_vmap
|
`bool`
|
Whether to use |
False
|
Creating a simple causal mask:¶
To create the following causal mask:
0 ■ ⬚ ⬚ ⬚ ⬚
1 ■ ■ ⬚ ⬚ ⬚
2 ■ ■ ■ ⬚ ⬚
3 ■ ■ ■ ■ ⬚
4 ■ ■ ■ ■ ■
You can do
>>> sdpa_mask(batch_size=1, cache_position=torch.arange(5), kv_length=5)
>>> tensor([[[[ True, False, False, False, False],
[ True, True, False, False, False],
[ True, True, True, False, False],
[ True, True, True, True, False],
[ True, True, True, True, True]]]])
Creating a sliding window mask:¶
To create the following sliding window mask (sliding_window=3):
0 ■ ⬚ ⬚ ⬚ ⬚
1 ■ ■ ⬚ ⬚ ⬚
2 ■ ■ ■ ⬚ ⬚
3 ⬚ ■ ■ ■ ⬚
4 ⬚ ⬚ ■ ■ ■
You can do
>>> sdpa_mask(batch_size=1, cache_position=torch.arange(5), kv_length=5, mask_function=sliding_window_causal_mask_function(3))
>>> tensor([[[[ True, False, False, False, False],
[ True, True, False, False, False],
[ True, True, True, False, False],
[False, True, True, True, False],
[False, False, True, True, True]]]])
Creating a chunked attention mask¶
To create the following chunked attention mask (chunk_size=3):
0 ■ ⬚ ⬚ ⬚ ⬚
1 ■ ■ ⬚ ⬚ ⬚
2 ■ ■ ■ ⬚ ⬚
3 ⬚ ⬚ ⬚ ■ ⬚
4 ⬚ ⬚ ⬚ ■ ■
You can do
>>> sdpa_mask(batch_size=1, cache_position=torch.arange(5), kv_length=5, mask_function=chunked_causal_mask_function(3, torch.zeros(1, dtype=int)))
>>> tensor([[[[ True, False, False, False, False],
[ True, True, False, False, False],
[ True, True, True, False, False],
[False, False, False, True, False],
[False, False, False, True, True]]]])
Source code in fastvideo/models/mask_utils.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |