import inspect
import unittest

from transformers import DataaVecVisionConfig
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,
        DataaVecVisionForImageClassification,
        DataaVecVisionForSemanticSegmentation,
        DataaVecVisionModel,
    )
    from transformers.models.dataavec.modeling_dataavec_vision import DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image

    from transformers import BeitImageProcessor






class     __UpperCAmelCase:






 """simple docstring"""




 def __init__(    self ,       __magic_name__ ,       __magic_name__=100 ,       __magic_name__=13 ,       __magic_name__=30 ,       __magic_name__=2 ,       __magic_name__=3 ,       __magic_name__=True ,       __magic_name__=True ,       __magic_name__=32 ,       __magic_name__=4 ,       __magic_name__=4 ,       __magic_name__=37 ,       __magic_name__="gelu" ,       __magic_name__=0.1 ,       __magic_name__=0.1 ,       __magic_name__=10 ,       __magic_name__=0.02 ,       __magic_name__=3 ,       __magic_name__=None ,       __magic_name__=[0, 1, 2, 3] ,       ):

        """simple docstring"""

        A_							:       Tuple	      =				parent
        A_							:       Union[str, Any]	      =				100
        A_							:       int	      =				batch_size
        A_							:       str	      =				image_size
        A_							:       List[Any]	      =				patch_size
        A_							:       Optional[int]	      =				num_channels
        A_							:       List[Any]	      =				is_training
        A_							:       Union[str, Any]	      =				use_labels
        A_							:       str	      =				hidden_size
        A_							:       Dict	      =				num_hidden_layers
        A_							:       List[str]	      =				num_attention_heads
        A_							:       Union[str, Any]	      =				intermediate_size
        A_							:       Tuple	      =				hidden_act
        A_							:       Tuple	      =				hidden_dropout_prob
        A_							:       Union[str, Any]	      =				attention_probs_dropout_prob
        A_							:       Optional[Any]	      =				type_sequence_label_size
        A_							:       Optional[int]	      =				initializer_range
        A_							:       Optional[int]	      =				scope
        A_							:       Union[str, Any]	      =				out_indices
        A_							:       List[Any]	      =				num_labels

        # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
        A_							:       List[str]	      =				(image_size // patch_size) ** 2
        A_							:       Optional[Any]	      =				num_patches + 1
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       Union[str, Any]	      =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

        A_							:       Optional[int]	      =				None
        A_							:       Tuple	      =				None
        if self.use_labels:
               A_							:       Union[str, Any]	      =				ids_tensor([self.batch_size] ,       self.type_sequence_label_size      )
               A_							:       int	      =				ids_tensor([self.batch_size, self.image_size, self.image_size] ,       self.num_labels      )

        A_							:       int	      =				self.get_config()

        return config, pixel_values, labels, pixel_labels
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        return DataaVecVisionConfig(
            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=__magic_name__ ,       initializer_range=self.initializer_range ,       out_indices=self.out_indices ,       )
 def  UpperCAmelCase   (    self ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__      ):

        """simple docstring"""

        A_							:       Union[str, Any]	      =				DataaVecVisionModel(config=__magic_name__      )
        model.to(__magic_name__      )
        model.eval()
        A_							:       Optional[int]	      =				model(__magic_name__      )
        # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
        A_							:       Union[str, Any]	      =				(self.image_size // self.patch_size) ** 2
        self.parent.assertEqual(result.last_hidden_state.shape ,       (self.batch_size, num_patches + 1, self.hidden_size)      )
 def  UpperCAmelCase   (    self ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__      ):

        """simple docstring"""

        A_							:       Dict	      =				self.type_sequence_label_size
        A_							:       int	      =				DataaVecVisionForImageClassification(__magic_name__      )
        model.to(__magic_name__      )
        model.eval()
        A_							:       int	      =				model(__magic_name__ ,       labels=__magic_name__      )
        self.parent.assertEqual(result.logits.shape ,       (self.batch_size, self.type_sequence_label_size)      )
 def  UpperCAmelCase   (    self ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__      ):

        """simple docstring"""

        A_							:       List[str]	      =				self.num_labels
        A_							:       Any	      =				DataaVecVisionForSemanticSegmentation(__magic_name__      )
        model.to(__magic_name__      )
        model.eval()
        A_							:       Any	      =				model(__magic_name__      )
        self.parent.assertEqual(
            result.logits.shape ,       (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)      )
        A_							:       List[Any]	      =				model(__magic_name__ ,       labels=__magic_name__      )
        self.parent.assertEqual(
            result.logits.shape ,       (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

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






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






 """simple docstring"""




 __magic_name__          =   (
     (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
     if is_torch_available()
     else ()
 )
 __magic_name__          =   (
     {
         """feature-extraction""": DataaVecVisionModel,
         """image-classification""": DataaVecVisionForImageClassification,
         """image-segmentation""": DataaVecVisionForSemanticSegmentation,
     }
     if is_torch_available()
     else {}
 )

 __magic_name__          =   False
 __magic_name__          =   False
 __magic_name__          =   False
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       Any	      =				DataaVecVisionModelTester(self      )
        A_							:       Any	      =				ConfigTester(
            self ,       config_class=__magic_name__ ,       has_text_modality=__magic_name__ ,       hidden_size=37      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

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

        """simple docstring"""

        pass
 @require_torch_multi_gpu
 @unittest.skip(
     reason='''Data2VecVision has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'''      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        pass
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_      ,							A_							:       int	      =				self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
               A_							:       Union[str, Any]	      =				model_class(__magic_name__      )
               self.assertIsInstance(model.get_input_embeddings() ,       (nn.Module)      )
               A_							:       List[Any]	      =				model.get_output_embeddings()
               self.assertTrue(x is None or isinstance(__magic_name__ ,       nn.Linear      )      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_      ,							A_							:       Any	      =				self.model_tester.prepare_config_and_inputs_for_common()

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

               A_							:       Optional[int]	      =				['''pixel_values''']
               self.assertListEqual(arg_names[:1] ,       __magic_name__      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       Dict	      =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__magic_name__      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       Union[str, Any]	      =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_segmentation(*__magic_name__      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        if not self.model_tester.is_training:
               return

        A_      ,							A_							:       int	      =				self.model_tester.prepare_config_and_inputs_for_common()
        A_							:       Union[str, Any]	      =				True

        for model_class in self.all_model_classes:
               if model_class in [*get_values(__magic_name__      )]:
                      continue

               A_							:       List[str]	      =				model_class(__magic_name__      )
               model.to(__magic_name__      )
               model.train()
               A_							:       str	      =				self._prepare_for_class(__magic_name__ ,       __magic_name__ ,       return_labels=__magic_name__      )
               A_							:       Any	      =				model(**__magic_name__      ).loss
               loss.backward()
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_      ,							A_							:       Dict	      =				self.model_tester.prepare_config_and_inputs_for_common()
        if not self.model_tester.is_training:
               return

        A_							:       Any	      =				False
        A_							:       Optional[Any]	      =				True

        for model_class in self.all_model_classes:
               if model_class in [*get_values(__magic_name__      )] or not model_class.supports_gradient_checkpointing:
                      continue
               # TODO: remove the following 3 lines once we have a MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING
               # this can then be incorporated into _prepare_for_class in test_modeling_common.py
               elif model_class.__name__ == "Data2VecVisionForSemanticSegmentation":
                      A_      ,							A_      ,							A_      ,							A_							:       List[str]	      =				inputs_dict['''pixel_values'''].shape
                      A_							:       List[Any]	      =				torch.zeros(
                          [self.model_tester.batch_size, height, width] ,       device=__magic_name__      ).long()
               A_							:       Tuple	      =				model_class(__magic_name__      )
               model.gradient_checkpointing_enable()
               model.to(__magic_name__      )
               model.train()
               A_							:       int	      =				self._prepare_for_class(__magic_name__ ,       __magic_name__ ,       return_labels=__magic_name__      )
               A_							:       Dict	      =				model(**__magic_name__      ).loss
               loss.backward()
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_      ,							A_							:       List[str]	      =				self.model_tester.prepare_config_and_inputs_for_common()

        A_							:       List[str]	      =				_config_zero_init(__magic_name__      )
        for model_class in self.all_model_classes:
               A_							:       Optional[Any]	      =				model_class(config=__magic_name__      )
               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""" ,       )
 def  UpperCAmelCase   (    self ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__=2e-4 ,       __magic_name__="outputs" ,       __magic_name__=None      ):

        """simple docstring"""

        super().check_pt_tf_outputs(__magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       Union[str, Any]	      =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*__magic_name__      )
 @slow
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               A_							:       Union[str, Any]	      =				DataaVecVisionModel.from_pretrained(__magic_name__      )
               self.assertIsNotNone(__magic_name__      )


def       a__      (					)		->	Optional[Any]:
       A_							:       Union[str, Any]	      =				Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
       return image






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






 """simple docstring"""




 @cached_property
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        return (
            BeitImageProcessor.from_pretrained('''facebook/data2vec-vision-base-ft1k'''      ) if is_vision_available() else None
        )
 @slow
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       List[Any]	      =				DataaVecVisionForImageClassification.from_pretrained('''facebook/data2vec-vision-base-ft1k'''      ).to(
            __magic_name__      )

        A_							:       Union[str, Any]	      =				self.default_image_processor
        A_							:       Optional[int]	      =				prepare_img()
        A_							:       Any	      =				image_processor(images=__magic_name__ ,       return_tensors='''pt'''      ).to(__magic_name__      )

        # forward pass
        with torch.no_grad():
               A_							:       List[Any]	      =				model(**__magic_name__      )
        A_							:       Tuple	      =				outputs.logits

        # verify the logits
        A_							:       Tuple	      =				torch.Size((1, 1000)      )
        self.assertEqual(logits.shape ,       __magic_name__      )

        A_							:       Any	      =				torch.tensor([0.32_77, -0.13_95, 0.09_11]      ).to(__magic_name__      )

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

        A_							:       Dict	      =				[model.config.labelaid[i] for i in ['''remote control, remote''', '''tabby, tabby cat''']]
        self.assertEqual(logits[0].topk(2      ).indices.cpu().tolist() ,       __magic_name__      )



from transformers import ProcessorMixin






class     __UpperCAmelCase(    A__				):






 """simple docstring"""




 __magic_name__          =   """AutoFeatureExtractor"""
 __magic_name__          =   """AutoTokenizer"""


