
    9i                     :    S SK r S SKJr  SSKJr   " S S\5      rg)    N)	rearrange   )AttentionBasec                   H   ^  \ rS rSr     SU 4S jjrS rU 4S jrSrU =r$ )MutualSelfAttentionControl   c                   > [         TU ]  5         XPl        Xl        X l        Ub  UO[        [        US5      5      U l        Ub  UO[        [        X5      5      U l        [        SU R                  5        [        SU R                  5        g)az  
Mutual self-attention control for Stable-Diffusion model
Args:
    start_step: the step to start mutual self-attention control
    start_layer: the layer to start mutual self-attention control
    layer_idx: list of the layers to apply mutual self-attention control
    step_idx: list the steps to apply mutual self-attention control
    total_steps: the total number of steps
N   z
step_idx: zlayer_idx: )
super__init__total_steps
start_stepstart_layerlistrange	layer_idxstep_idxprint)selfr   r   r   r   r   	__class__s         k/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/models/cv/image_editing/masactrl.pyr   #MutualSelfAttentionControl.__init__   sv     	&$&&/&;+r"B$$,$8d**?,lDMM*mT^^,    c	                 $   UR                   S   U-  n
[        USUS9n[        USUS9n[        USUS9n[        R                  " SX5      U	R	                  S5      -  nUR                  S5      n[        R                  " SXS5      n[        USU
S	9nU$ )
Nr   z(b h) n d -> h (b n) d)hzh i d, h j d -> h i jscalezh i j, h j d -> h i dzh (b n) d -> b n (h d))b)shaper   torcheinsumgetsoftmax)r   qkvsimattnis_crossplace_in_unet	num_headskwargsr   outs               r   
attn_batch%MutualSelfAttentionControl.attn_batch(   s    GGAJ)#a1Y?a1Y?a1Y?ll2A9FJJw<OO{{2ll2D<5;
r   c	           
        > U(       d7  U R                   U R                  ;  d  U R                  S-  U R                  ;  a  [        TU ]  " XX4XVUU40 U	D6$ UR                  S5      u  pUR                  S5      u  pUR                  S5      u  pUR                  S5      u  nnU R                  " XSU USU USU UUXx40 U	D6nU R                  " XSU USU USU UUXx40 U	D6n[        R                  " UU/SS9nU$ )z
Attention forward function
   Nr   )dim)
cur_stepr   cur_att_layerr   r   forwardchunkr.   r    cat)r   r$   r%   r&   r'   r(   r)   r*   r+   r,   quqckukcvuvcattnuattncout_uout_cr-   r   s                        r   r5   "MutualSelfAttentionControl.forward5   s$   
 t}}DMM9T=O=OST=T\`\j\j=j7?1#,8068 8 zz!}u z	NBz	N #JY -D<BD z	NBz	N #JY -D<BD iiA.
r   )r   r   r   r   r   )   
   NN2   )	__name__
__module____qualname____firstlineno__r   r.   r5   __static_attributes____classcell__)r   s   @r   r   r      s(     -4 r   r   )r    einopsr   masactrl_utilsr   r    r   r   <module>rO      s      )A Ar   