


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 					__magic_name__		(   lowercase__):
     A:						jnp.ndarray
     A:						jnp.ndarray



class 					__magic_name__		(   nn.Module):
     A:						int
     A:						Tuple[int]								=					(1_6, 3_2, 9_6, 2_5_6)
     A:						jnp.dtype								=					jnp.floataa


     def    UpperCAmelCase__		(					self							:   Optional[int]	)     ->					Dict:





      '''simple docstring'''






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

      UpperCamelCase__    :			Dict      				=      []
      for i in range(len(self.block_out_channels	) - 1	):
       UpperCamelCase__    :			Dict      				=      self.block_out_channels[i]
       UpperCamelCase__    :			Any      				=      self.block_out_channels[i + 1]
       UpperCamelCase__    :			Dict      				=      nn.Conv(
           __lowercase							,		kernel_size=(3, 3)							,		padding=((1, 1), (1, 1))							,		dtype=self.dtype							,		)
       blocks.append(__lowercase	)
       UpperCamelCase__    :			List[Any]      				=      nn.Conv(
           __lowercase							,		kernel_size=(3, 3)							,		strides=(2, 2)							,		padding=((1, 1), (1, 1))							,		dtype=self.dtype							,		)
       blocks.append(__lowercase	)
      UpperCamelCase__    :			int      				=      blocks

      UpperCamelCase__    :			Optional[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							:   Union[str, Any]							,		lowerCamelCase__							:   List[Any]	)     ->					Union[str, Any]:





      '''simple docstring'''






      UpperCamelCase__    :			List[Any]      				=      self.conv_in(__lowercase	)
      UpperCamelCase__    :			int      				=      nn.silu(__lowercase	)

      for block in self.blocks:
       UpperCamelCase__    :			int      				=      block(__lowercase	)
       UpperCamelCase__    :			Union[str, Any]      				=      nn.silu(__lowercase	)

      UpperCamelCase__    :			str      				=      self.conv_out(__lowercase	)

      return embedding



