
    +hKA                     B   S SK JrJrJr  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  SSKJrJr  SS	KJr  SS
KJrJrJr  \R8                  R:                   " S S\5      5       r " S S\R>                  5      r \ " S S\R>                  \\5      5       r!g)    )OptionalTupleUnionN)
FrozenDict   )ConfigMixinflax_register_to_config)
BaseOutput   )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/james-whalen/.local/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)#FlaxControlNetConditioningEmbedding/   conditioning_embedding_channels       `      .block_out_channelsdtypereturnNc           	         [         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 )	Nr   r   r      r3   r2   )kernel_sizepaddingr-   r3   )r   r   r4   stridesr5   r-   r4   r5   kernel_init	bias_initr-   )nnConvr,   r-   conv_inrangelenappendblocksr&   initializers
zeros_initconv_out)selfrA   i
channel_inchannel_outconv1conv2s          r"   setup)FlaxControlNetConditioningEmbedding.setup4   s   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;   silurA   rD   )rE   rM   	embeddingblocks       r"   __call__,FlaxControlNetConditioningEmbedding.__call__Z   sZ    LL.	GGI&	[[Ei(I	*I ! MM),	r!   )rA   r=   rD   r.   N)r   r   r   r   intr   r,   r   r   float32r-   rK   r   rR   r    r   r!   r"   r$   r$   /   sN    %((*;c3h;{{E399"$
L
S[[ 
S[[ 
r!   r$   c                      \ rS rSr% SrSr\\S'   Sr\\S'   Sr	\
\S4   \S	'   S
r\\\
\S4   4   \S'   Sr\
\S4   \S'   Sr\\S'   Sr\\\
\S4   4   \S'   Sr\\\\
\S4   4      \S'   Sr\\S'   Sr\\S'   S
r\\S'   \R2                  r\R4                  \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&\RH                  S'\\RH                  \\4   S(\RH                  S)\RH                  S*\S+\S,\S#\\%\
\
\RH                  S4   \RH                  4   4   4S- jjr&S.r'g)1FlaxControlNetModelg   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     ra   r,   r   layers_per_block   attention_head_dimNnum_attention_headsra   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   $ )Nr3   r-   )r3   r   rc   )paramsrg   rq   )r\   rZ   r   zerosrV   onesint32rf   jaxrandomsplitinit)rE   rn   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                 
   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[,        5      (       a  U4[)        U R*                  5      -  n/ n/ nUS   n[        R                  " USS
[        R.                  R1                  5       [        R.                  R1                  5       U R
                  S9nUR3                  U5        [5        U R*                  5       GH  u  pUnX   nU	[)        U5      S-
  :H  nU
S:X  aC  [7        UUU R8                  U R:                  X9   U(       + U R<                  XI   U R
                  S9	nO1[?        UUU R8                  U R:                  U(       + U R
                  S9nUR3                  U5        [A        U R:                  5       Ho  n[        R                  " USS
[        R.                  R1                  5       [        R.                  R1                  5       U R
                  S9nUR3                  U5        Mq     U(       a  GM:  [        R                  " USS
[        R.                  R1                  5       [        R.                  R1                  5       U R
                  S9nUR3                  U5        GM     XPl!        X`l"        US   n[G        UU R8                  US   U R<                  U R
                  S9U l$        [        R                  " USS
[        R.                  R1                  5       [        R.                  R1                  5       U R
                  S9U l%        g )Nr   r[   r0   r2   r1   r6   )ri   rj   rp   )r&   r,   VALIDr8   r3   r]   )	r\   out_channelsrg   
num_layersre   add_downsamplerh   r`   r-   )r\   r   rg   r   r   r-   )r\   rg   re   rh   r-   )&r,   re   rd   r;   r<   r-   r=   r   ri   configrj   	time_projr   time_embeddingr$   rm   controlnet_cond_embeddingr`   
isinstanceboolr?   r_   rU   rB   rC   r@   	enumerater   rg   rb   rh   r   r>   down_blockscontrolnet_down_blocksr   	mid_blockcontrolnet_mid_block)rE   r,   time_embed_dimre   r`   r   r   output_channelcontrolnet_blockrF   down_block_typeinput_channelis_final_block
down_block_mid_block_channels                   r"   rK   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!   rz   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.
bgrr3   )axisrp   r   )r   r   r   r3   )deterministicr   )r   r   )rl   r   flipr   r   arrayrt   r?   shapeastyperV   expand_dimsr   r   	transposer=   r   r   r   r   zipr   r   r   )rE   rz   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"   rR   FlaxControlNetModel.__call__:  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   rT   )g      ?TF)(r   r   r   r   r   rZ   rU   r   r\   r_   r   strr`   r   r   r,   rb   rd   re   r   rf   rg   floatrh   r   rV   r-   ri   rj   rl   rm   ru   Arrayr   r   rK   r   r   rR   r    r   r!   r"   rX   rX   g   s   +Z KK)eCHo  ;@%eD#I&6 67?*@c3h@c67c5c?237AE%U38_(<"=>E##GU"'4'{{E399" OT J16)36;L'sCxLd		 dj d~
L %( Q
Q
 eS01Q
  #{{	Q

 Q
 "Q
 Q
 Q
 
#U5c1A+BCKK+O%PP	QQ
 Q
r!   rX   )"typingr   r   r   flax
flax.linenlinenr;   ru   	jax.numpynumpyr   flax.core.frozen_dictr   configuration_utilsr   r	   utilsr
   embeddings_flaxr   r   modeling_flax_utilsr   unets.unet_2d_blocks_flaxr   r   r   struct	dataclassr   Moduler$   rX   r   r!   r"   <module>r      s    * )   
  , G  B 0  
&: 
& 
&5")) 5p c
"))^[ c
 c
r!   