"""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						:
   def __init__(     self	,__UpperCamelCase	,__UpperCamelCase=13	,__UpperCamelCase=[30, 30]	,__UpperCamelCase=2	,__UpperCamelCase=3	,__UpperCamelCase=True	,__UpperCamelCase=True	,__UpperCamelCase=32	,__UpperCamelCase=5	,__UpperCamelCase=4	,__UpperCamelCase=37	,__UpperCamelCase="gelu"	,__UpperCamelCase=0.1	,__UpperCamelCase=0.1	,__UpperCamelCase=10	,__UpperCamelCase=0.02	,__UpperCamelCase=3	,__UpperCamelCase=None	,__UpperCamelCase=8	,__UpperCamelCase=10	,)      -> Tuple:


        '''simple docstring'''


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


        '''simple docstring'''


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

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

        lowercase_      :							Optional[int]						=      self.get_config()

        return config, pixel_values, labels
   def      _UpperCAmelCase  (     self     )      -> int:


        '''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      _UpperCAmelCase  (     self	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase     )      -> Optional[Any]:


        '''simple docstring'''


        lowercase_      :							Optional[int]						=      YolosModel(config=lowerCAmelCase__     )
        model.to(lowerCAmelCase__     )
        model.eval()
        lowercase_      :							Optional[int]						=      model(lowerCAmelCase__     )
        self.parent.assertEqual(
            result.last_hidden_state.shape	,(self.batch_size, self.expected_seq_len, self.hidden_size)     )
   def      _UpperCAmelCase  (     self	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase     )      -> Optional[Any]:


        '''simple docstring'''


        lowercase_      :							List[str]						=      YolosForObjectDetection(lowerCAmelCase__     )
        model.to(lowerCAmelCase__     )
        model.eval()

        lowercase_      :							List[Any]						=      model(pixel_values=lowerCAmelCase__     )
        lowercase_      :							Tuple						=      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_      :							Optional[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      _UpperCAmelCase  (     self     )      -> Optional[Any]:


        '''simple docstring'''


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







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

   lowercase				=    False
   lowercase				=    False
   lowercase				=    False
   lowercase				=    False
   def      _UpperCAmelCase  (     self	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase=False     )      -> Union[str, Any]:


        '''simple docstring'''


        lowercase_      :							Dict						=      super()._prepare_for_class(lowerCAmelCase__	,lowerCAmelCase__	,return_labels=lowerCAmelCase__     )

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

        return inputs_dict
   def      _UpperCAmelCase  (     self     )      -> str:


        '''simple docstring'''


        lowercase_      :							Optional[int]						=      YolosModelTester(self     )
        lowercase_      :							Optional[Any]						=      ConfigTester(self	,config_class=lowerCAmelCase__	,has_text_modality=lowerCAmelCase__	,hidden_size=37     )
   def      _UpperCAmelCase  (     self     )      -> int:


        '''simple docstring'''


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


        '''simple docstring'''


        pass
   def      _UpperCAmelCase  (     self     )      -> List[Any]:


        '''simple docstring'''


        lowercase_      :							str						=      self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             lowercase_      :							int						=      model_class(lowerCAmelCase__     )
             self.assertIsInstance(model.get_input_embeddings()	,(nn.Module)     )
             lowercase_      :							str						=      model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(lowerCAmelCase__	,nn.Linear     )     )
   def      _UpperCAmelCase  (     self     )      -> Optional[Any]:


        '''simple docstring'''


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

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

             lowercase_      :							Union[str, Any]						=      ['''pixel_values''']
             self.assertListEqual(arg_names[:1]	,lowerCAmelCase__     )
   def      _UpperCAmelCase  (     self     )      -> List[str]:


        '''simple docstring'''


        lowercase_      :							Any						=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowerCAmelCase__     )
   def      _UpperCAmelCase  (     self     )      -> str:


        '''simple docstring'''


        lowercase_      :							int						=      self.model_tester.prepare_config_and_inputs_for_common()
        lowercase_      :							Optional[Any]						=      True

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

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

             lowercase_      :							Tuple						=      1
             self.assertEqual(out_len + added_hidden_states	,len(lowerCAmelCase__     )     )

             lowercase_      :							Dict						=      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      _UpperCAmelCase  (     self     )      -> int:


        '''simple docstring'''


        def check_hidden_states_output(__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase     ):
             lowercase_      :							Any						=      model_class(lowerCAmelCase__     )
             model.to(lowerCAmelCase__     )
             model.eval()

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

             lowercase_      :							List[Any]						=      outputs.hidden_states

             lowercase_      :							List[str]						=      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_      :							Optional[Any]						=      self.model_tester.expected_seq_len

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

        lowercase_      :							Optional[Any]						=      self.model_tester.prepare_config_and_inputs_for_common()

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

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

             check_hidden_states_output(lowerCAmelCase__	,lowerCAmelCase__	,lowerCAmelCase__     )
   def      _UpperCAmelCase  (     self     )      -> Dict:


        '''simple docstring'''


        lowercase_      :							str						=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_object_detection(*lowerCAmelCase__     )







   @slow
   def      _UpperCAmelCase  (     self     )      -> int:


        '''simple docstring'''


        for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             lowercase_      :							Tuple						=      YolosModel.from_pretrained(lowerCAmelCase__     )
             self.assertIsNotNone(lowerCAmelCase__     )






def      lowercase__(			):
     lowercase_      :							Optional[int]						=      Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' )
     return image







@require_torch
@require_vision
class 			UpperCamelCase						(     unittest.TestCase    ):
   @cached_property
   def      _UpperCAmelCase  (     self     )      -> Union[str, Any]:


        '''simple docstring'''


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







   @slow
   def      _UpperCAmelCase  (     self     )      -> str:


        '''simple docstring'''


        lowercase_      :							Dict						=      YolosForObjectDetection.from_pretrained('hustvl/yolos-small'     ).to(lowerCAmelCase__     )

        lowercase_      :							Any						=      self.default_image_processor
        lowercase_      :							Dict						=      prepare_img()
        lowercase_      :							str						=      image_processor(images=lowerCAmelCase__	,return_tensors='pt'     ).to(lowerCAmelCase__     )

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

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

        lowercase_      :							int						=      torch.tensor(
            [[-24.0248, -10.3024, -14.8290], [-42.0392, -16.8200, -27.4334], [-27.2743, -11.8154, -18.7148]]	,device=lowerCAmelCase__	,)
        lowercase_      :							Optional[Any]						=      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
        lowercase_      :							int						=      image_processor.post_process_object_detection(
            lowerCAmelCase__	,threshold=0.3	,target_sizes=[image.size[::-1]]     )[0]
        lowercase_      :							List[Any]						=      torch.tensor([0.9994, 0.9790, 0.9964, 0.9972, 0.9861]     ).to(lowerCAmelCase__     )
        lowercase_      :							Tuple						=      [75, 75, 17, 63, 17]
        lowercase_      :							Union[str, Any]						=      torch.tensor([335.0609, 79.3848, 375.4216, 187.2495]     ).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'''



import argparse
from pathlib import Path

import torch

from transformers import OPTConfig, OPTModel
from transformers.utils import logging


logging.set_verbosity_info()
lowerCAmelCase_  :							List[Any]             =    logging.get_logger(__name__)
def        UpperCAmelCase (    A	:					Dict					):
							SCREAMING_SNAKE_CASE       :   List[str]         =      torch.load(A       ,		map_location='''cpu'''					)
							if "model" in sd.keys():
														SCREAMING_SNAKE_CASE       :   List[Any]         =      torch.load(A       ,		map_location='''cpu'''					)['''model''']

							# pop unnecessary weights
							SCREAMING_SNAKE_CASE       :   Optional[int]         =      [
							    '''decoder.version''',
							    '''decoder.output_projection.weight''',
							]
							for key in keys_to_delete:
														if key in sd:
																					sd.pop(A					)

							SCREAMING_SNAKE_CASE       :   int         =      {
							    '''decoder.project_in_dim.weight''': '''decoder.project_in.weight''',
							    '''decoder.project_out_dim.weight''': '''decoder.project_out.weight''',
							    '''decoder.layer_norm.weight''': '''decoder.final_layer_norm.weight''',
							    '''decoder.layer_norm.bias''': '''decoder.final_layer_norm.bias''',
							}
							for old_key, new_key in keys_to_rename.items():
														if old_key in sd:
																					SCREAMING_SNAKE_CASE       :   Optional[int]         =      sd.pop(A					)

							SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      list(sd.keys()					)
							for key in keys:
														if ".qkv_proj." in key:
																					SCREAMING_SNAKE_CASE       :   str         =      sd[key]
																					# We split QKV in separate Q,K,V

																					SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      key.replace('''.qkv_proj.'''       ,		'''.q_proj.'''					)
																					SCREAMING_SNAKE_CASE       :   str         =      key.replace('''.qkv_proj.'''       ,		'''.k_proj.'''					)
																					SCREAMING_SNAKE_CASE       :   Any         =      key.replace('''.qkv_proj.'''       ,		'''.v_proj.'''					)

																					SCREAMING_SNAKE_CASE       :   Tuple         =      value.shape[0]
																					assert depth % 3 == 0
																					# `SequeuceParallelTransformerBlock` has QKV weight is separated in K,V,Q despite the naming:
																					# https://cs.github.com/facebookresearch/metaseq/blob/51871bd73cd04c038f239ea2a26db1d7f6b37927/metaseq/modules/sequence_parallel_transformer_layer.py#L97
																					SCREAMING_SNAKE_CASE       ,SCREAMING_SNAKE_CASE       ,SCREAMING_SNAKE_CASE       :   Tuple         =      torch.split(A       ,		depth // 3       ,		dim=0					)

																					SCREAMING_SNAKE_CASE       :   List[str]         =      q
																					SCREAMING_SNAKE_CASE       :   List[str]         =      k
																					SCREAMING_SNAKE_CASE       :   List[Any]         =      v
																					del sd[key]

							return sd
@torch.no_grad()
def        UpperCAmelCase (    A	:					int       ,		A	:					Union[str, Any]       ,		A	:					Dict=None					):
							SCREAMING_SNAKE_CASE       :   Optional[Any]         =      load_checkpoint(A					)

							if config is not None:
														SCREAMING_SNAKE_CASE       :   Tuple         =      OPTConfig.from_pretrained(A					)
							else:
														SCREAMING_SNAKE_CASE       :   int         =      OPTConfig()

							SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      OPTModel(A					).half().eval()
							model.load_state_dict(A					)

							# Check results
							Path(A					).mkdir(exist_ok=A					)
							model.save_pretrained(A					)


if __name__ == "__main__":
							lowerCAmelCase_  :							List[str]             =    argparse.ArgumentParser()
							# Required parameters
							parser.add_argument(
							    '--fairseq_path',
							    type=str,
							    help=(
							        'path to fairseq checkpoint in correct format. You can find all checkpoints in the correct format here:'
							        ' https://huggingface.co/models?other=opt_metasq'
							    ),
							)
							parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.')
							parser.add_argument('--hf_config', default=None, type=str, help='Define HF config.')
							lowerCAmelCase_  :							str             =    parser.parse_args()
							convert_opt_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, config=args.hf_config)




