






'''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        a_		(    lowerCamelCase       ):
		lowercase  	=   42
		lowercase  	=   42






class        a_		(    nn.Module       ):
		lowercase  	=   42
		lowercase  	=   (16, 32, 96, 2_56)
		lowercase  	=   jnp.floataa

		def 						A__		(   self   )						->   Any:



								"""simple docstring"""






								UpperCamelCase       						=      nn.Conv(
								    self.block_out_channels[0]    ,	kernel_size=(3, 3)    ,	padding=((1, 1), (1, 1))    ,	dtype=self.dtype    ,	)

								UpperCamelCase       						=      []
								for i in range(len(self.block_out_channels   ) - 1   ):
														UpperCamelCase       						=      self.block_out_channels[i]
														UpperCamelCase       						=      self.block_out_channels[i + 1]
														UpperCamelCase       						=      nn.Conv(
														    _SCREAMING_SNAKE_CASE    ,	kernel_size=(3, 3)    ,	padding=((1, 1), (1, 1))    ,	dtype=self.dtype    ,	)
														blocks.append(_SCREAMING_SNAKE_CASE   )
														UpperCamelCase       						=      nn.Conv(
														    _SCREAMING_SNAKE_CASE    ,	kernel_size=(3, 3)    ,	strides=(2, 2)    ,	padding=((1, 1), (1, 1))    ,	dtype=self.dtype    ,	)
														blocks.append(_SCREAMING_SNAKE_CASE   )
								UpperCamelCase       						=      blocks

								UpperCamelCase       						=      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    ,	_SCREAMING_SNAKE_CASE   )						->   Union[str, Any]:



								"""simple docstring"""






								UpperCamelCase       						=      self.conv_in(_SCREAMING_SNAKE_CASE   )
								UpperCamelCase       						=      nn.silu(_SCREAMING_SNAKE_CASE   )

								for block in self.blocks:
														UpperCamelCase       						=      block(_SCREAMING_SNAKE_CASE   )
														UpperCamelCase       						=      nn.silu(_SCREAMING_SNAKE_CASE   )

								UpperCamelCase       						=      self.conv_out(_SCREAMING_SNAKE_CASE   )

								return embedding






@flax_register_to_config
class        a_		(    nn.Module			, lowerCamelCase			, lowerCamelCase       ):
		lowercase  	=   32
		lowercase  	=   4
		lowercase  	=   (
		    """CrossAttnDownBlock2D""",
		    """CrossAttnDownBlock2D""",
		    """CrossAttnDownBlock2D""",
		    """DownBlock2D""",
		)
		lowercase  	=   False
		lowercase  	=   (3_20, 6_40, 12_80, 12_80)
		lowercase  	=   2
		lowercase  	=   8
		lowercase  	=   None
		lowercase  	=   12_80
		lowercase  	=   0.0
		lowercase  	=   False
		lowercase  	=   jnp.floataa
		lowercase  	=   True
		lowercase  	=   0
		lowercase  	=   """rgb"""
		lowercase  	=   (16, 32, 96, 2_56)

		def 						A__		(   self    ,	_SCREAMING_SNAKE_CASE   )						->   FrozenDict:



								"""simple docstring"""






								UpperCamelCase       						=      (1, self.in_channels, self.sample_size, self.sample_size)
								UpperCamelCase       						=      jnp.zeros(_SCREAMING_SNAKE_CASE    ,	dtype=jnp.floataa   )
								UpperCamelCase       						=      jnp.ones((1,)    ,	dtype=jnp.intaa   )
								UpperCamelCase       						=      jnp.zeros((1, 1, self.cross_attention_dim)    ,	dtype=jnp.floataa   )
								UpperCamelCase       						=      (1, 3, self.sample_size * 8, self.sample_size * 8)
								UpperCamelCase       						=      jnp.zeros(_SCREAMING_SNAKE_CASE    ,	dtype=jnp.floataa   )

								UpperCamelCase						,UpperCamelCase       						=      jax.random.split(_SCREAMING_SNAKE_CASE   )
								UpperCamelCase       						=      {"""params""": params_rng, """dropout""": dropout_rng}

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

		def 						A__		(   self   )						->   Dict:



								"""simple docstring"""






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

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

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

								UpperCamelCase       						=      FlaxControlNetConditioningEmbedding(
								    conditioning_embedding_channels=block_out_channels[0]    ,	block_out_channels=self.conditioning_embedding_out_channels    ,	)

								UpperCamelCase       						=      self.only_cross_attention
								if isinstance(_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE   ):
														UpperCamelCase       						=      (only_cross_attention,) * len(self.down_block_types   )

								if isinstance(_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE   ):
														UpperCamelCase       						=      (num_attention_heads,) * len(self.down_block_types   )

								# down
								UpperCamelCase       						=      []
								UpperCamelCase       						=      []

								UpperCamelCase       						=      block_out_channels[0]

								UpperCamelCase       						=      nn.Conv(
								    _SCREAMING_SNAKE_CASE    ,	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(_SCREAMING_SNAKE_CASE   )

								for i, down_block_type in enumerate(self.down_block_types   ):
														UpperCamelCase       						=      output_channel
														UpperCamelCase       						=      block_out_channels[i]
														UpperCamelCase       						=      i == len(_SCREAMING_SNAKE_CASE   ) - 1

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

														down_blocks.append(_SCREAMING_SNAKE_CASE   )

														for _ in range(self.layers_per_block   ):
																				UpperCamelCase       						=      nn.Conv(
																				    _SCREAMING_SNAKE_CASE    ,	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(_SCREAMING_SNAKE_CASE   )

														if not is_final_block:
																				UpperCamelCase       						=      nn.Conv(
																				    _SCREAMING_SNAKE_CASE    ,	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(_SCREAMING_SNAKE_CASE   )

								UpperCamelCase       						=      down_blocks
								UpperCamelCase       						=      controlnet_down_blocks

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

								UpperCamelCase       						=      nn.Conv(
								    _SCREAMING_SNAKE_CASE    ,	kernel_size=(1, 1)    ,	padding="""VALID"""    ,	kernel_init=nn.initializers.zeros_init()    ,	bias_init=nn.initializers.zeros_init()    ,	dtype=self.dtype    ,	)


		def __call__(   self    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE = 1.0    ,	_SCREAMING_SNAKE_CASE = True    ,	_SCREAMING_SNAKE_CASE = False    ,	)						->   Union[FlaxControlNetOutput, Tuple]:



								"""simple docstring"""






								UpperCamelCase       						=      self.controlnet_conditioning_channel_order
								if channel_order == "bgr":
														UpperCamelCase       						=      jnp.flip(_SCREAMING_SNAKE_CASE    ,	axis=1   )

								# 1. time
								if not isinstance(_SCREAMING_SNAKE_CASE    ,	jnp.ndarray   ):
														UpperCamelCase       						=      jnp.array([timesteps]    ,	dtype=jnp.intaa   )
								elif isinstance(_SCREAMING_SNAKE_CASE    ,	jnp.ndarray   ) and len(timesteps.shape   ) == 0:
														UpperCamelCase       						=      timesteps.astype(dtype=jnp.floataa   )
														UpperCamelCase       						=      jnp.expand_dims(_SCREAMING_SNAKE_CASE    ,	0   )

								UpperCamelCase       						=      self.time_proj(_SCREAMING_SNAKE_CASE   )
								UpperCamelCase       						=      self.time_embedding(_SCREAMING_SNAKE_CASE   )

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

								UpperCamelCase       						=      jnp.transpose(_SCREAMING_SNAKE_CASE    ,	(0, 2, 3, 1)   )
								UpperCamelCase       						=      self.controlnet_cond_embedding(_SCREAMING_SNAKE_CASE   )
								sample += controlnet_cond

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

								# 4. mid
								UpperCamelCase       						=      self.mid_block(_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	deterministic=not train   )

								# 5. contronet blocks
								UpperCamelCase       						=      ()
								for down_block_res_sample, controlnet_block in zip(_SCREAMING_SNAKE_CASE    ,	self.controlnet_down_blocks   ):
														UpperCamelCase       						=      controlnet_block(_SCREAMING_SNAKE_CASE   )
														controlnet_down_block_res_samples += (down_block_res_sample,)

								UpperCamelCase       						=      controlnet_down_block_res_samples

								UpperCamelCase       						=      self.controlnet_mid_block(_SCREAMING_SNAKE_CASE   )

								# 6. scaling
								UpperCamelCase       						=      [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=_SCREAMING_SNAKE_CASE    ,	mid_block_res_sample=_SCREAMING_SNAKE_CASE   )








'''simple docstring'''

from __future__ import annotations

from typing import Any






class        a_		:

  def __init__(   self    ,	_SCREAMING_SNAKE_CASE = 6   )						->   None:



        """simple docstring"""






        UpperCamelCase       						=      None
        UpperCamelCase       						=      None
        self.create_linked_list(_SCREAMING_SNAKE_CASE   )

  def 						A__		(   self    ,	_SCREAMING_SNAKE_CASE   )						->   None:



        """simple docstring"""






        UpperCamelCase       						=      Node()
        UpperCamelCase       						=      current_node
        UpperCamelCase       						=      current_node
        UpperCamelCase       						=      current_node
        for _ in range(1    ,	_SCREAMING_SNAKE_CASE   ):
              UpperCamelCase       						=      Node()
              UpperCamelCase       						=      current_node
              UpperCamelCase       						=      previous_node
              UpperCamelCase       						=      current_node
        UpperCamelCase       						=      self.front
        UpperCamelCase       						=      previous_node

  def 						A__		(   self   )						->   bool:



        """simple docstring"""






        return (
            self.front == self.rear
            and self.front is not None
            and self.front.data is None
        )

  def 						A__		(   self   )						->   Any | None:



        """simple docstring"""






        self.check_can_perform_operation()
        return self.front.data if self.front else None

  def 						A__		(   self    ,	_SCREAMING_SNAKE_CASE   )						->   None:



        """simple docstring"""






        if self.rear is None:
              return

        self.check_is_full()
        if not self.is_empty():
              UpperCamelCase       						=      self.rear.next
        if self.rear:
              UpperCamelCase       						=      data

  def 						A__		(   self   )						->   Any:



        """simple docstring"""






        self.check_can_perform_operation()
        if self.rear is None or self.front is None:
              return None
        if self.front == self.rear:
              UpperCamelCase       						=      self.front.data
              UpperCamelCase       						=      None
              return data

        UpperCamelCase       						=      self.front
        UpperCamelCase       						=      old_front.next
        UpperCamelCase       						=      old_front.data
        UpperCamelCase       						=      None
        return data

  def 						A__		(   self   )						->   None:



        """simple docstring"""






        if self.is_empty():
              raise Exception("""Empty Queue"""   )


  def 						A__		(   self   )						->   None:



        """simple docstring"""






        if self.rear and self.rear.next == self.front:
              raise Exception("""Full Queue"""   )






class        a_		:

  def __init__(   self   )						->   None:



        """simple docstring"""






        UpperCamelCase       						=      None
        UpperCamelCase       						=      None
        UpperCamelCase       						=      None


if __name__ == "__main__":
     import doctest

     doctest.testmod()
