





"""simple docstring"""




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		):
							lowerCamelCase  :   int
							lowerCamelCase  :   int
							lowerCamelCase  :   float    =0.0
							lowerCamelCase  :   int    =1
							lowerCamelCase  :   int    =1
							lowerCamelCase  :   bool    =True
							lowerCamelCase  :   bool    =False
							lowerCamelCase  :   bool    =False
							lowerCamelCase  :   bool    =False
							lowerCamelCase  :   jnp.dtype    =jnp.floataa






							def        __a  (  self )      ->					Dict:
								a :						Tuple      =							[]
								a :						int      =							[]

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

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

									a :						Optional[Any]      =							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 )

								a :						List[str]      =							resnets
								a :						Optional[Any]      =							attentions

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






							def __call__(  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__=True )      ->					Any:
								a :						Optional[int]      =							()

								for resnet, attn in zip(self.resnets ,   self.attentions ):
									a :						Optional[Any]      =							resnet(_lowerCAmelCase ,   _lowerCAmelCase ,   deterministic=_lowerCAmelCase )
									a :						int      =							attn(_lowerCAmelCase ,   _lowerCAmelCase ,   deterministic=_lowerCAmelCase )
									output_states += (hidden_states,)

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

								return hidden_states, output_states


class 			__UpperCamelCase							(		nn.Module		):
							lowerCamelCase  :   int
							lowerCamelCase  :   int
							lowerCamelCase  :   float    =0.0
							lowerCamelCase  :   int    =1
							lowerCamelCase  :   bool    =True
							lowerCamelCase  :   jnp.dtype    =jnp.floataa






							def        __a  (  self )      ->					List[Any]:
								a :						List[str]      =							[]

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

									a :						Optional[Any]      =							FlaxResnetBlockaD(
									    in_channels=_lowerCAmelCase ,   out_channels=self.out_channels ,   dropout_prob=self.dropout ,   dtype=self.dtype ,   )
									resnets.append(_lowerCAmelCase )
								a :						List[Any]      =							resnets

								if self.add_downsample:
									a :						Optional[int]      =							FlaxDownsampleaD(self.out_channels ,   dtype=self.dtype )






							def __call__(  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__=True )      ->					str:
								a :						int      =							()

								for resnet in self.resnets:
									a :						List[str]      =							resnet(_lowerCAmelCase ,   _lowerCAmelCase ,   deterministic=_lowerCAmelCase )
									output_states += (hidden_states,)

								if self.add_downsample:
									a :						Union[str, Any]      =							self.downsamplers_a(_lowerCAmelCase )
									output_states += (hidden_states,)

								return hidden_states, output_states


class 			__UpperCamelCase							(		nn.Module		):
							lowerCamelCase  :   int
							lowerCamelCase  :   int
							lowerCamelCase  :   int
							lowerCamelCase  :   float    =0.0
							lowerCamelCase  :   int    =1
							lowerCamelCase  :   int    =1
							lowerCamelCase  :   bool    =True
							lowerCamelCase  :   bool    =False
							lowerCamelCase  :   bool    =False
							lowerCamelCase  :   bool    =False
							lowerCamelCase  :   jnp.dtype    =jnp.floataa






							def        __a  (  self )      ->					int:
								a :						List[str]      =							[]
								a :						List[Any]      =							[]

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

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

									a :						List[str]      =							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 )

								a :						List[Any]      =							resnets
								a :						Union[str, Any]      =							attentions

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






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

									a :						Union[str, Any]      =							resnet(_lowerCAmelCase ,   _lowerCAmelCase ,   deterministic=_lowerCAmelCase )
									a :						Optional[Any]      =							attn(_lowerCAmelCase ,   _lowerCAmelCase ,   deterministic=_lowerCAmelCase )

								if self.add_upsample:
									a :						Tuple      =							self.upsamplers_a(_lowerCAmelCase )

								return hidden_states


