



"""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       __lowerCamelCase	:







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




      '''simple docstring'''




      _lowerCAmelCase					     =		parent
      _lowerCAmelCase					     =		100
      _lowerCAmelCase					     =		batch_size
      _lowerCAmelCase					     =		image_size
      _lowerCAmelCase					     =		patch_size
      _lowerCAmelCase					     =		num_channels
      _lowerCAmelCase					     =		is_training
      _lowerCAmelCase					     =		use_labels
      _lowerCAmelCase					     =		hidden_size
      _lowerCAmelCase					     =		num_hidden_layers
      _lowerCAmelCase					     =		num_attention_heads
      _lowerCAmelCase					     =		intermediate_size
      _lowerCAmelCase					     =		hidden_act
      _lowerCAmelCase					     =		hidden_dropout_prob
      _lowerCAmelCase					     =		attention_probs_dropout_prob
      _lowerCAmelCase					     =		type_sequence_label_size
      _lowerCAmelCase					     =		initializer_range
      _lowerCAmelCase					     =		scope
      _lowerCAmelCase					     =		out_indices
      _lowerCAmelCase					     =		num_labels

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







   def 							A__						(self				):




      '''simple docstring'''




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

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

      _lowerCAmelCase					     =		self.get_config()

      return config, pixel_values, labels, pixel_labels







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







   def 							A__						(self       ,	lowerCamelCase       ,	lowerCamelCase       ,	lowerCamelCase       ,	lowerCamelCase				):




      '''simple docstring'''




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




      '''simple docstring'''




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







   def 							A__						(self       ,	lowerCamelCase       ,	lowerCamelCase       ,	lowerCamelCase       ,	lowerCamelCase				):




      '''simple docstring'''




      _lowerCAmelCase					     =		self.num_labels
      _lowerCAmelCase					     =		DataaVecVisionForSemanticSegmentation(lowerCamelCase				)
      model.to(lowerCamelCase				)
      model.eval()
      _lowerCAmelCase					     =		model(lowerCamelCase				)
      self.parent.assertEqual(
          result.logits.shape       ,	(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)				)
      _lowerCAmelCase					     =		model(lowerCamelCase       ,	labels=lowerCamelCase				)
      self.parent.assertEqual(
          result.logits.shape       ,	(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)				)







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase					     =		self.prepare_config_and_inputs()
      _lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase					     =		config_and_inputs
      _lowerCAmelCase					     =		{"""pixel_values""": pixel_values}
      return config, inputs_dict





@require_torch
class       __lowerCamelCase	(   __lowercase   ,							__lowercase   ,							unittest.TestCase    ):
   __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 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase					     =		DataaVecVisionModelTester(self				)
      _lowerCAmelCase					     =		ConfigTester(
          self       ,	config_class=lowerCamelCase       ,	has_text_modality=lowerCamelCase       ,	hidden_size=37				)







   def 							A__						(self				):




      '''simple docstring'''




      self.config_tester.run_common_tests()







   def 							A__						(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 							A__						(self				):




      '''simple docstring'''




      pass







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase     ,						_lowerCAmelCase					     =		self.model_tester.prepare_config_and_inputs_for_common()

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







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase     ,						_lowerCAmelCase					     =		self.model_tester.prepare_config_and_inputs_for_common()

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

         _lowerCAmelCase					     =		["""pixel_values"""]
         self.assertListEqual(arg_names[:1]       ,	lowerCamelCase				)







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase					     =		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*lowerCamelCase				)







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase					     =		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_segmentation(*lowerCamelCase				)







   def 							A__						(self				):




      '''simple docstring'''




      if not self.model_tester.is_training:
         return

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

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

         _lowerCAmelCase					     =		model_class(lowerCamelCase				)
         model.to(lowerCamelCase				)
         model.train()
         _lowerCAmelCase					     =		self._prepare_for_class(lowerCamelCase       ,	lowerCamelCase       ,	return_labels=lowerCamelCase				)
         _lowerCAmelCase					     =		model(**lowerCamelCase				).loss
         loss.backward()







   def 							A__						(self				):




      '''simple docstring'''




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

      _lowerCAmelCase					     =		False
      _lowerCAmelCase					     =		True

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







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase     ,						_lowerCAmelCase					     =		self.model_tester.prepare_config_and_inputs_for_common()

      _lowerCAmelCase					     =		_config_zero_init(lowerCamelCase				)
      for model_class in self.all_model_classes:
         _lowerCAmelCase					     =		model_class(config=lowerCamelCase				)
         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 							A__						(self       ,	lowerCamelCase       ,	lowerCamelCase       ,	lowerCamelCase       ,	lowerCamelCase=2e-4       ,	lowerCamelCase="outputs"       ,	lowerCamelCase=None				):




      '''simple docstring'''




      super().check_pt_tf_outputs(lowerCamelCase       ,	lowerCamelCase       ,	lowerCamelCase       ,	lowerCamelCase       ,	lowerCamelCase       ,	lowerCamelCase				)







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase					     =		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*lowerCamelCase				)







   @slow
   def 							A__						(self				):




      '''simple docstring'''




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






def 	__UpperCAmelCase					(       )							->	Tuple:






   """simple docstring"""


   _lowerCAmelCase					     =		Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""   )
   return image



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







   @cached_property
   def 							A__						(self				):




      '''simple docstring'''




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







   @slow
   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase					     =		DataaVecVisionForImageClassification.from_pretrained("""facebook/data2vec-vision-base-ft1k"""				).to(
          lowerCamelCase				)

      _lowerCAmelCase					     =		self.default_image_processor
      _lowerCAmelCase					     =		prepare_img()
      _lowerCAmelCase					     =		image_processor(images=lowerCamelCase       ,	return_tensors="""pt"""				).to(lowerCamelCase				)

      # forward pass
      with torch.no_grad():
         _lowerCAmelCase					     =		model(**lowerCamelCase				)
      _lowerCAmelCase					     =		outputs.logits

      # verify the logits
      _lowerCAmelCase					     =		torch.Size((1, 1_000)				)
      self.assertEqual(logits.shape       ,	lowerCamelCase				)

      _lowerCAmelCase					     =		torch.tensor([0.3277, -0.1395, 0.0911]				).to(lowerCamelCase				)

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

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




"""simple docstring"""





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


SCREAMING_SNAKE_CASE				:     Union[str, Any]          =	{
    '''configuration_align''': [
        '''ALIGN_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''AlignConfig''',
        '''AlignTextConfig''',
        '''AlignVisionConfig''',
    ],
    '''processing_align''': ['''AlignProcessor'''],
}

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  SCREAMING_SNAKE_CASE				:     Any          =	[
      '''ALIGN_PRETRAINED_MODEL_ARCHIVE_LIST''',
      '''AlignModel''',
      '''AlignPreTrainedModel''',
      '''AlignTextModel''',
      '''AlignVisionModel''',
  ]

if TYPE_CHECKING:
  from .configuration_align import (
      ALIGN_PRETRAINED_CONFIG_ARCHIVE_MAP,
      AlignConfig,
      AlignTextConfig,
      AlignVisionConfig,
  )
  from .processing_align import AlignProcessor

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_align import (
        ALIGN_PRETRAINED_MODEL_ARCHIVE_LIST,
        AlignModel,
        AlignPreTrainedModel,
        AlignTextModel,
        AlignVisionModel,
    )

else:
  import sys

  SCREAMING_SNAKE_CASE				:     Any          =	_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)