
import inspect
import unittest

from transformers import BitConfig
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_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch
    from torch import nn

    from transformers import BitBackbone, BitForImageClassification, BitImageProcessor, BitModel
    from transformers.models.bit.modeling_bit import BIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image




class 					_lowerCamelCase			:





     """simple docstring"""







     def __init__(				self    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE=3    ,    __SCREAMING_SNAKE_CASE=3_2    ,    __SCREAMING_SNAKE_CASE=3    ,    __SCREAMING_SNAKE_CASE=1_0    ,    __SCREAMING_SNAKE_CASE=[8, 1_6, 3_2, 6_4]    ,    __SCREAMING_SNAKE_CASE=[1, 1, 2, 1]    ,    __SCREAMING_SNAKE_CASE=True    ,    __SCREAMING_SNAKE_CASE=True    ,    __SCREAMING_SNAKE_CASE="relu"    ,    __SCREAMING_SNAKE_CASE=3    ,    __SCREAMING_SNAKE_CASE=None    ,    __SCREAMING_SNAKE_CASE=["stage2", "stage3", "stage4"]    ,    __SCREAMING_SNAKE_CASE=[2, 3, 4]    ,    __SCREAMING_SNAKE_CASE=1    ,    )	-> Optional[int]:




           """simple docstring"""







           UpperCamelCase__     : Union[str, Any]											=					parent
           UpperCamelCase__     : Tuple											=					batch_size
           UpperCamelCase__     : str											=					image_size
           UpperCamelCase__     : Dict											=					num_channels
           UpperCamelCase__     : List[Any]											=					embeddings_size
           UpperCamelCase__     : str											=					hidden_sizes
           UpperCamelCase__     : Optional[Any]											=					depths
           UpperCamelCase__     : str											=					is_training
           UpperCamelCase__     : str											=					use_labels
           UpperCamelCase__     : Any											=					hidden_act
           UpperCamelCase__     : Optional[int]											=					num_labels
           UpperCamelCase__     : Any											=					scope
           UpperCamelCase__     : List[str]											=					len(__SCREAMING_SNAKE_CASE	)
           UpperCamelCase__     : str											=					out_features
           UpperCamelCase__     : Tuple											=					out_indices
           UpperCamelCase__     : Union[str, Any]											=					num_groups






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




           """simple docstring"""







           UpperCamelCase__     : Dict											=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

           UpperCamelCase__     : Optional[Any]											=					None
           if self.use_labels:
                 UpperCamelCase__     : int											=					ids_tensor([self.batch_size]    ,    self.num_labels	)

           UpperCamelCase__     : str											=					self.get_config()

           return config, pixel_values, labels






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> str:




           """simple docstring"""







           return BitConfig(
               num_channels=self.num_channels    ,    embeddings_size=self.embeddings_size    ,    hidden_sizes=self.hidden_sizes    ,    depths=self.depths    ,    hidden_act=self.hidden_act    ,    num_labels=self.num_labels    ,    out_features=self.out_features    ,    out_indices=self.out_indices    ,    num_groups=self.num_groups    ,    )






     def  __SCREAMING_SNAKE_CASE      (				self    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	)	-> Dict:




           """simple docstring"""







           UpperCamelCase__     : List[str]											=					BitModel(config=__SCREAMING_SNAKE_CASE	)
           model.to(__SCREAMING_SNAKE_CASE	)
           model.eval()
           UpperCamelCase__     : Dict											=					model(__SCREAMING_SNAKE_CASE	)
           self.parent.assertEqual(
               result.last_hidden_state.shape    ,    (self.batch_size, self.hidden_sizes[-1], self.image_size // 3_2, self.image_size // 3_2)    ,    )






     def  __SCREAMING_SNAKE_CASE      (				self    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	)	-> Dict:




           """simple docstring"""







           UpperCamelCase__     : Optional[Any]											=					self.num_labels
           UpperCamelCase__     : List[str]											=					BitForImageClassification(__SCREAMING_SNAKE_CASE	)
           model.to(__SCREAMING_SNAKE_CASE	)
           model.eval()
           UpperCamelCase__     : Optional[int]											=					model(__SCREAMING_SNAKE_CASE    ,    labels=__SCREAMING_SNAKE_CASE	)
           self.parent.assertEqual(result.logits.shape    ,    (self.batch_size, self.num_labels)	)






     def  __SCREAMING_SNAKE_CASE      (				self    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	)	-> Dict:




           """simple docstring"""







           UpperCamelCase__     : List[str]											=					BitBackbone(config=__SCREAMING_SNAKE_CASE	)
           model.to(__SCREAMING_SNAKE_CASE	)
           model.eval()
           UpperCamelCase__     : Union[str, Any]											=					model(__SCREAMING_SNAKE_CASE	)

           # verify feature maps
           self.parent.assertEqual(len(result.feature_maps	)    ,    len(config.out_features	)	)
           self.parent.assertListEqual(list(result.feature_maps[0].shape	)    ,    [self.batch_size, self.hidden_sizes[1], 4, 4]	)

           # verify channels
           self.parent.assertEqual(len(model.channels	)    ,    len(config.out_features	)	)
           self.parent.assertListEqual(model.channels    ,    config.hidden_sizes[1:]	)

           # verify backbone works with out_features=None
           UpperCamelCase__     : List[Any]											=					None
           UpperCamelCase__     : int											=					BitBackbone(config=__SCREAMING_SNAKE_CASE	)
           model.to(__SCREAMING_SNAKE_CASE	)
           model.eval()
           UpperCamelCase__     : List[Any]											=					model(__SCREAMING_SNAKE_CASE	)

           # verify feature maps
           self.parent.assertEqual(len(result.feature_maps	)    ,    1	)
           self.parent.assertListEqual(list(result.feature_maps[0].shape	)    ,    [self.batch_size, self.hidden_sizes[-1], 1, 1]	)

           # verify channels
           self.parent.assertEqual(len(model.channels	)    ,    1	)
           self.parent.assertListEqual(model.channels    ,    [config.hidden_sizes[-1]]	)






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




           """simple docstring"""







           UpperCamelCase__     : Dict											=					self.prepare_config_and_inputs()
           UpperCamelCase__					,UpperCamelCase__					,UpperCamelCase__     : int											=					config_and_inputs
           UpperCamelCase__     : str											=					{'''pixel_values''': pixel_values}
           return config, inputs_dict




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





     """simple docstring"""







     SCREAMING_SNAKE_CASE_		 =	(BitModel, BitForImageClassification, BitBackbone) if is_torch_available() else ()
     SCREAMING_SNAKE_CASE_		 =	(
         {'''feature-extraction''': BitModel, '''image-classification''': BitForImageClassification}
         if is_torch_available()
         else {}
     )

     SCREAMING_SNAKE_CASE_		 =	False
     SCREAMING_SNAKE_CASE_		 =	False
     SCREAMING_SNAKE_CASE_		 =	False
     SCREAMING_SNAKE_CASE_		 =	False
     SCREAMING_SNAKE_CASE_		 =	False






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> str:




           """simple docstring"""







           UpperCamelCase__     : Optional[Any]											=					BitModelTester(self	)
           UpperCamelCase__     : str											=					ConfigTester(self    ,    config_class=__SCREAMING_SNAKE_CASE    ,    has_text_modality=__SCREAMING_SNAKE_CASE	)






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> List[Any]:




           """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  __SCREAMING_SNAKE_CASE      (				self	)	-> Any:




           """simple docstring"""







           return






     @unittest.skip(reason='''Bit does not output attentions'''	)
     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Union[str, Any]:




           """simple docstring"""







           pass






     @unittest.skip(reason='''Bit does not use inputs_embeds'''	)
     def  __SCREAMING_SNAKE_CASE      (				self	)	-> str:




           """simple docstring"""







           pass






     @unittest.skip(reason='''Bit does not support input and output embeddings'''	)
     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Dict:




           """simple docstring"""







           pass






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




           """simple docstring"""







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

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

                 UpperCamelCase__     : Union[str, Any]											=					['''pixel_values''']
                 self.assertListEqual(arg_names[:1]    ,    __SCREAMING_SNAKE_CASE	)






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Any:




           """simple docstring"""







           UpperCamelCase__     : str											=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE	)






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> List[Any]:




           """simple docstring"""







           UpperCamelCase__     : Tuple											=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_backbone(*__SCREAMING_SNAKE_CASE	)






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Dict:




           """simple docstring"""







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

           for model_class in self.all_model_classes:
                 UpperCamelCase__     : Any											=					model_class(config=__SCREAMING_SNAKE_CASE	)
                 for name, module in model.named_modules():
                       if isinstance(__SCREAMING_SNAKE_CASE    ,    (nn.BatchNormad, nn.GroupNorm)	):
                             self.assertTrue(
                                 torch.all(module.weight == 1	)    ,    msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''    ,    )
                             self.assertTrue(
                                 torch.all(module.bias == 0	)    ,    msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''    ,    )






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Tuple:




           """simple docstring"""







           def check_hidden_states_output(__SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	):
                 UpperCamelCase__     : Union[str, Any]											=					model_class(__SCREAMING_SNAKE_CASE	)
                 model.to(__SCREAMING_SNAKE_CASE	)
                 model.eval()

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

                 UpperCamelCase__     : Optional[int]											=					outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

                 UpperCamelCase__     : List[Any]											=					self.model_tester.num_stages
                 self.assertEqual(len(__SCREAMING_SNAKE_CASE	)    ,    expected_num_stages + 1	)

                 # Bit's feature maps are of shape (batch_size, num_channels, height, width)
                 self.assertListEqual(
                     list(hidden_states[0].shape[-2:]	)    ,    [self.model_tester.image_size // 4, self.model_tester.image_size // 4]    ,    )

           UpperCamelCase__					,UpperCamelCase__     : List[str]											=					self.model_tester.prepare_config_and_inputs_for_common()
           UpperCamelCase__     : Any											=					['''preactivation''', '''bottleneck''']
           for model_class in self.all_model_classes:
                 for layer_type in layers_type:
                       UpperCamelCase__     : Any											=					layer_type
                       UpperCamelCase__     : int											=					True
                       check_hidden_states_output(__SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	)

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

                       check_hidden_states_output(__SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	)






     @unittest.skip(reason='''Bit does not use feedforward chunking'''	)
     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Dict:




           """simple docstring"""







           pass






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> str:




           """simple docstring"""







           UpperCamelCase__     : str											=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*__SCREAMING_SNAKE_CASE	)






     @slow
     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Tuple:




           """simple docstring"""







           for model_name in BIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 UpperCamelCase__     : Any											=					BitModel.from_pretrained(__SCREAMING_SNAKE_CASE	)
                 self.assertIsNotNone(__SCREAMING_SNAKE_CASE	)


def 				SCREAMING_SNAKE_CASE_   (      ):
      UpperCamelCase__     : Union[str, Any]											=					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
      return image




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





     """simple docstring"""







     @cached_property
     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Any:




           """simple docstring"""







           return (
               BitImageProcessor.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]	) if is_vision_available() else None
           )






     @slow
     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Optional[Any]:




           """simple docstring"""







           UpperCamelCase__     : List[Any]											=					BitForImageClassification.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]	).to(__SCREAMING_SNAKE_CASE	)

           UpperCamelCase__     : int											=					self.default_image_processor
           UpperCamelCase__     : List[str]											=					prepare_img()
           UpperCamelCase__     : Optional[Any]											=					image_processor(images=__SCREAMING_SNAKE_CASE    ,    return_tensors='''pt'''	).to(__SCREAMING_SNAKE_CASE	)

           # forward pass
           with torch.no_grad():
                 UpperCamelCase__     : List[str]											=					model(**__SCREAMING_SNAKE_CASE	)

           # verify the logits
           UpperCamelCase__     : List[str]											=					torch.Size((1, 1_0_0_0)	)
           self.assertEqual(outputs.logits.shape    ,    __SCREAMING_SNAKE_CASE	)

           UpperCamelCase__     : Dict											=					torch.tensor([[-0.6526, -0.5263, -1.4398]]	).to(__SCREAMING_SNAKE_CASE	)

           self.assertTrue(torch.allclose(outputs.logits[0, :3]    ,    __SCREAMING_SNAKE_CASE    ,    atol=1e-4	)	)




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





     """simple docstring"""







     SCREAMING_SNAKE_CASE_		 =	(BitBackbone,) if is_torch_available() else ()
     SCREAMING_SNAKE_CASE_		 =	BitConfig

     SCREAMING_SNAKE_CASE_		 =	False






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> List[Any]:




           """simple docstring"""







           UpperCamelCase__     : Optional[int]											=					BitModelTester(self	)




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    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE=1_3    ,    __SCREAMING_SNAKE_CASE=[3_0, 3_0]    ,    __SCREAMING_SNAKE_CASE=2    ,    __SCREAMING_SNAKE_CASE=3    ,    __SCREAMING_SNAKE_CASE=True    ,    __SCREAMING_SNAKE_CASE=True    ,    __SCREAMING_SNAKE_CASE=3_2    ,    __SCREAMING_SNAKE_CASE=5    ,    __SCREAMING_SNAKE_CASE=4    ,    __SCREAMING_SNAKE_CASE=3_7    ,    __SCREAMING_SNAKE_CASE="gelu"    ,    __SCREAMING_SNAKE_CASE=0.1    ,    __SCREAMING_SNAKE_CASE=0.1    ,    __SCREAMING_SNAKE_CASE=1_0    ,    __SCREAMING_SNAKE_CASE=0.02    ,    __SCREAMING_SNAKE_CASE=3    ,    __SCREAMING_SNAKE_CASE=None    ,    __SCREAMING_SNAKE_CASE=8    ,    __SCREAMING_SNAKE_CASE=1_0    ,    )	-> List[str]:




           """simple docstring"""







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






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> List[Any]:




           """simple docstring"""







           UpperCamelCase__     : Dict											=					floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]	)

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

           UpperCamelCase__     : Any											=					self.get_config()

           return config, pixel_values, labels






     def  __SCREAMING_SNAKE_CASE      (				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=__SCREAMING_SNAKE_CASE    ,    initializer_range=self.initializer_range    ,    num_detection_tokens=self.num_detection_tokens    ,    num_labels=self.num_labels    ,    )






     def  __SCREAMING_SNAKE_CASE      (				self    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	)	-> List[Any]:




           """simple docstring"""







           UpperCamelCase__     : Dict											=					YolosModel(config=__SCREAMING_SNAKE_CASE	)
           model.to(__SCREAMING_SNAKE_CASE	)
           model.eval()
           UpperCamelCase__     : Optional[Any]											=					model(__SCREAMING_SNAKE_CASE	)
           self.parent.assertEqual(
               result.last_hidden_state.shape    ,    (self.batch_size, self.expected_seq_len, self.hidden_size)	)






     def  __SCREAMING_SNAKE_CASE      (				self    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	)	-> str:




           """simple docstring"""







           UpperCamelCase__     : Optional[int]											=					YolosForObjectDetection(__SCREAMING_SNAKE_CASE	)
           model.to(__SCREAMING_SNAKE_CASE	)
           model.eval()

           UpperCamelCase__     : str											=					model(pixel_values=__SCREAMING_SNAKE_CASE	)
           UpperCamelCase__     : Union[str, Any]											=					model(__SCREAMING_SNAKE_CASE	)

           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__     : Optional[Any]											=					model(pixel_values=__SCREAMING_SNAKE_CASE    ,    labels=__SCREAMING_SNAKE_CASE	)

           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  __SCREAMING_SNAKE_CASE      (				self	)	-> Optional[Any]:




           """simple docstring"""







           UpperCamelCase__     : str											=					self.prepare_config_and_inputs()
           UpperCamelCase__					,UpperCamelCase__					,UpperCamelCase__     : Union[str, Any]											=					config_and_inputs
           UpperCamelCase__     : Union[str, Any]											=					{'''pixel_values''': pixel_values}
           return config, inputs_dict




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





     """simple docstring"""







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

     SCREAMING_SNAKE_CASE_		 =	False
     SCREAMING_SNAKE_CASE_		 =	False
     SCREAMING_SNAKE_CASE_		 =	False
     SCREAMING_SNAKE_CASE_		 =	False






     def  __SCREAMING_SNAKE_CASE      (				self    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE=False	)	-> List[str]:




           """simple docstring"""







           UpperCamelCase__     : int											=					super()._prepare_for_class(__SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    return_labels=__SCREAMING_SNAKE_CASE	)

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

           return inputs_dict






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> int:




           """simple docstring"""







           UpperCamelCase__     : Any											=					YolosModelTester(self	)
           UpperCamelCase__     : Union[str, Any]											=					ConfigTester(self    ,    config_class=__SCREAMING_SNAKE_CASE    ,    has_text_modality=__SCREAMING_SNAKE_CASE    ,    hidden_size=3_7	)






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Tuple:




           """simple docstring"""







           self.config_tester.run_common_tests()






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> str:




           """simple docstring"""







           pass






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> str:




           """simple docstring"""







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

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






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Tuple:




           """simple docstring"""







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

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

                 UpperCamelCase__     : List[Any]											=					['''pixel_values''']
                 self.assertListEqual(arg_names[:1]    ,    __SCREAMING_SNAKE_CASE	)






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> List[Any]:




           """simple docstring"""







           UpperCamelCase__     : Any											=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE	)






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




           """simple docstring"""







           UpperCamelCase__					,UpperCamelCase__     : List[Any]											=					self.model_tester.prepare_config_and_inputs_for_common()
           UpperCamelCase__     : List[str]											=					True

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

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

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

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

                 UpperCamelCase__     : Optional[int]											=					1
                 self.assertEqual(out_len + added_hidden_states    ,    len(__SCREAMING_SNAKE_CASE	)	)

                 UpperCamelCase__     : int											=					outputs.attentions

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




           """simple docstring"""







           def check_hidden_states_output(__SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	):
                 UpperCamelCase__     : str											=					model_class(__SCREAMING_SNAKE_CASE	)
                 model.to(__SCREAMING_SNAKE_CASE	)
                 model.eval()

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

                 UpperCamelCase__     : Tuple											=					outputs.hidden_states

                 UpperCamelCase__     : Tuple											=					getattr(
                     self.model_tester    ,    '''expected_num_hidden_layers'''    ,    self.model_tester.num_hidden_layers + 1	)
                 self.assertEqual(len(__SCREAMING_SNAKE_CASE	)    ,    __SCREAMING_SNAKE_CASE	)

                 # YOLOS has a different seq_length
                 UpperCamelCase__     : Dict											=					self.model_tester.expected_seq_len

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

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

           for model_class in self.all_model_classes:
                 UpperCamelCase__     : Optional[Any]											=					True
                 check_hidden_states_output(__SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	)

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

                 check_hidden_states_output(__SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	)






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Tuple:




           """simple docstring"""







           UpperCamelCase__     : List[Any]											=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_object_detection(*__SCREAMING_SNAKE_CASE	)






     @slow
     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Union[str, Any]:




           """simple docstring"""







           for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 UpperCamelCase__     : Dict											=					YolosModel.from_pretrained(__SCREAMING_SNAKE_CASE	)
                 self.assertIsNotNone(__SCREAMING_SNAKE_CASE	)


def 				SCREAMING_SNAKE_CASE_   (      ):
      UpperCamelCase__     : Dict											=					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
      return image




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





     """simple docstring"""







     @cached_property
     def  __SCREAMING_SNAKE_CASE      (				self	)	-> str:




           """simple docstring"""







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






     @slow
     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Dict:




           """simple docstring"""







           UpperCamelCase__     : List[Any]											=					YolosForObjectDetection.from_pretrained('''hustvl/yolos-small'''	).to(__SCREAMING_SNAKE_CASE	)

           UpperCamelCase__     : List[Any]											=					self.default_image_processor
           UpperCamelCase__     : int											=					prepare_img()
           UpperCamelCase__     : Optional[Any]											=					image_processor(images=__SCREAMING_SNAKE_CASE    ,    return_tensors='''pt'''	).to(__SCREAMING_SNAKE_CASE	)

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

           # verify outputs
           UpperCamelCase__     : Dict											=					torch.Size((1, 1_0_0, 9_2)	)
           self.assertEqual(outputs.logits.shape    ,    __SCREAMING_SNAKE_CASE	)

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

           # verify postprocessing
           UpperCamelCase__     : Any											=					image_processor.post_process_object_detection(
               __SCREAMING_SNAKE_CASE    ,    threshold=0.3    ,    target_sizes=[image.size[::-1]]	)[0]
           UpperCamelCase__     : List[Any]											=					torch.tensor([0.9994, 0.9790, 0.9964, 0.9972, 0.9861]	).to(__SCREAMING_SNAKE_CASE	)
           UpperCamelCase__     : List[str]											=					[7_5, 7_5, 1_7, 6_3, 1_7]
           UpperCamelCase__     : List[str]											=					torch.tensor([335.0609, 79.3848, 375.4216, 187.2495]	).to(__SCREAMING_SNAKE_CASE	)

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


