






'''simple docstring'''





from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
							import numpy as np
							import tensorflow as tf

							from transformers import (
							    TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
							    FlaubertConfig,
							    TFFlaubertForMultipleChoice,
							    TFFlaubertForQuestionAnsweringSimple,
							    TFFlaubertForSequenceClassification,
							    TFFlaubertForTokenClassification,
							    TFFlaubertModel,
							    TFFlaubertWithLMHeadModel,
							)





class 		SCREAMING_SNAKE_CASE__ :



		def __init__(     self:     Optional[Any]					, a:     Any					, )	->Optional[Any]:






								'''simple docstring'''


								a_					  = parent
								a_					  = 13
								a_					  = 7
								a_					  = True
								a_					  = True
								a_					  = True
								a_					  = True
								a_					  = True
								a_					  = False
								a_					  = False
								a_					  = False
								a_					  = 2
								a_					  = 99
								a_					  = 0
								a_					  = 32
								a_					  = 2
								a_					  = 4
								a_					  = 0.1
								a_					  = 0.1
								a_					  = 5_12
								a_					  = 16
								a_					  = 2
								a_					  = 0.02
								a_					  = 3
								a_					  = 4
								a_					  = '''last'''
								a_					  = True
								a_					  = None
								a_					  = 0



		def _lowerCAmelCase  (     self:     Optional[Any])	->List[Any]:






								'''simple docstring'''


								a_					  = ids_tensor([self.batch_size, self.seq_length]					, self.vocab_size)
								a_					  = random_attention_mask([self.batch_size, self.seq_length]					, dtype=tf.floataa)

								a_					  = None
								if self.use_input_lengths:
														a_					  = (
														    ids_tensor([self.batch_size]					, vocab_size=2) + self.seq_length - 2
														)  # small variation of seq_length

								a_					  = None
								if self.use_token_type_ids:
														a_					  = ids_tensor([self.batch_size, self.seq_length]					, self.n_langs)

								a_					  = None
								a_					  = None
								a_					  = None
								if self.use_labels:
														a_					  = ids_tensor([self.batch_size]					, self.type_sequence_label_size)
														a_					  = ids_tensor([self.batch_size, self.seq_length]					, self.num_labels)
														a_					  = ids_tensor([self.batch_size]					, 2					, dtype=tf.floataa)
														a_					  = ids_tensor([self.batch_size]					, self.num_choices)

								a_					  = FlaubertConfig(
								    vocab_size=self.vocab_size					, n_special=self.n_special					, emb_dim=self.hidden_size					, n_layers=self.num_hidden_layers					, n_heads=self.num_attention_heads					, dropout=self.hidden_dropout_prob					, attention_dropout=self.attention_probs_dropout_prob					, gelu_activation=self.gelu_activation					, sinusoidal_embeddings=self.sinusoidal_embeddings					, asm=self.asm					, causal=self.causal					, n_langs=self.n_langs					, max_position_embeddings=self.max_position_embeddings					, initializer_range=self.initializer_range					, summary_type=self.summary_type					, use_proj=self.use_proj					, bos_token_id=self.bos_token_id					, )

								return (
								    config,
								    input_ids,
								    token_type_ids,
								    input_lengths,
								    sequence_labels,
								    token_labels,
								    is_impossible_labels,
								    choice_labels,
								    input_mask,
								)



		def _lowerCAmelCase  (     self:     Any					, a:     List[str]					, a:     Any					, a:     Optional[int]					, a:     List[str]					, a:     Tuple					, a:     str					, a:     List[Any]					, a:     Optional[int]					, a:     str					, )	->List[str]:






								'''simple docstring'''


								a_					  = TFFlaubertModel(config=a)
								a_					  = {'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
								a_					  = model(a)

								a_					  = [input_ids, input_mask]
								a_					  = model(a)
								self.parent.assertEqual(result.last_hidden_state.shape					, (self.batch_size, self.seq_length, self.hidden_size))



		def _lowerCAmelCase  (     self:     Dict					, a:     Optional[int]					, a:     List[str]					, a:     Dict					, a:     List[Any]					, a:     Dict					, a:     int					, a:     Union[str, Any]					, a:     Any					, a:     Union[str, Any]					, )	->List[str]:






								'''simple docstring'''


								a_					  = TFFlaubertWithLMHeadModel(a)

								a_					  = {'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
								a_					  = model(a)

								self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.seq_length, self.vocab_size))



		def _lowerCAmelCase  (     self:     List[str]					, a:     int					, a:     Tuple					, a:     List[Any]					, a:     Optional[int]					, a:     List[Any]					, a:     Dict					, a:     Any					, a:     Any					, a:     Any					, )	->Optional[Any]:






								'''simple docstring'''


								a_					  = TFFlaubertForQuestionAnsweringSimple(a)

								a_					  = {'''input_ids''': input_ids, '''lengths''': input_lengths}

								a_					  = model(a)

								self.parent.assertEqual(result.start_logits.shape					, (self.batch_size, self.seq_length))
								self.parent.assertEqual(result.end_logits.shape					, (self.batch_size, self.seq_length))



		def _lowerCAmelCase  (     self:     Optional[Any]					, a:     Union[str, Any]					, a:     int					, a:     List[Any]					, a:     Optional[Any]					, a:     Optional[Any]					, a:     int					, a:     Tuple					, a:     List[str]					, a:     Optional[Any]					, )	->Any:






								'''simple docstring'''


								a_					  = TFFlaubertForSequenceClassification(a)

								a_					  = {'''input_ids''': input_ids, '''lengths''': input_lengths}

								a_					  = model(a)

								self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.type_sequence_label_size))



		def _lowerCAmelCase  (     self:     Union[str, Any]					, a:     Optional[int]					, a:     List[str]					, a:     List[Any]					, a:     List[Any]					, a:     str					, a:     Optional[int]					, a:     Any					, a:     List[str]					, a:     Tuple					, )	->str:






								'''simple docstring'''


								a_					  = self.num_labels
								a_					  = TFFlaubertForTokenClassification(config=a)
								a_					  = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
								a_					  = model(a)
								self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.seq_length, self.num_labels))



		def _lowerCAmelCase  (     self:     Optional[int]					, a:     Optional[int]					, a:     Optional[Any]					, a:     Optional[int]					, a:     int					, a:     Tuple					, a:     List[Any]					, a:     Dict					, a:     Dict					, a:     List[Any]					, )	->Any:






								'''simple docstring'''


								a_					  = self.num_choices
								a_					  = TFFlaubertForMultipleChoice(config=a)
								a_					  = tf.tile(tf.expand_dims(a					, 1)					, (1, self.num_choices, 1))
								a_					  = tf.tile(tf.expand_dims(a					, 1)					, (1, self.num_choices, 1))
								a_					  = tf.tile(tf.expand_dims(a					, 1)					, (1, self.num_choices, 1))
								a_					  = {
								    '''input_ids''': multiple_choice_inputs_ids,
								    '''attention_mask''': multiple_choice_input_mask,
								    '''token_type_ids''': multiple_choice_token_type_ids,
								}
								a_					  = model(a)
								self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.num_choices))




		def _lowerCAmelCase  (     self:     int)	->Any:






								'''simple docstring'''


								a_					  = self.prepare_config_and_inputs()
								(
								    a_    
								)					  = config_and_inputs
								a_					  = {
								    '''input_ids''': input_ids,
								    '''token_type_ids''': token_type_ids,
								    '''langs''': token_type_ids,
								    '''lengths''': input_lengths,
								}
								return config, inputs_dict





