



'''simple docstring'''


import inspect
import unittest
from math import floor

from transformers import CvtConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

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 transformers import CvtForImageClassification, CvtModel
						from transformers.models.cvt.modeling_cvt import CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import AutoImageProcessor




class      __UpperCamelCase       (__lowercase			):







	def   _a   (    self						)    ->    List[str]:
							lowercase        =							self.config_class(**self.inputs_dict						)
							self.parent.assertTrue(hasattr(_lowerCAmelCase ,				"""embed_dim"""						)						)
							self.parent.assertTrue(hasattr(_lowerCAmelCase ,				"""num_heads"""						)						)




class      __UpperCamelCase       :







	def __init__(    self ,				_lowerCAmelCase ,				_lowerCAmelCase=13 ,				_lowerCAmelCase=64 ,				_lowerCAmelCase=3 ,				_lowerCAmelCase=[16, 48, 96] ,				_lowerCAmelCase=[1, 3, 6] ,				_lowerCAmelCase=[1, 2, 10] ,				_lowerCAmelCase=[7, 3, 3] ,				_lowerCAmelCase=[4, 2, 2] ,				_lowerCAmelCase=[2, 1, 1] ,				_lowerCAmelCase=[2, 2, 2] ,				_lowerCAmelCase=[False, False, True] ,				_lowerCAmelCase=[0.0, 0.0, 0.0] ,				_lowerCAmelCase=0.02 ,				_lowerCAmelCase=1E-12 ,				_lowerCAmelCase=True ,				_lowerCAmelCase=True ,				_lowerCAmelCase=2 ,				)    ->    Optional[Any]:
							lowercase        =							parent
							lowercase        =							batch_size
							lowercase        =							image_size
							lowercase        =							patch_sizes
							lowercase        =							patch_stride
							lowercase        =							patch_padding
							lowercase        =							is_training
							lowercase        =							use_labels
							lowercase        =							num_labels
							lowercase        =							num_channels
							lowercase        =							embed_dim
							lowercase        =							num_heads
							lowercase        =							stride_kv
							lowercase        =							depth
							lowercase        =							cls_token
							lowercase        =							attention_drop_rate
							lowercase        =							initializer_range
							lowercase        =							layer_norm_eps







	def   _a   (    self						)    ->    int:
							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.num_labels						)

							lowercase        =							self.get_config()
							return config, pixel_values, labels







	def   _a   (    self						)    ->    Dict:
							return CvtConfig(
							    image_size=self.image_size ,				num_labels=self.num_labels ,				num_channels=self.num_channels ,				embed_dim=self.embed_dim ,				num_heads=self.num_heads ,				patch_sizes=self.patch_sizes ,				patch_padding=self.patch_padding ,				patch_stride=self.patch_stride ,				stride_kv=self.stride_kv ,				depth=self.depth ,				cls_token=self.cls_token ,				attention_drop_rate=self.attention_drop_rate ,				initializer_range=self.initializer_range ,				)







	def   _a   (    self ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)    ->    int:
							lowercase        =							CvtModel(config=_lowerCAmelCase						)
							model.to(_lowerCAmelCase						)
							model.eval()
							lowercase        =							model(_lowerCAmelCase						)
							lowercase        =							(self.image_size, self.image_size)
							lowercase        =							image_size[0], image_size[1]
							for i in range(len(self.depth						)						):
													lowercase        =							floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1						)
													lowercase        =							floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1						)
							self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, self.embed_dim[-1], height, width)						)







	def   _a   (    self ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)    ->    Tuple:
							lowercase        =							self.num_labels
							lowercase        =							CvtForImageClassification(_lowerCAmelCase						)
							model.to(_lowerCAmelCase						)
							model.eval()
							lowercase        =							model(_lowerCAmelCase ,				labels=_lowerCAmelCase						)
							self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.num_labels)						)







	def   _a   (    self						)    ->    int:
							lowercase        =							self.prepare_config_and_inputs()
							lowercase        =							config_and_inputs
							lowercase        =							{"pixel_values": pixel_values}
							return config, inputs_dict