class 			__UpperCamelCase							(		nn.Module		):
							lowerCamelCase  :   int
							lowerCamelCase  :   int
							lowerCamelCase  :   int
							lowerCamelCase  :   float    =0.0
							lowerCamelCase  :   int    =1
							lowerCamelCase  :   bool    =True
							lowerCamelCase  :   jnp.dtype    =jnp.floataa






							def        __a  (  self )      ->					Optional[Any]:
								a :						Union[str, Any]      =							[]

								for i in range(self.num_layers ):
									a :						List[str]      =							self.in_channels if (i == self.num_layers - 1) else self.out_channels
									a :						Optional[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(_lowerCAmelCase )

								a :						Optional[int]      =							resnets

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






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

									a :						List[Any]      =							resnet(_lowerCAmelCase ,   _lowerCAmelCase ,   deterministic=_lowerCAmelCase )

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

								return hidden_states


class 			__UpperCamelCase							(		nn.Module		):
							lowerCamelCase  :   int
							lowerCamelCase  :   float    =0.0
							lowerCamelCase  :   int    =1
							lowerCamelCase  :   int    =1
							lowerCamelCase  :   bool    =False
							lowerCamelCase  :   bool    =False
							lowerCamelCase  :   jnp.dtype    =jnp.floataa






							def        __a  (  self )      ->					str:
								# there is always at least one resnet
								a :						Dict      =							[
								    FlaxResnetBlockaD(
								        in_channels=self.in_channels ,   out_channels=self.in_channels ,   dropout_prob=self.dropout ,   dtype=self.dtype ,   )
								]

								a :						Tuple      =							[]

								for _ in range(self.num_layers ):
									a :						List[str]      =							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 )

									a :						Optional[int]      =							FlaxResnetBlockaD(
									    in_channels=self.in_channels ,   out_channels=self.in_channels ,   dropout_prob=self.dropout ,   dtype=self.dtype ,   )
									resnets.append(_lowerCAmelCase )

								a :						Optional[Any]      =							resnets
								a :						Tuple      =							attentions






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

								return hidden_states









'''simple docstring'''


from ...configuration_utils import PretrainedConfig
from ...utils import logging


_SCREAMING_SNAKE_CASE									=				logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE									=				{
    "naver-clova-ix/donut-base": "https://huggingface.co/naver-clova-ix/donut-base/resolve/main/config.json",
    # See all Donut models at https://huggingface.co/models?filter=donut-swin
}

class  lowerCAmelCase_		(		__magic_name__		):
 __lowerCamelCase    :				Union[str, Any]								=       "donut-swin"

 __lowerCamelCase    :				int								=       {
     "num_attention_heads": "num_heads",
     "num_hidden_layers": "num_layers",
 }




 def __init__(						self      ,  _lowerCAmelCase=224      ,  _lowerCAmelCase=4      ,  _lowerCAmelCase=3      ,  _lowerCAmelCase=96      ,  _lowerCAmelCase=[2, 2, 6, 2]      ,  _lowerCAmelCase=[3, 6, 12, 24]      ,  _lowerCAmelCase=7      ,  _lowerCAmelCase=4.0      ,  _lowerCAmelCase=True      ,  _lowerCAmelCase=0.0      ,  _lowerCAmelCase=0.0      ,  _lowerCAmelCase=0.1      ,  _lowerCAmelCase="gelu"      ,  _lowerCAmelCase=False      ,  _lowerCAmelCase=0.02      ,  _lowerCAmelCase=1E-5      ,  **_lowerCAmelCase      ,  )     ->				Optional[Any]:
     super().__init__(**_lowerCAmelCase		)

     _lowerCAmelCase						      =   image_size
     _lowerCAmelCase						      =   patch_size
     _lowerCAmelCase						      =   num_channels
     _lowerCAmelCase						      =   embed_dim
     _lowerCAmelCase						      =   depths
     _lowerCAmelCase						      =   len(_lowerCAmelCase		)
     _lowerCAmelCase						      =   num_heads
     _lowerCAmelCase						      =   window_size
     _lowerCAmelCase						      =   mlp_ratio
     _lowerCAmelCase						      =   qkv_bias
     _lowerCAmelCase						      =   hidden_dropout_prob
     _lowerCAmelCase						      =   attention_probs_dropout_prob
     _lowerCAmelCase						      =   drop_path_rate
     _lowerCAmelCase						      =   hidden_act
     _lowerCAmelCase						      =   use_absolute_embeddings
     _lowerCAmelCase						      =   layer_norm_eps
     _lowerCAmelCase						      =   initializer_range
     # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel
     # this indicates the channel dimension after the last stage of the model
     _lowerCAmelCase						      =   int(embed_dim * 2 ** (len(_lowerCAmelCase		) - 1)		)


