


'''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 					lowercase__						:





							'''simple docstring'''


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






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: List[str]  	=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

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

											_SCREAMING_SNAKE_CASE				: Any  	=   self.get_config()

											return config, pixel_values, labels






							def UpperCAmelCase_			(					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 UpperCAmelCase_			(					self					,      __snake_case					,      __snake_case					,      __snake_case						):
											_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   DonutSwinModel(config=__snake_case						)
											model.to(__snake_case						)
											model.eval()
											_SCREAMING_SNAKE_CASE				: Dict  	=   model(__snake_case						)

											_SCREAMING_SNAKE_CASE				: List[Any]  	=   ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths						) - 1))
											_SCREAMING_SNAKE_CASE				: Tuple  	=   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 UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   self.prepare_config_and_inputs()
											(
											    _SCREAMING_SNAKE_CASE 
											)				: str  	=   config_and_inputs
											_SCREAMING_SNAKE_CASE				: Tuple  	=   {"""pixel_values""": pixel_values}
											return config, inputs_dict





@require_torch
class 					lowercase__						(		_snake_case  ,  _snake_case  ,  unittest.TestCase      ):





							'''simple docstring'''


							A_							:   Tuple   			=						(DonutSwinModel,) if is_torch_available() else ()
							A_							:   Optional[Any]   			=						{"""feature-extraction""": DonutSwinModel} if is_torch_available() else {}
							A_							:   Tuple   			=						True

							A_							:   Tuple   			=						False
							A_							:   List[str]   			=						False
							A_							:   Optional[Any]   			=						False






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: str  	=   DonutSwinModelTester(self						)
											_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   ConfigTester(self					,      config_class=__snake_case					,      embed_dim=37						)






							def UpperCAmelCase_			(					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 UpperCAmelCase_			(					self						):
											return






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: int  	=   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__snake_case						)






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






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: Tuple  	=   self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
															_SCREAMING_SNAKE_CASE				: Any  	=   model_class(__snake_case						)
															self.assertIsInstance(model.get_input_embeddings()					,      (nn.Module)						)
															_SCREAMING_SNAKE_CASE				: Any  	=   model.get_output_embeddings()
															self.assertTrue(x is None or isinstance(__snake_case					,      nn.Linear						)						)






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: List[str]  	=   self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
															_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   model_class(__snake_case						)
															_SCREAMING_SNAKE_CASE				: Optional[int]  	=   inspect.signature(model.forward						)
															# signature.parameters is an OrderedDict => so arg_names order is deterministic
															_SCREAMING_SNAKE_CASE				: Any  	=   [*signature.parameters.keys()]

															_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   ["""pixel_values"""]
															self.assertListEqual(arg_names[:1]					,      __snake_case						)






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: Tuple  	=   self.model_tester.prepare_config_and_inputs_for_common()
											_SCREAMING_SNAKE_CASE				: List[str]  	=   True

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

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

															self.assertListEqual(
															    list(attentions[0].shape[-3:]						)					,      [self.model_tester.num_heads[0], window_size_squared, window_size_squared]					,      )
															_SCREAMING_SNAKE_CASE				: Dict  	=   len(__snake_case						)

															# Check attention is always last and order is fine
															_SCREAMING_SNAKE_CASE				: str  	=   True
															_SCREAMING_SNAKE_CASE				: Any  	=   True
															_SCREAMING_SNAKE_CASE				: Optional[int]  	=   model_class(__snake_case						)
															model.to(__snake_case						)
															model.eval()
															with torch.no_grad():
																			_SCREAMING_SNAKE_CASE				: Any  	=   model(**self._prepare_for_class(__snake_case					,      __snake_case						)						)

															if hasattr(self.model_tester					,      """num_hidden_states_types"""						):
																			_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   self.model_tester.num_hidden_states_types
															else:
																			# also another +1 for reshaped_hidden_states
																			_SCREAMING_SNAKE_CASE				: List[Any]  	=   2
															self.assertEqual(out_len + added_hidden_states					,      len(__snake_case						)						)

															_SCREAMING_SNAKE_CASE				: Optional[int]  	=   outputs.attentions

															self.assertEqual(len(__snake_case						)					,      __snake_case						)

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






							def UpperCAmelCase_			(					self					,      __snake_case					,      __snake_case					,      __snake_case					,      __snake_case						):
											_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   model_class(__snake_case						)
											model.to(__snake_case						)
											model.eval()

											with torch.no_grad():
															_SCREAMING_SNAKE_CASE				: int  	=   model(**self._prepare_for_class(__snake_case					,      __snake_case						)						)

											_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   outputs.hidden_states

											_SCREAMING_SNAKE_CASE				: Tuple  	=   getattr(
											    self.model_tester					,      """expected_num_hidden_layers"""					,      len(self.model_tester.depths						) + 1						)
											self.assertEqual(len(__snake_case						)					,      __snake_case						)

											# DonutSwin has a different seq_length
											_SCREAMING_SNAKE_CASE				: Optional[int]  	=   (
											    config.patch_size
											    if isinstance(config.patch_size					,      collections.abc.Iterable						)
											    else (config.patch_size, config.patch_size)
											)

											_SCREAMING_SNAKE_CASE				: Optional[int]  	=   (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]					,      )

											_SCREAMING_SNAKE_CASE				: Optional[int]  	=   outputs.reshaped_hidden_states
											self.assertEqual(len(__snake_case						)					,      __snake_case						)

											_SCREAMING_SNAKE_CASE				: Tuple  	=   reshaped_hidden_states[0].shape
											_SCREAMING_SNAKE_CASE				: Optional[int]  	=   (
											    reshaped_hidden_states[0].view(__snake_case					,      __snake_case					,      height * width						).permute(0					,      2					,      1						)
											)
											self.assertListEqual(
											    list(reshaped_hidden_states.shape[-2:]						)					,      [num_patches, self.model_tester.embed_dim]					,      )






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   self.model_tester.prepare_config_and_inputs_for_common()

											_SCREAMING_SNAKE_CASE				: Tuple  	=   (
											    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:
															_SCREAMING_SNAKE_CASE				: str  	=   True
															self.check_hidden_states_output(__snake_case					,      __snake_case					,      __snake_case					,      __snake_case						)

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

															self.check_hidden_states_output(__snake_case					,      __snake_case					,      __snake_case					,      __snake_case						)






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: Tuple  	=   self.model_tester.prepare_config_and_inputs_for_common()
											_SCREAMING_SNAKE_CASE				: Tuple  	=   3

											_SCREAMING_SNAKE_CASE				: Optional[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)
											)
											_SCREAMING_SNAKE_CASE				: Tuple  	=   (
											    config.patch_size
											    if isinstance(config.patch_size					,      collections.abc.Iterable						)
											    else (config.patch_size, config.patch_size)
											)

											_SCREAMING_SNAKE_CASE				: Tuple  	=   image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
											_SCREAMING_SNAKE_CASE				: Dict  	=   image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

											for model_class in self.all_model_classes:
															_SCREAMING_SNAKE_CASE				: int  	=   True
															self.check_hidden_states_output(__snake_case					,      __snake_case					,      __snake_case					,      (padded_height, padded_width)						)

															# check that output_hidden_states also work using config
															del inputs_dict["output_hidden_states"]
															_SCREAMING_SNAKE_CASE				: Union[str, Any]  	=   True
															self.check_hidden_states_output(__snake_case					,      __snake_case					,      __snake_case					,      (padded_height, padded_width)						)






							@slow
							def UpperCAmelCase_			(					self						):
											for model_name in DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															_SCREAMING_SNAKE_CASE				: Any  	=   DonutSwinModel.from_pretrained(__snake_case						)
															self.assertIsNotNone(__snake_case						)






							def UpperCAmelCase_			(					self						):
											_SCREAMING_SNAKE_CASE				: Dict  	=   self.model_tester.prepare_config_and_inputs_for_common()

											_SCREAMING_SNAKE_CASE				: str  	=   _config_zero_init(__snake_case						)
											for model_class in self.all_model_classes:
															_SCREAMING_SNAKE_CASE				: Any  	=   model_class(config=__snake_case						)
															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"""					,      )








'''simple docstring'''


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


UpperCAmelCase_	:     Union[str, Any]							     =	{'configuration_pegasus': ['PEGASUS_PRETRAINED_CONFIG_ARCHIVE_MAP', 'PegasusConfig']}

try:
				if not is_sentencepiece_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCAmelCase_	:     Dict							     =	['PegasusTokenizer']

try:
				if not is_tokenizers_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCAmelCase_	:     Optional[int]							     =	['PegasusTokenizerFast']

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCAmelCase_	:     Optional[int]							     =	[
				    'PEGASUS_PRETRAINED_MODEL_ARCHIVE_LIST',
				    'PegasusForCausalLM',
				    'PegasusForConditionalGeneration',
				    'PegasusModel',
				    'PegasusPreTrainedModel',
				]

try:
				if not is_tf_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCAmelCase_	:     Optional[Any]							     =	[
				    'TFPegasusForConditionalGeneration',
				    'TFPegasusModel',
				    'TFPegasusPreTrainedModel',
				]

try:
				if not is_flax_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCAmelCase_	:     Any							     =	[
				    'FlaxPegasusForConditionalGeneration',
				    'FlaxPegasusModel',
				    'FlaxPegasusPreTrainedModel',
				]


if TYPE_CHECKING:
				from .configuration_pegasus import PEGASUS_PRETRAINED_CONFIG_ARCHIVE_MAP, PegasusConfig

				try:
								if not is_sentencepiece_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_pegasus import PegasusTokenizer

				try:
								if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_pegasus_fast import PegasusTokenizerFast

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_pegasus import (
								    PEGASUS_PRETRAINED_MODEL_ARCHIVE_LIST,
								    PegasusForCausalLM,
								    PegasusForConditionalGeneration,
								    PegasusModel,
								    PegasusPreTrainedModel,
								)

				try:
								if not is_tf_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_tf_pegasus import TFPegasusForConditionalGeneration, TFPegasusModel, TFPegasusPreTrainedModel

				try:
								if not is_flax_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_flax_pegasus import (
								    FlaxPegasusForConditionalGeneration,
								    FlaxPegasusModel,
								    FlaxPegasusPreTrainedModel,
								)

else:
				import sys

				UpperCAmelCase_	:     List[str]							     =	_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




