





import shutil
import tempfile
import unittest

from transformers import SPIECE_UNDERLINE, BatchEncoding, MBartTokenizer, MBartTokenizerFast, is_torch_available
from transformers.testing_utils import (
    get_tests_dir,
    nested_simplify,
    require_sentencepiece,
    require_tokenizers,
    require_torch,
)

from ...test_tokenization_common import TokenizerTesterMixin


lowercase_					     =	get_tests_dir("""fixtures/test_sentencepiece.model""")


if is_torch_available():
   from transformers.models.mbart.modeling_mbart import shift_tokens_right

lowercase_					     =	250_004
lowercase_					     =	250_020
@require_sentencepiece
@require_tokenizers
class        SCREAMING_SNAKE_CASE     (__snake_case    ,			unittest.TestCase   ):
   _UpperCamelCase					:	Dict				      = MBartTokenizer
   _UpperCamelCase					:	Tuple				      = MBartTokenizerFast
   _UpperCamelCase					:	str				      = True
   _UpperCamelCase					:	List[Any]				      = True


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





     """simple docstring"""







     super().setUp()

     # We have a SentencePiece fixture for testing
     lowercase__			    =						MBartTokenizer(A_    ,  keep_accents=A_ )
     tokenizer.save_pretrained(self.tmpdirname )


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





     """simple docstring"""







     lowercase__			    =						MBartTokenizer(A_    ,  keep_accents=A_ )

     lowercase__			    =						tokenizer.tokenize('This is a test' )
     self.assertListEqual(A_    ,  ['▁This', '▁is', '▁a', '▁t', 'est'] )

     self.assertListEqual(
         tokenizer.convert_tokens_to_ids(A_ )    ,  [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]]    ,  )

     lowercase__			    =						tokenizer.tokenize('I was born in 92000, and this is falsé.' )
     self.assertListEqual(
         A_    ,  [
             SPIECE_UNDERLINE + 'I',
             SPIECE_UNDERLINE + 'was',
             SPIECE_UNDERLINE + 'b',
             'or',
             'n',
             SPIECE_UNDERLINE + 'in',
             SPIECE_UNDERLINE + '',
             '9',
             '2',
             '0',
             '0',
             '0',
             ',',
             SPIECE_UNDERLINE + 'and',
             SPIECE_UNDERLINE + 'this',
             SPIECE_UNDERLINE + 'is',
             SPIECE_UNDERLINE + 'f',
             'al',
             's',
             'é',
             '.',
         ]    ,  )
     lowercase__			    =						tokenizer.convert_tokens_to_ids(A_ )
     self.assertListEqual(
         A_    ,  [
             value + tokenizer.fairseq_offset
             for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4]
             #                                       ^ unk: 2 + 1 = 3                  unk: 2 + 1 = 3 ^
         ]    ,  )

     lowercase__			    =						tokenizer.convert_ids_to_tokens(A_ )
     self.assertListEqual(
         A_    ,  [
             SPIECE_UNDERLINE + 'I',
             SPIECE_UNDERLINE + 'was',
             SPIECE_UNDERLINE + 'b',
             'or',
             'n',
             SPIECE_UNDERLINE + 'in',
             SPIECE_UNDERLINE + '',
             '<unk>',
             '2',
             '0',
             '0',
             '0',
             ',',
             SPIECE_UNDERLINE + 'and',
             SPIECE_UNDERLINE + 'this',
             SPIECE_UNDERLINE + 'is',
             SPIECE_UNDERLINE + 'f',
             'al',
             's',
             '<unk>',
             '.',
         ]    ,  )


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





     """simple docstring"""







     if not self.test_slow_tokenizer:
       # as we don't have a slow version, we can't compare the outputs between slow and fast versions
       return

     lowercase__			    =						(self.rust_tokenizer_class, "hf-internal-testing/tiny-random-mbart", {})
     for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
       with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})""" ):
         lowercase__			    =						self.rust_tokenizer_class.from_pretrained(A_    ,  **A_ )
         lowercase__			    =						self.tokenizer_class.from_pretrained(A_    ,  **A_ )

         lowercase__			    =						tempfile.mkdtemp()

         lowercase__			    =						tokenizer_r.save_pretrained(A_ )
         lowercase__			    =						tokenizer_p.save_pretrained(A_ )

         # Checks it save with the same files + the tokenizer.json file for the fast one
         self.assertTrue(any('tokenizer.json' in f for f in tokenizer_r_files ) )
         lowercase__			    =						tuple(f for f in tokenizer_r_files if 'tokenizer.json' not in f )
         self.assertSequenceEqual(A_    ,  A_ )

         # Checks everything loads correctly in the same way
         lowercase__			    =						tokenizer_r.from_pretrained(A_ )
         lowercase__			    =						tokenizer_p.from_pretrained(A_ )

         # Check special tokens are set accordingly on Rust and Python
         for key in tokenizer_pp.special_tokens_map:
           self.assertTrue(hasattr(A_    ,  A_ ) )
           # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key))
           # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id"))

         shutil.rmtree(A_ )

         # Save tokenizer rust, legacy_format=True
         lowercase__			    =						tempfile.mkdtemp()

         lowercase__			    =						tokenizer_r.save_pretrained(A_    ,  legacy_format=A_ )
         lowercase__			    =						tokenizer_p.save_pretrained(A_ )

         # Checks it save with the same files
         self.assertSequenceEqual(A_    ,  A_ )

         # Checks everything loads correctly in the same way
         lowercase__			    =						tokenizer_r.from_pretrained(A_ )
         lowercase__			    =						tokenizer_p.from_pretrained(A_ )

         # Check special tokens are set accordingly on Rust and Python
         for key in tokenizer_pp.special_tokens_map:
           self.assertTrue(hasattr(A_    ,  A_ ) )

         shutil.rmtree(A_ )

         # Save tokenizer rust, legacy_format=False
         lowercase__			    =						tempfile.mkdtemp()

         lowercase__			    =						tokenizer_r.save_pretrained(A_    ,  legacy_format=A_ )
         lowercase__			    =						tokenizer_p.save_pretrained(A_ )

         # Checks it saved the tokenizer.json file
         self.assertTrue(any('tokenizer.json' in f for f in tokenizer_r_files ) )

         # Checks everything loads correctly in the same way
         lowercase__			    =						tokenizer_r.from_pretrained(A_ )
         lowercase__			    =						tokenizer_p.from_pretrained(A_ )

         # Check special tokens are set accordingly on Rust and Python
         for key in tokenizer_pp.special_tokens_map:
           self.assertTrue(hasattr(A_    ,  A_ ) )

         shutil.rmtree(A_ )






@require_torch
@require_sentencepiece
@require_tokenizers
class        SCREAMING_SNAKE_CASE     (unittest.TestCase   ):
   _UpperCamelCase					:	Union[str, Any]				      = 'facebook/mbart-large-en-ro'
   _UpperCamelCase					:	List[Any]				      = [
       ' UN Chief Says There Is No Military Solution in Syria',
       ' Secretary-General Ban Ki-moon says his response to Russia\'s stepped up military support for Syria is that "there is no military solution" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.',
   ]
   _UpperCamelCase					:	Dict				      = [
       'Şeful ONU declară că nu există o soluţie militară în Siria',
       'Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei'
       ' pentru Siria este că "nu există o soluţie militară" la conflictul de aproape cinci ani şi că noi arme nu vor'
       ' face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.',
   ]
   _UpperCamelCase					:	Optional[int]				      = [82_74, 12_78_73, 2_59_16, 7, 86_22, 20_71, 4_38, 6_74_85, 53, 18_78_95, 23, 5_17_12, 2, EN_CODE]


   @classmethod
   def  SCREAMING_SNAKE_CASE_							(					cls    :       Any )->     Union[str, Any]:





     """simple docstring"""







     lowercase__			    =						MBartTokenizer.from_pretrained(
         cls.checkpoint_name    ,  src_lang='en_XX'    ,  tgt_lang='ro_RO' )
     lowercase__			    =						1
     return cls


   def  SCREAMING_SNAKE_CASE_							(					self    :       Dict )->     Dict:





     """simple docstring"""







     self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['ar_AR']    ,  250_001 )
     self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['en_EN']    ,  250_004 )
     self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['ro_RO']    ,  250_020 )


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





     """simple docstring"""







     lowercase__			    =						self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0]
     self.assertListEqual(self.expected_src_tokens    ,  A_ )


   def  SCREAMING_SNAKE_CASE_							(					self    :       Any )->     int:





     """simple docstring"""







     self.assertIn(A_    ,  self.tokenizer.all_special_ids )
     lowercase__			    =						[RO_CODE, 884, 9_019, 96, 9, 916, 86_792, 36, 18_743, 15_596, 5, 2]
     lowercase__			    =						self.tokenizer.decode(A_    ,  skip_special_tokens=A_ )
     lowercase__			    =						self.tokenizer.decode(generated_ids[1:]    ,  skip_special_tokens=A_ )
     self.assertEqual(A_    ,  A_ )
     self.assertNotIn(self.tokenizer.eos_token    ,  A_ )


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





     """simple docstring"""







     lowercase__			    =						["this is gunna be a long sentence " * 20]
     assert isinstance(src_text[0]    ,  A_ )
     lowercase__			    =						10
     lowercase__			    =						self.tokenizer(A_    ,  max_length=A_    ,  truncation=A_ ).input_ids[0]
     self.assertEqual(ids[-2]    ,  2 )
     self.assertEqual(ids[-1]    ,  A_ )
     self.assertEqual(len(A_ )    ,  A_ )


   def  SCREAMING_SNAKE_CASE_							(					self    :       Optional[Any] )->     Dict:





     """simple docstring"""







     self.assertListEqual(self.tokenizer.convert_tokens_to_ids(['<mask>', 'ar_AR'] )    ,  [250_026, 250_001] )


   def  SCREAMING_SNAKE_CASE_							(					self    :       int )->     List[str]:





     """simple docstring"""







     lowercase__			    =						tempfile.mkdtemp()
     lowercase__			    =						self.tokenizer.fairseq_tokens_to_ids
     self.tokenizer.save_pretrained(A_ )
     lowercase__			    =						MBartTokenizer.from_pretrained(A_ )
     self.assertDictEqual(new_tok.fairseq_tokens_to_ids    ,  A_ )


   @require_torch
   def  SCREAMING_SNAKE_CASE_							(					self    :       List[Any] )->     List[Any]:





     """simple docstring"""







     lowercase__			    =						self.tokenizer(self.src_text    ,  text_target=self.tgt_text    ,  padding=A_    ,  return_tensors='pt' )
     lowercase__			    =						shift_tokens_right(batch['labels']    ,  self.tokenizer.pad_token_id )

     # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4
     assert batch.input_ids[1][-2:].tolist() == [2, EN_CODE]
     assert batch.decoder_input_ids[1][0].tolist() == RO_CODE
     assert batch.decoder_input_ids[1][-1] == 2
     assert batch.labels[1][-2:].tolist() == [2, RO_CODE]


   @require_torch
   def  SCREAMING_SNAKE_CASE_							(					self    :       List[Any] )->     Optional[int]:





     """simple docstring"""







     lowercase__			    =						self.tokenizer(
         self.src_text    ,  text_target=self.tgt_text    ,  padding=A_    ,  truncation=A_    ,  max_length=len(self.expected_src_tokens )    ,  return_tensors='pt'    ,  )

     lowercase__			    =						shift_tokens_right(batch['labels']    ,  self.tokenizer.pad_token_id )

     self.assertIsInstance(A_    ,  A_ )

     self.assertEqual((2, 14)    ,  batch.input_ids.shape )
     self.assertEqual((2, 14)    ,  batch.attention_mask.shape )
     lowercase__			    =						batch.input_ids.tolist()[0]
     self.assertListEqual(self.expected_src_tokens    ,  A_ )
     self.assertEqual(2    ,  batch.decoder_input_ids[0, -1] )  # EOS
     # Test that special tokens are reset
     self.assertEqual(self.tokenizer.prefix_tokens    ,  [] )
     self.assertEqual(self.tokenizer.suffix_tokens    ,  [self.tokenizer.eos_token_id, EN_CODE] )


   def  SCREAMING_SNAKE_CASE_							(					self    :       List[str] )->     Dict:





     """simple docstring"""







     lowercase__			    =						self.tokenizer(self.src_text    ,  padding=A_    ,  truncation=A_    ,  max_length=3    ,  return_tensors='pt' )
     lowercase__			    =						self.tokenizer(
         text_target=self.tgt_text    ,  padding=A_    ,  truncation=A_    ,  max_length=10    ,  return_tensors='pt' )
     lowercase__			    =						targets["input_ids"]
     lowercase__			    =						shift_tokens_right(A_    ,  self.tokenizer.pad_token_id )

     self.assertEqual(batch.input_ids.shape[1]    ,  3 )
     self.assertEqual(batch.decoder_input_ids.shape[1]    ,  10 )


   @require_torch
   def  SCREAMING_SNAKE_CASE_							(					self    :       List[str] )->     Any:





     """simple docstring"""







     lowercase__			    =						self.tokenizer._build_translation_inputs(
         'A test'    ,  return_tensors='pt'    ,  src_lang='en_XX'    ,  tgt_lang='ar_AR' )

     self.assertEqual(
         nested_simplify(A_ )    ,  {
             # A, test, EOS, en_XX
             'input_ids': [[62, 3_034, 2, 250_004]],
             'attention_mask': [[1, 1, 1, 1]],
             # ar_AR
             'forced_bos_token_id': 250_001,
         }    ,  )









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        SCREAMING_SNAKE_CASE     :


   def __init__(					self    :       List[Any]    ,  a    :       Any    ,  a    :       Optional[int]=13    ,  a    :       Tuple=30    ,  a    :       Union[str, Any]=2    ,  a    :       List[str]=3    ,  a    :       Dict=True    ,  a    :       List[str]=True    ,  a    :       List[Any]=32    ,  a    :       List[str]=5    ,  a    :       Optional[int]=4    ,  a    :       List[str]=37    ,  a    :       Dict="gelu"    ,  a    :       Dict=0.1    ,  a    :       List[str]=0.1    ,  a    :       int=10    ,  a    :       List[str]=0.02    ,  a    :       int=None    ,  a    :       List[str]=2    ,  )->     Dict:





     """simple docstring"""







     lowercase__			    =						parent
     lowercase__			    =						batch_size
     lowercase__			    =						image_size
     lowercase__			    =						patch_size
     lowercase__			    =						num_channels
     lowercase__			    =						is_training
     lowercase__			    =						use_labels
     lowercase__			    =						hidden_size
     lowercase__			    =						num_hidden_layers
     lowercase__			    =						num_attention_heads
     lowercase__			    =						intermediate_size
     lowercase__			    =						hidden_act
     lowercase__			    =						hidden_dropout_prob
     lowercase__			    =						attention_probs_dropout_prob
     lowercase__			    =						type_sequence_label_size
     lowercase__			    =						initializer_range
     lowercase__			    =						scope
     lowercase__			    =						encoder_stride

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


   def  SCREAMING_SNAKE_CASE_							(					self    :       Tuple )->     Any:





     """simple docstring"""







     lowercase__			    =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

     lowercase__			    =						self.get_config()

     return config, pixel_values, labels


   def  SCREAMING_SNAKE_CASE_							(					self    :       Tuple )->     List[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=a    ,  initializer_range=self.initializer_range    ,  encoder_stride=self.encoder_stride    ,  )


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





     """simple docstring"""







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


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





     """simple docstring"""







     lowercase__			    =						ViTForMaskedImageModeling(config=a )
     model.to(a )
     model.eval()
     lowercase__			    =						model(a )
     self.parent.assertEqual(
         result.reconstruction.shape    ,  (self.batch_size, self.num_channels, self.image_size, self.image_size) )

     # test greyscale images
     lowercase__			    =						1
     lowercase__			    =						ViTForMaskedImageModeling(a )
     model.to(a )
     model.eval()

     lowercase__			    =						floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
     lowercase__			    =						model(a )
     self.parent.assertEqual(result.reconstruction.shape    ,  (self.batch_size, 1, self.image_size, self.image_size) )


   def  SCREAMING_SNAKE_CASE_							(					self    :       List[str]    ,  a    :       List[str]    ,  a    :       int    ,  a    :       List[Any] )->     str:





     """simple docstring"""







     lowercase__			    =						self.type_sequence_label_size
     lowercase__			    =						ViTForImageClassification(a )
     model.to(a )
     model.eval()
     lowercase__			    =						model(a    ,  labels=a )
     self.parent.assertEqual(result.logits.shape    ,  (self.batch_size, self.type_sequence_label_size) )

     # test greyscale images
     lowercase__			    =						1
     lowercase__			    =						ViTForImageClassification(a )
     model.to(a )
     model.eval()

     lowercase__			    =						floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
     lowercase__			    =						model(a )
     self.parent.assertEqual(result.logits.shape    ,  (self.batch_size, self.type_sequence_label_size) )


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





     """simple docstring"""







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






@require_torch
class        SCREAMING_SNAKE_CASE     (UpperCAmelCase    ,			UpperCAmelCase    ,			unittest.TestCase   ):
   _UpperCamelCase					:	Any				      = (
       (
           ViTModel,
           ViTForImageClassification,
           ViTForMaskedImageModeling,
       )
       if is_torch_available()
       else ()
   )
   _UpperCamelCase					:	Union[str, Any]				      = (
       {'feature-extraction': ViTModel, 'image-classification': ViTForImageClassification}
       if is_torch_available()
       else {}
   )
   _UpperCamelCase					:	int				      = True

   _UpperCamelCase					:	int				      = False
   _UpperCamelCase					:	Union[str, Any]				      = False
   _UpperCamelCase					:	Dict				      = False


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





     """simple docstring"""







     lowercase__			    =						ViTModelTester(self )
     lowercase__			    =						ConfigTester(self    ,  config_class=a    ,  has_text_modality=a    ,  hidden_size=37 )


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





     """simple docstring"""







     self.config_tester.run_common_tests()


   @unittest.skip(reason='ViT does not use inputs_embeds' )
   def  SCREAMING_SNAKE_CASE_							(					self    :       Tuple )->     Optional[Any]:





     """simple docstring"""







     pass


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





     """simple docstring"""







     lowercase__    ,					lowercase__			    =						self.model_tester.prepare_config_and_inputs_for_common()

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


   def  SCREAMING_SNAKE_CASE_							(					self    :       List[str] )->     Optional[int]:





     """simple docstring"""







     lowercase__    ,					lowercase__			    =						self.model_tester.prepare_config_and_inputs_for_common()

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

       lowercase__			    =						['pixel_values']
       self.assertListEqual(arg_names[:1]    ,  a )


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





     """simple docstring"""







     lowercase__			    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*a )


   def  SCREAMING_SNAKE_CASE_							(					self    :       Tuple )->     Tuple:





     """simple docstring"""







     lowercase__			    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_image_modeling(*a )


   def  SCREAMING_SNAKE_CASE_							(					self    :       Tuple )->     int:





     """simple docstring"""







     lowercase__			    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*a )


   @slow
   def  SCREAMING_SNAKE_CASE_							(					self    :       Dict )->     List[Any]:





     """simple docstring"""







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






def     __UpperCamelCase						()       ->     str:
  lowercase__			    =						Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'	)
  return image


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


   @cached_property
   def  SCREAMING_SNAKE_CASE_							(					self    :       List[Any] )->     List[Any]:





     """simple docstring"""







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


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





     """simple docstring"""







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

     lowercase__			    =						self.default_image_processor
     lowercase__			    =						prepare_img()
     lowercase__			    =						image_processor(images=a    ,  return_tensors='pt' ).to(a )

     # forward pass
     with torch.no_grad():
       lowercase__			    =						model(**a )

     # verify the logits
     lowercase__			    =						torch.Size((1, 1_000) )
     self.assertEqual(outputs.logits.shape    ,  a )

     lowercase__			    =						torch.tensor([-0.2744, 0.8215, -0.0836] ).to(a )

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


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





     """simple docstring"""







     lowercase__			    =						ViTModel.from_pretrained('facebook/dino-vits8' ).to(a )

     lowercase__			    =						ViTImageProcessor.from_pretrained('facebook/dino-vits8'    ,  size=480 )
     lowercase__			    =						prepare_img()
     lowercase__			    =						image_processor(images=a    ,  return_tensors='pt' )
     lowercase__			    =						inputs.pixel_values.to(a )

     # forward pass
     with torch.no_grad():
       lowercase__			    =						model(a    ,  interpolate_pos_encoding=a )

     # verify the logits
     lowercase__			    =						torch.Size((1, 3_601, 384) )
     self.assertEqual(outputs.last_hidden_state.shape    ,  a )

     lowercase__			    =						torch.tensor(
         [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]] ).to(a )

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


   @slow
   @require_accelerate
   @require_torch_gpu
   def  SCREAMING_SNAKE_CASE_							(					self    :       str )->     str:





     """simple docstring"""







     lowercase__			    =						ViTModel.from_pretrained('facebook/dino-vits8'    ,  torch_dtype=torch.floataa    ,  device_map='auto' )
     lowercase__			    =						self.default_image_processor

     lowercase__			    =						prepare_img()
     lowercase__			    =						image_processor(images=a    ,  return_tensors='pt' )
     lowercase__			    =						inputs.pixel_values.to(a )

     # forward pass to make sure inference works in fp16
     with torch.no_grad():
       lowercase__			    =						model(a )


