






'''simple docstring'''






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







   def __init__(self       , __a       , __a=1_0_0       , __a=1_3       , __a=3_0       , __a=2       , __a=3       , __a=True       , __a=True       , __a=3_2       , __a=4       , __a=4       , __a=3_7       , __a="gelu"       , __a=0.1       , __a=0.1       , __a=1_0       , __a=0.02       , __a=3       , __a=None       , __a=[0, 1, 2, 3]       , )      ->   Optional[int]:
      """simple docstring"""






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

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







   def      SCREAMING_SNAKE_CASE__       (self)      ->   Optional[Any]:
      """simple docstring"""






      __snake_case					:   List[str]  				=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

      __snake_case					:   List[Any]  				=			None
      __snake_case					:   Optional[int]  				=			None
      if self.use_labels:
         __snake_case					:   int  				=			ids_tensor([self.batch_size]       , self.type_sequence_label_size)
         __snake_case					:   Optional[int]  				=			ids_tensor([self.batch_size, self.image_size, self.image_size]       , self.num_labels)

      __snake_case					:   List[str]  				=			self.get_config()

      return config, pixel_values, labels, pixel_labels







   def      SCREAMING_SNAKE_CASE__       (self)      ->   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=__a       , initializer_range=self.initializer_range       , out_indices=self.out_indices       , )







   def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a       , __a       , __a)      ->   Optional[Any]:
      """simple docstring"""






      __snake_case					:   Any  				=			DataaVecVisionModel(config=__a)
      model.to(__a)
      model.eval()
      __snake_case					:   Tuple  				=			model(__a)
      # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
      __snake_case					:   List[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      SCREAMING_SNAKE_CASE__       (self       , __a       , __a       , __a       , __a)      ->   int:
      """simple docstring"""






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







   def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a       , __a       , __a)      ->   List[Any]:
      """simple docstring"""






      __snake_case					:   Optional[int]  				=			self.num_labels
      __snake_case					:   Tuple  				=			DataaVecVisionForSemanticSegmentation(__a)
      model.to(__a)
      model.eval()
      __snake_case					:   str  				=			model(__a)
      self.parent.assertEqual(
          result.logits.shape       , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2))
      __snake_case					:   Any  				=			model(__a       , labels=__a)
      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)      ->   Optional[int]:
      """simple docstring"""






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




@require_torch
class a_     (     UpperCamelCase_		,   UpperCamelCase_		,   unittest.TestCase				):
   _snake_case	   =				(
       (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
       if is_torch_available()
       else ()
   )
   _snake_case	   =				(
       {
           """feature-extraction""": DataaVecVisionModel,
           """image-classification""": DataaVecVisionForImageClassification,
           """image-segmentation""": DataaVecVisionForSemanticSegmentation,
       }
       if is_torch_available()
       else {}
   )

   _snake_case	   =				False
   _snake_case	   =				False
   _snake_case	   =				False







   def      SCREAMING_SNAKE_CASE__       (self)      ->   Tuple:
      """simple docstring"""






      __snake_case					:   Optional[Any]  				=			DataaVecVisionModelTester(self)
      __snake_case					:   List[str]  				=			ConfigTester(
          self       , config_class=__a       , has_text_modality=__a       , hidden_size=3_7)







   def      SCREAMING_SNAKE_CASE__       (self)      ->   Tuple:
      """simple docstring"""






      self.config_tester.run_common_tests()







   def      SCREAMING_SNAKE_CASE__       (self)      ->   List[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)      ->   str:
      """simple docstring"""






      pass







   def      SCREAMING_SNAKE_CASE__       (self)      ->   Optional[Any]:
      """simple docstring"""






      __snake_case				,__snake_case					:   Union[str, Any]  				=			self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
         __snake_case					:   List[str]  				=			model_class(__a)
         self.assertIsInstance(model.get_input_embeddings()       , (nn.Module))
         __snake_case					:   Tuple  				=			model.get_output_embeddings()
         self.assertTrue(x is None or isinstance(__a       , nn.Linear))







   def      SCREAMING_SNAKE_CASE__       (self)      ->   Tuple:
      """simple docstring"""






      __snake_case				,__snake_case					:   Union[str, Any]  				=			self.model_tester.prepare_config_and_inputs_for_common()

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

         __snake_case					:   List[Any]  				=			['pixel_values']
         self.assertListEqual(arg_names[:1]       , __a)







   def      SCREAMING_SNAKE_CASE__       (self)      ->   List[Any]:
      """simple docstring"""






      __snake_case					:   List[Any]  				=			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__a)







   def      SCREAMING_SNAKE_CASE__       (self)      ->   Tuple:
      """simple docstring"""






      __snake_case					:   int  				=			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_segmentation(*__a)







   def      SCREAMING_SNAKE_CASE__       (self)      ->   Union[str, Any]:
      """simple docstring"""






      if not self.model_tester.is_training:
         return

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

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

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







   def      SCREAMING_SNAKE_CASE__       (self)      ->   Any:
      """simple docstring"""






      __snake_case				,__snake_case					:   Union[str, Any]  				=			self.model_tester.prepare_config_and_inputs_for_common()
      if not self.model_tester.is_training:
         return

      __snake_case					:   Tuple  				=			False
      __snake_case					:   List[Any]  				=			True

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







   def      SCREAMING_SNAKE_CASE__       (self)      ->   List[Any]:
      """simple docstring"""






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

      __snake_case					:   List[Any]  				=			_config_zero_init(__a)
      for model_class in self.all_model_classes:
         __snake_case					:   int  				=			model_class(config=__a)
         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       , __a       , __a       , __a       , __a=2E-4       , __a="outputs"       , __a=None)      ->   List[str]:
      """simple docstring"""






      super().check_pt_tf_outputs(__a       , __a       , __a       , __a       , __a       , __a)







   def      SCREAMING_SNAKE_CASE__       (self)      ->   str:
      """simple docstring"""






      __snake_case					:   Tuple  				=			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*__a)







   @slow
   def      SCREAMING_SNAKE_CASE__       (self)      ->   Optional[int]:
      """simple docstring"""






      for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         __snake_case					:   Dict  				=			DataaVecVisionModel.from_pretrained(__a)
         self.assertIsNotNone(__a)




