
    wi`                        d dl Z d dlmZmZmZmZ d dlZd dlmZ ddl	m
Z
mZ ddlmZ ddlmZ  ej        e          Z G d d	e          Z G d
 dee
          Z G d dej                  Z G d dej                  Z G d dej                  Z G d dej                  Z G d dej                  Z G d dej                  Z G d dej                  ZdS )    N)CallableListOptionalUnion   )ConfigMixinregister_to_config)logging   )
ModelMixinc                       e Zd ZdZded         f fdZddej        deee	                  deej                 fd	Z
	 	 	 	 ddeeej        f         dedededee         f
dZedeeeej        f                  fd            Z xZS )MultiAdaptera  
    MultiAdapter is a wrapper model that contains multiple adapter models and merges their outputs according to
    user-assigned weighting.

    This model inherits from [`ModelMixin`]. Check the superclass documentation for the generic methods the library
    implements for all the model (such as downloading or saving, etc.)

    Parameters:
        adapters (`List[T2IAdapter]`, *optional*, defaults to None):
            A list of `T2IAdapter` model instances.
    adapters
T2IAdapterc                 z   t          t          |                                            t          |          | _        t          j        |          | _        t          |          dk    rt          d          t          |          dk    rt          d          |d         j	        }|d         j
        }t          dt          |                    D ][}||         j	        |k    s||         j
        |k    r7t          d| d| d| d||         j	         d| d	||         j
                   \|| _	        || _
        d S )
Nr   zExpecting at least one adapterr   zQFor a single adapter, please use the `T2IAdapter` class instead of `MultiAdapter`zjExpecting all adapters to have the same downscaling behavior, but got:
adapters[0].total_downscale_factor=z
adapters[0].downscale_factor=z

adapter[`z`].total_downscale_factor=z`].downscale_factor=)superr   __init__lennum_adapternn
ModuleListr   
ValueErrortotal_downscale_factordownscale_factorrange)selfr   $first_adapter_total_downscale_factorfirst_adapter_downscale_factoridx	__class__s        h/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/diffusers/models/adapter.pyr   zMultiAdapter.__init__)   s   lD!!**,,,x==h//x==A=>>>x==Apqqq 08{/Q,)1!)E&CMM** 	 	C48\\\C=15SSS Z:^Z Z4RZ Z !$Z Z @H}?cZ Z !$	Z Z :B#9W	Z Z   T 'K# >    Nxsadapter_weightsreturnc                    |&t          j        d| j        z  g| j        z            }nt          j        |          }d}t          ||| j                  D ]{\  }}} ||          }|0|}t          t          |                    D ]}|||         z  ||<   Ct          t          |                    D ]}||xx         |||         z  z  cc<   ||S )a  
        Args:
            xs (`torch.Tensor`):
                (batch, channel, height, width) input images for multiple adapter models concated along dimension 1,
                `channel` should equal to `num_adapter` * "number of channel of image".
            adapter_weights (`List[float]`, *optional*, defaults to None):
                List of floats representing the weight which will be multiply to each adapter's output before adding
                them together.
        Nr   )torchtensorr   zipr   r   r   )	r   r#   r$   accume_statexwadapterfeaturesis	            r!   forwardzMultiAdapter.forwardL   s     "#lA0@,@+ADDT+TUUOO#l?;;O _dmDD 	7 	7MAq'wqzzH#'s<0011 : :A&',q/&9LOO: s8}}-- 7 7A OOOq8A;6OOOO7r"   Tsave_directoryis_main_processsave_functionsafe_serializationvariantc                 p    d}|}| j         D ])}|                    |||||           |dz  }|d| z   }*dS )a3  
        Save a model and its configuration file to a directory, so that it can be re-loaded using the
        `[`~models.adapter.MultiAdapter.from_pretrained`]` class method.

        Arguments:
            save_directory (`str` or `os.PathLike`):
                Directory to which to save. Will be created if it doesn't exist.
            is_main_process (`bool`, *optional*, defaults to `True`):
                Whether the process calling this is the main process or not. Useful when in distributed training like
                TPUs and need to call this function on all processes. In this case, set `is_main_process=True` only on
                the main process to avoid race conditions.
            save_function (`Callable`):
                The function to use to save the state dictionary. Useful on distributed training like TPUs when one
                need to replace `torch.save` by another method. Can be configured with the environment variable
                `DIFFUSERS_SAVE_MODE`.
            safe_serialization (`bool`, *optional*, defaults to `True`):
                Whether to save the model using `safetensors` or the traditional PyTorch way (that uses `pickle`).
            variant (`str`, *optional*):
                If specified, weights are saved in the format pytorch_model.<variant>.bin.
        r   )r2   r3   r4   r5   r   _N)r   save_pretrained)	r   r1   r2   r3   r4   r5   r   model_path_to_saver-   s	            r!   r8   zMultiAdapter.save_pretrainedg   sy    8 +} 
	@ 
	@G##" /+#5 $    1HC!3i#ii!?
	@ 
	@r"   pretrained_model_pathc                    d}g }|}t           j                            |          rSt          j        |fi |}|                    |           |dz  }|d| z   }t           j                            |          St                              t          |           d| d           t          |          dk    r6t          dt           j        
                    |           d|dz    d           | |          S )	a[  
        Instantiate a pretrained MultiAdapter model from multiple pre-trained adapter models.

        The model is set in evaluation mode by default using `model.eval()` (Dropout modules are deactivated). To train
        the model, you should first set it back in training mode with `model.train()`.

        The warning *Weights from XXX not initialized from pretrained model* means that the weights of XXX do not come
        pretrained with the rest of the model. It is up to you to train those weights with a downstream fine-tuning
        task.

        The warning *Weights from XXX not used in YYY* means that the layer XXX is not used by YYY, therefore those
        weights are discarded.

        Parameters:
            pretrained_model_path (`os.PathLike`):
                A path to a *directory* containing model weights saved using
                [`~diffusers.models.adapter.MultiAdapter.save_pretrained`], e.g., `./my_model_directory/adapter`.
            torch_dtype (`str` or `torch.dtype`, *optional*):
                Override the default `torch.dtype` and load the model under this dtype. If `"auto"` is passed the dtype
                will be automatically derived from the model's weights.
            output_loading_info(`bool`, *optional*, defaults to `False`):
                Whether or not to also return a dictionary containing missing keys, unexpected keys and error messages.
            device_map (`str` or `Dict[str, Union[int, str, torch.device]]`, *optional*):
                A map that specifies where each submodule should go. It doesn't need to be refined to each
                parameter/buffer name, once a given module name is inside, every submodule of it will be sent to the
                same device.

                To have Accelerate compute the most optimized `device_map` automatically, set `device_map="auto"`. For
                more information about each option see [designing a device
                map](https://hf.co/docs/accelerate/main/en/usage_guides/big_modeling#designing-a-device-map).
            max_memory (`Dict`, *optional*):
                A dictionary device identifier to maximum memory. Will default to the maximum memory available for each
                GPU and the available CPU RAM if unset.
            low_cpu_mem_usage (`bool`, *optional*, defaults to `True` if torch version >= 1.9.0 else `False`):
                Speed up model loading by not initializing the weights and only loading the pre-trained weights. This
                also tries to not use more than 1x model size in CPU memory (including peak memory) while loading the
                model. This is only supported when torch version >= 1.9.0. If you are using an older version of torch,
                setting this argument to `True` will raise an error.
            variant (`str`, *optional*):
                If specified load weights from `variant` filename, *e.g.* pytorch_model.<variant>.bin. `variant` is
                ignored when using `from_flax`.
            use_safetensors (`bool`, *optional*, defaults to `None`):
                If set to `None`, the `safetensors` weights will be downloaded if they're available **and** if the
                `safetensors` library is installed. If set to `True`, the model will be forcibly loaded from
                `safetensors` weights. If set to `False`, loading will *not* use `safetensors`.
        r   r   r7   z adapters loaded from .zNo T2IAdapters found under z. Expected at least _0)ospathisdirr   from_pretrainedappendloggerinfor   r   dirname)clsr:   kwargsr   r   model_path_to_loadr-   s          r!   rA   zMultiAdapter.from_pretrained   s3   ` 
 3gmm.// 	C 01CNNvNNGOOG$$$1HC!6S!B gmm.// 	C 	s8}}TT<QTTTUUUx==A Jbgoo>S.T.T  J  Jj  CG  kG  J  J  J   s8}}r"   N)TNTN)__name__
__module____qualname____doc__r   r   r'   Tensorr   floatr0   r   strr>   PathLikeboolr   r8   classmethodrA   __classcell__r    s   @r!   r   r      sJ       
 
!?l!3 !? !? !? !? !? !?F %, $u+9N Z^_d_kZl    < !%"&#'!%(@ (@c2;./(@ (@  	(@
 !(@ #(@ (@ (@ (@T DHU3CS=T4U D D D [D D D D Dr"   r   c                        e Zd ZdZedg ddddfdedee         d	ed
edef
 fd            Zde	j
        dee	j
                 fdZed             Zed             Z xZS )r   a  
    A simple ResNet-like model that accepts images containing control signals such as keyposes and depth. The model
    generates multiple feature maps that are used as additional conditioning in [`UNet2DConditionModel`]. The model's
    architecture follows the original implementation of
    [Adapter](https://github.com/TencentARC/T2I-Adapter/blob/686de4681515662c0ac2ffa07bf5dda83af1038a/ldm/modules/encoders/adapter.py#L97)
     and
     [AdapterLight](https://github.com/TencentARC/T2I-Adapter/blob/686de4681515662c0ac2ffa07bf5dda83af1038a/ldm/modules/encoders/adapter.py#L235).

    This model inherits from [`ModelMixin`]. Check the superclass documentation for the generic methods the library
    implements for all the model (such as downloading or saving, etc.)

    Parameters:
        in_channels (`int`, *optional*, defaults to 3):
            Number of channels of Aapter's input(*control image*). Set this parameter to 1 if you're using gray scale
            image as *control image*.
        channels (`List[int]`, *optional*, defaults to `(320, 640, 1280, 1280)`):
            The number of channel of each downsample block's output hidden state. The `len(block_out_channels)` will
            also determine the number of downsample blocks in the Adapter.
        num_res_blocks (`int`, *optional*, defaults to 2):
            Number of ResNet blocks in each downsample block.
        downscale_factor (`int`, *optional*, defaults to 8):
            A factor that determines the total downscale factor of the Adapter.
        adapter_type (`str`, *optional*, defaults to `full_adapter`):
            The type of Adapter to use. Choose either `full_adapter` or `full_adapter_xl` or `light_adapter`.
       @       r[   r      full_adapterin_channelschannelsnum_res_blocksr   adapter_typec                 $   t                                                       |dk    rt          ||||          | _        d S |dk    rt	          ||||          | _        d S |dk    rt          ||||          | _        d S t          d| d          )Nr]   full_adapter_xllight_adapterzUnsupported adapter_type: 'zH'. Choose either 'full_adapter' or 'full_adapter_xl' or 'light_adapter'.)r   r   FullAdapterr-   FullAdapterXLLightAdapterr   )r   r^   r_   r`   r   ra   r    s         r!   r   zT2IAdapter.__init__   s     	>))&{HnN^__DLLL...(hP`aaDLLL_,,'X~O_``DLLL8l 8 8 8  r"   r+   r%   c                 ,    |                      |          S )a  
        This function processes the input tensor `x` through the adapter model and returns a list of feature tensors,
        each representing information extracted at a different scale from the input. The length of the list is
        determined by the number of downsample blocks in the Adapter, as specified by the `channels` and
        `num_res_blocks` parameters during initialization.
        )r-   r   r+   s     r!   r0   zT2IAdapter.forward  s     ||Ar"   c                     | j         j        S rI   )r-   r   r   s    r!   r   z!T2IAdapter.total_downscale_factor  s    |22r"   c                 $    | j         j        j        S )zThe downscale factor applied in the T2I-Adapter's initial pixel unshuffle operation. If an input image's dimensions are
        not evenly divisible by the downscale_factor then an exception will be raised.
        )r-   	unshuffler   rk   s    r!   r   zT2IAdapter.downscale_factor  s    
 |%66r"   )rJ   rK   rL   rM   r	   intr   rP   r   r'   rN   r0   propertyr   r   rT   rU   s   @r!   r   r      s        4  444 !*  s) 	
       , $u|*<     3 3 X3 7 7 X7 7 7 7 7r"   r   c            	       ~     e Zd ZdZdg dddfdedee         ded	ef fd
Zdej        deej                 fdZ	 xZ
S )re   2
    See [`T2IAdapter`] for more information.
    rW   rX   r   r\   r^   r_   r`   r   c           
         t                                                       ||dz  z  }t          j        |          | _        t          j        |d         dd          | _        t          j        t          d         d                   gfdt          dt                              D                       | _        |dt                    dz
  z  z  | _        d S )Nr   r   rW   r   kernel_sizepaddingc                 T    g | ]$}t          |d z
           |         d          %S r   Tdown)AdapterBlock.0r/   r_   r`   s     r!   
<listcomp>z(FullAdapter.__init__.<locals>.<listcomp>9  sG        !!a%(1+~TXYYY  r"   )r   r   r   PixelUnshufflerm   Conv2dconv_inr   rz   r   r   bodyr   r   r^   r_   r`   r   r    s     `` r!   r   zFullAdapter.__init__(  s     	!$4a$77*+;<<yhqkqRSTTTMXa[(1+~FF    "1c(mm44  
 
	 '7s8}}q?P9Q&Q###r"   r+   r%   c                     |                      |          }|                     |          }g }| j        D ]"} ||          }|                    |           #|S )a  
        This method processes the input tensor `x` through the FullAdapter model and performs operations including
        pixel unshuffling, convolution, and a stack of AdapterBlocks. It returns a list of feature tensors, each
        capturing information at a different stage of processing within the FullAdapter model. The number of feature
        tensors in the list is determined by the number of downsample blocks specified during initialization.
        rm   r   r   rB   r   r+   r.   blocks       r!   r0   zFullAdapter.forwardB  sa     NN1LLOOY 	 	EaAOOAr"   rJ   rK   rL   rM   rn   r   r   r'   rN   r0   rT   rU   s   @r!   re   re   #  s          444 !R RR s)R 	R
 R R R R R R4 $u|*<        r"   re   c            	       ~     e Zd ZdZdg dddfdedee         ded	ef fd
Zdej        deej                 fdZ	 xZ
S )rf   rq   rW   rX   r      r^   r_   r`   r   c           	         t                                                       ||dz  z  }t          j        |          | _        t          j        ||d         dd          | _        g | _        t          t          |                    D ]}|dk    r9| j        
                    t          ||dz
           ||         |                     A|dk    r;| j        
                    t          ||dz
           ||         |d                     | j        
                    t          ||         ||         |                     t          j        | j                  | _        |dz  | _        d S )Nr   r   rW   r   rs   Trx   )r   r   r   r~   rm   r   r   r   r   r   rB   rz   r   r   )r   r^   r_   r`   r   r/   r    s         r!   r   zFullAdapterXL.__init__Z  sP    	!$4a$77*+;<<yhqkqRSTTT	s8}}%% 	Y 	YAAvv	  hq1uox{N![![\\\\a	  hq1uox{Nae!f!f!fgggg	  hqk8A;!W!WXXXXM$),,	&6&:###r"   r+   r%   c                     |                      |          }|                     |          }g }| j        D ]"} ||          }|                    |           #|S )z
        This method takes the tensor x as input and processes it through FullAdapterXL model. It consists of operations
        including unshuffling pixels, applying convolution layer and appending each block into list of feature tensors.
        r   r   s       r!   r0   zFullAdapterXL.forwardv  sa    
 NN1LLOOY 	 	EaAOOAr"   r   rU   s   @r!   rf   rf   U  s          444 "; ;; s); 	;
 ; ; ; ; ; ;8 $u|*<        r"   rf   c            	       Z     e Zd ZdZddedededef fdZdej        d	ej        fd
Z	 xZ
S )rz   a)  
    An AdapterBlock is a helper model that contains multiple ResNet-like blocks. It is used in the `FullAdapter` and
    `FullAdapterXL` models.

    Parameters:
        in_channels (`int`):
            Number of channels of AdapterBlock's input.
        out_channels (`int`):
            Number of channels of AdapterBlock's output.
        num_res_blocks (`int`):
            Number of ResNet blocks in the AdapterBlock.
        down (`bool`, *optional*, defaults to `False`):
            Whether to perform downsampling on AdapterBlock's input.
    Fr^   out_channelsr`   ry   c                 >   t                                                       d | _        |rt          j        ddd          | _        d | _        |k    rt          j        |d          | _        t          j        fdt          |          D              | _	        d S )Nr   Trt   stride	ceil_moder   rt   c                 .    g | ]}t                    S  )AdapterResnetBlock)r|   r7   r   s     r!   r}   z)AdapterBlock.__init__.<locals>.<listcomp>  s"    NNN1 ..NNNr"   )
r   r   
downsampler   	AvgPool2din_convr   
Sequentialr   resnets)r   r^   r   r`   ry   r    s     `  r!   r   zAdapterBlock.__init__  s     	T lqdSSSDO,&&9[,ANNNDL}NNNNn8M8MNNN
r"   r+   r%   c                     | j         |                      |          }| j        |                     |          }|                     |          }|S )a  
        This method takes tensor x as input and performs operations downsampling and convolutional layers if the
        self.downsample and self.in_conv properties of AdapterBlock model are specified. Then it applies a series of
        residual blocks to the input tensor.
        )r   r   r   ri   s     r!   r0   zAdapterBlock.forward  sH     ?&""A<#QALLOOr"   FrJ   rK   rL   rM   rn   rR   r   r'   rN   r0   rT   rU   s   @r!   rz   rz     s         
 
C 
s 
C 
W[ 
 
 
 
 
 
 %,        r"   rz   c                   L     e Zd ZdZdef fdZdej        dej        fdZ xZ	S )r   z
    An `AdapterResnetBlock` is a helper model that implements a ResNet-like block.

    Parameters:
        channels (`int`):
            Number of channels of AdapterResnetBlock's input and output.
    r_   c                     t                                                       t          j        ||dd          | _        t          j                    | _        t          j        ||d          | _        d S )NrW   r   rs   r   r   r   r   r   block1ReLUactblock2r   r_   r    s     r!   r   zAdapterResnetBlock.__init__  s\    i(1MMM799i(BBBr"   r+   r%   c                     |                      |                     |                    }|                     |          }||z   S )z
        This method takes input tensor x and applies a convolutional layer, ReLU activation, and another convolutional
        layer on the input tensor. It returns addition with the input tensor.
        r   r   r   r   r+   hs      r!   r0   zAdapterResnetBlock.forward  7     HHT[[^^$$KKNN1ur"   
rJ   rK   rL   rM   rn   r   r'   rN   r0   rT   rU   s   @r!   r   r     s{         C C C C C C C	 	%, 	 	 	 	 	 	 	 	r"   r   c            	       ~     e Zd ZdZdg dddfdedee         ded	ef fd
Zdej        deej                 fdZ	 xZ
S )rg   rq   rW   )rY   rZ   r[      r\   r^   r_   r`   r   c           	         t                                                       ||dz  z  }t          j        |          | _        t          j        t          |d                   gfdt          t                    dz
            D             t          d         d         d                    | _	        |dt                    z  z  | _
        d S )Nr   r   c                 T    g | ]$}t          |         |d z            d          %S rw   )LightAdapterBlockr{   s     r!   r}   z)LightAdapter.__init__.<locals>.<listcomp>  sG        &hqk8AE?NY]^^^  r"   r   Trx   )r   r   r   r~   rm   r   r   r   r   r   r   r   s     `` r!   r   zLightAdapter.__init__  s     	!$4a$77*+;<<M!+x{NKK    "3x==1#455   "(2,nSWXXX	
 	
	 '7!s8}}:L&M###r"   r+   r%   c                     |                      |          }g }| j        D ]"} ||          }|                    |           #|S )z
        This method takes the input tensor x and performs downscaling and appends it in list of feature tensors. Each
        feature tensor corresponds to a different level of processing within the LightAdapter.
        )rm   r   rB   r   s       r!   r0   zLightAdapter.forward  sR    
 NN1Y 	 	EaAOOAr"   r   rU   s   @r!   rg   rg     s          ... !N NN s)N 	N
 N N N N N N4 $u|*<        r"   rg   c            	       Z     e Zd ZdZddedededef fdZdej        d	ej        fd
