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,
    FlaxCrossAttnUpBlockaD,
    FlaxDownBlockaD,
    FlaxUNetMidBlockaDCrossAttn,
    FlaxUpBlockaD,
)




@flax.struct.dataclass
class   lowercase_    (    __snake_case		):



	"""simple docstring"""





	UpperCAmelCase_    :   Optional[Any]  =							42




@flax_register_to_config
class   lowercase_    (    nn.Module ,	__snake_case ,	__snake_case		):



	"""simple docstring"""





	UpperCAmelCase_    :   str  =							32
	UpperCAmelCase_    :   Dict  =							4
	UpperCAmelCase_    :   Optional[int]  =							4
	UpperCAmelCase_    :   Tuple  =							(
	    """CrossAttnDownBlock2D""",
	    """CrossAttnDownBlock2D""",
	    """CrossAttnDownBlock2D""",
	    """DownBlock2D""",
	)
	UpperCAmelCase_    :   int  =							("""UpBlock2D""", """CrossAttnUpBlock2D""", """CrossAttnUpBlock2D""", """CrossAttnUpBlock2D""")
	UpperCAmelCase_    :   Optional[int]  =							False
	UpperCAmelCase_    :   Union[str, Any]  =							(320, 640, 1280, 1280)
	UpperCAmelCase_    :   Any  =							2
	UpperCAmelCase_    :   List[Any]  =							8
	UpperCAmelCase_    :   int  =							None
	UpperCAmelCase_    :   Union[str, Any]  =							1280
	UpperCAmelCase_    :   Union[str, Any]  =							0.0
	UpperCAmelCase_    :   Optional[int]  =							False
	UpperCAmelCase_    :   List[str]  =							jnp.floataa
	UpperCAmelCase_    :   Dict  =							True
	UpperCAmelCase_    :   Union[str, Any]  =							0
	UpperCAmelCase_    :   Tuple  =							False


	def      SCREAMING_SNAKE_CASE_							(				self						,     __SCREAMING_SNAKE_CASE							)		->FrozenDict:
								lowerCAmelCase      						=						(1, self.in_channels, self.sample_size, self.sample_size)
								lowerCAmelCase      						=						jnp.zeros(__lowerCamelCase						,     dtype=jnp.floataa							)
								lowerCAmelCase      						=						jnp.ones((1,)						,     dtype=jnp.intaa							)
								lowerCAmelCase      						=						jnp.zeros((1, 1, self.cross_attention_dim)						,     dtype=jnp.floataa							)

								lowerCAmelCase						, lowerCAmelCase      						=						jax.random.split(__lowerCamelCase							)
								lowerCAmelCase      						=						{'''params''': params_rng, '''dropout''': dropout_rng}

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


	def      SCREAMING_SNAKE_CASE_							(				self							)		->Tuple:
								lowerCAmelCase      						=						self.block_out_channels
								lowerCAmelCase      						=						block_out_channels[0] * 4

								if self.num_attention_heads is not None:
															raise ValueError(
															    '''At the moment it is not possible to define the number of attention heads via `num_attention_heads` because of a naming issue as described in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131. Passing `num_attention_heads` will only be supported in diffusers v0.19.'''							)

								# 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      						=						self.num_attention_heads or self.attention_head_dim

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

								# time
								lowerCAmelCase      						=						FlaxTimesteps(
								    block_out_channels[0]						,     flip_sin_to_cos=self.flip_sin_to_cos						,     freq_shift=self.config.freq_shift							)
								lowerCAmelCase      						=						FlaxTimestepEmbedding(__lowerCamelCase						,     dtype=self.dtype							)

								lowerCAmelCase      						=						self.only_cross_attention
								if isinstance(__lowerCamelCase						,     __lowerCamelCase							):
															lowerCAmelCase      						=						(only_cross_attention,) * len(self.down_block_types							)

								if isinstance(__lowerCamelCase						,     __lowerCamelCase							):
															lowerCAmelCase      						=						(num_attention_heads,) * len(self.down_block_types							)

								# down
								lowerCAmelCase      						=						[]
								lowerCAmelCase      						=						block_out_channels[0]
								for i, down_block_type in enumerate(self.down_block_types							):
															lowerCAmelCase      						=						output_channel
															lowerCAmelCase      						=						block_out_channels[i]
															lowerCAmelCase      						=						i == len(__lowerCamelCase							) - 1

															if down_block_type == "CrossAttnDownBlock2D":
																						lowerCAmelCase      						=						FlaxCrossAttnDownBlockaD(
																						    in_channels=__lowerCamelCase						,     out_channels=__lowerCamelCase						,     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]						,     use_memory_efficient_attention=self.use_memory_efficient_attention						,     dtype=self.dtype						,     )
															else:
																						lowerCAmelCase      						=						FlaxDownBlockaD(
																						    in_channels=__lowerCamelCase						,     out_channels=__lowerCamelCase						,     dropout=self.dropout						,     num_layers=self.layers_per_block						,     add_downsample=not is_final_block						,     dtype=self.dtype						,     )

															down_blocks.append(__lowerCamelCase							)
								lowerCAmelCase      						=						down_blocks

								# mid
								lowerCAmelCase      						=						FlaxUNetMidBlockaDCrossAttn(
								    in_channels=block_out_channels[-1]						,     dropout=self.dropout						,     num_attention_heads=num_attention_heads[-1]						,     use_linear_projection=self.use_linear_projection						,     use_memory_efficient_attention=self.use_memory_efficient_attention						,     dtype=self.dtype						,     )

								# up
								lowerCAmelCase      						=						[]
								lowerCAmelCase      						=						list(reversed(__lowerCamelCase							)							)
								lowerCAmelCase      						=						list(reversed(__lowerCamelCase							)							)
								lowerCAmelCase      						=						list(reversed(__lowerCamelCase							)							)
								lowerCAmelCase      						=						reversed_block_out_channels[0]
								for i, up_block_type in enumerate(self.up_block_types							):
															lowerCAmelCase      						=						output_channel
															lowerCAmelCase      						=						reversed_block_out_channels[i]
															lowerCAmelCase      						=						reversed_block_out_channels[min(i + 1						,     len(__lowerCamelCase							) - 1							)]

															lowerCAmelCase      						=						i == len(__lowerCamelCase							) - 1

															if up_block_type == "CrossAttnUpBlock2D":
																						lowerCAmelCase      						=						FlaxCrossAttnUpBlockaD(
																						    in_channels=__lowerCamelCase						,     out_channels=__lowerCamelCase						,     prev_output_channel=__lowerCamelCase						,     num_layers=self.layers_per_block + 1						,     num_attention_heads=reversed_num_attention_heads[i]						,     add_upsample=not is_final_block						,     dropout=self.dropout						,     use_linear_projection=self.use_linear_projection						,     only_cross_attention=only_cross_attention[i]						,     use_memory_efficient_attention=self.use_memory_efficient_attention						,     dtype=self.dtype						,     )
															else:
																						lowerCAmelCase      						=						FlaxUpBlockaD(
																						    in_channels=__lowerCamelCase						,     out_channels=__lowerCamelCase						,     prev_output_channel=__lowerCamelCase						,     num_layers=self.layers_per_block + 1						,     add_upsample=not is_final_block						,     dropout=self.dropout						,     dtype=self.dtype						,     )

															up_blocks.append(__lowerCamelCase							)
															lowerCAmelCase      						=						output_channel
								lowerCAmelCase      						=						up_blocks

								# out
								lowerCAmelCase      						=						nn.GroupNorm(num_groups=32						,     epsilon=1e-5							)
								lowerCAmelCase      						=						nn.Conv(
								    self.out_channels						,     kernel_size=(3, 3)						,     strides=(1, 1)						,     padding=((1, 1), (1, 1))						,     dtype=self.dtype						,     )




	def __call__(				self						,     __SCREAMING_SNAKE_CASE						,     __SCREAMING_SNAKE_CASE						,     __SCREAMING_SNAKE_CASE						,     __SCREAMING_SNAKE_CASE=None						,     __SCREAMING_SNAKE_CASE=None						,     __SCREAMING_SNAKE_CASE = True						,     __SCREAMING_SNAKE_CASE = False						,     )		->Union[FlaxUNetaDConditionOutput, Tuple]:







								if not isinstance(__lowerCamelCase						,     jnp.ndarray							):
															lowerCAmelCase      						=						jnp.array([timesteps]						,     dtype=jnp.intaa							)
								elif isinstance(__lowerCamelCase						,     jnp.ndarray							) and len(timesteps.shape							) == 0:
															lowerCAmelCase      						=						timesteps.astype(dtype=jnp.floataa							)
															lowerCAmelCase      						=						jnp.expand_dims(__lowerCamelCase						,     0							)

								lowerCAmelCase      						=						self.time_proj(__lowerCamelCase							)
								lowerCAmelCase      						=						self.time_embedding(__lowerCamelCase							)

								# 2. pre-process
								lowerCAmelCase      						=						jnp.transpose(__lowerCamelCase						,     (0, 2, 3, 1)							)
								lowerCAmelCase      						=						self.conv_in(__lowerCamelCase							)

								# 3. down
								lowerCAmelCase      						=						(sample,)
								for down_block in self.down_blocks:
															if isinstance(__lowerCamelCase						,     __lowerCamelCase							):
																						lowerCAmelCase						, lowerCAmelCase      						=						down_block(__lowerCamelCase						,     __lowerCamelCase						,     __lowerCamelCase						,     deterministic=not train							)
															else:
																						lowerCAmelCase						, lowerCAmelCase      						=						down_block(__lowerCamelCase						,     __lowerCamelCase						,     deterministic=not train							)
															down_block_res_samples += res_samples

								if down_block_additional_residuals is not None:
															lowerCAmelCase      						=						()

															for down_block_res_sample, down_block_additional_residual in zip(
															    __lowerCamelCase						,     __lowerCamelCase							):
																						down_block_res_sample += down_block_additional_residual
																						new_down_block_res_samples += (down_block_res_sample,)

															lowerCAmelCase      						=						new_down_block_res_samples

								# 4. mid
								lowerCAmelCase      						=						self.mid_block(__lowerCamelCase						,     __lowerCamelCase						,     __lowerCamelCase						,     deterministic=not train							)

								if mid_block_additional_residual is not None:
															sample += mid_block_additional_residual

								# 5. up
								for up_block in self.up_blocks:
															lowerCAmelCase      						=						down_block_res_samples[-(self.layers_per_block + 1) :]
															lowerCAmelCase      						=						down_block_res_samples[: -(self.layers_per_block + 1)]
															if isinstance(__lowerCamelCase						,     __lowerCamelCase							):
																						lowerCAmelCase      						=						up_block(
																						    __lowerCamelCase						,     temb=__lowerCamelCase						,     encoder_hidden_states=__lowerCamelCase						,     res_hidden_states_tuple=__lowerCamelCase						,     deterministic=not train						,     )
															else:
																						lowerCAmelCase      						=						up_block(__lowerCamelCase						,     temb=__lowerCamelCase						,     res_hidden_states_tuple=__lowerCamelCase						,     deterministic=not train							)

        # 6. post-process
								lowerCAmelCase      						=						self.conv_norm_out(__lowerCamelCase							)
								lowerCAmelCase      						=						nn.silu(__lowerCamelCase							)
								lowerCAmelCase      						=						self.conv_out(__lowerCamelCase							)
								lowerCAmelCase      						=						jnp.transpose(__lowerCamelCase						,     (0, 3, 1, 2)							)

								if not return_dict:
															return (sample,)

								return FlaxUNetaDConditionOutput(sample=__lowerCamelCase							)







