





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





     """simple docstring"""



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





        '''simple docstring'''




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

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



     def 						SCREAMING_SNAKE_CASE		(							self       :Optional[int]  ):





        '''simple docstring'''




        A_					:				Tuple          =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

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

        A_					:				Union[str, Any]          =			self.get_config()

        return config, pixel_values, labels, pixel_labels



     def 						SCREAMING_SNAKE_CASE		(							self       :Optional[int]  ):





        '''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 						SCREAMING_SNAKE_CASE		(							self       :Union[str, Any]			,      snake_case       :str			,      snake_case       :Dict			,      snake_case       :Dict			,      snake_case       :str  ):





        '''simple docstring'''




        A_					:				Optional[int]          =			DataaVecVisionModel(config=snake_case  )
        model.to(snake_case  )
        model.eval()
        A_					:				Optional[Any]          =			model(snake_case  )
        # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
        A_					:				Optional[int]          =			(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 						SCREAMING_SNAKE_CASE		(							self       :str			,      snake_case       :str			,      snake_case       :Optional[int]			,      snake_case       :Tuple			,      snake_case       :Any  ):





        '''simple docstring'''




        A_					:				Any          =			self.type_sequence_label_size
        A_					:				List[Any]          =			DataaVecVisionForImageClassification(snake_case  )
        model.to(snake_case  )
        model.eval()
        A_					:				Union[str, Any]          =			model(snake_case			,      labels=snake_case  )
        self.parent.assertEqual(result.logits.shape			,      (self.batch_size, self.type_sequence_label_size)  )



     def 						SCREAMING_SNAKE_CASE		(							self       :Optional[int]			,      snake_case       :Any			,      snake_case       :List[str]			,      snake_case       :Union[str, Any]			,      snake_case       :List[Any]  ):





        '''simple docstring'''




        A_					:				Optional[Any]          =			self.num_labels
        A_					:				Dict          =			DataaVecVisionForSemanticSegmentation(snake_case  )
        model.to(snake_case  )
        model.eval()
        A_					:				Union[str, Any]          =			model(snake_case  )
        self.parent.assertEqual(
            result.logits.shape			,      (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)  )
        A_					:				Union[str, Any]          =			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 						SCREAMING_SNAKE_CASE		(							self       :Union[str, Any]  ):





        '''simple docstring'''




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




@require_torch
class 					__magic_name__  (							lowerCamelCase__    ,				lowerCamelCase__    ,				unittest.TestCase							):





     """simple docstring"""



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

     __UpperCamelCase					    =     False
     __UpperCamelCase					    =     False
     __UpperCamelCase					    =     False



     def 						SCREAMING_SNAKE_CASE		(							self       :List[Any]  ):





        '''simple docstring'''




        A_					:				int          =			DataaVecVisionModelTester(self  )
        A_					:				Union[str, Any]          =			ConfigTester(
            self			,      config_class=snake_case			,      has_text_modality=snake_case			,      hidden_size=37  )



     def 						SCREAMING_SNAKE_CASE		(							self       :Dict  ):





        '''simple docstring'''




        self.config_tester.run_common_tests()



     def 						SCREAMING_SNAKE_CASE		(							self       :Any  ):





        '''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 						SCREAMING_SNAKE_CASE		(							self       :List[Any]  ):





        '''simple docstring'''




        pass



     def 						SCREAMING_SNAKE_CASE		(							self       :Union[str, Any]  ):





        '''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(snake_case  )
           self.assertIsInstance(model.get_input_embeddings()			,      (nn.Module)  )
           A_					:				str          =			model.get_output_embeddings()
           self.assertTrue(x is None or isinstance(snake_case			,      nn.Linear  )  )



     def 						SCREAMING_SNAKE_CASE		(							self       :Tuple  ):





        '''simple docstring'''




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

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

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



     def 						SCREAMING_SNAKE_CASE		(							self       :Optional[Any]  ):





        '''simple docstring'''




        A_					:				str          =			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*snake_case  )



     def 						SCREAMING_SNAKE_CASE		(							self       :Dict  ):





        '''simple docstring'''




        A_					:				Tuple          =			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_segmentation(*snake_case  )



     def 						SCREAMING_SNAKE_CASE		(							self       :Union[str, Any]  ):





        '''simple docstring'''




        if not self.model_tester.is_training:
           return

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

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

           A_					:				List[str]          =			model_class(snake_case  )
           model.to(snake_case  )
           model.train()
           A_					:				Optional[Any]          =			self._prepare_for_class(snake_case			,      snake_case			,      return_labels=snake_case  )
           A_					:				int          =			model(**snake_case  ).loss
           loss.backward()



     def 						SCREAMING_SNAKE_CASE		(							self       :Union[str, Any]  ):





        '''simple docstring'''




        A_      ,						A_					:				List[Any]          =			self.model_tester.prepare_config_and_inputs_for_common()
        if not self.model_tester.is_training:
           return

        A_					:				int          =			False
        A_					:				Any          =			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":
              A_      ,						A_      ,						A_      ,						A_					:				Any          =			inputs_dict["pixel_values"].shape
              A_					:				Any          =			torch.zeros(
                  [self.model_tester.batch_size, height, width]			,      device=snake_case  ).long()
           A_					:				Tuple          =			model_class(snake_case  )
           model.gradient_checkpointing_enable()
           model.to(snake_case  )
           model.train()
           A_					:				List[str]          =			self._prepare_for_class(snake_case			,      snake_case			,      return_labels=snake_case  )
           A_					:				Tuple          =			model(**snake_case  ).loss
           loss.backward()



     def 						SCREAMING_SNAKE_CASE		(							self       :Any  ):





        '''simple docstring'''




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

        A_					:				str          =			_config_zero_init(snake_case  )
        for model_class in self.all_model_classes:
           A_					:				Optional[int]          =			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 						SCREAMING_SNAKE_CASE		(							self       :Tuple			,      snake_case       :int			,      snake_case       :str			,      snake_case       :Optional[int]			,      snake_case       :Any=2e-4			,      snake_case       :Optional[int]="outputs"			,      snake_case       :str=None  ):





        '''simple docstring'''




        super().check_pt_tf_outputs(snake_case			,      snake_case			,      snake_case			,      snake_case			,      snake_case			,      snake_case  )



     def 						SCREAMING_SNAKE_CASE		(							self       :List[str]  ):





        '''simple docstring'''




        A_					:				int          =			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*snake_case  )





     @slow
     def 						SCREAMING_SNAKE_CASE		(							self       :Optional[Any]  ):





        '''simple docstring'''




        for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           A_					:				List[str]          =			DataaVecVisionModel.from_pretrained(snake_case  )
           self.assertIsNotNone(snake_case  )




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





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





     """simple docstring"""



     @cached_property
     def 						SCREAMING_SNAKE_CASE		(							self       :int  ):





        '''simple docstring'''




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





     @slow
     def 						SCREAMING_SNAKE_CASE		(							self       :Dict  ):





        '''simple docstring'''




        A_					:				str          =			DataaVecVisionForImageClassification.from_pretrained("facebook/data2vec-vision-base-ft1k"  ).to(
            snake_case  )

        A_					:				List[Any]          =			self.default_image_processor
        A_					:				Any          =			prepare_img()
        A_					:				int          =			image_processor(images=snake_case			,      return_tensors="pt"  ).to(snake_case  )

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

        # verify the logits
        A_					:				Optional[int]          =			torch.Size((1, 1_000)  )
        self.assertEqual(logits.shape			,      snake_case  )

        A_					:				Optional[Any]          =			torch.tensor([0.3277, -0.1395, 0.0911]  ).to(snake_case  )

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

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







from __future__ import annotations

def      __snake_case						(					_lowerCAmelCase : int	) ->						list[int]:
   assert (
       isinstance(_lowerCAmelCase				,				_lowerCAmelCase	) and mask > 0
   ), f"mask needs to be positive integer, your input {mask}"
   A_					:				Optional[Any]          =			[]
   A_					:				Optional[int]          =			mask

   while submask:
      all_submasks.append(_lowerCAmelCase	)
      A_					:				List[str]          =			(submask - 1) & mask

   return all_submasks


if __name__ == "__main__":
 import doctest

 doctest.testmod()
