+
    1þÎjPX  ã                   ó&  € R t ^ RIt^ RIHt ^ RIHtHt ^ RIt]P                  ! ]	4      t
. R0OtR R lt]! ^R7      R R	 l4       tR1R R llt ! R R]4      t]P                   P#                  R/ R7      R2R R ll4       t]P&                  R2R R ll4       tRRRRRR3RR
RRRRRR/R R llt]P                   P#                  RR0R7      R2R R  ll4       t]P&                  R2R! R" ll4       tRRRRRR3RR
RRRRRR/R# R$ lltR% R& lt]P                   P#                  R'/ R7      R4R( R) ll4       t]P&                  R4R* R+ ll4       tR, R- lt]P;                  ]]R.7       ]P<                  P?                  ]P@                  PB                  PD                  4       ^ R/I#H$t$H%t%H&t&H't' ]%]']P@                  PP                  P$                  &   ]&]']P@                  PP                  P,                  &   ]$]']P@                  PP                  P4                  &   R# )5zÊ
Variable-length attention implementation using Flash Attention.

This module provides a high-level Python interface for variable-length attention
that calls into the optimized Flash Attention kernels.
N)Ú	lru_cache)ÚAnyÚ
NamedTupleÚ
AuxRequestc                ój   € V ^8„  d   QhR\         \        ,          R,          R\         \        ,          /# )é   Úwindow_sizeNÚreturn)ÚlistÚint)Úformats   "ÚO/opt/data/home/.local/lib/python3.14/site-packages/torch/nn/attention/varlen.pyÚ__annotate__r      s'   € ÷ ñ ¬¬S­	°DÕ(8ð ¼TÄ#½Yñ ó    c                 ód   € V f   RR.p \        V 4      ^8w  d   \        R\        V 4       24      hV # )Nz$window_size must have length 2, got éÿÿÿÿ)ÚlenÚ
ValueError)r   s   &r   Ú_normalize_window_sizer      s=   € ØÒØ˜2�hˆä
ˆ;Ó˜1ÔÜÐ?ÄÀKÓ@PÐ?QÐRÓSÐSØÐr   )Úmaxsizec                ó0   € V ^8„  d   QhR\         R\        /# )r   Údevice_indexr	   )r   Úbool)r   s   "r   r   r      s   € ÷ 
ñ 
¤Cð 
¬Dñ 
r   c                ó  € \         P                  P                  e   R# \         P                  P                  P                  4       pVe   VR8  d   R# \         P
                  P                  V 4      ^ ,          pV^	8X  g   V^
8X  d   R# R# )z;Cache device capability check to avoid repeated CUDA calls.Fi˜f T)ÚtorchÚversionÚhipÚbackendsÚcudnnÚcudaÚget_device_capability)r   Úcudnn_versionÚ	major_caps   &  r   Ú_should_use_cudnnr#      sk   € ô ‡}�}×ÑÒ$ÙÜ—N‘N×(Ñ(×0Ñ0Ó2€MØÒ °Ô 5ÙÜ—
‘
×0Ñ0°Ó>¸qÕA€IØ�A„~˜ bœÙÙr   Fc                ó4  € V ^8„  d   QhR\         P                  R\         P                  R\         P                  R\        R\        \        ,          R\        R\         P                  R,          R	\         P                  R,          R
\        R,          R\        /
# )r   ÚqueryÚkeyÚvalueÚmax_qr   Ú
enable_gqaÚ	seqused_kNÚblock_tableÚ
num_splitsr	   )r   ÚTensorr   r
   r   )r   s   "r   r   r   +   s’   € ÷ ñ Ü�<‰<ðä	�‰ðô �<‰<ðô ð	ô
 ”c•ðô ðô �|‰|˜dÕ"ðô —‘ Õ$ðô �d•
ðô 
ñr   c	                 óü  € V P                   '       d&   \        V P                  P                  4      '       g   R # V^€8:  d   R # V P                  \
        P                  \
        P                  39  d   R # V P                  R,          ^,          ^ 8w  g    VP                  R,          ^,          ^ 8w  d   R # VRR.8w  d   R # V'       g&   V P                  R4      VP                  R4      8w  d   R # Vf	   Ve   Vf   R # R# )FTr   éþÿÿÿ)
