vllm.model_executor.layers.fla.ops.fused_recurrent ΒΆ
FusedRecurrentFunction ΒΆ
Bases: Function
Source code in vllm/model_executor/layers/fla/ops/fused_recurrent.py
forward staticmethod
ΒΆ
forward(
ctx,
q: Tensor,
k: Tensor,
v: Tensor,
g: Tensor,
beta: Tensor,
scale: float,
initial_state: Tensor,
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,
)
Source code in vllm/model_executor/layers/fla/ops/fused_recurrent.py
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 |
|
fused_recurrent_gated_delta_rule_fwd ΒΆ
fused_recurrent_gated_delta_rule_fwd(
q: Tensor,
k: Tensor,
v: Tensor,
g: Tensor,
beta: Tensor,
scale: float,
initial_state: Tensor,
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]
Source code in vllm/model_executor/layers/fla/ops/fused_recurrent.py
fused_recurrent_gated_delta_rule_fwd_kernel ΒΆ
fused_recurrent_gated_delta_rule_fwd_kernel(
q,
k,
v,
g,
beta,
o,
h0,
ht,
cu_seqlens,
ssm_state_indices,
num_accepted_tokens,
scale,
N: int64,
T: int64,
B: constexpr,
H: constexpr,
HV: constexpr,
K: constexpr,
V: constexpr,
BK: constexpr,
BV: constexpr,
stride_init_state_token: constexpr,
stride_final_state_token: constexpr,
stride_indices_seq: constexpr,
stride_indices_tok: constexpr,
USE_INITIAL_STATE: constexpr,
INPLACE_FINAL_STATE: constexpr,
IS_BETA_HEADWISE: constexpr,
USE_QK_L2NORM_IN_KERNEL: constexpr,
IS_VARLEN: constexpr,
IS_CONTINUOUS_BATCHING: constexpr,
IS_SPEC_DECODING: constexpr,
)
Source code in vllm/model_executor/layers/fla/ops/fused_recurrent.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 |
|