ó
    oM‡jša  ã                   óÌ  • S SK r S SKJr  S SKrS SKJr  SSKJrJr  SSK	J
r
  SSKJr  \
R                  " \5      r " S S	\5      r " S
 S\\5      r " S S\R$                  5      r " S S\R$                  5      r " S S\R$                  5      r " S S\R$                  5      r " S S\R$                  5      r " S S\R$                  5      r " S S\R$                  5      rg)é    N)ÚCallableé   )ÚConfigMixinÚregister_to_config)Úloggingé   )Ú
ModelMixinc                   ó  ^ • \ rS rSrSrS\S   4U 4S jjrSS\R                  S\\	   S-  S	\\R                     4S
 jjr
    SS\\R                  -  S\S\S\S\S-  4
S jjr\S\\R                  -  S-  4S j5       rSrU =r$ )ÚMultiAdapteré   a}  
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 common methods such as downloading
or saving.

Args:
    adapters (`list[T2IAdapter]`, *optional*, defaults to None):
        A list of `T2IAdapter` model instances.
ÚadaptersÚ
T2IAdapterc                 ó&  >• [         [        U ]  5         [        U5      U l        [
        R                  " U5      U l        [        U5      S:X  a  [        S5      e[        U5      S:X  a  [        S5      eUS   R                  nUS   R                  n[        S[        U5      5       H\  nX   R                  U:w  d  X   R                  U:w  d  M)  [        SU SU SU SX   R                   SU S	X   R                   35      e   X l	        X0l
        g )
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        €Ú¦C:\Users\johnn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\diffusers/models/adapter.pyr   ÚMultiAdapter.__init__)   s$  ø€ ÜŒl˜DÑ*Ô,ä˜x›=ˆÔÜŸš hÓ/ˆŒäˆx‹=˜AÓÜÐ=Ó>Ð>äˆx‹=˜AÓÜÐpÓqÐqð 08¸©{×/QÑ/QÐ,Ø)1°!©×)EÑ)EÐ&Ü˜œC ›MÖ*ˆCà‘×4Ñ4Ð8\Ó\Ø‘=×1Ñ1Ð5SÕSä ð:Ø:^Ð9_ð `4Ø4RÐ3Sð T Ø #˜uÐ$>¸x¹}×?cÑ?cÐ>dð e Ø #˜uÐ$8¸¹×9WÑ9WÐ8Xð	Zóð ñ +ð 'KÔ#Ø >Õó    NÚxsÚadapter_weightsÚreturnc                 óž  • Uc2  [         R                  " SU R                  -  /U R                  -  5      nO[         R                  " U5      nSn[        XU R                  5       He  u  pEnU" U5      nUc(  Un[        [        U5      5       H  nXSU   -  X8'   M     M9  [        [        U5      5       H  nX8==   XWU   -  -  ss'   M     Mg     U$ )aê  
Args:
    xs (`torch.Tensor`):
        A tensor of shape (batch, channel, height, width) representing input images for multiple adapter
        models, concatenated along dimension 1(channel dimension). The `channel` dimension should be equal to
        `num_adapter` * number of channel per image.

    adapter_weights (`list[float]`, *optional*, defaults to None):
        A list of floats representing the weights which will be multiplied by each adapter's output before
        summing them together. If `None`, equal weights will be used for all adapters.

Returns:
    `list[torch.Tensor]`:
        A list of feature tensors, one per scale, obtained by summing the per-scale features of each adapter
        weighted by `adapter_weights`.
Nr   )ÚtorchÚtensorr   Úzipr   r   r   )	r   r"   r#   Úaccume_stateÚxÚwÚadapterÚfeaturesÚis	            r   ÚforwardÚMultiAdapter.forwardL   sÃ   € ð" Ñ"Ü#Ÿlšl¨A°×0@Ñ0@Ñ,@Ð+AÀD×DTÑDTÑ+TÓU‰Oä#Ÿlšl¨?Ó;ˆOàˆÜ  °d·m±mÖD‰MˆA�'Ù˜q“zˆHØÑ#Ø'�Üœs <Ó0Ö1�AØ&'°q©/Ñ&9�L“Oó 2ô œs 8›}Ö-�AØ “O q°A©;¡Ñ6•Oó .ñ Eð Ðr!   Úsave_directoryÚis_main_processÚsave_functionÚsafe_serializationÚvariantc           	      ór   • SnUnU R                    H#  nUR                  UUUUUS9  US-  nUSU 3-   nM%     g)a¦  
Save a model and its configuration file to a specified directory, allowing it to be re-loaded with the
`[`~models.adapter.MultiAdapter.from_pretrained`]` class method.

Args:
    save_directory (`str` or `os.PathLike`):
        The directory where the model will be saved. If the directory does not exist, it will be created.
    is_main_process (`bool`, optional, defaults=True):
        Indicates whether current process is the main process or not. Useful for distributed training (e.g.,
        TPUs) and need to call this function on all processes. In this case, set `is_main_process=True` only
        for the main process to avoid race conditions.
    save_function (`Callable`):
        Function used to save the state dictionary. Useful for distributed training (e.g., TPUs) to replace
        `torch.save` with another method. Can also be configured using`DIFFUSERS_SAVE_MODE` environment
        variable.
    safe_serialization (`bool`, optional, defaults=True):
        If `True`, save the model using `safetensors`. If `False`, save the model with `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   ÚMultiAdapter.save_pretrainedn   s[   € ð8 ˆØ+ÐØ—}”}ˆGØ×#Ñ#Ø"Ø /Ø+Ø#5Øð $ñ ð �1‰HˆCØ!3¸¸#¸°iÑ!?Òò %r!   Úpretrained_model_pathc                 óæ  • Sn/ nUn[         R                  R                  U5      (       a[  [        R                  " U40 UD6nUR                  U5        US-  nUSU 3-   n[         R                  R                  U5      (       a  M[  [        R                  [        U5       SU S35        [        U5      S:X  a2  [        S[         R                  R                  U5       SUS-    S35      eU " U5      $ )	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, set it back to training mode using `model.train()`.

