


"""simple docstring"""




import inspect
import unittest

from transformers import YolosConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch
				from torch import nn

				from transformers import YolosForObjectDetection, YolosModel
				from transformers.models.yolos.modeling_yolos import YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
				from PIL import Image

				from transformers import AutoImageProcessor
class 					__lowerCAmelCase					:


		'''simple docstring'''



		def __init__(  self							, _a							, _a=13							, _a=[30, 30]							, _a=2							, _a=3							, _a=True							, _a=True							, _a=32							, _a=5							, _a=4							, _a=37							, _a="gelu"							, _a=0.1							, _a=0.1							, _a=10							, _a=0.02							, _a=3							, _a=None							, _a=8							, _a=10							, ):
				__a           =			parent
				__a           =			batch_size
				__a           =			image_size
				__a           =			patch_size
				__a           =			num_channels
				__a           =			is_training
				__a           =			use_labels
				__a           =			hidden_size
				__a           =			num_hidden_layers
				__a           =			num_attention_heads
				__a           =			intermediate_size
				__a           =			hidden_act
				__a           =			hidden_dropout_prob
				__a           =			attention_probs_dropout_prob
				__a           =			type_sequence_label_size
				__a           =			initializer_range
				__a           =			num_labels
				__a           =			scope
				__a           =			n_targets
				__a           =			num_detection_tokens
				# we set the expected sequence length (which is used in several tests)
				# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) + num_detection_tokens
				__a           =			(image_size[1] // patch_size) * (image_size[0] // patch_size)
				__a           =			num_patches + 1 + self.num_detection_tokens

		def 			__UpperCAmelCase       (  self      ):
				__a           =			floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]      )

				__a           =			None
				if self.use_labels:
						# labels is a list of Dict (each Dict being the labels for a given example in the batch)
						__a           =			[]
						for i in range(self.batch_size      ):
								__a           =			{}
								__a           =			torch.randint(
								    high=self.num_labels							, size=(self.n_targets,)							, device=a_      )
								__a           =			torch.rand(self.n_targets							, 4							, device=a_      )
								labels.append(a_      )

				__a           =			self.get_config()

				return config, pixel_values, labels

		def 			__UpperCAmelCase       (  self      ):
				return YolosConfig(
				    image_size=self.image_size							, patch_size=self.patch_size							, num_channels=self.num_channels							, hidden_size=self.hidden_size							, num_hidden_layers=self.num_hidden_layers							, num_attention_heads=self.num_attention_heads							, intermediate_size=self.intermediate_size							, hidden_act=self.hidden_act							, hidden_dropout_prob=self.hidden_dropout_prob							, attention_probs_dropout_prob=self.attention_probs_dropout_prob							, is_decoder=a_							, initializer_range=self.initializer_range							, num_detection_tokens=self.num_detection_tokens							, num_labels=self.num_labels							, )

		def 			__UpperCAmelCase       (  self							, _a							, _a							, _a      ):
				__a           =			YolosModel(config=a_      )
				model.to(a_      )
				model.eval()
				__a           =			model(a_      )
				self.parent.assertEqual(
				    result.last_hidden_state.shape							, (self.batch_size, self.expected_seq_len, self.hidden_size)      )

		def 			__UpperCAmelCase       (  self							, _a							, _a							, _a      ):
				__a           =			YolosForObjectDetection(a_      )
				model.to(a_      )
				model.eval()

				__a           =			model(pixel_values=a_      )
				__a           =			model(a_      )

				self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.num_detection_tokens, self.num_labels + 1)      )
				self.parent.assertEqual(result.pred_boxes.shape							, (self.batch_size, self.num_detection_tokens, 4)      )

				__a           =			model(pixel_values=a_							, labels=a_      )

				self.parent.assertEqual(result.loss.shape							, ()      )
				self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.num_detection_tokens, self.num_labels + 1)      )
				self.parent.assertEqual(result.pred_boxes.shape							, (self.batch_size, self.num_detection_tokens, 4)      )





		def 			__UpperCAmelCase       (  self      ):
				__a           =			self.prepare_config_and_inputs()
				__a           =			config_and_inputs
				__a           =			{"""pixel_values""": pixel_values}
				return config, inputs_dict







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


		'''simple docstring'''



		__UpperCAmelCase		:			Dict										=				(YolosModel, YolosForObjectDetection) if is_torch_available() else ()
		__UpperCAmelCase		:			List[str]										=				(
		    {'feature-extraction': YolosModel, 'object-detection': YolosForObjectDetection} if is_torch_available() else {}
		)

		__UpperCAmelCase		:			int										=				False
		__UpperCAmelCase		:			Union[str, Any]										=				False
		__UpperCAmelCase		:			List[Any]										=				False
		__UpperCAmelCase		:			Any										=				False

		def 			__UpperCAmelCase       (  self							, _a							, _a							, _a=False      ):
				__a           =			super()._prepare_for_class(a_							, a_							, return_labels=a_      )

				if return_labels:
						if model_class.__name__ == "YolosForObjectDetection":
								__a           =			[]
								for i in range(self.model_tester.batch_size      ):
										__a           =			{}
										__a           =			torch.ones(
										    size=(self.model_tester.n_targets,)							, device=a_							, dtype=torch.long      )
										__a           =			torch.ones(
										    self.model_tester.n_targets							, 4							, device=a_							, dtype=torch.float      )
										labels.append(a_      )
								__a           =			labels

				return inputs_dict

		def 			__UpperCAmelCase       (  self      ):
				__a           =			YolosModelTester(self      )
				__a           =			ConfigTester(self							, config_class=a_							, has_text_modality=a_							, hidden_size=37      )

		def 			__UpperCAmelCase       (  self      ):
				self.config_tester.run_common_tests()

		def 			__UpperCAmelCase       (  self      ):
				# YOLOS does not use inputs_embeds
				pass

		def 			__UpperCAmelCase       (  self      ):
				__a           =			self.model_tester.prepare_config_and_inputs_for_common()

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

		def 			__UpperCAmelCase       (  self      ):
				__a           =			self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
						__a           =			model_class(a_      )
						__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]							, a_      )

		def 			__UpperCAmelCase       (  self      ):
				__a           =			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*a_      )

		def 			__UpperCAmelCase       (  self      ):
				__a           =			self.model_tester.prepare_config_and_inputs_for_common()
				__a           =			True

				# in YOLOS, the seq_len is different
				__a           =			self.model_tester.expected_seq_len
				for model_class in self.all_model_classes:
						__a           =			True
						__a           =			False
						__a           =			True
						__a           =			model_class(a_      )
						model.to(a_      )
						model.eval()
						with torch.no_grad():
								__a           =			model(**self._prepare_for_class(a_							, a_      )      )
						__a           =			outputs.attentions
						self.assertEqual(len(a_      )							, self.model_tester.num_hidden_layers      )

						# check that output_attentions also work using config
						del inputs_dict["output_attentions"]
						__a           =			True
						__a           =			model_class(a_      )
						model.to(a_      )
						model.eval()
						with torch.no_grad():
								__a           =			model(**self._prepare_for_class(a_							, a_      )      )
						__a           =			outputs.attentions
						self.assertEqual(len(a_      )							, self.model_tester.num_hidden_layers      )

						self.assertListEqual(
						    list(attentions[0].shape[-3:]      )							, [self.model_tester.num_attention_heads, seq_len, seq_len]							, )
						__a           =			len(a_      )

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

						__a           =			1
						self.assertEqual(out_len + added_hidden_states							, len(a_      )      )

						__a           =			outputs.attentions

						self.assertEqual(len(a_      )							, self.model_tester.num_hidden_layers      )
						self.assertListEqual(
						    list(self_attentions[0].shape[-3:]      )							, [self.model_tester.num_attention_heads, seq_len, seq_len]							, )

		def 			__UpperCAmelCase       (  self      ):
				def check_hidden_states_output(_a							, _a							, _a      ):
						__a           =			model_class(a_      )
						model.to(a_      )
						model.eval()

						with torch.no_grad():
								__a           =			model(**self._prepare_for_class(a_							, a_      )      )

						__a           =			outputs.hidden_states

						__a           =			getattr(
						    self.model_tester							, '''expected_num_hidden_layers'''							, self.model_tester.num_hidden_layers + 1      )
						self.assertEqual(len(a_      )							, a_      )

						# YOLOS has a different seq_length
						__a           =			self.model_tester.expected_seq_len

						self.assertListEqual(
						    list(hidden_states[0].shape[-2:]      )							, [seq_length, self.model_tester.hidden_size]							, )

				__a           =			self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
						__a           =			True
						check_hidden_states_output(a_							, a_							, a_      )

						# check that output_hidden_states also work using config
						del inputs_dict["output_hidden_states"]
						__a           =			True

						check_hidden_states_output(a_							, a_							, a_      )

		def 			__UpperCAmelCase       (  self      ):
				__a           =			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_object_detection(*a_      )





		@slow
		def 			__UpperCAmelCase       (  self      ):
				for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						__a           =			YolosModel.from_pretrained(a_      )
						self.assertIsNotNone(a_      )







