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




				def 	__snake_case							(							self	:				List[str]      ):
										snake_case       :     Optional[Any] =    self.config_class(**self.inputs_dict      )
										self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE_,    '''embed_dim'''      )      )
										self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE_,    '''num_heads'''      )      )




class 						a   :




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




				def 	__snake_case							(							self	:				List[Any]      ):
										snake_case       :     Union[str, Any] =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

										snake_case       :     List[str] =    None
										if self.use_labels:
																snake_case       :     str =    ids_tensor([self.batch_size],    self.num_labels      )

										snake_case       :     str =    self.get_config()
										return config, pixel_values, labels




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




				def 	__snake_case							(							self	:				Optional[int]      ):
										snake_case       :     Any =    self.prepare_config_and_inputs()
										snake_case,					snake_case,					snake_case       :     int =    config_and_inputs
										snake_case       :     Any =    {'''pixel_values''': pixel_values}
										return config, inputs_dict




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

				_snake_case		    = False
				_snake_case		    = False
				_snake_case		    = False
				_snake_case		    = False
				_snake_case		    = False




				def 	__snake_case							(							self	:				Any      ):
										snake_case       :     Optional[int] =    CvtModelTester(self      )
										snake_case       :     Any =    ConfigTester(self,    config_class=SCREAMING_SNAKE_CASE_,    has_text_modality=SCREAMING_SNAKE_CASE_,    hidden_size=37      )




				def 	__snake_case							(							self	:				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	:				int      ):
										return




				@unittest.skip(reason='''Cvt does not output attentions'''      )
				def 	__snake_case							(							self	:				int      ):
										pass




				@unittest.skip(reason='''Cvt does not use inputs_embeds'''      )
				def 	__snake_case							(							self	:				int      ):
										pass




				@unittest.skip(reason='''Cvt does not support input and output embeddings'''      )
				def 	__snake_case							(							self	:				int      ):
										pass




				def 	__snake_case							(							self	:				List[str]      ):
										snake_case,					snake_case       :     Optional[int] =    self.model_tester.prepare_config_and_inputs_for_common()

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

																snake_case       :     Optional[int] =    ['''pixel_values''']
																self.assertListEqual(arg_names[:1],    SCREAMING_SNAKE_CASE_      )




				def 	__snake_case							(							self	:				int      ):
										snake_case       :     Optional[int] =    self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_      )




				def 	__snake_case							(							self	:				Union[str, Any]      ):
										def check_hidden_states_output(SCREAMING_SNAKE_CASE_	:				List[Any],    SCREAMING_SNAKE_CASE_	:				str,    SCREAMING_SNAKE_CASE_	:				Dict      ):
																snake_case       :     Union[str, Any] =    model_class(SCREAMING_SNAKE_CASE_      )
																model.to(SCREAMING_SNAKE_CASE_      )
																model.eval()

																with torch.no_grad():
																						snake_case       :     List[Any] =    model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_,    SCREAMING_SNAKE_CASE_      )      )

																snake_case       :     List[Any] =    outputs.hidden_states

																snake_case       :     int =    len(self.model_tester.depth      )
																self.assertEqual(len(SCREAMING_SNAKE_CASE_      ),    SCREAMING_SNAKE_CASE_      )

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

										snake_case,					snake_case       :     Union[str, Any] =    self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																snake_case       :     Any =    True
																check_hidden_states_output(SCREAMING_SNAKE_CASE_,    SCREAMING_SNAKE_CASE_,    SCREAMING_SNAKE_CASE_      )

																# check that output_hidden_states also work using config
																del inputs_dict["output_hidden_states"]
																snake_case       :     Any =    True

																check_hidden_states_output(SCREAMING_SNAKE_CASE_,    SCREAMING_SNAKE_CASE_,    SCREAMING_SNAKE_CASE_      )




				def 	__snake_case							(							self	:				Optional[Any]      ):
										snake_case       :     List[str] =    self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_      )




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




				@slow
				def 	__snake_case							(							self	:				List[Any]      ):
										for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																snake_case       :     List[str] =    CvtModel.from_pretrained(SCREAMING_SNAKE_CASE_      )
																self.assertIsNotNone(SCREAMING_SNAKE_CASE_      )




def      A (       ):
						snake_case       :     Optional[Any] =    Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
						return image





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




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




				@slow
				def 	__snake_case							(							self	:				Optional[Any]      ):
										snake_case       :     List[str] =    CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]      ).to(SCREAMING_SNAKE_CASE_      )

										snake_case       :     Union[str, Any] =    self.default_image_processor
										snake_case       :     int =    prepare_img()
										snake_case       :     Any =    image_processor(images=SCREAMING_SNAKE_CASE_,    return_tensors='''pt'''      ).to(SCREAMING_SNAKE_CASE_      )

										# forward pass
										with torch.no_grad():
																snake_case       :     Tuple =    model(**SCREAMING_SNAKE_CASE_      )

										# verify the logits
										snake_case       :     int =    torch.Size((1, 10_00)      )
										self.assertEqual(outputs.logits.shape,    SCREAMING_SNAKE_CASE_      )

										snake_case       :     Optional[int] =    torch.tensor([0.9285, 0.9015, -0.3150]      ).to(SCREAMING_SNAKE_CASE_      )

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



'''simple docstring'''





import os
import sys

UpperCAmelCase    					=   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,
)


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

@add_start_docstrings(AutoConfig.__doc__       )
def      A (       *A_							:					Optional[Any]    ,							**A_							:					List[str]       ):
						return AutoConfig.from_pretrained(*A_    ,							**A_       )

@add_start_docstrings(AutoTokenizer.__doc__       )
def      A (       *A_							:					Dict    ,							**A_							:					str       ):
						return AutoTokenizer.from_pretrained(*A_    ,							**A_       )

@add_start_docstrings(AutoModel.__doc__       )
def      A (       *A_							:					Union[str, Any]    ,							**A_							:					Optional[Any]       ):
						return AutoModel.from_pretrained(*A_    ,							**A_       )

@add_start_docstrings(AutoModelForCausalLM.__doc__       )
def      A (       *A_							:					str    ,							**A_							:					Optional[Any]       ):
						return AutoModelForCausalLM.from_pretrained(*A_    ,							**A_       )

@add_start_docstrings(AutoModelForMaskedLM.__doc__       )
def      A (       *A_							:					List[str]    ,							**A_							:					Optional[Any]       ):
						return AutoModelForMaskedLM.from_pretrained(*A_    ,							**A_       )

@add_start_docstrings(AutoModelForSequenceClassification.__doc__       )
def      A (       *A_							:					Tuple    ,							**A_							:					List[str]       ):
						return AutoModelForSequenceClassification.from_pretrained(*A_    ,							**A_       )





@add_start_docstrings(AutoModelForQuestionAnswering.__doc__       )
def      A (       *A_							:					Tuple    ,							**A_							:					Any       ):
						return AutoModelForQuestionAnswering.from_pretrained(*A_    ,							**A_       )

