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




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




	def 			_UpperCamelCase    ( self					)		->							Optional[int]:
			lowerCamelCase     :			int		=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

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

			lowerCamelCase     :			int		=    self.get_config()

			return config, pixel_values, labels




	def 			_UpperCamelCase    ( self					)		->							Any:
			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		, UpperCAmelCase_		, UpperCAmelCase_		, UpperCAmelCase_					)		->							List[str]:
			lowerCamelCase     :			Union[str, Any]		=    DonutSwinModel(config=UpperCAmelCase_					)
			model.to(UpperCAmelCase_					)
			model.eval()
			lowerCamelCase     :			Optional[Any]		=    model(UpperCAmelCase_					)

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






@require_torch
class        _lowercase   (				__UpperCAmelCase				,       __UpperCAmelCase				,       unittest.TestCase		):
	lowercase_        =	(DonutSwinModel,) if is_torch_available() else ()
	lowercase_        =	{'feature-extraction': DonutSwinModel} if is_torch_available() else {}
	lowercase_        =	True

	lowercase_        =	False
	lowercase_        =	False
	lowercase_        =	False




	def 			_UpperCamelCase    ( self					)		->							List[Any]:
			lowerCamelCase     :			Optional[Any]		=    DonutSwinModelTester(self					)
			lowerCamelCase     :			Union[str, Any]		=    ConfigTester(self		, config_class=UpperCAmelCase_		, embed_dim=37					)




	def 			_UpperCamelCase    ( self					)		->							Optional[int]:
			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					)		->							int:
			return




	def 			_UpperCamelCase    ( self					)		->							List[Any]:
			lowerCamelCase     :			List[Any]		=    self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*UpperCAmelCase_					)




	def 			_UpperCamelCase    ( self					)		->							List[str]:
			# DonutSwin does not use inputs_embeds
			pass




	def 			_UpperCamelCase    ( self					)		->							int:
			lowerCamelCase	,    lowerCamelCase     :			List[str]		=    self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
					lowerCamelCase     :			List[str]		=    model_class(UpperCAmelCase_					)
					self.assertIsInstance(model.get_input_embeddings()		, (nn.Module)					)
					lowerCamelCase     :			str		=    model.get_output_embeddings()
					self.assertTrue(x is None or isinstance(UpperCAmelCase_		, nn.Linear					)					)




	def 			_UpperCamelCase    ( self					)		->							List[Any]:
			lowerCamelCase	,    lowerCamelCase     :			List[Any]		=    self.model_tester.prepare_config_and_inputs_for_common()

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

					lowerCamelCase     :			Tuple		=    ['pixel_values']
					self.assertListEqual(arg_names[:1]		, UpperCAmelCase_					)




	def 			_UpperCamelCase    ( self					)		->							Optional[int]:
			lowerCamelCase	,    lowerCamelCase     :			Optional[Any]		=    self.model_tester.prepare_config_and_inputs_for_common()
			lowerCamelCase     :			str		=    True

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

					# check that output_attentions also work using config
					del inputs_dict["output_attentions"]
					lowerCamelCase     :			int		=    True
					lowerCamelCase     :			Any		=    config.window_size**2
					lowerCamelCase     :			Optional[int]		=    model_class(UpperCAmelCase_					)
					model.to(UpperCAmelCase_					)
					model.eval()
					with torch.no_grad():
							lowerCamelCase     :			Optional[int]		=    model(**self._prepare_for_class(UpperCAmelCase_		, UpperCAmelCase_					)					)
					lowerCamelCase     :			Any		=    outputs.attentions
					self.assertEqual(len(UpperCAmelCase_					)		, UpperCAmelCase_					)

					self.assertListEqual(
					    list(attentions[0].shape[-3:]					)		, [self.model_tester.num_heads[0], window_size_squared, window_size_squared]		, )
					lowerCamelCase     :			Optional[Any]		=    len(UpperCAmelCase_					)

					# Check attention is always last and order is fine
					lowerCamelCase     :			Union[str, Any]		=    True
					lowerCamelCase     :			Tuple		=    True
					lowerCamelCase     :			str		=    model_class(UpperCAmelCase_					)
					model.to(UpperCAmelCase_					)
					model.eval()
					with torch.no_grad():
							lowerCamelCase     :			int		=    model(**self._prepare_for_class(UpperCAmelCase_		, UpperCAmelCase_					)					)

					if hasattr(self.model_tester		, 'num_hidden_states_types'					):
							lowerCamelCase     :			int		=    self.model_tester.num_hidden_states_types
					else:
							# also another +1 for reshaped_hidden_states
							lowerCamelCase     :			Union[str, Any]		=    2
					self.assertEqual(out_len + added_hidden_states		, len(UpperCAmelCase_					)					)

					lowerCamelCase     :			Dict		=    outputs.attentions

					self.assertEqual(len(UpperCAmelCase_					)		, UpperCAmelCase_					)

					self.assertListEqual(
					    list(self_attentions[0].shape[-3:]					)		, [self.model_tester.num_heads[0], window_size_squared, window_size_squared]		, )




	def 			_UpperCamelCase    ( self		, UpperCAmelCase_		, UpperCAmelCase_		, UpperCAmelCase_		, UpperCAmelCase_					)		->							str:
			lowerCamelCase     :			int		=    model_class(UpperCAmelCase_					)
			model.to(UpperCAmelCase_					)
			model.eval()

			with torch.no_grad():
					lowerCamelCase     :			Tuple		=    model(**self._prepare_for_class(UpperCAmelCase_		, UpperCAmelCase_					)					)

			lowerCamelCase     :			Any		=    outputs.hidden_states

			lowerCamelCase     :			Any		=    getattr(
			    self.model_tester		, 'expected_num_hidden_layers'		, len(self.model_tester.depths					) + 1					)
			self.assertEqual(len(UpperCAmelCase_					)		, UpperCAmelCase_					)

			# DonutSwin has a different seq_length
			lowerCamelCase     :			Optional[int]		=    (
			    config.patch_size
			    if isinstance(config.patch_size		, collections.abc.Iterable					)
			    else (config.patch_size, config.patch_size)
			)

			lowerCamelCase     :			Tuple		=    (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]		, )

			lowerCamelCase     :			List[str]		=    outputs.reshaped_hidden_states
			self.assertEqual(len(UpperCAmelCase_					)		, UpperCAmelCase_					)

			lowerCamelCase	,    lowerCamelCase	,    lowerCamelCase	,    lowerCamelCase     :			int		=    reshaped_hidden_states[0].shape
			lowerCamelCase     :			Any		=    (
			    reshaped_hidden_states[0].view(UpperCAmelCase_		, UpperCAmelCase_		, height * width					).permute(0		, 2		, 1					)
			)
			self.assertListEqual(
			    list(reshaped_hidden_states.shape[-2:]					)		, [num_patches, self.model_tester.embed_dim]		, )




	def 			_UpperCamelCase    ( self					)		->							int:
			lowerCamelCase	,    lowerCamelCase     :			Dict		=    self.model_tester.prepare_config_and_inputs_for_common()

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

			for model_class in self.all_model_classes:
					lowerCamelCase     :			Optional[int]		=    True
					self.check_hidden_states_output(UpperCAmelCase_		, UpperCAmelCase_		, UpperCAmelCase_		, UpperCAmelCase_					)

					# check that output_hidden_states also work using config
					del inputs_dict["output_hidden_states"]
					lowerCamelCase     :			List[Any]		=    True

					self.check_hidden_states_output(UpperCAmelCase_		, UpperCAmelCase_		, UpperCAmelCase_		, UpperCAmelCase_					)




	def 			_UpperCamelCase    ( self					)		->							List[str]:
			lowerCamelCase	,    lowerCamelCase     :			List[str]		=    self.model_tester.prepare_config_and_inputs_for_common()
			lowerCamelCase     :			Any		=    3

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

			lowerCamelCase     :			Any		=    image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
			lowerCamelCase     :			int		=    image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

			for model_class in self.all_model_classes:
					lowerCamelCase     :			int		=    True
					self.check_hidden_states_output(UpperCAmelCase_		, UpperCAmelCase_		, UpperCAmelCase_		, (padded_height, padded_width)					)

					# check that output_hidden_states also work using config
					del inputs_dict["output_hidden_states"]
					lowerCamelCase     :			List[Any]		=    True
					self.check_hidden_states_output(UpperCAmelCase_		, UpperCAmelCase_		, UpperCAmelCase_		, (padded_height, padded_width)					)




	@slow
	def 			_UpperCamelCase    ( self					)		->							str:
			for model_name in DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					lowerCamelCase     :			List[str]		=    DonutSwinModel.from_pretrained(UpperCAmelCase_					)
					self.assertIsNotNone(UpperCAmelCase_					)




	def 			_UpperCamelCase    ( self					)		->							Union[str, Any]:
			lowerCamelCase	,    lowerCamelCase     :			Union[str, Any]		=    self.model_tester.prepare_config_and_inputs_for_common()

			lowerCamelCase     :			Dict		=    _config_zero_init(UpperCAmelCase_					)
			for model_class in self.all_model_classes:
					lowerCamelCase     :			Tuple		=    model_class(config=UpperCAmelCase_					)
					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 numpy as np
