





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

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







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

							def       UpperCAmelCase__		(							self							:       int				)   ->    Any:







													"""simple docstring"""
													UpperCAmelCase_     : int         =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

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

													UpperCAmelCase_     : Optional[int]         =					self.get_config()

													return config, pixel_values, labels

							def       UpperCAmelCase__		(							self							:       int				)   ->    Optional[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							:       List[Any]    ,       __magic_name__							:       Optional[Any]    ,       __magic_name__							:       str    ,       __magic_name__							:       Union[str, Any]				)   ->    str:







													"""simple docstring"""
													UpperCAmelCase_     : Union[str, Any]         =					DonutSwinModel(config=__magic_name__				)
													model.to(__magic_name__				)
													model.eval()
													UpperCAmelCase_     : List[str]         =					model(__magic_name__				)

													UpperCAmelCase_     : Dict         =					((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       UpperCAmelCase__		(							self							:       str				)   ->    Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_     : Tuple         =					self.prepare_config_and_inputs()
													(
													    (
													    UpperCAmelCase_
													)  ,    (
													    UpperCAmelCase_
													)  ,    (
													    UpperCAmelCase_
													)  ,    
													)     : List[str]         =					config_and_inputs
													UpperCAmelCase_     : str         =					{'''pixel_values''': pixel_values}
													return config, inputs_dict





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

							__a   :				Any       =    False
							__a   :				Optional[int]       =    False
							__a   :				str       =    False

							def       UpperCAmelCase__		(							self							:       Tuple				)   ->    Dict:







													"""simple docstring"""
													UpperCAmelCase_     : Any         =					DonutSwinModelTester(self				)
													UpperCAmelCase_     : Optional[Any]         =					ConfigTester(self    ,       config_class=__magic_name__    ,       embed_dim=37				)

							def       UpperCAmelCase__		(							self							:       List[str]				)   ->    List[str]:







													"""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							:       Dict				)   ->    List[str]:







													"""simple docstring"""
													return

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







													"""simple docstring"""
													UpperCAmelCase_     : List[str]         =					self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*__magic_name__				)

							def       UpperCAmelCase__		(							self							:       Tuple				)   ->    int:







													"""simple docstring"""
													# DonutSwin does not use inputs_embeds
													pass

							def       UpperCAmelCase__		(							self							:       Optional[int]				)   ->    str:







													"""simple docstring"""
													UpperCAmelCase_  ,    UpperCAmelCase_     : int         =					self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																			UpperCAmelCase_     : Optional[Any]         =					model_class(__magic_name__				)
																			self.assertIsInstance(model.get_input_embeddings()    ,       (nn.Module)				)
																			UpperCAmelCase_     : Dict         =					model.get_output_embeddings()
																			self.assertTrue(x is None or isinstance(__magic_name__    ,       nn.Linear				)				)

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







													"""simple docstring"""
													UpperCAmelCase_  ,    UpperCAmelCase_     : str         =					self.model_tester.prepare_config_and_inputs_for_common()

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

																			UpperCAmelCase_     : Optional[int]         =					['''pixel_values''']
																			self.assertListEqual(arg_names[:1]    ,       __magic_name__				)

							def       UpperCAmelCase__		(							self							:       Union[str, Any]				)   ->    int:







													"""simple docstring"""
													UpperCAmelCase_  ,    UpperCAmelCase_     : Dict         =					self.model_tester.prepare_config_and_inputs_for_common()
													UpperCAmelCase_     : List[Any]         =					True

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

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

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

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

																			if hasattr(self.model_tester    ,       '''num_hidden_states_types'''				):
																									UpperCAmelCase_     : str         =					self.model_tester.num_hidden_states_types
																			else:
																									# also another +1 for reshaped_hidden_states
																									UpperCAmelCase_     : Dict         =					2
																			self.assertEqual(out_len + added_hidden_states    ,       len(__magic_name__				)				)

																			UpperCAmelCase_     : str         =					outputs.attentions

																			self.assertEqual(len(__magic_name__				)    ,       __magic_name__				)

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

							def       UpperCAmelCase__		(							self							:       Optional[int]    ,       __magic_name__							:       Union[str, Any]    ,       __magic_name__							:       Any    ,       __magic_name__							:       List[str]    ,       __magic_name__							:       List[str]				)   ->    str:







													"""simple docstring"""
													UpperCAmelCase_     : List[Any]         =					model_class(__magic_name__				)
													model.to(__magic_name__				)
													model.eval()

													with torch.no_grad():
																			UpperCAmelCase_     : Optional[Any]         =					model(**self._prepare_for_class(__magic_name__    ,       __magic_name__				)				)

													UpperCAmelCase_     : List[str]         =					outputs.hidden_states

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

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

													UpperCAmelCase_     : 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_     : Optional[Any]         =					outputs.reshaped_hidden_states
													self.assertEqual(len(__magic_name__				)    ,       __magic_name__				)

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

							def       UpperCAmelCase__		(							self							:       Optional[Any]				)   ->    Any:







													"""simple docstring"""
													UpperCAmelCase_  ,    UpperCAmelCase_     : int         =					self.model_tester.prepare_config_and_inputs_for_common()

													UpperCAmelCase_     : Dict         =					(
													    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_     : Optional[Any]         =					True
																			self.check_hidden_states_output(__magic_name__    ,       __magic_name__    ,       __magic_name__    ,       __magic_name__				)

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

																			self.check_hidden_states_output(__magic_name__    ,       __magic_name__    ,       __magic_name__    ,       __magic_name__				)

							def       UpperCAmelCase__		(							self							:       int				)   ->    int:







													"""simple docstring"""
													UpperCAmelCase_  ,    UpperCAmelCase_     : List[Any]         =					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_     : Optional[int]         =					(
													    config.patch_size
													    if isinstance(config.patch_size    ,       collections.abc.Iterable				)
													    else (config.patch_size, config.patch_size)
													)

													UpperCAmelCase_     : Optional[int]         =					image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
													UpperCAmelCase_     : Tuple         =					image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

													for model_class in self.all_model_classes:
																			UpperCAmelCase_     : Union[str, Any]         =					True
																			self.check_hidden_states_output(__magic_name__    ,       __magic_name__    ,       __magic_name__    ,       (padded_height, padded_width)				)

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

							@slow
							def       UpperCAmelCase__		(							self							:       Dict				)   ->    List[Any]:







													"""simple docstring"""
													for model_name in DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			UpperCAmelCase_     : Dict         =					DonutSwinModel.from_pretrained(__magic_name__				)
																			self.assertIsNotNone(__magic_name__				)



							def       UpperCAmelCase__		(							self							:       Dict				)   ->    Any:







													"""simple docstring"""
													UpperCAmelCase_  ,    UpperCAmelCase_     : Union[str, Any]         =					self.model_tester.prepare_config_and_inputs_for_common()

													UpperCAmelCase_     : List[Any]         =					_config_zero_init(__magic_name__				)
													for model_class in self.all_model_classes:
																			UpperCAmelCase_     : List[Any]         =					model_class(config=__magic_name__				)
																			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
from numpy import floataa
from numpy.typing import NDArray





def     lowerCamelCase_ (					SCREAMING_SNAKE_CASE__ :     NDArray[floataa],     SCREAMING_SNAKE_CASE__ :     NDArray[floataa]				)	->		NDArray[floataa]:
						UpperCAmelCase_  ,    UpperCAmelCase_     : Any         =					np.shape(SCREAMING_SNAKE_CASE__				)

						UpperCAmelCase_     : NDArray[floataa]         =					np.zeros((rows, 1),     dtype=SCREAMING_SNAKE_CASE__				)
						for row in reversed(range(SCREAMING_SNAKE_CASE__				)				):
												UpperCAmelCase_     : Union[str, Any]         =					0
												for col in range(row + 1,     SCREAMING_SNAKE_CASE__				):
																		total += coefficients[row, col] * x[col]

												UpperCAmelCase_     : str         =					(vector[row] - total) / coefficients[row, row]

						return x





def     lowerCamelCase_ (					SCREAMING_SNAKE_CASE__ :     NDArray[floataa],     SCREAMING_SNAKE_CASE__ :     NDArray[floataa]				)	->		NDArray[floataa]:
						UpperCAmelCase_  ,    UpperCAmelCase_     : List[Any]         =					np.shape(SCREAMING_SNAKE_CASE__				)
						if rows != columns:
												return np.array((),     dtype=SCREAMING_SNAKE_CASE__				)

						# augmented matrix
						UpperCAmelCase_     : NDArray[floataa]         =					np.concatenate((coefficients, vector),     axis=1				)
						UpperCAmelCase_     : Optional[Any]         =					augmented_mat.astype('''float64'''				)

						# scale the matrix leaving it triangular
						for row in range(rows - 1				):
												UpperCAmelCase_     : Optional[int]         =					augmented_mat[row, row]
												for col in range(row + 1,     SCREAMING_SNAKE_CASE__				):
																		UpperCAmelCase_     : Optional[Any]         =					augmented_mat[col, row] / pivot
																		augmented_mat[col, :] -= factor * augmented_mat[row, :]

						UpperCAmelCase_     : Optional[Any]         =					retroactive_resolution(
						    augmented_mat[:, 0:columns],     augmented_mat[:, columns : columns + 1]				)

						return x


if __name__ == "__main__":
					import doctest

					doctest.testmod()





