o
    i                     @   s   d Z ddlZddlmZ ddlmZ ddlZG dd dejZ	G dd dejZ
dd	 ZG d
d dejZG dd dejZG dd dejZG dd dejZG dd dejZdS )a  
    This file is part of ComfyUI.
    Copyright (C) 2024 Stability AI

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
    N)optimized_attentionc                       s*   e Zd Zd fdd	Zi fddZ  ZS )OptimizedAttention        Nc                    sl   t    || _|j||d||d| _|j||d||d| _|j||d||d| _|j||d||d| _d S )NT)biasdtypedevice)super__init__headsLinearto_qto_kto_vout_projselfcnheaddropoutr   r   
operations	__class__ 6/mnt/c/Users/fbmor/ComfyUI/comfy/ldm/cascade/common.pyr	      s   
zOptimizedAttention.__init__c                 C   s<   |  |}| |}| |}t|||| j|d}| |S )Ntransformer_options)r   r   r   r   r
   r   )r   qkvr   outr   r   r   forward#   s
   



zOptimizedAttention.forwardr   NNN__name__
__module____qualname__r	   r    __classcell__r   r   r   r   r          
r   c                       s,   e Zd Zd fdd	Zdi fddZ  ZS )	Attention2Dr   Nc                    s"   t    t|||||d| _d S )Nr   r   r   )r   r	   r   attnr   r   r   r   r	   -   s   
zAttention2D.__init__Fc                 C   sj   |j }||d|ddddd}|r tj||gdd}| j||||d}|dddj| }|S )Nr         dimr   )shapeviewsizepermutetorchcatr*   )r   xkv	self_attnr   
orig_shaper   r   r   r    2   s   $zAttention2D.forwardr!   r"   r   r   r   r   r(   ,   s    r(   c                 C   s   G dd d| j }|S )Nc                       s(   e Zd Z fddZ fddZ  ZS )z#LayerNorm2d_op.<locals>.LayerNorm2dc                    s   t  j|i | d S N)r   r	   )r   argskwargsr   r   r   r	   ?   s   z,LayerNorm2d_op.<locals>.LayerNorm2d.__init__c                    s$   t  |ddddddddS Nr   r-      r+   )r   r    r3   r   r6   r   r   r   r    B   s   $z+LayerNorm2d_op.<locals>.LayerNorm2d.forwardr"   r   r   r   r   LayerNorm2d>   s    r@   )	LayerNorm)r   r@   r   r   r   LayerNorm2d_op=   s   rB   c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )GlobalResponseNormzwfrom https://github.com/facebookresearch/ConvNeXt-V2/blob/3608f67cc1dae164790c5d0aead7bf2d73d9719b/models/utils.py#L105Nc              
      sJ   t    ttjddd|||d| _ttjddd|||d| _d S )Nr+   r   r   )r   r	   nn	Parameterr4   emptygammabeta)r   r/   r   r   r   r   r   r	   H   s   
"zGlobalResponseNorm.__init__c                 C   sT   t j|dddd}||jdddd  }tj| j|||  tj| j| | S )Nr-   )r+   r-   T)pr/   keepdimr,   )r/   rK   ư>)r4   normmeancomfyopscast_to_inputrH   rI   )r   r6   GxNxr   r   r   r    M   s   ,zGlobalResponseNorm.forward)NN)r#   r$   r%   __doc__r	   r    r&   r   r   r   r   rC   F   s    rC   c                       s(   e Zd Zd	 fdd	Zd
ddZ  ZS )ResBlockr   r>   r   Nc                    s   t    |j||||d |||d| _t||dd||d| _t|j|| |d ||dt	 t
|d ||dt||j|d |||d| _d S )Nr-   )kernel_sizepaddinggroupsr   r   FrL   elementwise_affineepsr   r      rD   )r   r	   Conv2d	depthwiserB   rM   rE   
Sequentialr   GELUrC   Dropoutchannelwise)r   r   c_skiprV   r   r   r   r   r   r   r   r	   T   s   

zResBlock.__init__c                 C   sX   |}|  | |}|d urtj||gdd}| |dddddddd}|| S )Nr+   r.   r   r-   r>   )rM   r^   r4   r5   rb   r3   )r   r6   x_skipx_resr   r   r   r    a   s   "zResBlock.forward)r   r>   r   NNNr:   r"   r   r   r   r   rU   S   s    rU   c                       s*   e Zd Zd fdd	Zi fddZ  ZS )		AttnBlockTr   Nc	           	   	      sb   t    || _t||dd||d| _t||||||d| _tt	 |j
||||d| _d S )NFrL   rY   r)   rD   )r   r	   r8   rB   rM   r(   	attentionrE   r_   SiLUr   	kv_mapper)	r   r   c_condr   r8   r   r   r   r   r   r   r   r	   k   s   

zAttnBlock.__init__c                 C   s,   |  |}|| j| ||| j|d }|S )N)r8   r   )ri   rg   rM   r8   )r   r6   r7   r   r   r   r   r    u   s   
zAttnBlock.forward)Tr   NNNr"   r   r   r   r   rf   j   r'   rf   c                       s&   e Zd Zd fdd	Zdd Z  ZS )FeedForwardBlockr   Nc                    sv   t    t||dd||d| _t|j||d ||dt t|d ||dt	||j|d |||d| _
d S )NFrL   rY   r\   rD   )r   r	   rB   rM   rE   r_   r   r`   rC   ra   rb   )r   r   r   r   r   r   r   r   r   r	   |   s   

zFeedForwardBlock.__init__c              	   C   s0   ||  | |dddddddd }|S r=   )rb   rM   r3   r?   r   r   r   r       s   ,zFeedForwardBlock.forwardr!   r"   r   r   r   r   rk   {   s    rk   c                       s0   e Zd Zdgdddf fdd	Zdd Z  ZS )TimestepBlockscaNc              
      sZ   t    |j||d ||d| _|| _|D ]}t| d| |j||d ||d qd S )Nr-   rD   mapper_)r   r	   r   mappercondssetattr)r   r   
c_timesteprp   r   r   r   cnamer   r   r   r	      s   
&zTimestepBlock.__init__c           	      C   s   |j t| jd dd}| |d d d d d d d f j ddd\}}t| jD ],\}}t| d| ||d  d d d d d d f j ddd\}}|| || }}q)|d|  | S )Nr+   r.   r   r-   rn   )chunklenrp   ro   	enumerategetattr)	r   r6   tabir   acbcr   r   r   r       s   0>zTimestepBlock.forwardr"   r   r   r   r   rl      s    rl   )rT   r4   torch.nnrE   comfy.ldm.modules.attentionr   	comfy.opsrO   Moduler   r(   rB   rC   rU   rf   rk   rl   r   r   r   r   <module>   s    	