Úis_cudar#   ÚdeviceÚindexÚdtyper   Úfloat16Úbfloat16ÚshapeÚsize)	r%   r&   r'   r(   r   r)   r*   r+   r,   s	   &&&&&&&&&r   Ú_can_use_cudnnr8   +   s¹   € ð �=�=ˆ=Ô 1°%·,±,×2DÑ2D× EÒ EÙØ�„|ÙØ‡{�{œ5Ÿ=™=¬%¯.©.Ð9Ô9ÙØ‡{�{�2…˜Õ˜aÔ 5§;¡;¨r¥?°QÕ#6¸!Ô#;ÙØ�r˜2�hÔÙß�U—Z‘Z “^ s§x¡x°£|Ô3ÙØÒ +Ò"9¸iÒ>OÙÙr   c                   ó4   a € ] tR t^Gt o RtRtV 3R ltRtV tR# )r   z”
Request which auxiliary outputs to compute from varlen_attn.

Each field is a boolean indicating whether that auxiliary output should be computed.
Fc                ó&   <€ V ^8„  d   Qh/ S[ ;R&   # )r   Úlse)r   )r   Ú__classdict__s   "€r   r   ÚAuxRequest.__annotate__G   s   ø‡ ‚ ñ 
Ñò r   © N)	Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r;   Ú__annotate_func__Ú__static_attributes__Ú__classdictcell__)r<   s   @r   r   r   G   s   ø‡ € ñð €C÷ ƒ r   ztorch_attn::_varlen_attn)Úmutates_argsc          !      ó*  € V ^8„  d   QhR\         P                  R\         P                  R\         P                  R\         P                  R\         P                  R,          R\        R\        R	\        R
\        R,          R\
        \        ,          R,          R\        R\         P                  R,          R\         P                  R,          R\        R,          R\        \         P                  \         P                  \         P                  3,          /# ©r   r%   r&   r'   Úcu_seq_qÚcu_seq_kNr(   Úmax_kÚ	is_causalÚscaler   r)   r*   r+   r,   r	   ©r   r-   r   r   Úfloatr
   Útuple)r   s   "r   r   r   R   s  € ÷ O+ñ O+Ü�<‰<ðO+ä	�‰ðO+ô �<‰<ðO+ô �l‰lð	O+ô
 �l‰l˜TÕ!ðO+ô ðO+ô ðO+ô ðO+ô �4�<ðO+ô ”c•˜TÕ!ðO+ô ðO+ô �|‰|˜dÕ"ðO+ô —‘ Õ$ðO+ô �d•
ðO+ô Œ5�<‰<œŸ™¤u§|¡|Ð3Õ4ñO+r   c                óP  € \        V	4      p	\        V VVVV	V
VVV4	      pV'       dj   \        P                  R4       \        P
                  P                  P                  V VVRVVVVRRVRVVVR7      pV^ ,          V^,          V^,          pppMb\        P                  R4       \        P
                  P                  P                  V VVVVVVRVRVV	^ ,          V	^,          VVVR7      w  ppp p\        P                  ! R
\        P                  V P                  R	7      pVVV3# )z’
Private custom op for variable-length attention.

