






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

					'''simple docstring'''

					def       lowerCAmelCase				(  self				:	Optional[Any]							)			->  Tuple:


												"""simple docstring"""

												__lowercase      :							str				=     self.config_class(**self.inputs_dict							)
												self.parent.assertTrue(hasattr(__a		,     """embed_dim"""							)							)
												self.parent.assertTrue(hasattr(__a		,     """num_heads"""							)							)







class 		lowerCAmelCase     :

					'''simple docstring'''

					def __init__(  self				:	List[str]		,     __a				:	List[Any]		,     __a				:	Dict=13		,     __a				:	Optional[Any]=64		,     __a				:	Optional[int]=3		,     __a				:	Dict=[16, 48, 96]		,     __a				:	Dict=[1, 3, 6]		,     __a				:	Dict=[1, 2, 10]		,     __a				:	Tuple=[7, 3, 3]		,     __a				:	List[str]=[4, 2, 2]		,     __a				:	Optional[int]=[2, 1, 1]		,     __a				:	List[Any]=[2, 2, 2]		,     __a				:	List[str]=[False, False, True]		,     __a				:	int=[0.0, 0.0, 0.0]		,     __a				:	Union[str, Any]=0.02		,     __a				:	Any=1E-12		,     __a				:	Union[str, Any]=True		,     __a				:	List[Any]=True		,     __a				:	List[Any]=2		,     )			->  Union[str, Any]:


												"""simple docstring"""

												__lowercase      :							Dict				=     parent
												__lowercase      :							Optional[Any]				=     batch_size
												__lowercase      :							int				=     image_size
												__lowercase      :							str				=     patch_sizes
												__lowercase      :							Dict				=     patch_stride
												__lowercase      :							Optional[Any]				=     patch_padding
												__lowercase      :							Dict				=     is_training
												__lowercase      :							Tuple				=     use_labels
												__lowercase      :							List[str]				=     num_labels
												__lowercase      :							Union[str, Any]				=     num_channels
												__lowercase      :							Any				=     embed_dim
												__lowercase      :							int				=     num_heads
												__lowercase      :							Dict				=     stride_kv
												__lowercase      :							Optional[Any]				=     depth
												__lowercase      :							int				=     cls_token
												__lowercase      :							Dict				=     attention_drop_rate
												__lowercase      :							Union[str, Any]				=     initializer_range
												__lowercase      :							List[str]				=     layer_norm_eps



					def       lowerCAmelCase				(  self				:	Any							)			->  Any:


												"""simple docstring"""

												__lowercase      :							str				=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

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

												__lowercase      :							Any				=     self.get_config()
												return config, pixel_values, labels



					def       lowerCAmelCase				(  self				:	str							)			->  Optional[int]:


												"""simple docstring"""

												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				:	Dict		,     __a				:	Any		,     __a				:	List[str]		,     __a				:	Union[str, Any]							)			->  Optional[int]:


												"""simple docstring"""

												__lowercase      :							Tuple				=     TFCvtModel(config=__a							)
												__lowercase      :							str				=     model(__a		,     training=__a							)
												__lowercase      :							Dict				=     (self.image_size, self.image_size)
												__lowercase			,			__lowercase      :							Optional[int]				=     image_size[0], image_size[1]
												for i in range(len(self.depth							)							):
																			__lowercase      :							Dict				=     floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1							)
																			__lowercase      :							Optional[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       lowerCAmelCase				(  self				:	int		,     __a				:	List[str]		,     __a				:	Optional[int]		,     __a				:	int							)			->  List[Any]:


												"""simple docstring"""

												__lowercase      :							Union[str, Any]				=     self.num_labels
												__lowercase      :							Optional[int]				=     TFCvtForImageClassification(__a							)
												__lowercase      :							Optional[Any]				=     model(__a		,     labels=__a		,     training=__a							)
												self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_labels)							)



					def       lowerCAmelCase				(  self				:	List[str]							)			->  str:


												"""simple docstring"""

												__lowercase      :							Any				=     self.prepare_config_and_inputs()
												__lowercase			,			__lowercase			,			__lowercase      :							Optional[Any]				=     config_and_inputs
												__lowercase      :							str				=     {"""pixel_values""": pixel_values}
												return config, inputs_dict







