
    
3jB                     V   S SK r S SKJr  S SKrS SKJr  S SKJ	r	  SSK
JrJr  SSKJrJr  SSKJrJr  SSKJr  SS	KJrJrJr  \R2                  " \5      r\ R8                  R:                   " S
 S\5      5       r " S S\R>                  5      r \ " S S\R>                  \\5      5       r!g)    N)
FrozenDict   )ConfigMixinflax_register_to_config)
BaseOutputlogging   )FlaxTimestepEmbeddingFlaxTimesteps)FlaxModelMixin)FlaxCrossAttnDownBlock2DFlaxDownBlock2DFlaxUNetMidBlock2DCrossAttnc                   V    \ rS rSr% Sr\R                  \S'   \R                  \S'   Srg)FlaxControlNetOutput"   z
The output of [`FlaxControlNetModel`].

Args:
    down_block_res_samples (`jnp.ndarray`):
    mid_block_res_sample (`jnp.ndarray`):
down_block_res_samplesmid_block_res_sample N)	__name__
__module____qualname____firstlineno____doc__jnpndarray__annotations____static_attributes__r       f/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/models/controlnets/controlnet_flax.pyr   r   "   s      KK'++%r   r   c                       \ rS rSr% \\S'   Sr\\S4   \S'   \R                  r
\R                  \S'   SS	 jrS
\R                  S\R                  4S jrSrg)#FlaxControlNetConditioningEmbedding0   conditioning_embedding_channels       `      .block_out_channelsdtypereturnNc           	         [         R                  S5        [        R                  " U R                  S   SSU R
                  S9U l        / n[        [        U R                  5      S-
  5       H  nU R                  U   nU R                  US-      n[        R                  " USSU R
                  S9nUR                  U5        [        R                  " USSSU R
                  S9nUR                  U5        M     Xl
        [        R                  " U R                  SS[        R                  R                  5       [        R                  R                  5       U R
                  S	9U l        g )
NFlax classes are deprecated and will be removed in Diffusers v1.0.0. We recommend migrating to PyTorch classes or pinning your version of Diffusers.r   r   r      r2   r1   )kernel_sizepaddingr+   r2   )r	   r	   r3   stridesr4   r+   r3   r4   kernel_init	bias_initr+   )loggerwarningnnConvr*   r+   conv_inrangelenappendblocksr$   initializers
zeros_initconv_out)selfrB   i
channel_inchannel_outconv1conv2s          r    setup)FlaxControlNetConditioningEmbedding.setup5   s3   [	

 ww##A&$**	
 s4223a78A003J11!a%8KGG"(jj	E MM% GG"(jjE MM% # 9$ 00$224oo002**
r   conditioningc                     U R                  U5      n[        R                  " U5      nU R                   H!  nU" U5      n[        R                  " U5      nM#     U R	                  U5      nU$ )N)r>   r<   silurB   rE   )rF   rN   	embeddingblocks       r    __call__,FlaxControlNetConditioningEmbedding.__call__`   sZ    LL.	GGI&	[[Ei(I	*I ! MM),	r   )rB   r>   rE   r,   N)r   r   r   r   intr   r*   tupler   float32r+   rL   r   rS   r   r   r   r    r"   r"   0   sN    %((*;c3h;{{E399")
V
S[[ 
S[[ 
r   r"   c                   x   \ rS rSr% SrSr\\S'   Sr\\S'   Sr	\
\S4   \S	'   S
r\\
\S4   -  \S'   Sr\
\S4   \S'   Sr\\S'   Sr\\
\S4   -  \S'   Sr\\
\S4   -  S-  \S'   Sr\\S'   Sr\\S'   S
r\\S'   \R.                  r\R0                  \S'   Sr\\S'   Sr\\S'   Sr\\S'   S r\
\S4   \S!'   S"\R<                  S#\4S$ jr S/S% jr!   S0S&\RD                  S'\RD                  \-  \-  S(\RD                  S)\RD                  S*\S+\S,\S#\#\
\
\RD                  S4   \RD                  4   -  4S- jjr$S.r%g)1FlaxControlNetModelm   u
  
A ControlNet model.

This model inherits from [`FlaxModelMixin`]. Check the superclass documentation for it’s generic methods
implemented for all models (such as downloading or saving).

This model is also a Flax Linen [`flax.linen.Module`](https://flax.readthedocs.io/en/latest/flax.linen.html#module)
subclass. Use it as a regular Flax Linen module and refer to the Flax documentation for all matters related to its
general usage and behavior.

Inherent JAX features such as the following are supported:

- [Just-In-Time (JIT) compilation](https://jax.readthedocs.io/en/latest/jax.html#just-in-time-compilation-jit)
- [Automatic Differentiation](https://jax.readthedocs.io/en/latest/jax.html#automatic-differentiation)
- [Vectorization](https://jax.readthedocs.io/en/latest/jax.html#vectorization-vmap)
- [Parallelization](https://jax.readthedocs.io/en/latest/jax.html#parallelization-pmap)

Parameters:
    sample_size (`int`, *optional*):
        The size of the input sample.
    in_channels (`int`, *optional*, defaults to 4):
        The number of channels in the input sample.
    down_block_types (`tuple[str]`, *optional*, defaults to `("FlaxCrossAttnDownBlock2D", "FlaxCrossAttnDownBlock2D", "FlaxCrossAttnDownBlock2D", "FlaxDownBlock2D")`):
        The tuple of downsample blocks to use.
    block_out_channels (`tuple[int]`, *optional*, defaults to `(320, 640, 1280, 1280)`):
        The tuple of output channels for each block.
    layers_per_block (`int`, *optional*, defaults to 2):
        The number of layers per block.
    attention_head_dim (`int` or `tuple[int]`, *optional*, defaults to 8):
        The dimension of the attention heads.
    num_attention_heads (`int` or `tuple[int]`, *optional*):
        The number of attention heads.
    cross_attention_dim (`int`, *optional*, defaults to 768):
        The dimension of the cross attention features.
    dropout (`float`, *optional*, defaults to 0):
        Dropout probability for down, up and bottleneck blocks.
    flip_sin_to_cos (`bool`, *optional*, defaults to `True`):
        Whether to flip the sin to cos in the time embedding.
    freq_shift (`int`, *optional*, defaults to 0): The frequency shift to apply to the time embedding.
    controlnet_conditioning_channel_order (`str`, *optional*, defaults to `rgb`):
        The channel order of conditional image. Will convert to `rgb` if it's `bgr`.
    conditioning_embedding_out_channels (`tuple`, *optional*, defaults to `(16, 32, 96, 256)`):
        The tuple of output channel for each block in the `conditioning_embedding` layer.
r'   sample_size   in_channels)CrossAttnDownBlock2Dr_   r_   DownBlock2D.down_block_typesFonly_cross_attention)i@  i     rc   r*   r	   layers_per_block   attention_head_dimNnum_attention_headsrc   cross_attention_dimg        dropoutuse_linear_projectionr+   Tflip_sin_to_cosr   
freq_shiftrgb%controlnet_conditioning_channel_orderr%   #conditioning_embedding_out_channelsrngr,   c                 2   SU R                   U R                  U R                  4n[        R                  " U[        R                  S9n[        R
                  " S[        R                  S9n[        R                  " SSU R                  4[        R                  S9nSSU R                  S-  U R                  S-  4n[        R                  " U[        R                  S9n[        R                  R                  U5      u  pXS.n
U R                  XXEU5      S   $ )Nr2   r+   )r2   r   re   )paramsri   rs   )r^   r\   r   zerosrX   onesint32rh   jaxrandomsplitinit)rF   rp   sample_shapesample	timestepsencoder_hidden_statescontrolnet_cond_shapecontrolnet_cond
params_rngdropout_rngrngss              r    init_weights FlaxControlNetModel.init_weights   s    4++T-=-=t?O?OP<s{{;HHT3	 #		1a1I1I*JRUR]R] ^!"At'7'7!';T=M=MPQ=Q R))$9M"%**"2"23"7
$=yyyYZbccr   c                 4   [         R                  S5        U R                  nUS   S-  nU R                  =(       d    U R                  n[
        R                  " US   SSSU R                  S9U l        [        US   U R                  U R                  R                  S9U l        [        X R                  S	9U l        [!        US   U R"                  S
9U l        U R&                  n[)        U[*        5      (       a  U4[-        U R.                  5      -  n[)        U[0        5      (       a  U4[-        U R.                  5      -  n/ n/ nUS   n[
        R                  " USS[
        R2                  R5                  5       [
        R2                  R5                  5       U R                  S9nUR7                  U5        [9        U R.                  5       GH  u  pUnX   nU	[-        U5      S-
  :H  nU
S:X  aC  [;        UUU R<                  U R>                  X9   U(       + U R@                  XI   U R                  S9	nO1[C        UUU R<                  U R>                  U(       + U R                  S9nUR7                  U5        [E        U R>                  5       Ho  n[
        R                  " USS[
        R2                  R5                  5       [
        R2                  R5                  5       U R                  S9nUR7                  U5        Mq     U(       a  GM:  [
        R                  " USS[
        R2                  R5                  5       [
        R2                  R5                  5       U R                  S9nUR7                  U5        GM     XPl#        X`l$        US   n[K        UU R<                  US   U R@                  U R                  S9U l&        [
        R                  " USS[
        R2                  R5                  5       [
        R2                  R5                  5       U R                  S9U l'        g )Nr.   r   r]   r/   r1   r0   r5   )rk   rl   rr   )r$   r*   VALIDr7   r2   r_   )	r^   out_channelsri   
num_layersrg   add_downsamplerj   rb   r+   )r^   r   ri   r   r   r+   )r^   ri   rg   rj   r+   )(r:   r;   r*   rg   rf   r<   r=   r+   r>   r   rk   configrl   	time_projr
   time_embeddingr"   ro   controlnet_cond_embeddingrb   
isinstanceboolr@   ra   rV   rC   rD   rA   	enumerater   ri   rd   rj   r   r?   down_blockscontrolnet_down_blocksr   	mid_blockcontrolnet_mid_block)rF   r*   time_embed_dimrg   rb   r   r   output_channelcontrolnet_blockrG   down_block_typeinput_channelis_final_block
down_block_mid_block_channels                   r    rL   FlaxControlNetModel.setup   s   [	

 "44+A.2 #66Q$:Q:Q wwq!$**
 'q!43G3GTXT_T_TjTj
 4N**U)L,>q,A#GG*
&
  $88*D11$8#:SAVAV=W#W )3//#6"83t?T?T;U"U !#+A.77224oo002**
 	%%&67"+D,A,A"BA*M/2N#&8"9A"==N"885 -!/ LL#44(;(>'5#5*.*D*D)=)@**

 - -!/ LL#44'5#5**
 z*4001#%77" &# " : : < oo88:**$  '--.>? 2 ">#%77" &# " : : < oo88:**$  '--.>?c #Cf '&<# /r24)LL 3B 7"&"<"<**
 %'GG224oo002**%
!r   r|   r}   r~   r   conditioning_scalereturn_dicttrainc                 D   U R                   nUS:X  a  [        R                  " USS9n[        U[        R                  5      (       d%  [        R
                  " U/[        R                  S9nOl[        U[        R                  5      (       aM  [        UR                  5      S:X  a4  UR                  [        R                  S9n[        R                  " US5      nU R                  U5      n	U R                  U	5      n	[        R                  " US5      nU R                  U5      n[        R                  " US5      nU R!                  U5      nX-  nU4n
U R"                   H9  n[        U[$        5      (       a  U" XX7(       + S9u  pOU" XU(       + S9u  pX-  n
M;     U R'                  XX7(       + S9nSn[)        XR*                  5       H  u  pU" U5      nX4-  nM     Un
U R-                  U5      nU
 Vs/ s H  oU-  PM	     n
nUU-  nU(       d  U
U4$ [/        U
US	9$ s  snf )
a*  
Args:
    sample (`jnp.ndarray`): (batch, channel, height, width) noisy inputs tensor
    timestep (`jnp.ndarray` or `float` or `int`): timesteps
    encoder_hidden_states (`jnp.ndarray`): (batch_size, sequence_length, hidden_size) encoder hidden states
    controlnet_cond (`jnp.ndarray`): (batch, channel, height, width) the conditional input tensor
    conditioning_scale (`float`, *optional*, defaults to `1.0`): the scale factor for controlnet outputs
    return_dict (`bool`, *optional*, defaults to `True`):
        Whether or not to return a [`models.unets.unet_2d_condition_flax.FlaxUNet2DConditionOutput`] instead of
        a plain tuple.
    train (`bool`, *optional*, defaults to `False`):
        Use deterministic functions and disable dropout when not training.

Returns:
    [`~models.unets.unet_2d_condition_flax.FlaxUNet2DConditionOutput`] or `tuple`:
        [`~models.unets.unet_2d_condition_flax.FlaxUNet2DConditionOutput`] if `return_dict` is True, otherwise
        a `tuple`. When returning a tuple, the first element is the sample tensor.
bgrr2   )axisrr   r   )r   r	   r   r2   )deterministicr   )r   r   )rn   r   flipr   r   arrayrv   r@   shapeastyperX   expand_dimsr   r   	transposer>   r   r   r   r   zipr   r   r   )rF   r|   r}   r~   r   r   r   r   channel_ordert_embr   r   res_samples!controlnet_down_block_res_samplesdown_block_res_sampler   r   s                    r    rS   FlaxControlNetModel.__call__E  s   8 BBE!!hhQ?O )S[[11		9+SYY?I	3;;//C	4HA4M!((s{{(;I	15Iy)##E* v|4f%--F88I! #)**J*&>??&0@Uen&o#&0RWi&X#"1" + /DT]^ -/)7:;QSnSn7o3!$45J$K!-1II- 8p "C#88@ Mc!cLb&+="=Lb!c 22*,@AA##9Pd
 	
 "ds   3H)r   r   r   r>   r   r   r   r   rU   )g      ?TF)&r   r   r   r   r   r\   rV   r   r^   ra   rW   strrb   r   r*   rd   rf   rg   rh   ri   floatrj   r   rX   r+   rk   rl   rn   ro   rw   Arrayr   r   rL   r   r   rS   r   r   r   r    rZ   rZ   m   s   +Z KK)eCHo  5:$tSy!119*@c3h@c01eCHo-18<uS#X.5<##GU"'4'{{E399" OT J16)36;L'sCxLd		 dj dC
V %( Q
Q
 ;;&,Q
  #{{	Q

 Q
 "Q
 Q
 Q
 
eCKK,<&=s{{&J K	KQ
 Q
r   rZ   )"flax
flax.linenlinenr<   rw   	jax.numpynumpyr   flax.core.frozen_dictr   configuration_utilsr   r   utilsr   r   embeddings_flaxr
   r   modeling_flax_utilsr   unets.unet_2d_blocks_flaxr   r   r   
get_loggerr   r:   struct	dataclassr   Moduler"   rZ   r   r   r    <module>r      s      
  , G ( B 0  
		H	% 
&: 
& 
&:")) :z h
"))^[ h
 h
r   