



'''simple docstring'''







from dataclasses import dataclass
from typing import Any, Dict, Optional

import torch
import torch.nn.functional as F
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..models.embeddings import ImagePositionalEmbeddings
from ..utils import BaseOutput, deprecate
from .attention import BasicTransformerBlock
from .embeddings import PatchEmbed
from .modeling_utils import ModelMixin




@dataclass
class 				__lowerCAmelCase(							snake_case__      ):
			__snake_case  :		Tuple		=      4_2




class 				__lowerCAmelCase(							snake_case__		,			snake_case__      ):






			@register_to_config
			def __init__(   self  :							int      ,       SCREAMING_SNAKE_CASE  :							str = 16      ,       SCREAMING_SNAKE_CASE  :							Tuple = 88      ,       SCREAMING_SNAKE_CASE  :							List[Any] = None      ,       SCREAMING_SNAKE_CASE  :							Optional[int] = None      ,       SCREAMING_SNAKE_CASE  :							Any = 1      ,       SCREAMING_SNAKE_CASE  :							Any = 0.0      ,       SCREAMING_SNAKE_CASE  :							List[str] = 32      ,       SCREAMING_SNAKE_CASE  :							Tuple = None      ,       SCREAMING_SNAKE_CASE  :							Optional[int] = False      ,       SCREAMING_SNAKE_CASE  :							Any = None      ,       SCREAMING_SNAKE_CASE  :							str = None      ,       SCREAMING_SNAKE_CASE  :							Optional[Any] = None      ,       SCREAMING_SNAKE_CASE  :							Tuple = "geglu"      ,       SCREAMING_SNAKE_CASE  :							Union[str, Any] = None      ,       SCREAMING_SNAKE_CASE  :							Optional[Any] = False      ,       SCREAMING_SNAKE_CASE  :							Tuple = False      ,       SCREAMING_SNAKE_CASE  :							str = False      ,       SCREAMING_SNAKE_CASE  :							str = "layer_norm"      ,       SCREAMING_SNAKE_CASE  :							str = True      ,       ):




					"""simple docstring"""







					super().__init__()
					SCREAMING_SNAKE_CASE_						:List[str]										=      use_linear_projection
					SCREAMING_SNAKE_CASE_						:Tuple										=      num_attention_heads
					SCREAMING_SNAKE_CASE_						:Optional[int]										=      attention_head_dim
					SCREAMING_SNAKE_CASE_						:Any										=      num_attention_heads * attention_head_dim

					# 1. Transformer2DModel can process both standard continuous images of shape `(batch_size, num_channels, width, height)` as well as quantized image embeddings of shape `(batch_size, num_image_vectors)`
					# Define whether input is continuous or discrete depending on configuration
					SCREAMING_SNAKE_CASE_						:List[str]										=      (in_channels is not None) and (patch_size is None)
					SCREAMING_SNAKE_CASE_						:List[Any]										=      num_vector_embeds is not None
					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      in_channels is not None and patch_size is not None

					if norm_type == "layer_norm" and num_embeds_ada_norm is not None:
							SCREAMING_SNAKE_CASE_						:Optional[int]										=      (
							    f'The configuration file of this model: {self.__class__} is outdated. `norm_type` is either not set or'
							    " incorrectly set to `'layer_norm'`.Make sure to set `norm_type` to `'ada_norm'` in the config."
							    " Please make sure to update the config accordingly as leaving `norm_type` might led to incorrect"
							    " results in future versions. If you have downloaded this checkpoint from the Hugging Face Hub, it"
							    " would be very nice if you could open a Pull request for the `transformer/config.json` file"
							)
							deprecate('norm_type!=num_embeds_ada_norm'      ,       '1.0.0'      ,       lowercase_      ,       standard_warn=lowercase_	)
							SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      "ada_norm"

					if self.is_input_continuous and self.is_input_vectorized:
							raise ValueError(
							    f'Cannot define both `in_channels`: {in_channels} and `num_vector_embeds`: {num_vector_embeds}. Make'
							    ' sure that either `in_channels` or `num_vector_embeds` is None.'	)
					elif self.is_input_vectorized and self.is_input_patches:
							raise ValueError(
							    f'Cannot define both `num_vector_embeds`: {num_vector_embeds} and `patch_size`: {patch_size}. Make'
							    ' sure that either `num_vector_embeds` or `num_patches` is None.'	)
					elif not self.is_input_continuous and not self.is_input_vectorized and not self.is_input_patches:
							raise ValueError(
							    f'Has to define `in_channels`: {in_channels}, `num_vector_embeds`: {num_vector_embeds}, or patch_size:'
							    f' {patch_size}. Make sure that `in_channels`, `num_vector_embeds` or `num_patches` is not None.'	)

					# 2. Define input layers
					if self.is_input_continuous:
							SCREAMING_SNAKE_CASE_						:Optional[Any]										=      in_channels

							SCREAMING_SNAKE_CASE_						:Dict										=      torch.nn.GroupNorm(num_groups=lowercase_      ,       num_channels=lowercase_      ,       eps=1E-6      ,       affine=lowercase_	)
							if use_linear_projection:
									SCREAMING_SNAKE_CASE_						:Dict										=      nn.Linear(lowercase_      ,       lowercase_	)
							else:
									SCREAMING_SNAKE_CASE_						:Tuple										=      nn.Convad(lowercase_      ,       lowercase_      ,       kernel_size=1      ,       stride=1      ,       padding=0	)
					elif self.is_input_vectorized:
							assert sample_size is not None, "Transformer2DModel over discrete input must provide sample_size"
							assert num_vector_embeds is not None, "Transformer2DModel over discrete input must provide num_embed"

							SCREAMING_SNAKE_CASE_						:Optional[Any]										=      sample_size
							SCREAMING_SNAKE_CASE_						:str										=      sample_size
							SCREAMING_SNAKE_CASE_						:List[str]										=      num_vector_embeds
							SCREAMING_SNAKE_CASE_						:Tuple										=      self.height * self.width

							SCREAMING_SNAKE_CASE_						:Dict										=      ImagePositionalEmbeddings(
							    num_embed=lowercase_      ,       embed_dim=lowercase_      ,       height=self.height      ,       width=self.width	)
					elif self.is_input_patches:
							assert sample_size is not None, "Transformer2DModel over patched input must provide sample_size"

							SCREAMING_SNAKE_CASE_						:Dict										=      sample_size
							SCREAMING_SNAKE_CASE_						:Optional[Any]										=      sample_size

							SCREAMING_SNAKE_CASE_						:Dict										=      patch_size
							SCREAMING_SNAKE_CASE_						:Optional[int]										=      PatchEmbed(
							    height=lowercase_      ,       width=lowercase_      ,       patch_size=lowercase_      ,       in_channels=lowercase_      ,       embed_dim=lowercase_      ,       )

					# 3. Define transformers blocks
					SCREAMING_SNAKE_CASE_						:Dict										=      nn.ModuleList(
					    [
					        BasicTransformerBlock(
					            lowercase_      ,       lowercase_      ,       lowercase_      ,       dropout=lowercase_      ,       cross_attention_dim=lowercase_      ,       activation_fn=lowercase_      ,       num_embeds_ada_norm=lowercase_      ,       attention_bias=lowercase_      ,       only_cross_attention=lowercase_      ,       upcast_attention=lowercase_      ,       norm_type=lowercase_      ,       norm_elementwise_affine=lowercase_      ,       )
					        for d in range(lowercase_	)
					    ]	)

					# 4. Define output layers
					SCREAMING_SNAKE_CASE_						:Optional[int]										=      in_channels if out_channels is None else out_channels
					if self.is_input_continuous:
							# TODO: should use out_channels for continuous projections
							if use_linear_projection:
									SCREAMING_SNAKE_CASE_						:Optional[int]										=      nn.Linear(lowercase_      ,       lowercase_	)
							else:
									SCREAMING_SNAKE_CASE_						:Dict										=      nn.Convad(lowercase_      ,       lowercase_      ,       kernel_size=1      ,       stride=1      ,       padding=0	)
					elif self.is_input_vectorized:
							SCREAMING_SNAKE_CASE_						:Dict										=      nn.LayerNorm(lowercase_	)
							SCREAMING_SNAKE_CASE_						:Optional[int]										=      nn.Linear(lowercase_      ,       self.num_vector_embeds - 1	)
					elif self.is_input_patches:
							SCREAMING_SNAKE_CASE_						:int										=      nn.LayerNorm(lowercase_      ,       elementwise_affine=lowercase_      ,       eps=1E-6	)
							SCREAMING_SNAKE_CASE_						:str										=      nn.Linear(lowercase_      ,       2 * inner_dim	)
							SCREAMING_SNAKE_CASE_						:List[Any]										=      nn.Linear(lowercase_      ,       patch_size * patch_size * self.out_channels	)






			def 				_lowercase	(   self  :							Any      ,       SCREAMING_SNAKE_CASE  :							Optional[Any]      ,       SCREAMING_SNAKE_CASE  :							Tuple = None      ,       SCREAMING_SNAKE_CASE  :							Tuple = None      ,       SCREAMING_SNAKE_CASE  :							Dict = None      ,       SCREAMING_SNAKE_CASE  :							str = None      ,       SCREAMING_SNAKE_CASE  :							List[str] = None      ,       SCREAMING_SNAKE_CASE  :							Optional[Any] = None      ,       SCREAMING_SNAKE_CASE  :							List[str] = True      ,       ):




					"""simple docstring"""







					if attention_mask is not None and attention_mask.ndim == 2:
							# assume that mask is expressed as:
							#   (1 = keep,      0 = discard)
							# convert mask into a bias that can be added to attention scores:
							#       (keep = +0,     discard = -10000.0)
							SCREAMING_SNAKE_CASE_						:int										=      (1 - attention_mask.to(hidden_states.dtype	)) * -10_000.0
							SCREAMING_SNAKE_CASE_						:Optional[int]										=      attention_mask.unsqueeze(1	)

					# convert encoder_attention_mask to a bias the same way we do for attention_mask
					if encoder_attention_mask is not None and encoder_attention_mask.ndim == 2:
							SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      (1 - encoder_attention_mask.to(hidden_states.dtype	)) * -10_000.0
							SCREAMING_SNAKE_CASE_						:List[Any]										=      encoder_attention_mask.unsqueeze(1	)

					# 1. Input
					if self.is_input_continuous:
							SCREAMING_SNAKE_CASE_						:str										=      hidden_states.shape
							SCREAMING_SNAKE_CASE_						:Optional[int]										=      hidden_states

							SCREAMING_SNAKE_CASE_						:str										=      self.norm(lowercase_	)
							if not self.use_linear_projection:
									SCREAMING_SNAKE_CASE_						:int										=      self.proj_in(lowercase_	)
									SCREAMING_SNAKE_CASE_						:List[str]										=      hidden_states.shape[1]
									SCREAMING_SNAKE_CASE_						:Optional[int]										=      hidden_states.permute(0      ,       2      ,       3      ,       1	).reshape(lowercase_      ,       height * width      ,       lowercase_	)
							else:
									SCREAMING_SNAKE_CASE_						:List[str]										=      hidden_states.shape[1]
									SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      hidden_states.permute(0      ,       2      ,       3      ,       1	).reshape(lowercase_      ,       height * width      ,       lowercase_	)
									SCREAMING_SNAKE_CASE_						:List[Any]										=      self.proj_in(lowercase_	)
					elif self.is_input_vectorized:
							SCREAMING_SNAKE_CASE_						:Optional[Any]										=      self.latent_image_embedding(lowercase_	)
					elif self.is_input_patches:
							SCREAMING_SNAKE_CASE_						:Dict										=      self.pos_embed(lowercase_	)

					# 2. Blocks
					for block in self.transformer_blocks:
							SCREAMING_SNAKE_CASE_						:List[str]										=      block(
							    lowercase_      ,       attention_mask=lowercase_      ,       encoder_hidden_states=lowercase_      ,       encoder_attention_mask=lowercase_      ,       timestep=lowercase_      ,       cross_attention_kwargs=lowercase_      ,       class_labels=lowercase_      ,       )

					# 3. Output
					if self.is_input_continuous:
							if not self.use_linear_projection:
									SCREAMING_SNAKE_CASE_						:Dict										=      hidden_states.reshape(lowercase_      ,       lowercase_      ,       lowercase_      ,       lowercase_	).permute(0      ,       3      ,       1      ,       2	).contiguous()
									SCREAMING_SNAKE_CASE_						:Optional[Any]										=      self.proj_out(lowercase_	)
							else:
									SCREAMING_SNAKE_CASE_						:str										=      self.proj_out(lowercase_	)
									SCREAMING_SNAKE_CASE_						:Tuple										=      hidden_states.reshape(lowercase_      ,       lowercase_      ,       lowercase_      ,       lowercase_	).permute(0      ,       3      ,       1      ,       2	).contiguous()

							SCREAMING_SNAKE_CASE_						:int										=      hidden_states + residual
					elif self.is_input_vectorized:
							SCREAMING_SNAKE_CASE_						:str										=      self.norm_out(lowercase_	)
							SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      self.out(lowercase_	)
							# (batch, self.num_vector_embeds - 1, self.num_latent_pixels)
							SCREAMING_SNAKE_CASE_						:Any										=      logits.permute(0      ,       2      ,       1	)

							# log(p(x_0))
							SCREAMING_SNAKE_CASE_						:int										=      F.log_softmax(logits.double()      ,       dim=1	).float()
					elif self.is_input_patches:
							# TODO: cleanup!
							SCREAMING_SNAKE_CASE_						:List[str]										=      self.transformer_blocks[0].norma.emb(
							    lowercase_      ,       lowercase_      ,       hidden_dtype=hidden_states.dtype	)
							SCREAMING_SNAKE_CASE_						:Optional[int]										=      self.proj_out_a(F.silu(lowercase_	)	).chunk(2      ,       dim=1	)
							SCREAMING_SNAKE_CASE_						:Dict										=      self.norm_out(lowercase_	) * (1 + scale[:, None]) + shift[:, None]
							SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      self.proj_out_a(lowercase_	)

							# unpatchify
							SCREAMING_SNAKE_CASE_						:str										=      int(hidden_states.shape[1] ** 0.5	)
							SCREAMING_SNAKE_CASE_						:Dict										=      hidden_states.reshape(
							    shape=(-1, height, width, self.patch_size, self.patch_size, self.out_channels)	)
							SCREAMING_SNAKE_CASE_						:Dict										=      torch.einsum('nhwpqc->nchpwq'      ,       lowercase_	)
							SCREAMING_SNAKE_CASE_						:List[str]										=      hidden_states.reshape(
							    shape=(-1, self.out_channels, height * self.patch_size, width * self.patch_size)	)

					if not return_dict:
							return (output,)

					return TransformeraDModelOutput(sample=lowercase_	)


