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





   """simple docstring"""





   def __init__( self     :		str						,     lowerCAmelCase__     :		List[Any]						,     lowerCAmelCase__     :		Tuple=1_3						,     lowerCAmelCase__     :		str=[3_0, 3_0]						,     lowerCAmelCase__     :		str=2						,     lowerCAmelCase__     :		Optional[Any]=3						,     lowerCAmelCase__     :		Any=True						,     lowerCAmelCase__     :		Optional[Any]=True						,     lowerCAmelCase__     :		Union[str, Any]=3_2						,     lowerCAmelCase__     :		Optional[Any]=5						,     lowerCAmelCase__     :		Union[str, Any]=4						,     lowerCAmelCase__     :		int=3_7						,     lowerCAmelCase__     :		Union[str, Any]="gelu"						,     lowerCAmelCase__     :		List[Any]=0.1						,     lowerCAmelCase__     :		Optional[int]=0.1						,     lowerCAmelCase__     :		List[Any]=1_0						,     lowerCAmelCase__     :		Any=0.02						,     lowerCAmelCase__     :		List[Any]=3						,     lowerCAmelCase__     :		int=None						,     lowerCAmelCase__     :		Dict=8						,     lowerCAmelCase__     :		List[Any]=1_0						,     )       ->    List[str]:







      """simple docstring"""




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




   def 			_lowerCAmelCase		( self     :		str			)       ->    List[str]:







      """simple docstring"""




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

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

      _UpperCAmelCase							:	Union[str, Any]							=   self.get_config()

      return config, pixel_values, labels




   def 			_lowerCAmelCase		( self     :		int			)       ->    List[Any]:







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




   def 			_lowerCAmelCase		( self     :		Optional[int]						,     lowerCAmelCase__     :		Any						,     lowerCAmelCase__     :		Dict						,     lowerCAmelCase__     :		Dict			)       ->    Optional[Any]:







      """simple docstring"""




      _UpperCAmelCase							:	Optional[Any]							=   YolosModel(config=__lowercase			)
      model.to(__lowercase			)
      model.eval()
      _UpperCAmelCase							:	Union[str, Any]							=   model(__lowercase			)
      self.parent.assertEqual(
          result.last_hidden_state.shape						,     (self.batch_size, self.expected_seq_len, self.hidden_size)			)




   def 			_lowerCAmelCase		( self     :		Optional[int]						,     lowerCAmelCase__     :		List[Any]						,     lowerCAmelCase__     :		List[str]						,     lowerCAmelCase__     :		int			)       ->    int:







      """simple docstring"""




      _UpperCAmelCase							:	str							=   YolosForObjectDetection(__lowercase			)
      model.to(__lowercase			)
      model.eval()

      _UpperCAmelCase							:	str							=   model(pixel_values=__lowercase			)
      _UpperCAmelCase							:	Optional[int]							=   model(__lowercase			)

      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							:	Dict							=   model(pixel_values=__lowercase						,     labels=__lowercase			)

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







      """simple docstring"""




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





