

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

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

class 				UpperCAmelCase_		(			nn.Module   ):
							"""simple docstring"""







							UpperCAmelCase__       :						int
							UpperCAmelCase__       :						int
							UpperCAmelCase__       :						float	    =    0.0
							UpperCAmelCase__       :						int	    =    1
							UpperCAmelCase__       :						int	    =    1
							UpperCAmelCase__       :						bool	    =    True
							UpperCAmelCase__       :						bool	    =    False
							UpperCAmelCase__       :						bool	    =    False
							UpperCAmelCase__       :						bool	    =    False
							UpperCAmelCase__       :						jnp.dtype	    =    jnp.floataa


							def 				__lowercase				(			self       )    ->    Union[str, Any]:
												_a	:    List[Any]         =		[]
												_a	:    List[str]         =		[]

												for i in range(self.num_layers       ):
																	_a	:    List[Any]         =		self.in_channels if i == 0 else self.out_channels

																	_a	:    Union[str, Any]         =		FlaxResnetBlockaD(
																	    in_channels=_a       ,							out_channels=self.out_channels       ,							dropout_prob=self.dropout       ,							dtype=self.dtype       ,							)
																	resnets.append(_a       )

																	_a	:    int         =		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       )

												_a	:    Any         =		resnets
												_a	:    Dict         =		attentions

												if self.add_downsample:
																	_a	:    Optional[Any]         =		FlaxDownsampleaD(self.out_channels       ,							dtype=self.dtype       )




							def __call__(			self       ,							_a       ,							_a       ,							_a       ,							_a=True       )    ->    Dict:
												_a	:    Any         =		()

												for resnet, attn in zip(self.resnets       ,							self.attentions       ):
																	_a	:    Tuple         =		resnet(_a       ,							_a       ,							deterministic=_a       )
																	_a	:    List[str]         =		attn(_a       ,							_a       ,							deterministic=_a       )
																	output_states += (hidden_states,)

												if self.add_downsample:
																	_a	:    List[str]         =		self.downsamplers_a(_a       )
																	output_states += (hidden_states,)

												return hidden_states, output_states



class 				UpperCAmelCase_		(			nn.Module   ):
							"""simple docstring"""







							UpperCAmelCase__       :						int
							UpperCAmelCase__       :						int
							UpperCAmelCase__       :						float	    =    0.0
							UpperCAmelCase__       :						int	    =    1
							UpperCAmelCase__       :						bool	    =    True
							UpperCAmelCase__       :						jnp.dtype	    =    jnp.floataa


							def 				__lowercase				(			self       )    ->    Optional[int]:
												_a	:    List[Any]         =		[]

												for i in range(self.num_layers       ):
																	_a	:    List[str]         =		self.in_channels if i == 0 else self.out_channels

																	_a	:    List[Any]         =		FlaxResnetBlockaD(
																	    in_channels=_a       ,							out_channels=self.out_channels       ,							dropout_prob=self.dropout       ,							dtype=self.dtype       ,							)
																	resnets.append(_a       )
												_a	:    str         =		resnets

												if self.add_downsample:
																	_a	:    Optional[Any]         =		FlaxDownsampleaD(self.out_channels       ,							dtype=self.dtype       )




							def __call__(			self       ,							_a       ,							_a       ,							_a=True       )    ->    Optional[int]:
												_a	:    Dict         =		()

												for resnet in self.resnets:
																	_a	:    Union[str, Any]         =		resnet(_a       ,							_a       ,							deterministic=_a       )
																	output_states += (hidden_states,)

												if self.add_downsample:
																	_a	:    List[str]         =		self.downsamplers_a(_a       )
																	output_states += (hidden_states,)

												return hidden_states, output_states



