






'''simple docstring'''

import inspect
import unittest

from transformers import ViTMSNConfig
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, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch
      from torch import nn

      from transformers import ViTMSNForImageClassification, ViTMSNModel
      from transformers.models.vit_msn.modeling_vit_msn import VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
      from PIL import Image

      from transformers import ViTImageProcessor




class      A     :






 """simple docstring"""





 def __init__(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase=13      ,  __lowerCAmelCase=30      ,  __lowerCAmelCase=2      ,  __lowerCAmelCase=3      ,  __lowerCAmelCase=True      ,  __lowerCAmelCase=True      ,  __lowerCAmelCase=32      ,  __lowerCAmelCase=5      ,  __lowerCAmelCase=4      ,  __lowerCAmelCase=37      ,  __lowerCAmelCase="gelu"      ,  __lowerCAmelCase=0.1      ,  __lowerCAmelCase=0.1      ,  __lowerCAmelCase=10      ,  __lowerCAmelCase=0.02      ,  __lowerCAmelCase=None      ,  ):
       UpperCamelCase_					: List[Any]            =				parent
       UpperCamelCase_					: List[str]            =				batch_size
       UpperCamelCase_					: List[Any]            =				image_size
       UpperCamelCase_					: Dict            =				patch_size
       UpperCamelCase_					: int            =				num_channels
       UpperCamelCase_					: List[str]            =				is_training
       UpperCamelCase_					: str            =				use_labels
       UpperCamelCase_					: str            =				hidden_size
       UpperCamelCase_					: Tuple            =				num_hidden_layers
       UpperCamelCase_					: str            =				num_attention_heads
       UpperCamelCase_					: Optional[Any]            =				intermediate_size
       UpperCamelCase_					: Tuple            =				hidden_act
       UpperCamelCase_					: str            =				hidden_dropout_prob
       UpperCamelCase_					: Optional[Any]            =				attention_probs_dropout_prob
       UpperCamelCase_					: Optional[Any]            =				type_sequence_label_size
       UpperCamelCase_					: Optional[int]            =				initializer_range
       UpperCamelCase_					: List[Any]            =				scope

       # in ViT MSN, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
       UpperCamelCase_					: Optional[Any]            =				(image_size // patch_size) ** 2
       UpperCamelCase_					: Dict            =				num_patches + 1
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: Optional[int]            =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

       UpperCamelCase_					: Tuple            =				None
       if self.use_labels:
             UpperCamelCase_					: List[str]            =				ids_tensor([self.batch_size]      ,  self.type_sequence_label_size			)

       UpperCamelCase_					: str            =				self.get_config()

       return config, pixel_values, labels
 def 	_UpperCAmelCase							(				self			):
       return ViTMSNConfig(
           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      ,  initializer_range=self.initializer_range      ,  )
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase			):
       UpperCamelCase_					: Optional[int]            =				ViTMSNModel(config=__lowerCAmelCase			)
       model.to(__lowerCAmelCase			)
       model.eval()
       UpperCamelCase_					: Optional[int]            =				model(__lowerCAmelCase			)
       self.parent.assertEqual(result.last_hidden_state.shape      ,  (self.batch_size, self.seq_length, self.hidden_size)			)
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase			):
       UpperCamelCase_					: Optional[int]            =				self.type_sequence_label_size
       UpperCamelCase_					: Dict            =				ViTMSNForImageClassification(__lowerCAmelCase			)
       model.to(__lowerCAmelCase			)
       model.eval()
       UpperCamelCase_					: int            =				model(__lowerCAmelCase      ,  labels=__lowerCAmelCase			)
       print("""Pixel and labels shape: {pixel_values.shape}, {labels.shape}"""			)
       print("""Labels: {labels}"""			)
       self.parent.assertEqual(result.logits.shape      ,  (self.batch_size, self.type_sequence_label_size)			)

       # test greyscale images
       UpperCamelCase_					: Optional[Any]            =				1
       UpperCamelCase_					: str            =				ViTMSNForImageClassification(__lowerCAmelCase			)
       model.to(__lowerCAmelCase			)
       model.eval()

       UpperCamelCase_					: List[str]            =				floats_tensor([self.batch_size, 1, self.image_size, self.image_size]			)
       UpperCamelCase_					: Tuple            =				model(__lowerCAmelCase			)
       self.parent.assertEqual(result.logits.shape      ,  (self.batch_size, self.type_sequence_label_size)			)





 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: Any            =				self.prepare_config_and_inputs()
       UpperCamelCase_     ,      UpperCamelCase_     ,      UpperCamelCase_					: Dict            =				config_and_inputs
       UpperCamelCase_					: List[str]            =				{"""pixel_values""": pixel_values}
       return config, inputs_dict






