





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 					__snake_case	:


    '''simple docstring'''







    def __init__(       self   ,				A_   ,				A_=13   ,				A_=30   ,				A_=2   ,				A_=3   ,				A_=True   ,				A_=True   ,				A_=32   ,				A_=2   ,				A_=4   ,				A_=37   ,				A_="gelu"   ,				A_=0.1   ,				A_=0.1   ,				A_=10   ,				A_=0.02   ,				A_=3   ,				A_=None   ,				):





        '''simple docstring'''

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

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



    def  lowercase_					(       self     ):





        '''simple docstring'''

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

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

        SCREAMING_SNAKE_CASE__											=			self.get_config()

        return config, pixel_values, labels



    def  lowercase_					(       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  lowercase_					(       self   ,				A_   ,				A_   ,				A_     ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__											=			TFViTModel(config=A_     )
        SCREAMING_SNAKE_CASE__											=			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.
        SCREAMING_SNAKE_CASE__											=			self.image_size // 2
        SCREAMING_SNAKE_CASE__											=			pixel_values[:, :, :image_size, :image_size]
        SCREAMING_SNAKE_CASE__											=			model(A_   ,				interpolate_pos_encoding=A_   ,				training=A_     )
        SCREAMING_SNAKE_CASE__											=			(image_size // self.patch_size) ** 2 + 1
        self.parent.assertEqual(result.last_hidden_state.shape   ,				(self.batch_size, seq_length, self.hidden_size)     )



    def  lowercase_					(       self   ,				A_   ,				A_   ,				A_     ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__											=			self.type_sequence_label_size
        SCREAMING_SNAKE_CASE__											=			TFViTForImageClassification(A_     )
        SCREAMING_SNAKE_CASE__											=			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.
        SCREAMING_SNAKE_CASE__											=			self.image_size // 2
        SCREAMING_SNAKE_CASE__											=			pixel_values[:, :, :image_size, :image_size]
        SCREAMING_SNAKE_CASE__											=			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
        SCREAMING_SNAKE_CASE__											=			1
        SCREAMING_SNAKE_CASE__											=			TFViTForImageClassification(A_     )
        SCREAMING_SNAKE_CASE__											=			floats_tensor([self.batch_size, 1, self.image_size, self.image_size]     )
        SCREAMING_SNAKE_CASE__											=			model(A_     )
        self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.type_sequence_label_size)     )




    def  lowercase_					(       self     ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__											=			self.prepare_config_and_inputs()
        SCREAMING_SNAKE_CASE__				,		SCREAMING_SNAKE_CASE__				,		SCREAMING_SNAKE_CASE__											=			config_and_inputs
        SCREAMING_SNAKE_CASE__											=			{'''pixel_values''': pixel_values}
        return config, inputs_dict





@require_tf
class 					__snake_case	(  __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     unittest.TestCase    ):


    '''simple docstring'''







    lowerCamelCase__    :		Optional[Any]				=	(TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
    lowerCamelCase__    :		Any				=	(
        {"""feature-extraction""": TFViTModel, """image-classification""": TFViTForImageClassification}
        if is_tf_available()
        else {}
    )

    lowerCamelCase__    :		int				=	False
    lowerCamelCase__    :		Tuple				=	False
    lowerCamelCase__    :		Optional[int]				=	False



    def  lowercase_					(       self     ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__											=			TFViTModelTester(self     )
        SCREAMING_SNAKE_CASE__											=			ConfigTester(self   ,				config_class=A_   ,				has_text_modality=A_   ,				hidden_size=37     )



    def  lowercase_					(       self     ):





        '''simple docstring'''

        self.config_tester.run_common_tests()



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





        '''simple docstring'''

        pass



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





        '''simple docstring'''

        pass



    def  lowercase_					(       self     ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__				,		SCREAMING_SNAKE_CASE__											=			self.model_tester.prepare_config_and_inputs_for_common()

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



    def  lowercase_					(       self     ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__				,		SCREAMING_SNAKE_CASE__											=			self.model_tester.prepare_config_and_inputs_for_common()

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

            SCREAMING_SNAKE_CASE__											=			['''pixel_values''']
            self.assertListEqual(arg_names[:1]   ,				A_     )



    def  lowercase_					(       self     ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__											=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*A_     )



    def  lowercase_					(       self     ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__											=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*A_     )




    @slow
    def  lowercase_					(       self     ):





        '''simple docstring'''

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





def 							__snake_case							(    )				->						Union[str, Any]:
    SCREAMING_SNAKE_CASE__											=			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
    return image



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


    '''simple docstring'''







    @cached_property
    def  lowercase_					(       self     ):





        '''simple docstring'''

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




    @slow
    def  lowercase_					(       self     ):





        '''simple docstring'''

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

        SCREAMING_SNAKE_CASE__											=			self.default_image_processor
        SCREAMING_SNAKE_CASE__											=			prepare_img()
        SCREAMING_SNAKE_CASE__											=			image_processor(images=A_   ,				return_tensors='''tf'''     )

        # forward pass
        SCREAMING_SNAKE_CASE__											=			model(**A_     )

        # verify the logits
        SCREAMING_SNAKE_CASE__											=			tf.TensorShape((1, 10_00)     )
        self.assertEqual(outputs.logits.shape   ,				A_     )

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

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








from ...configuration_utils import PretrainedConfig
from ...utils import logging


_UpperCamelCase			:	Optional[int]								=logging.get_logger(__name__)

_UpperCamelCase			:	Tuple								={
    'google/canine-s': 'https://huggingface.co/google/canine-s/resolve/main/config.json',
    # See all CANINE models at https://huggingface.co/models?filter=canine
}






class 						UpperCAmelCase__ (							__snake_case							):
		__snake_case      :       Any     =   "canine"







		def __init__(					self       ,A__=768       ,A__=12       ,A__=12       ,A__=3072       ,A__="gelu"       ,A__=0.1       ,A__=0.1       ,A__=16384       ,A__=16       ,A__=0.02       ,A__=1E-12       ,A__=0       ,A__=0xE_000       ,A__=0xE_001       ,A__=4       ,A__=4       ,A__=8       ,A__=16384       ,A__=128       ,**A__       ,):
						super().__init__(pad_token_id=A__       ,bos_token_id=A__       ,eos_token_id=A__       ,**A__		)

						_A	:				int   =      max_position_embeddings
						_A	:				Dict   =      hidden_size
						_A	:				List[str]   =      num_hidden_layers
						_A	:				Optional[Any]   =      num_attention_heads
						_A	:				Optional[Any]   =      intermediate_size
						_A	:				Optional[Any]   =      hidden_act
						_A	:				str   =      hidden_dropout_prob
						_A	:				Optional[int]   =      attention_probs_dropout_prob
						_A	:				Union[str, Any]   =      initializer_range
						_A	:				Dict   =      type_vocab_size
						_A	:				Optional[Any]   =      layer_norm_eps

						# Character config:
						_A	:				Union[str, Any]   =      downsampling_rate
						_A	:				List[str]   =      upsampling_kernel_size
						_A	:				str   =      num_hash_functions
						_A	:				Optional[Any]   =      num_hash_buckets
						_A	:				Optional[Any]   =      local_transformer_stride



