


'''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			:Any     ,						SCREAMING_SNAKE_CASE			:Tuple     ,						SCREAMING_SNAKE_CASE			:int=1_3     ,						SCREAMING_SNAKE_CASE			:Optional[Any]=3_0     ,						SCREAMING_SNAKE_CASE			:str=2     ,						SCREAMING_SNAKE_CASE			:str=3     ,						SCREAMING_SNAKE_CASE			:List[str]=True     ,						SCREAMING_SNAKE_CASE			:List[str]=True     ,						SCREAMING_SNAKE_CASE			:Optional[Any]=3_2     ,						SCREAMING_SNAKE_CASE			:Dict=5     ,						SCREAMING_SNAKE_CASE			:Union[str, Any]=4     ,						SCREAMING_SNAKE_CASE			:Tuple=3_7     ,						SCREAMING_SNAKE_CASE			:int="gelu"     ,						SCREAMING_SNAKE_CASE			:Optional[int]=0.1     ,						SCREAMING_SNAKE_CASE			:Tuple=0.1     ,						SCREAMING_SNAKE_CASE			:Union[str, Any]=1_0     ,						SCREAMING_SNAKE_CASE			:List[Any]=0.02     ,						SCREAMING_SNAKE_CASE			:Optional[Any]=None     ,						SCREAMING_SNAKE_CASE			:Optional[Any]=2     ,						)       ->					List[Any]:




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

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






      def __UpperCAmelCase       ( self			:Dict	)       ->					Any:




             '''simple docstring'''
             _a    : Union[str, Any]     					=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

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

             _a    : str     					=self.get_config()

             return config, pixel_values, labels






      def __UpperCAmelCase       ( self			:str	)       ->					str:




             '''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 __UpperCAmelCase       ( self			:List[Any]     ,						SCREAMING_SNAKE_CASE			:List[Any]     ,						SCREAMING_SNAKE_CASE			:List[str]     ,						SCREAMING_SNAKE_CASE			:List[Any]	)       ->					str:




             '''simple docstring'''
             _a    : Tuple     					=ViTModel(config=_a	)
             model.to(_a	)
             model.eval()
             _a    : str     					=model(_a	)
             self.parent.assertEqual(result.last_hidden_state.shape     ,						(self.batch_size, self.seq_length, self.hidden_size)	)






      def __UpperCAmelCase       ( self			:int     ,						SCREAMING_SNAKE_CASE			:Dict     ,						SCREAMING_SNAKE_CASE			:Union[str, Any]     ,						SCREAMING_SNAKE_CASE			:int	)       ->					Any:




             '''simple docstring'''
             _a    : Union[str, Any]     					=ViTForMaskedImageModeling(config=_a	)
             model.to(_a	)
             model.eval()
             _a    : int     					=model(_a	)
             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    : Tuple     					=ViTForMaskedImageModeling(_a	)
             model.to(_a	)
             model.eval()

             _a    : int     					=floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
             _a    : List[Any]     					=model(_a	)
             self.parent.assertEqual(result.reconstruction.shape     ,						(self.batch_size, 1, self.image_size, self.image_size)	)






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




             '''simple docstring'''
             _a    : Tuple     					=self.type_sequence_label_size
             _a    : Dict     					=ViTForImageClassification(_a	)
             model.to(_a	)
             model.eval()
             _a    : List[str]     					=model(_a     ,						labels=_a	)
             self.parent.assertEqual(result.logits.shape     ,						(self.batch_size, self.type_sequence_label_size)	)

             # test greyscale images
             _a    : str     					=1
             _a    : Tuple     					=ViTForImageClassification(_a	)
             model.to(_a	)
             model.eval()

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






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




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







