






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






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






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]] )

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

          UpperCAmelCase          =	self.get_config()

          return config, pixel_values, labels






    def     _UpperCamelCase							(			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     _UpperCamelCase							(			self     ,A     ,A     ,A ):
          UpperCAmelCase          =	YolosModel(config=A )
          model.to(A )
          model.eval()
          UpperCAmelCase          =	model(A )
          self.parent.assertEqual(
              result.last_hidden_state.shape     ,(self.batch_size, self.expected_seq_len, self.hidden_size) )






    def     _UpperCamelCase							(			self     ,A     ,A     ,A ):
          UpperCAmelCase          =	YolosForObjectDetection(A )
          model.to(A )
          model.eval()

          UpperCAmelCase          =	model(pixel_values=A )
          UpperCAmelCase          =	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) )

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







@require_torch
class  lowerCamelCase__ (	snake_case   ,					snake_case   ,					unittest.TestCase			):
    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     _UpperCamelCase							(			self     ,A     ,A     ,A=False ):
          UpperCAmelCase          =	super()._prepare_for_class(A     ,A     ,return_labels=A )

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

          return inputs_dict






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	YolosModelTester(self )
          UpperCAmelCase          =	ConfigTester(self     ,config_class=A     ,has_text_modality=A     ,hidden_size=37 )






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






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






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase      , UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs_for_common()

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






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase      , UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs_for_common()

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

                UpperCAmelCase          =	["""pixel_values"""]
                self.assertListEqual(arg_names[:1]     ,A )






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*A )






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase      , UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs_for_common()
          UpperCAmelCase          =	True

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

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

                UpperCAmelCase          =	1
                self.assertEqual(out_len + added_hidden_states     ,len(A ) )

                UpperCAmelCase          =	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     _UpperCamelCase							(			self ):
          def check_hidden_states_output(A     ,A     ,A ):
                UpperCAmelCase          =	model_class(A )
                model.to(A )
                model.eval()

                with torch.no_grad():
                      UpperCAmelCase          =	model(**self._prepare_for_class(A     ,A ) )

                UpperCAmelCase          =	outputs.hidden_states

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

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

          UpperCAmelCase      , UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                UpperCAmelCase          =	True
                check_hidden_states_output(A     ,A     ,A )

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

                check_hidden_states_output(A     ,A     ,A )






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_object_detection(*A )






    @slow
    def     _UpperCamelCase							(			self ):
          for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                UpperCAmelCase          =	YolosModel.from_pretrained(A )
                self.assertIsNotNone(A )







