vllm.models.glm5next.nvidia.ops.third_party.kda.fused_recurrent ¶
Functions:
-
fused_recurrent_gated_delta_rule–Args:
-
token_stride–Token stride (elements) of a
[B, T, H, D]or[B, T, H]tensor.
fused_recurrent_gated_delta_rule(q, k, v, g, beta=None, scale=None, initial_state=None, inplace_final_state=True, cu_seqlens=None, ssm_state_indices=None, num_accepted_tokens=None, use_qk_l2norm_in_kernel=False) ¶
Parameters:
-
(q¶Tensor) –queries of shape
[B, T, H, K]. -
(k¶Tensor) –keys of shape
[B, T, H, K]. -
(v¶Tensor) –values of shape
[B, T, HV, V]. GVA is applied ifHV > H. -
(g¶Tensor) –g (decays) of shape
[B, T, HV]. -
(beta¶Tensor, default:None) –betas of shape
[B, T, HV]. -
(scale¶Optional[int], default:None) –Scale factor for the RetNet attention scores. If not provided, it will default to
1 / sqrt(K). Default:None. -
(initial_state¶Optional[Tensor], default:None) –Initial state of shape
[N, HV, V, K]forNinput sequences. For equal-length input sequences,Nequals the batch sizeB. Default:None. -
(inplace_final_state¶bool, default:True) –bool: Whether to store the final state in-place to save memory. Default:
True. -
(cu_seqlens¶Tensor, default:None) –Cumulative sequence lengths of shape
[N+1]used for variable-length training, consistent with the FlashAttention API. -
(ssm_state_indices¶Optional[Tensor], default:None) –Indices to map the input sequences to the initial/final states.
-
(num_accepted_tokens¶Optional[Tensor], default:None) –Number of accepted tokens for each sequence during decoding.
Returns:
-
o(Tensor) –Outputs of shape
[B, T, HV, V]. -
final_state(Tensor) –Final state of shape
[N, HV, V, K].
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, V, K, 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.int32) >>> 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/models/glm5next/nvidia/ops/third_party/kda/fused_recurrent.py
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 | |
token_stride(x) ¶
Token stride (elements) of a [B, T, H, D] or [B, T, H] tensor.
The recurrent kernel walks tokens with this stride and addresses heads densely inside a token, so each token's [H, D] (or [H]) block must be contiguous, tokens must not overlap, and with B > 1 sequence n must start at token n * T (dense batch). Column slices of a wider per-token projection buffer satisfy this and are consumed in place.