


'''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'''
def 			a_							(  UpperCamelCase_	,     UpperCamelCase_ = 1_0_0_0   ):
							if (not isinstance(UpperCamelCase_	,     UpperCamelCase_   )) or (digit_position <= 0):
														raise ValueError("Digit position must be a positive integer"   )
							elif (not isinstance(UpperCamelCase_	,     UpperCamelCase_   )) or (precision < 0):
														raise ValueError("Precision must be a nonnegative integer"   )

							# compute an approximation of (16 ** (n - 1)) * pi whose fractional part is mostly
							# accurate
							A_ 						=		(
							    4 * _subsum(UpperCamelCase_	,     1	,     UpperCamelCase_   )
							    - 2 * _subsum(UpperCamelCase_	,     4	,     UpperCamelCase_   )
							    - _subsum(UpperCamelCase_	,     5	,     UpperCamelCase_   )
							    - _subsum(UpperCamelCase_	,     6	,     UpperCamelCase_   )
							)

							# return the first hex digit of the fractional part of the result
							return hex(int((sum_result % 1) * 1_6   )   )[2:]







def 			a_							(  UpperCamelCase_	,     UpperCamelCase_	,     UpperCamelCase_   ):
							A_ 						=		0.0
							for sum_index in range(digit_pos_to_extract + precision   ):
														A_ 						=		8 * sum_index + denominator_addend
														if sum_index < digit_pos_to_extract:
																					# if the exponential term is an integer and we mod it by the denominator
																					# before dividing, only the integer part of the sum will change;
																					# the fractional part will not
																					A_ 						=		pow(
																					    1_6	,     digit_pos_to_extract - 1 - sum_index	,     UpperCamelCase_   )
														else:
																					A_ 						=		pow(1_6	,     digit_pos_to_extract - 1 - sum_index   )
														total += exponential_term / denominator
							return total


if __name__ == "__main__":
							import doctest

							doctest.testmod()





