


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






  """simple docstring"""





  _SCREAMING_SNAKE_CASE		     =							42
  _SCREAMING_SNAKE_CASE		     =							42
  _SCREAMING_SNAKE_CASE		     =							0.0
  _SCREAMING_SNAKE_CASE		     =							1
  _SCREAMING_SNAKE_CASE		     =							1
  _SCREAMING_SNAKE_CASE		     =							True
  _SCREAMING_SNAKE_CASE		     =							False
  _SCREAMING_SNAKE_CASE		     =							False
  _SCREAMING_SNAKE_CASE		     =							False
  _SCREAMING_SNAKE_CASE		     =							jnp.floataa



  def        lowerCAmelCase__	(  self							:							Dict	) ->			Optional[Any]:




     """simple docstring"""
     snake_case_            =       []
     snake_case_            =       []

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

        snake_case_            =       FlaxResnetBlockaD(
            in_channels=_lowerCAmelCase     ,					out_channels=self.out_channels     ,					dropout_prob=self.dropout     ,					dtype=self.dtype     ,					)
        resnets.append(_lowerCAmelCase	)

        snake_case_            =       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(_lowerCAmelCase	)

     snake_case_            =       resnets
     snake_case_            =       attentions

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



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




     """simple docstring"""
     snake_case_            =       ()

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

     if self.add_downsample:
        snake_case_            =       self.downsamplers_a(_lowerCAmelCase	)
        output_states += (hidden_states,)

     return hidden_states, output_states






class      __lowerCAmelCase  (			nn.Module       ):






  """simple docstring"""





  _SCREAMING_SNAKE_CASE		     =							42
  _SCREAMING_SNAKE_CASE		     =							42
  _SCREAMING_SNAKE_CASE		     =							0.0
  _SCREAMING_SNAKE_CASE		     =							1
  _SCREAMING_SNAKE_CASE		     =							True
  _SCREAMING_SNAKE_CASE		     =							jnp.floataa



  def        lowerCAmelCase__	(  self							:							Optional[Any]	) ->			List[str]:




     """simple docstring"""
     snake_case_            =       []

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

        snake_case_            =       FlaxResnetBlockaD(
            in_channels=_lowerCAmelCase     ,					out_channels=self.out_channels     ,					dropout_prob=self.dropout     ,					dtype=self.dtype     ,					)
        resnets.append(_lowerCAmelCase	)
     snake_case_            =       resnets

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



  def __call__(  self							:							List[str]     ,					_lowerCAmelCase							:							List[str]     ,					_lowerCAmelCase							:							Optional[Any]     ,					_lowerCAmelCase							:							Union[str, Any]=True	) ->			Any:




     """simple docstring"""
     snake_case_            =       ()

     for resnet in self.resnets:
        snake_case_            =       resnet(_lowerCAmelCase     ,					_lowerCAmelCase     ,					deterministic=_lowerCAmelCase	)
        output_states += (hidden_states,)

     if self.add_downsample:
        snake_case_            =       self.downsamplers_a(_lowerCAmelCase	)
        output_states += (hidden_states,)

     return hidden_states, output_states






class      __lowerCAmelCase  (			nn.Module       ):






  """simple docstring"""





  _SCREAMING_SNAKE_CASE		     =							42
  _SCREAMING_SNAKE_CASE		     =							42
  _SCREAMING_SNAKE_CASE		     =							42
  _SCREAMING_SNAKE_CASE		     =							0.0
  _SCREAMING_SNAKE_CASE		     =							1
  _SCREAMING_SNAKE_CASE		     =							1
  _SCREAMING_SNAKE_CASE		     =							True
  _SCREAMING_SNAKE_CASE		     =							False
  _SCREAMING_SNAKE_CASE		     =							False
  _SCREAMING_SNAKE_CASE		     =							False
  _SCREAMING_SNAKE_CASE		     =							jnp.floataa



  def        lowerCAmelCase__	(  self							:							int	) ->			int:




     """simple docstring"""
     snake_case_            =       []
     snake_case_            =       []

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

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

        snake_case_            =       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(_lowerCAmelCase	)

     snake_case_            =       resnets
     snake_case_            =       attentions

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



  def __call__(  self							:							Optional[Any]     ,					_lowerCAmelCase							:							Optional[int]     ,					_lowerCAmelCase							:							Union[str, Any]     ,					_lowerCAmelCase							:							Tuple     ,					_lowerCAmelCase							:							Optional[Any]     ,					_lowerCAmelCase							:							Tuple=True	) ->			List[Any]:




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

        snake_case_            =       resnet(_lowerCAmelCase     ,					_lowerCAmelCase     ,					deterministic=_lowerCAmelCase	)
        snake_case_            =       attn(_lowerCAmelCase     ,					_lowerCAmelCase     ,					deterministic=_lowerCAmelCase	)

     if self.add_upsample:
        snake_case_            =       self.upsamplers_a(_lowerCAmelCase	)

     return hidden_states






