


'''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			:	Any							=				batch_size
											_SCREAMING_SNAKE_CASE			:	Dict							=				image_size
											_SCREAMING_SNAKE_CASE			:	int							=				patch_size
											_SCREAMING_SNAKE_CASE			:	str							=				num_channels
											_SCREAMING_SNAKE_CASE			:	Any							=				embed_dim
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				depths
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				num_heads
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				window_size
											_SCREAMING_SNAKE_CASE			:	Any							=				mlp_ratio
											_SCREAMING_SNAKE_CASE			:	List[Any]							=				qkv_bias
											_SCREAMING_SNAKE_CASE			:	List[str]							=				hidden_dropout_prob
											_SCREAMING_SNAKE_CASE			:	str							=				attention_probs_dropout_prob
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				drop_path_rate
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				hidden_act
											_SCREAMING_SNAKE_CASE			:	Any							=				use_absolute_embeddings
											_SCREAMING_SNAKE_CASE			:	Dict							=				patch_norm
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				layer_norm_eps
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				initializer_range
											_SCREAMING_SNAKE_CASE			:	List[str]							=				is_training
											_SCREAMING_SNAKE_CASE			:	Any							=				scope
											_SCREAMING_SNAKE_CASE			:	List[Any]							=				use_labels
											_SCREAMING_SNAKE_CASE			:	List[Any]							=				type_sequence_label_size
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				encoder_stride






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

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

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

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





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





							'''simple docstring'''


							A_							:				List[str]				     =  (DonutSwinModel,) if is_torch_available() else ()
							A_							:				List[Any]				     =  {'feature-extraction': DonutSwinModel} if is_torch_available() else {}
							A_							:				str				     =  True

							A_							:				Optional[Any]				     =  False
							A_							:				Any				     =  False
							A_							:				Dict				     =  False






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				DonutSwinModelTester(self  )
											_SCREAMING_SNAKE_CASE			:	Dict							=				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			:	Union[str, Any]							=				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			:	List[Any]							=				self.model_tester.prepare_config_and_inputs_for_common()

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






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

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

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






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	List[str]							=				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			:	Any							=				False
															_SCREAMING_SNAKE_CASE			:	Dict							=				True
															_SCREAMING_SNAKE_CASE			:	Optional[int]							=				model_class(__snake_case  )
															model.to(__snake_case  )
															model.eval()
															with torch.no_grad():
																			_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				model(**self._prepare_for_class(__snake_case   , __snake_case  )  )
															_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				outputs.attentions
															_SCREAMING_SNAKE_CASE			:	Optional[int]							=				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			:	str							=				True
															_SCREAMING_SNAKE_CASE			:	Dict							=				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[Any]							=				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			:	List[Any]							=				len(__snake_case  )

															# Check attention is always last and order is fine
															_SCREAMING_SNAKE_CASE			:	Dict							=				True
															_SCREAMING_SNAKE_CASE			:	List[str]							=				True
															_SCREAMING_SNAKE_CASE			:	Union[str, 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  )  )

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

															_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				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			:	Dict							=				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			:	Union[str, Any]							=				outputs.hidden_states

											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				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			:	List[Any]							=				(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			:	List[str]							=				outputs.reshaped_hidden_states
											self.assertEqual(len(__snake_case  )   , __snake_case  )

											_SCREAMING_SNAKE_CASE			:	Dict							=				reshaped_hidden_states[0].shape
											_SCREAMING_SNAKE_CASE			:	List[Any]							=				(
											    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			:	Any							=				self.model_tester.prepare_config_and_inputs_for_common()

											_SCREAMING_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)
											)

											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			:	str							=				True

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






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

											_SCREAMING_SNAKE_CASE			:	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)
											)
											_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			:	Dict							=				image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

											for model_class in self.all_model_classes:
															_SCREAMING_SNAKE_CASE			:	List[Any]							=				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			:	List[str]							=				DonutSwinModel.from_pretrained(__snake_case  )
															self.assertIsNotNone(__snake_case  )






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

											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				_config_zero_init(__snake_case  )
											for model_class in self.all_model_classes:
															_SCREAMING_SNAKE_CASE			:	int							=				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 ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase_		:       Tuple							       =		logging.get_logger(__name__)

UpperCAmelCase_		:       List[Any]							       =		{
    # pcqm4mv1 now deprecated
    'graphormer-base': 'https://huggingface.co/clefourrier/graphormer-base-pcqm4mv2/resolve/main/config.json',
    # See all Graphormer models at https://huggingface.co/models?filter=graphormer
}





class    lowercase__  (						_snake_case ):





							'''simple docstring'''


							A_							:				Union[str, Any]				     =  """graphormer"""
							A_							:				Tuple				     =  ["""past_key_values"""]






							def __init__(   self   , __snake_case = 1   , __snake_case = 512 * 9   , __snake_case = 512 * 3   , __snake_case = 512   , __snake_case = 512   , __snake_case = 512   , __snake_case = 128   , __snake_case = 5   , __snake_case = 1024   , __snake_case = "multi_hop"   , __snake_case = 512   , __snake_case = False   , __snake_case = 12   , __snake_case = 768   , __snake_case = 768   , __snake_case = 32   , __snake_case = 0.1   , __snake_case = 0.1   , __snake_case = 0.0   , __snake_case = False   , __snake_case = False   , __snake_case = False   , __snake_case = "gelu"   , __snake_case = None   , __snake_case = False   , __snake_case = 0   , __snake_case = False   , __snake_case = 0.0   , __snake_case = 8   , __snake_case = None   , __snake_case = None   , __snake_case = True   , __snake_case = True   , __snake_case=0   , __snake_case=1   , __snake_case=2   , **__snake_case   , ):
											_SCREAMING_SNAKE_CASE			:	Dict							=				num_classes
											_SCREAMING_SNAKE_CASE			:	Dict							=				num_atoms
											_SCREAMING_SNAKE_CASE			:	Dict							=				num_in_degree
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				num_out_degree
											_SCREAMING_SNAKE_CASE			:	Any							=				num_edges
											_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				num_spatial
											_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				num_edge_dis
											_SCREAMING_SNAKE_CASE			:	int							=				edge_type
											_SCREAMING_SNAKE_CASE			:	Tuple							=				multi_hop_max_dist
											_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				spatial_pos_max
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				max_nodes
											_SCREAMING_SNAKE_CASE			:	str							=				num_hidden_layers
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				embedding_dim
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				embedding_dim
											_SCREAMING_SNAKE_CASE			:	int							=				ffn_embedding_dim
											_SCREAMING_SNAKE_CASE			:	Dict							=				num_attention_heads
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				dropout
											_SCREAMING_SNAKE_CASE			:	List[str]							=				attention_dropout
											_SCREAMING_SNAKE_CASE			:	int							=				layerdrop
											_SCREAMING_SNAKE_CASE			:	List[Any]							=				encoder_normalize_before
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				pre_layernorm
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				apply_graphormer_init
											_SCREAMING_SNAKE_CASE			:	List[str]							=				activation_fn
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				embed_scale
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				freeze_embeddings
											_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				num_trans_layers_to_freeze
											_SCREAMING_SNAKE_CASE			:	List[Any]							=				share_input_output_embed
											_SCREAMING_SNAKE_CASE			:	Tuple							=				traceable
											_SCREAMING_SNAKE_CASE			:	List[str]							=				q_noise
											_SCREAMING_SNAKE_CASE			:	str							=				qn_block_size

											# These parameters are here for future extensions
											# atm, the model only supports self attention
											_SCREAMING_SNAKE_CASE			:	Any							=				kdim
											_SCREAMING_SNAKE_CASE			:	int							=				vdim
											_SCREAMING_SNAKE_CASE			:	Any							=				self_attention
											_SCREAMING_SNAKE_CASE			:	Tuple							=				bias

											super().__init__(
											    pad_token_id=__snake_case   , bos_token_id=__snake_case   , eos_token_id=__snake_case   , **__snake_case   , )




