o
    i,                     @   sL   d Z ddlZddlmZ ddlm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)nn   )LayerNorm2d_opc                       s&   e Zd Zd fdd	Zdd Z  ZS )CNetResBlockNc                    sd   t    tt||||dt |j||dddt||||dt |j||ddd| _d S )N)dtypedevice   r   )kernel_sizepadding)super__init__r   
Sequentialr   GELUConv2dblocks)selfcr   r   
operations	__class__ :/mnt/c/Users/fbmor/ComfyUI/comfy/ldm/cascade/controlnet.pyr      s   

zCNetResBlock.__init__c                 C   s   ||  | S N)r   )r   xr   r   r   forward$   s   zCNetResBlock.forward)NNN)__name__
__module____qualname__r   r   __classcell__r   r   r   r   r      s    r   c                       s4   e Zd Zddddddef fdd	Zdd Z  ZS )
ControlNetr   i   Nc                    s0  t    |d u rd}|| _|dkrod}tj j | _|dkrn| jd d j	j
}	j|dddd d| jd d< |dkr[|	d d d df  | jd d j	j
d d d df< n|	d d d |f  | jd d j	_
nn|d	kr|}tj||d
 dd dtjdddj|d
 |dd d| _nD|dkrtjj|dd dtjdddjddd dg fddtdD jddd dR  | _d}ntd| t | _tt|D ]$}
| jtj||dd dtjdddj||dd d qd| _|| _d| _d S )Neffneti   r   r         F)r	   stridebiasr   r   simple   r   )r	   r
   r   r   g?T)inplacelargei @  )r	   r   r      c                    s   g | ]
}t d  dqS )r)   )r   r   r   )r   .0_r   r   r   r   r   
<listcomp>E   s    z'ControlNet.__init__.<locals>.<listcomp>   zUnknown bottleneck mode: )r	   r$   r   r   )r   r   proj_blockstorchvisionmodelsefficientnet_v2_sfeaturesevalbackboneweightdatar   cloner   r   	LeakyReLUrange
ValueError
ModuleListprojectionslenappendxlinput_channelsunshuffle_amount)r   c_inc_projr0   bottleneck_moder   r   r   embd_channels
in_weightsr,   r   r-   r   r   )   sV   
"8&



zControlNet.__init__c                 C   s^   |  |}dd tt| jd D }t| jD ]\}}| j| |||< qd|d d d iS )Nc                 S   s   g | ]}d qS r   r   r*   r   r   r   r.   Y   s    z&ControlNet.forward.<locals>.<listcomp>r   input)r6   r;   maxr0   	enumerater>   )r   r   proj_outputsiidxr   r   r   r   W   s
   
zControlNet.forward)r   r   r   r   r   r   r   r   r   r   r   r   (   s    .r   )	__doc__r1   torchr   commonr   Moduler   r   r   r   r   r   <module>   s    