def        lowercase							(		) ->      List[Any]:
		__a           =			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)
		return image




@require_torch
@require_vision
class 					__lowerCAmelCase					(   unittest.TestCase       ):


		'''simple docstring'''



		@cached_property
		def 			__UpperCAmelCase       (  self      ):
				return AutoImageProcessor.from_pretrained('''hustvl/yolos-small'''      ) if is_vision_available() else None





		@slow
		def 			__UpperCAmelCase       (  self      ):
				__a           =			YolosForObjectDetection.from_pretrained('''hustvl/yolos-small'''      ).to(a_      )

				__a           =			self.default_image_processor
				__a           =			prepare_img()
				__a           =			image_processor(images=a_							, return_tensors='''pt'''      ).to(a_      )

				# forward pass
				with torch.no_grad():
						__a           =			model(inputs.pixel_values      )

				# verify outputs
				__a           =			torch.Size((1, 100, 92)      )
				self.assertEqual(outputs.logits.shape							, a_      )

				__a           =			torch.tensor(
				    [[-24.0248, -10.3024, -14.8290], [-42.0392, -16.8200, -27.4334], [-27.2743, -11.8154, -18.7148]]							, device=a_							, )
				__a           =			torch.tensor(
				    [[0.2559, 0.5455, 0.4706], [0.2989, 0.7279, 0.1875], [0.7732, 0.4017, 0.4462]]							, device=a_      )
				self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]							, a_							, atol=1E-4      )      )
				self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3]							, a_							, atol=1E-4      )      )

				# verify postprocessing
				__a           =			image_processor.post_process_object_detection(
				    a_							, threshold=0.3							, target_sizes=[image.size[::-1]]      )[0]
				__a           =			torch.tensor([0.9994, 0.9790, 0.9964, 0.9972, 0.9861]      ).to(a_      )
				__a           =			[75, 75, 17, 63, 17]
				__a           =			torch.tensor([335.0609, 79.3848, 375.4216, 187.2495]      ).to(a_      )

				self.assertEqual(len(results['''scores''']      )							, 5      )
				self.assertTrue(torch.allclose(results['''scores''']							, a_							, atol=1E-4      )      )
				self.assertSequenceEqual(results['''labels'''].tolist()							, a_      )
				self.assertTrue(torch.allclose(results['''boxes'''][0, :]							, a_      )      )











