
    sg                        U d Z ddlZddlmZmZ ddlmZ ddlmZ	 ddl
mZmZmZmZmZmZmZmZmZmZmZ g dZee   ed<   d	Ze	Z		 ee	_        d
e	_        deddfdZej:                  deee	   e	f   fd       ZdefdZy)zx This module contains functions and classes that alter the behavior of torch.nn.functional.scaled_dot_product_attention     N)ListUnion)warn)_SDPBackend)can_use_efficient_attentioncan_use_flash_attentioncudnn_sdp_enabledenable_cudnn_sdpenable_flash_sdpenable_math_sdpenable_mem_efficient_sdpflash_sdp_enabledmath_sdp_enabledmem_efficient_sdp_enabled
SDPAParams)
SDPBackendsdpa_kernelWARN_FOR_UNFUSED_KERNELS__all__Fr   paramsreturnc                     t         rFt        |       st        d       t        | d       t        |       st        d       t        | d       yyy)z
    If WARN_FOR_UNFUSED_KERNELS is set to True, this will raise warnings
    for all the reasons why the fused kernels can't be run. If using subclasses
    z*Efficient attention can't be used because:Tz&Flash attention can't be used because:N)r   r   r   r   )r   s    N/var/www/html/venv/lib/python3.12/site-packages/torch/nn/attention/__init__.py_raise_kernel_warningsr   8   sG    
  *62=>'5&v.9:#FD1 /	      backendsc              #   ~  K   t        | t        t        f      sJ d       t        | t              r| g} t        |       } t	               }t               }t               }t               }	 t        j                  | v }t        j                  | v }t        j                  | v }t        j                  | v }t        |       t        |       t        |       t        |       i  t        |       t        |       t        |       t        |       y# t        |       t        |       t        |       t        |       w xY ww)a  
    Context manager to select which backend to use for scaled dot product attention.

    .. warning:: This function is beta and subject to change.

    Args:
        backend (Union[List[SDPBackend], SDPBackend]): A backend or list of backends for scaled dot product attention.

    Example:

    .. code-block:: python

        from torch.nn.functional import scaled_dot_product_attention
        from torch.nn.attention import SDPBackend, sdpa_kernel
        # Only enable flash attention backend
        with sdpa_kernel(SDPBackend.FLASH_ATTENTION):
            scaled_dot_product_attention(...)

        # Enable the Math or Efficient attention backends
        with sdpa_kernel([SDPBackend.MATH, SDPBackend.EFFICIENT_ATTENTION]):
            scaled_dot_product_attention(...)

    This context manager can be used to select which backend to use for scaled dot product attention.
    Upon exiting the context manager, the previous state of the flags will be restored, enabling all backends.
    zKBackend must be an instance of SDPBackend or a list of SDPBackend instancesN)
isinstancelistr   setr	   r   r   r   CUDNN_ATTENTIONFLASH_ATTENTIONEFFICIENT_ATTENTIONMATHr
   r   r   r   )	r   previous_cudnnprevious_flashprevious_mem_efficientprevious_mathenable_cudnnenable_flashenable_mem_efficientenable_maths	            r   r   r   F   s    6 4$ UTU  (J':8}H,.N,.N#<#>*,M'!11X=!11X=)==I oo1&& !56$(( !78& 	(( !78&s   A$D='A8D -D=.D::D=c                       y)zEThis returns the closest matching tag for the flash attention backendz2.5.7 r.   r   r   _get_flash_versionr/      s    r   ) __doc__
contextlibtypingr   r   warningsr   torch._Cr   r   torch.backends.cudar   r   r	   r
   r   r   r   r   r   r   r   r   str__annotations__r   __name__
__module__r   contextmanagerr   r/   r.   r   r   <module>r;      s    ~    .    Oc N ! 
 
 !
 "
 2: 2$ 2 5'%Z 0* <= 5' 5'pC r   