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





@flax.struct.dataclass
class 				a__					(						A__       ):
     UpperCAmelCase__         =      42





@flax_register_to_config
class 				a__					(						nn.Module   ,							A__   ,							A__       ):
     UpperCAmelCase__         =      32
     UpperCAmelCase__         =      4
     UpperCAmelCase__         =      4
     UpperCAmelCase__         =      (
         "CrossAttnDownBlock2D",
         "CrossAttnDownBlock2D",
         "CrossAttnDownBlock2D",
         "DownBlock2D",
     )
     UpperCAmelCase__         =      ("UpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D")
     UpperCAmelCase__         =      False
     UpperCAmelCase__         =      (3_20, 6_40, 12_80, 12_80)
     UpperCAmelCase__         =      2
     UpperCAmelCase__         =      8
     UpperCAmelCase__         =      None
     UpperCAmelCase__         =      12_80
     UpperCAmelCase__         =      0.0
     UpperCAmelCase__         =      False
     UpperCAmelCase__         =      jnp.floataa
     UpperCAmelCase__         =      True
     UpperCAmelCase__         =      0
     UpperCAmelCase__         =      False



     def  lowerCamelCase_						(							self :str      ,  _lowerCamelCase :jax.random.KeyArray   ):







      '''simple docstring'''



      UpperCamelCase_     :					List[str]   	=(1, self.in_channels, self.sample_size, self.sample_size)
      UpperCamelCase_     :					List[str]   	=jnp.zeros(_lowerCamelCase      ,  dtype=jnp.floataa   )
      UpperCamelCase_     :					Tuple   	=jnp.ones((1,)      ,  dtype=jnp.intaa   )
      UpperCamelCase_     :					Tuple   	=jnp.zeros((1, 1, self.cross_attention_dim)      ,  dtype=jnp.floataa   )

      UpperCamelCase_	,     UpperCamelCase_     :					List[str]   	=jax.random.split(_lowerCamelCase   )
      UpperCamelCase_     :					List[str]   	={'params': params_rng, 'dropout': dropout_rng}

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



     def  lowerCamelCase_						(							self :Optional[int]   ):







      '''simple docstring'''



      UpperCamelCase_     :					List[Any]   	=self.block_out_channels
      UpperCamelCase_     :					List[Any]   	=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.
      UpperCamelCase_     :					str   	=self.num_attention_heads or self.attention_head_dim

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

      # time
      UpperCamelCase_     :					List[Any]   	=FlaxTimesteps(
          block_out_channels[0]      ,  flip_sin_to_cos=self.flip_sin_to_cos      ,  freq_shift=self.config.freq_shift   )
      UpperCamelCase_     :					Dict   	=FlaxTimestepEmbedding(_lowerCamelCase      ,  dtype=self.dtype   )

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

      if isinstance(_lowerCamelCase      ,  _lowerCamelCase   ):
       UpperCamelCase_     :					Dict   	=(num_attention_heads,) * len(self.down_block_types   )

      # down
      UpperCamelCase_     :					str   	=[]
      UpperCamelCase_     :					int   	=block_out_channels[0]
      for i, down_block_type in enumerate(self.down_block_types   ):
       UpperCamelCase_     :					int   	=output_channel
       UpperCamelCase_     :					Union[str, Any]   	=block_out_channels[i]
       UpperCamelCase_     :					Optional[Any]   	=i == len(_lowerCamelCase   ) - 1

       if down_block_type == "CrossAttnDownBlock2D":
        UpperCamelCase_     :					str   	=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:
        UpperCamelCase_     :					Any   	=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   )
      UpperCamelCase_     :					str   	=down_blocks

      # mid
      UpperCamelCase_     :					List[Any]   	=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
      UpperCamelCase_     :					Union[str, Any]   	=[]
      UpperCamelCase_     :					int   	=list(reversed(_lowerCamelCase   )   )
      UpperCamelCase_     :					List[str]   	=list(reversed(_lowerCamelCase   )   )
      UpperCamelCase_     :					str   	=list(reversed(_lowerCamelCase   )   )
      UpperCamelCase_     :					List[Any]   	=reversed_block_out_channels[0]
      for i, up_block_type in enumerate(self.up_block_types   ):
       UpperCamelCase_     :					Optional[Any]   	=output_channel
       UpperCamelCase_     :					List[str]   	=reversed_block_out_channels[i]
       UpperCamelCase_     :					str   	=reversed_block_out_channels[min(i + 1      ,  len(_lowerCamelCase   ) - 1   )]

       UpperCamelCase_     :					Any   	=i == len(_lowerCamelCase   ) - 1

       if up_block_type == "CrossAttnUpBlock2D":
        UpperCamelCase_     :					str   	=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:
        UpperCamelCase_     :					Optional[int]   	=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   )
       UpperCamelCase_     :					int   	=output_channel
      UpperCamelCase_     :					str   	=up_blocks

      # out
      UpperCamelCase_     :					Optional[Any]   	=nn.GroupNorm(num_groups=32      ,  epsilon=1E-5   )
      UpperCamelCase_     :					List[str]   	=nn.Conv(
          self.out_channels      ,  kernel_size=(3, 3)      ,  strides=(1, 1)      ,  padding=((1, 1), (1, 1))      ,  dtype=self.dtype      ,  )




     def __call__(							self :Union[str, Any]      ,  _lowerCamelCase :Optional[int]      ,  _lowerCamelCase :Dict      ,  _lowerCamelCase :Dict      ,  _lowerCamelCase :Any=None      ,  _lowerCamelCase :Dict=None      ,  _lowerCamelCase :bool = True      ,  _lowerCamelCase :bool = False      ,  ):







      '''simple docstring'''



      if not isinstance(_lowerCamelCase      ,  jnp.ndarray   ):
       UpperCamelCase_     :					List[Any]   	=jnp.array([timesteps]      ,  dtype=jnp.intaa   )
      elif isinstance(_lowerCamelCase      ,  jnp.ndarray   ) and len(timesteps.shape   ) == 0:
       UpperCamelCase_     :					List[str]   	=timesteps.astype(dtype=jnp.floataa   )
       UpperCamelCase_     :					Optional[int]   	=jnp.expand_dims(_lowerCamelCase      ,  0   )

      UpperCamelCase_     :					Union[str, Any]   	=self.time_proj(_lowerCamelCase   )
      UpperCamelCase_     :					Any   	=self.time_embedding(_lowerCamelCase   )

      # 2. pre-process
      UpperCamelCase_     :					Optional[Any]   	=jnp.transpose(_lowerCamelCase      ,  (0, 2, 3, 1)   )
      UpperCamelCase_     :					str   	=self.conv_in(_lowerCamelCase   )

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

      if down_block_additional_residuals is not None:
       UpperCamelCase_     :					str   	=()

       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,)

       UpperCamelCase_     :					Any   	=new_down_block_res_samples

      # 4. mid
      UpperCamelCase_     :					Tuple   	=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:
       UpperCamelCase_     :					Optional[Any]   	=down_block_res_samples[-(self.layers_per_block + 1) :]
       UpperCamelCase_     :					Optional[Any]   	=down_block_res_samples[: -(self.layers_per_block + 1)]
       if isinstance(_lowerCamelCase      ,  _lowerCamelCase   ):
        UpperCamelCase_     :					Optional[int]   	=up_block(
            _lowerCamelCase      ,  temb=_lowerCamelCase      ,  encoder_hidden_states=_lowerCamelCase      ,  res_hidden_states_tuple=_lowerCamelCase      ,  deterministic=not train      ,  )
       else:
        UpperCamelCase_     :					List[Any]   	=up_block(_lowerCamelCase      ,  temb=_lowerCamelCase      ,  res_hidden_states_tuple=_lowerCamelCase      ,  deterministic=not train   )

        # 6. post-process
      UpperCamelCase_     :					Any   	=self.conv_norm_out(_lowerCamelCase   )
      UpperCamelCase_     :					Optional[Any]   	=nn.silu(_lowerCamelCase   )
      UpperCamelCase_     :					str   	=self.conv_out(_lowerCamelCase   )
      UpperCamelCase_     :					Optional[int]   	=jnp.transpose(_lowerCamelCase      ,  (0, 3, 1, 2)   )

      if not return_dict:
       return (sample,)

      return FlaxUNetaDConditionOutput(sample=_lowerCamelCase   )


