




'''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				(     lowerCAmelCase				):



					'''simple docstring'''





					def     __snake_case						(	self     :						str)    ->							List[str]:
								A_									= self.config_class(**self.inputs_dict)
								self.parent.assertTrue(hasattr(_lowercase      ,       'embed_dim'))
								self.parent.assertTrue(hasattr(_lowercase      ,       'num_heads'))





class   __UpperCAmelCase				:



					'''simple docstring'''





					def __init__(	self     :						Optional[Any]      ,       _lowercase     :						List[Any]      ,       _lowercase     :						Union[str, Any]=13      ,       _lowercase     :						Dict=64      ,       _lowercase     :						Optional[int]=3      ,       _lowercase     :						str=[16, 48, 96]      ,       _lowercase     :						Dict=[1, 3, 6]      ,       _lowercase     :						Optional[Any]=[1, 2, 10]      ,       _lowercase     :						Optional[int]=[7, 3, 3]      ,       _lowercase     :						Union[str, Any]=[4, 2, 2]      ,       _lowercase     :						Tuple=[2, 1, 1]      ,       _lowercase     :						int=[2, 2, 2]      ,       _lowercase     :						Tuple=[False, False, True]      ,       _lowercase     :						Optional[Any]=[0.0, 0.0, 0.0]      ,       _lowercase     :						List[Any]=0.02      ,       _lowercase     :						Dict=1E-12      ,       _lowercase     :						int=True      ,       _lowercase     :						Dict=True      ,       _lowercase     :						List[str]=2      ,       )    ->							Optional[int]:
								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     :						List[str])    ->							Any:
								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     :						int)    ->							Optional[int]:
								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     :						Optional[Any]      ,       _lowercase     :						Any      ,       _lowercase     :						List[Any]      ,       _lowercase     :						Optional[int])    ->							Optional[int]:
								A_									= CvtModel(config=_lowercase)
								model.to(_lowercase)
								model.eval()
								A_									= model(_lowercase)
								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     :						Any      ,       _lowercase     :						Any      ,       _lowercase     :						str      ,       _lowercase     :						Optional[int])    ->							List[Any]:
								A_									= self.num_labels
								A_									= CvtForImageClassification(_lowercase)
								model.to(_lowercase)
								model.eval()
								A_									= model(_lowercase      ,       labels=_lowercase)
								self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.num_labels))



					def     __snake_case						(	self     :						str)    ->							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				(     lowerCAmelCase			,lowerCAmelCase			,unittest.TestCase				):



					'''simple docstring'''





					_UpperCamelCase							=		(CvtModel, CvtForImageClassification) if is_torch_available() else ()
					_UpperCamelCase							=		(
					    {"""feature-extraction""": CvtModel, """image-classification""": CvtForImageClassification}
					    if is_torch_available()
					    else {}
					)

					_UpperCamelCase							=		False
					_UpperCamelCase							=		False
					_UpperCamelCase							=		False
					_UpperCamelCase							=		False
					_UpperCamelCase							=		False



					def     __snake_case						(	self     :						str)    ->							Union[str, Any]:
								A_									= CvtModelTester(self)
								A_									= ConfigTester(self      ,       config_class=_lowercase      ,       has_text_modality=_lowercase      ,       hidden_size=37)



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



					@unittest.skip(reason='Cvt does not output attentions')
					def     __snake_case						(	self     :						Union[str, Any])    ->							Optional[Any]:
								pass



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



					@unittest.skip(reason='Cvt does not support input and output embeddings')
					def     __snake_case						(	self     :						Union[str, Any])    ->							Tuple:
								pass



					def     __snake_case						(	self     :						Optional[Any])    ->							Optional[Any]:
								A_		, A_									= self.model_tester.prepare_config_and_inputs_for_common()

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



					def     __snake_case						(	self     :						int)    ->							Dict:
								A_									= self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_lowercase)



					def     __snake_case						(	self     :						Optional[Any])    ->							List[str]:
								def check_hidden_states_output(_lowercase     :						Any      ,       _lowercase     :						Optional[Any]      ,       _lowercase     :						Any):
											A_									= model_class(_lowercase)
											model.to(_lowercase)
											model.eval()

											with torch.no_grad():
														A_									= model(**self._prepare_for_class(_lowercase      ,       _lowercase))

											A_									= outputs.hidden_states

											A_									= len(self.model_tester.depth)
											self.assertEqual(len(_lowercase)      ,       _lowercase)

											# 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(_lowercase      ,       _lowercase      ,       _lowercase)

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

											check_hidden_states_output(_lowercase      ,       _lowercase      ,       _lowercase)



					def     __snake_case						(	self     :						str)    ->							int:
								A_									= self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*_lowercase)



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



					@slow
					def     __snake_case						(	self     :						Optional[int])    ->							Dict:
								for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											A_									= CvtModel.from_pretrained(_lowercase)
											self.assertIsNotNone(_lowercase)





def 			lowerCamelCase(							)		->		Optional[Any]:
			A_									= Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'     )
			return image






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



					'''simple docstring'''





					@cached_property
					def     __snake_case						(	self     :						str)    ->							Optional[int]:
								return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0])



					@slow
					def     __snake_case						(	self     :						Optional[int])    ->							Union[str, Any]:
								A_									= CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]).to(_lowercase)

								A_									= self.default_image_processor
								A_									= prepare_img()
								A_									= image_processor(images=_lowercase      ,       return_tensors='pt').to(_lowercase)

								# forward pass
								with torch.no_grad():
											A_									= model(**_lowercase)

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

								A_									= torch.tensor([0.92_85, 0.90_15, -0.31_50]).to(_lowercase)

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










'''simple docstring'''
import os
import sys

_SCREAMING_SNAKE_CASE			=						os.path.join(os.path.dirname(__file__), "src")
sys.path.append(SRC_DIR)


from transformers import (
    AutoConfig,
    AutoModel,
    AutoModelForCausalLM,
    AutoModelForMaskedLM,
    AutoModelForQuestionAnswering,
    AutoModelForSequenceClassification,
    AutoTokenizer,
    add_start_docstrings,
)


_SCREAMING_SNAKE_CASE			=						[
    "torch",
    "numpy",
    "tokenizers",
    "filelock",
    "requests",
    "tqdm",
    "regex",
    "sentencepiece",
    "sacremoses",
    "importlib_metadata",
    "huggingface_hub",
]





@add_start_docstrings(AutoConfig.__doc__     )
def 			lowerCamelCase(							*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )		->		int:
			return AutoConfig.from_pretrained(*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )





@add_start_docstrings(AutoTokenizer.__doc__     )
def 			lowerCamelCase(							*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )		->		List[str]:
			return AutoTokenizer.from_pretrained(*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )





@add_start_docstrings(AutoModel.__doc__     )
def 			lowerCamelCase(							*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )		->		Union[str, Any]:
			return AutoModel.from_pretrained(*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )





@add_start_docstrings(AutoModelForCausalLM.__doc__     )
def 			lowerCamelCase(							*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )		->		Optional[int]:
			return AutoModelForCausalLM.from_pretrained(*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )





@add_start_docstrings(AutoModelForMaskedLM.__doc__     )
def 			lowerCamelCase(							*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )		->		Dict:
			return AutoModelForMaskedLM.from_pretrained(*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )





@add_start_docstrings(AutoModelForSequenceClassification.__doc__     )
def 			lowerCamelCase(							*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )		->		Union[str, Any]:
			return AutoModelForSequenceClassification.from_pretrained(*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )






@add_start_docstrings(AutoModelForQuestionAnswering.__doc__     )
def 			lowerCamelCase(							*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )		->		str:
			return AutoModelForQuestionAnswering.from_pretrained(*SCREAMING_SNAKE_CASE_       ,**SCREAMING_SNAKE_CASE_     )




