
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							:Tuple		,    lowerCAmelCase__							:str		,    lowerCAmelCase__							:str=13		,    lowerCAmelCase__							:List[Any]=[30, 30]		,    lowerCAmelCase__							:Dict=2		,    lowerCAmelCase__							:Union[str, Any]=3		,    lowerCAmelCase__							:int=True		,    lowerCAmelCase__							:str=True		,    lowerCAmelCase__							:Optional[Any]=32		,    lowerCAmelCase__							:Tuple=5		,    lowerCAmelCase__							:List[str]=4		,    lowerCAmelCase__							:List[str]=37		,    lowerCAmelCase__							:str="gelu"		,    lowerCAmelCase__							:List[str]=0.1		,    lowerCAmelCase__							:Tuple=0.1		,    lowerCAmelCase__							:Any=10		,    lowerCAmelCase__							:Optional[Any]=0.02		,    lowerCAmelCase__							:Any=3		,    lowerCAmelCase__							:List[str]=None		,    lowerCAmelCase__							:Any=8		,    lowerCAmelCase__							:Dict=10		,    )  ->List[Any]:
										lowercase         =      parent
										lowercase         =      batch_size
										lowercase         =      image_size
										lowercase         =      patch_size
										lowercase         =      num_channels
										lowercase         =      is_training
										lowercase         =      use_labels
										lowercase         =      hidden_size
										lowercase         =      num_hidden_layers
										lowercase         =      num_attention_heads
										lowercase         =      intermediate_size
										lowercase         =      hidden_act
										lowercase         =      hidden_dropout_prob
										lowercase         =      attention_probs_dropout_prob
										lowercase         =      type_sequence_label_size
										lowercase         =      initializer_range
										lowercase         =      num_labels
										lowercase         =      scope
										lowercase         =      n_targets
										lowercase         =      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
										lowercase         =      (image_size[1] // patch_size) * (image_size[0] // patch_size)
										lowercase         =      num_patches + 1 + self.num_detection_tokens





						def        SCREAMING_SNAKE_CASE(			self							:str     )  ->Optional[Any]:
										lowercase         =      floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]     )

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

										lowercase         =      self.get_config()

										return config, pixel_values, labels





						def        SCREAMING_SNAKE_CASE(			self							:Any     )  ->Any:
										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        SCREAMING_SNAKE_CASE(			self							:int		,    lowerCAmelCase__							:Any		,    lowerCAmelCase__							:Union[str, Any]		,    lowerCAmelCase__							:int     )  ->Dict:
										lowercase         =      YolosModel(config=lowerCAmelCase__     )
										model.to(lowerCAmelCase__     )
										model.eval()
										lowercase         =      model(lowerCAmelCase__     )
										self.parent.assertEqual(
										    result.last_hidden_state.shape		,    (self.batch_size, self.expected_seq_len, self.hidden_size)     )





						def        SCREAMING_SNAKE_CASE(			self							:int		,    lowerCAmelCase__							:Optional[Any]		,    lowerCAmelCase__							:int		,    lowerCAmelCase__							:List[Any]     )  ->Dict:
										lowercase         =      YolosForObjectDetection(lowerCAmelCase__     )
										model.to(lowerCAmelCase__     )
										model.eval()

										lowercase         =      model(pixel_values=lowerCAmelCase__     )
										lowercase         =      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)     )

										lowercase         =      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        SCREAMING_SNAKE_CASE(			self							:Any     )  ->Dict:
										lowercase         =      self.prepare_config_and_inputs()
										lowercase     ,	lowercase     ,	lowercase         =      config_and_inputs
										lowercase         =      {"pixel_values": pixel_values}
										return config, inputs_dict





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







						'''simple docstring'''



						UpperCamelCase      :  Optional[int]				   =					(YolosModel, YolosForObjectDetection) if is_torch_available() else ()
						UpperCamelCase      :  str				   =					(
						    {'''feature-extraction''': YolosModel, '''object-detection''': YolosForObjectDetection} if is_torch_available() else {}
						)

						UpperCamelCase      :  int				   =					False
						UpperCamelCase      :  Any				   =					False
						UpperCamelCase      :  Optional[Any]				   =					False
						UpperCamelCase      :  Optional[int]				   =					False





						def        SCREAMING_SNAKE_CASE(			self							:List[Any]		,    lowerCAmelCase__							:Tuple		,    lowerCAmelCase__							:str		,    lowerCAmelCase__							:Union[str, Any]=False     )  ->str:
										lowercase         =      super()._prepare_for_class(lowerCAmelCase__		,    lowerCAmelCase__		,    return_labels=lowerCAmelCase__     )

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

										return inputs_dict





						def        SCREAMING_SNAKE_CASE(			self							:str     )  ->Dict:
										lowercase         =      YolosModelTester(self     )
										lowercase         =      ConfigTester(self		,    config_class=lowerCAmelCase__		,    has_text_modality=lowerCAmelCase__		,    hidden_size=37     )





						def        SCREAMING_SNAKE_CASE(			self							:Optional[int]     )  ->Any:
										self.config_tester.run_common_tests()





						def        SCREAMING_SNAKE_CASE(			self							:List[str]     )  ->int:
										# YOLOS does not use inputs_embeds
										pass





						def        SCREAMING_SNAKE_CASE(			self							:Optional[Any]     )  ->Tuple:
										lowercase     ,	lowercase         =      self.model_tester.prepare_config_and_inputs_for_common()

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





						def        SCREAMING_SNAKE_CASE(			self							:Any     )  ->Dict:
										lowercase     ,	lowercase         =      self.model_tester.prepare_config_and_inputs_for_common()

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

														lowercase         =      ["pixel_values"]
														self.assertListEqual(arg_names[:1]		,    lowerCAmelCase__     )





						def        SCREAMING_SNAKE_CASE(			self							:Dict     )  ->Tuple:
										lowercase         =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCAmelCase__     )





						def        SCREAMING_SNAKE_CASE(			self							:Union[str, Any]     )  ->Tuple:
										lowercase     ,	lowercase         =      self.model_tester.prepare_config_and_inputs_for_common()
										lowercase         =      True

										# in YOLOS, the seq_len is different
										lowercase         =      self.model_tester.expected_seq_len
										for model_class in self.all_model_classes:
														lowercase         =      True
														lowercase         =      False
														lowercase         =      True
														lowercase         =      model_class(lowerCAmelCase__     )
														model.to(lowerCAmelCase__     )
														model.eval()
														with torch.no_grad():
																		lowercase         =      model(**self._prepare_for_class(lowerCAmelCase__		,    lowerCAmelCase__     )     )
														lowercase         =      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"]
														lowercase         =      True
														lowercase         =      model_class(lowerCAmelCase__     )
														model.to(lowerCAmelCase__     )
														model.eval()
														with torch.no_grad():
																		lowercase         =      model(**self._prepare_for_class(lowerCAmelCase__		,    lowerCAmelCase__     )     )
														lowercase         =      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]		,    )
														lowercase         =      len(lowerCAmelCase__     )

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

														lowercase         =      1
														self.assertEqual(out_len + added_hidden_states		,    len(lowerCAmelCase__     )     )

														lowercase         =      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        SCREAMING_SNAKE_CASE(			self							:List[Any]     )  ->Any:
										def check_hidden_states_output(lowerCAmelCase__							:Any		,    lowerCAmelCase__							:Optional[Any]		,    lowerCAmelCase__							:Any     ):
														lowercase         =      model_class(lowerCAmelCase__     )
														model.to(lowerCAmelCase__     )
														model.eval()

														with torch.no_grad():
																		lowercase         =      model(**self._prepare_for_class(lowerCAmelCase__		,    lowerCAmelCase__     )     )

														lowercase         =      outputs.hidden_states

														lowercase         =      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
														lowercase         =      self.model_tester.expected_seq_len

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

										lowercase     ,	lowercase         =      self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
														lowercase         =      True
														check_hidden_states_output(lowerCAmelCase__		,    lowerCAmelCase__		,    lowerCAmelCase__     )

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

														check_hidden_states_output(lowerCAmelCase__		,    lowerCAmelCase__		,    lowerCAmelCase__     )





						def        SCREAMING_SNAKE_CASE(			self							:List[Any]     )  ->Union[str, Any]:
										lowercase         =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_object_detection(*lowerCAmelCase__     )





						@slow
						def        SCREAMING_SNAKE_CASE(			self							:Any     )  ->Optional[int]:
										for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														lowercase         =      YolosModel.from_pretrained(lowerCAmelCase__     )
														self.assertIsNotNone(lowerCAmelCase__     )




