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







				"""simple docstring"""






				def __init__(      self							:							str      ,    _A							:							Tuple      ,    _A							:							Optional[int]=1_3      ,    _A							:							List[Any]=[3_0, 3_0]      ,    _A							:							List[str]=2      ,    _A							:							Dict=3      ,    _A							:							List[str]=True      ,    _A							:							Optional[Any]=True      ,    _A							:							Optional[int]=3_2      ,    _A							:							Tuple=5      ,    _A							:							Union[str, Any]=4      ,    _A							:							Union[str, Any]=3_7      ,    _A							:							Optional[Any]="gelu"      ,    _A							:							Tuple=0.1      ,    _A							:							int=0.1      ,    _A							:							List[str]=1_0      ,    _A							:							Dict=0.02      ,    _A							:							List[Any]=3      ,    _A							:							List[Any]=None      ,    _A							:							Dict=8      ,    _A							:							Optional[Any]=1_0      ,    ):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Dict			  =    parent
											_SCREAMING_SNAKE_CASE		:						int			  =    batch_size
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    image_size
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    patch_size
											_SCREAMING_SNAKE_CASE		:						str			  =    num_channels
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    is_training
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    use_labels
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    hidden_size
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    num_hidden_layers
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    num_attention_heads
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    intermediate_size
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    hidden_act
											_SCREAMING_SNAKE_CASE		:						int			  =    hidden_dropout_prob
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    attention_probs_dropout_prob
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    type_sequence_label_size
											_SCREAMING_SNAKE_CASE		:						Any			  =    initializer_range
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    num_labels
											_SCREAMING_SNAKE_CASE		:						Dict			  =    scope
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    n_targets
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    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
											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    (image_size[1] // patch_size) * (image_size[0] // patch_size)
											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    num_patches + 1 + self.num_detection_tokens





				def 		_lowerCAmelCase		(      self							:							Optional[Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						List[str]			  =    floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]])

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

											_SCREAMING_SNAKE_CASE		:						Tuple			  =    self.get_config()

											return config, pixel_values, labels





				def 		_lowerCAmelCase		(      self							:							List[str]):



											"""simple docstring"""







											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 		_lowerCAmelCase		(      self							:							Tuple      ,    _A							:							Tuple      ,    _A							:							Union[str, Any]      ,    _A							:							Any):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    YolosModel(config=_A)
											model.to(_A)
											model.eval()
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    model(_A)
											self.parent.assertEqual(
											    result.last_hidden_state.shape      ,    (self.batch_size, self.expected_seq_len, self.hidden_size))





				def 		_lowerCAmelCase		(      self							:							List[Any]      ,    _A							:							int      ,    _A							:							Any      ,    _A							:							str):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    YolosForObjectDetection(_A)
											model.to(_A)
											model.eval()

											_SCREAMING_SNAKE_CASE		:						int			  =    model(pixel_values=_A)
											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    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))

											_SCREAMING_SNAKE_CASE		:						str			  =    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 		_lowerCAmelCase		(      self							:							Optional[int]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Any			  =    self.prepare_config_and_inputs()
											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    config_and_inputs
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    {"""pixel_values""": pixel_values}
											return config, inputs_dict




@require_torch
class  _snake_case		(    __snake_case   ,       __snake_case   ,       unittest.TestCase ):







				"""simple docstring"""






				a			=						(YolosModel, YolosForObjectDetection) if is_torch_available() else ()
				a			=						(
				    {"feature-extraction": YolosModel, "object-detection": YolosForObjectDetection} if is_torch_available() else {}
				)

				a			=						False
				a			=						False
				a			=						False
				a			=						False





				def 		_lowerCAmelCase		(      self							:							Union[str, Any]      ,    _A							:							Tuple      ,    _A							:							List[Any]      ,    _A							:							Dict=False):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Dict			  =    super()._prepare_for_class(_A      ,    _A      ,    return_labels=_A)

											if return_labels:
																		if model_class.__name__ == "YolosForObjectDetection":
																									_SCREAMING_SNAKE_CASE		:						str			  =    []
																									for i in range(self.model_tester.batch_size):
																																_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    {}
																																_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    torch.ones(
																																    size=(self.model_tester.n_targets,)      ,    device=_A      ,    dtype=torch.long)
																																_SCREAMING_SNAKE_CASE		:						Tuple			  =    torch.ones(
																																    self.model_tester.n_targets      ,    4      ,    device=_A      ,    dtype=torch.float)
																																labels.append(_A)
																									_SCREAMING_SNAKE_CASE		:						int			  =    labels

											return inputs_dict





				def 		_lowerCAmelCase		(      self							:							Tuple):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						str			  =    YolosModelTester(self)
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    ConfigTester(self      ,    config_class=_A      ,    has_text_modality=_A      ,    hidden_size=3_7)





				def 		_lowerCAmelCase		(      self							:							Tuple):



											"""simple docstring"""







											self.config_tester.run_common_tests()





				def 		_lowerCAmelCase		(      self							:							Union[str, Any]):



											"""simple docstring"""







											pass





				def 		_lowerCAmelCase		(      self							:							Optional[int]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						Any			  =    self.model_tester.prepare_config_and_inputs_for_common()

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





				def 		_lowerCAmelCase		(      self							:							Optional[Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						str			  =    self.model_tester.prepare_config_and_inputs_for_common()

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

																		_SCREAMING_SNAKE_CASE		:						Any			  =    ["""pixel_values"""]
																		self.assertListEqual(arg_names[:1]      ,    _A)





				def 		_lowerCAmelCase		(      self							:							str):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						str			  =    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_A)





				def 		_lowerCAmelCase		(      self							:							List[str]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    self.model_tester.prepare_config_and_inputs_for_common()
											_SCREAMING_SNAKE_CASE		:						str			  =    True

											# in YOLOS, the seq_len is different
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    self.model_tester.expected_seq_len
											for model_class in self.all_model_classes:
																		_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    True
																		_SCREAMING_SNAKE_CASE		:						Dict			  =    False
																		_SCREAMING_SNAKE_CASE		:						Tuple			  =    True
																		_SCREAMING_SNAKE_CASE		:						int			  =    model_class(_A)
																		model.to(_A)
																		model.eval()
																		with torch.no_grad():
																									_SCREAMING_SNAKE_CASE		:						Tuple			  =    model(**self._prepare_for_class(_A      ,    _A))
																		_SCREAMING_SNAKE_CASE		:						int			  =    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"]
																		_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    True
																		_SCREAMING_SNAKE_CASE		:						List[Any]			  =    model_class(_A)
																		model.to(_A)
																		model.eval()
																		with torch.no_grad():
																									_SCREAMING_SNAKE_CASE		:						Dict			  =    model(**self._prepare_for_class(_A      ,    _A))
																		_SCREAMING_SNAKE_CASE		:						Tuple			  =    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]      ,    )
																		_SCREAMING_SNAKE_CASE		:						Tuple			  =    len(_A)

																		# Check attention is always last and order is fine
																		_SCREAMING_SNAKE_CASE		:						Dict			  =    True
																		_SCREAMING_SNAKE_CASE		:						List[Any]			  =    True
																		_SCREAMING_SNAKE_CASE		:						int			  =    model_class(_A)
																		model.to(_A)
																		model.eval()
																		with torch.no_grad():
																									_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    model(**self._prepare_for_class(_A      ,    _A))

																		_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    1
																		self.assertEqual(out_len + added_hidden_states      ,    len(_A))

																		_SCREAMING_SNAKE_CASE		:						int			  =    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 		_lowerCAmelCase		(      self							:							Tuple):



											"""simple docstring"""







											def check_hidden_states_output(_A							:							Optional[Any]      ,    _A							:							Optional[int]      ,    _A							:							str):
																		_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    model_class(_A)
																		model.to(_A)
																		model.eval()

																		with torch.no_grad():
																									_SCREAMING_SNAKE_CASE		:						Tuple			  =    model(**self._prepare_for_class(_A      ,    _A))

																		_SCREAMING_SNAKE_CASE		:						List[str]			  =    outputs.hidden_states

																		_SCREAMING_SNAKE_CASE		:						int			  =    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
																		_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    self.model_tester.expected_seq_len

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

											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						int			  =    self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		_SCREAMING_SNAKE_CASE		:						List[Any]			  =    True
																		check_hidden_states_output(_A      ,    _A      ,    _A)

																		# check that output_hidden_states also work using config
																		del inputs_dict["output_hidden_states"]
																		_SCREAMING_SNAKE_CASE		:						Tuple			  =    True

																		check_hidden_states_output(_A      ,    _A      ,    _A)





				def 		_lowerCAmelCase		(      self							:							Optional[int]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Tuple			  =    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_object_detection(*_A)





				@slow
				def 		_lowerCAmelCase		(      self							:							Union[str, Any]):



											"""simple docstring"""







											for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		_SCREAMING_SNAKE_CASE		:						List[str]			  =    YolosModel.from_pretrained(_A)
																		self.assertIsNotNone(_A)



def      lowerCamelCase_()->    List[str]:
							_SCREAMING_SNAKE_CASE		:						List[Any]			  =    Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""  )
							return image




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







				"""simple docstring"""






				@cached_property
				def 		_lowerCAmelCase		(      self							:							Optional[Any]):



											"""simple docstring"""







											return AutoImageProcessor.from_pretrained("""hustvl/yolos-small""") if is_vision_available() else None





				@slow
				def 		_lowerCAmelCase		(      self							:							Optional[int]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						int			  =    YolosForObjectDetection.from_pretrained("""hustvl/yolos-small""").to(_A)

											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    self.default_image_processor
											_SCREAMING_SNAKE_CASE		:						Any			  =    prepare_img()
											_SCREAMING_SNAKE_CASE		:						Any			  =    image_processor(images=_A      ,    return_tensors="""pt""").to(_A)

											# forward pass
											with torch.no_grad():
																		_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    model(inputs.pixel_values)

											# verify outputs
											_SCREAMING_SNAKE_CASE		:						str			  =    torch.Size((1, 1_0_0, 9_2))
											self.assertEqual(outputs.logits.shape      ,    _A)

											_SCREAMING_SNAKE_CASE		:						List[str]			  =    torch.tensor(
											    [[-24.0_248, -10.3_024, -14.8_290], [-42.0_392, -16.8_200, -27.4_334], [-27.2_743, -11.8_154, -18.7_148]]      ,    device=_A      ,    )
											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    torch.tensor(
											    [[0.2_559, 0.5_455, 0.4_706], [0.2_989, 0.7_279, 0.1_875], [0.7_732, 0.4_017, 0.4_462]]      ,    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
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    image_processor.post_process_object_detection(
											    _A      ,    threshold=0.3      ,    target_sizes=[image.size[::-1]])[0]
											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    torch.tensor([0.9_994, 0.9_790, 0.9_964, 0.9_972, 0.9_861]).to(_A)
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    [7_5, 7_5, 1_7, 6_3, 1_7]
											_SCREAMING_SNAKE_CASE		:						str			  =    torch.tensor([335.0_609, 79.3_848, 375.4_216, 187.2_495]).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"""





import torch
from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel




class  _snake_case		(    __snake_case ):







				"""simple docstring"""






				a			=						"M-CLIP"





				def __init__(      self							:							Optional[Any]      ,    _A							:							List[str]=1_0_2_4      ,    _A							:							Union[str, Any]=7_6_8      ,    **_A							:							Optional[Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Tuple			  =    transformerDimSize
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    imageDimSize
											super().__init__(**_A)




class  _snake_case		(    __snake_case ):







				"""simple docstring"""






				a			=						MCLIPConfig





				def __init__(      self							:							Dict      ,    _A							:							Optional[Any]      ,    *_A							:							Any      ,    **_A							:							Dict):



											"""simple docstring"""







											super().__init__(_A      ,    *_A      ,    **_A)
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    XLMRobertaModel(_A)
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    torch.nn.Linear(
											    in_features=config.transformerDimensions      ,    out_features=config.numDims)





				def 		_lowerCAmelCase		(      self							:							Union[str, Any]      ,    _A							:							str      ,    _A							:							int):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						str			  =    self.transformer(input_ids=_A      ,    attention_mask=_A)[0]
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    (embs * attention_mask.unsqueeze(2)).sum(dim=1) / attention_mask.sum(dim=1)[:, None]
											return self.LinearTransformation(_A), embs


