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   n/home/app/PaddleOCR-VL-test/.venv_paddleocr/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	   Zrequires_input_projectionZrequires_head_dimensionZrequires_separate_masksZrequires_same_k_q_dimensionsZrequires_skip_multi_headZrequires_squared_contextZsupports_attention_maskZ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'   r(   c                 O   s   t r%   )NotImplementedError)r   r-   r'   r(   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   Zconstantg        )modevalue)shapetorchnnZ
functionalpad)r0   r1   Z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   r,   r7   ZTensorr/   staticmethodr:   __classcell__r   r   r    r   r       s&   
 	 )	metaclass)abcr   r   dataclassesr   r   typingr   r   r   r7   Ztorch.nnr8   Zxformers._deprecation_warningr	   Zxformers.components.attentionr
   r   r   Moduler   r   r   r   r   <module>   s   	