def 		UpperCAmelCase					(		a_,						a_="utf-8",						a_="surrogatepass"			):





		'''simple docstring'''


		lowerCamelCase     :			str		=    bin(int.from_bytes(text.encode(a_,						a_			),						'big'			)			)[2:]
		return bits.zfill(8 * ((len(a_			) + 7) // 8)			)
def 		UpperCAmelCase					(		a_,						a_="utf-8",						a_="surrogatepass"			):





		'''simple docstring'''


		lowerCamelCase     :			List[Any]		=    int(a_,						2			)
		return n.to_bytes((n.bit_length() + 7) // 8,						'big'			).decode(a_,						a_			) or "\0"
def 		UpperCAmelCase					(		a_,						a_			):





		'''simple docstring'''


		if size_par + len(a_			) <= 2**size_par - (len(a_			) - 1):
				raise ValueError('size of parity don\'t match with size of data'			)

		lowerCamelCase     :			List[str]		=    []
		lowerCamelCase     :			str		=    []
		lowerCamelCase     :			Any		=    [bin(a_			)[2:] for x in range(1,						size_par + len(a_			) + 1			)]

		# sorted information data for the size of the output data
		lowerCamelCase     :			int		=    []
		# data position template + parity
		lowerCamelCase     :			Union[str, Any]		=    []
		# parity bit counter
		lowerCamelCase     :			Optional[int]		=    0
		# counter position of data bits
		lowerCamelCase     :			Any		=    0

		for x in range(1,						size_par + len(a_			) + 1			):
				# Performs a template of bit positions - who should be given,
				# and who should be parity
				if qtd_bp < size_par:
						if (np.log(a_			) / np.log(2			)).is_integer():
								data_out_gab.append('P'			)
								lowerCamelCase     :			List[Any]		=    qtd_bp + 1
						else:
								data_out_gab.append('D'			)
				else:
						data_out_gab.append('D'			)

				# Sorts the data to the new output size
				if data_out_gab[-1] == "D":
						data_ord.append(data[cont_data]			)
						cont_data += 1
				else:
						data_ord.append(a_			)

    # Calculates parity
		lowerCamelCase     :			Optional[Any]		=    0  # parity bit counter
		for bp in range(1,						size_par + 1			):
				# Bit counter one for a given parity
				lowerCamelCase     :			Union[str, Any]		=    0
				# counter to control the loop reading
				lowerCamelCase     :			Optional[int]		=    0
				for x in data_ord:
						if x is not None:
								try:
										lowerCamelCase     :			Union[str, Any]		=    (bin_pos[cont_loop])[-1 * (bp)]
								except IndexError:
										lowerCamelCase     :			str		=    '0'
								if aux == "1" and x == "1":
										cont_bo += 1
						cont_loop += 1
				parity.append(cont_bo % 2			)

				qtd_bp += 1

		# Mount the message
		lowerCamelCase     :			Tuple		=    0  # parity bit counter
		for x in range(0,						size_par + len(a_			)			):
				if data_ord[x] is None:
						data_out.append(str(parity[cont_bp]			)			)
						cont_bp += 1
				else:
						data_out.append(data_ord[x]			)

		return data_out
def 		UpperCAmelCase					(		a_,						a_			):





		'''simple docstring'''


		lowerCamelCase     :			str		=    []
		# Parity bit counter
		lowerCamelCase     :			Dict		=    0
		# Counter p data bit reading
		lowerCamelCase     :			int		=    0
		# list of parity received
		lowerCamelCase     :			Optional[int]		=    []
		lowerCamelCase     :			Any		=    []

		for x in range(1,						len(a_			) + 1			):
				# Performs a template of bit positions - who should be given,
				#  and who should be parity
				if qtd_bp < size_par and (np.log(a_			) / np.log(2			)).is_integer():
						data_out_gab.append('P'			)
						lowerCamelCase     :			List[Any]		=    qtd_bp + 1
				else:
						data_out_gab.append('D'			)

				# Sorts the data to the new output size
				if data_out_gab[-1] == "D":
						data_output.append(data[cont_data]			)
				else:
						parity_received.append(data[cont_data]			)
				cont_data += 1

		# -----------calculates the parity with the data
		lowerCamelCase     :			Dict		=    []
		lowerCamelCase     :			Optional[Any]		=    []
		lowerCamelCase     :			Dict		=    [bin(a_			)[2:] for x in range(1,						size_par + len(a_			) + 1			)]

		#  sorted information data for the size of the output data
		lowerCamelCase     :			Any		=    []
		# Data position feedback + parity
		lowerCamelCase     :			List[str]		=    []
		# Parity bit counter
		lowerCamelCase     :			List[str]		=    0
		# Counter p data bit reading
		lowerCamelCase     :			int		=    0

		for x in range(1,						size_par + len(a_			) + 1			):
				# Performs a template position of bits - who should be given,
				# and who should be parity
				if qtd_bp < size_par and (np.log(a_			) / np.log(2			)).is_integer():
						data_out_gab.append('P'			)
						lowerCamelCase     :			Tuple		=    qtd_bp + 1
				else:
						data_out_gab.append('D'			)

				# Sorts the data to the new output size
				if data_out_gab[-1] == "D":
						data_ord.append(data_output[cont_data]			)
						cont_data += 1
				else:
						data_ord.append(a_			)

    # Calculates parity
		lowerCamelCase     :			List[str]		=    0  # parity bit counter
		for bp in range(1,						size_par + 1			):
				# Bit counter one for a certain parity
				lowerCamelCase     :			List[Any]		=    0
				# Counter to control loop reading
				lowerCamelCase     :			List[Any]		=    0
				for x in data_ord:
						if x is not None:
								try:
										lowerCamelCase     :			Dict		=    (bin_pos[cont_loop])[-1 * (bp)]
								except IndexError:
										lowerCamelCase     :			List[Any]		=    '0'
								if aux == "1" and x == "1":
										cont_bo += 1
						cont_loop += 1
				parity.append(str(cont_bo % 2			)			)

				qtd_bp += 1

		# Mount the message
		lowerCamelCase     :			Tuple		=    0  # Parity bit counter
		for x in range(0,						size_par + len(a_			)			):
				if data_ord[x] is None:
						data_out.append(str(parity[cont_bp]			)			)
						cont_bp += 1
				else:
						data_out.append(data_ord[x]			)

		lowerCamelCase     :			Union[str, Any]		=    parity_received == parity
		return data_output, ack