def 							_SCREAMING_SNAKE_CASE	(					)     ->			List[Any]:







   """simple docstring"""
   __snake_case					:   Tuple  				=			Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'	)
   return image





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







   @cached_property
   def      SCREAMING_SNAKE_CASE__       (self)      ->   Optional[Any]:
      """simple docstring"""






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







   @slow
   def      SCREAMING_SNAKE_CASE__       (self)      ->   int:
      """simple docstring"""






      __snake_case					:   str  				=			DataaVecVisionForImageClassification.from_pretrained('facebook/data2vec-vision-base-ft1k').to(
          __a)

      __snake_case					:   Any  				=			self.default_image_processor
      __snake_case					:   Tuple  				=			prepare_img()
      __snake_case					:   Tuple  				=			image_processor(images=__a       , return_tensors='pt').to(__a)

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

      # verify the logits
      __snake_case					:   Optional[Any]  				=			torch.Size((1, 1_0_0_0))
      self.assertEqual(logits.shape       , __a)

      __snake_case					:   List[str]  				=			torch.tensor([0.3_277, -0.1_395, 0.0_911]).to(__a)

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

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







'''simple docstring'''






from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__A     						= {
    '''configuration_prophetnet''': ['''PROPHETNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ProphetNetConfig'''],
    '''tokenization_prophetnet''': ['''ProphetNetTokenizer'''],
}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __A     						= [
          '''PROPHETNET_PRETRAINED_MODEL_ARCHIVE_LIST''',
          '''ProphetNetDecoder''',
          '''ProphetNetEncoder''',
          '''ProphetNetForCausalLM''',
          '''ProphetNetForConditionalGeneration''',
          '''ProphetNetModel''',
          '''ProphetNetPreTrainedModel''',
      ]


if TYPE_CHECKING:
      from .configuration_prophetnet import PROPHETNET_PRETRAINED_CONFIG_ARCHIVE_MAP, ProphetNetConfig
      from .tokenization_prophetnet import ProphetNetTokenizer

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_prophetnet import (
                PROPHETNET_PRETRAINED_MODEL_ARCHIVE_LIST,
                ProphetNetDecoder,
                ProphetNetEncoder,
                ProphetNetForCausalLM,
                ProphetNetForConditionalGeneration,
                ProphetNetModel,
                ProphetNetPreTrainedModel,
            )

else:
      import sys

      __A     						= _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)