def  __snake_case							(	):







				'''simple docstring'''


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





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







						'''simple docstring'''



						@cached_property
						def        SCREAMING_SNAKE_CASE(			self							:int     )  ->str:
										return AutoImageProcessor.from_pretrained("hustvl/yolos-small"     ) if is_vision_available() else None





						@slow
						def        SCREAMING_SNAKE_CASE(			self							:Optional[Any]     )  ->Any:
										lowercase         =      YolosForObjectDetection.from_pretrained("hustvl/yolos-small"     ).to(lowerCAmelCase__     )

										lowercase         =      self.default_image_processor
										lowercase         =      prepare_img()
										lowercase         =      image_processor(images=lowerCAmelCase__		,    return_tensors="pt"     ).to(lowerCAmelCase__     )

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

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

										lowercase         =      torch.tensor(
										    [[-24.02_48, -10.30_24, -14.82_90], [-42.03_92, -16.82_00, -27.43_34], [-27.27_43, -11.81_54, -18.71_48]]		,    device=lowerCAmelCase__		,    )
										lowercase         =      torch.tensor(
										    [[0.25_59, 0.54_55, 0.47_06], [0.29_89, 0.72_79, 0.18_75], [0.77_32, 0.40_17, 0.44_62]]		,    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
										lowercase         =      image_processor.post_process_object_detection(
										    lowerCAmelCase__		,    threshold=0.3		,    target_sizes=[image.size[::-1]]     )[0]
										lowercase         =      torch.tensor([0.99_94, 0.97_90, 0.99_64, 0.99_72, 0.98_61]     ).to(lowerCAmelCase__     )
										lowercase         =      [75, 75, 17, 63, 17]
										lowercase         =      torch.tensor([3_35.06_09, 79.38_48, 3_75.42_16, 1_87.24_95]     ).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__     )     )


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


