

import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import (
    require_accelerate,
    require_torch,
    require_torch_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, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch
  from torch import nn

  from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel
  from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import ViTImageProcessor





class A__    :



 """simple docstring"""







 def __init__(		self :	Optional[Any]     ,						A_ :	Optional[Any]     ,						A_ :	Optional[int]=1_3     ,						A_ :	List[str]=3_0     ,						A_ :	int=2     ,						A_ :	str=3     ,						A_ :	Optional[int]=True     ,						A_ :	int=True     ,						A_ :	List[str]=3_2     ,						A_ :	Any=5     ,						A_ :	Dict=4     ,						A_ :	Optional[Any]=3_7     ,						A_ :	List[Any]="gelu"     ,						A_ :	Union[str, Any]=0.1     ,						A_ :	Optional[int]=0.1     ,						A_ :	Optional[Any]=1_0     ,						A_ :	str=0.02     ,						A_ :	Any=None     ,						A_ :	Optional[Any]=2     ,						):






      '''simple docstring'''




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

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

 def    __magic_name__       (		self :	Optional[Any]       ):






      '''simple docstring'''




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

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

      _lowerCAmelCase   :					List[Any]        =    self.get_config()

      return config, pixel_values, labels

 def    __magic_name__       (		self :	str       ):






      '''simple docstring'''




      return ViTConfig(
          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=_UpperCAmelCase     ,						initializer_range=self.initializer_range     ,						encoder_stride=self.encoder_stride     ,						)

 def    __magic_name__       (		self :	List[str]     ,						A_ :	Any     ,						A_ :	Tuple     ,						A_ :	Union[str, Any]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Optional[Any]        =    ViTModel(config=_UpperCAmelCase       )
      model.to(_UpperCAmelCase       )
      model.eval()
      _lowerCAmelCase   :					Optional[Any]        =    model(_UpperCAmelCase       )
      self.parent.assertEqual(result.last_hidden_state.shape     ,						(self.batch_size, self.seq_length, self.hidden_size)       )

 def    __magic_name__       (		self :	int     ,						A_ :	Optional[Any]     ,						A_ :	Optional[int]     ,						A_ :	Optional[int]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					str        =    ViTForMaskedImageModeling(config=_UpperCAmelCase       )
      model.to(_UpperCAmelCase       )
      model.eval()
      _lowerCAmelCase   :					Dict        =    model(_UpperCAmelCase       )
      self.parent.assertEqual(
          result.reconstruction.shape     ,						(self.batch_size, self.num_channels, self.image_size, self.image_size)       )

      # test greyscale images
      _lowerCAmelCase   :					List[str]        =    1
      _lowerCAmelCase   :					Optional[Any]        =    ViTForMaskedImageModeling(_UpperCAmelCase       )
      model.to(_UpperCAmelCase       )
      model.eval()

      _lowerCAmelCase   :					str        =    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
      _lowerCAmelCase   :					Any        =    model(_UpperCAmelCase       )
      self.parent.assertEqual(result.reconstruction.shape     ,						(self.batch_size, 1, self.image_size, self.image_size)       )

 def    __magic_name__       (		self :	int     ,						A_ :	List[Any]     ,						A_ :	str     ,						A_ :	Optional[int]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					str        =    self.type_sequence_label_size
      _lowerCAmelCase   :					List[Any]        =    ViTForImageClassification(_UpperCAmelCase       )
      model.to(_UpperCAmelCase       )
      model.eval()
      _lowerCAmelCase   :					Optional[int]        =    model(_UpperCAmelCase     ,						labels=_UpperCAmelCase       )
      self.parent.assertEqual(result.logits.shape     ,						(self.batch_size, self.type_sequence_label_size)       )

      # test greyscale images
      _lowerCAmelCase   :					Dict        =    1
      _lowerCAmelCase   :					Optional[Any]        =    ViTForImageClassification(_UpperCAmelCase       )
      model.to(_UpperCAmelCase       )
      model.eval()

      _lowerCAmelCase   :					Optional[Any]        =    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
      _lowerCAmelCase   :					Optional[int]        =    model(_UpperCAmelCase       )
      self.parent.assertEqual(result.logits.shape     ,						(self.batch_size, self.type_sequence_label_size)       )


 def    __magic_name__       (		self :	Dict       ):






      '''simple docstring'''




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





@require_torch
class A__    ( _UpperCamelCase  ,		_UpperCamelCase  ,		unittest.TestCase ):



 """simple docstring"""







 _lowercase     :					Any      =						(
     (
         ViTModel,
         ViTForImageClassification,
         ViTForMaskedImageModeling,
     )
     if is_torch_available()
     else ()
 )
 _lowercase     :					Optional[int]      =						(
     {'''feature-extraction''': ViTModel, '''image-classification''': ViTForImageClassification}
     if is_torch_available()
     else {}
 )
 _lowercase     :					str      =						True

 _lowercase     :					Dict      =						False
 _lowercase     :					Any      =						False
 _lowercase     :					Union[str, Any]      =						False

 def    __magic_name__       (		self :	List[str]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					List[str]        =    ViTModelTester(self       )
      _lowerCAmelCase   :					Optional[int]        =    ConfigTester(self     ,						config_class=_UpperCAmelCase     ,						has_text_modality=_UpperCAmelCase     ,						hidden_size=3_7       )

 def    __magic_name__       (		self :	int       ):






      '''simple docstring'''




      self.config_tester.run_common_tests()

 @unittest.skip(reason="ViT does not use inputs_embeds"       )
 def    __magic_name__       (		self :	Optional[Any]       ):






      '''simple docstring'''




      pass

 def    __magic_name__       (		self :	Optional[int]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					int        =    self.model_tester.prepare_config_and_inputs_for_common()

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

 def    __magic_name__       (		self :	Optional[int]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Tuple        =    self.model_tester.prepare_config_and_inputs_for_common()

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

           _lowerCAmelCase   :					List[str]        =    ['''pixel_values''']
           self.assertListEqual(arg_names[:1]     ,						_UpperCAmelCase       )

 def    __magic_name__       (		self :	List[str]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Optional[int]        =    self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*_UpperCAmelCase       )

 def    __magic_name__       (		self :	Tuple       ):






      '''simple docstring'''




      _lowerCAmelCase   :					List[Any]        =    self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_image_modeling(*_UpperCAmelCase       )

 def    __magic_name__       (		self :	List[Any]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					List[str]        =    self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*_UpperCAmelCase       )


 @slow
 def    __magic_name__       (		self :	int       ):






      '''simple docstring'''




      for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _lowerCAmelCase   :					Union[str, Any]        =    ViTModel.from_pretrained(_UpperCAmelCase       )
           self.assertIsNotNone(_UpperCAmelCase       )







def 	_snake_case		(    )       ->      Union[str, Any]:

     """simple docstring"""







     _lowerCAmelCase   :					Optional[int]        =    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"				)
     return image





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



 """simple docstring"""







 @cached_property
 def    __magic_name__       (		self :	Optional[Any]       ):






      '''simple docstring'''




      return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224"       ) if is_vision_available() else None

 @slow
 def    __magic_name__       (		self :	Any       ):






      '''simple docstring'''




      _lowerCAmelCase   :					str        =    ViTForImageClassification.from_pretrained("google/vit-base-patch16-224"       ).to(_UpperCAmelCase       )

      _lowerCAmelCase   :					str        =    self.default_image_processor
      _lowerCAmelCase   :					int        =    prepare_img()
      _lowerCAmelCase   :					Tuple        =    image_processor(images=_UpperCAmelCase     ,						return_tensors="pt"       ).to(_UpperCAmelCase       )

      # forward pass
      with torch.no_grad():
           _lowerCAmelCase   :					Union[str, Any]        =    model(**_UpperCAmelCase       )

      # verify the logits
      _lowerCAmelCase   :					Dict        =    torch.Size((1, 1_0_0_0)       )
      self.assertEqual(outputs.logits.shape     ,						_UpperCAmelCase       )

      _lowerCAmelCase   :					List[Any]        =    torch.tensor([-0.2744, 0.8215, -0.0836]       ).to(_UpperCAmelCase       )

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

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






      '''simple docstring'''




      _lowerCAmelCase   :					Tuple        =    ViTModel.from_pretrained("facebook/dino-vits8"       ).to(_UpperCAmelCase       )

      _lowerCAmelCase   :					Union[str, Any]        =    ViTImageProcessor.from_pretrained("facebook/dino-vits8"     ,						size=4_8_0       )
      _lowerCAmelCase   :					Any        =    prepare_img()
      _lowerCAmelCase   :					Tuple        =    image_processor(images=_UpperCAmelCase     ,						return_tensors="pt"       )
      _lowerCAmelCase   :					int        =    inputs.pixel_values.to(_UpperCAmelCase       )

      # forward pass
      with torch.no_grad():
           _lowerCAmelCase   :					Union[str, Any]        =    model(_UpperCAmelCase     ,						interpolate_pos_encoding=_UpperCAmelCase       )

      # verify the logits
      _lowerCAmelCase   :					Any        =    torch.Size((1, 3_6_0_1, 3_8_4)       )
      self.assertEqual(outputs.last_hidden_state.shape     ,						_UpperCAmelCase       )

      _lowerCAmelCase   :					str        =    torch.tensor(
          [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]]       ).to(_UpperCAmelCase       )

      self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3]     ,						_UpperCAmelCase     ,						atol=1E-4       )       )


 @slow
 @require_accelerate
 @require_torch_gpu
 def    __magic_name__       (		self :	Optional[Any]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Tuple        =    ViTModel.from_pretrained("facebook/dino-vits8"     ,						torch_dtype=torch.floataa     ,						device_map="auto"       )
      _lowerCAmelCase   :					Union[str, Any]        =    self.default_image_processor

      _lowerCAmelCase   :					List[Any]        =    prepare_img()
      _lowerCAmelCase   :					Any        =    image_processor(images=_UpperCAmelCase     ,						return_tensors="pt"       )
      _lowerCAmelCase   :					Dict        =    inputs.pixel_values.to(_UpperCAmelCase       )

      # forward pass to make sure inference works in fp16
      with torch.no_grad():
           _lowerCAmelCase   :					int        =    model(_UpperCAmelCase       )









from typing import TYPE_CHECKING

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


__UpperCAmelCase    				=     {
    'configuration_lxmert': ['LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LxmertConfig'],
    'tokenization_lxmert': ['LxmertTokenizer'],
}

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __UpperCAmelCase    				=     ['LxmertTokenizerFast']

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __UpperCAmelCase    				=     [
      'LxmertEncoder',
      'LxmertForPreTraining',
      'LxmertForQuestionAnswering',
      'LxmertModel',
      'LxmertPreTrainedModel',
      'LxmertVisualFeatureEncoder',
      'LxmertXLayer',
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __UpperCAmelCase    				=     [
      'TF_LXMERT_PRETRAINED_MODEL_ARCHIVE_LIST',
      'TFLxmertForPreTraining',
      'TFLxmertMainLayer',
      'TFLxmertModel',
      'TFLxmertPreTrainedModel',
      'TFLxmertVisualFeatureEncoder',
  ]


if TYPE_CHECKING:
  from .configuration_lxmert import LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, LxmertConfig
  from .tokenization_lxmert import LxmertTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_lxmert_fast import LxmertTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_lxmert import (
        LxmertEncoder,
        LxmertForPreTraining,
        LxmertForQuestionAnswering,
        LxmertModel,
        LxmertPreTrainedModel,
        LxmertVisualFeatureEncoder,
        LxmertXLayer,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_lxmert import (
        TF_LXMERT_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFLxmertForPreTraining,
        TFLxmertMainLayer,
        TFLxmertModel,
        TFLxmertPreTrainedModel,
        TFLxmertVisualFeatureEncoder,
    )

else:
  import sys

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






