"""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  _snake_case     (					a__		):






						def       lowerCamelCase__     (       self      :			int    ):
													__lowerCamelCase					:		Union[str, Any]		   =     self.config_class(**self.inputs_dict    )
													self.parent.assertTrue(hasattr(UpperCAmelCase							,					"embed_dim"    )    )
													self.parent.assertTrue(hasattr(UpperCAmelCase							,					"num_heads"    )    )

class  _snake_case     :






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






						def       lowerCamelCase__     (       self      :			Optional[int]    ):
													__lowerCamelCase					:		Tuple		   =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

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

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






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







						def       lowerCamelCase__     (       self      :			Tuple    ):
													__lowerCamelCase					:		str		   =     self.prepare_config_and_inputs()
													__lowerCamelCase   ,  __lowerCamelCase   ,  __lowerCamelCase					:		str		   =     config_and_inputs
													__lowerCamelCase					:		Dict		   =     {"pixel_values": pixel_values}
													return config, inputs_dict

@require_torch
class  _snake_case     (					a__				, a__				, 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       lowerCamelCase__     (       self      :			Dict    ):
													__lowerCamelCase					:		str		   =     CvtModelTester(self    )
													__lowerCamelCase					:		Optional[int]		   =     ConfigTester(self							,					config_class=UpperCAmelCase							,					has_text_modality=UpperCAmelCase							,					hidden_size=37    )






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






						@unittest.skip(reason="Cvt does not output attentions"    )
						def       lowerCamelCase__     (       self      :			List[Any]    ):
													pass






						@unittest.skip(reason="Cvt does not use inputs_embeds"    )
						def       lowerCamelCase__     (       self      :			str    ):
													pass






						@unittest.skip(reason="Cvt does not support input and output embeddings"    )
						def       lowerCamelCase__     (       self      :			List[Any]    ):
													pass






						def       lowerCamelCase__     (       self      :			Dict    ):
													__lowerCamelCase   ,  __lowerCamelCase					:		Optional[Any]		   =     self.model_tester.prepare_config_and_inputs_for_common()

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

																				__lowerCamelCase					:		Dict		   =     ["pixel_values"]
																				self.assertListEqual(arg_names[:1]							,					UpperCAmelCase    )






						def       lowerCamelCase__     (       self      :			Optional[Any]    ):
													__lowerCamelCase					:		List[Any]		   =     self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*UpperCAmelCase    )






						def       lowerCamelCase__     (       self      :			Optional[Any]    ):
													def check_hidden_states_output(UpperCAmelCase      :			Any							,					UpperCAmelCase      :			int							,					UpperCAmelCase      :			Optional[Any]    ):
																				__lowerCamelCase					:		int		   =     model_class(UpperCAmelCase    )
																				model.to(UpperCAmelCase    )
																				model.eval()

																				with torch.no_grad():
																											__lowerCamelCase					:		Any		   =     model(**self._prepare_for_class(UpperCAmelCase							,					UpperCAmelCase    )    )

																				__lowerCamelCase					:		str		   =     outputs.hidden_states

																				__lowerCamelCase					:		List[str]		   =     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,
																				    ]							,					)

													__lowerCamelCase   ,  __lowerCamelCase					:		str		   =     self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																				__lowerCamelCase					:		Dict		   =     True
																				check_hidden_states_output(UpperCAmelCase							,					UpperCAmelCase							,					UpperCAmelCase    )

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

																				check_hidden_states_output(UpperCAmelCase							,					UpperCAmelCase							,					UpperCAmelCase    )






						def       lowerCamelCase__     (       self      :			str    ):
													__lowerCamelCase					:		str		   =     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       lowerCamelCase__     (       self      :			Tuple    ):
													pass







						@slow
						def       lowerCamelCase__     (       self      :			int    ):
													for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																				__lowerCamelCase					:		str		   =     CvtModel.from_pretrained(UpperCAmelCase    )
																				self.assertIsNotNone(UpperCAmelCase    )


def    lowercase_   (   ) ->      int:



							'''simple docstring'''

							__lowerCamelCase					:		Union[str, Any]		   =     Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"      )
							return image







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






						@cached_property
						def       lowerCamelCase__     (       self      :			Any    ):
													return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]    )







						@slow
						def       lowerCamelCase__     (       self      :			Tuple    ):
													__lowerCamelCase					:		List[Any]		   =     CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]    ).to(UpperCAmelCase    )

													__lowerCamelCase					:		Any		   =     self.default_image_processor
													__lowerCamelCase					:		List[str]		   =     prepare_img()
													__lowerCamelCase					:		Optional[Any]		   =     image_processor(images=UpperCAmelCase							,					return_tensors="pt"    ).to(UpperCAmelCase    )

													# forward pass
													with torch.no_grad():
																				__lowerCamelCase					:		List[str]		   =     model(**UpperCAmelCase    )

													# verify the logits
													__lowerCamelCase					:		int		   =     torch.Size((1, 1000)    )
													self.assertEqual(outputs.logits.shape							,					UpperCAmelCase    )

													__lowerCamelCase					:		Optional[Any]		   =     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"""





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tensorflow_text_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__A  =				{
    '''configuration_bert''': ['''BERT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BertConfig''', '''BertOnnxConfig'''],
    '''tokenization_bert''': ['''BasicTokenizer''', '''BertTokenizer''', '''WordpieceTokenizer'''],
}