@require_torch
class      A     (   SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__, unittest.TestCase    ):






 """simple docstring"""





 __a      :    Union[str, Any]			  =    (ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else ()
 __a      :    List[Any]			  =    (
     {'''feature-extraction''': ViTMSNModel, '''image-classification''': ViTMSNForImageClassification}
     if is_torch_available()
     else {}
 )

 __a      :    Any			  =    False
 __a      :    Union[str, Any]			  =    False
 __a      :    Optional[Any]			  =    False
 __a      :    List[Any]			  =    False
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: Optional[int]            =				ViTMSNModelTester(self			)
       UpperCamelCase_					: List[Any]            =				ConfigTester(self      ,  config_class=__lowerCAmelCase      ,  has_text_modality=__lowerCAmelCase      ,  hidden_size=37			)
 def 	_UpperCAmelCase							(				self			):
       self.config_tester.run_common_tests()
 @unittest.skip(reason="""ViTMSN does not use inputs_embeds"""			)
 def 	_UpperCAmelCase							(				self			):
       pass
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_     ,      UpperCamelCase_					: Dict            =				self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
             UpperCamelCase_					: int            =				model_class(__lowerCAmelCase			)
             self.assertIsInstance(model.get_input_embeddings()      ,  (nn.Module)			)
             UpperCamelCase_					: Union[str, Any]            =				model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(__lowerCAmelCase      ,  nn.Linear			)			)
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_     ,      UpperCamelCase_					: Tuple            =				self.model_tester.prepare_config_and_inputs_for_common()

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

             UpperCamelCase_					: int            =				["""pixel_values"""]
             self.assertListEqual(arg_names[:1]      ,  __lowerCAmelCase			)
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: int            =				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__lowerCAmelCase			)
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: List[str]            =				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*__lowerCAmelCase			)





 @slow
 def 	_UpperCAmelCase							(				self			):
       for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             UpperCamelCase_					: List[Any]            =				ViTMSNModel.from_pretrained(__lowerCAmelCase			)
             self.assertIsNotNone(__lowerCAmelCase			)






def 				snake_case       (  )							->					Any:





      """simple docstring"""

      UpperCamelCase_					: Optional[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 	_UpperCAmelCase							(				self			):
       return ViTImageProcessor.from_pretrained("""facebook/vit-msn-small"""			) if is_vision_available() else None





 @slow
 def 	_UpperCAmelCase							(				self			):
       torch.manual_seed(2			)
       UpperCamelCase_					: Dict            =				ViTMSNForImageClassification.from_pretrained("""facebook/vit-msn-small"""			).to(__lowerCAmelCase			)

       UpperCamelCase_					: Optional[Any]            =				self.default_image_processor
       UpperCamelCase_					: Tuple            =				prepare_img()
       UpperCamelCase_					: List[str]            =				image_processor(images=__lowerCAmelCase      ,  return_tensors="""pt"""			).to(__lowerCAmelCase			)

       # forward pass
       with torch.no_grad():
             UpperCamelCase_					: List[Any]            =				model(**__lowerCAmelCase			)

       # verify the logits
       UpperCamelCase_					: Any            =				torch.Size((1, 10_00)			)
       self.assertEqual(outputs.logits.shape      ,  __lowerCAmelCase			)

       UpperCamelCase_					: Optional[Any]            =				torch.tensor([-0.08_03, -0.44_54, -0.23_75]			).to(__lowerCAmelCase			)

       self.assertTrue(torch.allclose(outputs.logits[0, :3]      ,  __lowerCAmelCase      ,  atol=1E-4			)			)














'''simple docstring'''

from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


UpperCamelCase      					={
    "configuration_clap": [
        "CLAP_PRETRAINED_MODEL_ARCHIVE_LIST",
        "ClapAudioConfig",
        "ClapConfig",
        "ClapTextConfig",
    ],
    "processing_clap": ["ClapProcessor"],
}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      UpperCamelCase      					=[
          "CLAP_PRETRAINED_MODEL_ARCHIVE_LIST",
          "ClapModel",
          "ClapPreTrainedModel",
          "ClapTextModel",
          "ClapTextModelWithProjection",
          "ClapAudioModel",
          "ClapAudioModelWithProjection",
      ]
      UpperCamelCase      					=["ClapFeatureExtractor"]

if TYPE_CHECKING:
      from .configuration_clap import (
          CLAP_PRETRAINED_MODEL_ARCHIVE_LIST,
          ClapAudioConfig,
          ClapConfig,
          ClapTextConfig,
      )
      from .processing_clap import ClapProcessor

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .feature_extraction_clap import ClapFeatureExtractor
            from .modeling_clap import (
                CLAP_PRETRAINED_MODEL_ARCHIVE_LIST,
                ClapAudioModel,
                ClapAudioModelWithProjection,
                ClapModel,
                ClapPreTrainedModel,
                ClapTextModel,
                ClapTextModelWithProjection,
            )


else:
      import sys

      UpperCamelCase      					=_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)