Warnings:
    *Weights from XXX not initialized from pretrained model* means that the weights of XXX are not pretrained
    with the rest of the model. It is up to you to train those weights with a downstream fine-tuning. *Weights
    from XXX not used in YYY* means that the layer XXX is not used by YYY, so those weights are discarded.

Args:
    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`.
    dtype (`torch.dtype`, *optional*):
        Override the default `torch.dtype` and load the model under this dtype.
    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, 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 mapping device identifiers to their maximum memory. 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 a `variant` file (*e.g.* pytorch_model.<variant>.bin).
    use_safetensors (`bool`, *optional*, defaults to `None`):
        If `None`, the `safetensors` weights will be downloaded if available **and** if`safetensors` library is
        installed. If `True`, the model will be forcibly loaded from`safetensors` weights. If `False`,
        `safetensors` is not used.
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   rB   ÚMultiAdapter.from_pretrained˜   s  € ðX ˆØˆð
 3ÐÜ�g‰g�m‰mÐ.×/Ñ/Ü ×0Ò0Ð1CÑNÀvÑNˆGØ�O‰O˜GÔ$à�1‰HˆCØ!6¸1¸S¸E¸Ñ!BÐô �g‰g�m‰mÐ.×/Ó/ô 	�‰”s˜8“}�oÐ%;Ð<QÐ;RÐRSÐTÔUäˆx‹=˜AÓÜØ-¬b¯g©g¯o©oÐ>SÓ.TÐ-UÐUiÐjð  CGñ  kGð  jHð  HIð  Jóð ñ �8‹}Ðr!   )r   r   r   r   ©N)TNTN)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Úlistr   r&   ÚTensorÚfloatr/   Ústrr?   ÚPathLikeÚboolr   r8   ÚclassmethodrB   Ú__static_attributes__Ú__classcell__©r   s   @r   r   r      sÒ   ø† ñ
ð!?  lÑ!3÷ !?ñF ˜%Ÿ,™,ð  ¸¸e¹ÀtÑ9Kð  ÐW[Ð\a×\hÑ\hÑWiõ  ðJ !%Ø"&Ø#'Ø"ñ(@à˜bŸk™kÑ)ð(@ð ð(@ð  ð	(@ð
 !ð(@ð �t‘õ(@ðT ð@°C¸"¿+¹+Ñ4EÈÑ4Ló @ó ö@r!   r   c                   óÆ   ^ • \ rS rSrSr\S/ SQSSS4S\S	\\   S
\S\S\4
U 4S jjj5       r	S\
R                  S\\
R                     4S jr\S 5       r\S 5       rSrU =r$ )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 common methods, such as
downloading or saving.

Args:
    in_channels (`int`, *optional*, defaults to `3`):
        The number of channels in the adapter's input (*control image*). Set it to 1 if you're using a gray scale
        image.
    channels (`list[int]`, *optional*, defaults to `(320, 640, 1280, 1280)`):
        The number of channels in each downsample block's output hidden state. The `len(block_out_channels)`
        determines 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`):
        Adapter type (`full_adapter` or `full_adapter_xl` or `light_adapter`) to use.