@require_tf
class 		SCREAMING_SNAKE_CASE__ (  __snake_case		,		__snake_case		,		unittest.TestCase     ):
		_UpperCAmelCase								=(
		    (
		        TFFlaubertModel,
		        TFFlaubertWithLMHeadModel,
		        TFFlaubertForSequenceClassification,
		        TFFlaubertForQuestionAnsweringSimple,
		        TFFlaubertForTokenClassification,
		        TFFlaubertForMultipleChoice,
		    )
		    if is_tf_available()
		    else ()
		)
		_UpperCAmelCase								=(
		    (TFFlaubertWithLMHeadModel,) if is_tf_available() else ()
		)  # TODO (PVP): Check other models whether language generation is also applicable
		_UpperCAmelCase								=(
		    {
		        '''feature-extraction''': TFFlaubertModel,
		        '''fill-mask''': TFFlaubertWithLMHeadModel,
		        '''question-answering''': TFFlaubertForQuestionAnsweringSimple,
		        '''text-classification''': TFFlaubertForSequenceClassification,
		        '''token-classification''': TFFlaubertForTokenClassification,
		        '''zero-shot''': TFFlaubertForSequenceClassification,
		    }
		    if is_tf_available()
		    else {}
		)
		_UpperCAmelCase								=False
		_UpperCAmelCase								=False



		def _lowerCAmelCase  (     self:     List[str]					, a:     Optional[int]					, a:     str					, a:     Union[str, Any]					, a:     int					, a:     List[str])	->List[Any]:






								'''simple docstring'''


								if (
								    pipeline_test_casse_name == "QAPipelineTests"
								    and tokenizer_name is not None
								    and not tokenizer_name.endswith("Fast")
								):
														# `QAPipelineTests` fails for a few models when the slower tokenizer are used.
														# (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
														# TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
														return True

								return False



		def _lowerCAmelCase  (     self:     Optional[Any])	->Any:






								'''simple docstring'''


								a_					  = TFFlaubertModelTester(self)
								a_					  = ConfigTester(self					, config_class=a					, emb_dim=37)



		def _lowerCAmelCase  (     self:     Optional[int])	->Optional[Any]:






								'''simple docstring'''


								self.config_tester.run_common_tests()



		def _lowerCAmelCase  (     self:     int)	->Optional[int]:






								'''simple docstring'''


								a_					  = self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_model(*a)



		def _lowerCAmelCase  (     self:     Union[str, Any])	->Dict:






								'''simple docstring'''


								a_					  = self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_lm_head(*a)



		def _lowerCAmelCase  (     self:     str)	->List[Any]:






								'''simple docstring'''


								a_					  = self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_qa(*a)



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






								'''simple docstring'''


								a_					  = self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_sequence_classif(*a)



		def _lowerCAmelCase  (     self:     int)	->Any:






								'''simple docstring'''


								a_					  = self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_for_token_classification(*a)



		def _lowerCAmelCase  (     self:     int)	->str:






								'''simple docstring'''


								a_					  = self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_for_multiple_choice(*a)




		@slow
		def _lowerCAmelCase  (     self:     Optional[Any])	->Union[str, Any]:






								'''simple docstring'''


								for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														a_					  = TFFlaubertModel.from_pretrained(a)
														self.assertIsNotNone(a)





