
"""simple docstring"""






from typing import Optional, Tuple, Union

import flax
import flax.linen as nn
import jax
import jax.numpy as jnp
from flax.core.frozen_dict import FrozenDict

from ..configuration_utils import ConfigMixin, flax_register_to_config
from ..utils import BaseOutput
from .embeddings_flax import FlaxTimestepEmbedding, FlaxTimesteps
from .modeling_flax_utils import FlaxModelMixin
from .unet_ad_blocks_flax import (
    FlaxCrossAttnDownBlockaD,
    FlaxDownBlockaD,
    FlaxUNetMidBlockaDCrossAttn,
)




@flax.struct.dataclass
class 			_SCREAMING_SNAKE_CASE					(			A__							):
			UpperCAmelCase_  :jnp.ndarray
			UpperCAmelCase_  :jnp.ndarray




class 			_SCREAMING_SNAKE_CASE					(			nn.Module							):
			UpperCAmelCase_  :int
			UpperCAmelCase_  :Tuple[int]				 =						(16, 32, 96, 256)
			UpperCAmelCase_  :jnp.dtype				 =						jnp.floataa






			def  __lowerCAmelCase (					self       )			->     Dict:
						lowerCAmelCase_    :Optional[int]			     =  nn.Conv(
						    self.block_out_channels[0]       ,   kernel_size=(3, 3)       ,   padding=((1, 1), (1, 1))       ,   dtype=self.dtype       ,   )

						lowerCAmelCase_    :int			     =  []
						for i in range(len(self.block_out_channels       ) - 1       ):
									lowerCAmelCase_    :Union[str, Any]			     =  self.block_out_channels[i]
									lowerCAmelCase_    :Optional[int]			     =  self.block_out_channels[i + 1]
									lowerCAmelCase_    :int			     =  nn.Conv(
									    __A       ,   kernel_size=(3, 3)       ,   padding=((1, 1), (1, 1))       ,   dtype=self.dtype       ,   )
									blocks.append(__A       )
									lowerCAmelCase_    :List[str]			     =  nn.Conv(
									    __A       ,   kernel_size=(3, 3)       ,   strides=(2, 2)       ,   padding=((1, 1), (1, 1))       ,   dtype=self.dtype       ,   )
									blocks.append(__A       )
						lowerCAmelCase_    :Optional[int]			     =  blocks

						lowerCAmelCase_    :int			     =  nn.Conv(
						    self.conditioning_embedding_channels       ,   kernel_size=(3, 3)       ,   padding=((1, 1), (1, 1))       ,   kernel_init=nn.initializers.zeros_init()       ,   bias_init=nn.initializers.zeros_init()       ,   dtype=self.dtype       ,   )






			def __call__(					self       ,   __A       )			->     Tuple:
						lowerCAmelCase_    :Dict			     =  self.conv_in(__A       )
						lowerCAmelCase_    :List[str]			     =  nn.silu(__A       )

						for block in self.blocks:
									lowerCAmelCase_    :Any			     =  block(__A       )
									lowerCAmelCase_    :Optional[int]			     =  nn.silu(__A       )

						lowerCAmelCase_    :List[Any]			     =  self.conv_out(__A       )

						return embedding