'''simple docstring'''






from ...configuration_utils import PretrainedConfig
from ...utils import logging


A_							: Any						 =	logging.get_logger(__name__)

A_							: Any						 =	{
    """microsoft/markuplm-base""": """https://huggingface.co/microsoft/markuplm-base/resolve/main/config.json""",
    """microsoft/markuplm-large""": """https://huggingface.co/microsoft/markuplm-large/resolve/main/config.json""",
}



class 							lowercase       (  _lowerCamelCase				):


 """simple docstring"""


 UpperCAmelCase							=  """markuplm"""







 def __init__(							self  ,a_=30_522  ,a_=768  ,a_=12  ,a_=12  ,a_=3_072  ,a_="gelu"  ,a_=0.1  ,a_=0.1  ,a_=512  ,a_=2  ,a_=0.02  ,a_=1E-1_2  ,a_=0  ,a_=0  ,a_=2  ,a_=256  ,a_=1_024  ,a_=216  ,a_=1_001  ,a_=32  ,a_=50  ,a_="absolute"  ,a_=True  ,a_=None  ,**a_  ,)		->   Union[str, Any]:
        super().__init__(
            pad_token_id=a_  ,bos_token_id=a_  ,eos_token_id=a_  ,**a_  ,)
        _UpperCAmelCase  :						Optional[int]			     = vocab_size
        _UpperCAmelCase  :						Tuple			     = hidden_size
        _UpperCAmelCase  :						str			     = num_hidden_layers
        _UpperCAmelCase  :						Dict			     = num_attention_heads
        _UpperCAmelCase  :						int			     = hidden_act
        _UpperCAmelCase  :						Optional[Any]			     = intermediate_size
        _UpperCAmelCase  :						Tuple			     = hidden_dropout_prob
        _UpperCAmelCase  :						List[str]			     = attention_probs_dropout_prob
        _UpperCAmelCase  :						Optional[int]			     = max_position_embeddings
        _UpperCAmelCase  :						Tuple			     = type_vocab_size
        _UpperCAmelCase  :						Dict			     = initializer_range
        _UpperCAmelCase  :						List[Any]			     = layer_norm_eps
        _UpperCAmelCase  :						Optional[Any]			     = position_embedding_type
        _UpperCAmelCase  :						Any			     = use_cache
        _UpperCAmelCase  :						List[Any]			     = classifier_dropout
        # additional properties
        _UpperCAmelCase  :						Dict			     = max_depth
        _UpperCAmelCase  :						Union[str, Any]			     = max_xpath_tag_unit_embeddings
        _UpperCAmelCase  :						Optional[int]			     = max_xpath_subs_unit_embeddings
        _UpperCAmelCase  :						List[Any]			     = tag_pad_id
        _UpperCAmelCase  :						Tuple			     = subs_pad_id
        _UpperCAmelCase  :						List[str]			     = xpath_unit_hidden_size



