
"""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						,					)						->				List[str]:
			lowerCamelCase	:     List[Any]            =       parent
			lowerCamelCase	:     List[Any]            =       batch_size
			lowerCamelCase	:     List[Any]            =       image_size
			lowerCamelCase	:     List[str]            =       patch_size
			lowerCamelCase	:     Dict            =       num_channels
			lowerCamelCase	:     int            =       embed_dim
			lowerCamelCase	:     Optional[Any]            =       depths
			lowerCamelCase	:     Optional[int]            =       num_heads
			lowerCamelCase	:     int            =       window_size
			lowerCamelCase	:     Optional[int]            =       mlp_ratio
			lowerCamelCase	:     Union[str, Any]            =       qkv_bias
			lowerCamelCase	:     str            =       hidden_dropout_prob
			lowerCamelCase	:     Union[str, Any]            =       attention_probs_dropout_prob
			lowerCamelCase	:     Union[str, Any]            =       drop_path_rate
			lowerCamelCase	:     Optional[int]            =       hidden_act
			lowerCamelCase	:     Optional[int]            =       use_absolute_embeddings
			lowerCamelCase	:     Optional[Any]            =       patch_norm
			lowerCamelCase	:     str            =       layer_norm_eps
			lowerCamelCase	:     Union[str, Any]            =       initializer_range
			lowerCamelCase	:     Any            =       is_training
			lowerCamelCase	:     Dict            =       scope
			lowerCamelCase	:     Optional[int]            =       use_labels
			lowerCamelCase	:     Dict            =       type_sequence_label_size
			lowerCamelCase	:     Optional[int]            =       encoder_stride




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

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

			lowerCamelCase	:     str            =       self.get_config()

			return config, pixel_values, labels




	def      _UpperCamelCase       (					self   )						->				Optional[int]:
			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_   )						->				int:
			lowerCamelCase	:     Optional[int]            =       DonutSwinModel(config=__UpperCAmelCase   )
			model.to(__UpperCAmelCase   )
			model.eval()
			lowerCamelCase	:     str            =       model(__UpperCAmelCase   )

			lowerCamelCase	:     Dict            =       ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths   ) - 1))
			lowerCamelCase	:     List[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   )						->				Tuple:
			lowerCamelCase	:     List[str]            =       self.prepare_config_and_inputs()
			(
			    lowerCamelCase 
			)	:     int            =       config_and_inputs
			lowerCamelCase	:     List[str]            =       {'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[str]:
			lowerCamelCase	:     Optional[int]            =       DonutSwinModelTester(self   )
			lowerCamelCase	:     Optional[int]            =       ConfigTester(self						,					config_class=__UpperCAmelCase						,					embed_dim=37   )




	def      _UpperCamelCase       (					self   )						->				Tuple:
			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   )						->				Tuple:
			return




	def      _UpperCamelCase       (					self   )						->				str:
			lowerCamelCase	:     Dict            =       self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*__UpperCAmelCase   )




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




	def      _UpperCamelCase       (					self   )						->				str:
			lowerCamelCase	:     Any            =       self.model_tester.prepare_config_and_inputs_for_common()

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




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

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

					lowerCamelCase	:     Dict            =       ['pixel_values']
					self.assertListEqual(arg_names[:1]						,					__UpperCAmelCase   )




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

			for model_class in self.all_model_classes:
					lowerCamelCase	:     Optional[Any]            =       True
					lowerCamelCase	:     List[str]            =       False
					lowerCamelCase	:     Optional[Any]            =       True
					lowerCamelCase	:     List[str]            =       model_class(__UpperCAmelCase   )
					model.to(__UpperCAmelCase   )
					model.eval()
					with torch.no_grad():
							lowerCamelCase	:     Union[str, Any]            =       model(**self._prepare_for_class(__UpperCAmelCase						,					__UpperCAmelCase   )   )
					lowerCamelCase	:     int            =       outputs.attentions
					lowerCamelCase	:     Optional[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	:     Optional[int]            =       True
					lowerCamelCase	:     List[str]            =       config.window_size**2
					lowerCamelCase	:     Optional[Any]            =       model_class(__UpperCAmelCase   )
					model.to(__UpperCAmelCase   )
					model.eval()
					with torch.no_grad():
							lowerCamelCase	:     Any            =       model(**self._prepare_for_class(__UpperCAmelCase						,					__UpperCAmelCase   )   )
					lowerCamelCase	:     Optional[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	:     List[str]            =       len(__UpperCAmelCase   )

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

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

					lowerCamelCase	:     List[Any]            =       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_   )						->				Dict:
			lowerCamelCase	:     List[Any]            =       model_class(__UpperCAmelCase   )
			model.to(__UpperCAmelCase   )
			model.eval()

			with torch.no_grad():
					lowerCamelCase	:     int            =       model(**self._prepare_for_class(__UpperCAmelCase						,					__UpperCAmelCase   )   )

			lowerCamelCase	:     Optional[int]            =       outputs.hidden_states

			lowerCamelCase	:     Tuple            =       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	:     Any            =       (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[Any]            =       outputs.reshaped_hidden_states
			self.assertEqual(len(__UpperCAmelCase   )						,					__UpperCAmelCase   )

			lowerCamelCase	:     Any            =       reshaped_hidden_states[0].shape
			lowerCamelCase	:     Union[str, 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   )						->				Optional[int]:
			lowerCamelCase	:     Tuple            =       self.model_tester.prepare_config_and_inputs_for_common()

			lowerCamelCase	:     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:
					lowerCamelCase	:     Union[str, Any]            =       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	:     Optional[Any]            =       True

					self.check_hidden_states_output(__UpperCAmelCase						,					__UpperCAmelCase						,					__UpperCAmelCase						,					__UpperCAmelCase   )




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

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

			lowerCamelCase	:     Optional[Any]            =       image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
			lowerCamelCase	:     List[str]            =       image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

			for model_class in self.all_model_classes:
					lowerCamelCase	:     Optional[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	:     int            =       True
					self.check_hidden_states_output(__UpperCAmelCase						,					__UpperCAmelCase						,					__UpperCAmelCase						,					(padded_height, padded_width)   )




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




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

			lowerCamelCase	:     Optional[Any]            =       _config_zero_init(__UpperCAmelCase   )
			for model_class in self.all_model_classes:
					lowerCamelCase	:     int            =       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 re
import tempfile
import unittest
from pathlib import Path

from datasets.utils.metadata import DatasetMetadata




def        A_					(					UpperCAmelCase__				)     ->     str:
   a   :	Optional[int]						=    min(re.search('^ +'       ,   UpperCAmelCase__				).end() if t.startswith(' '				) else 0 for t in string.splitlines()				)
   return "\n".join([line[indent_level:] for line in string.splitlines() if indent_level < len(UpperCAmelCase__				)]				)


SCREAMING_SNAKE_CASE__				:  Union[str, Any]						= "\\n---\nlanguage:\n- zh\n- en\ntask_ids:\n- sentiment-classification\n---\n# Begin of markdown\n\nSome cool dataset card\n"

SCREAMING_SNAKE_CASE__				:  List[str]						= "\\n---\n---\n# Begin of markdown\n\nSome cool dataset card\n"


SCREAMING_SNAKE_CASE__				:  Dict						= "\\n# Begin of markdown\n\nSome cool dataset card\n"


class 	A_       (     unittest.TestCase   ):





  """simple docstring"""

  def 							lowercase_     (						self						)				->					Any:
     with tempfile.TemporaryDirectory() as tmp_dir:
        a   :	Optional[Any]						=    Path(__UpperCAmelCase						) / 'README.md'
        with open(__UpperCAmelCase  ,					'w+'						) as readme_file:
           readme_file.write(__UpperCAmelCase						)
        a   :	str						=    DatasetMetadata.from_readme(__UpperCAmelCase						)
        self.assertDictEqual(__UpperCAmelCase  ,					{'language': ['zh', 'en'], 'task_ids': ['sentiment-classification']}						)

        with open(__UpperCAmelCase  ,					'w+'						) as readme_file:
           readme_file.write(__UpperCAmelCase						)
        a   :	Optional[Any]						=    DatasetMetadata.from_readme(__UpperCAmelCase						)
        self.assertDictEqual(__UpperCAmelCase  ,					{}						)

        with open(__UpperCAmelCase  ,					'w+'						) as readme_file:
           readme_file.write(__UpperCAmelCase						)
        a   :	str						=    DatasetMetadata.from_readme(__UpperCAmelCase						)
        self.assertEqual(__UpperCAmelCase  ,					{}						)






  def 							lowercase_     (						self						)				->					Tuple:
     a   :	List[str]						=    _dedent(
         '\\n            annotations_creators:\n            - found\n            language_creators:\n            - found\n            language:\n            - en\n            license:\n            - unknown\n            multilinguality:\n            - monolingual\n            pretty_name: Test Dataset\n            size_categories:\n            - 10K<n<100K\n            source_datasets:\n            - extended|other-yahoo-webscope-l6\n            task_categories:\n            - question-answering\n            task_ids:\n            - open-domain-qa\n            '						)
     assert DatasetMetadata.from_yaml_string(__UpperCAmelCase						)

     a   :	Optional[Any]						=    _dedent(
         '\\n            annotations_creators:\n            - found\n            language:\n            - en\n            license:\n            - unknown\n            multilinguality:\n            - monolingual\n            pretty_name: Test Dataset\n            size_categories:\n            - 10K<n<100K\n            source_datasets:\n            - extended|other-yahoo-webscope-l6\n            task_categories:\n            - question-answering\n            task_ids:\n            - open-domain-qa\n            task_ids:\n            - open-domain-qa\n            '						)
     with self.assertRaises(__UpperCAmelCase						):
        DatasetMetadata.from_yaml_string(__UpperCAmelCase						)

     a   :	List[Any]						=    _dedent(
         '\\n            annotations_creators:\n            - found\n            language_creators:\n            - found\n            language:\n            - en\n            license:\n            - unknown\n            multilinguality:\n            - monolingual\n            pretty_name: Test Dataset\n            size_categories:\n            - 10K<n<100K\n            source_datasets:\n            - extended|other-yahoo-webscope-l6\n            task_categories:\n            - text-classification\n            task_ids:\n            - multi-class-classification\n            paperswithcode_id:\n            - squad\n            configs:\n            - en\n            train-eval-index:\n            - config: en\n              task: text-classification\n              task_id: multi_class_classification\n              splits:\n                train_split: train\n                eval_split: test\n              col_mapping:\n                text: text\n                label: target\n              metrics:\n                - type: accuracy\n                  name: Accuracy\n            extra_gated_prompt: |\n              By clicking on “Access repository” below, you also agree to ImageNet Terms of Access:\n              [RESEARCHER_FULLNAME] (the "Researcher") has requested permission to use the ImageNet database (the "Database") at Princeton University and Stanford University. In exchange for such permission, Researcher hereby agrees to the following terms and conditions:\n              1. Researcher shall use the Database only for non-commercial research and educational purposes.\n            extra_gated_fields:\n              Company: text\n              Country: text\n              I agree to use this model for non-commerical use ONLY: checkbox\n            '						)
     assert DatasetMetadata.from_yaml_string(__UpperCAmelCase						)





