





'''simple docstring'''




import inspect
import unittest

from datasets import load_dataset
from packaging import version

from transformers import BeitConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_multi_gpu, 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, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch
 from torch import nn

 from transformers import (
     MODEL_MAPPING,
     BeitForImageClassification,
     BeitForMaskedImageModeling,
     BeitForSemanticSegmentation,
     BeitModel,
 )
 from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 import PIL
 from PIL import Image

 from transformers import BeitImageProcessor
class 			_lowerCAmelCase		:






     """simple docstring"""






     def __init__(   self							:			Optional[int]   ,       __snake_case							:			Dict   ,       __snake_case							:			Optional[int]=1_00   ,       __snake_case							:			Tuple=13   ,       __snake_case							:			List[Any]=30   ,       __snake_case							:			Union[str, Any]=2   ,       __snake_case							:			Tuple=3   ,       __snake_case							:			Union[str, Any]=True   ,       __snake_case							:			List[str]=True   ,       __snake_case							:			Optional[int]=32   ,       __snake_case							:			List[str]=4   ,       __snake_case							:			Optional[Any]=4   ,       __snake_case							:			str=37   ,       __snake_case							:			List[Any]="gelu"   ,       __snake_case							:			Union[str, Any]=0.1   ,       __snake_case							:			str=0.1   ,       __snake_case							:			Union[str, Any]=10   ,       __snake_case							:			Any=0.02   ,       __snake_case							:			Tuple=3   ,       __snake_case							:			List[str]=None   ,       __snake_case							:			Dict=[0, 1, 2, 3]   ,       )->	str:
        snake_case 						=   parent
        snake_case 						=   1_00
        snake_case 						=   batch_size
        snake_case 						=   image_size
        snake_case 						=   patch_size
        snake_case 						=   num_channels
        snake_case 						=   is_training
        snake_case 						=   use_labels
        snake_case 						=   hidden_size
        snake_case 						=   num_hidden_layers
        snake_case 						=   num_attention_heads
        snake_case 						=   intermediate_size
        snake_case 						=   hidden_act
        snake_case 						=   hidden_dropout_prob
        snake_case 						=   attention_probs_dropout_prob
        snake_case 						=   type_sequence_label_size
        snake_case 						=   initializer_range
        snake_case 						=   scope
        snake_case 						=   out_indices
        snake_case 						=   num_labels

        # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
        snake_case 						=   (image_size // patch_size) ** 2
        snake_case 						=   num_patches + 1



     def      lowerCAmelCase			(   self							:			int						)->	List[Any]:
        snake_case 						=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

        snake_case 						=   None
        snake_case 						=   None
        if self.use_labels:
           snake_case 						=   ids_tensor([self.batch_size]   ,       self.type_sequence_label_size						)
           snake_case 						=   ids_tensor([self.batch_size, self.image_size, self.image_size]   ,       self.num_labels						)

        snake_case 						=   self.get_config()

        return config, pixel_values, labels, pixel_labels



     def      lowerCAmelCase			(   self							:			Tuple						)->	Optional[Any]:
        return BeitConfig(
            vocab_size=self.vocab_size   ,       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=__snake_case   ,       initializer_range=self.initializer_range   ,       out_indices=self.out_indices   ,       )



     def      lowerCAmelCase			(   self							:			Optional[int]   ,       __snake_case							:			List[Any]   ,       __snake_case							:			Optional[int]   ,       __snake_case							:			Any   ,       __snake_case							:			Dict						)->	List[str]:
        snake_case 						=   BeitModel(config=__snake_case						)
        model.to(__snake_case						)
        model.eval()
        snake_case 						=   model(__snake_case						)
        self.parent.assertEqual(result.last_hidden_state.shape   ,       (self.batch_size, self.seq_length, self.hidden_size)						)



     def      lowerCAmelCase			(   self							:			str   ,       __snake_case							:			int   ,       __snake_case							:			List[Any]   ,       __snake_case							:			Dict   ,       __snake_case							:			Tuple						)->	int:
        snake_case 						=   BeitForMaskedImageModeling(config=__snake_case						)
        model.to(__snake_case						)
        model.eval()
        snake_case 						=   model(__snake_case						)
        self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.seq_length - 1, self.vocab_size)						)



     def      lowerCAmelCase			(   self							:			List[Any]   ,       __snake_case							:			str   ,       __snake_case							:			int   ,       __snake_case							:			Any   ,       __snake_case							:			Union[str, Any]						)->	Any:
        snake_case 						=   self.type_sequence_label_size
        snake_case 						=   BeitForImageClassification(__snake_case						)
        model.to(__snake_case						)
        model.eval()
        snake_case 						=   model(__snake_case   ,       labels=__snake_case						)
        self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.type_sequence_label_size)						)

        # test greyscale images
        snake_case 						=   1
        snake_case 						=   BeitForImageClassification(__snake_case						)
        model.to(__snake_case						)
        model.eval()

        snake_case 						=   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
        snake_case 						=   model(__snake_case   ,       labels=__snake_case						)
        self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.type_sequence_label_size)						)



     def      lowerCAmelCase			(   self							:			Tuple   ,       __snake_case							:			Optional[int]   ,       __snake_case							:			Tuple   ,       __snake_case							:			str   ,       __snake_case							:			str						)->	int:
        snake_case 						=   self.num_labels
        snake_case 						=   BeitForSemanticSegmentation(__snake_case						)
        model.to(__snake_case						)
        model.eval()
        snake_case 						=   model(__snake_case						)
        self.parent.assertEqual(
            result.logits.shape   ,       (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)						)
        snake_case 						=   model(__snake_case   ,       labels=__snake_case						)
        self.parent.assertEqual(
            result.logits.shape   ,       (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)						)



     def      lowerCAmelCase			(   self							:			Tuple						)->	List[str]:
        snake_case 						=   self.prepare_config_and_inputs()
        snake_case						,    snake_case						,    snake_case						,    snake_case 						=   config_and_inputs
        snake_case 						=   {"""pixel_values""": pixel_values}
        return config, inputs_dict




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






     """simple docstring"""






     snake_case_						      =   (
         (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation)
         if is_torch_available()
         else ()
     )
     snake_case_						      =   (
         {
             "feature-extraction": BeitModel,
             "image-classification": BeitForImageClassification,
             "image-segmentation": BeitForSemanticSegmentation,
         }
         if is_torch_available()
         else {}
     )

     snake_case_						      =   False
     snake_case_						      =   False
     snake_case_						      =   False



     def      lowerCAmelCase			(   self							:			str						)->	List[Any]:
        snake_case 						=   BeitModelTester(self						)
        snake_case 						=   ConfigTester(self   ,       config_class=__snake_case   ,       has_text_modality=__snake_case   ,       hidden_size=37						)



     def      lowerCAmelCase			(   self							:			List[Any]						)->	List[Any]:
        self.config_tester.run_common_tests()



     @unittest.skip(reason="""BEiT does not use inputs_embeds"""						)
     def      lowerCAmelCase			(   self							:			Any						)->	Tuple:
        pass



     @require_torch_multi_gpu
     @unittest.skip(reason="""BEiT has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`"""						)
     def      lowerCAmelCase			(   self							:			str						)->	Optional[Any]:
        pass



     def      lowerCAmelCase			(   self							:			Tuple						)->	Optional[int]:
        snake_case						,    snake_case 						=   self.model_tester.prepare_config_and_inputs_for_common()

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



     def      lowerCAmelCase			(   self							:			Tuple						)->	List[Any]:
        snake_case						,    snake_case 						=   self.model_tester.prepare_config_and_inputs_for_common()

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

           snake_case 						=   ["""pixel_values"""]
           self.assertListEqual(arg_names[:1]   ,       __snake_case						)



     def      lowerCAmelCase			(   self							:			int						)->	int:
        snake_case 						=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__snake_case						)



     def      lowerCAmelCase			(   self							:			Dict						)->	int:
        snake_case 						=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*__snake_case						)



     def      lowerCAmelCase			(   self							:			List[Any]						)->	int:
        snake_case 						=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*__snake_case						)



     def      lowerCAmelCase			(   self							:			List[Any]						)->	List[str]:
        snake_case 						=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_semantic_segmentation(*__snake_case						)



     def      lowerCAmelCase			(   self							:			Optional[int]						)->	Optional[int]:
        if not self.model_tester.is_training:
           return

        snake_case						,    snake_case 						=   self.model_tester.prepare_config_and_inputs_for_common()
        snake_case 						=   True

        for model_class in self.all_model_classes:
           # we don't test BeitForMaskedImageModeling
           if model_class in [*get_values(__snake_case						), BeitForMaskedImageModeling]:
              continue

           snake_case 						=   model_class(__snake_case						)
           model.to(__snake_case						)
           model.train()
           snake_case 						=   self._prepare_for_class(__snake_case   ,       __snake_case   ,       return_labels=__snake_case						)
           snake_case 						=   model(**__snake_case						).loss
           loss.backward()



     def      lowerCAmelCase			(   self							:			List[str]						)->	Dict:
        snake_case						,    snake_case 						=   self.model_tester.prepare_config_and_inputs_for_common()
        if not self.model_tester.is_training:
           return

        snake_case 						=   False
        snake_case 						=   True

        for model_class in self.all_model_classes:
           # we don't test BeitForMaskedImageModeling
           if (
               model_class in [*get_values(__snake_case						), BeitForMaskedImageModeling]
               or not model_class.supports_gradient_checkpointing
           ):
              continue

           snake_case 						=   model_class(__snake_case						)
           model.gradient_checkpointing_enable()
           model.to(__snake_case						)
           model.train()
           snake_case 						=   self._prepare_for_class(__snake_case   ,       __snake_case   ,       return_labels=__snake_case						)
           snake_case 						=   model(**__snake_case						).loss
           loss.backward()



     def      lowerCAmelCase			(   self							:			Tuple						)->	Union[str, Any]:
        snake_case						,    snake_case 						=   self.model_tester.prepare_config_and_inputs_for_common()

        snake_case 						=   _config_zero_init(__snake_case						)
        for model_class in self.all_model_classes:
           snake_case 						=   model_class(config=__snake_case						)
           for name, param in model.named_parameters():
              # we skip lambda parameters as these require special initial values
              # determined by config.layer_scale_init_value
              if "lambda" in name:
                 continue
              if param.requires_grad:
                 self.assertIn(
                     ((param.data.mean() * 1e9).round() / 1e9).item()   ,       [0.0, 1.0]   ,       msg=f'''Parameter {name} of model {model_class} seems not properly initialized'''   ,       )



     @slow
     def      lowerCAmelCase			(   self							:			Optional[Any]						)->	str:
        for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           snake_case 						=   BeitModel.from_pretrained(__snake_case						)
           self.assertIsNotNone(__snake_case						)





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


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






     """simple docstring"""






     @cached_property
     def      lowerCAmelCase			(   self							:			List[Any]						)->	List[str]:
        return BeitImageProcessor.from_pretrained("""microsoft/beit-base-patch16-224"""						) if is_vision_available() else None



     @slow
     def      lowerCAmelCase			(   self							:			Optional[int]						)->	int:
        snake_case 						=   BeitForMaskedImageModeling.from_pretrained("""microsoft/beit-base-patch16-224-pt22k"""						).to(__snake_case						)

        snake_case 						=   self.default_image_processor
        snake_case 						=   prepare_img()
        snake_case 						=   image_processor(images=__snake_case   ,       return_tensors="""pt"""						).pixel_values.to(__snake_case						)

        # prepare bool_masked_pos
        snake_case 						=   torch.ones((1, 1_96)   ,       dtype=torch.bool						).to(__snake_case						)

        # forward pass
        with torch.no_grad():
           snake_case 						=   model(pixel_values=__snake_case   ,       bool_masked_pos=__snake_case						)
        snake_case 						=   outputs.logits

        # verify the logits
        snake_case 						=   torch.Size((1, 1_96, 81_92)						)
        self.assertEqual(logits.shape   ,       __snake_case						)

        snake_case 						=   torch.tensor(
            [[-3.24_37, 0.50_72, -13.91_74], [-3.24_56, 0.49_48, -13.94_01], [-3.20_33, 0.51_21, -13.85_50]]						).to(__snake_case						)

        self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3]   ,       __snake_case   ,       atol=1e-2						)						)



     @slow
     def      lowerCAmelCase			(   self							:			Tuple						)->	Optional[int]:
        snake_case 						=   BeitForImageClassification.from_pretrained("""microsoft/beit-base-patch16-224"""						).to(__snake_case						)

        snake_case 						=   self.default_image_processor
        snake_case 						=   prepare_img()
        snake_case 						=   image_processor(images=__snake_case   ,       return_tensors="""pt"""						).to(__snake_case						)

        # forward pass
        with torch.no_grad():
           snake_case 						=   model(**__snake_case						)
        snake_case 						=   outputs.logits

        # verify the logits
        snake_case 						=   torch.Size((1, 10_00)						)
        self.assertEqual(logits.shape   ,       __snake_case						)

        snake_case 						=   torch.tensor([-1.23_85, -1.09_87, -1.01_08]						).to(__snake_case						)

        self.assertTrue(torch.allclose(logits[0, :3]   ,       __snake_case   ,       atol=1e-4						)						)

        snake_case 						=   2_81
        self.assertEqual(logits.argmax(-1						).item()   ,       __snake_case						)



     @slow
     def      lowerCAmelCase			(   self							:			str						)->	Any:
        snake_case 						=   BeitForImageClassification.from_pretrained("""microsoft/beit-large-patch16-224-pt22k-ft22k"""						).to(
            __snake_case						)

        snake_case 						=   self.default_image_processor
        snake_case 						=   prepare_img()
        snake_case 						=   image_processor(images=__snake_case   ,       return_tensors="""pt"""						).to(__snake_case						)

        # forward pass
        with torch.no_grad():
           snake_case 						=   model(**__snake_case						)
        snake_case 						=   outputs.logits

        # verify the logits
        snake_case 						=   torch.Size((1, 2_18_41)						)
        self.assertEqual(logits.shape   ,       __snake_case						)

        snake_case 						=   torch.tensor([1.68_81, -0.27_87, 0.59_01]						).to(__snake_case						)

        self.assertTrue(torch.allclose(logits[0, :3]   ,       __snake_case   ,       atol=1e-4						)						)

        snake_case 						=   23_96
        self.assertEqual(logits.argmax(-1						).item()   ,       __snake_case						)



     @slow
     def      lowerCAmelCase			(   self							:			Union[str, Any]						)->	int:
        snake_case 						=   BeitForSemanticSegmentation.from_pretrained("""microsoft/beit-base-finetuned-ade-640-640"""						)
        snake_case 						=   model.to(__snake_case						)

        snake_case 						=   BeitImageProcessor(do_resize=__snake_case   ,       size=6_40   ,       do_center_crop=__snake_case						)

        snake_case 						=   load_dataset("""hf-internal-testing/fixtures_ade20k"""   ,       split="""test"""						)
        snake_case 						=   Image.open(ds[0]["""file"""]						)
        snake_case 						=   image_processor(images=__snake_case   ,       return_tensors="""pt"""						).to(__snake_case						)

        # forward pass
        with torch.no_grad():
           snake_case 						=   model(**__snake_case						)
        snake_case 						=   outputs.logits

        # verify the logits
        snake_case 						=   torch.Size((1, 1_50, 1_60, 1_60)						)
        self.assertEqual(logits.shape   ,       __snake_case						)

        snake_case 						=   version.parse(PIL.__version__						) < version.parse("""9.0.0"""						)

        if is_pillow_less_than_a:
           snake_case 						=   torch.tensor(
               [
                   [[-4.92_25, -2.39_54, -3.05_22], [-2.88_22, -1.00_46, -1.75_61], [-2.95_49, -1.32_28, -2.13_47]],
                   [[-5.81_68, -3.41_29, -4.07_78], [-3.86_51, -2.22_14, -3.02_77], [-3.83_56, -2.46_43, -3.35_35]],
                   [[-0.00_78, 3.99_52, 4.07_54], [2.98_56, 4.69_44, 5.00_35], [3.24_13, 4.78_13, 4.99_69]],
               ]   ,       device=__snake_case   ,       )
        else:
           snake_case 						=   torch.tensor(
               [
                   [[-4.89_60, -2.36_88, -3.03_55], [-2.84_78, -0.98_36, -1.74_18], [-2.94_49, -1.33_32, -2.14_56]],
                   [[-5.80_81, -3.41_24, -4.10_06], [-3.85_61, -2.20_81, -3.03_23], [-3.83_65, -2.46_01, -3.36_69]],
                   [[-0.03_09, 3.98_68, 4.05_40], [2.96_40, 4.68_77, 4.99_76], [3.20_81, 4.76_90, 4.99_42]],
               ]   ,       device=__snake_case   ,       )

        self.assertTrue(torch.allclose(logits[0, :3, :3, :3]   ,       __snake_case   ,       atol=1e-4						)						)



     @slow
     def      lowerCAmelCase			(   self							:			Tuple						)->	Any:
        snake_case 						=   BeitForSemanticSegmentation.from_pretrained("""microsoft/beit-base-finetuned-ade-640-640"""						)
        snake_case 						=   model.to(__snake_case						)

        snake_case 						=   BeitImageProcessor(do_resize=__snake_case   ,       size=6_40   ,       do_center_crop=__snake_case						)

        snake_case 						=   load_dataset("""hf-internal-testing/fixtures_ade20k"""   ,       split="""test"""						)
        snake_case 						=   Image.open(ds[0]["""file"""]						)
        snake_case 						=   image_processor(images=__snake_case   ,       return_tensors="""pt"""						).to(__snake_case						)

        # forward pass
        with torch.no_grad():
           snake_case 						=   model(**__snake_case						)

        snake_case 						=   outputs.logits.detach().cpu()

        snake_case 						=   image_processor.post_process_semantic_segmentation(outputs=__snake_case   ,       target_sizes=[(5_00, 3_00)]						)
        snake_case 						=   torch.Size((5_00, 3_00)						)
        self.assertEqual(segmentation[0].shape   ,       __snake_case						)

        snake_case 						=   image_processor.post_process_semantic_segmentation(outputs=__snake_case						)
        snake_case 						=   torch.Size((1_60, 1_60)						)
        self.assertEqual(segmentation[0].shape   ,       __snake_case						)







'''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__)



