






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:    Optional[Any] ,      UpperCAmelCase_:    Optional[int] ,      UpperCAmelCase_:    Optional[int]=13 ,      UpperCAmelCase_:    List[str]=32 ,      UpperCAmelCase_:    Optional[Any]=2 ,      UpperCAmelCase_:    List[str]=3 ,      UpperCAmelCase_:    Dict=16 ,      UpperCAmelCase_:    Dict=[1, 2, 1] ,      UpperCAmelCase_:    List[Any]=[2, 2, 4] ,      UpperCAmelCase_:    Any=2 ,      UpperCAmelCase_:    Optional[Any]=2.0 ,      UpperCAmelCase_:    Dict=True ,      UpperCAmelCase_:    str=0.0 ,      UpperCAmelCase_:    Optional[Any]=0.0 ,      UpperCAmelCase_:    int=0.1 ,      UpperCAmelCase_:    Union[str, Any]="gelu" ,      UpperCAmelCase_:    Union[str, Any]=False ,      UpperCAmelCase_:    Union[str, Any]=True ,      UpperCAmelCase_:    List[str]=0.02 ,      UpperCAmelCase_:    Optional[int]=1E-5 ,      UpperCAmelCase_:    Tuple=True ,      UpperCAmelCase_:    List[str]=None ,      UpperCAmelCase_:    List[Any]=True ,      UpperCAmelCase_:    Union[str, Any]=10 ,      UpperCAmelCase_:    Union[str, Any]=8 ,      ):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE       =    parent
									_SCREAMING_SNAKE_CASE       =    batch_size
									_SCREAMING_SNAKE_CASE       =    image_size
									_SCREAMING_SNAKE_CASE       =    patch_size
									_SCREAMING_SNAKE_CASE       =    num_channels
									_SCREAMING_SNAKE_CASE       =    embed_dim
									_SCREAMING_SNAKE_CASE       =    depths
									_SCREAMING_SNAKE_CASE       =    num_heads
									_SCREAMING_SNAKE_CASE       =    window_size
									_SCREAMING_SNAKE_CASE       =    mlp_ratio
									_SCREAMING_SNAKE_CASE       =    qkv_bias
									_SCREAMING_SNAKE_CASE       =    hidden_dropout_prob
									_SCREAMING_SNAKE_CASE       =    attention_probs_dropout_prob
									_SCREAMING_SNAKE_CASE       =    drop_path_rate
									_SCREAMING_SNAKE_CASE       =    hidden_act
									_SCREAMING_SNAKE_CASE       =    use_absolute_embeddings
									_SCREAMING_SNAKE_CASE       =    patch_norm
									_SCREAMING_SNAKE_CASE       =    layer_norm_eps
									_SCREAMING_SNAKE_CASE       =    initializer_range
									_SCREAMING_SNAKE_CASE       =    is_training
									_SCREAMING_SNAKE_CASE       =    scope
									_SCREAMING_SNAKE_CASE       =    use_labels
									_SCREAMING_SNAKE_CASE       =    type_sequence_label_size
									_SCREAMING_SNAKE_CASE       =    encoder_stride







			def 				UpperCamelCase (      self:    str	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE       =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

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

									_SCREAMING_SNAKE_CASE       =    self.get_config()

									return config, pixel_values, labels







			def 				UpperCamelCase (      self:    List[Any]	):







									'''simple docstring'''




									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:    Dict ,      UpperCAmelCase_:    Any ,      UpperCAmelCase_:    Union[str, Any] ,      UpperCAmelCase_:    List[str]	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE       =    DonutSwinModel(config=_snake_case	)
									model.to(_snake_case	)
									model.eval()
									_SCREAMING_SNAKE_CASE       =    model(_snake_case	)

									_SCREAMING_SNAKE_CASE       =    ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths	) - 1))
									_SCREAMING_SNAKE_CASE       =    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:    List[Any]	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE       =    self.prepare_config_and_inputs()
									(
									    (
									    _SCREAMING_SNAKE_CASE      
									) ,	(
									    _SCREAMING_SNAKE_CASE      
									) ,	(
									    _SCREAMING_SNAKE_CASE      
									) ,	      
									)       =    config_and_inputs
									_SCREAMING_SNAKE_CASE       =    {"""pixel_values""": pixel_values}
									return config, inputs_dict



