o
     i                     @   s   d dl mZmZ d dlmZmZ d dlmZmZm	Z	 d dl
Z
d dlmZ d dlmZ d dlmZ eG dd dZe	d	d
dZG dd
 d
ejedZdS )    )ABCMetaabstractmethod)asdict	dataclass)OptionalTypeTypeVarN)deprecated_function)AttentionMaskc                   @   s"   e Zd ZU dZeed< eed< dS )AttentionConfigzWParameters required for all Attentions.
    Can accept and store extra parameters.
    namedropoutN)__name__
__module____qualname____doc__str__annotations__float r   r   V/home/ubuntu/.local/lib/python3.10/site-packages/xformers/components/attention/base.pyr      s   
 r   Self	Attention)boundc                	       s   e Zd ZU dZdZee ed< eddee	 f fddZ
edee ded	efd
dZedejdejdejd	ejfddZedejdejfddZ  ZS )r   zWThe base Attention mechanism, which is typically a sub-part of the multi-head attentionN_causal_maskr   c                    sF   t    t|  d| _d| _d| _d| _d| _d| _d| _	d| _
d S )NTF)super__init__r	   requires_input_projectionrequires_head_dimensionrequires_separate_masksrequires_same_k_q_dimensionsrequires_skip_multi_headrequires_squared_contextsupports_attention_masksupports_key_padding_mask)selfr   argskwargs	__class__r   r   r   %   s   

zAttention.__init__clsconfigreturnc                 C   s(   t |}dd | D }| di |S )Nc                 S   s   i | ]\}}|d ur||qS Nr   ).0kvr   r   r   
<dictcomp>G   s    z)Attention.from_config.<locals>.<dictcomp>r   )r   items)r*   r+   fieldsr   r   r   from_configA   s   zAttention.from_configqr/   r0   c                 O   s   t r-   )NotImplementedError)r%   r5   r/   r0   r&   r'   r   r   r   forwardK   s   zAttention.forwardxmaskc                 C   sj   | j d |j d kr3| j d |j d k sJ dddd|j d | j d  ddf}tjjj| |dddS | S )zP
        If the sequence is shorter than the mask, return a padded view
        zmSequence is bigger than the provided mask, cannot infer what to do with it. Please update your attention maskr   constantg        )modevalue)shapetorchnn
functionalpad)r8   r9   pad_sizer   r   r   _maybe_pad_sequenceQ   s    zAttention._maybe_pad_sequencer-   )r   r   r   r   r   r   r
   r   r   r   r   classmethodr   r   r   r4   r@   Tensorr7   staticmethodrE   __classcell__r   r   r(   r   r       s&   
 	 )	metaclass)abcr   r   dataclassesr   r   typingr   r   r   r@   torch.nnrA   xformers._deprecation_warningr	   xformers.components.attentionr
   r   r   Moduler   r   r   r   r   <module>   s   	