"""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__(	__A       ):





						def  snake_case__		(				self   )    -> Dict:
													A__		   =							self.config_class(**self.inputs_dict   )
													self.parent.assertTrue(hasattr(__UpperCAmelCase						,'embed_dim'   )   )
													self.parent.assertTrue(hasattr(__UpperCAmelCase						,'num_heads'   )   )






class 			UpperCamelCase__:





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





						def  snake_case__		(				self   )    -> Optional[int]:
													A__		   =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

													A__		   =							None
													if self.use_labels:
																				A__		   =							ids_tensor([self.batch_size]						,self.num_labels   )

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





						def  snake_case__		(				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  snake_case__		(				self						,__UpperCAmelCase						,__UpperCAmelCase						,__UpperCAmelCase   )    -> List[Any]:
													A__		   =							CvtModel(config=__UpperCAmelCase   )
													model.to(__UpperCAmelCase   )
													model.eval()
													A__		   =							model(__UpperCAmelCase   )
													A__		   =							(self.image_size, self.image_size)
													A__   ,     A__		   =							image_size[0], image_size[1]
													for i in range(len(self.depth   )   ):
																				A__		   =							floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1   )
																				A__		   =							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  snake_case__		(				self						,__UpperCAmelCase						,__UpperCAmelCase						,__UpperCAmelCase   )    -> str:
													A__		   =							self.num_labels
													A__		   =							CvtForImageClassification(__UpperCAmelCase   )
													model.to(__UpperCAmelCase   )
													model.eval()
													A__		   =							model(__UpperCAmelCase						,labels=__UpperCAmelCase   )
													self.parent.assertEqual(result.logits.shape						,(self.batch_size, self.num_labels)   )





						def  snake_case__		(				self   )    -> Dict:
													A__		   =							self.prepare_config_and_inputs()
													A__   ,     A__   ,     A__		   =							config_and_inputs
													A__		   =							{'pixel_values': pixel_values}
													return config, inputs_dict






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

						lowerCAmelCase__     :  List[Any]  							=						False
						lowerCAmelCase__     :  Optional[int]  							=						False
						lowerCAmelCase__     :  List[Any]  							=						False
						lowerCAmelCase__     :  Union[str, Any]  							=						False
						lowerCAmelCase__     :  Union[str, Any]  							=						False





						def  snake_case__		(				self   )    -> Union[str, Any]:
													A__		   =							CvtModelTester(self   )
													A__		   =							ConfigTester(self						,config_class=__UpperCAmelCase						,has_text_modality=__UpperCAmelCase						,hidden_size=37   )





						def  snake_case__		(				self   )    -> Optional[int]:
													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  snake_case__		(				self   )    -> List[Any]:
													return





						@unittest.skip(reason='Cvt does not output attentions'   )
						def  snake_case__		(				self   )    -> Dict:
													pass





						@unittest.skip(reason='Cvt does not use inputs_embeds'   )
						def  snake_case__		(				self   )    -> Optional[Any]:
													pass





						@unittest.skip(reason='Cvt does not support input and output embeddings'   )
						def  snake_case__		(				self   )    -> Dict:
													pass





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

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

																				A__		   =							['pixel_values']
																				self.assertListEqual(arg_names[:1]						,__UpperCAmelCase   )





						def  snake_case__		(				self   )    -> Dict:
													A__		   =							self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*__UpperCAmelCase   )





						def  snake_case__		(				self   )    -> Dict:
													def check_hidden_states_output(__UpperCAmelCase						,__UpperCAmelCase						,__UpperCAmelCase   ):
																				A__		   =							model_class(__UpperCAmelCase   )
																				model.to(__UpperCAmelCase   )
																				model.eval()

																				with torch.no_grad():
																											A__		   =							model(**self._prepare_for_class(__UpperCAmelCase						,__UpperCAmelCase   )   )

																				A__		   =							outputs.hidden_states

																				A__		   =							len(self.model_tester.depth   )
																				self.assertEqual(len(__UpperCAmelCase   )						,__UpperCAmelCase   )

																				# 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,
																				    ]						,)

													A__   ,     A__		   =							self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																				A__		   =							True
																				check_hidden_states_output(__UpperCAmelCase						,__UpperCAmelCase						,__UpperCAmelCase   )

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

																				check_hidden_states_output(__UpperCAmelCase						,__UpperCAmelCase						,__UpperCAmelCase   )





						def  snake_case__		(				self   )    -> int:
													A__		   =							self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase   )





						@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'   )
						def  snake_case__		(				self   )    -> Optional[int]:
													pass





						@slow
						def  snake_case__		(				self   )    -> Optional[Any]:
													for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																				A__		   =							CvtModel.from_pretrained(__UpperCAmelCase   )
																				self.assertIsNotNone(__UpperCAmelCase   )






def      UpperCAmelCase    (		):






							"""simple docstring"""





							A__		   =							Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' )
							return image


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





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





						@slow
						def  snake_case__		(				self   )    -> Optional[Any]:
													A__		   =							CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]   ).to(__UpperCAmelCase   )

													A__		   =							self.default_image_processor
													A__		   =							prepare_img()
													A__		   =							image_processor(images=__UpperCAmelCase						,return_tensors='pt'   ).to(__UpperCAmelCase   )

													# forward pass
													with torch.no_grad():
																				A__		   =							model(**__UpperCAmelCase   )

													# verify the logits
													A__		   =							torch.Size((1, 10_00)   )
													self.assertEqual(outputs.logits.shape						,__UpperCAmelCase   )

													A__		   =							torch.tensor([0.9_2_8_5, 0.9_0_1_5, -0.3_1_5_0]   ).to(__UpperCAmelCase   )

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






"""simple docstring"""




import os
import socket
from contextlib import contextmanager

import torch

from ..commands.config.default import write_basic_config  # noqa: F401
from ..state import PartialState
from .dataclasses import DistributedType
from .imports import is_deepspeed_available, is_tpu_available
from .transformer_engine import convert_model
from .versions import is_torch_version


if is_deepspeed_available():
				from deepspeed import DeepSpeedEngine

if is_tpu_available(check_device=False):
				import torch_xla.core.xla_model as xm






def      UpperCAmelCase    (		UpperCamelCase__ ):






							"""simple docstring"""





							if is_torch_version('<'				,			'2.0.0' ) or not hasattr(UpperCamelCase__				,			'_dynamo' ):
														return False
							return isinstance(UpperCamelCase__				,			torch._dynamo.eval_frame.OptimizedModule )






def      UpperCAmelCase    (		UpperCamelCase__				,			UpperCamelCase__ = True ):






							"""simple docstring"""





							A__		   =							(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

							A__		   =							is_compiled_module(UpperCamelCase__ )
							if is_compiled:
														A__		   =							model
														A__		   =							model._orig_mod

							if is_deepspeed_available():
														options += (DeepSpeedEngine,)

							while isinstance(UpperCamelCase__				,			UpperCamelCase__ ):
														A__		   =							model.module

							if not keep_fpaa_wrapper:
														A__		   =							getattr(UpperCamelCase__				,			'forward' )
														A__		   =							model.__dict__.pop('_original_forward'				,			UpperCamelCase__ )
														if original_forward is not None:
																					while hasattr(UpperCamelCase__				,			'__wrapped__' ):
																												A__		   =							forward.__wrapped__
																												if forward == original_forward:
																																			break
																					A__		   =							forward
														if getattr(UpperCamelCase__				,			'_converted_to_transformer_engine'				,			UpperCamelCase__ ):
																					convert_model(UpperCamelCase__				,			to_transformer_engine=UpperCamelCase__ )

							if is_compiled:
														A__		   =							model
														A__		   =							compiled_model

							return model






def      UpperCAmelCase    (		):






							"""simple docstring"""





							PartialState().wait_for_everyone()






def      UpperCAmelCase    (		UpperCamelCase__				,			UpperCamelCase__ ):






							"""simple docstring"""





							if PartialState().distributed_type == DistributedType.TPU:
														xm.save(UpperCamelCase__				,			UpperCamelCase__ )
							elif PartialState().local_process_index == 0:
														torch.save(UpperCamelCase__				,			UpperCamelCase__ )






@contextmanager
def      UpperCAmelCase    (		**UpperCamelCase__ ):






							"""simple docstring"""





							for key, value in kwargs.items():
														A__		   =							str(UpperCamelCase__ )

							yield

							for key in kwargs:
														if key.upper() in os.environ:
																					del os.environ[key.upper()]






def      UpperCAmelCase    (		UpperCamelCase__ ):






							"""simple docstring"""





							if not hasattr(UpperCamelCase__				,			'__qualname__' ) and not hasattr(UpperCamelCase__				,			'__name__' ):
														A__		   =							getattr(UpperCamelCase__				,			'__class__'				,			UpperCamelCase__ )
							if hasattr(UpperCamelCase__				,			'__qualname__' ):
														return obj.__qualname__
							if hasattr(UpperCamelCase__				,			'__name__' ):
														return obj.__name__
							return str(UpperCamelCase__ )






def      UpperCAmelCase    (		UpperCamelCase__				,			UpperCamelCase__ ):






							"""simple docstring"""





							for key, value in source.items():
														if isinstance(UpperCamelCase__				,			UpperCamelCase__ ):
																					A__		   =							destination.setdefault(UpperCamelCase__				,			{} )
																					merge_dicts(UpperCamelCase__				,			UpperCamelCase__ )
														else:
																					A__		   =							value

							return destination






def      UpperCAmelCase    (		UpperCamelCase__ = None ):






							"""simple docstring"""





							if port is None:
														A__		   =							29_500
							with socket.socket(socket.AF_INET				,			socket.SOCK_STREAM ) as s:
														return s.connect_ex(('localhost', port) ) == 0