@require_torch
class      __UpperCamelCase       (__lowercase			,   __lowercase			,   unittest.TestCase			):
	__A			  =					(CvtModel, CvtForImageClassification) if is_torch_available() else ()
	__A			  =					(
	    {'''feature-extraction''': CvtModel, '''image-classification''': CvtForImageClassification}
	    if is_torch_available()
	    else {}
	)

	__A			  =					False
	__A			  =					False
	__A			  =					False
	__A			  =					False
	__A			  =					False







	def   _a   (    self						)    ->    Dict:
							lowercase        =							CvtModelTester(self						)
							lowercase        =							ConfigTester(self ,				config_class=_lowerCAmelCase ,				has_text_modality=_lowerCAmelCase ,				hidden_size=37						)







	def   _a   (    self						)    ->    Any:
							self.create_and_test_config_common_properties()
							self.config_tester.create_and_test_config_to_json_string()
							self.config_tester.create_and_test_config_to_json_file()
							self.config_tester.create_and_test_config_from_and_save_pretrained()
							self.config_tester.create_and_test_config_with_num_labels()
							self.config_tester.check_config_can_be_init_without_params()
							self.config_tester.check_config_arguments_init()







	def   _a   (    self						)    ->    Tuple:
							return







	@unittest.skip(reason="""Cvt does not output attentions"""						)
	def   _a   (    self						)    ->    Optional[Any]:
							pass







	@unittest.skip(reason="""Cvt does not use inputs_embeds"""						)
	def   _a   (    self						)    ->    Dict:
							pass







	@unittest.skip(reason="""Cvt does not support input and output embeddings"""						)
	def   _a   (    self						)    ->    int:
							pass







	def   _a   (    self						)    ->    Union[str, Any]:
							lowercase        =							self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
													lowercase        =							model_class(_lowerCAmelCase						)
													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] ,				_lowerCAmelCase						)







	def   _a   (    self						)    ->    Optional[int]:
							lowercase        =							self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_lowerCAmelCase						)







	def   _a   (    self						)    ->    Union[str, Any]:
							def check_hidden_states_output(_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						):
													lowercase        =							model_class(_lowerCAmelCase						)
													model.to(_lowerCAmelCase						)
													model.eval()

													with torch.no_grad():
																			lowercase        =							model(**self._prepare_for_class(_lowerCAmelCase ,				_lowerCAmelCase						)						)

													lowercase        =							outputs.hidden_states

													lowercase        =							len(self.model_tester.depth						)
													self.assertEqual(len(_lowerCAmelCase						) ,				_lowerCAmelCase						)

													# verify the first hidden states (first block)
													self.assertListEqual(
													    list(hidden_states[0].shape[-3:]						) ,				[
													        self.model_tester.embed_dim[0],
													        self.model_tester.image_size // 4,
													        self.model_tester.image_size // 4,
													    ] ,				)

							lowercase        =							self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
													lowercase        =							True
													check_hidden_states_output(_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)

													# check that output_hidden_states also work using config
													del inputs_dict["output_hidden_states"]
													lowercase        =							True

													check_hidden_states_output(_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)







	def   _a   (    self						)    ->    Dict:
							lowercase        =							self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase						)







	@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""						)
	def   _a   (    self						)    ->    Any:
							pass







	@slow
	def   _a   (    self						)    ->    Optional[Any]:
							for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													lowercase        =							CvtModel.from_pretrained(_lowerCAmelCase						)
													self.assertIsNotNone(_lowerCAmelCase						)







def      SCREAMING_SNAKE_CASE				(				):
						lowercase        =							Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""						)
						return image




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







	@cached_property
	def   _a   (    self						)    ->    List[Any]:
							return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]						)







	@slow
	def   _a   (    self						)    ->    int:
							lowercase        =							CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]						).to(_lowerCAmelCase						)

							lowercase        =							self.default_image_processor
							lowercase        =							prepare_img()
							lowercase        =							image_processor(images=_lowerCAmelCase ,				return_tensors="""pt"""						).to(_lowerCAmelCase						)

							# forward pass
							with torch.no_grad():
													lowercase        =							model(**_lowerCAmelCase						)

							# verify the logits
							lowercase        =							torch.Size((1, 1000)						)
							self.assertEqual(outputs.logits.shape ,				_lowerCAmelCase						)

							lowercase        =							torch.tensor([0.9285, 0.9015, -0.3150]						).to(_lowerCAmelCase						)

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







'''simple docstring'''


from dataclasses import dataclass, field
from typing import Tuple

from ..utils import cached_property, is_tf_available, logging, requires_backends
from .benchmark_args_utils import BenchmarkArguments


if is_tf_available():
						import tensorflow as tf


lowercase_      :				Union[str, Any]   					=    logging.get_logger(__name__)