@require_torch
class A__       (						__lowercase ,     __lowercase ,     unittest.TestCase      ):
      __UpperCamelCase       :  Union[str, Any]	       =							(
          (
              ViTModel,
              ViTForImageClassification,
              ViTForMaskedImageModeling,
          )
          if is_torch_available()
          else ()
      )
      __UpperCamelCase       :  str	       =							(
          {"feature-extraction": ViTModel, "image-classification": ViTForImageClassification}
          if is_torch_available()
          else {}
      )
      __UpperCamelCase       :  Dict	       =							True

      __UpperCamelCase       :  Union[str, Any]	       =							False
      __UpperCamelCase       :  Optional[int]	       =							False
      __UpperCamelCase       :  Dict	       =							False






      def __UpperCAmelCase       ( self			:Tuple	)       ->					Optional[Any]:




             '''simple docstring'''
             _a    : List[str]     					=ViTModelTester(self	)
             _a    : List[Any]     					=ConfigTester(self     ,						config_class=_a     ,						has_text_modality=_a     ,						hidden_size=3_7	)






      def __UpperCAmelCase       ( self			:Optional[int]	)       ->					Dict:




             '''simple docstring'''
             self.config_tester.run_common_tests()






      @unittest.skip(reason="""ViT does not use inputs_embeds"""	)
      def __UpperCAmelCase       ( self			:Optional[Any]	)       ->					Union[str, Any]:




             '''simple docstring'''
             pass






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




             '''simple docstring'''
             _a    : List[Any]     					=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    : Tuple     					=model.get_output_embeddings()
                    self.assertTrue(x is None or isinstance(_a     ,						nn.Linear	)	)






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




             '''simple docstring'''
             _a    : Any     					=self.model_tester.prepare_config_and_inputs_for_common()

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

                    _a    : str     					=['''pixel_values''']
                    self.assertListEqual(arg_names[:1]     ,						_a	)






      def __UpperCAmelCase       ( self			:List[Any]	)       ->					Tuple:




             '''simple docstring'''
             _a    : str     					=self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*_a	)






      def __UpperCAmelCase       ( self			:int	)       ->					Dict:




             '''simple docstring'''
             _a    : List[str]     					=self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_masked_image_modeling(*_a	)






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




             '''simple docstring'''
             _a    : Optional[int]     					=self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_image_classification(*_a	)






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




             '''simple docstring'''
             for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    _a    : Any     					=ViTModel.from_pretrained(_a	)
                    self.assertIsNotNone(_a	)


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







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






      @cached_property
      def __UpperCAmelCase       ( self			:str	)       ->					List[str]:




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






      @slow
      def __UpperCAmelCase       ( self			:Tuple	)       ->					List[Any]:




             '''simple docstring'''
             _a    : List[Any]     					=ViTForImageClassification.from_pretrained("""google/vit-base-patch16-224"""	).to(_a	)

             _a    : int     					=self.default_image_processor
             _a    : Any     					=prepare_img()
             _a    : str     					=image_processor(images=_a     ,						return_tensors="""pt"""	).to(_a	)

             # forward pass
             with torch.no_grad():
                    _a    : Dict     					=model(**_a	)

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

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

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






      @slow
      def __UpperCAmelCase       ( self			:Optional[int]	)       ->					Any:




             '''simple docstring'''
             # 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.
             _a    : Dict     					=ViTModel.from_pretrained("""facebook/dino-vits8"""	).to(_a	)

             _a    : List[Any]     					=ViTImageProcessor.from_pretrained("""facebook/dino-vits8"""     ,						size=4_8_0	)
             _a    : Dict     					=prepare_img()
             _a    : str     					=image_processor(images=_a     ,						return_tensors="""pt"""	)
             _a    : str     					=inputs.pixel_values.to(_a	)

             # forward pass
             with torch.no_grad():
                    _a    : List[Any]     					=model(_a     ,						interpolate_pos_encoding=_a	)

             # verify the logits
             _a    : Tuple     					=torch.Size((1, 3_6_0_1, 3_8_4)	)
             self.assertEqual(outputs.last_hidden_state.shape     ,						_a	)

             _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(_a	)

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






      @slow
      @require_accelerate
      @require_torch_gpu
      def __UpperCAmelCase       ( self			:Optional[Any]	)       ->					Optional[int]:




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

             _a    : Any     					=prepare_img()
             _a    : Union[str, Any]     					=image_processor(images=_a     ,						return_tensors="""pt"""	)
             _a    : Optional[Any]     					=inputs.pixel_values.to(_a	)

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





import os
import time
import warnings
from dataclasses import dataclass, field
from enum import Enum
from typing import List, Optional, Union

import torch
from filelock import FileLock
from torch.utils.data import Dataset

from ...tokenization_utils_base import PreTrainedTokenizerBase
from ...utils import logging
from ..processors.glue import glue_convert_examples_to_features, glue_output_modes, glue_processors
from ..processors.utils import InputFeatures


a__				=		logging.get_logger(__name__)