class      __lowerCAmelCase  (			nn.Module       ):






  """simple docstring"""





  _SCREAMING_SNAKE_CASE		     =							42
  _SCREAMING_SNAKE_CASE		     =							42
  _SCREAMING_SNAKE_CASE		     =							42
  _SCREAMING_SNAKE_CASE		     =							0.0
  _SCREAMING_SNAKE_CASE		     =							1
  _SCREAMING_SNAKE_CASE		     =							True
  _SCREAMING_SNAKE_CASE		     =							jnp.floataa



  def        lowerCAmelCase__	(  self							:							Optional[Any]	) ->			str:




     """simple docstring"""
     snake_case_            =       []

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

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

     snake_case_            =       resnets

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



  def __call__(  self							:							Tuple     ,					_lowerCAmelCase							:							Optional[Any]     ,					_lowerCAmelCase							:							Dict     ,					_lowerCAmelCase							:							List[str]     ,					_lowerCAmelCase							:							Optional[int]=True	) ->			Tuple:




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

        snake_case_            =       resnet(_lowerCAmelCase     ,					_lowerCAmelCase     ,					deterministic=_lowerCAmelCase	)

     if self.add_upsample:
        snake_case_            =       self.upsamplers_a(_lowerCAmelCase	)

     return hidden_states






class      __lowerCAmelCase  (			nn.Module       ):






  """simple docstring"""





  _SCREAMING_SNAKE_CASE		     =							42
  _SCREAMING_SNAKE_CASE		     =							0.0
  _SCREAMING_SNAKE_CASE		     =							1
  _SCREAMING_SNAKE_CASE		     =							1
  _SCREAMING_SNAKE_CASE		     =							False
  _SCREAMING_SNAKE_CASE		     =							False
  _SCREAMING_SNAKE_CASE		     =							jnp.floataa



  def        lowerCAmelCase__	(  self							:							Any	) ->			int:




     """simple docstring"""
     # there is always at least one resnet
     snake_case_            =       [
         FlaxResnetBlockaD(
             in_channels=self.in_channels     ,					out_channels=self.in_channels     ,					dropout_prob=self.dropout     ,					dtype=self.dtype     ,					)
     ]

     snake_case_            =       []

     for _ in range(self.num_layers	):
        snake_case_            =       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(_lowerCAmelCase	)

        snake_case_            =       FlaxResnetBlockaD(
            in_channels=self.in_channels     ,					out_channels=self.in_channels     ,					dropout_prob=self.dropout     ,					dtype=self.dtype     ,					)
        resnets.append(_lowerCAmelCase	)

     snake_case_            =       resnets
     snake_case_            =       attentions



  def __call__(  self							:							Union[str, Any]     ,					_lowerCAmelCase							:							Tuple     ,					_lowerCAmelCase							:							Union[str, Any]     ,					_lowerCAmelCase							:							Union[str, Any]     ,					_lowerCAmelCase							:							str=True	) ->			Dict:




     """simple docstring"""
     snake_case_            =       self.resnets[0](_lowerCAmelCase     ,					_lowerCAmelCase	)
     for attn, resnet in zip(self.attentions     ,					self.resnets[1:]	):
        snake_case_            =       attn(_lowerCAmelCase     ,					_lowerCAmelCase     ,					deterministic=_lowerCAmelCase	)
        snake_case_            =       resnet(_lowerCAmelCase     ,					_lowerCAmelCase     ,					deterministic=_lowerCAmelCase	)

     return hidden_states








import importlib.metadata
from typing import Union

from packaging.version import Version, parse

from .constants import STR_OPERATION_TO_FUNC


SCREAMING_SNAKE_CASE						:Union[str, Any]			=  parse(importlib.metadata.version('''torch'''))



def       _lowerCAmelCase    (				lowerCAmelCase_				:Union[str, Version]		,    lowerCAmelCase_				:str		,    lowerCAmelCase_				:str				)->Any:
   '''simple docstring'''






   if operation not in STR_OPERATION_TO_FUNC.keys():
      raise ValueError(F'''`operation` must be one of {list(STR_OPERATION_TO_FUNC.keys()				)}, received {operation}'''				)
   snake_case_            =       STR_OPERATION_TO_FUNC[operation]
   if isinstance(lowerCAmelCase_		,    lowerCAmelCase_				):
      snake_case_            =       parse(importlib.metadata.version(lowerCAmelCase_				)				)
   return operation(lowerCAmelCase_		,    parse(lowerCAmelCase_				)				)



def       _lowerCAmelCase    (				lowerCAmelCase_				:str		,    lowerCAmelCase_				:str				)->Any:
   '''simple docstring'''






   return compare_versions(lowerCAmelCase_		,    lowerCAmelCase_		,    lowerCAmelCase_				)