@flax_register_to_config
class 			_SCREAMING_SNAKE_CASE					(			nn.Module	, A__	, A__							):
			UpperCAmelCase_  :int				 =						32
			UpperCAmelCase_  :int				 =						4
			UpperCAmelCase_  :Tuple[str]				 =						(
			    "CrossAttnDownBlock2D",
			    "CrossAttnDownBlock2D",
			    "CrossAttnDownBlock2D",
			    "DownBlock2D",
			)
			UpperCAmelCase_  :Union[bool, Tuple[bool]]				 =						False
			UpperCAmelCase_  :Tuple[int]				 =						(320, 640, 1280, 1280)
			UpperCAmelCase_  :int				 =						2
			UpperCAmelCase_  :Union[int, Tuple[int]]				 =						8
			UpperCAmelCase_  :Optional[Union[int, Tuple[int]]]				 =						None
			UpperCAmelCase_  :int				 =						1280
			UpperCAmelCase_  :float				 =						0.0
			UpperCAmelCase_  :bool				 =						False
			UpperCAmelCase_  :jnp.dtype				 =						jnp.floataa
			UpperCAmelCase_  :bool				 =						True
			UpperCAmelCase_  :int				 =						0
			UpperCAmelCase_  :str				 =						"rgb"
			UpperCAmelCase_  :Tuple[int]				 =						(16, 32, 96, 256)






			def  __lowerCAmelCase (					self       ,   __A       )			->     FrozenDict:
						# init input tensors
						lowerCAmelCase_    :Optional[int]			     =  (1, self.in_channels, self.sample_size, self.sample_size)
						lowerCAmelCase_    :Dict			     =  jnp.zeros(__A       ,   dtype=jnp.floataa       )
						lowerCAmelCase_    :List[Any]			     =  jnp.ones((1,)       ,   dtype=jnp.intaa       )
						lowerCAmelCase_    :Optional[int]			     =  jnp.zeros((1, 1, self.cross_attention_dim)       ,   dtype=jnp.floataa       )
						lowerCAmelCase_    :Any			     =  (1, 3, self.sample_size * 8, self.sample_size * 8)
						lowerCAmelCase_    :Optional[int]			     =  jnp.zeros(__A       ,   dtype=jnp.floataa       )

						lowerCAmelCase_     ,       lowerCAmelCase_    :Optional[int]			     =  jax.random.split(__A       )
						lowerCAmelCase_    :Optional[int]			     =  {"""params""": params_rng, """dropout""": dropout_rng}

						return self.init(__A       ,   __A       ,   __A       ,   __A       ,   __A       )["params"]






			def  __lowerCAmelCase (					self       )			->     List[str]:
						lowerCAmelCase_    :Union[str, Any]			     =  self.block_out_channels
						lowerCAmelCase_    :int			     =  block_out_channels[0] * 4

						# If `num_attention_heads` is not defined (which is the case for most models)
						# it will default to `attention_head_dim`. This looks weird upon first reading it and it is.
						# The reason for this behavior is to correct for incorrectly named variables that were introduced
						# when this library was created. The incorrect naming was only discovered much later in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131
						# Changing `attention_head_dim` to `num_attention_heads` for 40,000+ configurations is too backwards breaking
						# which is why we correct for the naming here.
						lowerCAmelCase_    :Dict			     =  self.num_attention_heads or self.attention_head_dim

						# input
						lowerCAmelCase_    :int			     =  nn.Conv(
						    block_out_channels[0]       ,   kernel_size=(3, 3)       ,   strides=(1, 1)       ,   padding=((1, 1), (1, 1))       ,   dtype=self.dtype       ,   )

						# time
						lowerCAmelCase_    :Optional[Any]			     =  FlaxTimesteps(
						    block_out_channels[0]       ,   flip_sin_to_cos=self.flip_sin_to_cos       ,   freq_shift=self.config.freq_shift       )
						lowerCAmelCase_    :Optional[Any]			     =  FlaxTimestepEmbedding(__A       ,   dtype=self.dtype       )

						lowerCAmelCase_    :int			     =  FlaxControlNetConditioningEmbedding(
						    conditioning_embedding_channels=block_out_channels[0]       ,   block_out_channels=self.conditioning_embedding_out_channels       ,   )

						lowerCAmelCase_    :List[str]			     =  self.only_cross_attention
						if isinstance(__A       ,   __A       ):
									lowerCAmelCase_    :List[str]			     =  (only_cross_attention,) * len(self.down_block_types       )

						if isinstance(__A       ,   __A       ):
									lowerCAmelCase_    :Optional[Any]			     =  (num_attention_heads,) * len(self.down_block_types       )

						# down
						lowerCAmelCase_    :Dict			     =  []
						lowerCAmelCase_    :Optional[Any]			     =  []

						lowerCAmelCase_    :Dict			     =  block_out_channels[0]

						lowerCAmelCase_    :List[Any]			     =  nn.Conv(
						    __A       ,   kernel_size=(1, 1)       ,   padding="""VALID"""       ,   kernel_init=nn.initializers.zeros_init()       ,   bias_init=nn.initializers.zeros_init()       ,   dtype=self.dtype       ,   )
						controlnet_down_blocks.append(__A       )

						for i, down_block_type in enumerate(self.down_block_types       ):
									lowerCAmelCase_    :List[Any]			     =  output_channel
									lowerCAmelCase_    :List[str]			     =  block_out_channels[i]
									lowerCAmelCase_    :Tuple			     =  i == len(__A       ) - 1

									if down_block_type == "CrossAttnDownBlock2D":
												lowerCAmelCase_    :Tuple			     =  FlaxCrossAttnDownBlockaD(
												    in_channels=__A       ,   out_channels=__A       ,   dropout=self.dropout       ,   num_layers=self.layers_per_block       ,   num_attention_heads=num_attention_heads[i]       ,   add_downsample=not is_final_block       ,   use_linear_projection=self.use_linear_projection       ,   only_cross_attention=only_cross_attention[i]       ,   dtype=self.dtype       ,   )
									else:
												lowerCAmelCase_    :Optional[int]			     =  FlaxDownBlockaD(
												    in_channels=__A       ,   out_channels=__A       ,   dropout=self.dropout       ,   num_layers=self.layers_per_block       ,   add_downsample=not is_final_block       ,   dtype=self.dtype       ,   )

									down_blocks.append(__A       )

									for _ in range(self.layers_per_block       ):
												lowerCAmelCase_    :List[str]			     =  nn.Conv(
												    __A       ,   kernel_size=(1, 1)       ,   padding="""VALID"""       ,   kernel_init=nn.initializers.zeros_init()       ,   bias_init=nn.initializers.zeros_init()       ,   dtype=self.dtype       ,   )
												controlnet_down_blocks.append(__A       )

									if not is_final_block:
												lowerCAmelCase_    :str			     =  nn.Conv(
												    __A       ,   kernel_size=(1, 1)       ,   padding="""VALID"""       ,   kernel_init=nn.initializers.zeros_init()       ,   bias_init=nn.initializers.zeros_init()       ,   dtype=self.dtype       ,   )
												controlnet_down_blocks.append(__A       )

						lowerCAmelCase_    :List[Any]			     =  down_blocks
						lowerCAmelCase_    :Optional[Any]			     =  controlnet_down_blocks

						# mid
						lowerCAmelCase_    :int			     =  block_out_channels[-1]
						lowerCAmelCase_    :List[Any]			     =  FlaxUNetMidBlockaDCrossAttn(
						    in_channels=__A       ,   dropout=self.dropout       ,   num_attention_heads=num_attention_heads[-1]       ,   use_linear_projection=self.use_linear_projection       ,   dtype=self.dtype       ,   )

						lowerCAmelCase_    :Dict			     =  nn.Conv(
						    __A       ,   kernel_size=(1, 1)       ,   padding="""VALID"""       ,   kernel_init=nn.initializers.zeros_init()       ,   bias_init=nn.initializers.zeros_init()       ,   dtype=self.dtype       ,   )






			def __call__(					self       ,   __A       ,   __A       ,   __A       ,   __A       ,   __A = 1.0       ,   __A = True       ,   __A = False       ,   )			->     Union[FlaxControlNetOutput, Tuple]:
						lowerCAmelCase_    :Union[str, Any]			     =  self.controlnet_conditioning_channel_order
						if channel_order == "bgr":
									lowerCAmelCase_    :Optional[int]			     =  jnp.flip(__A       ,   axis=1       )

						# 1. time
						if not isinstance(__A       ,   jnp.ndarray       ):
									lowerCAmelCase_    :List[str]			     =  jnp.array([timesteps]       ,   dtype=jnp.intaa       )
						elif isinstance(__A       ,   jnp.ndarray       ) and len(timesteps.shape       ) == 0:
									lowerCAmelCase_    :str			     =  timesteps.astype(dtype=jnp.floataa       )
									lowerCAmelCase_    :Union[str, Any]			     =  jnp.expand_dims(__A       ,   0       )

						lowerCAmelCase_    :List[Any]			     =  self.time_proj(__A       )
						lowerCAmelCase_    :Optional[Any]			     =  self.time_embedding(__A       )

						# 2. pre-process
						lowerCAmelCase_    :int			     =  jnp.transpose(__A       ,   (0, 2, 3, 1)       )
						lowerCAmelCase_    :List[Any]			     =  self.conv_in(__A       )

						lowerCAmelCase_    :Union[str, Any]			     =  jnp.transpose(__A       ,   (0, 2, 3, 1)       )
						lowerCAmelCase_    :List[str]			     =  self.controlnet_cond_embedding(__A       )
						sample += controlnet_cond

						# 3. down
						lowerCAmelCase_    :Any			     =  (sample,)
						for down_block in self.down_blocks:
									if isinstance(__A       ,   __A       ):
												lowerCAmelCase_     ,       lowerCAmelCase_    :Any			     =  down_block(__A       ,   __A       ,   __A       ,   deterministic=not train       )
									else:
												lowerCAmelCase_     ,       lowerCAmelCase_    :Union[str, Any]			     =  down_block(__A       ,   __A       ,   deterministic=not train       )
									down_block_res_samples += res_samples

						# 4. mid
						lowerCAmelCase_    :int			     =  self.mid_block(__A       ,   __A       ,   __A       ,   deterministic=not train       )

						# 5. contronet blocks
						lowerCAmelCase_    :Dict			     =  ()
						for down_block_res_sample, controlnet_block in zip(__A       ,   self.controlnet_down_blocks       ):
									lowerCAmelCase_    :Union[str, Any]			     =  controlnet_block(__A       )
									controlnet_down_block_res_samples += (down_block_res_sample,)

						lowerCAmelCase_    :Optional[Any]			     =  controlnet_down_block_res_samples

						lowerCAmelCase_    :List[Any]			     =  self.controlnet_mid_block(__A       )

						# 6. scaling
						lowerCAmelCase_    :List[Any]			     =  [sample * conditioning_scale for sample in down_block_res_samples]
						mid_block_res_sample *= conditioning_scale

						if not return_dict:
									return (down_block_res_samples, mid_block_res_sample)

						return FlaxControlNetOutput(
						    down_block_res_samples=__A       ,   mid_block_res_sample=__A       )









