




import flax.linen as nn
import jax.numpy as jnp

from .attention_flax import FlaxTransformeraDModel
from .resnet_flax import FlaxDownsampleaD, FlaxResnetBlockaD, FlaxUpsampleaD






class     __lowerCAmelCase (       nn.Module ):
			snake_case_      : str      =     42
			snake_case_      : Optional[int]      =     42
			snake_case_      : Optional[Any]      =     0.0
			snake_case_      : int      =     1
			snake_case_      : List[str]      =     1
			snake_case_      : Union[str, Any]      =     True
			snake_case_      : str      =     False
			snake_case_      : Dict      =     False
			snake_case_      : Optional[Any]      =     False
			snake_case_      : Optional[int]      =     jnp.floataa







			def UpperCamelCase			(       self    :			str      ):




									"""simple docstring"""


									_UpperCAmelCase													=     []
									_UpperCAmelCase													=     []

									for i in range(self.num_layers      ):
															_UpperCAmelCase													=     self.in_channels if i == 0 else self.out_channels

															_UpperCAmelCase													=     FlaxResnetBlockaD(
															    in_channels=_a    ,    out_channels=self.out_channels    ,    dropout_prob=self.dropout    ,    dtype=self.dtype    ,    )
															resnets.append(_a      )

															_UpperCAmelCase													=     FlaxTransformeraDModel(
															    in_channels=self.out_channels    ,    n_heads=self.num_attention_heads    ,    d_head=self.out_channels // self.num_attention_heads    ,    depth=1    ,    use_linear_projection=self.use_linear_projection    ,    only_cross_attention=self.only_cross_attention    ,    use_memory_efficient_attention=self.use_memory_efficient_attention    ,    dtype=self.dtype    ,    )
															attentions.append(_a      )

									_UpperCAmelCase													=     resnets
									_UpperCAmelCase													=     attentions

									if self.add_downsample:
															_UpperCAmelCase													=     FlaxDownsampleaD(self.out_channels    ,    dtype=self.dtype      )







			def __call__(       self    :			Dict    ,    snake_case__    :			Union[str, Any]    ,    snake_case__    :			Union[str, Any]    ,    snake_case__    :			str    ,    snake_case__    :			Optional[Any]=True      ):




									"""simple docstring"""


									_UpperCAmelCase													=     ()

									for resnet, attn in zip(self.resnets    ,    self.attentions      ):
															_UpperCAmelCase													=     resnet(_a    ,    _a    ,    deterministic=_a      )
															_UpperCAmelCase													=     attn(_a    ,    _a    ,    deterministic=_a      )
															output_states += (hidden_states,)

									if self.add_downsample:
															_UpperCAmelCase													=     self.downsamplers_a(_a      )
															output_states += (hidden_states,)

									return hidden_states, output_states






class     __lowerCAmelCase (       nn.Module ):
			snake_case_      : List[str]      =     42
			snake_case_      : Dict      =     42
			snake_case_      : str      =     0.0
			snake_case_      : Union[str, Any]      =     1
			snake_case_      : Tuple      =     True
			snake_case_      : List[str]      =     jnp.floataa







			def UpperCamelCase			(       self    :			Dict      ):




									"""simple docstring"""


									_UpperCAmelCase													=     []

									for i in range(self.num_layers      ):
															_UpperCAmelCase													=     self.in_channels if i == 0 else self.out_channels

															_UpperCAmelCase													=     FlaxResnetBlockaD(
															    in_channels=_a    ,    out_channels=self.out_channels    ,    dropout_prob=self.dropout    ,    dtype=self.dtype    ,    )
															resnets.append(_a      )
									_UpperCAmelCase													=     resnets

									if self.add_downsample:
															_UpperCAmelCase													=     FlaxDownsampleaD(self.out_channels    ,    dtype=self.dtype      )







			def __call__(       self    :			Dict    ,    snake_case__    :			int    ,    snake_case__    :			Tuple    ,    snake_case__    :			Union[str, Any]=True      ):




									"""simple docstring"""


									_UpperCAmelCase													=     ()

									for resnet in self.resnets:
															_UpperCAmelCase													=     resnet(_a    ,    _a    ,    deterministic=_a      )
															output_states += (hidden_states,)

									if self.add_downsample:
															_UpperCAmelCase													=     self.downsamplers_a(_a      )
															output_states += (hidden_states,)

									return hidden_states, output_states






class     __lowerCAmelCase (       nn.Module ):
			snake_case_      : Dict      =     42
			snake_case_      : List[str]      =     42
			snake_case_      : List[Any]      =     42
			snake_case_      : List[Any]      =     0.0
			snake_case_      : str      =     1
			snake_case_      : Tuple      =     1
			snake_case_      : Optional[Any]      =     True
			snake_case_      : Any      =     False
			snake_case_      : int      =     False
			snake_case_      : Union[str, Any]      =     False
			snake_case_      : Optional[Any]      =     jnp.floataa







			def UpperCamelCase			(       self    :			int      ):




									"""simple docstring"""


									_UpperCAmelCase													=     []
									_UpperCAmelCase													=     []

									for i in range(self.num_layers      ):
															_UpperCAmelCase													=     self.in_channels if (i == self.num_layers - 1) else self.out_channels
															_UpperCAmelCase													=     self.prev_output_channel if i == 0 else self.out_channels

															_UpperCAmelCase													=     FlaxResnetBlockaD(
															    in_channels=resnet_in_channels + res_skip_channels    ,    out_channels=self.out_channels    ,    dropout_prob=self.dropout    ,    dtype=self.dtype    ,    )
															resnets.append(_a      )

															_UpperCAmelCase													=     FlaxTransformeraDModel(
															    in_channels=self.out_channels    ,    n_heads=self.num_attention_heads    ,    d_head=self.out_channels // self.num_attention_heads    ,    depth=1    ,    use_linear_projection=self.use_linear_projection    ,    only_cross_attention=self.only_cross_attention    ,    use_memory_efficient_attention=self.use_memory_efficient_attention    ,    dtype=self.dtype    ,    )
															attentions.append(_a      )

									_UpperCAmelCase													=     resnets
									_UpperCAmelCase													=     attentions

									if self.add_upsample:
															_UpperCAmelCase													=     FlaxUpsampleaD(self.out_channels    ,    dtype=self.dtype      )







			def __call__(       self    :			Optional[Any]    ,    snake_case__    :			Optional[Any]    ,    snake_case__    :			Dict    ,    snake_case__    :			Union[str, Any]    ,    snake_case__    :			str    ,    snake_case__    :			List[str]=True      ):




									"""simple docstring"""


									for resnet, attn in zip(self.resnets    ,    self.attentions      ):
															# pop res hidden states
															_UpperCAmelCase													=     res_hidden_states_tuple[-1]
															_UpperCAmelCase													=     res_hidden_states_tuple[:-1]
															_UpperCAmelCase													=     jnp.concatenate((hidden_states, res_hidden_states)    ,    axis=-1      )

															_UpperCAmelCase													=     resnet(_a    ,    _a    ,    deterministic=_a      )
															_UpperCAmelCase													=     attn(_a    ,    _a    ,    deterministic=_a      )

									if self.add_upsample:
															_UpperCAmelCase													=     self.upsamplers_a(_a      )

									return hidden_states






class     __lowerCAmelCase (       nn.Module ):
			snake_case_      : Optional[int]      =     42
			snake_case_      : Optional[int]      =     42
			snake_case_      : Optional[int]      =     42
			snake_case_      : List[Any]      =     0.0
			snake_case_      : Optional[int]      =     1
			snake_case_      : Any      =     True
			snake_case_      : str      =     jnp.floataa







			def UpperCamelCase			(       self    :			Optional[int]      ):




									"""simple docstring"""


									_UpperCAmelCase													=     []

									for i in range(self.num_layers      ):
															_UpperCAmelCase													=     self.in_channels if (i == self.num_layers - 1) else self.out_channels
															_UpperCAmelCase													=     self.prev_output_channel if i == 0 else self.out_channels

															_UpperCAmelCase													=     FlaxResnetBlockaD(
															    in_channels=resnet_in_channels + res_skip_channels    ,    out_channels=self.out_channels    ,    dropout_prob=self.dropout    ,    dtype=self.dtype    ,    )
															resnets.append(_a      )

									_UpperCAmelCase													=     resnets

									if self.add_upsample:
															_UpperCAmelCase													=     FlaxUpsampleaD(self.out_channels    ,    dtype=self.dtype      )







			def __call__(       self    :			str    ,    snake_case__    :			Dict    ,    snake_case__    :			Dict    ,    snake_case__    :			Optional[Any]    ,    snake_case__    :			str=True      ):




									"""simple docstring"""


									for resnet in self.resnets:
															# pop res hidden states
															_UpperCAmelCase													=     res_hidden_states_tuple[-1]
															_UpperCAmelCase													=     res_hidden_states_tuple[:-1]
															_UpperCAmelCase													=     jnp.concatenate((hidden_states, res_hidden_states)    ,    axis=-1      )

															_UpperCAmelCase													=     resnet(_a    ,    _a    ,    deterministic=_a      )

									if self.add_upsample:
															_UpperCAmelCase													=     self.upsamplers_a(_a      )

									return hidden_states






class     __lowerCAmelCase (       nn.Module ):
			snake_case_      : List[Any]      =     42
			snake_case_      : List[str]      =     0.0
			snake_case_      : str      =     1
			snake_case_      : Optional[int]      =     1
			snake_case_      : Optional[Any]      =     False
			snake_case_      : int      =     False
			snake_case_      : Union[str, Any]      =     jnp.floataa







			def UpperCamelCase			(       self    :			List[str]      ):




									"""simple docstring"""


									_UpperCAmelCase													=     [
									    FlaxResnetBlockaD(
									        in_channels=self.in_channels    ,    out_channels=self.in_channels    ,    dropout_prob=self.dropout    ,    dtype=self.dtype    ,    )
									]

									_UpperCAmelCase													=     []

									for _ in range(self.num_layers      ):
															_UpperCAmelCase													=     FlaxTransformeraDModel(
															    in_channels=self.in_channels    ,    n_heads=self.num_attention_heads    ,    d_head=self.in_channels // self.num_attention_heads    ,    depth=1    ,    use_linear_projection=self.use_linear_projection    ,    use_memory_efficient_attention=self.use_memory_efficient_attention    ,    dtype=self.dtype    ,    )
															attentions.append(_a      )

															_UpperCAmelCase													=     FlaxResnetBlockaD(
															    in_channels=self.in_channels    ,    out_channels=self.in_channels    ,    dropout_prob=self.dropout    ,    dtype=self.dtype    ,    )
															resnets.append(_a      )

									_UpperCAmelCase													=     resnets
									_UpperCAmelCase													=     attentions







			def __call__(       self    :			Union[str, Any]    ,    snake_case__    :			List[Any]    ,    snake_case__    :			Tuple    ,    snake_case__    :			Optional[Any]    ,    snake_case__    :			str=True      ):




									"""simple docstring"""


									_UpperCAmelCase													=     self.resnets[0](_a    ,    _a      )
									for attn, resnet in zip(self.attentions    ,    self.resnets[1:]      ):
															_UpperCAmelCase													=     attn(_a    ,    _a    ,    deterministic=_a      )
															_UpperCAmelCase													=     resnet(_a    ,    _a    ,    deterministic=_a      )

									return hidden_states







'''simple docstring'''
from typing import Optional, Union

import torch
from torch import nn

from ...configuration_utils import ConfigMixin, register_to_config
from ...models.modeling_utils import ModelMixin







class 							A__    (       A__     ,   A__  ):





  @register_to_config
  def __init__(		self      :	Dict							,					_a      :	int = 768							,					)	->    Union[str, Any]:





       '''simple docstring'''






       super().__init__()

       _SCREAMING_SNAKE_CASE							    =nn.Parameter(torch.zeros(1							,					_a ) )
       _SCREAMING_SNAKE_CASE							    =nn.Parameter(torch.ones(1							,					_a ) )





  def 						A  (		self      :	Tuple							,					_a      :	Optional[Union[str, torch.device]] = None							,					_a      :	Optional[torch.dtype] = None							,					)	->    List[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =nn.Parameter(self.mean.to(_a ).to(_a ) )
       _SCREAMING_SNAKE_CASE							    =nn.Parameter(self.std.to(_a ).to(_a ) )
       return self





  def 						A  (		self      :	Tuple							,					_a      :	str )	->    str:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =(embeds - self.mean) * 1.0 / self.std
       return embeds





  def 						A  (		self      :	List[str]							,					_a      :	Optional[Any] )	->    Tuple:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =(embeds * self.std) + self.mean
       return embeds
