




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 __snake_case     :







   def __init__( self						:		Optional[int]   ,		_snake_case						:		Any   ,		_snake_case						:		int=100   ,		_snake_case						:		Union[str, Any]=13   ,		_snake_case						:		Optional[int]=30   ,		_snake_case						:		str=2   ,		_snake_case						:		Optional[Any]=3   ,		_snake_case						:		Optional[Any]=True   ,		_snake_case						:		Dict=True   ,		_snake_case						:		str=32   ,		_snake_case						:		List[Any]=4   ,		_snake_case						:		Union[str, Any]=4   ,		_snake_case						:		str=37   ,		_snake_case						:		Any="gelu"   ,		_snake_case						:		Optional[int]=0.1   ,		_snake_case						:		str=0.1   ,		_snake_case						:		List[str]=10   ,		_snake_case						:		str=0.0_2   ,		_snake_case						:		Dict=3   ,		_snake_case						:		List[Any]=None   ,		_snake_case						:		str=[0, 1, 2, 3]   ,		):

        """simple docstring"""


        UpperCAmelCase_		=				parent
        UpperCAmelCase_		=				100
        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_		=				scope
        UpperCAmelCase_		=				out_indices
        UpperCAmelCase_		=				num_labels

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







   def lowerCamelCase		( self						:		Tuple):

        """simple docstring"""


        UpperCAmelCase_		=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

        UpperCAmelCase_		=				self.get_config()

        return config, pixel_values, labels, pixel_labels







   def lowerCamelCase		( self						:		List[Any]):

        """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=_snake_case   ,		initializer_range=self.initializer_range   ,		out_indices=self.out_indices   ,		)







   def lowerCamelCase		( self						:		List[str]   ,		_snake_case						:		List[Any]   ,		_snake_case						:		Dict   ,		_snake_case						:		int   ,		_snake_case						:		Optional[Any]):

        """simple docstring"""


        UpperCAmelCase_		=				DataaVecVisionModel(config=_snake_case)
        model.to(_snake_case)
        model.eval()
        UpperCAmelCase_		=				model(_snake_case)
        # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
        UpperCAmelCase_		=				(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 lowerCamelCase		( self						:		List[Any]   ,		_snake_case						:		Tuple   ,		_snake_case						:		List[Any]   ,		_snake_case						:		str   ,		_snake_case						:		Dict):

        """simple docstring"""


        UpperCAmelCase_		=				self.type_sequence_label_size
        UpperCAmelCase_		=				DataaVecVisionForImageClassification(_snake_case)
        model.to(_snake_case)
        model.eval()
        UpperCAmelCase_		=				model(_snake_case   ,		labels=_snake_case)
        self.parent.assertEqual(result.logits.shape   ,		(self.batch_size, self.type_sequence_label_size))







   def lowerCamelCase		( self						:		Optional[int]   ,		_snake_case						:		Optional[Any]   ,		_snake_case						:		Union[str, Any]   ,		_snake_case						:		str   ,		_snake_case						:		Any):

        """simple docstring"""


        UpperCAmelCase_		=				self.num_labels
        UpperCAmelCase_		=				DataaVecVisionForSemanticSegmentation(_snake_case)
        model.to(_snake_case)
        model.eval()
        UpperCAmelCase_		=				model(_snake_case)
        self.parent.assertEqual(
            result.logits.shape   ,		(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2))
        UpperCAmelCase_		=				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						:		List[str]):

        """simple docstring"""


        UpperCAmelCase_		=				self.prepare_config_and_inputs()
        UpperCAmelCase_	,			UpperCAmelCase_	,			UpperCAmelCase_	,			UpperCAmelCase_		=				config_and_inputs
        UpperCAmelCase_		=				{'''pixel_values''': pixel_values}
        return config, inputs_dict







@require_torch
class __snake_case     (     a					,    a					,    unittest.TestCase						):
   UpperCAmelCase__    :       Union[str, Any]		=    (
       (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
       if is_torch_available()
       else ()
   )
   UpperCAmelCase__    :       Dict		=    (
       {
           '''feature-extraction''': DataaVecVisionModel,
           '''image-classification''': DataaVecVisionForImageClassification,
           '''image-segmentation''': DataaVecVisionForSemanticSegmentation,
       }
       if is_torch_available()
       else {}
   )

   UpperCAmelCase__    :       List[Any]		=    False
   UpperCAmelCase__    :       int		=    False
   UpperCAmelCase__    :       int		=    False







   def lowerCamelCase		( self						:		List[Any]):

        """simple docstring"""


        UpperCAmelCase_		=				DataaVecVisionModelTester(self)
        UpperCAmelCase_		=				ConfigTester(
            self   ,		config_class=_snake_case   ,		has_text_modality=_snake_case   ,		hidden_size=37)







   def lowerCamelCase		( self						:		Any):

        """simple docstring"""


        self.config_tester.run_common_tests()







   def lowerCamelCase		( self						:		Tuple):

        """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 lowerCamelCase		( self						:		Optional[Any]):

        """simple docstring"""


        pass







   def lowerCamelCase		( self						:		Any):

        """simple docstring"""


        UpperCAmelCase_	,			UpperCAmelCase_		=				self.model_tester.prepare_config_and_inputs_for_common()

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







   def lowerCamelCase		( self						:		Optional[Any]):

        """simple docstring"""


        UpperCAmelCase_	,			UpperCAmelCase_		=				self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             UpperCAmelCase_		=				model_class(_snake_case)
             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]   ,		_snake_case)







   def lowerCamelCase		( self						:		Tuple):

        """simple docstring"""


        UpperCAmelCase_		=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_snake_case)







   def lowerCamelCase		( self						:		int):

        """simple docstring"""


        UpperCAmelCase_		=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_segmentation(*_snake_case)







   def lowerCamelCase		( self						:		Tuple):

        """simple docstring"""


        if not self.model_tester.is_training:
             return

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

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

             UpperCAmelCase_		=				model_class(_snake_case)
             model.to(_snake_case)
             model.train()
             UpperCAmelCase_		=				self._prepare_for_class(_snake_case   ,		_snake_case   ,		return_labels=_snake_case)
             UpperCAmelCase_		=				model(**_snake_case).loss
             loss.backward()







   def lowerCamelCase		( self						:		Optional[int]):

        """simple docstring"""


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

        UpperCAmelCase_		=				False
        UpperCAmelCase_		=				True

        for model_class in self.all_model_classes:
             if model_class in [*get_values(_snake_case)] 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":
                  UpperCAmelCase_	,			UpperCAmelCase_	,			UpperCAmelCase_	,			UpperCAmelCase_		=				inputs_dict['''pixel_values'''].shape
                  UpperCAmelCase_		=				torch.zeros(
                      [self.model_tester.batch_size, height, width]   ,		device=_snake_case).long()
             UpperCAmelCase_		=				model_class(_snake_case)
             model.gradient_checkpointing_enable()
             model.to(_snake_case)
             model.train()
             UpperCAmelCase_		=				self._prepare_for_class(_snake_case   ,		_snake_case   ,		return_labels=_snake_case)
             UpperCAmelCase_		=				model(**_snake_case).loss
             loss.backward()







   def lowerCamelCase		( self						:		Optional[int]):

        """simple docstring"""


        UpperCAmelCase_	,			UpperCAmelCase_		=				self.model_tester.prepare_config_and_inputs_for_common()

        UpperCAmelCase_		=				_config_zero_init(_snake_case)
        for model_class in self.all_model_classes:
             UpperCAmelCase_		=				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"""   ,		)







   def lowerCamelCase		( self						:		Optional[int]   ,		_snake_case						:		Union[str, Any]   ,		_snake_case						:		Union[str, Any]   ,		_snake_case						:		Optional[Any]   ,		_snake_case						:		List[str]=2e-4   ,		_snake_case						:		Optional[int]="outputs"   ,		_snake_case						:		Union[str, Any]=None):

        """simple docstring"""


        super().check_pt_tf_outputs(_snake_case   ,		_snake_case   ,		_snake_case   ,		_snake_case   ,		_snake_case   ,		_snake_case)







   def lowerCamelCase		( self						:		Optional[int]):

        """simple docstring"""


        UpperCAmelCase_		=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*_snake_case)







   @slow
   def lowerCamelCase		( self						:		List[Any]):

        """simple docstring"""


        for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             UpperCAmelCase_		=				DataaVecVisionModel.from_pretrained(_snake_case)
             self.assertIsNotNone(_snake_case)







def   A		()  -> Dict:







     """simple docstring"""


     UpperCAmelCase_		=				Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)
     return image







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







   @cached_property
   def lowerCamelCase		( self						:		List[Any]):

        """simple docstring"""


        return (
            BeitImageProcessor.from_pretrained('''facebook/data2vec-vision-base-ft1k''') if is_vision_available() else None
        )







   @slow
   def lowerCamelCase		( self						:		Tuple):

        """simple docstring"""


        UpperCAmelCase_		=				DataaVecVisionForImageClassification.from_pretrained('''facebook/data2vec-vision-base-ft1k''').to(
            _snake_case)

        UpperCAmelCase_		=				self.default_image_processor
        UpperCAmelCase_		=				prepare_img()
        UpperCAmelCase_		=				image_processor(images=_snake_case   ,		return_tensors='''pt''').to(_snake_case)

        # forward pass
        with torch.no_grad():
             UpperCAmelCase_		=				model(**_snake_case)
        UpperCAmelCase_		=				outputs.logits

        # verify the logits
        UpperCAmelCase_		=				torch.Size((1, 1000))
        self.assertEqual(logits.shape   ,		_snake_case)

        UpperCAmelCase_		=				torch.tensor([0.3_2_7_7, -0.1_3_9_5, 0.0_9_1_1]).to(_snake_case)

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

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












def   A		(__A  :       int			)  -> int:







     """simple docstring"""


     if not isinstance(__A  ,							__A			):
          UpperCAmelCase_		=				F"""Input value of [number={number}] must be an integer"""
          raise TypeError(__A			)
     if number < 1:
          raise ValueError('''Input must be a positive integer'''			)
     return number * (2 * number - 1)


if __name__ == "__main__":
    import doctest

    doctest.testmod()