def _a			(		):







      """simple docstring"""






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







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






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






    @slow
    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	YolosForObjectDetection.from_pretrained("""hustvl/yolos-small""" ).to(A )

          UpperCAmelCase          =	self.default_image_processor
          UpperCAmelCase          =	prepare_img()
          UpperCAmelCase          =	image_processor(images=A     ,return_tensors="""pt""" ).to(A )

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

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

          UpperCAmelCase          =	torch.tensor(
              [[-24.0248, -10.3024, -14.8290], [-42.0392, -16.8200, -27.4334], [-27.2743, -11.8154, -18.7148]]     ,device=A     ,)
          UpperCAmelCase          =	torch.tensor(
              [[0.2559, 0.5455, 0.4706], [0.2989, 0.7279, 0.1875], [0.7732, 0.4017, 0.4462]]     ,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
          UpperCAmelCase          =	image_processor.post_process_object_detection(
              A     ,threshold=0.3     ,target_sizes=[image.size[::-1]] )[0]
          UpperCAmelCase          =	torch.tensor([0.9994, 0.9790, 0.9964, 0.9972, 0.9861] ).to(A )
          UpperCAmelCase          =	[75, 75, 17, 63, 17]
          UpperCAmelCase          =	torch.tensor([335.0609, 79.3848, 375.4216, 187.2495] ).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"""







import argparse
import re

import torch
from CLAP import create_model

from transformers import AutoFeatureExtractor, ClapConfig, ClapModel


_UpperCamelCase					      =  {
    """text_branch""": """text_model""",
    """audio_branch""": """audio_model.audio_encoder""",
    """attn""": """attention.self""",
    """self.proj""": """output.dense""",
    """attention.self_mask""": """attn_mask""",
    """mlp.fc1""": """intermediate.dense""",
    """mlp.fc2""": """output.dense""",
    """norm1""": """layernorm_before""",
    """norm2""": """layernorm_after""",
    """bn0""": """batch_norm""",
}

_UpperCamelCase					      =  AutoFeatureExtractor.from_pretrained("""laion/clap-htsat-unfused""", truncation="""rand_trunc""")

def _a			(		_snake_case				,	_snake_case=False							):







      """simple docstring"""






      UpperCAmelCase      , UpperCAmelCase          =	create_model(
          """HTSAT-tiny"""				,	"""roberta"""				,	_snake_case				,	precision="""fp32"""				,	device="""cuda:0""" if torch.cuda.is_available() else """cpu"""				,	enable_fusion=_snake_case				,	fusion_type="""aff_2d""" if enable_fusion else None				,	)
      return model, model_cfg

def _a			(		_snake_case							):







      """simple docstring"""






      UpperCAmelCase          =	{}

      UpperCAmelCase          =	R""".*sequential.(\d+).*"""
      UpperCAmelCase          =	R""".*_projection.(\d+).*"""

      for key, value in state_dict.items():
            # check if any key needs to be modified
            for key_to_modify, new_key in KEYS_TO_MODIFY_MAPPING.items():
                  if key_to_modify in key:
                        UpperCAmelCase          =	key.replace(_snake_case				,	_snake_case							)

            if re.match(_snake_case				,	_snake_case							):
                  # replace sequential layers with list
                  UpperCAmelCase          =	re.match(_snake_case				,	_snake_case							).group(1							)

                  UpperCAmelCase          =	key.replace(F'''sequential.{sequential_layer}.'''				,	F'''layers.{int(_snake_case							)//3}.linear.'''							)
            elif re.match(_snake_case				,	_snake_case							):
                  UpperCAmelCase          =	int(re.match(_snake_case				,	_snake_case							).group(1							)							)

                  # Because in CLAP they use `nn.Sequential`...
                  UpperCAmelCase          =	1 if projecton_layer == 0 else 2

                  UpperCAmelCase          =	key.replace(F'''_projection.{projecton_layer}.'''				,	F'''_projection.linear{transformers_projection_layer}.'''							)

            if "audio" and "qkv" in key:
                  # split qkv into query key and value
                  UpperCAmelCase          =	value
                  UpperCAmelCase          =	mixed_qkv.size(0							) // 3

                  UpperCAmelCase          =	mixed_qkv[:qkv_dim]
                  UpperCAmelCase          =	mixed_qkv[qkv_dim : qkv_dim * 2]
                  UpperCAmelCase          =	mixed_qkv[qkv_dim * 2 :]

                  UpperCAmelCase          =	query_layer
                  UpperCAmelCase          =	key_layer
                  UpperCAmelCase          =	value_layer
            else:
                  UpperCAmelCase          =	value

      return model_state_dict

def _a			(		_snake_case				,	_snake_case				,	_snake_case				,	_snake_case=False							):







      """simple docstring"""






      UpperCAmelCase      , UpperCAmelCase          =	init_clap(_snake_case				,	enable_fusion=_snake_case							)

      clap_model.eval()
      UpperCAmelCase          =	clap_model.state_dict()
      UpperCAmelCase          =	rename_state_dict(_snake_case							)

      UpperCAmelCase          =	ClapConfig()
      UpperCAmelCase          =	enable_fusion
      UpperCAmelCase          =	ClapModel(_snake_case							)

      # ignore the spectrogram embedding layer
      model.load_state_dict(_snake_case				,	strict=_snake_case							)

      model.save_pretrained(_snake_case							)
      transformers_config.save_pretrained(_snake_case							)


if __name__ == "__main__":
  _UpperCamelCase					      =  argparse.ArgumentParser()
  parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""")
  parser.add_argument("""--checkpoint_path""", default=None, type=str, help="""Path to fairseq checkpoint""")
  parser.add_argument("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""")
  parser.add_argument("""--enable_fusion""", action="""store_true""", help="""Whether to enable fusion or not""")
  _UpperCamelCase					      =  parser.parse_args()

  convert_clap_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.enable_fusion)