@flax_register_to_config
class 					__magic_name__		(   nn.Module    , lowercase__    , lowercase__):
     A:						int								=					3_2
     A:						int								=					4
     A:						Tuple[str]								=					(
         "CrossAttnDownBlock2D",
         "CrossAttnDownBlock2D",
         "CrossAttnDownBlock2D",
         "DownBlock2D",
     )
     A:						Union[bool, Tuple[bool]]								=					False
     A:						Tuple[int]								=					(3_2_0, 6_4_0, 1_2_8_0, 1_2_8_0)
     A:						int								=					2
     A:						Union[int, Tuple[int]]								=					8
     A:						Optional[Union[int, Tuple[int]]]								=					None
     A:						int								=					1_2_8_0
     A:						float								=					0.0
     A:						bool								=					False
     A:						jnp.dtype								=					jnp.floataa
     A:						bool								=					True
     A:						int								=					0
     A:						str								=					"rgb"
     A:						Tuple[int]								=					(1_6, 3_2, 9_6, 2_5_6)


     def    UpperCAmelCase__		(					self							:   Tuple							,		lowerCamelCase__							:   jax.random.KeyArray	)     ->					FrozenDict:





      '''simple docstring'''






      UpperCamelCase__    :			Any      				=      (1, self.in_channels, self.sample_size, self.sample_size)
      UpperCamelCase__    :			Dict      				=      jnp.zeros(__lowercase							,		dtype=jnp.floataa	)
      UpperCamelCase__    :			str      				=      jnp.ones((1,)							,		dtype=jnp.intaa	)
      UpperCamelCase__    :			Optional[int]      				=      jnp.zeros((1, 1, self.cross_attention_dim)							,		dtype=jnp.floataa	)
      UpperCamelCase__    :			int      				=      (1, 3, self.sample_size * 8, self.sample_size * 8)
      UpperCamelCase__    :			int      				=      jnp.zeros(__lowercase							,		dtype=jnp.floataa	)

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

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


     def    UpperCAmelCase__		(					self							:   Union[str, Any]	)     ->					List[Any]:





      '''simple docstring'''






      UpperCamelCase__    :			Union[str, Any]      				=      self.block_out_channels
      UpperCamelCase__    :			str      				=      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.
      UpperCamelCase__    :			Optional[int]      				=      self.num_attention_heads or self.attention_head_dim

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

      # time
      UpperCamelCase__    :			Union[str, Any]      				=      FlaxTimesteps(
          block_out_channels[0]							,		flip_sin_to_cos=self.flip_sin_to_cos							,		freq_shift=self.config.freq_shift	)
      UpperCamelCase__    :			Any      				=      FlaxTimestepEmbedding(__lowercase							,		dtype=self.dtype	)

      UpperCamelCase__    :			str      				=      FlaxControlNetConditioningEmbedding(
          conditioning_embedding_channels=block_out_channels[0]							,		block_out_channels=self.conditioning_embedding_out_channels							,		)

      UpperCamelCase__    :			Union[str, Any]      				=      self.only_cross_attention
      if isinstance(__lowercase							,		__lowercase	):
       UpperCamelCase__    :			List[Any]      				=      (only_cross_attention,) * len(self.down_block_types	)

      if isinstance(__lowercase							,		__lowercase	):
       UpperCamelCase__    :			Optional[int]      				=      (num_attention_heads,) * len(self.down_block_types	)

      # down
      UpperCamelCase__    :			Dict      				=      []
      UpperCamelCase__    :			List[Any]      				=      []

      UpperCamelCase__    :			str      				=      block_out_channels[0]

      UpperCamelCase__    :			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	)

      for i, down_block_type in enumerate(self.down_block_types	):
       UpperCamelCase__    :			Tuple      				=      output_channel
       UpperCamelCase__    :			Union[str, Any]      				=      block_out_channels[i]
       UpperCamelCase__    :			int      				=      i == len(__lowercase	) - 1

       if down_block_type == "CrossAttnDownBlock2D":
        UpperCamelCase__    :			Union[str, Any]      				=      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:
        UpperCamelCase__    :			Dict      				=      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	):
        UpperCamelCase__    :			Dict      				=      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:
        UpperCamelCase__    :			Any      				=      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	)

      UpperCamelCase__    :			int      				=      down_blocks
      UpperCamelCase__    :			Union[str, Any]      				=      controlnet_down_blocks

      # mid
      UpperCamelCase__    :			Any      				=      block_out_channels[-1]
      UpperCamelCase__    :			Optional[Any]      				=      FlaxUNetMidBlockaDCrossAttn(
          in_channels=__lowercase							,		dropout=self.dropout							,		num_attention_heads=num_attention_heads[-1]							,		use_linear_projection=self.use_linear_projection							,		dtype=self.dtype							,		)

      UpperCamelCase__    :			List[Any]      				=      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							:   List[Any]							,		lowerCamelCase__							:   Union[str, Any]							,		lowerCamelCase__							:   int							,		lowerCamelCase__							:   List[str]							,		lowerCamelCase__							:   Any							,		lowerCamelCase__							:   float = 1.0							,		lowerCamelCase__							:   bool = True							,		lowerCamelCase__							:   bool = False							,		)     ->					Union[FlaxControlNetOutput, Tuple]:





      '''simple docstring'''






      UpperCamelCase__    :			Union[str, Any]      				=      self.controlnet_conditioning_channel_order
      if channel_order == "bgr":
       UpperCamelCase__    :			List[Any]      				=      jnp.flip(__lowercase							,		axis=1	)

      # 1. time
      if not isinstance(__lowercase							,		jnp.ndarray	):
       UpperCamelCase__    :			List[Any]      				=      jnp.array([timesteps]							,		dtype=jnp.intaa	)
      elif isinstance(__lowercase							,		jnp.ndarray	) and len(timesteps.shape	) == 0:
       UpperCamelCase__    :			int      				=      timesteps.astype(dtype=jnp.floataa	)
       UpperCamelCase__    :			Tuple      				=      jnp.expand_dims(__lowercase							,		0	)

      UpperCamelCase__    :			Any      				=      self.time_proj(__lowercase	)
      UpperCamelCase__    :			Optional[Any]      				=      self.time_embedding(__lowercase	)

      # 2. pre-process
      UpperCamelCase__    :			int      				=      jnp.transpose(__lowercase							,		(0, 2, 3, 1)	)
      UpperCamelCase__    :			Union[str, Any]      				=      self.conv_in(__lowercase	)

      UpperCamelCase__    :			Tuple      				=      jnp.transpose(__lowercase							,		(0, 2, 3, 1)	)
      UpperCamelCase__    :			Union[str, Any]      				=      self.controlnet_cond_embedding(__lowercase	)
      sample += controlnet_cond

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

      # 4. mid
      UpperCamelCase__    :			Tuple      				=      self.mid_block(__lowercase							,		__lowercase							,		__lowercase							,		deterministic=not train	)

      # 5. contronet blocks
      UpperCamelCase__    :			str      				=      ()
      for down_block_res_sample, controlnet_block in zip(__lowercase							,		self.controlnet_down_blocks	):
       UpperCamelCase__    :			Dict      				=      controlnet_block(__lowercase	)
       controlnet_down_block_res_samples += (down_block_res_sample,)

      UpperCamelCase__    :			Dict      				=      controlnet_down_block_res_samples

      UpperCamelCase__    :			Optional[int]      				=      self.controlnet_mid_block(__lowercase	)

      # 6. scaling
      UpperCamelCase__    :			List[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	)










