o
    kio                     @   s   U d Z ddlZddlmZ ddlmZmZ eeZ	G dd deZ
ede
dB f Zed Zi aeeef ed	< daeee
f dB ed
< deeB deddfddZdeeB ddfddZdee fddZdedB fddZddeddfddZdS )zRegistry for flash attention implementations.

This module contains the registration system for flash attention implementations.
It has no torch dependencies to avoid circular imports during initialization.
    N)Callable)LiteralProtocolc                   @   s   e Zd ZdddZdS )FlashAttentionHandlereturnNc                 C   s   d S )N )selfr   r   f/var/www/addictedbytheproject.nl/epg/venv/lib/python3.10/site-packages/torch/nn/attention/_registry.pyremove   s    zFlashAttentionHandle.remove)r   N)__name__
__module____qualname__r
   r   r   r   r	   r      s    r   .)FA3FA4_FLASH_ATTENTION_IMPLS_FLASH_ATTENTION_ACTIVEimplregister_fnr   c                C   s   |t | < dS )a#  
    Register the callable that activates a flash attention impl.

    .. note::
        This function is intended for SDPA backend providers to register their
        implementations. End users should use :func:`activate_flash_attention_impl`
        to activate a registered implementation.

    Args:
        impl: Implementation identifier (e.g., ``"FA4"``).
        register_fn: Callable that performs the actual dispatcher registration.
            This function will be invoked by :func:`activate_flash_attention_impl`
            and should register custom kernels with the PyTorch dispatcher.
            It may optionally return a handle implementing
            :class:`FlashAttentionHandle` to keep any necessary state alive.

    Example:
        >>> def my_impl_register(module_path: str = "my_flash_impl"):
        ...     # Register custom kernels with torch dispatcher
        ...     pass  # doctest: +SKIP
        >>> register_flash_attention_impl(
        ...     "MyImpl", register_fn=my_impl_register
        ... )  # doctest: +SKIP
    N)r   )r   r   r   r   r	   register_flash_attention_impl   s   r   c                 C   sP   t dd t| }|du rtd|  dt  | }|dur&| |fadS dS )ac  
    Activate into the dispatcher a previously registered flash attention impl.

    .. note::
        Backend providers should NOT automatically activate their implementation
        on import. Users should explicitly opt-in by calling this function or via
        environment variables to ensure multiple provider libraries can coexist.

    Args:
        impl: Implementation identifier to activate. See
            :func:`~torch.nn.attention.list_flash_attention_impls` for available
            implementations.
            If the backend's :func:`register_flash_attention_impl` callable
            returns a :class:`FlashAttentionHandle`, the registry keeps that
            handle alive for the lifetime of the process (until explicit
            uninstall support exists).

    Example:
        >>> activate_flash_attention_impl("FA4")  # doctest: +SKIP
    F)_raise_warnNzUnknown flash attention impl 'z'. Available implementations: )restore_flash_attention_implr   get
ValueErrorlist_flash_attention_implsr   )r   r   handler   r   r	   activate_flash_attention_impl=   s   
r   c                   C   s   t t S )zBReturn the names of all available flash attention implementations.)sortedr   keysr   r   r   r	   r   f   s   r   c                   C   s   t durt d S t S )z
    Return the currently activated flash attention impl name, if any.

    ``None`` indicates that no custom impl has been activated.
    Nr   )r   r   r   r   r	   current_flash_attention_implk   s
   r   Tr   c                 C   sB   d}t dur
t d }|dur|  da dS | rtd da dS )z0
    Restore the default FA2 implementation
    N   zDTrying to restore default FA2 impl when no custom impl was activated)r   r
   loggerwarning)r   r   r   r   r	   r   x   s   r   )T)__doc__loggingcollections.abcr   typingr   r   	getLoggerr   r    r   _RegisterFn_FlashAttentionImplr   dictstr__annotations__r   tupler   r   listr   r   boolr   r   r   r   r	   <module>   s2   

!
)