

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



      """simple docstring"""







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

           # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
           SCREAMING_SNAKE_CASE 		=(image_size // patch_size) ** 2
           SCREAMING_SNAKE_CASE 		=num_patches + 1
      def 			_lowerCAmelCase	( self	:				str							):
           SCREAMING_SNAKE_CASE 		=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

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

           SCREAMING_SNAKE_CASE 		=self.get_config()

           return config, pixel_values, labels, pixel_labels
      def 			_lowerCAmelCase	( self	:				Optional[int]							):
           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=lowercase_     ,initializer_range=self.initializer_range     ,out_indices=self.out_indices     ,)
      def 			_lowerCAmelCase	( self	:				Union[str, Any]     ,snake_case	:				Any     ,snake_case	:				Dict     ,snake_case	:				Any     ,snake_case	:				Union[str, Any]							):
           SCREAMING_SNAKE_CASE 		=DataaVecVisionModel(config=lowercase_							)
           model.to(lowercase_							)
           model.eval()
           SCREAMING_SNAKE_CASE 		=model(lowercase_							)
           # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
           SCREAMING_SNAKE_CASE 		=(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	:				str     ,snake_case	:				Optional[Any]     ,snake_case	:				Dict     ,snake_case	:				int     ,snake_case	:				Dict							):
           SCREAMING_SNAKE_CASE 		=self.type_sequence_label_size
           SCREAMING_SNAKE_CASE 		=DataaVecVisionForImageClassification(lowercase_							)
           model.to(lowercase_							)
           model.eval()
           SCREAMING_SNAKE_CASE 		=model(lowercase_     ,labels=lowercase_							)
           self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.type_sequence_label_size)							)
      def 			_lowerCAmelCase	( self	:				int     ,snake_case	:				List[Any]     ,snake_case	:				str     ,snake_case	:				Optional[int]     ,snake_case	:				Optional[Any]							):
           SCREAMING_SNAKE_CASE 		=self.num_labels
           SCREAMING_SNAKE_CASE 		=DataaVecVisionForSemanticSegmentation(lowercase_							)
           model.to(lowercase_							)
           model.eval()
           SCREAMING_SNAKE_CASE 		=model(lowercase_							)
           self.parent.assertEqual(
               result.logits.shape     ,(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)							)
           SCREAMING_SNAKE_CASE 		=model(lowercase_     ,labels=lowercase_							)
           self.parent.assertEqual(
               result.logits.shape     ,(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)							)


      def 			_lowerCAmelCase	( self	:				Union[str, Any]							):
           SCREAMING_SNAKE_CASE 		=self.prepare_config_and_inputs()
           SCREAMING_SNAKE_CASE 		=config_and_inputs
           SCREAMING_SNAKE_CASE 		={"pixel_values": pixel_values}
           return config, inputs_dict