import warnings
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		:       List[Any]			     = logging.get_logger(__name__)

a		:       List[Any]			     = {
    "nvidia/segformer-b0-finetuned-ade-512-512": (
        "https://huggingface.co/nvidia/segformer-b0-finetuned-ade-512-512/resolve/main/config.json"
    ),
    # See all SegFormer models at https://huggingface.co/models?filter=segformer
}
class     a    ( lowercase__   ):



      """simple docstring"""


      a  : int							       =	'segformer'
      def __init__(      self					:			Dict		,      __lowercase					:			str=3		,      __lowercase					:			Dict=4		,      __lowercase					:			Any=[2, 2, 2, 2]		,      __lowercase					:			Optional[int]=[8, 4, 2, 1]		,      __lowercase					:			List[str]=[32, 64, 160, 256]		,      __lowercase					:			Union[str, Any]=[7, 3, 3, 3]		,      __lowercase					:			Optional[int]=[4, 2, 2, 2]		,      __lowercase					:			Any=[1, 2, 5, 8]		,      __lowercase					:			List[str]=[4, 4, 4, 4]		,      __lowercase					:			Any="gelu"		,      __lowercase					:			Optional[int]=0.0		,      __lowercase					:			Dict=0.0		,      __lowercase					:			Optional[Any]=0.1		,      __lowercase					:			int=0.02		,      __lowercase					:			Optional[int]=0.1		,      __lowercase					:			Optional[Any]=1e-6		,      __lowercase					:			Tuple=256		,      __lowercase					:			List[Any]=255		,      **__lowercase					:			Union[str, Any]		,      )							->      List[Any]:
        super().__init__(**__lowercase					)

        if "reshape_last_stage" in kwargs and kwargs["reshape_last_stage"] is False:
          warnings.warn(
              """Reshape_last_stage is set to False in this config. This argument is deprecated and will soon be"""
              """ removed, as the behaviour will default to that of reshape_last_stage = True."""		,      __lowercase		,      )

        __UpperCAmelCase							:	Tuple           =  num_channels
        __UpperCAmelCase							:	Any           =  num_encoder_blocks
        __UpperCAmelCase							:	List[Any]           =  depths
        __UpperCAmelCase							:	Dict           =  sr_ratios
        __UpperCAmelCase							:	int           =  hidden_sizes
        __UpperCAmelCase							:	Optional[Any]           =  patch_sizes
        __UpperCAmelCase							:	List[Any]           =  strides
        __UpperCAmelCase							:	List[str]           =  mlp_ratios
        __UpperCAmelCase							:	Tuple           =  num_attention_heads
        __UpperCAmelCase							:	List[str]           =  hidden_act
        __UpperCAmelCase							:	Tuple           =  hidden_dropout_prob
        __UpperCAmelCase							:	Dict           =  attention_probs_dropout_prob
        __UpperCAmelCase							:	Any           =  classifier_dropout_prob
        __UpperCAmelCase							:	Optional[int]           =  initializer_range
        __UpperCAmelCase							:	Optional[Any]           =  drop_path_rate
        __UpperCAmelCase							:	int           =  layer_norm_eps
        __UpperCAmelCase							:	Dict           =  decoder_hidden_size
        __UpperCAmelCase							:	Tuple           =  kwargs.get("""reshape_last_stage"""		,      __lowercase					)
        __UpperCAmelCase							:	int           =  semantic_loss_ignore_index


class     a    ( lowercase__   ):



      """simple docstring"""


      a  : Union[str, Any]							       =	version.parse('1.11'   )
      @property
      def 	UpperCAmelCase      (      self					:			Union[str, Any]					)							->      Mapping[str, Mapping[int, str]]:
        return OrderedDict(
            [
                ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
            ]					)
      @property
      def 	UpperCAmelCase      (      self					:			Optional[Any]					)							->      float:
        return 1e-4




      @property
      def 	UpperCAmelCase      (      self					:			str					)							->      int:
        return 12


