ó
    oM‡j×=  ã                   óŠ  • S SK r S SKJr  S SKJs  Jr  SSKJr  SSKJ	r	  SSK
J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$                  S\ R$                  S-  S\S\S\ R$                  4
S jjrg)é    Né   )Ú	deprecateé   )ÚRMSNorm)Úupfirdn2d_nativec                   ó�   ^ • \ rS rSrSr    SS\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$ )ÚDownsample1Dé   aÏ  A 1D downsampling layer with an optional convolution.

Parameters:
    channels (`int`):
        number of channels in the inputs and outputs.
    use_conv (`bool`, default `False`):
        option to use a convolution.
    out_channels (`int`, optional):
        number of output channels. Defaults to `channels`.
    padding (`int`, default `1`):
        padding for the convolution.
    name (`str`, default `conv`):
        name of the downsampling 1D layer.
NÚchannelsÚuse_convÚout_channelsÚpaddingÚnamec                 óP  >• [         TU ]  5         Xl        U=(       d    UU l        X l        X@l        SnXPl        U(       a1  [        R                  " U R                  U R                  SXdS9U l	        g U R                  U R                  :X  d   e[        R                  " XfS9U l	        g )Nr   é   ©Ústrider   ©Úkernel_sizer   )ÚsuperÚ__init__r   r   r   r   r   ÚnnÚConv1dÚconvÚ	AvgPool1d)Úselfr   r   r   r   r   r   Ú	__class__s          €Ú«C:\Users\johnn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\diffusers/models/downsampling.pyr   ÚDownsample1D.__init__(   s�   ø€ ô 	‰ÑÔØ ŒØ(×4¨HˆÔØ ŒØŒØˆØŒ	æÜŸ	š	 $§-¡-°×1BÑ1BÀAÈfÑfˆD�Ià—=‘= D×$5Ñ$5Ó5Ð5Ð5ÜŸš°ÑGˆD�Ió    ÚinputsÚreturnc                 ób   • UR                   S   U R                  :X  d   eU R                  U5      $ )Nr   )Úshaper   r   )r   r!   s     r   ÚforwardÚDownsample1D.forward>   s+   € Ø�|‰|˜A‰ $§-¡-Ó/Ð/Ð/Ø�y‰y˜Ó Ð r    )r   r   r   r   r   r   )FNr   r   ©Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__ÚintÚboolÚstrr   ÚtorchÚTensorr%   Ú__static_attributes__Ú__classcell__©r   s   @r   r	   r	      s{   ø† ñð$ Ø#'ØØñHàðHð ðHð ˜D‘jð	Hð
 ðHð ÷Hð Hð,!˜eŸl™lð !¨u¯|©|÷ !ò !r    r	   c                   óš   ^ • \ rS rSrSr         SS\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$ )ÚDownsample2DéC   aÏ  A 2D downsampling layer with an optional convolution.

Parameters:
    channels (`int`):
        number of channels in the inputs and outputs.
    use_conv (`bool`, default `False`):
        option to use a convolution.
    out_channels (`int`, optional):
        number of output channels. Defaults to `channels`.
    padding (`int`, default `1`):
        padding for the convolution.
    name (`str`, default `conv`):
        name of the downsampling 2D layer.