@require_torch
class     A__							(       UpperCAmelCase_					,		UpperCAmelCase_					,		unittest.TestCase   ):





   """simple docstring"""





   UpperCamelCase_       :						Optional[int]    				=     (YolosModel, YolosForObjectDetection) if is_torch_available() else ()
   UpperCamelCase_       :						Tuple    				=     (
       {"""feature-extraction""": YolosModel, """object-detection""": YolosForObjectDetection} if is_torch_available() else {}
   )

   UpperCamelCase_       :						Optional[Any]    				=     False
   UpperCamelCase_       :						Optional[Any]    				=     False
   UpperCamelCase_       :						Union[str, Any]    				=     False
   UpperCamelCase_       :						List[str]    				=     False




   def 			_lowerCAmelCase		( self     :		Any						,     lowerCAmelCase__     :		int						,     lowerCAmelCase__     :		Optional[Any]						,     lowerCAmelCase__     :		Tuple=False			)       ->    Tuple:







      """simple docstring"""




      _UpperCAmelCase							:	Optional[Any]							=   super()._prepare_for_class(__lowercase						,     __lowercase						,     return_labels=__lowercase			)

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

      return inputs_dict




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







      """simple docstring"""




      _UpperCAmelCase							:	Any							=   YolosModelTester(self			)
      _UpperCAmelCase							:	List[Any]							=   ConfigTester(self						,     config_class=__lowercase						,     has_text_modality=__lowercase						,     hidden_size=3_7			)




   def 			_lowerCAmelCase		( self     :		List[str]			)       ->    Tuple:







      """simple docstring"""




      self.config_tester.run_common_tests()




   def 			_lowerCAmelCase		( self     :		Optional[int]			)       ->    List[str]:







      """simple docstring"""




      pass




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







      """simple docstring"""




      _UpperCAmelCase							:	Dict							=   self.model_tester.prepare_config_and_inputs_for_common()

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




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







      """simple docstring"""




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

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

         _UpperCAmelCase							:	Tuple							=   ['''pixel_values''']
         self.assertListEqual(arg_names[:1]						,     __lowercase			)




   def 			_lowerCAmelCase		( self     :		Optional[int]			)       ->    Any:







      """simple docstring"""




      _UpperCAmelCase							:	List[Any]							=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__lowercase			)




   def 			_lowerCAmelCase		( self     :		Tuple			)       ->    str:







      """simple docstring"""




      _UpperCAmelCase							:	int							=   self.model_tester.prepare_config_and_inputs_for_common()
      _UpperCAmelCase							:	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							:	Dict							=   True
         _UpperCAmelCase							:	List[Any]							=   False
         _UpperCAmelCase							:	Optional[Any]							=   True
         _UpperCAmelCase							:	List[Any]							=   model_class(__lowercase			)
         model.to(__lowercase			)
         model.eval()
         with torch.no_grad():
            _UpperCAmelCase							:	Optional[Any]							=   model(**self._prepare_for_class(__lowercase						,     __lowercase			)			)
         _UpperCAmelCase							:	List[str]							=   outputs.attentions
         self.assertEqual(len(__lowercase			)						,     self.model_tester.num_hidden_layers			)

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

         self.assertListEqual(
             list(attentions[0].shape[-3:]			)						,     [self.model_tester.num_attention_heads, seq_len, seq_len]						,     )
         _UpperCAmelCase							:	int							=   len(__lowercase			)

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

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

         _UpperCAmelCase							:	Union[str, Any]							=   outputs.attentions

         self.assertEqual(len(__lowercase			)						,     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 			_lowerCAmelCase		( self     :		str			)       ->    Optional[Any]:







      """simple docstring"""




      def check_hidden_states_output(lowerCAmelCase__     :		Union[str, Any]						,     lowerCAmelCase__     :		Optional[Any]						,     lowerCAmelCase__     :		List[str]			):
         _UpperCAmelCase							:	List[Any]							=   model_class(__lowercase			)
         model.to(__lowercase			)
         model.eval()

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

         _UpperCAmelCase							:	Any							=   outputs.hidden_states

         _UpperCAmelCase							:	Any							=   getattr(
             self.model_tester						,     "expected_num_hidden_layers"						,     self.model_tester.num_hidden_layers + 1			)
         self.assertEqual(len(__lowercase			)						,     __lowercase			)

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

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

      _UpperCAmelCase							:	Dict							=   self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
         _UpperCAmelCase							:	Tuple							=   True
         check_hidden_states_output(__lowercase						,     __lowercase						,     __lowercase			)

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

         check_hidden_states_output(__lowercase						,     __lowercase						,     __lowercase			)




   def 			_lowerCAmelCase		( self     :		List[str]			)       ->    Any:







      """simple docstring"""




      _UpperCAmelCase							:	Tuple							=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_object_detection(*__lowercase			)




   @slow
   def 			_lowerCAmelCase		( self     :		List[str]			)       ->    Any:







      """simple docstring"""




      for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         _UpperCAmelCase							:	Any							=   YolosModel.from_pretrained(__lowercase			)
         self.assertIsNotNone(__lowercase			)






def    __UpperCAmelCase			(  ):
   _UpperCAmelCase							:	Union[str, Any]							=   Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
   return image






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





   """simple docstring"""





   @cached_property
   def 			_lowerCAmelCase		( self     :		List[str]			)       ->    List[str]:







      """simple docstring"""




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




   @slow
   def 			_lowerCAmelCase		( self     :		int			)       ->    Any:







      """simple docstring"""




      _UpperCAmelCase							:	List[str]							=   YolosForObjectDetection.from_pretrained("hustvl/yolos-small"			).to(__lowercase			)

      _UpperCAmelCase							:	Dict							=   self.default_image_processor
      _UpperCAmelCase							:	Optional[int]							=   prepare_img()
      _UpperCAmelCase							:	Any							=   image_processor(images=__lowercase						,     return_tensors="pt"			).to(__lowercase			)

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

      # verify outputs
      _UpperCAmelCase							:	str							=   torch.Size((1, 1_0_0, 9_2)			)
      self.assertEqual(outputs.logits.shape						,     __lowercase			)

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

      # verify postprocessing
      _UpperCAmelCase							:	Tuple							=   image_processor.post_process_object_detection(
          __lowercase						,     threshold=0.3						,     target_sizes=[image.size[::-1]]			)[0]
      _UpperCAmelCase							:	Optional[int]							=   torch.tensor([0.9994, 0.9790, 0.9964, 0.9972, 0.9861]			).to(__lowercase			)
      _UpperCAmelCase							:	Optional[int]							=   [7_5, 7_5, 1_7, 6_3, 1_7]
      _UpperCAmelCase							:	int							=   torch.tensor([3_3_5.0_6_0_9, 79.3848, 3_7_5.4_2_1_6, 1_8_7.2_4_9_5]			).to(__lowercase			)

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

import torch

from ...utils import logging
from . import AlbertConfig, AlbertForPreTraining, load_tf_weights_in_albert


logging.set_verbosity_info()



def lowerCamelCase  (   SCREAMING_SNAKE_CASE  ,			SCREAMING_SNAKE_CASE  ,			SCREAMING_SNAKE_CASE	):
    '''simple docstring'''







    __UpperCamelCase     :List[Any]			  =		AlbertConfig.from_json_file(SCREAMING_SNAKE_CASE	)
    print(f"""Building PyTorch model from configuration: {config}"""	)
    __UpperCamelCase     :List[str]			  =		AlbertForPreTraining(SCREAMING_SNAKE_CASE	)

    # Load weights from tf checkpoint
    load_tf_weights_in_albert(SCREAMING_SNAKE_CASE  ,			SCREAMING_SNAKE_CASE  ,			SCREAMING_SNAKE_CASE	)

    # Save pytorch-model
    print(f"""Save PyTorch model to {pytorch_dump_path}"""	)
    torch.save(model.state_dict()  ,			SCREAMING_SNAKE_CASE	)


if __name__ == "__main__":
    __lowercase							 =					argparse.ArgumentParser()
    # Required parameters
    parser.add_argument(
        '''--tf_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.'''
    )
    parser.add_argument(
        '''--albert_config_file''',
        default=None,
        type=str,
        required=True,
        help=(
            '''The config json file corresponding to the pre-trained ALBERT model. \n'''
            '''This specifies the model architecture.'''
        ),
    )
    parser.add_argument(
        '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.'''
    )
    __lowercase							 =					parser.parse_args()
    convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.albert_config_file, args.pytorch_dump_path)





