





"""simple docstring"""





from __future__ import annotations

import inspect
import unittest
from math import floor

import numpy as np

from transformers import CvtConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
							import tensorflow as tf

							from transformers import TFCvtForImageClassification, TFCvtModel
							from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor
class 		SCREAMING_SNAKE_CASE_				(     snake_case__ ):
				"""simple docstring"""




				def        __lowercase	(  self	:List[str]      ):
											__lowerCamelCase   :   Union[str, Any]       =self.config_class(**self.inputs_dict      )
											self.parent.assertTrue(hasattr(__lowercase    ,     '''embed_dim'''      )      )
											self.parent.assertTrue(hasattr(__lowercase    ,     '''num_heads'''      )      )

class 		SCREAMING_SNAKE_CASE_				:
				"""simple docstring"""




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




				def        __lowercase	(  self	:List[str]      ):
											__lowerCamelCase   :   Optional[int]       =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

											__lowerCamelCase   :   str       =None
											if self.use_labels:
																		# create a random int32 tensor of given shape
																		__lowerCamelCase   :   List[str]       =ids_tensor([self.batch_size]    ,     self.num_labels      )

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




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




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

@require_tf
class 		SCREAMING_SNAKE_CASE_				(     snake_case__							,	snake_case__							,	unittest.TestCase ):
				"""simple docstring"""
				__snake_case :							Dict									=		(TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
				__snake_case :							Union[str, Any]									=		(
				    {"""feature-extraction""": TFCvtModel, """image-classification""": TFCvtForImageClassification}
				    if is_tf_available()
				    else {}
				)
				__snake_case :							Optional[int]									=		False
				__snake_case :							List[Any]									=		False
				__snake_case :							Optional[int]									=		False
				__snake_case :							str									=		False
				__snake_case :							Dict									=		False




				def        __lowercase	(  self	:List[Any]      ):
											__lowerCamelCase   :   Tuple       =TFCvtModelTester(self      )
											__lowerCamelCase   :   Optional[int]       =TFCvtConfigTester(self    ,     config_class=__lowercase    ,     has_text_modality=__lowercase    ,     hidden_size=37      )




				def        __lowercase	(  self	:Any      ):
											self.config_tester.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()




				@unittest.skip(reason='''Cvt does not output attentions'''      )
				def        __lowercase	(  self	:Optional[Any]      ):
											pass




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




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




				@unittest.skipIf(
				    not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''      )      ) == 0    ,     reason='''TF does not support backprop for grouped convolutions on CPU.'''    ,     )
				def        __lowercase	(  self	:Any      ):
											super().test_dataset_conversion()




				@unittest.skipIf(
				    not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''      )      ) == 0    ,     reason='''TF does not support backprop for grouped convolutions on CPU.'''    ,     )
				@slow
				def        __lowercase	(  self	:Dict      ):
											super().test_keras_fit()




				@unittest.skip(reason='''Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8'''      )
				def        __lowercase	(  self	:int      ):
											__lowerCamelCase   :   int       =tf.keras.mixed_precision.Policy('''mixed_float16'''      )
											tf.keras.mixed_precision.set_global_policy(__lowercase      )
											super().test_keras_fit()
											tf.keras.mixed_precision.set_global_policy('''float32'''      )




				def        __lowercase	(  self	:Dict      ):
											__lowerCamelCase     ,    __lowerCamelCase   :   Dict       =self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		__lowerCamelCase   :   int       =model_class(__lowercase      )
																		__lowerCamelCase   :   Optional[int]       =inspect.signature(model.call      )
																		# signature.parameters is an OrderedDict => so arg_names order is deterministic
																		__lowerCamelCase   :   Union[str, Any]       =[*signature.parameters.keys()]

																		__lowerCamelCase   :   Dict       =['''pixel_values''']
																		self.assertListEqual(arg_names[:1]    ,     __lowercase      )




				def        __lowercase	(  self	:List[str]      ):
											def check_hidden_states_output(__lowercase	:Any    ,     __lowercase	:List[Any]    ,     __lowercase	:Union[str, Any]      ):
																		__lowerCamelCase   :   Optional[Any]       =model_class(__lowercase      )

																		__lowerCamelCase   :   Optional[int]       =model(**self._prepare_for_class(__lowercase    ,     __lowercase      )      )
																		__lowerCamelCase   :   Tuple       =outputs.hidden_states

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

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

											for model_class in self.all_model_classes:
																		__lowerCamelCase   :   str       =True
																		check_hidden_states_output(__lowercase    ,     __lowercase    ,     __lowercase      )

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

																		check_hidden_states_output(__lowercase    ,     __lowercase    ,     __lowercase      )




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




				def        __lowercase	(  self	:List[str]      ):
											__lowerCamelCase   :   Dict       =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*__lowercase      )




				@slow
				def        __lowercase	(  self	:List[Any]      ):
											for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		__lowerCamelCase   :   int       =TFCvtModel.from_pretrained(__lowercase      )
																		self.assertIsNotNone(__lowercase      )



def 					lowerCAmelCase_      (   ):


							'''simple docstring'''







							__lowerCamelCase   :   Optional[int]       =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
							return image