Z	 xZ
S )r   aB  
    A `LightAdapterBlock` is a helper model that contains multiple `LightAdapterResnetBlocks`. It is used in the
    `LightAdapter` model.

    Parameters:
        in_channels (`int`):
            Number of channels of LightAdapterBlock's input.
        out_channels (`int`):
            Number of channels of LightAdapterBlock's output.
        num_res_blocks (`int`):
            Number of LightAdapterResnetBlocks in the LightAdapterBlock.
        down (`bool`, *optional*, defaults to `False`):
            Whether to perform downsampling on LightAdapterBlock's input.
    Fr^   r   r`   ry   c                 f   t                                                       |dz  d | _        |rt          j        ddd          | _        t          j        |d          | _        t          j        fdt          |          D              | _	        t          j        |d          | _
        d S )Nr   r   Tr   r   r   c                 .    g | ]}t                    S r   )LightAdapterResnetBlock)r|   r7   mid_channelss     r!   r}   z.LightAdapterBlock.__init__.<locals>.<listcomp>  s#    &l&l&lQR'>|'L'L&l&l&lr"   )r   r   r   r   r   r   r   r   r   r   out_conv)r   r^   r   r`   ry   r   r    s        @r!   r   zLightAdapterBlock.__init__  s    #q( 	T lqdSSSDOylJJJ}&l&l&l&lV[\jVkVk&l&l&lm	,!LLLr"   r+   r%   c                     | j         |                      |          }|                     |          }|                     |          }|                     |          }|S )z
        This method takes tensor x as input and performs downsampling if required. Then it applies in convolution
        layer, a sequence of residual blocks, and out convolutional layer.
        )r   r   r   r   ri   s     r!   r0   zLightAdapterBlock.forward   sQ    
 ?&""ALLOOLLOOMM!r"   r   r   rU   s   @r!   r   r     s         