@require_torch
class  __UpperCAmelCase						(lowerCamelCase__			,lowerCamelCase__			,unittest.TestCase       ):
			__snake_case							: Union[str, Any]							=				(DonutSwinModel,) if is_torch_available() else ()
			__snake_case							: int							=				{"feature-extraction": DonutSwinModel} if is_torch_available() else {}
			__snake_case							: Tuple							=				True

			__snake_case							: Tuple							=				False
			__snake_case							: Any							=				False
			__snake_case							: Dict							=				False







			def 				UpperCamelCase (      self:    Dict	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE       =    DonutSwinModelTester(self	)
									_SCREAMING_SNAKE_CASE       =    ConfigTester(self ,      config_class=_snake_case ,      embed_dim=37	)







			def 				UpperCamelCase (      self:    Optional[Any]	):







									'''simple docstring'''




									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:    Optional[int]	):







									'''simple docstring'''




									return







			def 				UpperCamelCase (      self:    Any	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE       =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_snake_case	)







			def 				UpperCamelCase (      self:    Optional[Any]	):







									'''simple docstring'''




									pass







			def 				UpperCamelCase (      self:    Union[str, Any]	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE       =    self.model_tester.prepare_config_and_inputs_for_common()

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







			def 				UpperCamelCase (      self:    str	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE       =    self.model_tester.prepare_config_and_inputs_for_common()

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

															_SCREAMING_SNAKE_CASE       =    ["""pixel_values"""]
															self.assertListEqual(arg_names[:1] ,      _snake_case	)







			def 				UpperCamelCase (      self:    List[Any]	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE       =    self.model_tester.prepare_config_and_inputs_for_common()
									_SCREAMING_SNAKE_CASE       =    True

									for model_class in self.all_model_classes:
															_SCREAMING_SNAKE_CASE       =    True
															_SCREAMING_SNAKE_CASE       =    False
															_SCREAMING_SNAKE_CASE       =    True
															_SCREAMING_SNAKE_CASE       =    model_class(_snake_case	)
															model.to(_snake_case	)
															model.eval()
															with torch.no_grad():
																					_SCREAMING_SNAKE_CASE       =    model(**self._prepare_for_class(_snake_case ,      _snake_case	)	)
															_SCREAMING_SNAKE_CASE       =    outputs.attentions
															_SCREAMING_SNAKE_CASE       =    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       =    True
															_SCREAMING_SNAKE_CASE       =    config.window_size**2
															_SCREAMING_SNAKE_CASE       =    model_class(_snake_case	)
															model.to(_snake_case	)
															model.eval()
															with torch.no_grad():
																					_SCREAMING_SNAKE_CASE       =    model(**self._prepare_for_class(_snake_case ,      _snake_case	)	)
															_SCREAMING_SNAKE_CASE       =    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       =    len(_snake_case	)

															# Check attention is always last and order is fine
															_SCREAMING_SNAKE_CASE       =    True
															_SCREAMING_SNAKE_CASE       =    True
															_SCREAMING_SNAKE_CASE       =    model_class(_snake_case	)
															model.to(_snake_case	)
															model.eval()
															with torch.no_grad():
																					_SCREAMING_SNAKE_CASE       =    model(**self._prepare_for_class(_snake_case ,      _snake_case	)	)

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

															_SCREAMING_SNAKE_CASE       =    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:    str ,      UpperCAmelCase_:    Optional[int] ,      UpperCAmelCase_:    Optional[int] ,      UpperCAmelCase_:    List[Any] ,      UpperCAmelCase_:    Any	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE       =    model_class(_snake_case	)
									model.to(_snake_case	)
									model.eval()

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

									_SCREAMING_SNAKE_CASE       =    outputs.hidden_states

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

									_SCREAMING_SNAKE_CASE       =    (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       =    outputs.reshaped_hidden_states
									self.assertEqual(len(_snake_case	) ,      _snake_case	)

									_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE       =    reshaped_hidden_states[0].shape
									_SCREAMING_SNAKE_CASE       =    (
									    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:    List[str]	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE       =    self.model_tester.prepare_config_and_inputs_for_common()

									_SCREAMING_SNAKE_CASE       =    (
									    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       =    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       =    True

															self.check_hidden_states_output(_snake_case ,      _snake_case ,      _snake_case ,      _snake_case	)







			def 				UpperCamelCase (      self:    int	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE       =    self.model_tester.prepare_config_and_inputs_for_common()
									_SCREAMING_SNAKE_CASE       =    3

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

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

									for model_class in self.all_model_classes:
															_SCREAMING_SNAKE_CASE       =    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       =    True
															self.check_hidden_states_output(_snake_case ,      _snake_case ,      _snake_case ,      (padded_height, padded_width)	)







			@slow
			def 				UpperCamelCase (      self:    Union[str, Any]	):







									'''simple docstring'''




									for model_name in DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															_SCREAMING_SNAKE_CASE       =    DonutSwinModel.from_pretrained(_snake_case	)
															self.assertIsNotNone(_snake_case	)







			def 				UpperCamelCase (      self:    Dict	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE       =    self.model_tester.prepare_config_and_inputs_for_common()

									_SCREAMING_SNAKE_CASE       =    _config_zero_init(_snake_case	)
									for model_class in self.all_model_classes:
															_SCREAMING_SNAKE_CASE       =    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' ,      )






import argparse
import json
import re
import string
import sys
from collections import Counter


def 						_UpperCAmelCase							( snake_case     ):





					"""simple docstring"""

					def remove_articles(snake_case     ):
										return re.sub(R"""\b(a|an|the)\b"""      ,		""" """      ,		snake_case     )

					def white_space_fix(snake_case     ):
										return " ".join(text.split()     )

					def remove_punc(snake_case     ):
										_lowerCAmelCase         =       set(string.punctuation     )
										return "".join(ch for ch in text if ch not in exclude     )

					def lower(snake_case     ):
										return text.lower()

					return white_space_fix(remove_articles(remove_punc(lower(snake_case     )     )     )     )


def 						_UpperCAmelCase							( snake_case      ,		snake_case     ):





					"""simple docstring"""

					_lowerCAmelCase         =       normalize_answer(snake_case     ).split()
					_lowerCAmelCase         =       normalize_answer(snake_case     ).split()
					_lowerCAmelCase         =       Counter(snake_case     ) & Counter(snake_case     )
					_lowerCAmelCase         =       sum(common.values()     )
					if num_same == 0:
										return 0
					_lowerCAmelCase         =       1.0 * num_same / len(snake_case     )
					_lowerCAmelCase         =       1.0 * num_same / len(snake_case     )
					_lowerCAmelCase         =       (2 * precision * recall) / (precision + recall)
					return fa


def 						_UpperCAmelCase							( snake_case      ,		snake_case     ):





					"""simple docstring"""

					return normalize_answer(snake_case     ) == normalize_answer(snake_case     )


def 						_UpperCAmelCase							( snake_case      ,		snake_case      ,		snake_case     ):





					"""simple docstring"""

					_lowerCAmelCase         =       []
					for ground_truth in ground_truths:
										_lowerCAmelCase         =       metric_fn(snake_case      ,		snake_case     )
										scores_for_ground_truths.append(snake_case     )
					return max(snake_case     )


def 						_UpperCAmelCase							( snake_case      ,		snake_case     ):





					"""simple docstring"""

					_lowerCAmelCase         =       _lowerCAmelCase         =       _lowerCAmelCase         =       0
					for article in dataset:
										for paragraph in article["paragraphs"]:
															for qa in paragraph["qas"]:
																				total += 1
																				if qa["id"] not in predictions:
																									_lowerCAmelCase         =       """Unanswered question """ + qa["""id"""] + """ will receive score 0."""
																									print(snake_case      ,		file=sys.stderr     )
																									continue
																				_lowerCAmelCase         =       [x["""text"""] for x in qa["""answers"""]]
																				_lowerCAmelCase         =       predictions[qa["""id"""]]
																				exact_match += metric_max_over_ground_truths(snake_case      ,		snake_case      ,		snake_case     )
																				fa += metric_max_over_ground_truths(snake_case      ,		snake_case      ,		snake_case     )

					_lowerCAmelCase         =       100.0 * exact_match / total
					_lowerCAmelCase         =       100.0 * fa / total

					return {"exact_match": exact_match, "f1": fa}


if __name__ == "__main__":
	A__       =			"""1.1"""
	A__       =			argparse.ArgumentParser(description="""Evaluation for SQuAD """ + expected_version)
	parser.add_argument("""dataset_file""", help="""Dataset file""")
	parser.add_argument("""prediction_file""", help="""Prediction File""")
	A__       =			parser.parse_args()
	with open(args.dataset_file) as dataset_file:
		A__       =			json.load(dataset_file)
		if dataset_json["version"] != expected_version:
			print(
			    """Evaluation expects v-""" + expected_version + """, but got dataset with v-""" + dataset_json["""version"""],
			    file=sys.stderr,
			)
		A__       =			dataset_json["""data"""]
	with open(args.prediction_file) as prediction_file:
		A__       =			json.load(prediction_file)
	print(json.dumps(evaluate(dataset, predictions)))