@require_tf
class 		lowerCAmelCase     ( __a      ,       __a      ,       unittest.TestCase			):

					'''simple docstring'''

					_A						:			Dict									=      (TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
					_A						:			Dict									=      (
					    {'''feature-extraction''': TFCvtModel, '''image-classification''': TFCvtForImageClassification}
					    if is_tf_available()
					    else {}
					)
					_A						:			int									=      False
					_A						:			Any									=      False
					_A						:			List[str]									=      False
					_A						:			Optional[Any]									=      False
					_A						:			Tuple									=      False



					def       lowerCAmelCase				(  self				:	Union[str, Any]							)			->  str:


												"""simple docstring"""

												__lowercase      :							Optional[Any]				=     TFCvtModelTester(self							)
												__lowercase      :							str				=     TFCvtConfigTester(self		,     config_class=__a		,     has_text_modality=__a		,     hidden_size=37							)



					def       lowerCAmelCase				(  self				:	Dict							)			->  Any:


												"""simple docstring"""

												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       lowerCAmelCase				(  self				:	Union[str, Any]							)			->  str:


												"""simple docstring"""

												pass



					@unittest.skip(reason="""Cvt does not use inputs_embeds"""							)
					def       lowerCAmelCase				(  self				:	List[str]							)			->  Union[str, Any]:


												"""simple docstring"""

												pass



					@unittest.skip(reason="""Cvt does not support input and output embeddings"""							)
					def       lowerCAmelCase				(  self				:	Tuple							)			->  Optional[Any]:


												"""simple docstring"""

												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       lowerCAmelCase				(  self				:	Tuple							)			->  Optional[int]:


												"""simple docstring"""

												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       lowerCAmelCase				(  self				:	List[Any]							)			->  Any:


												"""simple docstring"""

												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       lowerCAmelCase				(  self				:	Tuple							)			->  Any:


												"""simple docstring"""

												__lowercase      :							List[str]				=     tf.keras.mixed_precision.Policy("""mixed_float16"""							)
												tf.keras.mixed_precision.set_global_policy(__a							)
												super().test_keras_fit()
												tf.keras.mixed_precision.set_global_policy("""float32"""							)



					def       lowerCAmelCase				(  self				:	Dict							)			->  Any:


												"""simple docstring"""

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

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

																			__lowercase      :							List[Any]				=     ["""pixel_values"""]
																			self.assertListEqual(arg_names[:1]		,     __a							)



					def       lowerCAmelCase				(  self				:	Dict							)			->  Union[str, Any]:


												"""simple docstring"""

												def check_hidden_states_output(__a				:	Tuple		,     __a				:	int		,     __a				:	List[Any]							):
																			__lowercase      :							Optional[Any]				=     model_class(__a							)

																			__lowercase      :							str				=     model(**self._prepare_for_class(__a		,     __a							)							)
																			__lowercase      :							str				=     outputs.hidden_states

																			__lowercase      :							int				=     len(self.model_tester.depth							)
																			self.assertEqual(len(__a							)		,     __a							)

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

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

												for model_class in self.all_model_classes:
																			__lowercase      :							List[str]				=     True
																			check_hidden_states_output(__a		,     __a		,     __a							)

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

																			check_hidden_states_output(__a		,     __a		,     __a							)



					def       lowerCAmelCase				(  self				:	Any							)			->  List[Any]:


												"""simple docstring"""

												__lowercase      :							Tuple				=     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*__a							)



					def       lowerCAmelCase				(  self				:	Any							)			->  Tuple:


												"""simple docstring"""

												__lowercase      :							Any				=     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*__a							)



					@slow
					def       lowerCAmelCase				(  self				:	Optional[int]							)			->  int:


												"""simple docstring"""

												for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			__lowercase      :							List[str]				=     TFCvtModel.from_pretrained(__a							)
																			self.assertIsNotNone(__a							)