try:
				if not is_tokenizers_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__A  =				['''BertTokenizerFast''']

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__A  =				[
				    '''BERT_PRETRAINED_MODEL_ARCHIVE_LIST''',
				    '''BertForMaskedLM''',
				    '''BertForMultipleChoice''',
				    '''BertForNextSentencePrediction''',
				    '''BertForPreTraining''',
				    '''BertForQuestionAnswering''',
				    '''BertForSequenceClassification''',
				    '''BertForTokenClassification''',
				    '''BertLayer''',
				    '''BertLMHeadModel''',
				    '''BertModel''',
				    '''BertPreTrainedModel''',
				    '''load_tf_weights_in_bert''',
				]

try:
				if not is_tf_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__A  =				[
				    '''TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST''',
				    '''TFBertEmbeddings''',
				    '''TFBertForMaskedLM''',
				    '''TFBertForMultipleChoice''',
				    '''TFBertForNextSentencePrediction''',
				    '''TFBertForPreTraining''',
				    '''TFBertForQuestionAnswering''',
				    '''TFBertForSequenceClassification''',
				    '''TFBertForTokenClassification''',
				    '''TFBertLMHeadModel''',
				    '''TFBertMainLayer''',
				    '''TFBertModel''',
				    '''TFBertPreTrainedModel''',
				]
try:
				if not is_tensorflow_text_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__A  =				['''TFBertTokenizer''']

try:
				if not is_flax_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__A  =				[
				    '''FlaxBertForCausalLM''',
				    '''FlaxBertForMaskedLM''',
				    '''FlaxBertForMultipleChoice''',
				    '''FlaxBertForNextSentencePrediction''',
				    '''FlaxBertForPreTraining''',
				    '''FlaxBertForQuestionAnswering''',
				    '''FlaxBertForSequenceClassification''',
				    '''FlaxBertForTokenClassification''',
				    '''FlaxBertModel''',
				    '''FlaxBertPreTrainedModel''',
				]

if TYPE_CHECKING:
				from .configuration_bert import BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, BertConfig, BertOnnxConfig
				from .tokenization_bert import BasicTokenizer, BertTokenizer, WordpieceTokenizer

				try:
								if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_bert_fast import BertTokenizerFast

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_bert import (
								    BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
								    BertForMaskedLM,
								    BertForMultipleChoice,
								    BertForNextSentencePrediction,
								    BertForPreTraining,
								    BertForQuestionAnswering,
								    BertForSequenceClassification,
								    BertForTokenClassification,
								    BertLayer,
								    BertLMHeadModel,
								    BertModel,
								    BertPreTrainedModel,
								    load_tf_weights_in_bert,
								)

				try:
								if not is_tf_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_tf_bert import (
								    TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
								    TFBertEmbeddings,
								    TFBertForMaskedLM,
								    TFBertForMultipleChoice,
								    TFBertForNextSentencePrediction,
								    TFBertForPreTraining,
								    TFBertForQuestionAnswering,
								    TFBertForSequenceClassification,
								    TFBertForTokenClassification,
								    TFBertLMHeadModel,
								    TFBertMainLayer,
								    TFBertModel,
								    TFBertPreTrainedModel,
								)

				try:
								if not is_tensorflow_text_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_bert_tf import TFBertTokenizer

				try:
								if not is_flax_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_flax_bert import (
								    FlaxBertForCausalLM,
								    FlaxBertForMaskedLM,
								    FlaxBertForMultipleChoice,
								    FlaxBertForNextSentencePrediction,
								    FlaxBertForPreTraining,
								    FlaxBertForQuestionAnswering,
								    FlaxBertForSequenceClassification,
								    FlaxBertForTokenClassification,
								    FlaxBertModel,
								    FlaxBertPreTrainedModel,
								)

else:
				import sys

				__A  =				_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)