


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




     '''simple docstring'''


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


     def 			_lowerCamelCase			(						self)      ->    Optional[Any]:
            _A				:	int       =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

            _A				:	List[str]       =			None
            if self.use_labels:
                   _A				:	Optional[Any]       =			ids_tensor([self.batch_size]			,						self.type_sequence_label_size)

            _A				:	Optional[Any]       =			self.get_config()

            return config, pixel_values, labels


     def 			_lowerCamelCase			(						self)      ->    List[str]:
            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 			_lowerCamelCase			(						self			,						__lowerCamelCase			,						__lowerCamelCase			,						__lowerCamelCase)      ->    Dict:
            _A				:	List[str]       =			DonutSwinModel(config=__lowerCamelCase)
            model.to(__lowerCamelCase)
            model.eval()
            _A				:	Optional[int]       =			model(__lowerCamelCase)

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







@require_torch
class 					lowerCAmelCase__	(    a					,   a					,   unittest.TestCase):




     '''simple docstring'''
     __SCREAMING_SNAKE_CASE        =			(DonutSwinModel,) if is_torch_available() else ()
     __SCREAMING_SNAKE_CASE        =			{"feature-extraction": DonutSwinModel} if is_torch_available() else {}
     __SCREAMING_SNAKE_CASE        =			True

     __SCREAMING_SNAKE_CASE        =			False
     __SCREAMING_SNAKE_CASE        =			False
     __SCREAMING_SNAKE_CASE        =			False


     def 			_lowerCamelCase			(						self)      ->    Dict:
            _A				:	List[str]       =			DonutSwinModelTester(self)
            _A				:	Optional[Any]       =			ConfigTester(self			,						config_class=__lowerCamelCase			,						embed_dim=3_7)


     def 			_lowerCamelCase			(						self)      ->    int:
            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 			_lowerCamelCase			(						self)      ->    Union[str, Any]:
            return


     def 			_lowerCamelCase			(						self)      ->    Any:
            _A				:	List[Any]       =			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*__lowerCamelCase)


     def 			_lowerCamelCase			(						self)      ->    int:
            # DonutSwin does not use inputs_embeds
            pass


     def 			_lowerCamelCase			(						self)      ->    int:
            _A   ,      _A				:	Tuple       =			self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                   _A				:	str       =			model_class(__lowerCamelCase)
                   self.assertIsInstance(model.get_input_embeddings()			,						(nn.Module))
                   _A				:	Optional[int]       =			model.get_output_embeddings()
                   self.assertTrue(x is None or isinstance(__lowerCamelCase			,						nn.Linear))


     def 			_lowerCamelCase			(						self)      ->    List[Any]:
            _A   ,      _A				:	Dict       =			self.model_tester.prepare_config_and_inputs_for_common()

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

                   _A				:	Union[str, Any]       =			["pixel_values"]
                   self.assertListEqual(arg_names[:1]			,						__lowerCamelCase)


     def 			_lowerCamelCase			(						self)      ->    str:
            _A   ,      _A				:	str       =			self.model_tester.prepare_config_and_inputs_for_common()
            _A				:	int       =			True

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

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

                   self.assertListEqual(
                       list(attentions[0].shape[-3:])			,						[self.model_tester.num_heads[0], window_size_squared, window_size_squared]			,						)
                   _A				:	List[str]       =			len(__lowerCamelCase)

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

                   if hasattr(self.model_tester			,						"num_hidden_states_types"):
                          _A				:	Any       =			self.model_tester.num_hidden_states_types
                   else:
                          # also another +1 for reshaped_hidden_states
                          _A				:	Tuple       =			2
                   self.assertEqual(out_len + added_hidden_states			,						len(__lowerCamelCase))

                   _A				:	Union[str, Any]       =			outputs.attentions

                   self.assertEqual(len(__lowerCamelCase)			,						__lowerCamelCase)

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


     def 			_lowerCamelCase			(						self			,						__lowerCamelCase			,						__lowerCamelCase			,						__lowerCamelCase			,						__lowerCamelCase)      ->    Union[str, Any]:
            _A				:	Any       =			model_class(__lowerCamelCase)
            model.to(__lowerCamelCase)
            model.eval()

            with torch.no_grad():
                   _A				:	str       =			model(**self._prepare_for_class(__lowerCamelCase			,						__lowerCamelCase))

            _A				:	Tuple       =			outputs.hidden_states

            _A				:	str       =			getattr(
                self.model_tester			,						"expected_num_hidden_layers"			,						len(self.model_tester.depths) + 1)
            self.assertEqual(len(__lowerCamelCase)			,						__lowerCamelCase)

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

            _A				:	Dict       =			(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]			,						)

            _A				:	Union[str, Any]       =			outputs.reshaped_hidden_states
            self.assertEqual(len(__lowerCamelCase)			,						__lowerCamelCase)

            _A   ,      _A   ,      _A   ,      _A				:	Optional[int]       =			reshaped_hidden_states[0].shape
            _A				:	List[str]       =			(
                reshaped_hidden_states[0].view(__lowerCamelCase			,						__lowerCamelCase			,						height * width).permute(0			,						2			,						1)
            )
            self.assertListEqual(
                list(reshaped_hidden_states.shape[-2:])			,						[num_patches, self.model_tester.embed_dim]			,						)


     def 			_lowerCamelCase			(						self)      ->    Optional[Any]:
            _A   ,      _A				:	Optional[Any]       =			self.model_tester.prepare_config_and_inputs_for_common()

            _A				:	Dict       =			(
                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:
                   _A				:	Any       =			True
                   self.check_hidden_states_output(__lowerCamelCase			,						__lowerCamelCase			,						__lowerCamelCase			,						__lowerCamelCase)

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

                   self.check_hidden_states_output(__lowerCamelCase			,						__lowerCamelCase			,						__lowerCamelCase			,						__lowerCamelCase)


     def 			_lowerCamelCase			(						self)      ->    Dict:
            _A   ,      _A				:	int       =			self.model_tester.prepare_config_and_inputs_for_common()
            _A				:	Dict       =			3

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

            _A				:	Dict       =			image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
            _A				:	Optional[Any]       =			image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

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

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


     @slow
     def 			_lowerCamelCase			(						self)      ->    str:
            for model_name in DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   _A				:	Optional[Any]       =			DonutSwinModel.from_pretrained(__lowerCamelCase)
                   self.assertIsNotNone(__lowerCamelCase)


     def 			_lowerCamelCase			(						self)      ->    List[Any]:
            _A   ,      _A				:	Any       =			self.model_tester.prepare_config_and_inputs_for_common()

            _A				:	Tuple       =			_config_zero_init(__lowerCamelCase)
            for model_class in self.all_model_classes:
                   _A				:	List[str]       =			model_class(config=__lowerCamelCase)
                   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"			,						)




import warnings

from .generation import FlaxGenerationMixin







class 					lowerCAmelCase__	(    a):




     '''simple docstring'''
     # warning at import time
     warnings.warn(
         "Importing `FlaxGenerationMixin` from `src/transformers/generation_flax_utils.py` is deprecated and will "
         "be removed in Transformers v5. Import as `from transformers import FlaxGenerationMixin` instead."					,   a					,   )