def       snake_case_		(     ):
							__lowercase      :							Any				=     Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      )
							return image



@require_tf
@require_vision
class 		lowerCAmelCase     ( unittest.TestCase			):

					'''simple docstring'''

					@cached_property
					def       lowerCAmelCase				(  self				:	str							)			->  int:


												"""simple docstring"""

												return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]							)



					@slow
					def       lowerCAmelCase				(  self				:	Any							)			->  Optional[int]:


												"""simple docstring"""

												__lowercase      :							int				=     TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]							)

												__lowercase      :							Any				=     self.default_image_processor
												__lowercase      :							Optional[int]				=     prepare_img()
												__lowercase      :							Optional[Any]				=     image_processor(images=__a		,     return_tensors="""tf"""							)

												# forward pass
												__lowercase      :							List[str]				=     model(**__a							)

												# verify the logits
												__lowercase      :							str				=     tf.TensorShape((1, 1000)							)
												self.assertEqual(outputs.logits.shape		,     __a							)

												__lowercase      :							Dict				=     tf.constant([0.9285, 0.9015, -0.3150]							)
												self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()		,     __a		,     atol=1E-4							)							)







from typing import TYPE_CHECKING

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


lowerCamelCase : int						   =       {
    '''configuration_funnel''': ['''FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''FunnelConfig'''],
    '''convert_funnel_original_tf_checkpoint_to_pytorch''': [],
    '''tokenization_funnel''': ['''FunnelTokenizer'''],
}

try:
	if not is_tokenizers_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	lowerCamelCase : List[str]						   =       ['''FunnelTokenizerFast''']

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	lowerCamelCase : Union[str, Any]						   =       [
	    '''FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST''',
	    '''FunnelBaseModel''',
	    '''FunnelForMaskedLM''',
	    '''FunnelForMultipleChoice''',
	    '''FunnelForPreTraining''',
	    '''FunnelForQuestionAnswering''',
	    '''FunnelForSequenceClassification''',
	    '''FunnelForTokenClassification''',
	    '''FunnelModel''',
	    '''FunnelPreTrainedModel''',
	    '''load_tf_weights_in_funnel''',
	]

try:
	if not is_tf_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	lowerCamelCase : str						   =       [
	    '''TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST''',
	    '''TFFunnelBaseModel''',
	    '''TFFunnelForMaskedLM''',
	    '''TFFunnelForMultipleChoice''',
	    '''TFFunnelForPreTraining''',
	    '''TFFunnelForQuestionAnswering''',
	    '''TFFunnelForSequenceClassification''',
	    '''TFFunnelForTokenClassification''',
	    '''TFFunnelModel''',
	    '''TFFunnelPreTrainedModel''',
	]


if TYPE_CHECKING:
	from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig
	from .tokenization_funnel import FunnelTokenizer

	try:
		if not is_tokenizers_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_funnel_fast import FunnelTokenizerFast

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_funnel import (
		    FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST,
		    FunnelBaseModel,
		    FunnelForMaskedLM,
		    FunnelForMultipleChoice,
		    FunnelForPreTraining,
		    FunnelForQuestionAnswering,
		    FunnelForSequenceClassification,
		    FunnelForTokenClassification,
		    FunnelModel,
		    FunnelPreTrainedModel,
		    load_tf_weights_in_funnel,
		)

	try:
		if not is_tf_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_tf_funnel import (
		    TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TFFunnelBaseModel,
		    TFFunnelForMaskedLM,
		    TFFunnelForMultipleChoice,
		    TFFunnelForPreTraining,
		    TFFunnelForQuestionAnswering,
		    TFFunnelForSequenceClassification,
		    TFFunnelForTokenClassification,
		    TFFunnelModel,
		    TFFunnelPreTrainedModel,
		)

else:
	import sys

	lowerCamelCase : Any						   =       _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)