Nr   r   r   r   r   c           	      ó0  >• [         TU ]  5         Xl        U=(       d    UU l        X l        X@l        SnXPl        US:X  a  [        R                  " XU	5      U l	        O1US:X  a  [        XU	5      U l	        OUc  S U l	        O[        SU 35      eU(       a,  [        R                  " U R                  U R                  XkXJS9nO0U R                  U R                  :X  d   e[        R                  " X»S9nUS:X  a  XÀl        XÀl        g US:X  a  XÀl        g XÀl        g )	Nr   Úln_normÚrms_normzunknown norm_type: )r   r   r   Úbiasr   r   ÚConv2d_0)r   r   r   r   r   r   r   r   Ú	LayerNormÚnormr   Ú
ValueErrorÚConv2dÚ	AvgPool2dr<   r   )r   r   r   r   r   r   r   Ú	norm_typeÚepsÚelementwise_affiner;   r   r   r   s                €r   r   ÚDownsample2D.__init__S   sú   ø€ ô 	‰ÑÔØ ŒØ(×4¨HˆÔØ ŒØŒØˆØŒ	à˜	Ó!ÜŸš XÐ4FÓGˆD�IØ˜*Ó$Ü Ð/AÓBˆD�IØÑØˆD�IäÐ2°9°+Ð>Ó?Ð?æÜ—9’9Ø—‘˜t×0Ñ0¸kÐbiñ‰Dð —=‘= D×$5Ñ$5Ó5Ð5Ð5Ü—<’<¨FÑBˆDð �6‹>Ø ŒMØ�IØ�ZÓØ�Ià�Ir    Úhidden_statesr"   c                 óü  • [        U5      S:”  d  UR                  SS 5      b  Sn[        SSU5        UR                  S   U R                  :X  d   eU R
                  b5  U R                  UR                  SSSS5      5      R                  SSSS5      nU R                  (       a(  U R                  S:X  a  Sn[        R                  " XS	SS
9nUR                  S   U R                  :X  d   eU R                  U5      nU$ )Nr   ÚscalezðThe `scale` argument is deprecated and will be ignored. Please remove it, as passing it will raise an error in the future. `scale` should directly be passed while calling the underlying pipeline component i.e., via `cross_attention_kwargs`.z1.0.0r   r   r   ©r   r   r   r   Úconstant©ÚmodeÚvalue)ÚlenÚgetr   r$   r   r>   Úpermuter   r   ÚFÚpadr   )r   rF   ÚargsÚkwargsÚdeprecation_messagerR   s         r   r%   ÚDownsample2D.forward‚   sè   € Üˆt‹9�q‹=˜FŸJ™J w°Ó5ÑAð #UÐÜ�g˜wÐ(;Ô<Ø×"Ñ" 1Ñ%¨¯©Ó6Ð6Ð6à�9‰9Ñ Ø ŸI™I m×&;Ñ&;¸A¸qÀ!ÀQÓ&GÓH×PÑPÐQRÐTUÐWXÐZ[Ó\ˆMà�=�=˜TŸ\™\¨QÓ.ØˆCÜŸEšE -¸:ÈQÑOˆMà×"Ñ" 1Ñ%¨¯©Ó6Ð6Ð6àŸ	™	 -Ó0ˆàÐr    )r<   r   r   r   r>   r   r   r   )	FNr   r   r   NNNTr'   r4   s   @r   r6   r6   C   s„   ø† ñð$ Ø#'ØØØØØØØñ-àð-ð ð-ð ˜D‘jð	-ð
 ð-ð ÷-ð -ð^ U§\¡\ð ÀuÇ|Á|÷ ò r    r6   c                   ó*  ^ • \ rS rSrSr    SS\S-  S\S-  S\S\\\\\4   4U 4S jjjr    SS	\	R                  S
\	R                  S-  S\	R                  S-  S\S\S\	R                  4S jjrS	\	R                  S\	R                  4S jrSrU =r$ )ÚFirDownsample2Dé–   a”  A 2D FIR downsampling layer with an optional convolution.

Parameters:
    channels (`int`):
        number of channels in the inputs and outputs.
    use_conv (`bool`, default `False`):
        option to use a convolution.
    out_channels (`int`, optional):
        number of output channels. Defaults to `channels`.
    fir_kernel (`tuple`, default `(1, 3, 3, 1)`):
        kernel for the FIR filter.
Nr   r   r   Ú
fir_kernelc                 ó¢   >• [         TU ]  5         U(       a  UOUnU(       a  [        R                  " XSSSS9U l        X@l        X0l        X l        g )Nr   r   ©r   r   r   )r   r   r   r@   r<   rZ   r   r   )r   r   r   r   rZ   r   s        €r   r   ÚFirDownsample2D.__init__¤   sD   ø€ ô 	‰ÑÔÞ'3‘|¸ˆÞÜŸIšI hÈ!ÐTUÐ_`ÑaˆDŒMØ$ŒØ ŒØ(Õr    rF   ÚweightÚkernelÚfactorÚgainr"   c                 óÌ  • [        U[        5      (       a  US:¼  d   eUc  S/U-  n[        R                  " U[        R                  S9nUR
                  S:X  a  [        R                  " X35      nU[        R                  " U5      -  nX5-  nU R                  (       a{  UR                  u    pgnUR                  S   U-
  US-
  -   n	XD/n
[        UUR                  UR                  UR                  S9U	S-   S-  U	S-  4S9n[        R                  " X²U
SS9nU$ UR                  S   U-
  n	[        UUR                  UR                  UR                  S9UU	S-   S-  U	S-  4S9nU$ )	az  Fused `Conv2d()` followed by `downsample_2d()`.
Padding is performed only once at the beginning, not between the operations. The fused op is considerably more
efficient than performing the same calculation using standard TensorFlow ops. It supports gradients of
arbitrary order.

Args:
    hidden_states (`torch.Tensor`):
        Input tensor of the shape `[N, C, H, W]` or `[N, H, W, C]`.
    weight (`torch.Tensor`, *optional*):
        Weight tensor of the shape `[filterH, filterW, inChannels, outChannels]`. Grouped convolution can be
        performed by `inChannels = x.shape[0] // numGroups`.
    kernel (`torch.Tensor`, *optional*):
        FIR filter of the shape `[firH, firW]` or `[firN]` (separable). The default is `[1] * factor`, which
        corresponds to average pooling.
    factor (`int`, *optional*, default to `2`):
        Integer downsampling factor.
    gain (`float`, *optional*, default to `1.0`):
        Scaling factor for signal magnitude.

Returns:
    output (`torch.Tensor`):
        Tensor of the shape `[N, C, H // factor, W // factor]` or `[N, H // factor, W // factor, C]`, and same
        datatype as `x`.
r   ©Údtyper   ©Údevicerd   r   )rR   r   ©ÚdownrR   )Ú
isinstancer-   r0   ÚtensorÚfloat32ÚndimÚouterÚsumr   r$   r   Útorf   rd   rQ   Úconv2d)r   rF   r^   r_   r`   ra   Ú_ÚconvHÚconvWÚ	pad_valueÚstride_valueÚupfirdn_inputÚoutputs                r   Ú_downsample_2dÚFirDownsample2D._downsample_2d³   sh  € ôB ˜&¤#×&Ñ&¨6°Q«;Ð6Ð6Ø‰>Ø�S˜6‘\ˆFô —’˜f¬E¯M©MÑ:ˆØ�;‰;˜!ÓÜ—[’[ Ó0ˆFØ”%—)’)˜FÓ#Ñ#ˆà‘ˆà�=�=Ø!'§¡ÑˆAˆq˜ØŸ™ a™¨6Ñ1°e¸a±iÑ@ˆIØ"Ð+ˆLÜ,ØØ—	‘	 ×!5Ñ!5¸]×=PÑ=P�	ÐQØ !‘m¨Ñ)¨9¸©>Ð:ñˆMô
 —X’X˜m¸LÐRSÑTˆFð ˆð Ÿ™ Q™¨&Ñ0ˆIÜ%ØØ—	‘	 ×!5Ñ!5¸]×=PÑ=P�	ÐQØØ !‘m¨Ñ)¨9¸©>Ð:ñ	ˆFð ˆr    c                 ó  • U R                   (       aZ  U R                  XR                  R                  U R                  S9nX R                  R
                  R                  SSSS5      -   nU$ U R                  XR                  SS9nU$ )N)r^   r_   r   éÿÿÿÿr   )r_   r`   )r   rx   r<   r^   rZ   r;   Úreshape)r   rF   Údownsample_inputs      r   r%   ÚFirDownsample2D.forwardõ   s   € Ø�=�=Ø#×2Ñ2°=ÏÉ×I]ÑI]Ðfj×fuÑfuÐ2ÐvÐØ,¯}©}×/AÑ/A×/IÑ/IÈ!ÈRÐQRÐTUÓ/VÑVˆMð Ðð !×/Ñ/°ÇoÁoÐ^_Ð/Ð`ˆMàÐr    )r<   rZ   r   r   )NNF)r   r   r   r   )NNr   r   )r(   r)   r*   r+   r,   r-   r.   Útupler   r0   r1   Úfloatrx   r%   r2   r3   r4   s   @r   rX   rX   –   së   ø† ñð  $Ø#'ØØ0<ñ)à˜‘*ð)ð ˜D‘jð)ð ð	)ð
 ˜#˜s C¨Ð,Ñ-÷)ð )ð$ '+Ø&*ØØñ@à—|‘|ð@ð —‘˜tÑ#ð@ð —‘˜tÑ#ð	@ð
 ð@ð ð@ð 
�‰õ@ðD U§\¡\ð °e·l±l÷ ò r    rX   c                   ór   ^ • \ rS rSrSrS	S\4U 4S jjjrS\R                  S\R                  4S jr	Sr
U =r$ )
ÚKDownsample2Dé   z{A 2D K-downsampling layer.

Parameters:
    pad_mode (`str`, *optional*, default to `"reflect"`): the padding mode to use.
Úpad_modec                 óÐ   >• [         TU ]  5         Xl        [        R                  " / SQ/5      nUR
                  S   S-  S-
  U l        U R                  SUR                  U-  SS9  g )N)ç      À?ç      Ø?r‡   r†   r   r   r_   F)Ú
persistent)	r   r   r„   r0   rj   r$   rR   Úregister_bufferÚT)r   r„   Ú	kernel_1dr   s      €r   r   ÚKDownsample2D.__init__  s]   ø€ Ü‰ÑÔØ ŒÜ—L’LÒ">Ð!?Ó@ˆ	Ø—?‘? 1Ñ%¨Ñ*¨QÑ.ˆŒØ×Ñ˜X y§{¡{°YÑ'>È5ÐÒQr    r!   r"   c                 ó&  • [         R                  " XR                  4S-  U R                  5      nUR                  UR                  S   UR                  S   U R
                  R                  S   U R
                  R                  S   /5      n[        R                  " UR                  S   UR                  S9nU R
                  R                  U5      S S S 24   R                  UR                  S   SS5      nXBX34'   [         R                  " XSS9$ )Né   r   r   )rf   r{   r   )r   )rQ   rR   r„   Ú	new_zerosr$   r_   r0   Úarangerf   ro   Úexpandrp   )r   r!   r^   Úindicesr_   s        r   r%   ÚKDownsample2D.forward  sÛ   € Ü—’�v§¡˜{¨Q™°·±Ó>ˆØ×!Ñ!à—‘˜Q‘Ø—‘˜Q‘Ø—‘×!Ñ! !Ñ$Ø—‘×!Ñ! !Ñ$ð	ó
ˆô —,’,˜vŸ|™|¨A™°v·}±}ÑEˆØ—‘—‘ Ó'¨ªa¨Ñ0×7Ñ7¸¿¹ÀQ¹ÈÈRÓPˆØ#)ˆwÐÑ Ü�xŠx˜¨qÑ1Ð1r    )rR   r„   )Úreflect)r(   r)   r*   r+   r,   r/   r   r0   r1   r%   r2   r3   r4   s   @r   r‚   r‚      s<   ø† ññR ÷ Rð Rð2˜eŸl™lð 2¨u¯|©|÷ 2ò 2r    r‚   c                   óŽ   ^ • \ rS rSrSr    SS\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$ )ÚCogVideoXDownsample3Di  aO  
A 3D Downsampling layer using in [CogVideoX]() by Tsinghua University & ZhipuAI

Args:
    in_channels (`int`):
        Number of channels in the input image.
    out_channels (`int`):
        Number of channels produced by the convolution.
    kernel_size (`int`, defaults to `3`):
        Size of the convolving kernel.
    stride (`int`, defaults to `2`):
        Stride of the convolution.
    padding (`int`, defaults to `0`):
        Padding added to all four sides of the input.
    compress_time (`bool`, defaults to `False`):
        Whether or not to compress the time dimension.
Úin_channelsr   r   r   r   Úcompress_timec                 ód   >• [         TU ]  5         [        R                  " XX4US9U l        X`l        g )Nr\   )r   r   r   r@   r   r˜   )r   r—   r   r   r   r   r˜   r   s          €r   r   ÚCogVideoXDownsample3D.__init__2  s+   ø€ ô 	‰ÑÔä—I’I˜kÀ[ÐipÑqˆŒ	Ø*Õr    Úxr"   c                 óÂ  • U R                   (       Ga  UR                  u  p#pEnUR                  SSSSS5      R                  X%-  U-  X45      nUR                  S   S-  S:X  a„  US   USSS 24   p‡UR                  S   S:”  a  [        R
                  " USSS	9n[        R                  " US
   U/SS9nUR                  X%XcUR                  S   5      R                  SSSSS5      nOI[        R
                  " USSS	9nUR                  X%XcUR                  S   5      R                  SSSSS5      nSn	[        R                  " XSSS9nUR                  u  p#pEnUR                  SSSSS5      R                  X$-  X5U5      nU R                  U5      nUR                  X$UR                  S   UR                  S   UR                  S   5      R                  SSSSS5      nU$ )Nr   r   rŽ   r   r   r{   ).r   .r   ).N)ÚdimrI   rJ   rK   )
r˜   r$   rP   r|   rQ   Ú
avg_pool1dr0   ÚcatrR   r   )
r   r›   Ú
batch_sizer   ÚframesÚheightÚwidthÚx_firstÚx_restrR   s
             r   r%   ÚCogVideoXDownsample3D.forward@  sÑ  € Ø××ÐØ:;¿'¹'Ñ7ˆJ &°%ð —	‘	˜!˜Q  1 aÓ(×0Ñ0°Ñ1DÀuÑ1LÈhÓ_ˆAà�w‰w�r‰{˜Q‰ !Ó#Ø"# F¡)¨Q¨s°A±B¨w©Z˜Ø—<‘< Ñ# aÓ'äŸ\š\¨&¸aÈÑJ�Fä—I’I˜w yÑ1°6Ð:ÀÑC�à—I‘I˜j°%À1Ç7Á7È2Á;ÓO×WÑWÐXYÐ[\Ð^_ÐabÐdeÓf‘ô —L’L °¸!Ñ<�à—I‘I˜j°%À1Ç7Á7È2Á;ÓO×WÑWÐXYÐ[\Ð^_ÐabÐdeÓf�ð ˆÜ�EŠE�!˜z°Ñ3ˆØ67·g±gÑ3ˆ
˜f¨eà�I‰I�a˜˜A˜q !Ó$×,Ñ,¨ZÑ-@À(ÐTYÓZˆØ�I‰I�a‹Lˆà�I‰I�j¨!¯'©'°!©*°a·g±g¸a±jÀ!Ç'Á'È!Á*ÓM×UÑUÐVWÐYZÐ\]Ð_`ÐbcÓdˆØˆr    )r˜   r   )r   r   r   F)r(   r)   r*   r+   r,   r-   r.   r   r0   r1   r%   r2   r3   r4   s   @r   r–   r–     sz   ø† ñð, ØØØ#ñ+àð+ð ð+ð ð	+ð
 ð+ð ð+ð ÷+ð +ð˜Ÿ™ð ¨%¯,©,÷ ò r    r–   rF   r_   r`   ra   r"   c                 ó´  • [        U[        5      (       a  US:¼  d   eUc  S/U-  n[        R                  " U[        R                  S9nUR
                  S:X  a  [        R                  " X5      nU[        R                  " U5      -  nX-  nUR                  S   U-
  n[        U UR                  U R                  U R                  S9UUS-   S-  US-  4S9nU$ )aý  Downsample2D a batch of 2D images with the given filter.
Accepts a batch of 2D images of the shape `[N, C, H, W]` or `[N, H, W, C]` and downsamples each image with the
given filter. The filter is normalized so that if the input pixels are constant, they will be scaled by the
specified `gain`. Pixels outside the image are assumed to be zero, and the filter is padded with zeros so that its
shape is a multiple of the downsampling factor.

Args:
    hidden_states (`torch.Tensor`)
        Input tensor of the shape `[N, C, H, W]` or `[N, H, W, C]`.
    kernel (`torch.Tensor`, *optional*):
        FIR filter of the shape `[firH, firW]` or `[firN]` (separable). The default is `[1] * factor`, which
        corresponds to average pooling.
    factor (`int`, *optional*, default to `2`):
        Integer downsampling factor.
    gain (`float`, *optional*, default to `1.0`):
        Scaling factor for signal magnitude.

Returns:
    output (`torch.Tensor`):
        Tensor of the shape `[N, C, H // factor, W // factor]`
r   rc   r   re   r   rg   )ri   r-   r0   rj   rk   rl   rm   rn   r$   r   ro   rf   rd   )rF   r_   r`   ra   rt   rw   s         r   Údownsample_2dr¨   b  sÏ   € ô8 �fœc×"Ñ" v°£{Ð2Ð2Ø�~Ø��v‘ˆä�\Š\˜&¬¯©Ñ6€FØ‡{�{�aÓÜ—’˜VÓ,ˆØ
Œe�iŠi˜ÓÑ€Fà‰]€FØ—‘˜Q‘ &Ñ(€IÜØØ�	‰	˜×-Ñ-°]×5HÑ5Hˆ	ÐIØØ˜!‰m Ñ! 9°¡>Ð2ñ	€Fð €Mr    )Nr   r   )r0   Útorch.nnr   Útorch.nn.functionalÚ
functionalrQ   Úutilsr   Únormalizationr   Ú
upsamplingr   ÚModuler	   r6   rX   r‚   r–   r1   r-   r€   r¨   © r    r   Ú<module>r±      sÅ   ðó Ý ß Ð å Ý "Ý (ô(!�2—9‘9ô (!ôVP�2—9‘9ô Pôff�b—i‘iô fôT2�B—I‘Iô 2ô<A˜BŸI™Iô AðL #'ØØñ	-Ø—<‘<ð-à�L‰L˜4Ñð-ð ð-ð ð	-ð
 ‡\�\ö-r    