


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




			def __init__(		self	:  Optional[Any]						,	lowerCamelCase__	:  Optional[Any]						,	lowerCamelCase__	:  int=13						,	lowerCamelCase__	:  Union[str, Any]=32						,	lowerCamelCase__	:  List[Any]=2						,	lowerCamelCase__	:  Optional[int]=3						,	lowerCamelCase__	:  Optional[Any]=16						,	lowerCamelCase__	:  Dict=[1, 2, 1]						,	lowerCamelCase__	:  Any=[2, 2, 4]						,	lowerCamelCase__	:  str=2						,	lowerCamelCase__	:  Any=2.0						,	lowerCamelCase__	:  int=True						,	lowerCamelCase__	:  Tuple=0.0						,	lowerCamelCase__	:  Dict=0.0						,	lowerCamelCase__	:  int=0.1						,	lowerCamelCase__	:  str="gelu"						,	lowerCamelCase__	:  Dict=False						,	lowerCamelCase__	:  Dict=True						,	lowerCamelCase__	:  List[Any]=0.0_2						,	lowerCamelCase__	:  Dict=1E-5						,	lowerCamelCase__	:  str=True						,	lowerCamelCase__	:  Optional[Any]=None						,	lowerCamelCase__	:  List[Any]=True						,	lowerCamelCase__	:  str=10						,	lowerCamelCase__	:  Union[str, Any]=8						,	) ->Any:



					'''simple docstring'''







					_UpperCAmelCase		:							Optional[Any]             =   parent
					_UpperCAmelCase		:							Optional[int]             =   batch_size
					_UpperCAmelCase		:							List[Any]             =   image_size
					_UpperCAmelCase		:							Dict             =   patch_size
					_UpperCAmelCase		:							Dict             =   num_channels
					_UpperCAmelCase		:							Dict             =   embed_dim
					_UpperCAmelCase		:							List[Any]             =   depths
					_UpperCAmelCase		:							Any             =   num_heads
					_UpperCAmelCase		:							List[Any]             =   window_size
					_UpperCAmelCase		:							Dict             =   mlp_ratio
					_UpperCAmelCase		:							List[Any]             =   qkv_bias
					_UpperCAmelCase		:							Tuple             =   hidden_dropout_prob
					_UpperCAmelCase		:							Optional[int]             =   attention_probs_dropout_prob
					_UpperCAmelCase		:							Tuple             =   drop_path_rate
					_UpperCAmelCase		:							Tuple             =   hidden_act
					_UpperCAmelCase		:							Any             =   use_absolute_embeddings
					_UpperCAmelCase		:							str             =   patch_norm
					_UpperCAmelCase		:							Any             =   layer_norm_eps
					_UpperCAmelCase		:							List[Any]             =   initializer_range
					_UpperCAmelCase		:							str             =   is_training
					_UpperCAmelCase		:							Dict             =   scope
					_UpperCAmelCase		:							Dict             =   use_labels
					_UpperCAmelCase		:							Optional[Any]             =   type_sequence_label_size
					_UpperCAmelCase		:							Optional[int]             =   encoder_stride




			def 				lowerCAmelCase__     (		self	:  str			) ->Optional[Any]:



					'''simple docstring'''







					_UpperCAmelCase		:							Any             =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

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

					_UpperCAmelCase		:							str             =   self.get_config()

					return config, pixel_values, labels




			def 				lowerCAmelCase__     (		self	:  List[str]			) ->Dict:



					'''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 				lowerCAmelCase__     (		self	:  Dict						,	lowerCamelCase__	:  List[Any]						,	lowerCamelCase__	:  Any						,	lowerCamelCase__	:  Union[str, Any]			) ->Dict:



					'''simple docstring'''







					_UpperCAmelCase		:							Optional[Any]             =   DonutSwinModel(config=lowerCamelCase__			)
					model.to(lowerCamelCase__			)
					model.eval()
					_UpperCAmelCase		:							Any             =   model(lowerCamelCase__			)

					_UpperCAmelCase		:							List[str]             =   ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths			) - 1))
					_UpperCAmelCase		:							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 				lowerCAmelCase__     (		self	:  Union[str, Any]			) ->Dict:



					'''simple docstring'''







					_UpperCAmelCase		:							List[str]             =   self.prepare_config_and_inputs()
					(
					    (
					    _UpperCAmelCase
					)       ,     (
					    _UpperCAmelCase
					)       ,     (
					    _UpperCAmelCase
					)       ,     
					)		:							Union[str, Any]             =   config_and_inputs
					_UpperCAmelCase		:							Optional[int]             =   {"pixel_values": pixel_values}
					return config, inputs_dict






@require_torch
class 						lowerCAmelCase__      (      UpperCAmelCase__     ,							UpperCAmelCase__     ,							unittest.TestCase						):
			lowerCAmelCase			:						List[str]							 =  (DonutSwinModel,) if is_torch_available() else ()
			lowerCAmelCase			:						List[Any]							 =  {"feature-extraction": DonutSwinModel} if is_torch_available() else {}
			lowerCAmelCase			:						Union[str, Any]							 =  True

			lowerCAmelCase			:						Any							 =  False
			lowerCAmelCase			:						Dict							 =  False
			lowerCAmelCase			:						Dict							 =  False




			def 				lowerCAmelCase__     (		self	:  Tuple			) ->List[Any]:



					'''simple docstring'''







					_UpperCAmelCase		:							Union[str, Any]             =   DonutSwinModelTester(self			)
					_UpperCAmelCase		:							List[Any]             =   ConfigTester(self						,	config_class=lowerCamelCase__						,	embed_dim=37			)




			def 				lowerCAmelCase__     (		self	:  Any			) ->Optional[int]:



					'''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 				lowerCAmelCase__     (		self	:  str			) ->Union[str, Any]:



					'''simple docstring'''







					return




			def 				lowerCAmelCase__     (		self	:  Any			) ->Any:



					'''simple docstring'''







					_UpperCAmelCase		:							Optional[Any]             =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*lowerCamelCase__			)




			def 				lowerCAmelCase__     (		self	:  Dict			) ->Tuple:



					'''simple docstring'''







					pass




			def 				lowerCAmelCase__     (		self	:  Optional[Any]			) ->str:



					'''simple docstring'''







					_UpperCAmelCase       ,     _UpperCAmelCase		:							Dict             =   self.model_tester.prepare_config_and_inputs_for_common()

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




			def 				lowerCAmelCase__     (		self	:  Tuple			) ->Union[str, Any]:



					'''simple docstring'''







					_UpperCAmelCase       ,     _UpperCAmelCase		:							str             =   self.model_tester.prepare_config_and_inputs_for_common()

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

							_UpperCAmelCase		:							int             =   ["pixel_values"]
							self.assertListEqual(arg_names[:1]						,	lowerCamelCase__			)




			def 				lowerCAmelCase__     (		self	:  int			) ->Dict:



					'''simple docstring'''







					_UpperCAmelCase       ,     _UpperCAmelCase		:							Tuple             =   self.model_tester.prepare_config_and_inputs_for_common()
					_UpperCAmelCase		:							Tuple             =   True

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

							# check that output_attentions also work using config
							del inputs_dict["output_attentions"]
							_UpperCAmelCase		:							int             =   True
							_UpperCAmelCase		:							List[Any]             =   config.window_size**2
							_UpperCAmelCase		:							List[Any]             =   model_class(lowerCamelCase__			)
							model.to(lowerCamelCase__			)
							model.eval()
							with torch.no_grad():
									_UpperCAmelCase		:							List[str]             =   model(**self._prepare_for_class(lowerCamelCase__						,	lowerCamelCase__			)			)
							_UpperCAmelCase		:							Tuple             =   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]						,	)
							_UpperCAmelCase		:							Optional[int]             =   len(lowerCamelCase__			)

							# Check attention is always last and order is fine
							_UpperCAmelCase		:							List[Any]             =   True
							_UpperCAmelCase		:							Dict             =   True
							_UpperCAmelCase		:							int             =   model_class(lowerCamelCase__			)
							model.to(lowerCamelCase__			)
							model.eval()
							with torch.no_grad():
									_UpperCAmelCase		:							Dict             =   model(**self._prepare_for_class(lowerCamelCase__						,	lowerCamelCase__			)			)

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

							_UpperCAmelCase		:							str             =   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	:  str						,	lowerCamelCase__	:  Optional[int]						,	lowerCamelCase__	:  List[str]						,	lowerCamelCase__	:  List[str]						,	lowerCamelCase__	:  Optional[Any]			) ->Union[str, Any]:



					'''simple docstring'''







					_UpperCAmelCase		:							List[str]             =   model_class(lowerCamelCase__			)
					model.to(lowerCamelCase__			)
					model.eval()

					with torch.no_grad():
							_UpperCAmelCase		:							Optional[int]             =   model(**self._prepare_for_class(lowerCamelCase__						,	lowerCamelCase__			)			)

					_UpperCAmelCase		:							List[str]             =   outputs.hidden_states

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

					_UpperCAmelCase		:							List[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]						,	)

					_UpperCAmelCase		:							Any             =   outputs.reshaped_hidden_states
					self.assertEqual(len(lowerCamelCase__			)						,	lowerCamelCase__			)

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



					'''simple docstring'''







					_UpperCAmelCase       ,     _UpperCAmelCase		:							Any             =   self.model_tester.prepare_config_and_inputs_for_common()

					_UpperCAmelCase		:							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)
					)

					for model_class in self.all_model_classes:
							_UpperCAmelCase		:							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"]
							_UpperCAmelCase		:							str             =   True

							self.check_hidden_states_output(lowerCamelCase__						,	lowerCamelCase__						,	lowerCamelCase__						,	lowerCamelCase__			)




			def 				lowerCAmelCase__     (		self	:  Tuple			) ->Tuple:



					'''simple docstring'''







					_UpperCAmelCase       ,     _UpperCAmelCase		:							Tuple             =   self.model_tester.prepare_config_and_inputs_for_common()
					_UpperCAmelCase		:							Optional[int]             =   3

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

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

					for model_class in self.all_model_classes:
							_UpperCAmelCase		:							List[Any]             =   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"]
							_UpperCAmelCase		:							Dict             =   True
							self.check_hidden_states_output(lowerCamelCase__						,	lowerCamelCase__						,	lowerCamelCase__						,	(padded_height, padded_width)			)




			@slow
			def 				lowerCAmelCase__     (		self	:  Union[str, Any]			) ->Optional[Any]:



					'''simple docstring'''







					for model_name in DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							_UpperCAmelCase		:							str             =   DonutSwinModel.from_pretrained(lowerCamelCase__			)
							self.assertIsNotNone(lowerCamelCase__			)






			def 				lowerCAmelCase__     (		self	:  int			) ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase       ,     _UpperCAmelCase		:							List[str]             =   self.model_tester.prepare_config_and_inputs_for_common()

					_UpperCAmelCase		:							Optional[int]             =   _config_zero_init(lowerCamelCase__			)
					for model_class in self.all_model_classes:
							_UpperCAmelCase		:							Optional[int]             =   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"""						,	)







'''simple docstring'''


import io
import json
import subprocess


lowerCamelCase__     						=  [
    ['en', 'ru'],
    ['ru', 'en'],
    ['en', 'de'],
    ['de', 'en'],
]

lowerCamelCase__     						=  8






def 			__lowerCAmelCase			(__lowerCAmelCase   ,			__lowerCAmelCase ):
		_UpperCAmelCase		:							List[str]             =   {}
		for src, tgt in pairs:
				_UpperCAmelCase		:							Tuple             =   F"""{src}-{tgt}"""
				_UpperCAmelCase		:							Tuple             =   F"""sacrebleu -t wmt19 -l {pair} --echo src""".split()
				_UpperCAmelCase		:							List[Any]             =   subprocess.run(__lowerCAmelCase   ,			stdout=subprocess.PIPE ).stdout.decode("utf-8" ).splitlines()
				_UpperCAmelCase		:							Tuple             =   F"""sacrebleu -t wmt19 -l {pair} --echo ref""".split()
				_UpperCAmelCase		:							List[str]             =   subprocess.run(__lowerCAmelCase   ,			stdout=subprocess.PIPE ).stdout.decode("utf-8" ).splitlines()
				_UpperCAmelCase		:							Tuple             =   {"src": src_lines[:n_objs], "tgt": tgt_lines[:n_objs]}
		return text


lowerCamelCase__     						=  get_all_data(pairs, n_objs)
lowerCamelCase__     						=  './fsmt_val_data.json'
with io.open(filename, 'w', encoding='utf-8') as f:
						lowerCamelCase__     						=  json.dump(text, f, indent=2, ensure_ascii=False)