é   ©é@  é€  é   ra   r   é   Úfull_adapterÚin_channelsÚchannelsÚnum_res_blocksr   Úadapter_typec                 óÎ   >• [         TU ]  5         US:X  a  [        XX45      U l        g US:X  a  [	        XX45      U l        g US:X  a  [        XX45      U l        g [        SU S35      e)Nrc   Ú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   rd   re   rf   r   rg   r   s         €r   r   ÚT2IAdapter.__init__÷   sq   ø€ ô 	‰ÑÔà˜>Ó)Ü& {¸nÓ_ˆD�LØÐ.Ó.Ü(¨ÀÓaˆD�LØ˜_Ó,Ü'¨¸~Ó`ˆD�LäØ-¨l¨^ð <8ð 8óð r!   r*   r$   c                 ó$   • U R                  U5      $ )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.

Args:
    x (`torch.Tensor`):
        The input tensor to process through the adapter model.

Returns:
    `list[torch.Tensor]`:
        A list of feature tensors, each representing information extracted at a different scale from the input.
        The length of the list equals the number of downsample blocks in the adapter.
©r,   ©r   r*   s     r   r/   ÚT2IAdapter.forward  s   € ð  �|‰|˜A‹Ðr!   c                 ó.   • U R                   R                  $ rK   )r,   r   ©r   s    r   r   Ú!T2IAdapter.total_downscale_factor   s   € à�|‰|×2Ñ2Ð2r!   c                 óB   • U R                   R                  R                  $ )zÇThe 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   rt   s    r   r   ÚT2IAdapter.downscale_factor$  s   € ð
 �|‰|×%Ñ%×6Ñ6Ð6r!   rp   )rL   rM   rN   rO   rP   r   ÚintrQ   rT   r   r&   rR   r/   Úpropertyr   r   rX   rY   rZ   s   @r   r   r   Ü   s©   ø† ñð4 ð Ú4ØØ !Ø*ñàðð �s‘)ðð ð	ð
 ðð ÷ó ðð,˜Ÿ™ð ¨$¨u¯|©|Ñ*<ô ð$ ñ3ó ð3ð ñ7ó ö7r!   r   c            	       ó–   ^ • \ rS rSrSrS/ SQSS4S\S\\   S	\S