@dataclass
class 				UpperCAmelCase_		:
							"""simple docstring"""







							UpperCAmelCase__       :						str	    =    field(metadata={"help": "The name of the task to train on: " + ", ".join(glue_processors.keys()   )}   )
							UpperCAmelCase__       :						str	    =    field(
							    metadata={"help": "The input data dir. Should contain the .tsv files (or other data files) for the task."}   )
							UpperCAmelCase__       :						int	    =    field(
							    default=128				,       metadata={
							        "help": (
							            "The maximum total input sequence length after tokenization. Sequences longer "
							            "than this will be truncated, sequences shorter will be padded."
							        )
							    }				,       )
							UpperCAmelCase__       :						bool	    =    field(
							    default=__lowercase				,       metadata={"help": "Overwrite the cached training and evaluation sets"}   )


							def 				__lowercase				(			self       )    ->    List[Any]:
												_a	:    List[Any]         =		self.task_name.lower()



class 				UpperCAmelCase_		(			__lowercase   ):
							"""simple docstring"""







							UpperCAmelCase__       :						List[str]	    =    "train"
							UpperCAmelCase__       :						List[str]	    =    "dev"
							UpperCAmelCase__       :						Union[str, Any]	    =    "test"



class 				UpperCAmelCase_		(			__lowercase   ):
							"""simple docstring"""







							UpperCAmelCase__       :						GlueDataTrainingArguments
							UpperCAmelCase__       :						str
							UpperCAmelCase__       :						List[InputFeatures]


							def __init__(			self       ,							_a       ,							_a       ,							_a = None       ,							_a = Split.train       ,							_a = None       ,							)    ->    str:
												warnings.warn(
												    '''This dataset will be removed from the library soon, preprocessing should be handled with the 🤗 Datasets '''
												    '''library. You can have a look at this example script for pointers: '''
												    '''https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py'''       ,							_a       ,							)
												_a	:    List[str]         =		args
												_a	:    List[str]         =		glue_processors[args.task_name]()
												_a	:    List[Any]         =		glue_output_modes[args.task_name]
												if isinstance(_a       ,							_a       ):
																	try:
																						_a	:    Any         =		Split[mode]
																	except KeyError:
																						raise KeyError('''mode is not a valid split name'''       )
        # Load data features from cache or dataset file
												_a	:    Union[str, Any]         =		os.path.join(
												    cache_dir if cache_dir is not None else args.data_dir       ,							F"""cached_{mode.value}_{tokenizer.__class__.__name__}_{args.max_seq_length}_{args.task_name}"""       ,							)
												_a	:    str         =		self.processor.get_labels()
												if args.task_name in ["mnli", "mnli-mm"] and tokenizer.__class__.__name__ in (
												    "RobertaTokenizer",
												    "RobertaTokenizerFast",
												    "XLMRobertaTokenizer",
												    "BartTokenizer",
												    "BartTokenizerFast",
												):
																	# HACK(label indices are swapped in RoBERTa pretrained model)
																	_a      ,					_a	:    Optional[int]         =		label_list[2], label_list[1]
												_a	:    Dict         =		label_list

												# Make sure only the first process in distributed training processes the dataset,
												# and the others will use the cache.
												_a	:    Optional[int]         =		cached_features_file + '''.lock'''
												with FileLock(_a       ):
																	if os.path.exists(_a       ) and not args.overwrite_cache:
																						_a	:    List[Any]         =		time.time()
																						_a	:    str         =		torch.load(_a       )
																						logger.info(
																						    F"""Loading features from cached file {cached_features_file} [took %.3f s]"""       ,							time.time() - start       )
																	else:
																						logger.info(F"""Creating features from dataset file at {args.data_dir}"""       )

																						if mode == Split.dev:
																											_a	:    str         =		self.processor.get_dev_examples(args.data_dir       )
																						elif mode == Split.test:
																											_a	:    Any         =		self.processor.get_test_examples(args.data_dir       )
																						else:
																											_a	:    List[str]         =		self.processor.get_train_examples(args.data_dir       )
																						if limit_length is not None:
																											_a	:    Dict         =		examples[:limit_length]
																						_a	:    Union[str, Any]         =		glue_convert_examples_to_features(
																						    _a       ,							_a       ,							max_length=args.max_seq_length       ,							label_list=_a       ,							output_mode=self.output_mode       ,							)
																						_a	:    Optional[int]         =		time.time()
																						torch.save(self.features       ,							_a       )
																						# ^ This seems to take a lot of time so I want to investigate why and how we can improve.
																						logger.info(
																						    F"""Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]"""       )


							def __len__(			self       )    ->    Optional[int]:
												return len(self.features       )


							def __getitem__(			self       ,							_a       )    ->    InputFeatures:
												return self.features[i]




							def 				__lowercase				(			self       )    ->    Tuple:
												return self.label_list