@dataclass
class      __UpperCamelCase       (_UpperCAmelCase			):
	__A			  =					[
	    '''no_inference''',
	    '''no_cuda''',
	    '''no_tpu''',
	    '''no_speed''',
	    '''no_memory''',
	    '''no_env_print''',
	    '''no_multi_process''',
	]







	def __init__(    self ,				**_lowerCAmelCase						)    ->    Optional[int]:
							'''simple docstring'''






							for deprecated_arg in self.deprecated_args:
													if deprecated_arg in kwargs:
																			lowercase        =							deprecated_arg[3:]
																			lowercase        =							not kwargs.pop(_lowerCAmelCase						)
																			logger.warning(
																			    F"""{deprecated_arg} is depreciated. Please use --no-{positive_arg} or"""
																			    F""" {positive_arg}={kwargs[positive_arg]}"""						)
							lowercase        =							kwargs.pop("""tpu_name""" ,				self.tpu_name						)
							lowercase        =							kwargs.pop("""device_idx""" ,				self.device_idx						)
							lowercase        =							kwargs.pop("""eager_mode""" ,				self.eager_mode						)
							lowercase        =							kwargs.pop("""use_xla""" ,				self.use_xla						)
							super().__init__(**_lowerCAmelCase						)

	__A			  =					field(
	    default=_UpperCAmelCase			,   metadata={'''help''': '''Name of TPU'''}			,   )
	__A			  =					field(
	    default=0			,   metadata={'''help''': '''CPU / GPU device index. Defaults to 0.'''}			,   )
	__A			  =					field(default=_UpperCAmelCase			,   metadata={'''help''': '''Benchmark models in eager model.'''}			)
	__A			  =					field(
	    default=_UpperCAmelCase			,   metadata={
	        '''help''': '''Benchmark models using XLA JIT compilation. Note that `eager_model` has to be set to `False`.'''
	    }			,   )







	@cached_property
	def   _a   (    self						)    ->    Tuple["tf.distribute.cluster_resolver.TPUClusterResolver"]:
							'''simple docstring'''






							requires_backends(self ,				["""tf"""]						)
							lowercase        =							None
							if self.tpu:
													try:
																			if self.tpu_name:
																									lowercase        =							tf.distribute.cluster_resolver.TPUClusterResolver(self.tpu_name						)
																			else:
																									lowercase        =							tf.distribute.cluster_resolver.TPUClusterResolver()
													except ValueError:
																			lowercase        =							None
							return tpu







	@cached_property
	def   _a   (    self						)    ->    Tuple["tf.distribute.Strategy", "tf.distribute.cluster_resolver.TPUClusterResolver"]:
							'''simple docstring'''






							requires_backends(self ,				["""tf"""]						)
							if self.is_tpu:
													tf.config.experimental_connect_to_cluster(self._setup_tpu						)
													tf.tpu.experimental.initialize_tpu_system(self._setup_tpu						)

													lowercase        =							tf.distribute.TPUStrategy(self._setup_tpu						)
							else:
													# currently no multi gpu is allowed
													if self.is_gpu:
																			# TODO: Currently only single GPU is supported
																			tf.config.set_visible_devices(self.gpu_list[self.device_idx] ,				"""GPU"""						)
																			lowercase        =							tf.distribute.OneDeviceStrategy(device=F"""/gpu:{self.device_idx}"""						)
													else:
																			tf.config.set_visible_devices([] ,				"""GPU"""						)  # disable GPU
																			lowercase        =							tf.distribute.OneDeviceStrategy(device=F"""/cpu:{self.device_idx}"""						)

							return strategy







	@property
	def   _a   (    self						)    ->    bool:
							'''simple docstring'''






							requires_backends(self ,				["""tf"""]						)
							return self._setup_tpu is not None







	@property
	def   _a   (    self						)    ->    "tf.distribute.Strategy":
							'''simple docstring'''






							requires_backends(self ,				["""tf"""]						)
							return self._setup_strategy







	@property
	def   _a   (    self						)    ->    Tuple:
							'''simple docstring'''






							requires_backends(self ,				["""tf"""]						)
							return tf.config.list_physical_devices("""GPU"""						)







	@property
	def   _a   (    self						)    ->    int:
							'''simple docstring'''






							requires_backends(self ,				["""tf"""]						)
							if self.cuda:
													return len(self.gpu_list						)
							return 0







	@property
	def   _a   (    self						)    ->    bool:
							'''simple docstring'''






							return self.n_gpu > 0