@require_tf
@require_sentencepiece
@require_tokenizers
class 		SCREAMING_SNAKE_CASE__ (  unittest.TestCase     ):



		@slow
		def _lowerCAmelCase  (     self:     List[str])	->int:






								'''simple docstring'''


								a_					  = TFFlaubertModel.from_pretrained("jplu/tf-flaubert-small-cased")

								a_					  = tf.convert_to_tensor(
								    [[0, 1_58, 7_35, 25_92, 14_24, 67_27, 82, 1]]					, dtype=tf.intaa					, )  # "J'aime flaubert !"

								a_					  = model(a)[0]
								a_					  = tf.TensorShape((1, 8, 5_12))
								self.assertEqual(output.shape					, a)
								# compare the actual values for a slice.
								a_					  = tf.convert_to_tensor(
								    [
								        [
								            [-1.876_8773, -1.56_6555, 0.2707_2418],
								            [-1.692_0038, -0.587_3505, 1.932_9599],
								            [-2.956_3985, -1.699_3835, 1.797_2052],
								        ]
								    ]					, dtype=tf.floataa					, )

								self.assertTrue(np.allclose(output[:, :3, :3].numpy()					, expected_slice.numpy()					, atol=1e-4))





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





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






         """simple docstring"""






         A			:  Union[str, Any]   =		parent
         A			:  Optional[Any]   =		batch_size
         A			:  Tuple   =		image_size
         A			:  str   =		patch_size
         A			:  Dict   =		num_channels
         A			:  Tuple   =		is_training
         A			:  List[Any]   =		use_labels
         A			:  Optional[Any]   =		hidden_size
         A			:  Dict   =		num_hidden_layers
         A			:  Union[str, Any]   =		num_attention_heads
         A			:  List[Any]   =		intermediate_size
         A			:  Any   =		hidden_act
         A			:  Dict   =		hidden_dropout_prob
         A			:  Any   =		attention_probs_dropout_prob
         A			:  Dict   =		type_sequence_label_size
         A			:  List[Any]   =		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			:  Optional[Any]   =		num_patches + 1





     def 							__lowerCAmelCase				(				self      )					->							Optional[int]:






         """simple docstring"""






         A			:  List[str]   =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

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

         A			:  Dict   =		self.get_config()

         return config, pixel_values, labels





     def 							__lowerCAmelCase				(				self      )					->							Any:






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





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )					->							int:






         """simple docstring"""






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





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )					->							Any:






         """simple docstring"""






         A			:  Any   =		ViTForMaskedImageModeling(config=SCREAMING_SNAKE_CASE      )
         model.to(SCREAMING_SNAKE_CASE      )
         model.eval()
         A			:  Any   =		model(SCREAMING_SNAKE_CASE      )
         self.parent.assertEqual(
             result.reconstruction.shape		,     (self.batch_size, self.num_channels, self.image_size, self.image_size)      )

         # test greyscale images
         A			:  Optional[Any]   =		1
         A			:  str   =		ViTForMaskedImageModeling(SCREAMING_SNAKE_CASE      )
         model.to(SCREAMING_SNAKE_CASE      )
         model.eval()

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





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )					->							int:






         """simple docstring"""






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

         # test greyscale images
         A			:  int   =		1
         A			:  Any   =		ViTForImageClassification(SCREAMING_SNAKE_CASE      )
         model.to(SCREAMING_SNAKE_CASE      )
         model.eval()

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





     def 							__lowerCAmelCase				(				self      )					->							Optional[int]:






         """simple docstring"""






         A			:  int   =		self.prepare_config_and_inputs()
         (
             (
             A
         ),    (
             A
         ),    (
             A
         ),    
         )			:  int   =		config_and_inputs
         A			:  Dict   =		{'''pixel_values''': pixel_values}
         return config, inputs_dict





