

"""simple docstring"""
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       lowerCamelCase	:


  '''simple docstring'''







  def __init__(self							,       _lowerCamelCase							,       _lowerCamelCase=13							,       _lowerCamelCase=30							,       _lowerCamelCase=2							,       _lowerCamelCase=3							,       _lowerCamelCase=True							,       _lowerCamelCase=True							,       _lowerCamelCase=32							,       _lowerCamelCase=5							,       _lowerCamelCase=4							,       _lowerCamelCase=37							,       _lowerCamelCase="gelu"							,       _lowerCamelCase=0.1							,       _lowerCamelCase=0.1							,       _lowerCamelCase=10							,       _lowerCamelCase=0.02							,       _lowerCamelCase=None							,       _lowerCamelCase=2							,       ):


        """simple docstring"""

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

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



  def  _a      (self	):


        """simple docstring"""

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

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

        UpperCAmelCase__			:							str							=    self.get_config()

        return config, pixel_values, labels



  def  _a      (self	):


        """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=_lowerCamelCase							,       initializer_range=self.initializer_range							,       encoder_stride=self.encoder_stride							,       )



  def  _a      (self							,       _lowerCamelCase							,       _lowerCamelCase							,       _lowerCamelCase	):


        """simple docstring"""

        UpperCAmelCase__			:							Tuple							=    ViTModel(config=_lowerCamelCase	)
        model.to(_lowerCamelCase	)
        model.eval()
        UpperCAmelCase__			:							str							=    model(_lowerCamelCase	)
        self.parent.assertEqual(result.last_hidden_state.shape							,       (self.batch_size, self.seq_length, self.hidden_size)	)



  def  _a      (self							,       _lowerCamelCase							,       _lowerCamelCase							,       _lowerCamelCase	):


        """simple docstring"""

        UpperCAmelCase__			:							int							=    ViTForMaskedImageModeling(config=_lowerCamelCase	)
        model.to(_lowerCamelCase	)
        model.eval()
        UpperCAmelCase__			:							Optional[Any]							=    model(_lowerCamelCase	)
        self.parent.assertEqual(
            result.reconstruction.shape							,       (self.batch_size, self.num_channels, self.image_size, self.image_size)	)

        # test greyscale images
        UpperCAmelCase__			:							int							=    1
        UpperCAmelCase__			:							List[str]							=    ViTForMaskedImageModeling(_lowerCamelCase	)
        model.to(_lowerCamelCase	)
        model.eval()

        UpperCAmelCase__			:							str							=    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
        UpperCAmelCase__			:							Dict							=    model(_lowerCamelCase	)
        self.parent.assertEqual(result.reconstruction.shape							,       (self.batch_size, 1, self.image_size, self.image_size)	)



  def  _a      (self							,       _lowerCamelCase							,       _lowerCamelCase							,       _lowerCamelCase	):


        """simple docstring"""

        UpperCAmelCase__			:							List[Any]							=    self.type_sequence_label_size
        UpperCAmelCase__			:							Any							=    ViTForImageClassification(_lowerCamelCase	)
        model.to(_lowerCamelCase	)
        model.eval()
        UpperCAmelCase__			:							Any							=    model(_lowerCamelCase							,       labels=_lowerCamelCase	)
        self.parent.assertEqual(result.logits.shape							,       (self.batch_size, self.type_sequence_label_size)	)

        # test greyscale images
        UpperCAmelCase__			:							Dict							=    1
        UpperCAmelCase__			:							List[Any]							=    ViTForImageClassification(_lowerCamelCase	)
        model.to(_lowerCamelCase	)
        model.eval()

        UpperCAmelCase__			:							List[str]							=    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
        UpperCAmelCase__			:							Optional[Any]							=    model(_lowerCamelCase	)
        self.parent.assertEqual(result.logits.shape							,       (self.batch_size, self.type_sequence_label_size)	)




  def  _a      (self	):


        """simple docstring"""

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




@require_torch
class       lowerCamelCase	(       lowerCAmelCase__					,					lowerCAmelCase__					,					unittest.TestCase   ):


  '''simple docstring'''







  SCREAMING_SNAKE_CASE					      =  (
      (
          ViTModel,
          ViTForImageClassification,
          ViTForMaskedImageModeling,
      )
      if is_torch_available()
      else ()
  )
  SCREAMING_SNAKE_CASE					      =  (
      {'feature-extraction': ViTModel, 'image-classification': ViTForImageClassification}
      if is_torch_available()
      else {}
  )
  SCREAMING_SNAKE_CASE					      =  True

  SCREAMING_SNAKE_CASE					      =  False
  SCREAMING_SNAKE_CASE					      =  False
  SCREAMING_SNAKE_CASE					      =  False



  def  _a      (self	):


        """simple docstring"""

        UpperCAmelCase__			:							str							=    ViTModelTester(self	)
        UpperCAmelCase__			:							int							=    ConfigTester(self							,       config_class=_lowerCamelCase							,       has_text_modality=_lowerCamelCase							,       hidden_size=37	)



  def  _a      (self	):


        """simple docstring"""

        self.config_tester.run_common_tests()



  @unittest.skip(reason="""ViT does not use inputs_embeds"""	)
  def  _a      (self	):


        """simple docstring"""

        pass



  def  _a      (self	):


        """simple docstring"""

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

        for model_class in self.all_model_classes:
              UpperCAmelCase__			:							Union[str, Any]							=    model_class(_lowerCamelCase	)
              self.assertIsInstance(model.get_input_embeddings()							,       (nn.Module)	)
              UpperCAmelCase__			:							Dict							=    model.get_output_embeddings()
              self.assertTrue(x is None or isinstance(_lowerCamelCase							,       nn.Linear	)	)



  def  _a      (self	):


        """simple docstring"""

        UpperCAmelCase__					,		UpperCAmelCase__			:							str							=    self.model_tester.prepare_config_and_inputs_for_common()

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

              UpperCAmelCase__			:							Tuple							=    ["""pixel_values"""]
              self.assertListEqual(arg_names[:1]							,       _lowerCamelCase	)



  def  _a      (self	):


        """simple docstring"""

        UpperCAmelCase__			:							List[Any]							=    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_lowerCamelCase	)



  def  _a      (self	):


        """simple docstring"""

        UpperCAmelCase__			:							Optional[Any]							=    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_image_modeling(*_lowerCamelCase	)



  def  _a      (self	):


        """simple docstring"""

        UpperCAmelCase__			:							Optional[Any]							=    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 VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              UpperCAmelCase__			:							Tuple							=    ViTModel.from_pretrained(_lowerCamelCase	)
              self.assertIsNotNone(_lowerCamelCase	)




def 					a__						(						)	-> Optional[Any]:
      UpperCAmelCase__			:							Optional[int]							=    Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""    )
      return image





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


  '''simple docstring'''







  @cached_property
  def  _a      (self	):


        """simple docstring"""

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



  @slow
  def  _a      (self	):


        """simple docstring"""

        UpperCAmelCase__			:							List[str]							=    ViTForImageClassification.from_pretrained("""google/vit-base-patch16-224"""	).to(_lowerCamelCase	)

        UpperCAmelCase__			:							Dict							=    self.default_image_processor
        UpperCAmelCase__			:							Any							=    prepare_img()
        UpperCAmelCase__			:							Tuple							=    image_processor(images=_lowerCamelCase							,       return_tensors="""pt"""	).to(_lowerCamelCase	)

        # forward pass
        with torch.no_grad():
              UpperCAmelCase__			:							List[Any]							=    model(**_lowerCamelCase	)

        # verify the logits
        UpperCAmelCase__			:							List[Any]							=    torch.Size((1, 1000)	)
        self.assertEqual(outputs.logits.shape							,       _lowerCamelCase	)

        UpperCAmelCase__			:							Any							=    torch.tensor([-0.2_744, 0.8_215, -0.0_836]	).to(_lowerCamelCase	)

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



  @slow
  def  _a      (self	):


        """simple docstring"""

        UpperCAmelCase__			:							Tuple							=    ViTModel.from_pretrained("""facebook/dino-vits8"""	).to(_lowerCamelCase	)

        UpperCAmelCase__			:							List[Any]							=    ViTImageProcessor.from_pretrained("""facebook/dino-vits8"""							,       size=480	)
        UpperCAmelCase__			:							Tuple							=    prepare_img()
        UpperCAmelCase__			:							Tuple							=    image_processor(images=_lowerCamelCase							,       return_tensors="""pt"""	)
        UpperCAmelCase__			:							Dict							=    inputs.pixel_values.to(_lowerCamelCase	)

        # forward pass
        with torch.no_grad():
              UpperCAmelCase__			:							Dict							=    model(_lowerCamelCase							,       interpolate_pos_encoding=_lowerCamelCase	)

        # verify the logits
        UpperCAmelCase__			:							Optional[Any]							=    torch.Size((1, 3601, 384)	)
        self.assertEqual(outputs.last_hidden_state.shape							,       _lowerCamelCase	)

        UpperCAmelCase__			:							Optional[int]							=    torch.tensor(
            [[4.2_340, 4.3_906, -6.6_692], [4.5_463, 1.8_928, -6.7_257], [4.4_429, 0.8_496, -5.8_585]]	).to(_lowerCamelCase	)

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




  @slow
  @require_accelerate
  @require_torch_gpu
  def  _a      (self	):


        """simple docstring"""

        UpperCAmelCase__			:							Tuple							=    ViTModel.from_pretrained("""facebook/dino-vits8"""							,       torch_dtype=torch.floataa							,       device_map="""auto"""	)
        UpperCAmelCase__			:							Any							=    self.default_image_processor

        UpperCAmelCase__			:							int							=    prepare_img()
        UpperCAmelCase__			:							str							=    image_processor(images=_lowerCamelCase							,       return_tensors="""pt"""	)
        UpperCAmelCase__			:							Any							=    inputs.pixel_values.to(_lowerCamelCase	)

        # forward pass to make sure inference works in fp16
        with torch.no_grad():
              UpperCAmelCase__			:							List[str]							=    model(_lowerCamelCase	)




"""simple docstring"""
from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available


_A							=  {}

try:
    if not is_sentencepiece_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    _A							=  ["""BartphoTokenizer"""]

if TYPE_CHECKING:
    try:
        if not is_sentencepiece_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .tokenization_bartpho import BartphoTokenizer

else:
    import sys

    _A							=  _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

