



"""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      UpperCAmelCase__		(      __snake_case     ):
		lowerCAmelCase_       :	Tuple 							=     42






@flax_register_to_config
class      UpperCAmelCase__		(      nn.Module   ,__snake_case   ,__snake_case     ):
		lowerCAmelCase_       :	List[Any] 							=     32
		lowerCAmelCase_       :	List[str] 							=     4
		lowerCAmelCase_       :	Any 							=     4
		lowerCAmelCase_       :	Any 							=     (
		    """CrossAttnDownBlock2D""",
		    """CrossAttnDownBlock2D""",
		    """CrossAttnDownBlock2D""",
		    """DownBlock2D""",
		)
		lowerCAmelCase_       :	Tuple 							=     ("""UpBlock2D""", """CrossAttnUpBlock2D""", """CrossAttnUpBlock2D""", """CrossAttnUpBlock2D""")
		lowerCAmelCase_       :	int 							=     False
		lowerCAmelCase_       :	Dict 							=     (3_20, 6_40, 12_80, 12_80)
		lowerCAmelCase_       :	Dict 							=     2
		lowerCAmelCase_       :	List[Any] 							=     8
		lowerCAmelCase_       :	Union[str, Any] 							=     None
		lowerCAmelCase_       :	Dict 							=     12_80
		lowerCAmelCase_       :	Optional[int] 							=     0.0
		lowerCAmelCase_       :	Optional[int] 							=     False
		lowerCAmelCase_       :	Any 							=     jnp.floataa
		lowerCAmelCase_       :	Tuple 							=     True
		lowerCAmelCase_       :	Dict 							=     0
		lowerCAmelCase_       :	int 							=     False



		def 					A_		( self  :    Dict      ,							snake_case  :    List[str]				)			->						Union[str, Any]:






									'''simple docstring'''

									A         =					(1, self.in_channels, self.sample_size, self.sample_size)
									A         =					jnp.zeros(_lowercase      ,							dtype=jnp.floataa				)
									A         =					jnp.ones((1,)      ,							dtype=jnp.intaa				)
									A         =					jnp.zeros((1, 1, self.cross_attention_dim)      ,							dtype=jnp.floataa				)

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

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



		def 					A_		( self  :    Optional[int]				)			->						Union[str, Any]:






									'''simple docstring'''

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

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

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

									A         =					self.only_cross_attention
									if isinstance(_lowercase      ,							_lowercase				):
																A         =					(only_cross_attention,) * len(self.down_block_types				)

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

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

																if down_block_type == "CrossAttnDownBlock2D":
																							A         =					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]      ,							use_memory_efficient_attention=self.use_memory_efficient_attention      ,							dtype=self.dtype      ,							)
																else:
																							A         =					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				)
									A         =					down_blocks

									# mid
									A         =					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
									A         =					[]
									A         =					list(reversed(_lowercase				)				)
									A         =					list(reversed(_lowercase				)				)
									A         =					list(reversed(_lowercase				)				)
									A         =					reversed_block_out_channels[0]
									for i, up_block_type in enumerate(self.up_block_types				):
																A         =					output_channel
																A         =					reversed_block_out_channels[i]
																A         =					reversed_block_out_channels[min(i + 1      ,							len(_lowercase				) - 1				)]

																A         =					i == len(_lowercase				) - 1

																if up_block_type == "CrossAttnUpBlock2D":
																							A         =					FlaxCrossAttnUpBlockaD(
																							    in_channels=_lowercase      ,							out_channels=_lowercase      ,							prev_output_channel=_lowercase      ,							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:
																							A         =					FlaxUpBlockaD(
																							    in_channels=_lowercase      ,							out_channels=_lowercase      ,							prev_output_channel=_lowercase      ,							num_layers=self.layers_per_block + 1      ,							add_upsample=not is_final_block      ,							dropout=self.dropout      ,							dtype=self.dtype      ,							)

																up_blocks.append(_lowercase				)
																A         =					output_channel
									A         =					up_blocks

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



		def __call__( self  :    Any      ,							snake_case  :    List[Any]      ,							snake_case  :    Union[str, Any]      ,							snake_case  :    int      ,							snake_case  :    Optional[int]=None      ,							snake_case  :    Optional[Any]=None      ,							snake_case  :    List[str] = True      ,							snake_case  :    Union[str, Any] = False      ,							)			->						str:






									'''simple docstring'''

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

									A         =					self.time_proj(_lowercase				)
									A         =					self.time_embedding(_lowercase				)

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

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

									if down_block_additional_residuals is not None:
																A         =					()

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

																A         =					new_down_block_res_samples

									# 4. mid
									A         =					self.mid_block(_lowercase      ,							_lowercase      ,							_lowercase      ,							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:
																A         =					down_block_res_samples[-(self.layers_per_block + 1) :]
																A         =					down_block_res_samples[: -(self.layers_per_block + 1)]
																if isinstance(_lowercase      ,							_lowercase				):
																							A         =					up_block(
																							    _lowercase      ,							temb=_lowercase      ,							encoder_hidden_states=_lowercase      ,							res_hidden_states_tuple=_lowercase      ,							deterministic=not train      ,							)
																else:
																							A         =					up_block(_lowercase      ,							temb=_lowercase      ,							res_hidden_states_tuple=_lowercase      ,							deterministic=not train				)

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

									if not return_dict:
																return (sample,)

									return FlaxUNetaDConditionOutput(sample=_lowercase				)








"""simple docstring"""







from __future__ import annotations

import sys
from collections import deque
from typing import Generic, TypeVar

A    =			TypeVar('T')






class      UpperCAmelCase__		(      Generic[T]     ):
		lowerCAmelCase_       :	deque[T]  # Cache store of keys
		lowerCAmelCase_       :	set[T]  # References of the keys in cache
		lowerCAmelCase_       :	int 							=     10  # Maximum capacity of cache



		def __init__( self  :    int      ,							snake_case  :    int				)			->						None:






									'''simple docstring'''

									A         =					deque()
									A         =					set()
									if not n:
																A         =					sys.maxsize
									elif n < 0:
																raise ValueError('n should be an integer greater than 0.'				)
									else:
																A         =					n



		def 					A_		( self  :    Optional[Any]      ,							snake_case  :    T				)			->						None:






									'''simple docstring'''

									if x not in self.key_reference:
																if len(self.dq_store				) == LRUCache._MAX_CAPACITY:
																							A         =					self.dq_store.pop()
																							self.key_reference.remove(snake_case				)
									else:
																self.dq_store.remove(snake_case				)

									self.dq_store.appendleft(snake_case				)
									self.key_reference.add(snake_case				)



		def 					A_		( self  :    Dict				)			->						None:






									'''simple docstring'''

									for k in self.dq_store:
																print(snake_case				)



		def __repr__( self  :    int				)			->						str:






									'''simple docstring'''

									return f"""LRUCache({self._MAX_CAPACITY}) => {list(self.dq_store				)}"""


if __name__ == "__main__":
							import doctest

							doctest.testmod()

							A    =			LRUCache(4)
							lru_cache.refer('A')
							lru_cache.refer(2)
							lru_cache.refer(3)
							lru_cache.refer('A')
							lru_cache.refer(4)
							lru_cache.refer(5)
							lru_cache.display()

							print(lru_cache)
							assert str(lru_cache) == "LRUCache(4) => [5, 4, 'A', 3]"