@require_torch
class 					A							(				__snake_case       ,     __snake_case       ,     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      )					->							Tuple:






         """simple docstring"""






         A			:  List[str]   =		ViTModelTester(self      )
         A			:  List[str]   =		ConfigTester(self		,     config_class=SCREAMING_SNAKE_CASE		,     has_text_modality=SCREAMING_SNAKE_CASE		,     hidden_size=37      )





     def 							__lowerCAmelCase				(				self      )					->							Optional[Any]:






         """simple docstring"""






         self.config_tester.run_common_tests()





     @unittest.skip(reason='''ViT does not use inputs_embeds'''      )
     def 							__lowerCAmelCase				(				self      )					->							Dict:






         """simple docstring"""






         pass





     def 							__lowerCAmelCase				(				self      )					->							int:






         """simple docstring"""






         A,    A			:  List[Any]   =		self.model_tester.prepare_config_and_inputs_for_common()

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





     def 							__lowerCAmelCase				(				self      )					->							Optional[int]:






         """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			:  Tuple   =		model_class(SCREAMING_SNAKE_CASE      )
             A			:  str   =		inspect.signature(model.forward      )
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             A			:  List[str]   =		[*signature.parameters.keys()]

             A			:  Optional[int]   =		['''pixel_values''']
             self.assertListEqual(arg_names[:1]		,     SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							Optional[int]:






         """simple docstring"""






         A			:  Optional[Any]   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							Optional[int]:






         """simple docstring"""






         A			:  Union[str, Any]   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_image_modeling(*SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							Any:






         """simple docstring"""






         A			:  Optional[Any]   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE      )





     @slow
     def 							__lowerCAmelCase				(				self      )					->							List[str]:






         """simple docstring"""






         for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             A			:  Optional[Any]   =		ViTModel.from_pretrained(SCREAMING_SNAKE_CASE      )
             self.assertIsNotNone(SCREAMING_SNAKE_CASE      )







def 						lowerCAmelCase_							(  ):




    '''simple docstring'''



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







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





     @cached_property
     def 							__lowerCAmelCase				(				self      )					->							Union[str, Any]:






         """simple docstring"""






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





     @slow
     def 							__lowerCAmelCase				(				self      )					->							Any:






         """simple docstring"""






         A			:  List[Any]   =		ViTForImageClassification.from_pretrained('''google/vit-base-patch16-224'''      ).to(SCREAMING_SNAKE_CASE      )

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

         # forward pass
         with torch.no_grad():
             A			:  Any   =		model(**SCREAMING_SNAKE_CASE      )

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

         A			:  List[str]   =		torch.tensor([-0.2_744, 0.8_215, -0.0_836]      ).to(SCREAMING_SNAKE_CASE      )

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





     @slow
     def 							__lowerCAmelCase				(				self      )					->							Union[str, Any]:






         """simple docstring"""






         A			:  Dict   =		ViTModel.from_pretrained('''facebook/dino-vits8'''      ).to(SCREAMING_SNAKE_CASE      )

         A			:  int   =		ViTImageProcessor.from_pretrained('''facebook/dino-vits8'''		,     size=480      )
         A			:  Tuple   =		prepare_img()
         A			:  List[Any]   =		image_processor(images=SCREAMING_SNAKE_CASE		,     return_tensors='''pt'''      )
         A			:  Tuple   =		inputs.pixel_values.to(SCREAMING_SNAKE_CASE      )

         # forward pass
         with torch.no_grad():
             A			:  Any   =		model(SCREAMING_SNAKE_CASE		,     interpolate_pos_encoding=SCREAMING_SNAKE_CASE      )

         # verify the logits
         A			:  int   =		torch.Size((1, 3601, 384)      )
         self.assertEqual(outputs.last_hidden_state.shape		,     SCREAMING_SNAKE_CASE      )

         A			:  Union[str, Any]   =		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(SCREAMING_SNAKE_CASE      )

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





     @slow
     @require_accelerate
     @require_torch_gpu
     def 							__lowerCAmelCase				(				self      )					->							Union[str, Any]:






         """simple docstring"""






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

         A			:  List[str]   =		prepare_img()
         A			:  Union[str, Any]   =		image_processor(images=SCREAMING_SNAKE_CASE		,     return_tensors='''pt'''      )
         A			:  List[Any]   =		inputs.pixel_values.to(SCREAMING_SNAKE_CASE      )

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