"""simple docstring"""






from collections.abc import Callable





class 				a__					:



     def __init__(							self :Tuple      ,  _lowerCamelCase :Callable | None = None   ):







      '''simple docstring'''



      UpperCamelCase_     :					list   	=[]
      # Stores indexes of each item for supporting updates and deletion.
      UpperCamelCase_     :					dict   	={}
      # Stores current size of heap.
      UpperCamelCase_     :					Any   	=0
      # Stores function used to evaluate the score of an item on which basis ordering
      # will be done.
      UpperCamelCase_     :					List[str]   	=key or (lambda _lowerCamelCase   : x)



     def  lowerCamelCase_						(							self :Tuple      ,  _lowerCamelCase :int   ):







      '''simple docstring'''



      return int((i - 1) / 2   ) if i > 0 else None



     def  lowerCamelCase_						(							self :Optional[Any]      ,  _lowerCamelCase :int   ):







      '''simple docstring'''



      UpperCamelCase_     :					List[str]   	=int(2 * i + 1   )
      return left if 0 < left < self.size else None



     def  lowerCamelCase_						(							self :Tuple      ,  _lowerCamelCase :int   ):







      '''simple docstring'''



      UpperCamelCase_     :					Optional[Any]   	=int(2 * i + 2   )
      return right if 0 < right < self.size else None



     def  lowerCamelCase_						(							self :Dict      ,  _lowerCamelCase :int      ,  _lowerCamelCase :int   ):







      '''simple docstring'''



      UpperCamelCase_	,     UpperCamelCase_     :					Optional[int]   	=(
          self.pos_map[self.arr[j][0]],
          self.pos_map[self.arr[i][0]],
      )
      # Then swap the items in the list.
      UpperCamelCase_	,     UpperCamelCase_     :					Union[str, Any]   	=self.arr[j], self.arr[i]



     def  lowerCamelCase_						(							self :Optional[Any]      ,  _lowerCamelCase :int      ,  _lowerCamelCase :int   ):







      '''simple docstring'''



      return self.arr[i][1] < self.arr[j][1]



     def  lowerCamelCase_						(							self :Any      ,  _lowerCamelCase :int   ):







      '''simple docstring'''



      UpperCamelCase_     :					int   	=self._left(_lowerCamelCase   )
      UpperCamelCase_     :					List[Any]   	=self._right(_lowerCamelCase   )
      UpperCamelCase_     :					Optional[Any]   	=i

      if left is not None and not self._cmp(_lowerCamelCase      ,  _lowerCamelCase   ):
       UpperCamelCase_     :					Optional[int]   	=left
      if right is not None and not self._cmp(_lowerCamelCase      ,  _lowerCamelCase   ):
       UpperCamelCase_     :					List[Any]   	=right

      return valid_parent



     def  lowerCamelCase_						(							self :Any      ,  _lowerCamelCase :int   ):







      '''simple docstring'''



      UpperCamelCase_     :					Dict   	=self._parent(_lowerCamelCase   )
      while parent is not None and not self._cmp(_lowerCamelCase      ,  _lowerCamelCase   ):
       self._swap(_lowerCamelCase      ,  _lowerCamelCase   )
       UpperCamelCase_	,     UpperCamelCase_     :					Dict   	=parent, self._parent(_lowerCamelCase   )



     def  lowerCamelCase_						(							self :List[str]      ,  _lowerCamelCase :int   ):







      '''simple docstring'''



      UpperCamelCase_     :					Optional[Any]   	=self._get_valid_parent(_lowerCamelCase   )
      while valid_parent != index:
       self._swap(_lowerCamelCase      ,  _lowerCamelCase   )
       UpperCamelCase_	,     UpperCamelCase_     :					int   	=valid_parent, self._get_valid_parent(_lowerCamelCase   )



     def  lowerCamelCase_						(							self :Optional[Any]      ,  _lowerCamelCase :int      ,  _lowerCamelCase :int   ):







      '''simple docstring'''



      if item not in self.pos_map:
       return
      UpperCamelCase_     :					List[Any]   	=self.pos_map[item]
      UpperCamelCase_     :					int   	=[item, self.key(_lowerCamelCase   )]
      # Make sure heap is right in both up and down direction.
      # Ideally only one of them will make any change.
      self._heapify_up(_lowerCamelCase   )
      self._heapify_down(_lowerCamelCase   )



     def  lowerCamelCase_						(							self :Tuple      ,  _lowerCamelCase :int   ):







      '''simple docstring'''



      if item not in self.pos_map:
       return
      UpperCamelCase_     :					Any   	=self.pos_map[item]
      del self.pos_map[item]
      UpperCamelCase_     :					Dict   	=self.arr[self.size - 1]
      UpperCamelCase_     :					Optional[int]   	=index
      self.size -= 1
      # Make sure heap is right in both up and down direction. Ideally only one
      # of them will make any change- so no performance loss in calling both.
      if self.size > index:
       self._heapify_up(_lowerCamelCase   )
       self._heapify_down(_lowerCamelCase   )



     def  lowerCamelCase_						(							self :Optional[int]      ,  _lowerCamelCase :int      ,  _lowerCamelCase :int   ):







      '''simple docstring'''



      UpperCamelCase_     :					Optional[int]   	=len(self.arr   )
      if arr_len == self.size:
       self.arr.append([item, self.key(_lowerCamelCase   )]   )
      else:
       UpperCamelCase_     :					str   	=[item, self.key(_lowerCamelCase   )]
      UpperCamelCase_     :					Optional[int]   	=self.size
      self.size += 1
      self._heapify_up(self.size - 1   )



     def  lowerCamelCase_						(							self :List[Any]   ):







      '''simple docstring'''



      return self.arr[0] if self.size else None




     def  lowerCamelCase_						(							self :Tuple   ):







      '''simple docstring'''



      UpperCamelCase_     :					int   	=self.get_top()
      if top_item_tuple:
       self.delete_item(top_item_tuple[0]   )
      return top_item_tuple




def       A_  (				):
 pass


if __name__ == "__main__":
 import doctest

 doctest.testmod()
