vllm.model_executor.layers.fla.ops ΒΆ
Modules:
Name | Description |
---|---|
chunk | |
chunk_delta_h | |
chunk_o | |
chunk_scaled_dot_kkt | |
cumsum | |
fused_recurrent | |
index | |
l2norm | |
layernorm_guard | |
op | |
solve_tril | |
utils | |
wy_fast | |
__all__ module-attribute
ΒΆ
RMSNormGated ΒΆ
Bases: Module
Source code in vllm/model_executor/layers/fla/ops/layernorm_guard.py
__init__ ΒΆ
__init__(
hidden_size,
eps: float = 1e-05,
group_size: Optional[int] = None,
norm_before_gate: bool = False,
device: Optional[device] = None,
dtype: Optional[dtype] = None,
)
If group_size is not None, we do GroupNorm with each group having group_size elements. group_size=None is equivalent to group_size=hidden_size (i.e. there's only 1 group).
Source code in vllm/model_executor/layers/fla/ops/layernorm_guard.py
forward ΒΆ
If z is not None, we do norm(x) * silu(z) if norm_before_gate, else norm(x * silu(z))
Source code in vllm/model_executor/layers/fla/ops/layernorm_guard.py
chunk_gated_delta_rule ΒΆ
chunk_gated_delta_rule(
q: Tensor,
k: Tensor,
v: Tensor,
g: Tensor,
beta: Tensor,
scale: float = None,
initial_state: Tensor = None,
output_final_state: bool = False,
cu_seqlens: Optional[LongTensor] = None,
head_first: bool = False,
use_qk_l2norm_in_kernel: bool = False,
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q | Tensor | queries of shape | required |
k | Tensor | keys of shape | required |
v | Tensor | values of shape | required |
g | Tensor | (forget) gating tensor (in log space!) of shape | required |
beta | Tensor | betas of shape | required |
scale | Optional[int] | Scale factor for the RetNet attention scores. If not provided, it will default to | None |
initial_state | Optional[Tensor] | Initial state of shape | None |
output_final_state | Optional[bool] | Whether to output the final state of shape | False |
cu_seqlens | LongTensor | Cumulative sequence lengths of shape | None |
head_first | Optional[bool] | Whether the inputs are in the head-first format, which is not supported for variable-length inputs. Default: | False |
Returns:
Name | Type | Description |
---|---|---|
o | Tensor | Outputs of shape |
final_state | Tensor | Final state of shape |
Examples:: >>> import torch >>> import torch.nn.functional as F >>> from einops import rearrange >>> from fla.ops.gated_delta_rule import chunk_gated_delta_rule # inputs with equal lengths >>> B, T, H, K, V = 4, 2048, 4, 512, 512 >>> q = torch.randn(B, T, H, K, dtype=torch.bfloat16, device='cuda') >>> k = F.normalize(torch.randn(B, T, H, K, dtype=torch.bfloat16, device='cuda'), p=2, dim=-1) >>> v = torch.randn(B, T, H, V, dtype=torch.bfloat16, device='cuda') >>> beta = torch.rand(B, T, H, dtype=torch.bfloat16, device='cuda').sigmoid() >>> g = F.logsigmoid(torch.rand(B, T, H, dtype=torch.bfloat16, device='cuda')) >>> h0 = torch.randn(B, H, K, V, dtype=torch.bfloat16, device='cuda') >>> o, ht = chunk_gated_delta_rule( q, k, v, g, beta, initial_state=h0, output_final_state=True ) # for variable-length inputs, the batch size B
is expected to be 1 and cu_seqlens
is required >>> q, k, v, beta, g = map(lambda x: rearrange(x, 'b t ... -> 1 (b t) ...'), (q, k, v, beta, g)) # for a batch with 4 sequences, cu_seqlens
with 5 start/end positions are expected >>> cu_seqlens = q.new_tensor([0, 2048, 4096, 6144, 8192], dtype=torch.long) >>> o_var, ht_var = chunk_gated_delta_rule( q, k, v, g, beta, initial_state=h0, output_final_state=True, cu_seqlens=cu_seqlens )
Source code in vllm/model_executor/layers/fla/ops/chunk.py
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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
|
fused_recurrent_gated_delta_rule ΒΆ
fused_recurrent_gated_delta_rule(
q: Tensor,
k: Tensor,
v: Tensor,
g: Tensor,
beta: Tensor = None,
scale: float = None,
initial_state: Tensor = None,
inplace_final_state: bool = True,
cu_seqlens: Optional[LongTensor] = None,
ssm_state_indices: Optional[Tensor] = None,
num_accepted_tokens: Optional[Tensor] = None,
use_qk_l2norm_in_kernel: bool = False,
) -> tuple[Tensor, Tensor]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q | Tensor | queries of shape | required |
k | Tensor | keys of shape | required |
v | Tensor | values of shape | required |
g | Tensor | g (decays) of shape | required |
beta | Tensor | betas of shape | None |
scale | Optional[int] | Scale factor for the RetNet attention scores. If not provided, it will default to | None |
initial_state | Optional[Tensor] | Initial state of shape | None |
inplace_final_state | bool | bool: Whether to store the final state in-place to save memory. Default: | True |
cu_seqlens | LongTensor | Cumulative sequence lengths of shape | None |
ssm_state_indices | Optional[Tensor] | Indices to map the input sequences to the initial/final states. | None |
num_accepted_tokens | Optional[Tensor] | Number of accepted tokens for each sequence during decoding. | None |
Returns:
Name | Type | Description |
---|---|---|
o | Tensor | Outputs of shape |
final_state | Tensor | Final state of shape |
Examples:: >>> import torch >>> import torch.nn.functional as F >>> from einops import rearrange >>> from fla.ops.gated_delta_rule import fused_recurrent_gated_delta_rule # inputs with equal lengths >>> B, T, H, HV, K, V = 4, 2048, 4, 8, 512, 512 >>> q = torch.randn(B, T, H, K, device='cuda') >>> k = F.normalize(torch.randn(B, T, H, K, device='cuda'), p=2, dim=-1) >>> v = torch.randn(B, T, HV, V, device='cuda') >>> g = F.logsigmoid(torch.rand(B, T, HV, device='cuda')) >>> beta = torch.rand(B, T, HV, device='cuda').sigmoid() >>> h0 = torch.randn(B, HV, K, V, device='cuda') >>> o, ht = fused_gated_recurrent_delta_rule( q, k, v, g, beta, initial_state=h0, ) # for variable-length inputs, the batch size B
is expected to be 1 and cu_seqlens
is required >>> q, k, v, g, beta = map(lambda x: rearrange(x, 'b t ... -> 1 (b t) ...'), (q, k, v, g, beta)) # for a batch with 4 sequences, cu_seqlens
with 5 start/end positions are expected >>> cu_seqlens = q.new_tensor([0, 2048, 4096, 6144, 8192], dtype=torch.long) >>> o_var, ht_var = fused_gated_recurrent_delta_rule( q, k, v, g, beta, initial_state=h0, cu_seqlens=cu_seqlens )
Source code in vllm/model_executor/layers/fla/ops/fused_recurrent.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
|