+
    1þÎjØ5  ã                   ó´  € R t ^ RIHtHt ^ RIHt ^ RIt^ RIHu H	t
 ^ RIHtHtHtHt ^ RIHt ^ RIHtHtHtHt . ROt]P0                  P3                  ]4       ]P0                  P3                  ]4       ]P0                  P3                  ]4       ]P0                  P3                  ]4        ! R	 R]4      t ! R
 R]P6                  4      tR R ltR R ltR# )zCDefines bias subclasses that work with scaled_dot_product_attention)ÚautoÚIntEnum)ÚwarnN)Úcan_use_efficient_attentionÚcan_use_flash_attentionÚis_flash_attention_availableÚ
SDPAParams)Ú_raise_kernel_warnings)Ú_calculate_scaleÚ_input_requires_gradÚ_postprocess_flash_outputÚ_validate_sdpa_inputÚCausalVariantÚ
CausalBiasc                   ó6   € ] tR t^!tRt]! 4       t]! 4       tRtR# )r   a·  
Enum for causal variants used in attention mechanisms.

Defines two types of causal biases:

``UPPER_LEFT``: Represents upper-left triangular bias for standard causal attention.
The equivalent pytorch code for constructing this bias is:

.. code-block:: python

    torch.tril(torch.ones(size, dtype=torch.bool))

For instance, with ``shape=(3,4)``, the materialized bias tensor will be:

.. code-block:: text

    [[1, 0, 0, 0],
     [1, 1, 0, 0],
     [1, 1, 1, 0]]


``LOWER_RIGHT``: Represents lower-right triangular bias, the include values are aligned to the lower
right corner of the matrix.

The equivalent pytorch code for constructing this bias is:

.. code-block:: python

    diagonal_offset = size[1] - size[0]
    torch.tril(
        torch.ones(size, dtype=torch.bool),
        diagonal=diagonal_offset,
    )

For instance, with ``shape=(3,4)``, the materialized bias tensor will be:

.. code-block:: text

    [[1, 1, 0, 0],
     [1, 1, 1, 0],
     [1, 1, 1, 1]]

Note that these variants are equivalent to each other when the sequence lengths of the query and key/value
tensors are equal since the triangular matrix is square.

.. warning:: This enum is a prototype and subject to change.
© N)	Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   Ú
UPPER_LEFTÚLOWER_RIGHTÚ__static_attributes__r   ó    ÚM/opt/data/home/.local/lib/python3.14/site-packages/torch/nn/attention/bias.pyr   r   !   s   † ñ.ñ` “€JÙ“&„Kr   c                   óÈ   a a€ ] tR t^Vt oRtV3R lV 3R lltV3R lR ltV3R lR ltRV3R lR	 llt]	RV3R
 lR ll4       t
]RV 3R ll4       tV3R lR ltRtVtV ;t# )r   aü  
A bias representing causal attention patterns. For an overview of the bias structure, see the :class:`CausalVariant` enum.

This class is used for defining causal (triangular) attention biases. For constructing the bias, there exist
two factory functions: :func:`causal_upper_left` and :func:`causal_lower_right`.

Example:

.. code-block:: python

    from torch.nn.attention.bias import causal_lower_right

    bsz, num_heads, seqlen_q, seqlen_kv, head_dim = 32, 8, 4, 12, 8

    # Create a lower-right causal bias
    attn_bias = causal_lower_right(seqlen_q, seqlen_kv)

    q = torch.randn(
        bsz, num_heads, seqlen_q, head_dim, device="cuda", dtype=torch.float16
    )
    k = torch.randn(
        bsz, num_heads, seqlen_kv, head_dim, device="cuda", dtype=torch.float16
    )
    v = torch.randn(
        bsz, num_heads, seqlen_kv, head_dim, device="cuda", dtype=torch.float16
    )

    out = F.scaled_dot_product_attention(q, k, v, attn_bias)

.. warning:: This class is a prototype and subject to change.
c                ó0   <€ V ^8„  d   QhRS[ RS[RS[RR/# )é   ÚvariantÚ	seq_len_qÚ
seq_len_kvÚreturnN)r   Úint)ÚformatÚ__classdict__s   "€r   Ú__annotate__ÚCausalBias.__annotate__w   s*   ø€ ÷ ñ ¡ð ¹#ð É3ð ÐSWñ r   c                ó  <€ \        V\        4      '       g"   \        R\        V4      P                   24      h\
        SV `  4        Wn        W n        W0n	        W#8”  d'   V\        P                  8X  d   \        R^R7       R# R# R# )a¦  
