Ë
    S²j(  ã                   ó&  — U d Z ddlZddlZg Zee   ed<   dej                  defd„Z	dej                  de
dej                  fd	„Zd
e
dedz  defd„Z	 	 	 	 	 ddej                  dej                  dej                  dej                  dz  ddf
d„Zy)zCDefines utilities for interacting with scaled_dot_product_attentioné    NÚ__all__ÚtensorsÚreturnc                  ó&   — t        d„ | D «       «      S )z0Returns True if any of the tensors requires gradc              3   ó4   K  — | ]  }|j                   –— Œ y ­w)N)Úrequires_grad)Ú.0Úts     úX/var/www/html/ia-enhanced/venv/lib/python3.12/site-packages/torch/nn/attention/_utils.pyú	<genexpr>z'_input_requires_grad.<locals>.<genexpr>   s   è ø€ Ò0 1ˆq��Ñ0ùs   ‚)Úany)r   s    r   Ú_input_requires_gradr      s   € äÑ0¨Ô0Ó0Ð0ó    Úinpt_tensorÚog_sizec                 ó@   — | j                  d«      |k7  r	| dd|…f   S | S )z'Handles the unpad of the last dimensionéÿÿÿÿ.N)Úsize)r   r   s     r   Ú_postprocess_flash_outputr      s.   € à×Ñ˜Ó˜wÒ&Ø˜3   ˜=Ñ)Ð)ØÐr   Úhead_dim_sizeÚscalec                 ó:   — |�|S dt        j                  | «      z  S )z�
    For FlashAttention we pad the head dimension to be a multiple of 8 so we need to scale the output
    by the original head size and not the padded.
    g      ð?)ÚmathÚsqrt)r   r   s     r   Ú_calculate_scaler      s#   € ð
 ÐØˆØ”—‘˜=Ó)Ñ)Ð)r   ÚqueryÚkeyÚvalueÚ	attn_maskc           	      óŒ  — |se| j                   |j                   k7  s| j                   |j                   k7  r3t        d| j                   › d|j                   › d|j                   › d�«      ‚| j                  |j                  k7  s| j                  |j                  k7  r3t        d| j                  › d|j                  › d|j                  › d�«      ‚| j                  «       dk  s&|j                  «       dk  s|j                  «       dk  r?t        d	| j                  «       › d
|j                  «       › d|j                  «       › d�«      ‚y )NzLExpected query, key, and value to have the same dtype, but got query.dtype: z, key.dtype: z, and value.dtype: z	 instead.zSExpected query, key, and value to have the same device type, but got query.device: z, key.device: z, and value.device: é   zUExpected query, key, and value to all be  at least 2 dimensional, but got query.dim: z, key.dim: z and value.dim: )ÚdtypeÚ
ValueErrorÚdeviceÚdim)r   r   r   r   Ú	dropout_pÚ	is_causalr   Úallow_lowp_kvs           r   Ú_validate_sdpa_inputr)   "   s%  € ñ Ø�;‰;˜#Ÿ)™)Ò# u§{¡{°e·k±kÒ'AÜð(Ø(-¯© }°MÀ#Ç)Á)Àð M$Ø$)§K¡K =°	ð;óð ð
 ‡|�|�s—z‘zÒ! U§\¡\°U·\±\Ò%AÜð%Ø%*§\¡\ N°.ÀÇÁÀð M!Ø!&§¡ ¨ið9ó
ð 	
ð
 ‡y�yƒ{�Q‚˜#Ÿ'™'›) aš-¨5¯9©9«;¸ª?ÜØcØ�y‰y‹{ˆm˜; s§w¡w£y kÐ1AÀ%Ç)Á)Ã+ÀÈiðYó
ð 	
ð ,;r   )Ng        FNF)Ú__doc__r   Útorchr   ÚlistÚstrÚ__annotations__ÚTensorÚboolr   Úintr   Úfloatr   r)   © r   r   ú<module>r4      sÎ   ðâ Iã ã ð €ˆˆc‰Ó ð1 5§<¡<ð 1°Dó 1ð
¨5¯<©<ð À#ð È%Ï,É,ó ð* Cð *°¸±ð *Àó *ð &*ØØØ
Øñ
Ø�<‰<ð
à	�‰ð
ð �<‰<ð
ð �|‰|˜dÑ"ð	
ð 
ô
r   