from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__a	 =      logging.get_logger(__name__)

__a	 =      {
    """google/mobilenet_v1_1.0_224""": """https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json""",
    """google/mobilenet_v1_0.75_192""": """https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json""",
    # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1
}






class 	__lowercase					(	__snake_case	):
      UpperCamelCase										=							'''mobilenet_v1'''





      def __init__(		self    :       Any       ,							__lowerCamelCase    :       Union[str, Any]=3       ,							__lowerCamelCase    :       Dict=2_2_4       ,							__lowerCamelCase    :       Union[str, Any]=1.0       ,							__lowerCamelCase    :       Optional[int]=8       ,							__lowerCamelCase    :       Optional[int]="relu6"       ,							__lowerCamelCase    :       Optional[int]=True       ,							__lowerCamelCase    :       List[str]=0.999       ,							__lowerCamelCase    :       Optional[Any]=0.02       ,							__lowerCamelCase    :       str=0.001       ,							**__lowerCamelCase    :       Optional[Any]       ,							)     -> Optional[int]:






         """simple docstring"""







         super().__init__(**__lowerCamelCase							)

         if depth_multiplier <= 0:
            raise ValueError("""depth_multiplier must be greater than zero."""							)

         UpperCAmelCase      					=  num_channels
         UpperCAmelCase      					=  image_size
         UpperCAmelCase      					=  depth_multiplier
         UpperCAmelCase      					=  min_depth
         UpperCAmelCase      					=  hidden_act
         UpperCAmelCase      					=  tf_padding
         UpperCAmelCase      					=  classifier_dropout_prob
         UpperCAmelCase      					=  initializer_range
         UpperCAmelCase      					=  layer_norm_eps






class 	__lowercase					(	__snake_case	):
      UpperCamelCase										=							version.parse('''1.11'''	)





      @property
      def 							_lowercase					(		self    :       Optional[int]							)     -> Mapping[str, Mapping[int, str]]:






         """simple docstring"""







         return OrderedDict([("""pixel_values""", {0: """batch"""})]							)





      @property
      def 							_lowercase					(		self    :       Union[str, Any]							)     -> Mapping[str, Mapping[int, str]]:






         """simple docstring"""







         if self.task == "image-classification":
            return OrderedDict([("""logits""", {0: """batch"""})]							)
         else:
            return OrderedDict([("""last_hidden_state""", {0: """batch"""}), ("""pooler_output""", {0: """batch"""})]							)





      @property
      def 							_lowercase					(		self    :       List[Any]							)     -> float:






         """simple docstring"""







         return 1e-4