_snake_case      :							List[str]     	=    logging.get_logger(__name__)

_snake_case      :							int     	=    {
    "google/switch-base-8": "https://huggingface.co/google/switch-base-8/blob/main/config.json",
}





class   UpperCamelCase_      (      __a		):







						'''simple docstring'''



						UpperCamelCase      :  List[str]				   =					'''switch_transformers'''
						UpperCamelCase      :  Dict				   =					['''past_key_values''']
						UpperCamelCase      :  Union[str, Any]				   =					{'''hidden_size''': '''d_model''', '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers'''}





						def __init__(			self							:int		,    lowerCAmelCase__							:str=32128		,    lowerCAmelCase__							:Union[str, Any]=768		,    lowerCAmelCase__							:Tuple=64		,    lowerCAmelCase__							:Any=2048		,    lowerCAmelCase__							:Optional[int]=64		,    lowerCAmelCase__							:Any=12		,    lowerCAmelCase__							:Any=3		,    lowerCAmelCase__							:Union[str, Any]=12		,    lowerCAmelCase__							:Tuple=3		,    lowerCAmelCase__							:str=12		,    lowerCAmelCase__							:Dict=8		,    lowerCAmelCase__							:Union[str, Any]=False		,    lowerCAmelCase__							:List[str]=0.01		,    lowerCAmelCase__							:Union[str, Any]="float32"		,    lowerCAmelCase__							:Optional[int]=False		,    lowerCAmelCase__							:Optional[Any]=32		,    lowerCAmelCase__							:int=128		,    lowerCAmelCase__							:Optional[Any]=0.1		,    lowerCAmelCase__							:Optional[Any]=1E-6		,    lowerCAmelCase__							:Any=0.0_01		,    lowerCAmelCase__							:Union[str, Any]=0.0_01		,    lowerCAmelCase__							:List[Any]=1.0		,    lowerCAmelCase__							:Optional[Any]="relu"		,    lowerCAmelCase__							:Optional[int]=True		,    lowerCAmelCase__							:List[Any]=False		,    lowerCAmelCase__							:int=True		,    lowerCAmelCase__							:int=0		,    lowerCAmelCase__							:Optional[Any]=1		,    **lowerCAmelCase__							:List[Any]		,    )  ->Dict:
										lowercase         =      vocab_size
										lowercase         =      d_model
										lowercase         =      d_kv
										lowercase         =      d_ff

										lowercase         =      num_sparse_encoder_layers

										lowercase         =      num_layers
										lowercase         =      (
										    num_decoder_layers if num_decoder_layers is not None else self.num_layers
										)  # default = symmetry
										lowercase         =      num_sparse_decoder_layers

										# This tells us, each how many encoder layer we'll have to set a sparse layer.
										if self.num_sparse_encoder_layers > 0:
														lowercase         =      self.num_layers // self.num_sparse_encoder_layers
										else:
														lowercase         =      self.num_layers  # HACK: this will create 0 sparse layers

										# This tells us, each how many encoder layer we'll have to set a sparse layer.
										if self.num_sparse_decoder_layers > 0:
														lowercase         =      self.num_decoder_layers // self.num_sparse_decoder_layers
										else:
														lowercase         =      self.num_decoder_layers  # HACK: this will create 0 sparse layers

										lowercase         =      num_heads
										lowercase         =      num_experts
										lowercase         =      expert_capacity
										lowercase         =      router_bias
										lowercase         =      router_jitter_noise
										if router_dtype not in ["float32", "float16", "bfloat16"]:
														raise ValueError(F'''`router_dtype` must be one of \'float32\', \'float16\' or \'bfloat16\', got {router_dtype}'''     )
										lowercase         =      router_dtype

										lowercase         =      router_ignore_padding_tokens
										lowercase         =      relative_attention_num_buckets
										lowercase         =      relative_attention_max_distance

										lowercase         =      dropout_rate
										lowercase         =      layer_norm_epsilon
										lowercase         =      initializer_factor
										lowercase         =      feed_forward_proj
										lowercase         =      use_cache
										lowercase         =      add_router_probs

										lowercase         =      router_z_loss_coef
										lowercase         =      router_aux_loss_coef

										lowercase         =      self.feed_forward_proj.split("-"     )
										lowercase         =      act_info[-1]
										lowercase         =      act_info[0] == "gated"

										if len(lowerCAmelCase__     ) > 1 and act_info[0] != "gated" or len(lowerCAmelCase__     ) > 2:
														raise ValueError(
														    F'''`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer.'''
														    "Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. "
														    "'gated-gelu' or 'relu'"     )

										# for backwards compatibility
										if feed_forward_proj == "gated-gelu":
														lowercase         =      "gelu_new"

										super().__init__(
										    pad_token_id=lowerCAmelCase__		,    eos_token_id=lowerCAmelCase__		,    is_encoder_decoder=lowerCAmelCase__		,    **lowerCAmelCase__		,    )