class 				UpperCAmelCase_		(			nn.Module   ):
							"""simple docstring"""







							UpperCAmelCase__       :						int
							UpperCAmelCase__       :						int
							UpperCAmelCase__       :						int
							UpperCAmelCase__       :						float	    =    0.0
							UpperCAmelCase__       :						int	    =    1
							UpperCAmelCase__       :						int	    =    1
							UpperCAmelCase__       :						bool	    =    True
							UpperCAmelCase__       :						bool	    =    False
							UpperCAmelCase__       :						bool	    =    False
							UpperCAmelCase__       :						bool	    =    False
							UpperCAmelCase__       :						jnp.dtype	    =    jnp.floataa


							def 				__lowercase				(			self       )    ->    List[str]:
												_a	:    Any         =		[]
												_a	:    Any         =		[]

												for i in range(self.num_layers       ):
																	_a	:    Union[str, Any]         =		self.in_channels if (i == self.num_layers - 1) else self.out_channels
																	_a	:    str         =		self.prev_output_channel if i == 0 else self.out_channels

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

																	_a	:    Optional[int]         =		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       )

												_a	:    str         =		resnets
												_a	:    Optional[Any]         =		attentions

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




							def __call__(			self       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a=True       )    ->    Union[str, Any]:
												for resnet, attn in zip(self.resnets       ,							self.attentions       ):
																	# pop res hidden states
																	_a	:    Tuple         =		res_hidden_states_tuple[-1]
																	_a	:    Tuple         =		res_hidden_states_tuple[:-1]
																	_a	:    Tuple         =		jnp.concatenate((hidden_states, res_hidden_states)       ,							axis=-1       )

																	_a	:    List[Any]         =		resnet(_a       ,							_a       ,							deterministic=_a       )
																	_a	:    int         =		attn(_a       ,							_a       ,							deterministic=_a       )

												if self.add_upsample:
																	_a	:    Union[str, Any]         =		self.upsamplers_a(_a       )

												return hidden_states



class 				UpperCAmelCase_		(			nn.Module   ):
							"""simple docstring"""







							UpperCAmelCase__       :						int
							UpperCAmelCase__       :						int
							UpperCAmelCase__       :						int
							UpperCAmelCase__       :						float	    =    0.0
							UpperCAmelCase__       :						int	    =    1
							UpperCAmelCase__       :						bool	    =    True
							UpperCAmelCase__       :						jnp.dtype	    =    jnp.floataa


							def 				__lowercase				(			self       )    ->    Optional[Any]:
												_a	:    Union[str, Any]         =		[]

												for i in range(self.num_layers       ):
																	_a	:    Optional[Any]         =		self.in_channels if (i == self.num_layers - 1) else self.out_channels
																	_a	:    Any         =		self.prev_output_channel if i == 0 else self.out_channels

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

												_a	:    Union[str, Any]         =		resnets

												if self.add_upsample:
																	_a	:    List[Any]         =		FlaxUpsampleaD(self.out_channels       ,							dtype=self.dtype       )




							def __call__(			self       ,							_a       ,							_a       ,							_a       ,							_a=True       )    ->    int:
												for resnet in self.resnets:
																	# pop res hidden states
																	_a	:    List[str]         =		res_hidden_states_tuple[-1]
																	_a	:    Optional[int]         =		res_hidden_states_tuple[:-1]
																	_a	:    Optional[int]         =		jnp.concatenate((hidden_states, res_hidden_states)       ,							axis=-1       )

																	_a	:    Optional[int]         =		resnet(_a       ,							_a       ,							deterministic=_a       )

												if self.add_upsample:
																	_a	:    Optional[int]         =		self.upsamplers_a(_a       )

												return hidden_states



class 				UpperCAmelCase_		(			nn.Module   ):
							"""simple docstring"""







							UpperCAmelCase__       :						int
							UpperCAmelCase__       :						float	    =    0.0
							UpperCAmelCase__       :						int	    =    1
							UpperCAmelCase__       :						int	    =    1
							UpperCAmelCase__       :						bool	    =    False
							UpperCAmelCase__       :						bool	    =    False
							UpperCAmelCase__       :						jnp.dtype	    =    jnp.floataa


							def 				__lowercase				(			self       )    ->    Any:
												# there is always at least one resnet
												_a	:    Any         =		[
												    FlaxResnetBlockaD(
												        in_channels=self.in_channels       ,							out_channels=self.in_channels       ,							dropout_prob=self.dropout       ,							dtype=self.dtype       ,							)
												]

												_a	:    int         =		[]

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

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

												_a	:    Optional[int]         =		resnets
												_a	:    str         =		attentions




							def __call__(			self       ,							_a       ,							_a       ,							_a       ,							_a=True       )    ->    Dict:
												_a	:    List[str]         =		self.resnets[0](_a       ,							_a       )
												for attn, resnet in zip(self.attentions       ,							self.resnets[1:]       ):
																	_a	:    int         =		attn(_a       ,							_a       ,							deterministic=_a       )
																	_a	:    Optional[Any]         =		resnet(_a       ,							_a       ,							deterministic=_a       )

												return hidden_states






