o
    ߥi                     @   sD   d dl Z d dlmZ d dlmZmZ G dd dZdefddZdS )    N)	rearrangerepeatc                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )AttentionBasec                 C   s   d| _ d| _d| _d S )Nr   )cur_stepnum_att_layerscur_att_layerself r   e/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/cv/image_editing/masactrl_utils.py__init__   s   
zAttentionBase.__init__c                 C   s   d S )Nr   r	   r   r   r   
after_step   s   zAttentionBase.after_stepc	              	   K   sZ   | j ||||||||fi |	}
|  jd7  _| j| jkr+d| _|  jd7  _|   |
S )N   r   )forwardr   r   r   r   r
   qkvsimattnis_crossplace_in_unet	num_headskwargsoutr   r   r   __call__   s   zAttentionBase.__call__c	                 K   s    t d||}
t|
d|d}
|
S )Nzb i j, b j d -> b i dz(b h) n d -> b n (h d)h)torcheinsumr   r   r   r   r   r   !   s   zAttentionBase.forwardc                 C   s   d| _ d| _d S )Nr   )r   r   r	   r   r   r   reset'   s   
zAttentionBase.resetN)__name__
__module____qualname__r   r   r   r   r!   r   r   r   r   r      s    r   editorc                    s   fdd  fddd}| j  D ]*\}}d|v r%||dd7 }qd|v r2||dd7 }qd|v r>||dd7 }q|_d	S )
zY
    Register a attention editor to Diffuser Pipeline, refer from [Prompt-to-Prompt]
    c                    s   	 	 	 	 d fdd	}|S )Nc                    s:  |dur|}|dur|}j }t|tjjjrj d }nj }j | }|du}|r0|n| }|}	|}	t
 fdd|||	f\}}}	td||j }
|durt|d}t|
jj }t|d d}|dddddf  d	d	}|
| | |
jd
d}|||	|
||jjd	}||S )z
            The attention is similar to the original implementation of LDM CrossAttention class
            except adding some modifications on the attention
            Nr   c                    s   t | d dS )Nzb n (h d) -> (b h) n dr   )r   )tr   r   r   <lambda>N   s    zZregister_attention_editor_diffusers.<locals>.ca_forward.<locals>.forward.<locals>.<lambda>zb i d, b j d -> b i jzb ... -> b (...)zb j -> (b h) () jr   r   r   )dim)scale)to_out
isinstancennmodules	container
ModuleListheadsto_qto_kto_vmapr   r    r)   r   finfodtypemaxr   masked_fill_softmax)xencoder_hidden_statesattention_maskcontextmaskr*   r   r   r   r   r   max_neg_valuer   r   )r%   r   r
   r   r   r   3   sL   	





 zHregister_attention_editor_diffusers.<locals>.ca_forward.<locals>.forward)NNNNr   )r
   r   r   )r%   )r   r
   r   
ca_forward1   s   6z7register_attention_editor_diffusers.<locals>.ca_forwardc                    sP   |   D ]!\}}| jjdkr | || _|d   S t| dr%|||}q|S )N	Attentionr   children)named_children	__class__r"   r   hasattr)netcountr   namesubnet)r@   register_editorr   r   rJ   k   s   
z<register_attention_editor_diffusers.<locals>.register_editorr   downmidupN)unetrC   r   )modelr%   cross_att_countnet_namerF   r   )r@   r%   rJ   r   #register_attention_editor_diffusers,   s   :	
rR   )r   torch.nnr,   einopsr   r   r   rR   r   r   r   r   <module>   s
   !