


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

						"""simple docstring"""






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






						def 							_UpperCAmelCase					(     self       :			Optional[int]  ):
													A_ 						=		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

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

													A_ 						=		self.get_config()

													return config, pixel_values, labels






						def 							_UpperCAmelCase					(     self       :			Tuple  ):
													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       :			Union[str, Any]   ,     lowerCAmelCase       :			Dict   ,     lowerCAmelCase       :			List[str]   ,     lowerCAmelCase       :			Any  ):
													A_ 						=		DonutSwinModel(config=lowerCAmelCase  )
													model.to(lowerCAmelCase  )
													model.eval()
													A_ 						=		model(lowerCAmelCase  )

													A_ 						=		((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths  ) - 1))
													A_ 						=		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]  ):
													A_ 						=		self.prepare_config_and_inputs()
													(
													    (
													    A_				
													)						,				(
													    A_				
													)						,				(
													    A_				
													)						,								
													) 						=		config_and_inputs
													A_ 						=		{"pixel_values": pixel_values}
													return config, inputs_dict






@require_torch
class 				__lowerCAmelCase      (      lowercase							, lowercase							, unittest.TestCase	):

						"""simple docstring"""






						_UpperCAmelCase		:					str      	=(DonutSwinModel,) if is_torch_available() else ()
						_UpperCAmelCase		:					str      	={"feature-extraction": DonutSwinModel} if is_torch_available() else {}
						_UpperCAmelCase		:					Any      	=True

						_UpperCAmelCase		:					Any      	=False
						_UpperCAmelCase		:					int      	=False
						_UpperCAmelCase		:					int      	=False






						def 							_UpperCAmelCase					(     self       :			Union[str, Any]  ):
													A_ 						=		DonutSwinModelTester(self  )
													A_ 						=		ConfigTester(self   ,     config_class=lowerCAmelCase   ,     embed_dim=37  )






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






						def 							_UpperCAmelCase					(     self       :			List[str]  ):
													A_ 						=		self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*lowerCAmelCase  )






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






						def 							_UpperCAmelCase					(     self       :			List[Any]  ):
													A_						,				A_ 						=		self.model_tester.prepare_config_and_inputs_for_common()

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






						def 							_UpperCAmelCase					(     self       :			Tuple  ):
													A_						,				A_ 						=		self.model_tester.prepare_config_and_inputs_for_common()

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

																				A_ 						=		["pixel_values"]
																				self.assertListEqual(arg_names[:1]   ,     lowerCAmelCase  )






						def 							_UpperCAmelCase					(     self       :			Optional[int]  ):
													A_						,				A_ 						=		self.model_tester.prepare_config_and_inputs_for_common()
													A_ 						=		True

													for model_class in self.all_model_classes:
																				A_ 						=		True
																				A_ 						=		False
																				A_ 						=		True
																				A_ 						=		model_class(lowerCAmelCase  )
																				model.to(lowerCAmelCase  )
																				model.eval()
																				with torch.no_grad():
																											A_ 						=		model(**self._prepare_for_class(lowerCAmelCase   ,     lowerCAmelCase  )  )
																				A_ 						=		outputs.attentions
																				A_ 						=		len(self.model_tester.depths  )
																				self.assertEqual(len(lowerCAmelCase  )   ,     lowerCAmelCase  )

																				# check that output_attentions also work using config
																				del inputs_dict["output_attentions"]
																				A_ 						=		True
																				A_ 						=		config.window_size**2
																				A_ 						=		model_class(lowerCAmelCase  )
																				model.to(lowerCAmelCase  )
																				model.eval()
																				with torch.no_grad():
																											A_ 						=		model(**self._prepare_for_class(lowerCAmelCase   ,     lowerCAmelCase  )  )
																				A_ 						=		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_ 						=		len(lowerCAmelCase  )

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

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

																				A_ 						=		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 							_UpperCAmelCase					(     self       :			int   ,     lowerCAmelCase       :			int   ,     lowerCAmelCase       :			Tuple   ,     lowerCAmelCase       :			Tuple   ,     lowerCAmelCase       :			List[str]  ):
													A_ 						=		model_class(lowerCAmelCase  )
													model.to(lowerCAmelCase  )
													model.eval()

													with torch.no_grad():
																				A_ 						=		model(**self._prepare_for_class(lowerCAmelCase   ,     lowerCAmelCase  )  )

													A_ 						=		outputs.hidden_states

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

													A_ 						=		(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_ 						=		outputs.reshaped_hidden_states
													self.assertEqual(len(lowerCAmelCase  )   ,     lowerCAmelCase  )

													A_						,				A_						,				A_						,				A_ 						=		reshaped_hidden_states[0].shape
													A_ 						=		(
													    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 							_UpperCAmelCase					(     self       :			str  ):
													A_						,				A_ 						=		self.model_tester.prepare_config_and_inputs_for_common()

													A_ 						=		(
													    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_ 						=		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_ 						=		True

																				self.check_hidden_states_output(lowerCAmelCase   ,     lowerCAmelCase   ,     lowerCAmelCase   ,     lowerCAmelCase  )






						def 							_UpperCAmelCase					(     self       :			Union[str, Any]  ):
													A_						,				A_ 						=		self.model_tester.prepare_config_and_inputs_for_common()
													A_ 						=		3

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

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

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






						@slow
						def 							_UpperCAmelCase					(     self       :			Dict  ):
													for model_name in DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																				A_ 						=		DonutSwinModel.from_pretrained(lowerCAmelCase  )
																				self.assertIsNotNone(lowerCAmelCase  )






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

													A_ 						=		_config_zero_init(lowerCAmelCase  )
													for model_class in self.all_model_classes:
																				A_ 						=		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 shutil
import tempfile
import unittest
from pathlib import Path
from shutil import copyfile

from transformers import SpeechaTextTokenizer, is_speech_available
from transformers.models.speech_to_text.tokenization_speech_to_text import VOCAB_FILES_NAMES, save_json
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, require_torchaudio
from transformers.utils import FEATURE_EXTRACTOR_NAME

from .test_feature_extraction_speech_to_text import floats_list


if is_speech_available():
							from transformers import SpeechaTextFeatureExtractor, SpeechaTextProcessor


__SCREAMING_SNAKE_CASE				:							int		       =		get_tests_dir('''fixtures/test_sentencepiece.model''')






@require_torch
@require_torchaudio
@require_sentencepiece
class 				__lowerCAmelCase      (      unittest.TestCase	):

						"""simple docstring"""






						def 							_UpperCAmelCase					(     self       :			List[str]  ):
													A_ 						=		tempfile.mkdtemp()

													A_ 						=		["<s>", "<pad>", "</s>", "<unk>", "▁This", "▁is", "▁a", "▁t", "est"]
													A_ 						=		dict(zip(lowerCAmelCase   ,     range(len(lowerCAmelCase  )  )  )  )
													A_ 						=		Path(self.tmpdirname  )
													save_json(lowerCAmelCase   ,     save_dir / VOCAB_FILES_NAMES["vocab_file"]  )
													if not (save_dir / VOCAB_FILES_NAMES["spm_file"]).exists():
																				copyfile(lowerCAmelCase   ,     save_dir / VOCAB_FILES_NAMES["spm_file"]  )

													A_ 						=		SpeechaTextTokenizer.from_pretrained(self.tmpdirname  )
													tokenizer.save_pretrained(self.tmpdirname  )

													A_ 						=		{
													    "feature_size": 24,
													    "num_mel_bins": 24,
													    "padding_value": 0.0,
													    "sampling_rate": 1_60_00,
													    "return_attention_mask": False,
													    "do_normalize": True,
													}
													save_json(lowerCAmelCase   ,     save_dir / FEATURE_EXTRACTOR_NAME  )






						def 							_UpperCAmelCase					(     self       :			Union[str, Any]   ,     **lowerCAmelCase       :			str  ):
													return SpeechaTextTokenizer.from_pretrained(self.tmpdirname   ,     **lowerCAmelCase  )






						def 							_UpperCAmelCase					(     self       :			List[Any]   ,     **lowerCAmelCase       :			Optional[Any]  ):
													return SpeechaTextFeatureExtractor.from_pretrained(self.tmpdirname   ,     **lowerCAmelCase  )






						def 							_UpperCAmelCase					(     self       :			Any  ):
													shutil.rmtree(self.tmpdirname  )






						def 							_UpperCAmelCase					(     self       :			Union[str, Any]  ):
													A_ 						=		self.get_tokenizer()
													A_ 						=		self.get_feature_extractor()

													A_ 						=		SpeechaTextProcessor(tokenizer=lowerCAmelCase   ,     feature_extractor=lowerCAmelCase  )

													processor.save_pretrained(self.tmpdirname  )
													A_ 						=		SpeechaTextProcessor.from_pretrained(self.tmpdirname  )

													self.assertEqual(processor.tokenizer.get_vocab()   ,     tokenizer.get_vocab()  )
													self.assertIsInstance(processor.tokenizer   ,     lowerCAmelCase  )

													self.assertEqual(processor.feature_extractor.to_json_string()   ,     feature_extractor.to_json_string()  )
													self.assertIsInstance(processor.feature_extractor   ,     lowerCAmelCase  )






						def 							_UpperCAmelCase					(     self       :			Dict  ):
													A_ 						=		SpeechaTextProcessor(
													    tokenizer=self.get_tokenizer()   ,     feature_extractor=self.get_feature_extractor()  )
													processor.save_pretrained(self.tmpdirname  )

													A_ 						=		self.get_tokenizer(bos_token="(BOS)"   ,     eos_token="(EOS)"  )
													A_ 						=		self.get_feature_extractor(do_normalize=lowerCAmelCase   ,     padding_value=1.0  )

													A_ 						=		SpeechaTextProcessor.from_pretrained(
													    self.tmpdirname   ,     bos_token="(BOS)"   ,     eos_token="(EOS)"   ,     do_normalize=lowerCAmelCase   ,     padding_value=1.0  )

													self.assertEqual(processor.tokenizer.get_vocab()   ,     tokenizer_add_kwargs.get_vocab()  )
													self.assertIsInstance(processor.tokenizer   ,     lowerCAmelCase  )

													self.assertEqual(processor.feature_extractor.to_json_string()   ,     feature_extractor_add_kwargs.to_json_string()  )
													self.assertIsInstance(processor.feature_extractor   ,     lowerCAmelCase  )






						def 							_UpperCAmelCase					(     self       :			str  ):
													A_ 						=		self.get_feature_extractor()
													A_ 						=		self.get_tokenizer()

													A_ 						=		SpeechaTextProcessor(tokenizer=lowerCAmelCase   ,     feature_extractor=lowerCAmelCase  )

													A_ 						=		floats_list((3, 10_00)  )

													A_ 						=		feature_extractor(lowerCAmelCase   ,     return_tensors="np"  )
													A_ 						=		processor(lowerCAmelCase   ,     return_tensors="np"  )

													for key in input_feat_extract.keys():
																				self.assertAlmostEqual(input_feat_extract[key].sum()   ,     input_processor[key].sum()   ,     delta=1e-2  )






						def 							_UpperCAmelCase					(     self       :			Optional[int]  ):
													A_ 						=		self.get_feature_extractor()
													A_ 						=		self.get_tokenizer()

													A_ 						=		SpeechaTextProcessor(tokenizer=lowerCAmelCase   ,     feature_extractor=lowerCAmelCase  )

													A_ 						=		"This is a test string"

													A_ 						=		processor(text=lowerCAmelCase  )

													A_ 						=		tokenizer(lowerCAmelCase  )

													for key in encoded_tok.keys():
																				self.assertListEqual(encoded_tok[key]   ,     encoded_processor[key]  )






						def 							_UpperCAmelCase					(     self       :			int  ):
													A_ 						=		self.get_feature_extractor()
													A_ 						=		self.get_tokenizer()

													A_ 						=		SpeechaTextProcessor(tokenizer=lowerCAmelCase   ,     feature_extractor=lowerCAmelCase  )

													A_ 						=		[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

													A_ 						=		processor.batch_decode(lowerCAmelCase  )
													A_ 						=		tokenizer.batch_decode(lowerCAmelCase  )

													self.assertListEqual(lowerCAmelCase   ,     lowerCAmelCase  )






						def 							_UpperCAmelCase					(     self       :			Tuple  ):
													A_ 						=		self.get_feature_extractor()
													A_ 						=		self.get_tokenizer()

													A_ 						=		SpeechaTextProcessor(tokenizer=lowerCAmelCase   ,     feature_extractor=lowerCAmelCase  )

													self.assertListEqual(
													    processor.model_input_names   ,     feature_extractor.model_input_names   ,     msg="`processor` and `feature_extractor` model input names do not match"   ,     )