Initializes the CausalBias instance with a specified variant and sequence lengths.

Args:
    variant (CausalVariant): The type of causal bias to use (either UPPER_LEFT or LOWER_RIGHT).
    seq_len_q (int): The sequence length of the query tensor.
    seq_len_kv (int): The sequence length of the key/value tensor.

Raises a warning if the LOWER_RIGHT variant is used with seq_len_q > seq_len_kv, as it may produce NaNs.
z%variant must be a CausalVariant, got zTLower right causal bias will produce NaNs in the output when seq_len_q > seq_len_kv!)Ú
stacklevelN)Ú
isinstancer   ÚAssertionErrorÚtyper   ÚsuperÚ__init__r   r    r!   r   r   )Úselfr   r    r!   Ú	__class__s   &&&&€r   r.   ÚCausalBias.__init__w   sz   ø€ ô ˜'¤=×1Ò1Ü Ø7¼¸W»×8NÑ8NÐ7OÐPóð ô 	‰ÑÔØŒØ"ŒØ$ŒØÔ! g´×1JÑ1JÔ&JÜØfØ÷ñ 'KÑ!r   c                óN   <€ V ^8„  d   QhRS[ P                  RS[ P                  /# ©r   Údevicer"   ©Útorchr4   ÚTensor)r$   r%   s   "€r   r&   r'   �   s#   ø€ ÷ 
ñ 
¡%§,¡,ð 
±5·<±<ñ 
r   c           	     ó¢   € \         P                  ! \         P                  ! V P                  V P                  V\         P
                  R7      4      # )zUpper left causal bias©r4   Údtype)r6   ÚtrilÚonesr    r!   Úbool©r/   r4   s   &&r   Ú_upper_leftÚCausalBias._upper_left�   s1   € ä�zŠzÜ�JŠJ�t—~‘~ t§¡¸vÌUÏZÉZÔXó
ð 	
r   c                óN   <€ V ^8„  d   QhRS[ P                  RS[ P                  /# r3   r5   )r$   r%   s   "€r   r&   r'   –   s#   ø€ ÷ 
ñ 
¡5§<¡<ð 
±E·L±Lñ 
r   c           	     óà   € V P                   V P                  ,
          p\        P                  ! \        P                  ! V P                  V P                   V\        P
                  R7      VR7      # )zLower right causal biasr9   )Údiagonal)r!   r    r6   r;   r<   r=   )r/   r4   Údiagonal_offsets   && r   Ú_lower_rightÚCausalBias._lower_right–   sK   € àŸ/™/¨D¯N©NÕ:ˆÜ�zŠzÜ�JŠJØ—‘ §¡¸ÄeÇjÁjôð %ô	
ð 	
r   c                ó\   <€ V ^8„  d   QhRS[ P                  R,          RS[ P                  /# )r   r4   Nr"   r5   )r$   r%   s   "€r   r&   r'   ¡   s(   ø€ ÷ -ñ -¡5§<¡<°$Õ#6ð -Á%Ç,Á,ñ -r   c                óú   € Vf   \         P                  ! R4      pV P                  \        P                  8X  d   V P                  V4      # V P                  \        P                  8X  d   V P                  V4      # R# )aX  
Materializes the causal bias into a tensor form.

Depending on the variant, this method generates either an upper-left or lower-right
triangular matrix to represent the causal bias.

Args:
    device (Optional[torch.device]): The device on which to create the tensor. Defaults to CPU.

Returns:
    torch.Tensor: The materialized bias tensor.
NÚcpu)r6   r4   r   r   r   r?   r   rE   r>   s   &&r   Ú_materializeÚCausalBias._materialize¡   sb   € ð Š>Ü—\’\ %Ó(ˆFØ�<‰<œ=×3Ñ3Ô3Ø×#Ñ# FÓ+Ð+Ø�\‰\œ]×6Ñ6Ô6Ø×$Ñ$ VÓ,Ð,ñ 7r   c                ó¬   <€ V ^8„  d   QhRS[ P                  RS[ P                  RS[ P                  RRRS[RS[RS[R	,          R
S[RS[ P                  /	# )r   ÚqueryÚkeyÚvalueÚ	attn_maskr   Ú	dropout_pÚ	is_causalÚscaleNÚ
enable_gqar"   )r6   r7   Úfloatr=   )r$   r%   s   "€r   r&   r'   ¶   s‚   ø€ ÷ oñ oÙ�|‰|ðoá�\‰\ðoñ �|‰|ðoð  ð	oñ
 ðoñ ðoñ �t�|ðoñ ðoñ 
�‰ñor   c                óà  € V'       d   \        R4      hVP                  VP                  8X  g    VP                  \        P
                  8X  d   \        P                  ! V VVRVRVVR7      # VP                  \        P                  8X  EdM   \        WVRWEV4       \        WVRWEV4      p\        V4      '       Ed$   V P                  P                  R8X  d   ^M^p	V P                  R
4      p
\        W¦4      pW©,          ^ 8g  pV'       d”   WšV	,          ,
          p\         P"                  P$                  P'                  V ^ V34      p \         P"                  P$                  P'                  V^ V34      p\         P"                  P$                  P'                  V^ V34      p\         P(                  P*                  P-                  V VVVRRVR7      ^ ,          p\/        Wê4      # \1        V4      '       d§   Rp\3        WV4      '       d   Rp\         P(                  P*                  P5                  V P7                  ^^4      VP7                  ^^4      VP7                  ^^4      RRRRRV\9        VP                  4      VVRR7      ^ ,          P7                  ^^4      # \;        V4       \        P                  ! V VVVP=                  V P                  4      VRVVR7      # \        R	VP                   24      h)a   
Handles the logic for computing attention with the specified causal bias.

Args:
    query (Tensor): Query tensor; shape :math:`(N, ..., L, E)`.
    key (Tensor): Key tensor; shape :math:`(N, ..., S, E)`.
    value (Tensor): Value tensor; shape :math:`(N, ..., S, Ev)`.
    attn_mask (CausalBias): The type of causal attention to apply.
        A boolean mask where a value of True indicates that the element *should* take part in attention.
        A float mask of the same type as query, key, value that is added to the attention score.
    dropout_p (float): Dropout probability; if greater than 0.0, dropout is applied
    is_causal (bool): If true, assumes upper left causal attention masking and errors if both attn_mask and is_causal
        are set.
    scale (optional float): Scaling factor applied prior to softmax. If None, the default value is set
        to :math:`\frac{1}{\sqrt{E}}`.
    enable_gqa (optional bool): If set to True, Grouped Query Attention (GQA) is enabled, by default it is set to False.

Returns:
    output (Tensor): Attention output; shape :math:`(N, ..., L, Ev)`.

Raises:
    ValueError: If the causal bias variant is not a CausalVariant type.

z.CausalBias should not be used with causal=TrueNT)rP   rQ   rR   rS   rT   ÚxpuF)rR   Úreturn_debug_maskrS   )
ÚbiasÚcu_seqlens_qÚcu_seqlens_kÚmax_seqlen_qÚmax_seqlen_krQ   Úcustom_mask_typeÚcompute_log_sumexprS   Úseqlen_kz<CausalBias.variant must be a CausalVariant type, but found: éÿÿÿÿ)Ú
ValueErrorr    r!   r   r   r   ÚFÚscaled_dot_product_attentionr   r   r   r   r4   r,   Úsizer
   r6   ÚnnÚ
functionalÚpadÚopsÚatenÚ#_scaled_dot_product_flash_attentionr   r   r   Ú_efficient_attention_forwardÚ	transposer#   r	   rJ   )rM   rN   rO   rP   rQ   rR   rS   rT   Úsdpa_paramsÚ	alignmentÚog_head_sizeÚog_scaleÚneeds_paddingÚpad_lenÚoutr_   s   &&&&&&&&        r   Ú	_dispatchÚCausalBias._dispatchµ   s»  € ÷F ÜÐMÓNÐNð ×Ñ 9×#7Ñ#7Ô7Ø× Ñ ¤M×$<Ñ$<Ô<ä×1Ò1ØØØØØ#ØØØ%ô	ð 	ð ×Ñ¤-×";Ñ";Õ;Ü  ¨U°D¸)ÐPUÔVÜ$Ø˜E 4¨¸zóˆKô ' {×3Ó3Ø!&§¡×!2Ñ!2°eÔ!;™AÀ�	Ø$Ÿz™z¨"›~�Ü+¨LÓ@�Ø ,Õ 8¸AÑ =�ß Ø'¸)Õ+CÕD�GÜ!ŸH™H×/Ñ/×3Ñ3°E¸A¸w¸<ÓH�EÜŸ(™(×-Ñ-×1Ñ1°#¸¸7°|ÓD�CÜ!ŸH™H×/Ñ/×3Ñ3°E¸A¸w¸<ÓH�EÜ—i‘i—n‘n×HÑHØØØØØ"Ø&+Ø"ð Ió ð õ�ô 1°ÓCÐCÜ*¨;×7Ò7Ø%*Ð"Ü'¨°E×:Ò:Ø)-Ð&Ü—y‘y—~‘~×BÑBØ—O‘O A qÓ)Ø—M‘M ! QÓ'Ø—O‘O A qÓ)ØØ!%Ø!%Ø!%Ø!%Ø'Ü%(¨×):Ñ):Ó%;Ø'9ØØ!ð Có ð õ÷ ‘Y˜q !“_ð%ô  ' {Ô3ä×5Ò5ØØØØ'×4Ñ4°U·\±\ÓBØ'Ø#ØØ)ô	ð 	ô ØNÈy×O`ÑO`ÐNaÐbóð r   c                ó¤   <€ Vf   / pV\         P                  P                  P                  J d   V P                  ! V/ VB # \
        SV `  WW44      # )zjDefines the behavior of torch.nn.functional.scaled_dot_product_attention when the attn_bias is an AttnBias)r6   rf   rg   rd   ru   r-   Ú__torch_function__)ÚclsÚfuncÚtypesÚargsÚkwargsr0   s   &&&&&€r   rx   ÚCausalBias.__torch_function__'  sM   ø€ ð Š>ØˆFØ”5—8‘8×&Ñ&×CÑCÓCØ—=’= $Ð1¨&Ñ1Ð1Ü‰wÑ)¨$°tÓDÐDr   c                ó    <€ V ^8„  d   QhRS[ /# ©r   r"   )Ústr)r$   r%   s   "€r   r&   r'   0  s   ø€ ÷ .ñ .™#ñ .r   c                ó>   € V P                  4       P                  4       # ©N)rJ   Ú__repr__)r/   s   &r   r„   ÚCausalBias.__repr__0  s   € Ø× Ñ Ó"×+Ñ+Ó-Ð-r   )r!   r    r   rƒ   )g        FNF)r   N)r   r   r   r   r   r.   r?   rE   rJ   Ústaticmethodru   Úclassmethodrx   r„   r   Ú__classdictcell__Ú__classcell__)r0   r%   s   @@r   r   r   V   su   ù‡ € ñ÷@ó ÷2
ð 
÷
ð 
÷-ò -ð( ÷oñ oó ðoðb öEó ðE÷.÷ .ð .r   c                ó$   € V ^8„  d   QhR\         /# r€   ©r   )r$   s   "r   r&   r&   4  s   € ÷ Gñ G¤
ñ Gr   c                 ót   € \        V 4      ^8w  d   \        R4      hV w  r\        \        P                  W4      # )aã  
Creates an upper-left triangular causal bias.

This function generates an upper-left triangular matrix to represent causal attention bias with a
diagonal offset set so that the inclusive values are aligned to the upper left corner of the matrix.
This equivalent to the `is_causal=True` argument in `scaled_dot_product_attention`.

The equivalent pytorch code for constructing this bias is:

.. code-block:: python

    torch.tril(torch.ones(size, dtype=torch.bool))

For instance, with `shape=(3,4)`, the materialized bias tensor will be:

.. code-block:: text

    [[1, 0, 0, 0],
     [1, 1, 0, 0],
     [1, 1, 1, 0]]

Args:
    size: The size of the bias matrix.

Returns:
    CausalBias: The UPPER_LEFT triangular causal bias variant.
z*causal_upper_left only supports 2D tensors)Úlenr+   r   r   r   ©re   r    r!   s   *  r   Úcausal_upper_leftr�   4  s6   € ô8 ˆ4ƒy�A„~ÜÐIÓJÐJØ Ñ€IÜ”m×.Ñ.°	ÓFÐFr   c                ó$   € V ^8„  d   QhR\         /# r€   r‹   )r$   s   "r   r&   r&   V  s   € ÷ "Hñ "H¤ñ "Hr   c                 ót   € \        V 4      ^8w  d   \        R4      hV w  r\        \        P                  W4      # )aê  
Creates a lower-right triangular causal bias.

This function generates a lower-right triangular matrix to represent causal attention bias with a
diagonal offset set so that the inclusive values are aligned to the lower right corner of the matrix.

The equivalent pytorch code for constructing this bias is:

.. code-block:: python

    diagonal_offset = size[1] - size[0]
    torch.tril(
        torch.ones(size, dtype=torch.bool),
        diagonal=diagonal_offset,
    )

For instance, with `shape=(3,4)`, the materialized bias tensor will be:

.. code-block:: text

    [[1, 1, 0, 0],
     [1, 1, 1, 0],
     [1, 1, 1, 1]]

Args:
    size: The size of the bias matrix.

Returns:
    CausalBias: The LOWER_RIGHT triangular causal bias variant.
z+causal_lower_right only supports 2D tensors)r�   r+   r   r   r   rŽ   s   *  r   Úcausal_lower_rightr’   V  s6   € ô> ˆ4ƒy�A„~ÜÐJÓKÐKØ Ñ€IÜ”m×/Ñ/°ÓGÐGr   )r�   r’   r   r   )r   Úenumr   r   Úwarningsr   r6   Útorch.nn.functionalrf   rg   rc   Útorch.backends.cudar   r   r   r   Útorch.nn.attentionr	   Útorch.nn.attention._utilsr
   r   r   r   Ú__all__Ú_dynamoÚallow_in_graphr   r7   r   r�   r’   r   r   r   Ú<module>rœ      s²   ðá Iç Ý ã ß Ð ÷ó õ 6÷ó ò U€ð ‡�× Ñ Ð9Ô :Ø ‡�× Ñ Ð4Ô 5Ø ‡�× Ñ Ð8Ô 9Ø ‡�× Ñ ˜ZÔ (ô2�Gô 2ôj[.�—‘ô [.õ|G÷D"Hr   