'''simple docstring'''
import collections
import inspect
import unittest

from transformers import DonutSwinConfig
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import is_torch_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch
  from torch import nn

  from transformers import DonutSwinModel
  from transformers.models.donut.modeling_donut_swin import DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST






class  UpperCAmelCase_					:





 def __init__(     self			,							lowercase_			,							lowercase_=13			,							lowercase_=32			,							lowercase_=2			,							lowercase_=3			,							lowercase_=16			,							lowercase_=[1, 2, 1]			,							lowercase_=[2, 2, 4]			,							lowercase_=2			,							lowercase_=2.0			,							lowercase_=True			,							lowercase_=0.0			,							lowercase_=0.0			,							lowercase_=0.1			,							lowercase_="gelu"			,							lowercase_=False			,							lowercase_=True			,							lowercase_=0.02			,							lowercase_=1E-5			,							lowercase_=True			,							lowercase_=None			,							lowercase_=True			,							lowercase_=10			,							lowercase_=8			,							):
     snake_case_			:     List[Any]        =    parent
     snake_case_			:     str        =    batch_size
     snake_case_			:     str        =    image_size
     snake_case_			:     Any        =    patch_size
     snake_case_			:     Any        =    num_channels
     snake_case_			:     Any        =    embed_dim
     snake_case_			:     Dict        =    depths
     snake_case_			:     Union[str, Any]        =    num_heads
     snake_case_			:     Dict        =    window_size
     snake_case_			:     str        =    mlp_ratio
     snake_case_			:     Optional[int]        =    qkv_bias
     snake_case_			:     Tuple        =    hidden_dropout_prob
     snake_case_			:     str        =    attention_probs_dropout_prob
     snake_case_			:     int        =    drop_path_rate
     snake_case_			:     List[str]        =    hidden_act
     snake_case_			:     int        =    use_absolute_embeddings
     snake_case_			:     Union[str, Any]        =    patch_norm
     snake_case_			:     Any        =    layer_norm_eps
     snake_case_			:     Optional[int]        =    initializer_range
     snake_case_			:     Optional[Any]        =    is_training
     snake_case_			:     Union[str, Any]        =    scope
     snake_case_			:     Dict        =    use_labels
     snake_case_			:     List[Any]        =    type_sequence_label_size
     snake_case_			:     Dict        =    encoder_stride





 def 							snake_case__		(     self):
     snake_case_			:     str        =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

     snake_case_			:     Tuple        =    None
     if self.use_labels:
         snake_case_			:     Union[str, Any]        =    ids_tensor([self.batch_size]			,							self.type_sequence_label_size)

     snake_case_			:     Any        =    self.get_config()

     return config, pixel_values, labels





 def 							snake_case__		(     self):
     return DonutSwinConfig(
         image_size=self.image_size			,							patch_size=self.patch_size			,							num_channels=self.num_channels			,							embed_dim=self.embed_dim			,							depths=self.depths			,							num_heads=self.num_heads			,							window_size=self.window_size			,							mlp_ratio=self.mlp_ratio			,							qkv_bias=self.qkv_bias			,							hidden_dropout_prob=self.hidden_dropout_prob			,							attention_probs_dropout_prob=self.attention_probs_dropout_prob			,							drop_path_rate=self.drop_path_rate			,							hidden_act=self.hidden_act			,							use_absolute_embeddings=self.use_absolute_embeddings			,							path_norm=self.patch_norm			,							layer_norm_eps=self.layer_norm_eps			,							initializer_range=self.initializer_range			,							encoder_stride=self.encoder_stride			,							)





 def 							snake_case__		(     self			,							lowercase_			,							lowercase_			,							lowercase_):
     snake_case_			:     str        =    DonutSwinModel(config=lowercase_)
     model.to(lowercase_)
     model.eval()
     snake_case_			:     Union[str, Any]        =    model(lowercase_)

     snake_case_			:     Tuple        =    ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths) - 1))
     snake_case_			:     Any        =    int(config.embed_dim * 2 ** (len(config.depths) - 1))

     self.parent.assertEqual(result.last_hidden_state.shape			,							(self.batch_size, expected_seq_len, expected_dim))





 def 							snake_case__		(     self):
     snake_case_			:     Tuple        =    self.prepare_config_and_inputs()
     (
         (
         snake_case_
     )      ,			(
         snake_case_
     )      ,			(
         snake_case_
     )      ,			
     )			:     Optional[int]        =    config_and_inputs
     snake_case_			:     Dict        =    {"pixel_values": pixel_values}
     return config, inputs_dict






