o
     i                     @   s<  U d dl Z d dlmZ d dlmZmZmZmZmZ d dl	Z	d dl
mZmZmZ ddlmZ ddlmZ ddlmZmZ e d	Zi Zeeef ed
< e Zee ed< dZdZdeeeef ef fddZ	 eeeeeZeeegeegef f ed< defddZ dd Z!ddl"m#Z# g dZ$eeee%j&d dS )    N)Path)AnyCallableDictSetUnion)generate_matching_configget_registry_decoratorimport_all_modules   )SparseCS)AttentionMask)	AttentionAttentionConfigxformersATTENTION_REGISTRYATTENTION_CLASS_NAMESg333333?Tconfigc              
   C   sv   t | ts0zt| t| d  j}W n  ty/ } z| d }t| dt   |d}~ww | }t|j	 j
|S )aQ  Builds an attention from a config.

    This assumes a 'name' key in the config which is used to determine what
    attention class to instantiate. For instance, a config `{"name": "my_attention",
    "foo": "bar"}` will find a class that was registered as "my_attention"
    (see :func:`register_attention`) and call .from_config on it.namez not available among N)
isinstancer   r   r   r   KeyErrorloggerwarningkeysr   constructorfrom_config)r   config_instanceer    r   Z/home/ubuntu/.local/lib/python3.10/site-packages/xformers/components/attention/__init__.pybuild_attention$   s   
r    register_attentionreturnc                 C   s,   t |  |   tkrt| S t| S N)torchcount_nonzeroitemnumel_DENSITY_THRESHOLDr   	from_boolsparsifymatrixr   r   r   maybe_sparsifyT   s   
r-   c                 C   s   t rt| S |  S r#   )_USE_SPUTNIKr   	to_sparser+   r   r   r   r*   ]   s   r*   )ScaledDotProduct)r0   r   r   r    r!   zxformers.components.attention)'loggingpathlibr   typingr   r   r   r   r   r$   xformers.utilsr   r	   r
   _sputnik_sparser   attention_maskr   baser   r   	getLoggerr   r   str__annotations__setr   r(   r.   r    r!   r-   r*   scaled_dot_productr0   __all____file__parentr   r   r   r   <module>   s.   

"		