





'''simple docstring'''





from __future__ import annotations

import inspect
import unittest

from transformers import ViTConfig
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 TFViTForImageClassification, TFViTModel


if is_vision_available():
			from PIL import Image

			from transformers import ViTImageProcessor






class       __A					:

			'''simple docstring'''







			def __init__(					self ,						__lowerCAmelCase ,						__lowerCAmelCase=1_3 ,						__lowerCAmelCase=3_0 ,						__lowerCAmelCase=2 ,						__lowerCAmelCase=3 ,						__lowerCAmelCase=True ,						__lowerCAmelCase=True ,						__lowerCAmelCase=3_2 ,						__lowerCAmelCase=2 ,						__lowerCAmelCase=4 ,						__lowerCAmelCase=3_7 ,						__lowerCAmelCase="gelu" ,						__lowerCAmelCase=0.1 ,						__lowerCAmelCase=0.1 ,						__lowerCAmelCase=1_0 ,						__lowerCAmelCase=0.02 ,						__lowerCAmelCase=3 ,						__lowerCAmelCase=None ,						):



				'''simple docstring'''







				lowerCamelCase__              =	parent
				lowerCamelCase__              =	batch_size
				lowerCamelCase__              =	image_size
				lowerCamelCase__              =	patch_size
				lowerCamelCase__              =	num_channels
				lowerCamelCase__              =	is_training
				lowerCamelCase__              =	use_labels
				lowerCamelCase__              =	hidden_size
				lowerCamelCase__              =	num_hidden_layers
				lowerCamelCase__              =	num_attention_heads
				lowerCamelCase__              =	intermediate_size
				lowerCamelCase__              =	hidden_act
				lowerCamelCase__              =	hidden_dropout_prob
				lowerCamelCase__              =	attention_probs_dropout_prob
				lowerCamelCase__              =	type_sequence_label_size
				lowerCamelCase__              =	initializer_range
				lowerCamelCase__              =	scope

				# in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
				lowerCamelCase__              =	(image_size // patch_size) ** 2
				lowerCamelCase__              =	num_patches + 1





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

				lowerCamelCase__              =	None
				if self.use_labels:
					lowerCamelCase__              =	ids_tensor([self.batch_size] ,						self.type_sequence_label_size							)

				lowerCamelCase__              =	self.get_config()

				return config, pixel_values, labels





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				return ViTConfig(
				    image_size=self.image_size ,						patch_size=self.patch_size ,						num_channels=self.num_channels ,						hidden_size=self.hidden_size ,						num_hidden_layers=self.num_hidden_layers ,						num_attention_heads=self.num_attention_heads ,						intermediate_size=self.intermediate_size ,						hidden_act=self.hidden_act ,						hidden_dropout_prob=self.hidden_dropout_prob ,						attention_probs_dropout_prob=self.attention_probs_dropout_prob ,						is_decoder=__A ,						initializer_range=self.initializer_range ,						)





			def 	__lowerCamelCase       (					self ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase							):



				'''simple docstring'''







				lowerCamelCase__              =	TFViTModel(config=__A							)
				lowerCamelCase__              =	model(__A ,						training=__A							)
				self.parent.assertEqual(result.last_hidden_state.shape ,						(self.batch_size, self.seq_length, self.hidden_size)							)

				# Test with an image with different size than the one specified in config.
				lowerCamelCase__              =	self.image_size // 2
				lowerCamelCase__              =	pixel_values[:, :, :image_size, :image_size]
				lowerCamelCase__              =	model(__A ,						interpolate_pos_encoding=__A ,						training=__A							)
				lowerCamelCase__              =	(image_size // self.patch_size) ** 2 + 1
				self.parent.assertEqual(result.last_hidden_state.shape ,						(self.batch_size, seq_length, self.hidden_size)							)





			def 	__lowerCamelCase       (					self ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase							):



				'''simple docstring'''







				lowerCamelCase__              =	self.type_sequence_label_size
				lowerCamelCase__              =	TFViTForImageClassification(__A							)
				lowerCamelCase__              =	model(__A ,						labels=__A ,						training=__A							)
				self.parent.assertEqual(result.logits.shape ,						(self.batch_size, self.type_sequence_label_size)							)

				# Test with an image with different size than the one specified in config.
				lowerCamelCase__              =	self.image_size // 2
				lowerCamelCase__              =	pixel_values[:, :, :image_size, :image_size]
				lowerCamelCase__              =	model(__A ,						interpolate_pos_encoding=__A ,						training=__A							)
				self.parent.assertEqual(result.logits.shape ,						(self.batch_size, self.type_sequence_label_size)							)

				# test greyscale images
				lowerCamelCase__              =	1
				lowerCamelCase__              =	TFViTForImageClassification(__A							)
				lowerCamelCase__              =	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
				lowerCamelCase__              =	model(__A							)
				self.parent.assertEqual(result.logits.shape ,						(self.batch_size, self.type_sequence_label_size)							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	self.prepare_config_and_inputs()
				lowerCamelCase__              =	config_and_inputs
				lowerCamelCase__              =	{"pixel_values": pixel_values}
				return config, inputs_dict






@require_tf
class       __A					(					UpperCamelCase_    ,       UpperCamelCase_    ,       unittest.TestCase				):

			'''simple docstring'''







			lowerCAmelCase_							  =		(TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
			lowerCAmelCase_							  =		(
			    {"""feature-extraction""": TFViTModel, """image-classification""": TFViTForImageClassification}
			    if is_tf_available()
			    else {}
			)

			lowerCAmelCase_							  =		False
			lowerCAmelCase_							  =		False
			lowerCAmelCase_							  =		False





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	TFViTModelTester(self							)
				lowerCamelCase__              =	ConfigTester(self ,						config_class=__A ,						has_text_modality=__A ,						hidden_size=3_7							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				self.config_tester.run_common_tests()





			@unittest.skip(reason='''ViT does not use inputs_embeds'''							)
			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				pass





			@unittest.skip(reason='''ViT does not use inputs_embeds'''							)
			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				pass





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
					lowerCamelCase__              =	model_class(__A							)
					self.assertIsInstance(model.get_input_embeddings() ,						(tf.keras.layers.Layer)							)
					lowerCamelCase__              =	model.get_output_embeddings()
					self.assertTrue(x is None or isinstance(__A ,						tf.keras.layers.Layer							)							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
					lowerCamelCase__              =	model_class(__A							)
					lowerCamelCase__              =	inspect.signature(model.call							)
					# signature.parameters is an OrderedDict => so arg_names order is deterministic
					lowerCamelCase__              =	[*signature.parameters.keys()]

					lowerCamelCase__              =	["pixel_values"]
					self.assertListEqual(arg_names[:1] ,						__A							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*__A							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*__A							)





			@slow
			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	TFViTModel.from_pretrained('''google/vit-base-patch16-224'''							)
				self.assertIsNotNone(__A							)





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







	'''simple docstring'''







	lowerCamelCase__              =	Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
	return image






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

			'''simple docstring'''







			@cached_property
			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				return ViTImageProcessor.from_pretrained('''google/vit-base-patch16-224'''							) if is_vision_available() else None





			@slow
			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	TFViTForImageClassification.from_pretrained('''google/vit-base-patch16-224'''							)

				lowerCamelCase__              =	self.default_image_processor
				lowerCamelCase__              =	prepare_img()
				lowerCamelCase__              =	image_processor(images=__A ,						return_tensors='''tf'''							)

				# forward pass
				lowerCamelCase__              =	model(**__A							)

				# verify the logits
				lowerCamelCase__              =	tf.TensorShape((1, 1_0_0_0)							)
				self.assertEqual(outputs.logits.shape ,						__A							)

				lowerCamelCase__              =	tf.constant([-0.2744, 0.8215, -0.0836]							)

				tf.debugging.assert_near(outputs.logits[0, :3] ,						__A ,						atol=1E-4							)










from typing import TYPE_CHECKING

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


_a						=   {
    "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:
			_a						=   ["FunnelTokenizerFast"]

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			_a						=   [
			    "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:
			_a						=   [
			    "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

			_a						=   _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)