'''simple docstring'''
from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


lowerCamelCase :      List[str]					 =     logging.get_logger(__name__)

lowerCamelCase :      List[Any]					 =     {
    "facebook/convnextv2-tiny-1k-224": "https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json",
}







class 							A__    (       A__     ,   A__  ):
  A__		 =				'convnextv2'





  def __init__(		self      :	Tuple							,					_a      :	Optional[int]=3							,					_a      :	Any=4							,					_a      :	int=4							,					_a      :	Union[str, Any]=None							,					_a      :	List[str]=None							,					_a      :	Optional[Any]="gelu"							,					_a      :	Any=0.02							,					_a      :	Any=1e-12							,					_a      :	Tuple=0.0							,					_a      :	int=224							,					_a      :	Any=None							,					_a      :	Optional[int]=None							,					**_a      :	List[str]							,					)	->    Optional[Any]:





       '''simple docstring'''






       super().__init__(**_a )

       _SCREAMING_SNAKE_CASE							    =num_channels
       _SCREAMING_SNAKE_CASE							    =patch_size
       _SCREAMING_SNAKE_CASE							    =num_stages
       _SCREAMING_SNAKE_CASE							    =[96, 192, 384, 768] if hidden_sizes is None else hidden_sizes
       _SCREAMING_SNAKE_CASE							    =[3, 3, 9, 3] if depths is None else depths
       _SCREAMING_SNAKE_CASE							    =hidden_act
       _SCREAMING_SNAKE_CASE							    =initializer_range
       _SCREAMING_SNAKE_CASE							    =layer_norm_eps
       _SCREAMING_SNAKE_CASE							    =drop_path_rate
       _SCREAMING_SNAKE_CASE							    =image_size
       _SCREAMING_SNAKE_CASE							    =['stem'] + [f"stage{idx}" for idx in range(1							,					len(self.depths ) + 1 )]
       _SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE							    =get_aligned_output_features_output_indices(
           out_features=_a							,					out_indices=_a							,					stage_names=self.stage_names )