This is the internal implementation. Users should use the public varlen_attn function instead.
ú#Using cuDNN backend for varlen_attnNTç        F)r%   r&   r'   Ú	attn_biasÚ	cum_seq_qÚ	cum_seq_kr(   rL   Úcompute_log_sumexpÚ	dropout_prM   Úreturn_debug_maskrN   r*   r+   ú-Using Flash Attention backend for varlen_attn)rZ   rN   Úwindow_size_leftÚwindow_size_rightr*   r+   r,   ©r3   r1   ©r   )r   r8   ÚlogÚinfor   ÚopsÚatenÚ_cudnn_attention_forwardÚ_flash_attention_forwardÚzerosÚuint64r1   )r%   r&   r'   rJ   rK   r(   rL   rM   rN   r   r)   r*   r+   r,   Ú	use_cudnnÚresultÚoutputÚsoftmax_lseÚ	rng_stateÚ_Ú
rng_state_s   &&&&&&&&&&&&&&       r   Ú_varlen_attnro   Q   sH  € ô, )¨Ó5€KÜØØØØØØØØØó
€I÷ Ü�‰Ð6Ô7Ü—‘—‘×8Ñ8ØØØØØØØØØ#ØØØ#ØØØ#ð 9ó 
ˆð$ *0°­°F¸1µI¸vÀa½y˜Y�ˆ˜Yä�‰Ð@ÔAÜ/4¯y©y¯~©~×/VÑ/VØØØØØØØØØØ#ØØ(¨�^Ø)¨!�nØØ#Ø!ð! 0Wó 0
Ñ,ˆ�˜Y¨¨1ô& —’Ø”E—L‘L¨¯©ô€Jð �; 
Ð*Ð*r   c          !      ó*  € V ^8„  d   QhR\         P                  R\         P                  R\         P                  R\         P                  R\         P                  R,          R\        R\        R	\        R
\        R,          R\
        \        ,          R,          R\        R\         P                  R,          R\         P                  R,          R\        R,          R\        \         P                  \         P                  \         P                  3,          /# rI   rO   )r   s   "r   r   r   ¥   sò   € ÷ %(ñ %(Ü�<‰<ð%(ä	�‰ð%(ô �<‰<ð%(ô �l‰lð	%(ô
 �l‰l˜TÕ!ð%(ô ð%(ô ð%(ô ð%(ô �4�<ð%(ô ”c•˜TÕ!ð%(ô ð%(ô �|‰|˜dÕ"ð%(ô —‘ Õ$ð%(ô �d•
ð%(ô Œ5�<‰<œŸ™¤u§|¡|Ð3Õ4ñ%(r   c                óZ  € \        V	4      p	\        P                  ! V 4      pV P                  ^ 4      pV P                  ^4      p\        P                  ! VV3\        P
                  V P                  R7      p\        P                  ! R\        P                  V P                  R7      pVVV3# )zÓ
Fake implementation for meta tensor computation and tracing.

Based on the 3D varlen path from meta__flash_attention_forward:
- query shape: (total, num_heads, head_dim)
- logsumexp shape: (num_heads, total_q)
r^   r_   )r   r   Ú
empty_liker7   ÚemptyrP   r1   rg   )r%   r&   r'   rJ   rK   r(   rL   rM   rN   r   r)   r*   r+   r,   rj   Útotal_qÚ	num_headsÚ	logsumexprl   s   &&&&&&&&&&&&&&     r   Ú_varlen_attn_fakerw   ¤   s‡   € ô0 )¨Ó5€Kô ×Ò˜eÓ$€Fð �j‰j˜‹m€GØ—
‘
˜1“€IÜ—’Ø	�GÐ¤E§K¡K¸¿¹ô€Iô —’˜D¬¯©¸U¿\¹\ÔJ€Ià�9˜iÐ'Ð'r   Ú
return_auxrN   r   r)   r*   r+   r,   c          !      óB  € V ^8„  d   QhR\         P                  R\         P                  R\         P                  R\         P                  R\         P                  R,          R\        R\        R	\        R,          R
\        R,          R\
        \        \        3,          R\        R\         P                  R,          R\         P                  R,          R\        R,          R\         P                  \
        \         P                  \         P                  3,          ,          /# )r   r%   r&   r'   rJ   rK   Nr(   rL   rx   rN   r   r)   r*   r+   r,   r	   ©r   r-   r   r   rP   rQ   r   )r   s   "r   r   r   Í   s
  € ÷ Vñ VÜ�<‰<ðVä	�‰ðVô �<‰<ðVô �l‰lð	Vô
 �l‰l˜TÕ!ðVô ðVô ðVô ˜TÕ!ðVô �4�<ðVô ”sœC�x•ðVô ðVô �|‰|˜dÕ"ðVô —‘ Õ$ðVô �d•
ðVô  ‡\�\”Eœ%Ÿ,™,¬¯©Ð4Õ5Õ5ñ!Vr   c               óÆ  € V P                  ^4      pVe   VP                  ^4      MVP                  ^4      pV
'       g   Wï8w  d   \        RV RV R24      hV
'       d    Wï,          ^ 8w  d   \        RV RV R24      hV	R8H  p\        P                  P                  P                  V VVVVVVVV\        V	4      V
VVV4      w  pppVe   VP                  '       d   VV3# V# )a-  Compute variable-length attention using Flash Attention.

This function is similar to scaled_dot_product_attention but optimized for
variable-length sequences using cumulative sequence position tensors.

Args:
    query (Tensor): Query tensor; shape :math:`(T_q, H_q, D)`
    key (Tensor): Key tensor; shape :math:`(T_k, H_{kv}, D)`, or
        :math:`(\text{total\_pages}, \text{page\_size}, H_{kv}, D)` when ``block_table`` is provided.
    value (Tensor): Value tensor; shape :math:`(T_k, H_{kv}, D)`, or
        :math:`(\text{total\_pages}, \text{page\_size}, H_{kv}, D)` when ``block_table`` is provided.
    cu_seq_q (Tensor): Cumulative sequence positions for queries; shape :math:`(N+1,)`
    cu_seq_k (Tensor): Cumulative sequence positions for keys/values; shape :math:`(N+1,)`
    max_q (int): Maximum query sequence length in the batch.
    max_k (int): Maximum key/value sequence length in the batch.
    return_aux (Optional[AuxRequest]): If not None and ``return_aux.lse`` is True, also returns the logsumexp tensor.
    scale (float, optional): Scaling factor for attention scores
    window_size (tuple[int, int], optional): Window size for sliding window attention as (left, right).
        Use (-1, -1) for full attention (default), (-1, 0) for causal attention,
        or (W, 0) for causal attention with sliding window of size W.
    enable_gqa (bool): If set to True, enables Grouped Query Attention (GQA)
        and allows key/value to have fewer heads than query.
        Each KV head is shared by a group of :math:`H_q / H_{kv}` query heads,
        so :math:`H_q` must be divisible by :math:`H_{kv}`.
        Default is False.
    seqused_k (Tensor, optional): Number of valid KV tokens per batch element; shape :math:`(N,)`.
        When set, only the first ``seqused_k[i]`` tokens in the key/value sequence for batch
        element *i* participate in attention. Useful for KV-cache decoding where the cache slot
        is larger than the actual sequence. Inference-only (not supported in backward).
    block_table (Tensor, optional): Block table for paged KV cache; shape
        :math:`(N, \text{max\_pages\_per\_seq})`, dtype ``int32``.
        Requires ``seqused_k``. Inference-only (not supported in backward).

        When ``block_table`` is provided, ``key`` and ``value`` are a "pool" of
        pages of tokens of KV data and the pages belong to any sequence/order.
        The ``block_table`` is what maps each sequence's logical chunks
        back to physical pages in this pool.

        ``seqused_k[i]`` tells the kernel how many tokens in sequence *i* are
        actually valid, since the last page is typically only partially filled.
    num_splits (int, optional): Number of splits for split-KV. Set to ``1``
        to disable split-KV which enables batch invariance. Split-KV
        parallelizes the key/value sequence dimension across multiple thread
        blocks and combines partial results. The split decision depends
        on ``max_k`` (the longest sequence in the batch), so different batch
        compositions can change the reduction order and produce different
        floating-point results for the same sequence. When this is disabled,
        bitwise identical outputs are guaranteed for a given sequence
        regardless of what other sequences are in the batch, at the
        cost of lower GPU utilization when there are few queries. When
        ``None`` (default), the kernel chooses automatically.

Returns:
    output (Tensor): Output tensor from attention computation; shape :math:`(T_q, H_q, D)`.

    If ``return_aux`` is not None and ``return_aux.lse`` is True:
        lse (Tensor): Log-sum-exp of attention scores; shape :math:`(H_q, T_q)`.

Shape legend:
    - :math:`N`: Batch size
    - :math:`T_q`: Total number of query tokens in the batch (sum of all query sequence lengths)
    - :math:`T_k`: Total number of key/value tokens in the batch (sum of all key/value sequence lengths)
    - :math:`H_q`: Number of query attention heads
    - :math:`H_{kv}`: Number of key/value attention heads (equal to :math:`H_q` unless GQA is enabled)
    - :math:`D`: Head dimension

Example::

    >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
    >>> batch_size, max_seq_len, embed_dim, num_heads = 2, 512, 1024, 16
    >>> head_dim = embed_dim // num_heads
    >>> seq_lengths = []
    >>> for _ in range(batch_size):
    ...     length = torch.randint(1, max_seq_len // 64 + 1, (1,)).item() * 64
    ...     seq_lengths.append(min(length, max_seq_len))
    >>> seq_lengths = torch.tensor(seq_lengths, device="cuda")
    >>> total_tokens = seq_lengths.sum().item()
    >>>
    >>> # Create packed query, key, value tensors
    >>> query = torch.randn(
    ...     total_tokens, num_heads, head_dim, dtype=torch.float16, device="cuda"
    ... )
    >>> key = torch.randn(
    ...     total_tokens, num_heads, head_dim, dtype=torch.float16, device="cuda"
    ... )
    >>> value = torch.randn(
    ...     total_tokens, num_heads, head_dim, dtype=torch.float16, device="cuda"
    ... )
    >>>
    >>> # Build cumulative sequence tensor
    >>> cu_seq = torch.zeros(batch_size + 1, device="cuda", dtype=torch.int32)
    >>> cu_seq[1:] = seq_lengths.cumsum(0)
    >>> max_len = seq_lengths.max().item()
    >>>
    >>> # Call varlen_attn
    >>> output = varlen_attn(
    ...     query, key, value, cu_seq, cu_seq, max_len, max_len
    ... )
úGExpect query and key/value to have the same number of heads but got Hq=ú	 and Hkv=ú&. Try setting enable_gqa=True for GQA.úMExpect number of query heads to be a multiple of kv heads for GQA but got Hq=Ú.©r   é    )r7   r   r   rb   Ú
torch_attnro   r
   r;   )r%   r&   r'   rJ   rK   r(   rL   rx   rN   r   r)   r*   r+   r,   Únum_heads_qÚnum_heads_krM   Úoutr;   rm   s   &&&&&&&$$$$$$$      r   Úvarlen_attnr‡   Í   s	  € ðj —*‘*˜Q“-€KØ!,Ò!8�#—(‘(˜1”+¸c¿h¹hÀq»k€Kß˜+Ô4ÜðØ%˜ i°¨}ð =3ð4ó
ð 	
÷
 �kÕ/°1Ô4ÜðØ%˜ i°¨}¸Að?ó
ð 	
ð
 ˜wÑ&€IÜ—)‘)×&Ñ&×3Ñ3ØØØØØØØØØÜˆ[ÓØØØØó�K€Cˆˆað  Ò *§.§. .Ø�CˆxˆØ€Jr   ztorch_attn::_varlen_attn_outr†   c                 óö  € V ^8„  d   QhR\         P                  R\         P                  R\         P                  R\         P                  R\         P                  R\         P                  R,          R\        R	\        R
\        R\        R,          R\
        \        ,          R,          R\        R\         P                  R,          R\         P                  R,          R\        R,          R\         P                  /# ©r   r†   r%   r&   r'   rJ   rK   Nr(   rL   rM   rN   r   r)   r*   r+   r,   r	   ©r   r-   r   r   rP   r
   )r   s   "r   r   r   g  sì   € ÷ ,ñ ,Ü	�‰ð,ä�<‰<ð,ô 
�‰ð,ô �<‰<ð	,ô
 �l‰lð,ô �l‰l˜TÕ!ð,ô ð,ô ð,ô ð,ô �4�<ð,ô ”c•˜TÕ!ð,ô ð,ô �|‰|˜dÕ"ð,ô —‘ Õ$ð,ô �d•
ð,ô  ‡\�\ñ!,r   c                óÖ   € \        V
4      p
\        P                  R4       \        P                  P
                  P                  V VVVVVVVRVRV	V
^ ,          V
^,          VVVR7      pV# )zŸ
Private custom op for variable-length attention with pre-allocated output.
Same as _varlen_attn but writes the attention output into the provided out tensor.
z1Using Flash Attention backend for varlen_attn_outrT   F)rN   r\   r]   r*   r+   r,   )r   r`   ra   r   rb   rc   Ú+_flash_attention_forward_no_dropout_inplace)r†   r%   r&   r'   rJ   rK   r(   rL   rM   rN   r   r)   r*   r+   r,   rk   s   &&&&&&&&&&&&&&& r   Ú_varlen_attn_outr�   f  s|   € ô, )¨Ó5€Kä‡H�HÐ@ÔAÜ—)‘)—.‘.×LÑLØØØØØØØØØØØØØ$ Q�Ø% a�.ØØØð# Mó €Kð( Ðr   c                 óö  € V ^8„  d   QhR\         P                  R\         P                  R\         P                  R\         P                  R\         P                  R\         P                  R,          R\        R	\        R
\        R\        R,          R\
        \        ,          R,          R\        R\         P                  R,          R\         P                  R,          R\        R,          R\         P                  /# r‰   rŠ   )r   s   "r   r   r   —  sì   € ÷ ñ Ü	�‰ðä�<‰<ðô 
�‰ðô �<‰<ð	ô
 �l‰lðô �l‰l˜TÕ!ðô ðô ðô ðô �4�<ðô ”c•˜TÕ!ðô ðô �|‰|˜dÕ"ðô —‘ Õ$ðô �d•
ðô  ‡\�\ñ!r   c                ó°   € VP                  ^ 4      pVP                  ^4      p\        P                  ! VV3\        P                  VP                  R7      pV# )ú>
Fake implementation for meta tensor computation and tracing.
r^   )r7   r   rs   rP   r1   )r†   r%   r&   r'   rJ   rK   r(   rL   rM   rN   r   r)   r*   r+   r,   rt   ru   rv   s   &&&&&&&&&&&&&&&   r   Ú_varlen_attn_out_faker‘   –  sH   € ð* �j‰j˜‹m€GØ—
‘
˜1“€IÜ—’Ø	�GÐ¤E§K¡K¸¿¹ô€Ið Ðr   c          #      ób  € V ^8„  d   QhR\         P                  R\         P                  R\         P                  R\         P                  R\         P                  R\         P                  R,          R\        R	\        R
\        R,          R\        R,          R\
        \        \        3,          R\        R\         P                  R,          R\         P                  R,          R\        R,          R\         P                  \
        \         P                  \         P                  3,          ,          /# )r   r†   r%   r&   r'   rJ   rK   Nr(   rL   rx   rN   r   r)   r*   r+   r,   r	   rz   )r   s   "r   r   r   ´  s  € ÷ :ñ :Ü	�‰ð:ä�<‰<ð:ô 
�‰ð:ô �<‰<ð	:ô
 �l‰lð:ô �l‰l˜TÕ!ð:ô ð:ô ð:ô ˜TÕ!ð:ô �4�<ð:ô ”sœC�x•ð:ô ð:ô �|‰|˜dÕ"ð:ô —‘ Õ$ð:ô  �d•
ð!:ô" ‡\�\”Eœ%Ÿ,™,¬¯©Ð4Õ5Õ5ñ#:r   c               óÄ  € VP                  ^4      pVe   VP                  ^4      MVP                  ^4      pV'       g   VV8w  d   \        RV RV R24      hV'       d!   VV,          ^ 8w  d   \        RV RV R24      hV
R8H  p\        P                  P                  P                  V VVVVVVVVV	\        V
4      VVVV4      pVe   VP                  '       d   V V3# V # )zÜCompute variable-length attention using Flash Attention with a pre-allocated output tensor.

Same as :func:`varlen_attn` but writes the attention output into the provided ``out`` tensor
instead of allocating a new one.

r|   r}   r~   r   r€   r�   )r7   r   r   rb   rƒ   r�   r
   r;   )r†   r%   r&   r'   rJ   rK   r(   rL   rx   rN   r   r)   r*   r+   r,   r„   r…   rM   r;   s   &&&&&&&&$$$$$$$    r   Úvarlen_attn_outr”   ´  s	  € ð0 —*‘*˜Q“-€KØ!,Ò!8�#—(‘(˜1”+¸c¿h¹hÀq»k€Kß˜+¨Ô4ÜðØ%˜ i°¨}ð =3ð4ó
ð 	
÷
 �k KÕ/°1Ô4ÜðØ%˜ i°¨}¸Að?ó
ð 	
ð
 ˜wÑ&€IÜ
�)‰)×
Ñ
×
/Ñ
/ØØØØØØØØØØÜˆ[ÓØØØØó€Cð" Ò *§.§. .Ø�CˆxˆØ€Jr   c                óZ   € V ^8„  d   QhR\         R\        \         R3,          R\         RR/# )r   ÚctxÚinputs.rj   r	   N)r   rQ   )r   s   "r   r   r   ñ  s0   € ÷ "ñ "œð "¤U¬3°¨8¥_ð "¼cð "Àdñ "r   c                 óØ   € Vw  ppppppp	p
ppppppVw  pppVe   \        R4      hVe   \        R4      hV P                  W4WVVVVV4       W€n        W�n        W n        W°n        WÀn        R # )Nz)seqused_k is an inference-only parameter.z+block_table is an inference-only parameter.)ÚRuntimeErrorÚsave_for_backwardr(   rL   rM   rN   r   )r–   r—   rj   r%   r&   r'   rJ   rK   r(   rL   rM   rN   r   r)   r*   r+   r,   r†   r;   rl   s   &&&                 r   Ú_setup_contextr›   ñ  s—   € ð  	ñØØØØØØØØØØØØØØà Ñ€CˆˆiàÒÜÐFÓGÐGØÒÜÐHÓIÐIà×Ñ˜% e°xÀÀcÈ9ÔUà„IØ„IØ„MØ„IØ!†Or   z!torch_attn::_varlen_attn_backwardc          !      ó  € V ^8„  d   QhR\         P                  R\         P                  R\         P                  R\         P                  R\         P                  R\         P                  R\         P                  R\         P                  R	\        R
\        R\        R\         P                  R\        R,          R\
        \        ,          R,          R\        \         P                  \         P                  \         P                  3,          /# ©r   Úgrad_outr%   r&   r'   r†   r;   rJ   rK   r(   rL   rM   rl   rN   Nr   r	   rO   )r   s   "r   r   r     sø   € ÷ Cñ CÜ�l‰lðCä�<‰<ðCô 
�‰ðCô �<‰<ð	Cô
 
�‰ðCô 
�‰ðCô �l‰lðCô �l‰lðCô ðCô ðCô ðCô �|‰|ðCô �4�<ðCô ”c•˜TÕ!ðCô Œ5�<‰<œŸ™¤u§|¡|Ð3Õ4ñCr   c                 óF  € \        V4      p\        P                  ! ^ VP                  R7      p\	        WW8V4      pV'       d{   \
        P                  R4       V^ ,          R	8w  g   V^,          R	8w  d   \        R4      h\        P                  P                  P                  V VVVVVVVVV	RVVRV
VR7      w  pppMa\
        P                  R4       \        P                  P                  P                  V VVVVVVVVV	RV
VVVV^ ,          V^,          R7      w  pppVVV3# )
r‚   )r1   rS   zTcuDNN backend does not support window attention. Please use Flash Attention backend.rT   N)rž   r%   r&   r'   r†   rv   rV   rW   r(   rL   rY   Úphilox_seedÚphilox_offsetrU   rM   rN   r[   )rN   r\   r]   r   )r   r   rs   r1   r8   r`   ra   r™   rb   rc   Ú_cudnn_attention_backwardÚ_flash_attention_backward)rž   r%   r&   r'   r†   r;   rJ   rK   r(   rL   rM   rl   rN   r   Úunusedrh   ÚdqÚdkÚdvs   &&&&&&&&&&&&&&     r   Ú_varlen_attn_backwardr¨     s7  € ô" )¨Ó5€Kä�[Š[˜ 5§<¡<Ô0€Fä˜u¨5¸ÓE€IçÜ�‰Ð6Ô7Ø�q�>˜RÔ ;¨q¥>°RÔ#7ÜØfóð ô —Y‘Y—^‘^×=Ñ=ØØØØØØØØØØØØ!Ø#ØØØð! >ó 
‰
ˆˆB‘ô& 	�‰Ð@ÔAÜ—Y‘Y—^‘^×=Ñ=ØØØØØØØØØØØØØØØØ(¨�^Ø)¨!�nð# >ó 
‰
ˆˆB�ð& ˆr�2ˆ:Ðr   c          !      ó  € V ^8„  d   QhR\         P                  R\         P                  R\         P                  R\         P                  R\         P                  R\         P                  R\         P                  R\         P                  R	\        R
\        R\        R\         P                  R\        R,          R\
        \        ,          R,          R\        \         P                  \         P                  \         P                  3,          /# r�   rO   )r   s   "r   r   r   Z  sç   € ÷ ,ñ ,Ü�l‰lð,ä�<‰<ð,ô 