@require_tf
@require_vision
class 		SCREAMING_SNAKE_CASE_				(     unittest.TestCase ):
				"""simple docstring"""




				@cached_property
				def        __lowercase	(  self	:List[str]      ):
											return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]      )




				@slow
				def        __lowercase	(  self	:List[Any]      ):
											__lowerCamelCase   :   str       =TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]      )

											__lowerCamelCase   :   List[Any]       =self.default_image_processor
											__lowerCamelCase   :   Union[str, Any]       =prepare_img()
											__lowerCamelCase   :   List[Any]       =image_processor(images=__lowercase    ,     return_tensors='''tf'''      )

											# forward pass
											__lowerCamelCase   :   Optional[int]       =model(**__lowercase      )

											# verify the logits
											__lowerCamelCase   :   Union[str, Any]       =tf.TensorShape((1, 1000)      )
											self.assertEqual(outputs.logits.shape    ,     __lowercase      )

											__lowerCamelCase   :   List[Any]       =tf.constant([0.9285, 0.9015, -0.3150]      )
											self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()    ,     __lowercase    ,     atol=1e-4      )      )











"""simple docstring"""





import contextlib
import importlib
import io
import unittest

import transformers

# Try to import everything from transformers to ensure every object can be loaded.
from transformers import *  # noqa F406
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, require_tf, require_torch
from transformers.utils import ContextManagers, find_labels, is_flax_available, is_tf_available, is_torch_available


if is_torch_available():
							from transformers import BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification

if is_tf_available():
							from transformers import TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification

if is_flax_available():
							from transformers import FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification


_UpperCamelCase					      =   DUMMY_UNKNOWN_IDENTIFIER
# An actual model hosted on huggingface.co

_UpperCamelCase					      =   'main'
# Default branch name
_UpperCamelCase					      =   'f2c752cfc5c0ab6f4bdec59acea69eefbee381c2'
# One particular commit (not the top of `main`)
_UpperCamelCase					      =   'aaaaaaa'
# This commit does not exist, so we should 404.

_UpperCamelCase					      =   'd9e9f15bc825e4b2c9249e9578f884bbcb5e3684'
# Sha-1 of config.json on the top of `main`, for checking purposes
_UpperCamelCase					      =   '4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d3'



@contextlib.contextmanager
def 					lowerCAmelCase_      (   ):


							'''simple docstring'''







							print('''Welcome!''' )
							yield
							print('''Bye!''' )



@contextlib.contextmanager
def 					lowerCAmelCase_      (   ):


							'''simple docstring'''







							print('''Bonjour!''' )
							yield
							print('''Au revoir!''' )
class 		SCREAMING_SNAKE_CASE_				(     unittest.TestCase ):
				"""simple docstring"""




				def        __lowercase	(  self	:str      ):
											# If the spec is missing, importlib would not be able to import the module dynamically.
											assert transformers.__spec__ is not None
											assert importlib.util.find_spec('''transformers'''      ) is not None

class 		SCREAMING_SNAKE_CASE_				(     unittest.TestCase ):
				"""simple docstring"""




				@unittest.mock.patch('''sys.stdout'''    ,     new_callable=io.StringIO      )
				def        __lowercase	(  self	:str    ,     __lowercase	:str      ):
											with ContextManagers([]      ):
																		print('''Transformers are awesome!'''      )
											# The print statement adds a new line at the end of the output
											self.assertEqual(mock_stdout.getvalue()    ,     '''Transformers are awesome!\n'''      )




				@unittest.mock.patch('''sys.stdout'''    ,     new_callable=io.StringIO      )
				def        __lowercase	(  self	:Any    ,     __lowercase	:Tuple      ):
											with ContextManagers([context_en()]      ):
																		print('''Transformers are awesome!'''      )
											# The output should be wrapped with an English welcome and goodbye
											self.assertEqual(mock_stdout.getvalue()    ,     '''Welcome!\nTransformers are awesome!\nBye!\n'''      )




				@unittest.mock.patch('''sys.stdout'''    ,     new_callable=io.StringIO      )
				def        __lowercase	(  self	:List[Any]    ,     __lowercase	:Optional[Any]      ):
											with ContextManagers([context_fr(), context_en()]      ):
																		print('''Transformers are awesome!'''      )
											# The output should be wrapped with an English and French welcome and goodbye
											self.assertEqual(mock_stdout.getvalue()    ,     '''Bonjour!\nWelcome!\nTransformers are awesome!\nBye!\nAu revoir!\n'''      )




				@require_torch
				def        __lowercase	(  self	:Tuple      ):
											self.assertEqual(find_labels(__lowercase      )    ,     ['''labels''']      )
											self.assertEqual(find_labels(__lowercase      )    ,     ['''labels''', '''next_sentence_label''']      )
											self.assertEqual(find_labels(__lowercase      )    ,     ['''start_positions''', '''end_positions''']      )
											class 		SCREAMING_SNAKE_CASE_				(     snake_case__ ):
															"""simple docstring"""
															pass

											self.assertEqual(find_labels(__lowercase      )    ,     ['''labels''']      )




				@require_tf
				def        __lowercase	(  self	:str      ):
											self.assertEqual(find_labels(__lowercase      )    ,     ['''labels''']      )
											self.assertEqual(find_labels(__lowercase      )    ,     ['''labels''', '''next_sentence_label''']      )
											self.assertEqual(find_labels(__lowercase      )    ,     ['''start_positions''', '''end_positions''']      )
											class 		SCREAMING_SNAKE_CASE_				(     snake_case__ ):
															"""simple docstring"""
															pass

											self.assertEqual(find_labels(__lowercase      )    ,     ['''labels''']      )




				@require_flax
				def        __lowercase	(  self	:str      ):
											# Flax models don't have labels
											self.assertEqual(find_labels(__lowercase      )    ,     []      )
											self.assertEqual(find_labels(__lowercase      )    ,     []      )
											self.assertEqual(find_labels(__lowercase      )    ,     []      )
											class 		SCREAMING_SNAKE_CASE_				(     snake_case__ ):
															"""simple docstring"""
															pass

											self.assertEqual(find_labels(__lowercase      )    ,     []      )




