






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





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


      '''simple docstring'''




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

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





    def     lowerCamelCase__				(							self     : Tuple      ):


      '''simple docstring'''




      a					:      int    =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

      a					:      Any    =       None
      if self.use_labels:
        a					:      Union[str, Any]    =       ids_tensor([self.batch_size]    ,		self.type_sequence_label_size      )

      a					:      Any    =       self.get_config()

      return config, pixel_values, labels





    def     lowerCamelCase__				(							self     : Optional[int]      ):


      '''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=A    ,		initializer_range=self.initializer_range    ,		encoder_stride=self.encoder_stride    ,		)





    def     lowerCamelCase__				(							self     : Union[str, Any]    ,		A     : str    ,		A     : Tuple    ,		A     : int      ):


      '''simple docstring'''




      a					:      int    =       ViTModel(config=A      )
      model.to(A      )
      model.eval()
      a					:      Tuple    =       model(A      )
      self.parent.assertEqual(result.last_hidden_state.shape    ,		(self.batch_size, self.seq_length, self.hidden_size)      )





    def     lowerCamelCase__				(							self     : Union[str, Any]    ,		A     : Optional[int]    ,		A     : Dict    ,		A     : List[str]      ):


      '''simple docstring'''




      a					:      Union[str, Any]    =       ViTForMaskedImageModeling(config=A      )
      model.to(A      )
      model.eval()
      a					:      str    =       model(A      )
      self.parent.assertEqual(
          result.reconstruction.shape    ,		(self.batch_size, self.num_channels, self.image_size, self.image_size)      )

      # test greyscale images
      a					:      List[Any]    =       1
      a					:      Optional[int]    =       ViTForMaskedImageModeling(A      )
      model.to(A      )
      model.eval()

      a					:      Union[str, Any]    =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
      a					:      Tuple    =       model(A      )
      self.parent.assertEqual(result.reconstruction.shape    ,		(self.batch_size, 1, self.image_size, self.image_size)      )





    def     lowerCamelCase__				(							self     : List[Any]    ,		A     : Optional[int]    ,		A     : int    ,		A     : List[str]      ):


      '''simple docstring'''




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

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

      a					:      Union[str, Any]    =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
      a					:      Dict    =       model(A      )
      self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.type_sequence_label_size)      )





    def     lowerCamelCase__				(							self     : str      ):


      '''simple docstring'''




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






@require_torch
class 						snake_case  (  UpperCAmelCase       ,			UpperCAmelCase       ,			unittest.TestCase	):
    __magic_name__   					=					(
        (
            ViTModel,
            ViTForImageClassification,
            ViTForMaskedImageModeling,
        )
        if is_torch_available()
        else ()
    )
    __magic_name__   					=					(
        {'''feature-extraction''': ViTModel, '''image-classification''': ViTForImageClassification}
        if is_torch_available()
        else {}
    )
    __magic_name__   					=					True

    __magic_name__   					=					False
    __magic_name__   					=					False
    __magic_name__   					=					False





    def     lowerCamelCase__				(							self     : List[str]      ):


      '''simple docstring'''




      a					:      List[str]    =       ViTModelTester(self      )
      a					:      Optional[Any]    =       ConfigTester(self    ,		config_class=A    ,		has_text_modality=A    ,		hidden_size=3_7      )





    def     lowerCamelCase__				(							self     : Dict      ):


      '''simple docstring'''




      self.config_tester.run_common_tests()





    @unittest.skip(reason='ViT does not use inputs_embeds'      )
    def     lowerCamelCase__				(							self     : int      ):


      '''simple docstring'''




      pass





    def     lowerCamelCase__				(							self     : Optional[Any]      ):


      '''simple docstring'''




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

      for model_class in self.all_model_classes:
        a					:      Optional[Any]    =       model_class(A      )
        self.assertIsInstance(model.get_input_embeddings()    ,		(nn.Module)      )
        a					:      Optional[Any]    =       model.get_output_embeddings()
        self.assertTrue(x is None or isinstance(A    ,		nn.Linear      )      )





    def     lowerCamelCase__				(							self     : str      ):


      '''simple docstring'''




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

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

        a					:      Union[str, Any]    =       ['pixel_values']
        self.assertListEqual(arg_names[:1]    ,		A      )





    def     lowerCamelCase__				(							self     : Optional[Any]      ):


      '''simple docstring'''




      a					:      Optional[int]    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*A      )





    def     lowerCamelCase__				(							self     : List[Any]      ):


      '''simple docstring'''




      a					:      Tuple    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_image_modeling(*A      )





    def     lowerCamelCase__				(							self     : Optional[int]      ):


      '''simple docstring'''




      a					:      Dict    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*A      )





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


      '''simple docstring'''




      for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        a					:      Tuple    =       ViTModel.from_pretrained(A      )
        self.assertIsNotNone(A      )




def       snake_case  ():







  '''simple docstring'''

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






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





    @cached_property
    def     lowerCamelCase__				(							self     : Tuple      ):


      '''simple docstring'''




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





    @slow
    def     lowerCamelCase__				(							self     : str      ):


      '''simple docstring'''




      a					:      str    =       ViTForImageClassification.from_pretrained('google/vit-base-patch16-224'      ).to(A      )

      a					:      str    =       self.default_image_processor
      a					:      Optional[Any]    =       prepare_img()
      a					:      Optional[int]    =       image_processor(images=A    ,		return_tensors='pt'      ).to(A      )

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

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

      a					:      int    =       torch.tensor([-0.27_44, 0.82_15, -0.08_36]      ).to(A      )

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





    @slow
    def     lowerCamelCase__				(							self     : Tuple      ):


      '''simple docstring'''




      a					:      Optional[Any]    =       ViTModel.from_pretrained('facebook/dino-vits8'      ).to(A      )

      a					:      Dict    =       ViTImageProcessor.from_pretrained('facebook/dino-vits8'    ,		size=4_8_0      )
      a					:      Dict    =       prepare_img()
      a					:      Any    =       image_processor(images=A    ,		return_tensors='pt'      )
      a					:      Any    =       inputs.pixel_values.to(A      )

      # forward pass
      with torch.no_grad():
        a					:      List[str]    =       model(A    ,		interpolate_pos_encoding=A      )

      # verify the logits
      a					:      Optional[Any]    =       torch.Size((1, 3_6_0_1, 3_8_4)      )
      self.assertEqual(outputs.last_hidden_state.shape    ,		A      )

      a					:      str    =       torch.tensor(
          [[4.23_40, 4.39_06, -6.66_92], [4.54_63, 1.89_28, -6.72_57], [4.44_29, 0.84_96, -5.85_85]]      ).to(A      )

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





    @slow
    @require_accelerate
    @require_torch_gpu
    def     lowerCamelCase__				(							self     : str      ):


      '''simple docstring'''




      a					:      Tuple    =       ViTModel.from_pretrained('facebook/dino-vits8'    ,		torch_dtype=torch.floataa    ,		device_map='auto'      )
      a					:      List[Any]    =       self.default_image_processor

      a					:      Optional[Any]    =       prepare_img()
      a					:      Tuple    =       image_processor(images=A    ,		return_tensors='pt'      )
      a					:      Tuple    =       inputs.pixel_values.to(A      )

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












"""simple docstring"""


import argparse
from collections import defaultdict

import yaml


_UpperCamelCase    :   int   			=						'docs/source/en/_toctree.yml'




def       snake_case  (A_				:Optional[Any]				):







  '''simple docstring'''

  a					:      List[Any]    =       defaultdict(A_				)
  for doc in model_doc:
    counts[doc["local"]] += 1
  a					:      Optional[Any]    =       [key for key, value in counts.items() if value > 1]

  a					:      List[str]    =       []
  for duplicate_key in duplicates:
    a					:      int    =       list({doc['title'] for doc in model_doc if doc['local'] == duplicate_key}				)
    if len(A_				) > 1:
      raise ValueError(
          f'''{duplicate_key} is present several times in the documentation table of content at '''
          '`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the '
          'others.'				)
    # Only add this once
    new_doc.append({'local': duplicate_key, 'title': titles[0]}				)

  # Add none duplicate-keys
  new_doc.extend([doc for doc in model_doc if counts[doc['local']] == 1]				)

  # Sort
  return sorted(A_     ,      key=lambda A_				: s["title"].lower()				)




def       snake_case  (A_				:List[str]=False				):







  '''simple docstring'''

  with open(A_     ,      encoding='utf-8'				) as f:
    a					:      Dict    =       yaml.safe_load(f.read()				)

  # Get to the API doc
  a					:      Optional[Any]    =       0
  while content[api_idx]["title"] != "API":
    api_idx += 1
  a					:      List[str]    =       content[api_idx]['sections']

  # Then to the model doc
  a					:      Optional[int]    =       0
  while api_doc[model_idx]["title"] != "Models":
    model_idx += 1

  a					:      Optional[Any]    =       api_doc[model_idx]['sections']

  a					:      Dict    =       [(idx, section) for idx, section in enumerate(A_				) if 'sections' in section]
  a					:      List[str]    =       False
  for idx, modality_doc in modalities_docs:
    a					:      str    =       modality_doc['sections']
    a					:      str    =       clean_model_doc_toc(A_				)

    if old_modality_doc != new_modality_doc:
      a					:      str    =       True
      if overwrite:
        a					:      Any    =       new_modality_doc

  if diff:
    if overwrite:
      a					:      Any    =       model_doc
      a					:      str    =       api_doc
      with open(A_     ,      'w'     ,      encoding='utf-8'				) as f:
        f.write(yaml.dump(A_     ,      allow_unicode=A_				)				)
    else:
      raise ValueError(
          'The model doc part of the table of content is not properly sorted, run `make style` to fix this.'				)


if __name__ == "__main__":
   _UpperCamelCase    :   Optional[int]   			=						argparse.ArgumentParser()
   parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.')
   _UpperCamelCase    :   Any   			=						parser.parse_args()

   check_model_doc(args.fix_and_overwrite)




