






'''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 _lowercase			:




       def __init__(							self:       Union[str, Any]    ,							UpperCamelCase__:       List[Any]    ,							UpperCamelCase__:       List[str]=13    ,							UpperCamelCase__:       List[str]=30    ,							UpperCamelCase__:       List[Any]=2    ,							UpperCamelCase__:       Tuple=3    ,							UpperCamelCase__:       str=True    ,							UpperCamelCase__:       int=True    ,							UpperCamelCase__:       List[Any]=32    ,							UpperCamelCase__:       Any=5    ,							UpperCamelCase__:       Optional[Any]=4    ,							UpperCamelCase__:       List[Any]=37    ,							UpperCamelCase__:       List[Any]="gelu"    ,							UpperCamelCase__:       Optional[int]=0.1    ,							UpperCamelCase__:       Tuple=0.1    ,							UpperCamelCase__:       str=10    ,							UpperCamelCase__:       Optional[int]=0.02    ,							UpperCamelCase__:       Optional[Any]=None    ,							UpperCamelCase__:       List[Any]=2    ,							):
              lowerCamelCase__      :			Union[str, Any] 						= parent
              lowerCamelCase__      :			str 						= batch_size
              lowerCamelCase__      :			str 						= image_size
              lowerCamelCase__      :			Union[str, Any] 						= patch_size
              lowerCamelCase__      :			int 						= num_channels
              lowerCamelCase__      :			List[Any] 						= is_training
              lowerCamelCase__      :			str 						= use_labels
              lowerCamelCase__      :			Any 						= hidden_size
              lowerCamelCase__      :			Dict 						= num_hidden_layers
              lowerCamelCase__      :			int 						= num_attention_heads
              lowerCamelCase__      :			Any 						= intermediate_size
              lowerCamelCase__      :			List[Any] 						= hidden_act
              lowerCamelCase__      :			Union[str, Any] 						= hidden_dropout_prob
              lowerCamelCase__      :			str 						= attention_probs_dropout_prob
              lowerCamelCase__      :			Dict 						= type_sequence_label_size
              lowerCamelCase__      :			List[Any] 						= initializer_range
              lowerCamelCase__      :			List[str] 						= scope
              lowerCamelCase__      :			Union[str, Any] 						= encoder_stride

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




       def 	lowerCamelCase_       (							self:       str  ):
              lowerCamelCase__      :			List[Any] 						= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

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

              lowerCamelCase__      :			str 						= self.get_config()

              return config, pixel_values, labels




       def 	lowerCamelCase_       (							self:       Union[str, Any]  ):
              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 	lowerCamelCase_       (							self:       int    ,							UpperCamelCase__:       Optional[int]    ,							UpperCamelCase__:       Any    ,							UpperCamelCase__:       Union[str, Any]  ):
              lowerCamelCase__      :			str 						= ViTModel(config=UpperCamelCase__  )
              model.to(UpperCamelCase__  )
              model.eval()
              lowerCamelCase__      :			Union[str, Any] 						= model(UpperCamelCase__  )
              self.parent.assertEqual(result.last_hidden_state.shape    ,							(self.batch_size, self.seq_length, self.hidden_size)  )




       def 	lowerCamelCase_       (							self:       int    ,							UpperCamelCase__:       Dict    ,							UpperCamelCase__:       Union[str, Any]    ,							UpperCamelCase__:       int  ):
              lowerCamelCase__      :			int 						= ViTForMaskedImageModeling(config=UpperCamelCase__  )
              model.to(UpperCamelCase__  )
              model.eval()
              lowerCamelCase__      :			Union[str, Any] 						= 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[Any] 						= 1
              lowerCamelCase__      :			str 						= ViTForMaskedImageModeling(UpperCamelCase__  )
              model.to(UpperCamelCase__  )
              model.eval()

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




       def 	lowerCamelCase_       (							self:       List[str]    ,							UpperCamelCase__:       Union[str, Any]    ,							UpperCamelCase__:       Optional[int]    ,							UpperCamelCase__:       Optional[Any]  ):
              lowerCamelCase__      :			Union[str, Any] 						= self.type_sequence_label_size
              lowerCamelCase__      :			List[str] 						= ViTForImageClassification(UpperCamelCase__  )
              model.to(UpperCamelCase__  )
              model.eval()
              lowerCamelCase__      :			str 						= model(UpperCamelCase__    ,							labels=UpperCamelCase__  )
              self.parent.assertEqual(result.logits.shape    ,							(self.batch_size, self.type_sequence_label_size)  )

              # test greyscale images
              lowerCamelCase__      :			List[str] 						= 1
              lowerCamelCase__      :			List[Any] 						= ViTForImageClassification(UpperCamelCase__  )
              model.to(UpperCamelCase__  )
              model.eval()

              lowerCamelCase__      :			str 						= floats_tensor([self.batch_size, 1, self.image_size, self.image_size]  )
              lowerCamelCase__      :			List[str] 						= model(UpperCamelCase__  )
              self.parent.assertEqual(result.logits.shape    ,							(self.batch_size, self.type_sequence_label_size)  )




       def 	lowerCamelCase_       (							self:       int  ):
              lowerCamelCase__      :			Tuple 						= self.prepare_config_and_inputs()
              (
                  lowerCamelCase__	
              )      :			Optional[Any] 						= config_and_inputs
              lowerCamelCase__      :			str 						= {"""pixel_values""": pixel_values}
              return config, inputs_dict



@require_torch
class _lowercase			(       _lowercase       ,   _lowercase       ,   unittest.TestCase	):
       a          = (
           (
               ViTModel,
               ViTForImageClassification,
               ViTForMaskedImageModeling,
           )
           if is_torch_available()
           else ()
       )
       a          = (
           {"""feature-extraction""": ViTModel, """image-classification""": ViTForImageClassification}
           if is_torch_available()
           else {}
       )
       a          = True

       a          = False
       a          = False
       a          = False




       def 	lowerCamelCase_       (							self:       Optional[int]  ):
              lowerCamelCase__      :			List[Any] 						= ViTModelTester(self  )
              lowerCamelCase__      :			List[Any] 						= ConfigTester(self    ,							config_class=UpperCamelCase__    ,							has_text_modality=UpperCamelCase__    ,							hidden_size=37  )




       def 	lowerCamelCase_       (							self:       List[Any]  ):
              self.config_tester.run_common_tests()




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




       def 	lowerCamelCase_       (							self:       Any  ):
              lowerCamelCase__      :			str 						= self.model_tester.prepare_config_and_inputs_for_common()

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




       def 	lowerCamelCase_       (							self:       int  ):
              lowerCamelCase__      :			str 						= self.model_tester.prepare_config_and_inputs_for_common()

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

                     lowerCamelCase__      :			List[str] 						= ["""pixel_values"""]
                     self.assertListEqual(arg_names[:1]    ,							UpperCamelCase__  )




       def 	lowerCamelCase_       (							self:       Optional[int]  ):
              lowerCamelCase__      :			Optional[int] 						= self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(*UpperCamelCase__  )




       def 	lowerCamelCase_       (							self:       Dict  ):
              lowerCamelCase__      :			List[str] 						= self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_masked_image_modeling(*UpperCamelCase__  )




       def 	lowerCamelCase_       (							self:       List[str]  ):
              lowerCamelCase__      :			Optional[int] 						= self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__  )




       @slow
       def 	lowerCamelCase_       (							self:       Any  ):
              for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                     lowerCamelCase__      :			Optional[int] 						= ViTModel.from_pretrained(UpperCamelCase__  )
                     self.assertIsNotNone(UpperCamelCase__  )





def 				SCREAMING_SNAKE_CASE_ ()   -> Union[str, Any]:
       lowerCamelCase__      :			Tuple 						= Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""   )
       return image



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




       @cached_property
       def 	lowerCamelCase_       (							self:       Union[str, Any]  ):
              return ViTImageProcessor.from_pretrained("""google/vit-base-patch16-224"""  ) if is_vision_available() else None




       @slow
       def 	lowerCamelCase_       (							self:       List[Any]  ):
              lowerCamelCase__      :			int 						= ViTForImageClassification.from_pretrained("""google/vit-base-patch16-224"""  ).to(UpperCamelCase__  )

              lowerCamelCase__      :			Optional[int] 						= self.default_image_processor
              lowerCamelCase__      :			Any 						= prepare_img()
              lowerCamelCase__      :			str 						= image_processor(images=UpperCamelCase__    ,							return_tensors="""pt"""  ).to(UpperCamelCase__  )

              # forward pass
              with torch.no_grad():
                     lowerCamelCase__      :			int 						= model(**UpperCamelCase__  )

              # verify the logits
              lowerCamelCase__      :			Union[str, Any] 						= torch.Size((1, 1_000)  )
              self.assertEqual(outputs.logits.shape    ,							UpperCamelCase__  )

              lowerCamelCase__      :			List[Any] 						= torch.tensor([-0.2_744, 0.8_215, -0.0_836]  ).to(UpperCamelCase__  )

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




       @slow
       def 	lowerCamelCase_       (							self:       Union[str, Any]  ):
              # ViT models have an `interpolate_pos_encoding` argument in their forward method,
              # allowing to interpolate the pre-trained position embeddings in order to use
              # the model on higher resolutions. The DINO model by Facebook AI leverages this
              # to visualize self-attention on higher resolution images.
              lowerCamelCase__      :			Optional[Any] 						= ViTModel.from_pretrained("""facebook/dino-vits8"""  ).to(UpperCamelCase__  )

              lowerCamelCase__      :			int 						= ViTImageProcessor.from_pretrained("""facebook/dino-vits8"""    ,							size=480  )
              lowerCamelCase__      :			List[str] 						= prepare_img()
              lowerCamelCase__      :			List[Any] 						= image_processor(images=UpperCamelCase__    ,							return_tensors="""pt"""  )
              lowerCamelCase__      :			Optional[int] 						= inputs.pixel_values.to(UpperCamelCase__  )

              # forward pass
              with torch.no_grad():
                     lowerCamelCase__      :			int 						= model(UpperCamelCase__    ,							interpolate_pos_encoding=UpperCamelCase__  )

              # verify the logits
              lowerCamelCase__      :			Dict 						= torch.Size((1, 3_601, 384)  )
              self.assertEqual(outputs.last_hidden_state.shape    ,							UpperCamelCase__  )

              lowerCamelCase__      :			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(UpperCamelCase__  )

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




       @slow
       @require_accelerate
       @require_torch_gpu
       def 	lowerCamelCase_       (							self:       Dict  ):
              lowerCamelCase__      :			Dict 						= ViTModel.from_pretrained("""facebook/dino-vits8"""    ,							torch_dtype=torch.floataa    ,							device_map="""auto"""  )
              lowerCamelCase__      :			Tuple 						= self.default_image_processor

              lowerCamelCase__      :			str 						= prepare_img()
              lowerCamelCase__      :			Dict 						= 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__      :			str 						= model(UpperCamelCase__  )









'''simple docstring'''







from typing import TYPE_CHECKING

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


_A			:    Any					  ={
    '''configuration_trocr''': ['''TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TrOCRConfig'''],
    '''processing_trocr''': ['''TrOCRProcessor'''],
}


try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			_A			:    str					  =[
			    '''TROCR_PRETRAINED_MODEL_ARCHIVE_LIST''',
			    '''TrOCRForCausalLM''',
			    '''TrOCRPreTrainedModel''',
			]


if TYPE_CHECKING:
			from .configuration_trocr import TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP, TrOCRConfig
			from .processing_trocr import TrOCRProcessor

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_trocr import TROCR_PRETRAINED_MODEL_ARCHIVE_LIST, TrOCRForCausalLM, TrOCRPreTrainedModel

else:
			import sys

			_A			:    Dict					  =_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)