�‰ð,ô �<‰<ð	,ô
 
�‰ð,ô 
�‰ð,ô �l‰lð,ô �l‰lð,ô ð,ô ð,ô ð,ô �|‰|ð,ô �4�<ð,ô ”c•˜TÕ!ð,ô Œ5�<‰<œŸ™¤u§|¡|Ð3Õ4ñ,r   c                ó¤   € \        V4      p\        P                  ! V4      p\        P                  ! V4      p\        P                  ! V4      pWïV3# )r�   )r   r   rr   )rž   r%   r&   r'   r†   r;   rJ   rK   r(   rL   rM   rl   rN   r   Ú
grad_queryÚgrad_keyÚ
grad_values   &&&&&&&&&&&&&&   r   Ú_varlen_attn_backward_faker®   Y  sI   € ô( )¨Ó5€Kä×!Ò! %Ó(€JÜ×Ò Ó$€HÜ×!Ò! %Ó(€Jà Ð+Ð+r   c                óÌ   € V ^8„  d   QhR\         R\        P                  R\        P                  R\        P                  R\        \        P                  R,          R3,          /# )r   r–   rž   Úgrad_lseÚgrad_rngr	   N.)r   r   r-   rQ   )r   s   "r   r   r   v  sS   € ÷ 1ñ 1Ü	ð1ÜŸ™ð1Ü05·±ð1ÜHMÏÉð1ä
Œ5�<‰<˜$Õ Ð#Õ$ñ1r   c                 ó2  € V P                   w  rErgr‰r«V P                  pV P                  pV P                  pV P                  pV P
                  p\        P                  P                  P                  VVVVV	V
VVVVVVVV4      w  ppp^pVVV.RV,          O5# )é   )N)
Úsaved_tensorsr(   rL   rM   rN   r   r   rb   rƒ   r¨   )r–   rž   r°   r±   r%   r&   r'   rJ   rK   r†   r;   rl   r(   rL   rM   rN   r   r¥   r¦   r§   Ú
num_paramss   &&&&                 r   Ú	_backwardr¶   v  s¯   € ð BE×ARÑARÑ>€E� °à�I‰I€EØ�I‰I€EØ—‘€IØ�I‰I€EØ—/‘/€Kä—‘×%Ñ%×;Ñ;ØØØØØØØØØØØØØØó�J€BˆˆBð$ €JØ��BÐ0˜' JÕ.Ñ0Ð0r   )Úsetup_context)Ú_varlen_attn_backward_flopÚ_varlen_attn_forward_flopÚ_varlen_attn_out_flopÚflop_registry)r‡   r”   r   )FNNN)FNNFNNN)r   r   )NN))rC   ÚloggingÚ	functoolsr   Útypingr   r   r   Ú	getLoggerr?   r`   Ú__all__r   r#   r8   r   ÚlibraryÚ	custom_opro   Úregister_fakerw   r‡   r�   r‘   r”   r›   r¨   r®   r¶   Úregister_autogradÚ_dynamoÚdisallow_in_graphrb   rc   rŒ   Útorch.utils.flop_counterr¸   r¹   rº   r»   rƒ   r>   r   r   Ú<module>rÈ      ss  ðñó Ý ß "ã ð ×Ò˜Ó!€â
:€õñ �1Ôô
ó ð
÷ô8�ô ð ‡�×ÑÐ3À"ÐÓEöO+ó FðO+ðd ×Ñö%(ó ð%(ðPVð %)ðVð ðVð $,ðVð ðVð &*ðVð (,ðVð "÷Vðr ‡�×ÑÐ7ÀuÀgÐÓNö,ó Oð,ð^ ×Ñöó  ðð::ð %)ð:ð ð:ð $,ð:ð ð:ð &*ð:ð (,ð:ð  "÷!:õz"ðB ‡�×ÑÐ<È2ÐÓNöCó OðCðL ×$Ñ$ö,ó %ð,õ81ðB × Ñ ˜y¸Ð Ô Gà ‡�× Ñ Ø	‡I�I‡N�N×>Ñ>ô÷ó ð 4M€ˆe�i‰i×"Ñ"×/Ñ/Ñ 0Ø7L€ˆe�i‰i×"Ñ"×3Ñ3Ñ 4Ø<V€ˆe�i‰i×"Ñ"×8Ñ8Ó 9r   