

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        A__(a_							):
						"""simple docstring"""






						_A    :				jnp.ndarray
						_A    :				jnp.ndarray







class        A__(nn.Module							):
						"""simple docstring"""






						_A    :				int
						_A    :				Tuple[int]       					=			(16, 32, 96, 256)
						_A    :				jnp.dtype       					=			jnp.floataa





						def 			UpperCamelCase__					(    self    )     -> Any:
									a_						:						Any 							= nn.Conv(
									    self.block_out_channels[0]  ,      kernel_size=(3, 3)  ,      padding=((1, 1), (1, 1))  ,      dtype=self.dtype  ,      )

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

									a_						:						Any 							= 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  ,      _lowercase    )     -> Optional[Any]:
									a_						:						str 							= self.conv_in(_lowercase    )
									a_						:						Any 							= nn.silu(_lowercase    )

									for block in self.blocks:
												a_						:						Optional[int] 							= block(_lowercase    )
												a_						:						List[str] 							= nn.silu(_lowercase    )

									a_						:						Optional[Any] 							= self.conv_out(_lowercase    )

									return embedding







@flax_register_to_config
class        A__(nn.Module,  a_,  a_							):
						"""simple docstring"""






						_A    :				int       					=			32
						_A    :				int       					=			4
						_A    :				Tuple[str]       					=			(
						    "CrossAttnDownBlock2D",
						    "CrossAttnDownBlock2D",
						    "CrossAttnDownBlock2D",
						    "DownBlock2D",
						)
						_A    :				Union[bool, Tuple[bool]]       					=			False
						_A    :				Tuple[int]       					=			(320, 640, 1_280, 1_280)
						_A    :				int       					=			2
						_A    :				Union[int, Tuple[int]]       					=			8
						_A    :				Optional[Union[int, Tuple[int]]]       					=			None
						_A    :				int       					=			1_280
						_A    :				float       					=			0.0
						_A    :				bool       					=			False
						_A    :				jnp.dtype       					=			jnp.floataa
						_A    :				bool       					=			True
						_A    :				int       					=			0
						_A    :				str       					=			"rgb"
						_A    :				Tuple[int]       					=			(16, 32, 96, 256)





						def 			UpperCamelCase__					(    self  ,      _lowercase    )     -> FrozenDict:
									# init input tensors
									a_						:						Tuple 							= (1, self.in_channels, self.sample_size, self.sample_size)
									a_						:						Tuple 							= jnp.zeros(_lowercase  ,      dtype=jnp.floataa    )
									a_						:						Dict 							= jnp.ones((1,)  ,      dtype=jnp.intaa    )
									a_						:						Union[str, Any] 							= jnp.zeros((1, 1, self.cross_attention_dim)  ,      dtype=jnp.floataa    )
									a_						:						List[Any] 							= (1, 3, self.sample_size * 8, self.sample_size * 8)
									a_						:						int 							= jnp.zeros(_lowercase  ,      dtype=jnp.floataa    )

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

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





						def 			UpperCamelCase__					(    self    )     -> List[str]:
									a_						:						Dict 							= self.block_out_channels
									a_						:						Any 							= 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.
									a_						:						List[Any] 							= self.num_attention_heads or self.attention_head_dim

									# input
									a_						:						Union[str, Any] 							= nn.Conv(
									    block_out_channels[0]  ,      kernel_size=(3, 3)  ,      strides=(1, 1)  ,      padding=((1, 1), (1, 1))  ,      dtype=self.dtype  ,      )

									# time
									a_						:						int 							= FlaxTimesteps(
									    block_out_channels[0]  ,      flip_sin_to_cos=self.flip_sin_to_cos  ,      freq_shift=self.config.freq_shift    )
									a_						:						List[Any] 							= FlaxTimestepEmbedding(_lowercase  ,      dtype=self.dtype    )

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

									a_						:						Any 							= self.only_cross_attention
									if isinstance(_lowercase  ,      _lowercase    ):
												a_						:						List[str] 							= (only_cross_attention,) * len(self.down_block_types    )

									if isinstance(_lowercase  ,      _lowercase    ):
												a_						:						Any 							= (num_attention_heads,) * len(self.down_block_types    )

									# down
									a_						:						List[str] 							= []
									a_						:						int 							= []

									a_						:						Union[str, Any] 							= block_out_channels[0]

									a_						:						str 							= nn.Conv(
									    _lowercase  ,      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(_lowercase    )

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

												if down_block_type == "CrossAttnDownBlock2D":
															a_						:						Optional[int] 							= FlaxCrossAttnDownBlockaD(
															    in_channels=_lowercase  ,      out_channels=_lowercase  ,      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:
															a_						:						Any 							= FlaxDownBlockaD(
															    in_channels=_lowercase  ,      out_channels=_lowercase  ,      dropout=self.dropout  ,      num_layers=self.layers_per_block  ,      add_downsample=not is_final_block  ,      dtype=self.dtype  ,      )

												down_blocks.append(_lowercase    )

												for _ in range(self.layers_per_block    ):
															a_						:						Optional[int] 							= nn.Conv(
															    _lowercase  ,      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(_lowercase    )

												if not is_final_block:
															a_						:						List[str] 							= nn.Conv(
															    _lowercase  ,      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(_lowercase    )

									a_						:						List[str] 							= down_blocks
									a_						:						Union[str, Any] 							= controlnet_down_blocks

									# mid
									a_						:						Tuple 							= block_out_channels[-1]
									a_						:						Optional[int] 							= FlaxUNetMidBlockaDCrossAttn(
									    in_channels=_lowercase  ,      dropout=self.dropout  ,      num_attention_heads=num_attention_heads[-1]  ,      use_linear_projection=self.use_linear_projection  ,      dtype=self.dtype  ,      )

									a_						:						int 							= nn.Conv(
									    _lowercase  ,      kernel_size=(1, 1)  ,      padding="""VALID"""  ,      kernel_init=nn.initializers.zeros_init()  ,      bias_init=nn.initializers.zeros_init()  ,      dtype=self.dtype  ,      )





						def __call__(    self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase = 1.0  ,      _lowercase = True  ,      _lowercase = False  ,      )     -> Union[FlaxControlNetOutput, Tuple]:
									a_						:						Dict 							= self.controlnet_conditioning_channel_order
									if channel_order == "bgr":
												a_						:						str 							= jnp.flip(_lowercase  ,      axis=1    )

									# 1. time
									if not isinstance(_lowercase  ,      jnp.ndarray    ):
												a_						:						Tuple 							= jnp.array([timesteps]  ,      dtype=jnp.intaa    )
									elif isinstance(_lowercase  ,      jnp.ndarray    ) and len(timesteps.shape    ) == 0:
												a_						:						str 							= timesteps.astype(dtype=jnp.floataa    )
												a_						:						Optional[Any] 							= jnp.expand_dims(_lowercase  ,      0    )

									a_						:						Tuple 							= self.time_proj(_lowercase    )
									a_						:						Dict 							= self.time_embedding(_lowercase    )

									# 2. pre-process
									a_						:						str 							= jnp.transpose(_lowercase  ,      (0, 2, 3, 1)    )
									a_						:						List[Any] 							= self.conv_in(_lowercase    )

									a_						:						Optional[int] 							= jnp.transpose(_lowercase  ,      (0, 2, 3, 1)    )
									a_						:						Dict 							= self.controlnet_cond_embedding(_lowercase    )
									sample += controlnet_cond

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

									# 4. mid
									a_						:						Optional[Any] 							= self.mid_block(_lowercase  ,      _lowercase  ,      _lowercase  ,      deterministic=not train    )

									# 5. contronet blocks
									a_						:						Optional[Any] 							= ()
									for down_block_res_sample, controlnet_block in zip(_lowercase  ,      self.controlnet_down_blocks    ):
												a_						:						int 							= controlnet_block(_lowercase    )
												controlnet_down_block_res_samples += (down_block_res_sample,)

									a_						:						Tuple 							= controlnet_down_block_res_samples

									a_						:						Union[str, Any] 							= self.controlnet_mid_block(_lowercase    )

									# 6. scaling
									a_						:						str 							= [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=_lowercase  ,      mid_block_res_sample=_lowercase    )





from ...configuration_utils import PretrainedConfig
from ...utils import logging


__snake_case				:      Any   		=			logging.get_logger(__name__)

__snake_case				:      Union[str, Any]   		=			{
    """microsoft/cvt-13""": """https://huggingface.co/microsoft/cvt-13/resolve/main/config.json""",
    # See all Cvt models at https://huggingface.co/models?filter=cvt
}



class        A__(a_							):
						"""simple docstring"""






						_A    :				Union[str, Any]       					=			'''cvt'''





						def __init__(    self  ,      _lowercase=3  ,      _lowercase=[7, 3, 3]  ,      _lowercase=[4, 2, 2]  ,      _lowercase=[2, 1, 1]  ,      _lowercase=[64, 192, 384]  ,      _lowercase=[1, 3, 6]  ,      _lowercase=[1, 2, 10]  ,      _lowercase=[4.0, 4.0, 4.0]  ,      _lowercase=[0.0, 0.0, 0.0]  ,      _lowercase=[0.0, 0.0, 0.0]  ,      _lowercase=[0.0, 0.0, 0.1]  ,      _lowercase=[True, True, True]  ,      _lowercase=[False, False, True]  ,      _lowercase=["dw_bn", "dw_bn", "dw_bn"]  ,      _lowercase=[3, 3, 3]  ,      _lowercase=[1, 1, 1]  ,      _lowercase=[2, 2, 2]  ,      _lowercase=[1, 1, 1]  ,      _lowercase=[1, 1, 1]  ,      _lowercase=0.0_2  ,      _lowercase=1e-12  ,      **_lowercase  ,      )     -> Tuple:
									super().__init__(**_lowercase    )
									a_						:						str 							= num_channels
									a_						:						Optional[int] 							= patch_sizes
									a_						:						Dict 							= patch_stride
									a_						:						int 							= patch_padding
									a_						:						int 							= embed_dim
									a_						:						Tuple 							= num_heads
									a_						:						Optional[Any] 							= depth
									a_						:						str 							= mlp_ratio
									a_						:						Optional[int] 							= attention_drop_rate
									a_						:						int 							= drop_rate
									a_						:						List[Any] 							= drop_path_rate
									a_						:						List[Any] 							= qkv_bias
									a_						:						Dict 							= cls_token
									a_						:						Union[str, Any] 							= qkv_projection_method
									a_						:						List[Any] 							= kernel_qkv
									a_						:						Optional[Any] 							= padding_kv
									a_						:						Any 							= stride_kv
									a_						:						Dict 							= padding_q
									a_						:						Optional[int] 							= stride_q
									a_						:						Tuple 							= initializer_range
									a_						:						int 							= layer_norm_eps


