

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


		"""simple docstring"""

		def __init__( self    ,			snake_case    ,			snake_case=1_3    ,			snake_case=[3_0, 3_0]    ,			snake_case=2    ,			snake_case=3    ,			snake_case=True    ,			snake_case=True    ,			snake_case=3_2    ,			snake_case=5    ,			snake_case=4    ,			snake_case=3_7    ,			snake_case="gelu"    ,			snake_case=0.1    ,			snake_case=0.1    ,			snake_case=1_0    ,			snake_case=0.02    ,			snake_case=3    ,			snake_case=None    ,			snake_case=8    ,			snake_case=1_0    ,			):


							'''simple docstring'''

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







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							Optional[int]   					=   floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]			)

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

							UpperCAmelCase		:							str   					=   self.get_config()

							return config, pixel_values, labels







		def       A_       ( self			):


							'''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=_lowerCamelCase    ,			initializer_range=self.initializer_range    ,			num_detection_tokens=self.num_detection_tokens    ,			num_labels=self.num_labels    ,			)







		def       A_       ( self    ,			snake_case    ,			snake_case    ,			snake_case			):


							'''simple docstring'''

							UpperCAmelCase		:							List[Any]   					=   YolosModel(config=_lowerCamelCase			)
							model.to(_lowerCamelCase			)
							model.eval()
							UpperCAmelCase		:							Tuple   					=   model(_lowerCamelCase			)
							self.parent.assertEqual(
							    result.last_hidden_state.shape    ,			(self.batch_size, self.expected_seq_len, self.hidden_size)			)







		def       A_       ( self    ,			snake_case    ,			snake_case    ,			snake_case			):


							'''simple docstring'''

							UpperCAmelCase		:							int   					=   YolosForObjectDetection(_lowerCamelCase			)
							model.to(_lowerCamelCase			)
							model.eval()

							UpperCAmelCase		:							Optional[Any]   					=   model(pixel_values=_lowerCamelCase			)
							UpperCAmelCase		:							Dict   					=   model(_lowerCamelCase			)

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

							UpperCAmelCase		:							int   					=   model(pixel_values=_lowerCamelCase    ,			labels=_lowerCamelCase			)

							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       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							Tuple   					=   self.prepare_config_and_inputs()
							UpperCAmelCase					, UpperCAmelCase					, UpperCAmelCase		:							int   					=   config_and_inputs
							UpperCAmelCase		:							Union[str, Any]   					=   {"pixel_values": pixel_values}
							return config, inputs_dict







@require_torch
class   UpperCamelCase__    (   a_							,							a_							,							unittest.TestCase   ):


		"""simple docstring"""

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

		SCREAMING_SNAKE_CASE__  :	List[str]   =							False
		SCREAMING_SNAKE_CASE__  :	str   =							False
		SCREAMING_SNAKE_CASE__  :	Tuple   =							False
		SCREAMING_SNAKE_CASE__  :	int   =							False







		def       A_       ( self    ,			snake_case    ,			snake_case    ,			snake_case=False			):


							'''simple docstring'''

							UpperCAmelCase		:							List[str]   					=   super()._prepare_for_class(_lowerCamelCase    ,			_lowerCamelCase    ,			return_labels=_lowerCamelCase			)

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

							return inputs_dict







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							int   					=   YolosModelTester(self			)
							UpperCAmelCase		:							Tuple   					=   ConfigTester(self    ,			config_class=_lowerCamelCase    ,			has_text_modality=_lowerCamelCase    ,			hidden_size=3_7			)







		def       A_       ( self			):


							'''simple docstring'''

							self.config_tester.run_common_tests()







		def       A_       ( self			):


							'''simple docstring'''

							pass







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase					, UpperCAmelCase		:							List[Any]   					=   self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
												UpperCAmelCase		:							Union[str, Any]   					=   model_class(_lowerCamelCase			)
												self.assertIsInstance(model.get_input_embeddings()    ,			(nn.Module)			)
												UpperCAmelCase		:							Optional[int]   					=   model.get_output_embeddings()
												self.assertTrue(x is None or isinstance(_lowerCamelCase    ,			nn.Linear			)			)







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase					, UpperCAmelCase		:							Optional[int]   					=   self.model_tester.prepare_config_and_inputs_for_common()

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

												UpperCAmelCase		:							int   					=   ["pixel_values"]
												self.assertListEqual(arg_names[:1]    ,			_lowerCamelCase			)







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							Any   					=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_lowerCamelCase			)







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase					, UpperCAmelCase		:							int   					=   self.model_tester.prepare_config_and_inputs_for_common()
							UpperCAmelCase		:							List[str]   					=   True

							# in YOLOS, the seq_len is different
							UpperCAmelCase		:							List[Any]   					=   self.model_tester.expected_seq_len
							for model_class in self.all_model_classes:
												UpperCAmelCase		:							str   					=   True
												UpperCAmelCase		:							Tuple   					=   False
												UpperCAmelCase		:							int   					=   True
												UpperCAmelCase		:							List[str]   					=   model_class(_lowerCamelCase			)
												model.to(_lowerCamelCase			)
												model.eval()
												with torch.no_grad():
																	UpperCAmelCase		:							List[Any]   					=   model(**self._prepare_for_class(_lowerCamelCase    ,			_lowerCamelCase			)			)
												UpperCAmelCase		:							Any   					=   outputs.attentions
												self.assertEqual(len(_lowerCamelCase			)    ,			self.model_tester.num_hidden_layers			)

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

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

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

												UpperCAmelCase		:							List[Any]   					=   1
												self.assertEqual(out_len + added_hidden_states    ,			len(_lowerCamelCase			)			)

												UpperCAmelCase		:							Any   					=   outputs.attentions

												self.assertEqual(len(_lowerCamelCase			)    ,			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       A_       ( self			):


							'''simple docstring'''

							def check_hidden_states_output(snake_case    ,			snake_case    ,			snake_case			):
												UpperCAmelCase		:							Dict   					=   model_class(_lowerCamelCase			)
												model.to(_lowerCamelCase			)
												model.eval()

												with torch.no_grad():
																	UpperCAmelCase		:							Union[str, Any]   					=   model(**self._prepare_for_class(_lowerCamelCase    ,			_lowerCamelCase			)			)

												UpperCAmelCase		:							List[str]   					=   outputs.hidden_states

												UpperCAmelCase		:							Union[str, Any]   					=   getattr(
												    self.model_tester    ,			"expected_num_hidden_layers"    ,			self.model_tester.num_hidden_layers + 1			)
												self.assertEqual(len(_lowerCamelCase			)    ,			_lowerCamelCase			)

												# YOLOS has a different seq_length
												UpperCAmelCase		:							List[str]   					=   self.model_tester.expected_seq_len

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

							UpperCAmelCase					, UpperCAmelCase		:							Optional[int]   					=   self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
												UpperCAmelCase		:							Dict   					=   True
												check_hidden_states_output(_lowerCamelCase    ,			_lowerCamelCase    ,			_lowerCamelCase			)

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

												check_hidden_states_output(_lowerCamelCase    ,			_lowerCamelCase    ,			_lowerCamelCase			)







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							str   					=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_object_detection(*_lowerCamelCase			)







		@slow
		def       A_       ( self			):


							'''simple docstring'''

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





def       lowercase					(     ):



					'''simple docstring'''







					UpperCAmelCase		:							int   					=   Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
					return image







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


		"""simple docstring"""

		@cached_property
		def       A_       ( self			):


							'''simple docstring'''

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







		@slow
		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							Dict   					=   YolosForObjectDetection.from_pretrained("hustvl/yolos-small"			).to(_lowerCamelCase			)

							UpperCAmelCase		:							Optional[Any]   					=   self.default_image_processor
							UpperCAmelCase		:							List[str]   					=   prepare_img()
							UpperCAmelCase		:							Tuple   					=   image_processor(images=_lowerCamelCase    ,			return_tensors="pt"			).to(_lowerCamelCase			)

							# forward pass
							with torch.no_grad():
												UpperCAmelCase		:							Optional[Any]   					=   model(inputs.pixel_values			)

							# verify outputs
							UpperCAmelCase		:							List[str]   					=   torch.Size((1, 1_0_0, 9_2)			)
							self.assertEqual(outputs.logits.shape    ,			_lowerCamelCase			)

							UpperCAmelCase		:							Union[str, Any]   					=   torch.tensor(
							    [[-2_4.0_2_4_8, -1_0.3_0_2_4, -1_4.8_2_9_0], [-4_2.0_3_9_2, -1_6.8_2_0_0, -2_7.4_3_3_4], [-2_7.2_7_4_3, -1_1.8_1_5_4, -1_8.7_1_4_8]]    ,			device=_lowerCamelCase    ,			)
							UpperCAmelCase		:							Tuple   					=   torch.tensor(
							    [[0.2559, 0.5455, 0.4706], [0.2989, 0.7279, 0.1875], [0.7732, 0.4017, 0.4462]]    ,			device=_lowerCamelCase			)
							self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]    ,			_lowerCamelCase    ,			atol=1e-4			)			)
							self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3]    ,			_lowerCamelCase    ,			atol=1e-4			)			)

							# verify postprocessing
							UpperCAmelCase		:							Dict   					=   image_processor.post_process_object_detection(
							    _lowerCamelCase    ,			threshold=0.3    ,			target_sizes=[image.size[::-1]]			)[0]
							UpperCAmelCase		:							str   					=   torch.tensor([0.9994, 0.9790, 0.9964, 0.9972, 0.9861]			).to(_lowerCamelCase			)
							UpperCAmelCase		:							Dict   					=   [7_5, 7_5, 1_7, 6_3, 1_7]
							UpperCAmelCase		:							List[Any]   					=   torch.tensor([3_3_5.0_6_0_9, 7_9.3_8_4_8, 3_7_5.4_2_1_6, 1_8_7.2_4_9_5]			).to(_lowerCamelCase			)

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








"""simple docstring"""



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


_UpperCamelCase		:  Dict						=		{
    'configuration_roberta': ['ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RobertaConfig', 'RobertaOnnxConfig'],
    'tokenization_roberta': ['RobertaTokenizer'],
}

try:
    if not is_tokenizers_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    _UpperCamelCase		:  str						=		['RobertaTokenizerFast']

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    _UpperCamelCase		:  Tuple						=		[
        'ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST',
        'RobertaForCausalLM',
        'RobertaForMaskedLM',
        'RobertaForMultipleChoice',
        'RobertaForQuestionAnswering',
        'RobertaForSequenceClassification',
        'RobertaForTokenClassification',
        'RobertaModel',
        'RobertaPreTrainedModel',
    ]

try:
    if not is_tf_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    _UpperCamelCase		:  List[Any]						=		[
        'TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST',
        'TFRobertaForCausalLM',
        'TFRobertaForMaskedLM',
        'TFRobertaForMultipleChoice',
        'TFRobertaForQuestionAnswering',
        'TFRobertaForSequenceClassification',
        'TFRobertaForTokenClassification',
        'TFRobertaMainLayer',
        'TFRobertaModel',
        'TFRobertaPreTrainedModel',
    ]

try:
    if not is_flax_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    _UpperCamelCase		:  Optional[int]						=		[
        'FlaxRobertaForCausalLM',
        'FlaxRobertaForMaskedLM',
        'FlaxRobertaForMultipleChoice',
        'FlaxRobertaForQuestionAnswering',
        'FlaxRobertaForSequenceClassification',
        'FlaxRobertaForTokenClassification',
        'FlaxRobertaModel',
        'FlaxRobertaPreTrainedModel',
    ]


if TYPE_CHECKING:
    from .configuration_roberta import ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, RobertaConfig, RobertaOnnxConfig
    from .tokenization_roberta import RobertaTokenizer

    try:
        if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .tokenization_roberta_fast import RobertaTokenizerFast

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_roberta import (
            ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
            RobertaForCausalLM,
            RobertaForMaskedLM,
            RobertaForMultipleChoice,
            RobertaForQuestionAnswering,
            RobertaForSequenceClassification,
            RobertaForTokenClassification,
            RobertaModel,
            RobertaPreTrainedModel,
        )

    try:
        if not is_tf_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_tf_roberta import (
            TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
            TFRobertaForCausalLM,
            TFRobertaForMaskedLM,
            TFRobertaForMultipleChoice,
            TFRobertaForQuestionAnswering,
            TFRobertaForSequenceClassification,
            TFRobertaForTokenClassification,
            TFRobertaMainLayer,
            TFRobertaModel,
            TFRobertaPreTrainedModel,
        )

    try:
        if not is_flax_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_flax_roberta import (
            FlaxRobertaForCausalLM,
            FlaxRobertaForMaskedLM,
            FlaxRobertaForMultipleChoice,
            FlaxRobertaForQuestionAnswering,
            FlaxRobertaForSequenceClassification,
            FlaxRobertaForTokenClassification,
            FlaxRobertaModel,
            FlaxRobertaPreTrainedModel,
        )

else:
    import sys

    _UpperCamelCase		:  Optional[Any]						=		_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)