\4U 4S jjjrS\R                  S\\R                     4S jr
SrU =r$ )rk   i/  ú*
See [`T2IAdapter`] for more information.
r]   r^   r   rb   rd   re   rf   r   c                 ó¦  >• [         TU ]  5         XS-  -  n[        R                  " U5      U l        [        R
                  " XS   SSS9U l        [        R                  " [        US   US   U5      /[        S[        U5      5       Vs/ s H  n[        X%S-
     X%   USS9PM     snQ5      U l        US[        U5      S-
  -  -  U l        g s  snf ©Nr   r   r]   r   ©Úkernel_sizeÚpaddingT©Údown)r   r   r   ÚPixelUnshufflerw   ÚConv2dÚconv_inr   ÚAdapterBlockr   r   Úbodyr   ©r   rd   re   rf   r   r.   r   s         €r   r   ÚFullAdapter.__init__4  sÕ   ø€ ô 	‰ÑÔà!°aÑ$7Ñ7ˆä×*Ò*Ð+;Ó<ˆŒÜ—y’y °q©kÀqÐRSÑTˆŒä—M’Mä˜X a™[¨(°1©+°~ÓFðô # 1¤c¨(£mÔ4óâ4˜ô ! ¨a©%¡°(±+¸~ÐTXÔYÙ4ñðó
ˆŒ	ð '7¸¼sÀ8»}ÈqÑ?PÑ9QÑ&QˆÕ#ùòs   Â
C
r*   r$   c                 ó¦   • U R                  U5      nU R                  U5      n/ nU R                   H  nU" U5      nUR                  U5        M     U$ )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.
©rw   r†   rˆ   rC   ©r   r*   r-   Úblocks       r   r/   ÚFullAdapter.forwardN  sN   € ð �N‰N˜1ÓˆØ�L‰L˜‹Oˆàˆà—Y”YˆEÙ�a“ˆAØ�O‰O˜AÖñ ð ˆr!   ©rˆ   r†   r   rw   ©rL   rM   rN   rO   rP   ry   rQ   r   r&   rR   r/   rX   rY   rZ   s   @r   rk   rk   /  su   ø† ñð Ú4ØØ !ñRàðRð �s‘)ðRð ð	Rð
 ÷Rð Rð4˜Ÿ™ð ¨$¨u¯|©|Ñ*<÷ ò r!   rk   c            	       ó–   ^ • \ rS rSrSrS/ SQSS4S\S\\   S	\S
\4U 4S jjjrS\R                  S\\R                     4S jr
SrU =r$ )rl   ia  r|   r]   r^   r   é   rd   re   rf   r   c           
      ój  >• [         TU ]  5         XS-  -  n[        R                  " U5      U l        [        R
                  " XS   SSS9U l        / U l        [        [        U5      5       H–  nUS:X  a/  U R                  R                  [        X%S-
     X%   U5      5        M8  US:X  a.  U R                  R                  [        X%S-
     X%   USS95        Ml  U R                  R                  [        X%   X%   U5      5        M˜     [        R                  " U R                  5      U l        US-  U l        g r~   )r   r   r   r„   rw   r…   r†   rˆ   r   r   rC   r‡   r   r   r‰   s         €r   r   ÚFullAdapterXL.__init__f  sü   ø€ ô 	‰ÑÔà!°aÑ$7Ñ7ˆä×*Ò*Ð+;Ó<ˆŒÜ—y’y °q©kÀqÐRSÑTˆŒàˆŒ	ä”s˜8“}Ö%ˆAØ�A‹vØ—	‘	× Ñ ¤¨h¸1±u©o¸x¹{ÈNÓ![Ö\Ø�a“Ø—	‘	× Ñ ¤¨h¸1±u©o¸x¹{ÈNÐaeÑ!fÖgà—	‘	× Ñ ¤¨h©k¸8¹;ÈÓ!WÖXñ &ô —M’M $§)¡)Ó,ˆŒ	à&6¸Ñ&:ˆÕ#r!   r*   r$   c                 ó¦   • U R                  U5      nU R                  U5      n/ nU R                   H  nU" U5      nUR                  U5        M     U$ )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   r/   ÚFullAdapterXL.forward‚  sN   € ð
 �N‰N˜1ÓˆØ�L‰L˜‹Oˆàˆà—Y”YˆEÙ�a“ˆAØ�O‰O˜AÖñ ð ˆr!   r�   r‘   rZ   s   @r   rl   rl   a  so   ø† ñð Ú4ØØ "ñ;àð;ð �s‘)ð;ð ð	;ð
 ÷;ð ;ð8˜Ÿ™ð ¨$¨u¯|©|Ñ*<÷ ò r!   rl   c            	       ó~   ^ • \ rS rSrSrSS\S\S\S\4U 4S jjjrS\R                  S	\R                  4S
 jr
SrU =r$ )r‡   i“  aó  
An AdapterBlock is a helper model that contains multiple ResNet-like blocks. It is used in the `FullAdapter` and
`FullAdapterXL` models.

Args:
    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`):
        If `True`, perform downsampling on AdapterBlock's input.
rd   Úout_channelsrf   rƒ   c                 ó@  >• [         TU ]  5         S U l        U(       a  [        R                  " SSSS9U l        S U l        X:w  a  [        R                  " XSS9U l        [        R                  " [        U5       Vs/ s H  n[        U5      PM     sn6 U l
        g s  snf )Nr   T©r€   ÚstrideÚ	ceil_moder   ©r€   )r   r   Ú
downsampler   Ú	AvgPool2dÚin_convr…   Ú
Sequentialr   ÚAdapterResnetBlockÚresnets)r   rd   r™   rf   rƒ   r7   r   s         €r   r   ÚAdapterBlock.__init__£  s}   ø€ Ü‰ÑÔàˆŒÞÜ Ÿlšl°qÀÈdÑSˆDŒOàˆŒØÓ&ÜŸ9š9 [ÈAÑNˆDŒLä—}’}Ü8=¸nÔ8MÓNÒ8M°1Ô  Ö.Ñ8MÑNð
ˆ�ùÚNs   Á<Br*   r$   c                 ó    • U R                   b  U R                  U5      nU R                  b  U R                  U5      nU R                  U5      nU$ )zü
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¤   rq   s     r   r/   ÚAdapterBlock.forward²  sE   € ð �?‰?Ñ&Ø—‘ Ó"ˆAà�<‰<Ñ#Ø—‘˜Q“ˆAà�L‰L˜‹Oˆàˆr!   r§   ©F©rL   rM   rN   rO   rP   ry   rV   r   r&   rR   r/   rX   rY   rZ   s   @r   r‡   r‡   “  sO   ø† ññ
 Cð 
°sð 
ÈCð 
ÐW[÷ 
ð 
ð˜Ÿ™ð ¨%¯,©,÷ ò r!   r‡   c                   ón   ^ • \ rS rSrSrS\4U 4S jjrS\R                  S\R                  4S jr	Sr
U =r$ )	r£   iÃ  z²
An `AdapterResnetBlock` is a helper model that implements a ResNet-like block.

Args:
    channels (`int`):
        Number of channels of AdapterResnetBlock's input and output.
re   c                 óÀ   >• [         TU ]  5         [        R                  " XSSS9U l        [        R
                  " 5       U l        [        R                  " XSS9U l        g )Nr]   r   r   rž   ©r   r   r   r…   Úblock1ÚReLUÚactÚblock2©r   re   r   s     €r   r   ÚAdapterResnetBlock.__init__Ì  sA   ø€ Ü‰ÑÔÜ—i’i ÀÈ1ÑMˆŒÜ—7’7“9ˆŒÜ—i’i ÀÑBˆ�r!   r*   r$   c                 ól   • U R                  U R                  U5      5      nU R                  U5      nX!-   $ )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   r/   ÚAdapterResnetBlock.forwardÒ  ó.   € ð �H‰H�T—[‘[ “^Ó$ˆØ�K‰K˜‹Nˆà‰uˆr!   rµ   ©rL   rM   rN   rO   rP   ry   r   r&   rR   r/   rX   rY   rZ   s   @r   r£   r£   Ã  s6   ø† ñðC ÷ Cð	˜Ÿ™ð 	¨%¯,©,÷ 	ò 	r!   r£   c            	       ó–   ^ • \ rS rSrSrS/ SQSS4S\S\\   S	\S
\4U 4S jjjrS\R                  S\\R                     4S jr
SrU =r$ )rm   iá  r|   r]   )r_   r`   ra   é   rb   rd   re   rf   r   c                 ó„  >• [         TU ]  5         XS-  -  n[        R                  " U5      U l        [        R
                  " [        XS   U5      /[        [        U5      S-
  5       Vs/ s H  n[        X%   X%S-      USS9PM     snQ[        US   US   USS9P5      U l	        US[        U5      -  -  U l
        g s  snf )Nr   r   r   Tr‚   éÿÿÿÿ)r   r   r   r„   rw   r   ÚLightAdapterBlockr   r   rˆ   r   r‰   s         €r   r   ÚLightAdapter.__init__æ  sÒ   ø€ ô 	‰ÑÔà!°aÑ$7Ñ7ˆä×*Ò*Ð+;Ó<ˆŒä—M’Mä! +¸©{¸NÓKðô #¤3 x£=°1Ñ#4Ô5óâ5˜ô & h¡k°8À¹E±?ÀNÐY]Ô^Ù5ñðô " (¨2¡,°¸±¸nÐSWÑXðó	
ˆŒ	ð '7¸!¼sÀ8»}Ñ:LÑ&MˆÕ#ùòs   Á*B=
r*   r$   c                 ó„   • U R                  U5      n/ nU R                   H  nU" U5      nUR                  U5        M     U$ )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.
)rw   rˆ   rC   r�   s       r   r/   ÚLightAdapter.forward   sA   € ð
 �N‰N˜1Óˆàˆà—Y”YˆEÙ�a“ˆAØ�O‰O˜AÖñ ð ˆr!   )rˆ   r   rw   r‘   rZ   s   @r   rm   rm   á  su   ø† ñð Ú.ØØ !ñNàðNð �s‘)ðNð ð	Nð
 ÷Nð Nð4˜Ÿ™ð ¨$¨u¯|©|Ñ*<÷ ò r!   rm   c            	       ó~   ^ • \ rS rSrSrSS\S\S\S\4U 4S jjjrS\R                  S	\R                  4S
 jr
SrU =r$ )r¿   i  a  
A `LightAdapterBlock` is a helper model that contains multiple `LightAdapterResnetBlocks`. It is used in the
`LightAdapter` model.

Args:
    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`):
        If `True`, perform downsampling on LightAdapterBlock's input.
rd   r™   rf   rƒ   c                 óf  >• [         TU ]  5         US-  nS U l        U(       a  [        R                  " SSSS9U l        [        R
                  " XSS9U l        [        R                  " [        U5       Vs/ s H  n[        U5      PM     sn6 U l
        [        R
                  " XRSS9U l        g s  snf )Nr¼   r   Tr›   r   rž   )r   r   rŸ   r   r    r…   r¡   r¢   r   ÚLightAdapterResnetBlockr¤   Úout_conv)r   rd   r™   rf   rƒ   Úmid_channelsr7   r   s          €r   r   ÚLightAdapterBlock.__init__   s�   ø€ Ü‰ÑÔØ# qÑ(ˆàˆŒÞÜ Ÿlšl°qÀÈdÑSˆDŒOä—y’y ÈÑJˆŒÜ—}’}ÔV[Ð\jÔVkÓ&lÒVkÐQRÔ'>¸|Ö'LÑVkÑ&lÐmˆŒÜŸ	š	 ,È!ÑLˆ�ùò 'ms   Á5B.r*   r$   c                 ó¨   • U R                   b  U R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nU$ )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Æ   rq   s     r   r/   ÚLightAdapterBlock.forward,  sI   € ð
 �?‰?Ñ&Ø—‘ Ó"ˆAà�L‰L˜‹OˆØ�L‰L˜‹OˆØ�M‰M˜!Óˆàˆr!   )rŸ   r¡   rÆ   r¤   r©   rª   rZ   s   @r   r¿   r¿     sU   ø† ññ
M Cð 
M°sð 
MÈCð 
MÐW[÷ 
Mð 
Mð˜Ÿ™ð ¨%¯,©,÷ ò r!   r¿   c                   ón   ^ • \ rS rSrSrS\4U 4S jjrS\R                  S\R                  4S jr	Sr
U =r$ )	rÅ   i;  zü
A `LightAdapterResnetBlock` is a helper model that implements a ResNet-like block with a slightly different
architecture than `AdapterResnetBlock`.

Args:
    channels (`int`):
        Number of channels of LightAdapterResnetBlock's input and output.
re   c                 óÂ   >• [         TU ]  5         [        R                  " XSSS9U l        [        R
                  " 5       U l        [        R                  " XSSS9U l        g )Nr]   r   r   r­   r²   s     €r   r   Ú LightAdapterResnetBlock.__init__E  sC   ø€ Ü‰ÑÔÜ—i’i ÀÈ1ÑMˆŒÜ—7’7“9ˆŒÜ—i’i ÀÈ1ÑMˆ�r!   r*   r$   c                 ól   • U R                  U R                  U5      5      nU R                  U5      nX!-   $ )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   r/   ÚLightAdapterResnetBlock.forwardK  r¹   r!   rµ   rº   rZ   s   @r   rÅ   rÅ   ;  s6   ø† ñðN ÷ Nð	˜Ÿ™ð 	¨%¯,©,÷ 	ò 	r!   rÅ   )r?   Útypingr   r&   Útorch.nnr   Úconfiguration_utilsr   r   Úutilsr   Úmodeling_utilsr	   Ú
get_loggerrL   rD   r   r   ÚModulerk   rl   r‡   r£   rm   r¿   rÅ   © r!   r   Ú<module>rØ      sÃ   ðó 
Ý ã Ý ç AÝ Ý &ð 
×	Ò	˜HÓ	%€ô}�:ô }ô@M7�˜[ô M7ôf/�"—)‘)ô /ôd/�B—I‘Iô /ôd-�2—9‘9ô -ô`˜Ÿ™ô ô<,�2—9‘9ô ,ô^(˜Ÿ	™	ô (ôV˜bŸi™iõ r!   