from __future__ import annotations

import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

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


if is_tf_available():
					import tensorflow as tf

					from transformers import TFViTForImageClassification, TFViTModel


if is_vision_available():
					from PIL import Image

					from transformers import ViTImageProcessor
class     lowerCAmelCase_     :







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

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







							def  UpperCamelCase_						(   self      :     Optional[Any]		):
												_UpperCamelCase			     =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

												_UpperCamelCase			     =     self.get_config()

												return config, pixel_values, labels







							def  UpperCamelCase_						(   self      :     int		):
												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 ,			)







							def  UpperCamelCase_						(   self      :     Tuple ,			_A      :     Tuple ,			_A      :     int ,			_A      :     Dict		):
												_UpperCamelCase			     =     TFViTModel(config=_A		)
												_UpperCamelCase			     =     model(_A ,			training=_A		)
												self.parent.assertEqual(result.last_hidden_state.shape ,			(self.batch_size, self.seq_length, self.hidden_size)		)

												# Test with an image with different size than the one specified in config.
												_UpperCamelCase			     =     self.image_size // 2
												_UpperCamelCase			     =     pixel_values[:, :, :image_size, :image_size]
												_UpperCamelCase			     =     model(_A ,			interpolate_pos_encoding=_A ,			training=_A		)
												_UpperCamelCase			     =     (image_size // self.patch_size) ** 2 + 1
												self.parent.assertEqual(result.last_hidden_state.shape ,			(self.batch_size, seq_length, self.hidden_size)		)







							def  UpperCamelCase_						(   self      :     int ,			_A      :     Any ,			_A      :     Union[str, Any] ,			_A      :     List[str]		):
												_UpperCamelCase			     =     self.type_sequence_label_size
												_UpperCamelCase			     =     TFViTForImageClassification(_A		)
												_UpperCamelCase			     =     model(_A ,			labels=_A ,			training=_A		)
												self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.type_sequence_label_size)		)

												# Test with an image with different size than the one specified in config.
												_UpperCamelCase			     =     self.image_size // 2
												_UpperCamelCase			     =     pixel_values[:, :, :image_size, :image_size]
												_UpperCamelCase			     =     model(_A ,			interpolate_pos_encoding=_A ,			training=_A		)
												self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.type_sequence_label_size)		)

												# test greyscale images
												_UpperCamelCase			     =     1
												_UpperCamelCase			     =     TFViTForImageClassification(_A		)
												_UpperCamelCase			     =     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
												_UpperCamelCase			     =     model(_A		)
												self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.type_sequence_label_size)		)







							def  UpperCamelCase_						(   self      :     int		):
												_UpperCamelCase			     =     self.prepare_config_and_inputs()
												_UpperCamelCase     ,    _UpperCamelCase     ,    _UpperCamelCase			     =     config_and_inputs
												_UpperCamelCase			     =     {'''pixel_values''': pixel_values}
												return config, inputs_dict




@require_tf
class     lowerCAmelCase_     (       __lowercase,  __lowercase,  unittest.TestCase ):
							UpperCAmelCase				   =     (TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
							UpperCAmelCase				   =     (
							    {"feature-extraction": TFViTModel, "image-classification": TFViTForImageClassification}
							    if is_tf_available()
							    else {}
							)

							UpperCAmelCase				   =     False
							UpperCAmelCase				   =     False
							UpperCAmelCase				   =     False







							def  UpperCamelCase_						(   self      :     Any		):
												_UpperCamelCase			     =     TFViTModelTester(self		)
												_UpperCamelCase			     =     ConfigTester(self ,			config_class=_A ,			has_text_modality=_A ,			hidden_size=37		)







							def  UpperCamelCase_						(   self      :     Any		):
												self.config_tester.run_common_tests()







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







							@unittest.skip(reason='''ViT does not use inputs_embeds'''		)
							def  UpperCamelCase_						(   self      :     List[str]		):
												pass







							def  UpperCamelCase_						(   self      :     Optional[Any]		):
												_UpperCamelCase     ,    _UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																	_UpperCamelCase			     =     model_class(_A		)
																	self.assertIsInstance(model.get_input_embeddings() ,			(tf.keras.layers.Layer)		)
																	_UpperCamelCase			     =     model.get_output_embeddings()
																	self.assertTrue(x is None or isinstance(_A ,			tf.keras.layers.Layer		)		)







							def  UpperCamelCase_						(   self      :     Union[str, Any]		):
												_UpperCamelCase     ,    _UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs_for_common()

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

																	_UpperCamelCase			     =     ['''pixel_values''']
																	self.assertListEqual(arg_names[:1] ,			_A		)







							def  UpperCamelCase_						(   self      :     Optional[int]		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*_A		)







							def  UpperCamelCase_						(   self      :     Optional[int]		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*_A		)







							@slow
							def  UpperCamelCase_						(   self      :     Optional[int]		):
												_UpperCamelCase			     =     TFViTModel.from_pretrained('''google/vit-base-patch16-224'''		)
												self.assertIsNotNone(_A		)




def 					_snake_case	(  ):
					_UpperCamelCase			     =     Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						)
					return image







@require_tf
@require_vision
class     lowerCAmelCase_     (       unittest.TestCase ):







							@cached_property
							def  UpperCamelCase_						(   self      :     List[Any]		):
												return ViTImageProcessor.from_pretrained('''google/vit-base-patch16-224'''		) if is_vision_available() else None







							@slow
							def  UpperCamelCase_						(   self      :     Any		):
												_UpperCamelCase			     =     TFViTForImageClassification.from_pretrained('''google/vit-base-patch16-224'''		)

												_UpperCamelCase			     =     self.default_image_processor
												_UpperCamelCase			     =     prepare_img()
												_UpperCamelCase			     =     image_processor(images=_A ,			return_tensors='''tf'''		)

												# forward pass
												_UpperCamelCase			     =     model(**_A		)

												# verify the logits
												_UpperCamelCase			     =     tf.TensorShape((1, 1000)		)
												self.assertEqual(outputs.logits.shape ,			_A		)

												_UpperCamelCase			     =     tf.constant([-0.2744, 0.8215, -0.0836]		)

												tf.debugging.assert_near(outputs.logits[0, :3] ,			_A ,			atol=1e-4		)