@require_torch
class  UpperCAmelCase_					(						snake_case__     ,    snake_case__     ,    unittest.TestCase       ):
 UpperCAmelCase_		     =    (DonutSwinModel,) if is_torch_available() else ()
 UpperCAmelCase_		     =    {"""feature-extraction""": DonutSwinModel} if is_torch_available() else {}
 UpperCAmelCase_		     =    True

 UpperCAmelCase_		     =    False
 UpperCAmelCase_		     =    False
 UpperCAmelCase_		     =    False





 def 							snake_case__		(     self):
     snake_case_			:     Union[str, Any]        =    DonutSwinModelTester(self)
     snake_case_			:     Optional[Any]        =    ConfigTester(self			,							config_class=lowercase_			,							embed_dim=37)





 def 							snake_case__		(     self):
     self.create_and_test_config_common_properties()
     self.config_tester.create_and_test_config_to_json_string()
     self.config_tester.create_and_test_config_to_json_file()
     self.config_tester.create_and_test_config_from_and_save_pretrained()
     self.config_tester.create_and_test_config_with_num_labels()
     self.config_tester.check_config_can_be_init_without_params()
     self.config_tester.check_config_arguments_init()





 def 							snake_case__		(     self):
     return





 def 							snake_case__		(     self):
     snake_case_			:     int        =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowercase_)





 def 							snake_case__		(     self):
     # DonutSwin does not use inputs_embeds
     pass





 def 							snake_case__		(     self):
     snake_case_      ,			snake_case_			:     Tuple        =    self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         snake_case_			:     Any        =    model_class(lowercase_)
         self.assertIsInstance(model.get_input_embeddings()			,							(nn.Module))
         snake_case_			:     List[str]        =    model.get_output_embeddings()
         self.assertTrue(x is None or isinstance(lowercase_			,							nn.Linear))





 def 							snake_case__		(     self):
     snake_case_      ,			snake_case_			:     Optional[Any]        =    self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         snake_case_			:     Optional[Any]        =    model_class(lowercase_)
         snake_case_			:     Any        =    inspect.signature(model.forward)
         # signature.parameters is an OrderedDict => so arg_names order is deterministic
         snake_case_			:     Union[str, Any]        =    [*signature.parameters.keys()]

         snake_case_			:     Any        =    ["pixel_values"]
         self.assertListEqual(arg_names[:1]			,							lowercase_)





 def 							snake_case__		(     self):
     snake_case_      ,			snake_case_			:     Union[str, Any]        =    self.model_tester.prepare_config_and_inputs_for_common()
     snake_case_			:     Union[str, Any]        =    True

     for model_class in self.all_model_classes:
         snake_case_			:     Union[str, Any]        =    True
         snake_case_			:     Tuple        =    False
         snake_case_			:     Dict        =    True
         snake_case_			:     int        =    model_class(lowercase_)
         model.to(lowercase_)
         model.eval()
         with torch.no_grad():
             snake_case_			:     str        =    model(**self._prepare_for_class(lowercase_			,							lowercase_))
         snake_case_			:     Dict        =    outputs.attentions
         snake_case_			:     Union[str, Any]        =    len(self.model_tester.depths)
         self.assertEqual(len(lowercase_)			,							lowercase_)

         # check that output_attentions also work using config
         del inputs_dict["output_attentions"]
         snake_case_			:     Optional[Any]        =    True
         snake_case_			:     Tuple        =    config.window_size**2
         snake_case_			:     List[str]        =    model_class(lowercase_)
         model.to(lowercase_)
         model.eval()
         with torch.no_grad():
             snake_case_			:     Tuple        =    model(**self._prepare_for_class(lowercase_			,							lowercase_))
         snake_case_			:     Tuple        =    outputs.attentions
         self.assertEqual(len(lowercase_)			,							lowercase_)

         self.assertListEqual(
             list(attentions[0].shape[-3:])			,							[self.model_tester.num_heads[0], window_size_squared, window_size_squared]			,							)
         snake_case_			:     Any        =    len(lowercase_)

         # Check attention is always last and order is fine
         snake_case_			:     Union[str, Any]        =    True
         snake_case_			:     Union[str, Any]        =    True
         snake_case_			:     List[Any]        =    model_class(lowercase_)
         model.to(lowercase_)
         model.eval()
         with torch.no_grad():
             snake_case_			:     Tuple        =    model(**self._prepare_for_class(lowercase_			,							lowercase_))

         if hasattr(self.model_tester			,							"num_hidden_states_types"):
             snake_case_			:     Dict        =    self.model_tester.num_hidden_states_types
         else:
             # also another +1 for reshaped_hidden_states
             snake_case_			:     Dict        =    2
         self.assertEqual(out_len + added_hidden_states			,							len(lowercase_))

         snake_case_			:     Any        =    outputs.attentions

         self.assertEqual(len(lowercase_)			,							lowercase_)

         self.assertListEqual(
             list(self_attentions[0].shape[-3:])			,							[self.model_tester.num_heads[0], window_size_squared, window_size_squared]			,							)





 def 							snake_case__		(     self			,							lowercase_			,							lowercase_			,							lowercase_			,							lowercase_):
     snake_case_			:     Union[str, Any]        =    model_class(lowercase_)
     model.to(lowercase_)
     model.eval()

     with torch.no_grad():
         snake_case_			:     List[str]        =    model(**self._prepare_for_class(lowercase_			,							lowercase_))

     snake_case_			:     Dict        =    outputs.hidden_states

     snake_case_			:     Dict        =    getattr(
         self.model_tester			,							"expected_num_hidden_layers"			,							len(self.model_tester.depths) + 1)
     self.assertEqual(len(lowercase_)			,							lowercase_)

     # DonutSwin has a different seq_length
     snake_case_			:     List[Any]        =    (
         config.patch_size
         if isinstance(config.patch_size			,							collections.abc.Iterable)
         else (config.patch_size, config.patch_size)
     )

     snake_case_			:     str        =    (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

     self.assertListEqual(
         list(hidden_states[0].shape[-2:])			,							[num_patches, self.model_tester.embed_dim]			,							)

     snake_case_			:     str        =    outputs.reshaped_hidden_states
     self.assertEqual(len(lowercase_)			,							lowercase_)

     snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_			:     Dict        =    reshaped_hidden_states[0].shape
     snake_case_			:     Union[str, Any]        =    (
         reshaped_hidden_states[0].view(lowercase_			,							lowercase_			,							height * width).permute(0			,							2			,							1)
     )
     self.assertListEqual(
         list(reshaped_hidden_states.shape[-2:])			,							[num_patches, self.model_tester.embed_dim]			,							)





 def 							snake_case__		(     self):
     snake_case_      ,			snake_case_			:     int        =    self.model_tester.prepare_config_and_inputs_for_common()

     snake_case_			:     Union[str, Any]        =    (
         self.model_tester.image_size
         if isinstance(self.model_tester.image_size			,							collections.abc.Iterable)
         else (self.model_tester.image_size, self.model_tester.image_size)
     )

     for model_class in self.all_model_classes:
         snake_case_			:     List[str]        =    True
         self.check_hidden_states_output(lowercase_			,							lowercase_			,							lowercase_			,							lowercase_)

         # check that output_hidden_states also work using config
         del inputs_dict["output_hidden_states"]
         snake_case_			:     List[Any]        =    True

         self.check_hidden_states_output(lowercase_			,							lowercase_			,							lowercase_			,							lowercase_)





 def 							snake_case__		(     self):
     snake_case_      ,			snake_case_			:     List[str]        =    self.model_tester.prepare_config_and_inputs_for_common()
     snake_case_			:     Any        =    3

     snake_case_			:     int        =    (
         self.model_tester.image_size
         if isinstance(self.model_tester.image_size			,							collections.abc.Iterable)
         else (self.model_tester.image_size, self.model_tester.image_size)
     )
     snake_case_			:     int        =    (
         config.patch_size
         if isinstance(config.patch_size			,							collections.abc.Iterable)
         else (config.patch_size, config.patch_size)
     )

     snake_case_			:     str        =    image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
     snake_case_			:     List[str]        =    image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

     for model_class in self.all_model_classes:
         snake_case_			:     str        =    True
         self.check_hidden_states_output(lowercase_			,							lowercase_			,							lowercase_			,							(padded_height, padded_width))

         # check that output_hidden_states also work using config
         del inputs_dict["output_hidden_states"]
         snake_case_			:     Dict        =    True
         self.check_hidden_states_output(lowercase_			,							lowercase_			,							lowercase_			,							(padded_height, padded_width))





 @slow
 def 							snake_case__		(     self):
     for model_name in DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         snake_case_			:     List[str]        =    DonutSwinModel.from_pretrained(lowercase_)
         self.assertIsNotNone(lowercase_)





 def 							snake_case__		(     self):
     snake_case_      ,			snake_case_			:     Union[str, Any]        =    self.model_tester.prepare_config_and_inputs_for_common()

     snake_case_			:     Dict        =    _config_zero_init(lowercase_)
     for model_class in self.all_model_classes:
         snake_case_			:     str        =    model_class(config=lowercase_)
         for name, param in model.named_parameters():
             if "embeddings" not in name and param.requires_grad:
                 self.assertIn(
                     ((param.data.mean() * 1E9).round() / 1E9).item()			,							[0.0, 1.0]			,							msg=F'Parameter {name} of model {model_class} seems not properly initialized'			,							)




