



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






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






							def snake_case_  (   self      ):
									a_     :      int 						=     floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]      )

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

									a_     :      Dict 						=     self.get_config()

									return config, pixel_values, labels






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






							def snake_case_  (   self			,     a_			,     a_			,     a_      ):
									a_     :      Dict 						=     YolosModel(config=a_      )
									model.to(a_      )
									model.eval()
									a_     :      Tuple 						=     model(a_      )
									self.parent.assertEqual(
									    result.last_hidden_state.shape			,     (self.batch_size, self.expected_seq_len, self.hidden_size)      )






							def snake_case_  (   self			,     a_			,     a_			,     a_      ):
									a_     :      Dict 						=     YolosForObjectDetection(a_      )
									model.to(a_      )
									model.eval()

									a_     :      Optional[int] 						=     model(pixel_values=a_      )
									a_     :      Tuple 						=     model(a_      )

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

									a_     :      Tuple 						=     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 snake_case_  (   self      ):
									a_     :      Optional[int] 						=     self.prepare_config_and_inputs()
									a_						,				a_						,				a_     :      Tuple 						=     config_and_inputs
									a_     :      Any 						=     {"pixel_values": pixel_values}
									return config, inputs_dict




@require_torch
class 			snake_case_						(						a_     ,a_     ,unittest.TestCase				):
							__lowerCAmelCase    					=					(YolosModel, YolosForObjectDetection) if is_torch_available() else ()
							__lowerCAmelCase    					=					(
							    {"feature-extraction": YolosModel, "object-detection": YolosForObjectDetection} if is_torch_available() else {}
							)

							__lowerCAmelCase    					=					False
							__lowerCAmelCase    					=					False
							__lowerCAmelCase    					=					False
							__lowerCAmelCase    					=					False






							def snake_case_  (   self			,     a_			,     a_			,     a_=False      ):
									a_     :      List[str] 						=     super()._prepare_for_class(a_			,     a_			,     return_labels=a_      )

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

									return inputs_dict






							def snake_case_  (   self      ):
									a_     :      str 						=     YolosModelTester(self      )
									a_     :      List[Any] 						=     ConfigTester(self			,     config_class=a_			,     has_text_modality=a_			,     hidden_size=3_7      )






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






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






							def snake_case_  (   self      ):
									a_						,				a_     :      Any 						=     self.model_tester.prepare_config_and_inputs_for_common()

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






							def snake_case_  (   self      ):
									a_						,				a_     :      Any 						=     self.model_tester.prepare_config_and_inputs_for_common()

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

											a_     :      Tuple 						=     ["pixel_values"]
											self.assertListEqual(arg_names[:1]			,     a_      )






							def snake_case_  (   self      ):
									a_     :      List[Any] 						=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*a_      )






							def snake_case_  (   self      ):
									a_						,				a_     :      Any 						=     self.model_tester.prepare_config_and_inputs_for_common()
									a_     :      int 						=     True

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

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

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

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

											a_     :      Optional[Any] 						=     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 snake_case_  (   self      ):
									def check_hidden_states_output(a_			,     a_			,     a_      ):
											a_     :      Tuple 						=     model_class(a_      )
											model.to(a_      )
											model.eval()

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

											a_     :      List[str] 						=     outputs.hidden_states

											a_     :      Dict 						=     getattr(
											    self.model_tester			,     "expected_num_hidden_layers"			,     self.model_tester.num_hidden_layers + 1      )
											self.assertEqual(len(a_      )			,     a_      )

											# YOLOS has a different seq_length
											a_     :      Tuple 						=     self.model_tester.expected_seq_len

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

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

									for model_class in self.all_model_classes:
											a_     :      Optional[int] 						=     True
											check_hidden_states_output(a_			,     a_			,     a_      )

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

											check_hidden_states_output(a_			,     a_			,     a_      )






							def snake_case_  (   self      ):
									a_     :      Tuple 						=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_object_detection(*a_      )






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



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




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






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






							@slow
							def snake_case_  (   self      ):
									a_     :      int 						=     YolosForObjectDetection.from_pretrained("hustvl/yolos-small"      ).to(a_      )

									a_     :      Any 						=     self.default_image_processor
									a_     :      Dict 						=     prepare_img()
									a_     :      Tuple 						=     image_processor(images=a_			,     return_tensors="pt"      ).to(a_      )

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

									# verify outputs
									a_     :      int 						=     torch.Size((1, 1_0_0, 9_2)      )
									self.assertEqual(outputs.logits.shape			,     a_      )

									a_     :      Optional[int] 						=     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_			,     )
									a_     :      Tuple 						=     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
									a_     :      str 						=     image_processor.post_process_object_detection(
									    a_			,     threshold=0.3			,     target_sizes=[image.size[::-1]]      )[0]
									a_     :      Tuple 						=     torch.tensor([0.9_994, 0.9_790, 0.9_964, 0.9_972, 0.9_861]      ).to(a_      )
									a_     :      Dict 						=     [7_5, 7_5, 1_7, 6_3, 1_7]
									a_     :      Dict 						=     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"""
from ...configuration_utils import PretrainedConfig
from ...utils import logging


SCREAMING_SNAKE_CASE_				= logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_				= {
    """google/realm-cc-news-pretrained-embedder""": (
        """https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/config.json"""
    ),
    """google/realm-cc-news-pretrained-encoder""": (
        """https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/config.json"""
    ),
    """google/realm-cc-news-pretrained-scorer""": (
        """https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/config.json"""
    ),
    """google/realm-cc-news-pretrained-openqa""": (
        """https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/config.json"""
    ),
    """google/realm-orqa-nq-openqa""": """https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/config.json""",
    """google/realm-orqa-nq-reader""": """https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/config.json""",
    """google/realm-orqa-wq-openqa""": """https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/config.json""",
    """google/realm-orqa-wq-reader""": """https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/config.json""",
    # See all REALM models at https://huggingface.co/models?filter=realm
}




class 			snake_case_						(						a_				):
							__lowerCAmelCase    					=					"realm"






							def __init__(   self			,     a_=3_0_5_2_2			,     a_=7_6_8			,     a_=1_2_8			,     a_=1_2			,     a_=1_2			,     a_=8			,     a_=3_0_7_2			,     a_="gelu_new"			,     a_=0.1			,     a_=0.1			,     a_=5_1_2			,     a_=2			,     a_=0.02			,     a_=1e-12			,     a_=2_5_6			,     a_=1_0			,     a_=1e-3			,     a_=5			,     a_=3_2_0			,     a_=1_3_3_5_3_7_1_8			,     a_=5_0_0_0			,     a_=1			,     a_=0			,     a_=2			,     **a_			,     ):
									super().__init__(pad_token_id=a_			,     bos_token_id=a_			,     eos_token_id=a_			,     **a_      )

									# Common config
									a_     :      Optional[int] 						=     vocab_size
									a_     :      List[Any] 						=     max_position_embeddings
									a_     :      Optional[Any] 						=     hidden_size
									a_     :      Optional[Any] 						=     retriever_proj_size
									a_     :      List[str] 						=     num_hidden_layers
									a_     :      List[Any] 						=     num_attention_heads
									a_     :      Tuple 						=     num_candidates
									a_     :      str 						=     intermediate_size
									a_     :      Optional[int] 						=     hidden_act
									a_     :      List[str] 						=     hidden_dropout_prob
									a_     :      List[str] 						=     attention_probs_dropout_prob
									a_     :      Tuple 						=     initializer_range
									a_     :      Tuple 						=     type_vocab_size
									a_     :      str 						=     layer_norm_eps

									# Reader config
									a_     :      str 						=     span_hidden_size
									a_     :      Union[str, Any] 						=     max_span_width
									a_     :      Tuple 						=     reader_layer_norm_eps
									a_     :      List[Any] 						=     reader_beam_size
									a_     :      str 						=     reader_seq_len

									# Retrieval config
									a_     :      str 						=     num_block_records
									a_     :      int 						=     searcher_beam_size