import dataclasses
import json
import warnings
from dataclasses import dataclass, field
from time import time
from typing import List

from ..utils import logging


_lowerCAmelCase       =		logging.get_logger(__name__)
def 					_snake_case	(  __snake_case=None  ,     __snake_case=None						):
					return field(default_factory=lambda: default  ,     metadata=__snake_case						)







@dataclass
class     lowerCAmelCase_     :
							UpperCAmelCase				   =     list_field(
							    default=[],  metadata={
							        "help": (
							            "Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version"
							            " of all available models"
							        )
							    },  )

							UpperCAmelCase				   =     list_field(
							    default=[8],  metadata={"help": "List of batch sizes for which memory and time performance will be evaluated"} )

							UpperCAmelCase				   =     list_field(
							    default=[8, 32, 128, 512],  metadata={"help": "List of sequence lengths for which memory and time performance will be evaluated"},  )

							UpperCAmelCase				   =     field(
							    default=__lowercase,  metadata={"help": "Whether to benchmark inference of model. Inference can be disabled via --no-inference."},  )
							UpperCAmelCase				   =     field(
							    default=__lowercase,  metadata={"help": "Whether to run on available cuda devices. Cuda can be disabled via --no-cuda."},  )
							UpperCAmelCase				   =     field(
							    default=__lowercase,  metadata={"help": "Whether to run on available tpu devices. TPU can be disabled via --no-tpu."} )
							UpperCAmelCase				   =     field(default=__lowercase,  metadata={"help": "Use FP16 to accelerate inference."} )
							UpperCAmelCase				   =     field(default=__lowercase,  metadata={"help": "Benchmark training of model"} )
							UpperCAmelCase				   =     field(default=__lowercase,  metadata={"help": "Verbose memory tracing"} )
							UpperCAmelCase				   =     field(
							    default=__lowercase,  metadata={"help": "Whether to perform speed measurements. Speed measurements can be disabled via --no-speed."},  )
							UpperCAmelCase				   =     field(
							    default=__lowercase,  metadata={
							        "help": "Whether to perform memory measurements. Memory measurements can be disabled via --no-memory"
							    },  )
							UpperCAmelCase				   =     field(default=__lowercase,  metadata={"help": "Trace memory line by line"} )
							UpperCAmelCase				   =     field(default=__lowercase,  metadata={"help": "Save result to a CSV file"} )
							UpperCAmelCase				   =     field(default=__lowercase,  metadata={"help": "Save all print statements in a log file"} )
							UpperCAmelCase				   =     field(default=__lowercase,  metadata={"help": "Whether to print environment information"} )
							UpperCAmelCase				   =     field(
							    default=__lowercase,  metadata={
							        "help": (
							            "Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use"
							            " multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled"
							            " for debugging / testing and on TPU."
							        )
							    },  )
							UpperCAmelCase				   =     field(
							    default=F"""inference_time_{round(time() )}.csv""",  metadata={"help": "CSV filename used if saving time results to csv."},  )
							UpperCAmelCase				   =     field(
							    default=F"""inference_memory_{round(time() )}.csv""",  metadata={"help": "CSV filename used if saving memory results to csv."},  )
							UpperCAmelCase				   =     field(
							    default=F"""train_time_{round(time() )}.csv""",  metadata={"help": "CSV filename used if saving time results to csv for training."},  )
							UpperCAmelCase				   =     field(
							    default=F"""train_memory_{round(time() )}.csv""",  metadata={"help": "CSV filename used if saving memory results to csv for training."},  )
							UpperCAmelCase				   =     field(
							    default=F"""env_info_{round(time() )}.csv""",  metadata={"help": "CSV filename used if saving environment information."},  )
							UpperCAmelCase				   =     field(
							    default=F"""log_{round(time() )}.csv""",  metadata={"help": "Log filename used if print statements are saved in log."},  )
							UpperCAmelCase				   =     field(default=3,  metadata={"help": "Times an experiment will be run."} )
							UpperCAmelCase				   =     field(
							    default=__lowercase,  metadata={
							        "help": (
							            "Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain"
							            " model weights."
							        )
							    },  )







							def  UpperCamelCase_						(   self      :     Union[str, Any]		):
												warnings.warn(
												    F"""The class {self.__class__} is deprecated. Hugging Face Benchmarking utils"""
												    ''' are deprecated in general and it is advised to use external Benchmarking libraries '''
												    ''' to benchmark Transformer models.''' ,			_A ,			)







							def  UpperCamelCase_						(   self      :     str		):
												return json.dumps(dataclasses.asdict(self		) ,			indent=2		)







							@property
							def  UpperCamelCase_						(   self      :     List[Any]		):
												if len(self.models		) <= 0:
																	raise ValueError(
																	    '''Please make sure you provide at least one model name / model identifier, *e.g.* `--models'''
																	    ''' bert-base-cased` or `args.models = [\'bert-base-cased\'].'''		)
												return self.models







							@property
							def  UpperCamelCase_						(   self      :     Optional[int]		):
												if not self.multi_process:
																	return False
												elif self.is_tpu:
																	logger.info('''Multiprocessing is currently not possible on TPU.'''		)
																	return False
												else:
																	return True