@require_torch
class 						a_			(				__A					,     __A					,     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 			_lowerCAmelCase	( self	:				Optional[Any]							):
           SCREAMING_SNAKE_CASE 		=DataaVecVisionModelTester(self							)
           SCREAMING_SNAKE_CASE 		=ConfigTester(
               self     ,config_class=lowercase_     ,has_text_modality=lowercase_     ,hidden_size=37							)
      def 			_lowerCAmelCase	( self	:				List[Any]							):
           self.config_tester.run_common_tests()
      def 			_lowerCAmelCase	( self	:				Any							):
           # Data2VecVision does not use inputs_embeds
           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	:				List[str]							):
           pass
      def 			_lowerCAmelCase	( self	:				Tuple							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs_for_common()

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

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

                SCREAMING_SNAKE_CASE 		=["pixel_values"]
                self.assertListEqual(arg_names[:1]     ,lowercase_							)
      def 			_lowerCAmelCase	( self	:				str							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*lowercase_							)
      def 			_lowerCAmelCase	( self	:				Optional[Any]							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_segmentation(*lowercase_							)
      def 			_lowerCAmelCase	( self	:				Optional[Any]							):
           if not self.model_tester.is_training:
                return

           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs_for_common()
           SCREAMING_SNAKE_CASE 		=True

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

                SCREAMING_SNAKE_CASE 		=model_class(lowercase_							)
                model.to(lowercase_							)
                model.train()
                SCREAMING_SNAKE_CASE 		=self._prepare_for_class(lowercase_     ,lowercase_     ,return_labels=lowercase_							)
                SCREAMING_SNAKE_CASE 		=model(**lowercase_							).loss
                loss.backward()
      def 			_lowerCAmelCase	( self	:				List[Any]							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs_for_common()
           if not self.model_tester.is_training:
                return

           SCREAMING_SNAKE_CASE 		=False
           SCREAMING_SNAKE_CASE 		=True

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

           SCREAMING_SNAKE_CASE 		=_config_zero_init(lowercase_							)
           for model_class in self.all_model_classes:
                SCREAMING_SNAKE_CASE 		=model_class(config=lowercase_							)
                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	:				Dict     ,snake_case	:				List[Any]     ,snake_case	:				Optional[int]     ,snake_case	:				List[Any]     ,snake_case	:				int=2e-4     ,snake_case	:				List[str]="outputs"     ,snake_case	:				List[Any]=None							):
           # We override with a slightly higher tol value, as semseg models tend to diverge a bit more
           super().check_pt_tf_outputs(lowercase_     ,lowercase_     ,lowercase_     ,lowercase_     ,lowercase_     ,lowercase_							)
      def 			_lowerCAmelCase	( self	:				str							):
           SCREAMING_SNAKE_CASE 		=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*lowercase_							)


      @slow
      def 			_lowerCAmelCase	( self	:				Any							):
           for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                SCREAMING_SNAKE_CASE 		=DataaVecVisionModel.from_pretrained(lowercase_							)
                self.assertIsNotNone(lowercase_							)







def      snake_case__							(  ):





     """simple docstring"""







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



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



      """simple docstring"""







      @cached_property
      def 			_lowerCAmelCase	( self	:				Optional[Any]							):
           return (
               BeitImageProcessor.from_pretrained('facebook/data2vec-vision-base-ft1k'							) if is_vision_available() else None
           )


      @slow
      def 			_lowerCAmelCase	( self	:				Union[str, Any]							):
           SCREAMING_SNAKE_CASE 		=DataaVecVisionForImageClassification.from_pretrained('facebook/data2vec-vision-base-ft1k'							).to(
               lowercase_							)

           SCREAMING_SNAKE_CASE 		=self.default_image_processor
           SCREAMING_SNAKE_CASE 		=prepare_img()
           SCREAMING_SNAKE_CASE 		=image_processor(images=lowercase_     ,return_tensors='pt'							).to(lowercase_							)

           # forward pass
           with torch.no_grad():
                SCREAMING_SNAKE_CASE 		=model(**lowercase_							)
           SCREAMING_SNAKE_CASE 		=outputs.logits

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

           SCREAMING_SNAKE_CASE 		=torch.tensor([0.3_277, -0.1_395, 0.0_911]							).to(lowercase_							)

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

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


import unittest
from pathlib import Path

from transformers import is_vision_available, load_tool
from transformers.testing_utils import get_tests_dir

from .test_tools_common import ToolTesterMixin


if is_vision_available():
      from PIL import Image



class     snake_case_			(						unittest.TestCase    ,__A			):






   def  __UpperCamelCase	( self							:	int    )   ->		Union[str, Any]:
       lowercase__					:  Any          =							load_tool("image-segmentation"    )
       self.tool.setup()
       lowercase__					:  Optional[int]          =							load_tool("image-segmentation"      ,		remote=lowercase_    )






   def  __UpperCamelCase	( self							:	Any    )   ->		Tuple:
       lowercase__					:  Any          =							Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"    )    ) / "000000039769.png"    )
       lowercase__					:  Tuple          =							self.tool(lowercase_      ,		"cat"    )
       self.assertTrue(isinstance(lowercase_      ,		Image.Image    )    )






   def  __UpperCamelCase	( self							:	Optional[int]    )   ->		Dict:
       lowercase__					:  List[Any]          =							Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"    )    ) / "000000039769.png"    )
       lowercase__					:  Optional[int]          =							self.remote_tool(lowercase_      ,		"cat"    )
       self.assertTrue(isinstance(lowercase_      ,		Image.Image    )    )






   def  __UpperCamelCase	( self							:	List[Any]    )   ->		Dict:
       lowercase__					:  Optional[Any]          =							Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"    )    ) / "000000039769.png"    )
       lowercase__					:  int          =							self.tool(image=lowercase_      ,		label="cat"    )
       self.assertTrue(isinstance(lowercase_      ,		Image.Image    )    )







   def  __UpperCamelCase	( self							:	Dict    )   ->		List[Any]:
       lowercase__					:  Tuple          =							Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"    )    ) / "000000039769.png"    )
       lowercase__					:  Union[str, Any]          =							self.remote_tool(image=lowercase_      ,		label="cat"    )
       self.assertTrue(isinstance(lowercase_      ,		Image.Image    )    )