import re

import jax.numpy as jnp
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.random import PRNGKey

from ..utils import logging


a__				=		logging.get_logger(__name__)



def    __UpperCAmelCase (	__a      :			Dict	)			->     Tuple:

					"""simple docstring"""

					_a	:    Any         =		R'''\w+[.]\d+'''
					_a	:    Union[str, Any]         =		re.findall(__a		,__a	)
					for pat in pats:
										_a	:    int         =		key.replace(__a		,'''_'''.join(pat.split('''.'''	)	)	)
					return key



def    __UpperCAmelCase (	__a      :			List[str]		,__a      :			Union[str, Any]		,__a      :			Optional[int]	)			->     Tuple:

					"""simple docstring"""

					_a	:    Dict         =		pt_tuple_key[:-1] + ('''scale''',)
					if (
					    any('''norm''' in str_ for str_ in pt_tuple_key	)
					    and (pt_tuple_key[-1] == "bias")
					    and (pt_tuple_key[:-1] + ("bias",) not in random_flax_state_dict)
					    and (pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict)
					):
										_a	:    Dict         =		pt_tuple_key[:-1] + ('''scale''',)
										return renamed_pt_tuple_key, pt_tensor
					elif pt_tuple_key[-1] in ["weight", "gamma"] and pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict:
										_a	:    Optional[int]         =		pt_tuple_key[:-1] + ('''scale''',)
										return renamed_pt_tuple_key, pt_tensor

					# embedding
					if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict:
										_a	:    Union[str, Any]         =		pt_tuple_key[:-1] + ('''embedding''',)
										return renamed_pt_tuple_key, pt_tensor

					# conv layer
					_a	:    Tuple         =		pt_tuple_key[:-1] + ('''kernel''',)
					if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4:
										_a	:    List[str]         =		pt_tensor.transpose(2		,3		,1		,0	)
										return renamed_pt_tuple_key, pt_tensor

					# linear layer
					_a	:    Dict         =		pt_tuple_key[:-1] + ('''kernel''',)
					if pt_tuple_key[-1] == "weight":
										_a	:    Union[str, Any]         =		pt_tensor.T
										return renamed_pt_tuple_key, pt_tensor

					# old PyTorch layer norm weight
					_a	:    Dict         =		pt_tuple_key[:-1] + ('''weight''',)
					if pt_tuple_key[-1] == "gamma":
										return renamed_pt_tuple_key, pt_tensor

					# old PyTorch layer norm bias
					_a	:    Union[str, Any]         =		pt_tuple_key[:-1] + ('''bias''',)
					if pt_tuple_key[-1] == "beta":
										return renamed_pt_tuple_key, pt_tensor

					return pt_tuple_key, pt_tensor







def    __UpperCAmelCase (	__a      :			Dict		,__a      :			str		,__a      :			str=42	)			->     Optional[int]:

					"""simple docstring"""

					_a	:    Tuple         =		{k: v.numpy() for k, v in pt_state_dict.items()}

					# Step 2: Since the model is stateless, get random Flax params
					_a	:    List[Any]         =		flax_model.init_weights(PRNGKey(__a	)	)

					_a	:    Optional[int]         =		flatten_dict(__a	)
					_a	:    Tuple         =		{}

					# Need to change some parameters name to match Flax names
					for pt_key, pt_tensor in pt_state_dict.items():
										_a	:    List[str]         =		rename_key(__a	)
										_a	:    Optional[Any]         =		tuple(renamed_pt_key.split('''.'''	)	)

										# Correctly rename weight parameters
										_a      ,					_a	:    List[str]         =		rename_key_and_reshape_tensor(__a		,__a		,__a	)

										if flax_key in random_flax_state_dict:
															if flax_tensor.shape != random_flax_state_dict[flax_key].shape:
																				raise ValueError(
																				    F"""PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape """
																				    F"""{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}."""	)

        # also add unexpected weight so that warning is thrown
										_a	:    Dict         =		jnp.asarray(__a	)

					return unflatten_dict(__a	)