M 
MC 
Ms 
MC 
MW[ 
M 
M 
M 
M 
M 
M %,        r"   r   c                   L     e Zd ZdZdef fdZdej        dej        fdZ xZ	S )r   a  
    A `LightAdapterResnetBlock` is a helper model that implements a ResNet-like block with a slightly different
    architecture than `AdapterResnetBlock`.

    Parameters:
        channels (`int`):
            Number of channels of LightAdapterResnetBlock's input and output.
    r_   c                     t                                                       t          j        ||dd          | _        t          j                    | _        t          j        ||dd          | _        d S )NrW   r   rs   r   r   s     r!   r   z LightAdapterResnetBlock.__init__9  s^    i(1MMM799i(1MMMr"   r+   r%   c                     |                      |                     |                    }|                     |          }||z   S )z
        This function takes input tensor x and processes it through one convolutional layer, ReLU activation, and
        another convolutional layer and adds it to input tensor.
        r   r   s      r!   r0   zLightAdapterResnetBlock.forward?  r   r"   r   rU   s   @r!   r   r   /  s{         N N N N N N N	 	%, 	 	 	 	 	 	 	 	r"   r   )r>   typingr   r   r   r   r'   torch.nnr   configuration_utilsr   r	   utilsr
   modeling_utilsr   
get_loggerrJ   rC   r   r   Modulere   rf   rz   r   rg   r   r   r   r"   r!   <module>r      sJ   
			 2 2 2 2 2 2 2 2 2 2 2 2        A A A A A A A A       & & & & & & 
	H	%	%z z z z z: z z zzD7 D7 D7 D7 D7[ D7 D7 D7T/ / / / /") / / /d/ / / / /BI / / /d- - - - -29 - - -`       <, , , , ,29 , , ,^( ( ( ( (	 ( ( (V    bi     r"   