





from typing import Optional, Tuple, Union

import tensorflow as tf

from ...activations_tf import ACTaFN
from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward
from ...modeling_tf_outputs import (
    TFBaseModelOutputWithNoAttention,
    TFBaseModelOutputWithPoolingAndNoAttention,
    TFSequenceClassifierOutput,
)
from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs
from ...tf_utils import shape_list
from ...utils import logging
from .configuration_regnet import RegNetConfig


_lowerCAmelCase							:					Optional[Any]		    =		logging.get_logger(__name__)

# General docstring
_lowerCAmelCase							:					Optional[int]		    =		"RegNetConfig"

# Base docstring
_lowerCAmelCase							:					str		    =		"facebook/regnet-y-040"
_lowerCAmelCase							:					str		    =		[1, 1_088, 7, 7]

# Image classification docstring
_lowerCAmelCase							:					List[str]		    =		"facebook/regnet-y-040"
_lowerCAmelCase							:					Optional[Any]		    =		"tabby, tabby cat"

_lowerCAmelCase							:					Optional[Any]		    =		[
    "facebook/regnet-y-040",
    # See all regnet models at https://huggingface.co/models?filter=regnet
]


class     __magic_name__     (							tf.keras.layers.Layer							):





     """simple docstring"""



     def __init__(							self				:List[str]	,					snake_case				:Tuple	,					snake_case				:List[str] = 3	,					snake_case				:List[str] = 1	,					snake_case				:List[Any] = 1	,					snake_case				:Optional[int] = "relu"	,					**snake_case				:Tuple	,					):





        '''simple docstring'''




        super().__init__(**_A     )
        # The padding and conv has been verified in
        # https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb
        A_    : int								=	tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2     )
        A_    : int								=	tf.keras.layers.ConvaD(
            filters=_A	,					kernel_size=_A	,					strides=_A	,					padding="VALID"	,					groups=_A	,					use_bias=_A	,					name="convolution"	,					)
        A_    : List[str]								=	tf.keras.layers.BatchNormalization(epsilon=1e-5	,					momentum=0.9	,					name="normalization"     )
        A_    : List[Any]								=	ACTaFN[activation] if activation is not None else tf.identity





     def   SCREAMING_SNAKE_CASE						(							self				:str	,					snake_case				:Tuple     ):





        '''simple docstring'''




        A_    : Optional[int]								=	self.convolution(self.padding(_A     )     )
        A_    : Optional[int]								=	self.normalization(_A     )
        A_    : Tuple								=	self.activation(_A     )
        return hidden_state




class     __magic_name__     (							tf.keras.layers.Layer							):





     """simple docstring"""



     def __init__(							self				:Optional[Any]	,					snake_case				:Union[str, Any]	,					**snake_case				:List[Any]     ):





        '''simple docstring'''




        super().__init__(**_A     )
        A_    : Any								=	config.num_channels
        A_    : int								=	TFRegNetConvLayer(
            out_channels=config.embedding_size	,					kernel_size=3	,					stride=2	,					activation=config.hidden_act	,					name="embedder"	,					)





     def   SCREAMING_SNAKE_CASE						(							self				:List[str]	,					snake_case				:List[Any]     ):





        '''simple docstring'''




        A_    : List[str]								=	shape_list(_A     )[1]
        if tf.executing_eagerly() and num_channels != self.num_channels:
           raise ValueError(
               "Make sure that the channel dimension of the pixel values match with the one set in the configuration."     )

        # When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format.
        # So change the input format from `NCHW` to `NHWC`.
        # shape = (batch_size, in_height, in_width, in_channels=num_channels)
        A_    : int								=	tf.transpose(_A	,					perm=(0, 2, 3, 1)     )
        A_    : Optional[Any]								=	self.embedder(_A     )
        return hidden_state




class     __magic_name__     (							tf.keras.layers.Layer							):





     """simple docstring"""



     def __init__(							self				:List[Any]	,					snake_case				:str	,					snake_case				:str = 2	,					**snake_case				:Tuple     ):





        '''simple docstring'''




        super().__init__(**_A     )
        A_    : List[Any]								=	tf.keras.layers.ConvaD(
            filters=_A	,					kernel_size=1	,					strides=_A	,					use_bias=_A	,					name="convolution"     )
        A_    : List[Any]								=	tf.keras.layers.BatchNormalization(epsilon=1e-5	,					momentum=0.9	,					name="normalization"     )





     def   SCREAMING_SNAKE_CASE						(							self				:Optional[Any]	,					snake_case				:Dict	,					snake_case				:str = False     ):





        '''simple docstring'''




        return self.normalization(self.convolution(_A     )	,					training=_A     )




class     __magic_name__     (							tf.keras.layers.Layer							):





     """simple docstring"""



     def __init__(							self				:Any	,					snake_case				:int	,					snake_case				:str	,					**snake_case				:Dict     ):





        '''simple docstring'''




        super().__init__(**_A     )
        A_    : int								=	tf.keras.layers.GlobalAveragePoolingaD(keepdims=_A	,					name="pooler"     )
        A_    : Optional[Any]								=	[
            tf.keras.layers.ConvaD(filters=_A	,					kernel_size=1	,					activation="relu"	,					name="attention.0"     ),
            tf.keras.layers.ConvaD(filters=_A	,					kernel_size=1	,					activation="sigmoid"	,					name="attention.2"     ),
        ]





     def   SCREAMING_SNAKE_CASE						(							self				:Optional[int]	,					snake_case				:str     ):





        '''simple docstring'''




        A_    : int								=	self.pooler(_A     )
        for layer_module in self.attention:
           A_    : Optional[int]								=	layer_module(_A     )
        A_    : Dict								=	hidden_state * pooled
        return hidden_state




class     __magic_name__     (							tf.keras.layers.Layer							):





     """simple docstring"""



     def __init__(							self				:Any	,					snake_case				:Union[str, Any]	,					snake_case				:Dict	,					snake_case				:Optional[Any]	,					snake_case				:Dict = 1	,					**snake_case				:str     ):





        '''simple docstring'''




        super().__init__(**_A     )
        A_    : Union[str, Any]								=	in_channels != out_channels or stride != 1
        A_    : Tuple								=	max(1	,					out_channels // config.groups_width     )
        A_    : Dict								=	(
            TFRegNetShortCut(_A	,					stride=_A	,					name="shortcut"     )
            if should_apply_shortcut
            else tf.keras.layers.Activation("linear"	,					name="shortcut"     )
        )
        # `self.layers` instead of `self.layer` because that is a reserved argument.
        A_    : Dict								=	[
            TFRegNetConvLayer(_A	,					kernel_size=1	,					activation=config.hidden_act	,					name="layer.0"     ),
            TFRegNetConvLayer(
                _A	,					stride=_A	,					groups=_A	,					activation=config.hidden_act	,					name="layer.1"     ),
            TFRegNetConvLayer(_A	,					kernel_size=1	,					activation=_A	,					name="layer.2"     ),
        ]
        A_    : Dict								=	ACTaFN[config.hidden_act]





     def   SCREAMING_SNAKE_CASE						(							self				:Any	,					snake_case				:List[Any]     ):





        '''simple docstring'''




        A_    : str								=	hidden_state
        for layer_module in self.layers:
           A_    : int								=	layer_module(_A     )
        A_    : Optional[Any]								=	self.shortcut(_A     )
        hidden_state += residual
        A_    : List[Any]								=	self.activation(_A     )
        return hidden_state




class     __magic_name__     (							tf.keras.layers.Layer							):





     """simple docstring"""



     def __init__(							self				:Optional[Any]	,					snake_case				:List[str]	,					snake_case				:Any	,					snake_case				:str	,					snake_case				:List[Any] = 1	,					**snake_case				:Union[str, Any]     ):





        '''simple docstring'''




        super().__init__(**_A     )
        A_    : Dict								=	in_channels != out_channels or stride != 1
        A_    : Tuple								=	max(1	,					out_channels // config.groups_width     )
        A_    : Any								=	(
            TFRegNetShortCut(_A	,					stride=_A	,					name="shortcut"     )
            if should_apply_shortcut
            else tf.keras.layers.Activation("linear"	,					name="shortcut"     )
        )
        A_    : Optional[int]								=	[
            TFRegNetConvLayer(_A	,					kernel_size=1	,					activation=config.hidden_act	,					name="layer.0"     ),
            TFRegNetConvLayer(
                _A	,					stride=_A	,					groups=_A	,					activation=config.hidden_act	,					name="layer.1"     ),
            TFRegNetSELayer(_A	,					reduced_channels=int(round(in_channels / 4     )     )	,					name="layer.2"     ),
            TFRegNetConvLayer(_A	,					kernel_size=1	,					activation=_A	,					name="layer.3"     ),
        ]
        A_    : Optional[Any]								=	ACTaFN[config.hidden_act]





     def   SCREAMING_SNAKE_CASE						(							self				:Union[str, Any]	,					snake_case				:Dict     ):





        '''simple docstring'''




        A_    : List[str]								=	hidden_state
        for layer_module in self.layers:
           A_    : int								=	layer_module(_A     )
        A_    : Optional[int]								=	self.shortcut(_A     )
        hidden_state += residual
        A_    : List[Any]								=	self.activation(_A     )
        return hidden_state




class     __magic_name__     (							tf.keras.layers.Layer							):





     """simple docstring"""



     def __init__(							self				:int	,					snake_case				:Optional[int]	,					snake_case				:Tuple	,					snake_case				:List[Any]	,					snake_case				:Dict = 2	,					snake_case				:Dict = 2	,					**snake_case				:List[Any]     ):





        '''simple docstring'''




        super().__init__(**_A     )

        A_    : Any								=	TFRegNetXLayer if config.layer_type == "x" else TFRegNetYLayer
        A_    : int								=	[
            # downsampling is done in the first layer with stride of 2
            layer(_A	,					_A	,					_A	,					stride=_A	,					name="layers.0"     ),
            *[layer(_A	,					_A	,					_A	,					name=f"layers.{i+1}"     ) for i in range(depth - 1     )],
        ]





     def   SCREAMING_SNAKE_CASE						(							self				:Optional[int]	,					snake_case				:Union[str, Any]     ):





        '''simple docstring'''




        for layer_module in self.layers:
           A_    : str								=	layer_module(_A     )
        return hidden_state




class     __magic_name__     (							tf.keras.layers.Layer							):





     """simple docstring"""



     def __init__(							self				:Any	,					snake_case				:Optional[Any]	,					**snake_case				:Optional[int]     ):





        '''simple docstring'''




        super().__init__(**_A     )
        A_    : Union[str, Any]								=	[]
        # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input
        self.stages.append(
            TFRegNetStage(
                _A	,					config.embedding_size	,					config.hidden_sizes[0]	,					stride=2 if config.downsample_in_first_stage else 1	,					depth=config.depths[0]	,					name="stages.0"	,					)     )
        A_    : Optional[Any]								=	zip(config.hidden_sizes	,					config.hidden_sizes[1:]     )
        for i, ((in_channels, out_channels), depth) in enumerate(zip(_A	,					config.depths[1:]     )     ):
           self.stages.append(TFRegNetStage(_A	,					_A	,					_A	,					depth=_A	,					name=f"stages.{i+1}"     )     )





     def   SCREAMING_SNAKE_CASE						(							self				:Optional[int]	,					snake_case				:List[str]	,					snake_case				:Optional[int] = False	,					snake_case				:Union[str, Any] = True     ):





        '''simple docstring'''




        A_    : Optional[Any]								=	() if output_hidden_states else None

        for stage_module in self.stages:
           if output_hidden_states:
              A_    : Union[str, Any]								=	hidden_states + (hidden_state,)

           A_    : Optional[int]								=	stage_module(_A     )

        if output_hidden_states:
           A_    : Optional[int]								=	hidden_states + (hidden_state,)

        if not return_dict:
           return tuple(v for v in [hidden_state, hidden_states] if v is not None     )

        return TFBaseModelOutputWithNoAttention(last_hidden_state=_A	,					hidden_states=_A     )




@keras_serializable
class     __magic_name__     (							tf.keras.layers.Layer							):





     """simple docstring"""



     __UpperCamelCase    			=   RegNetConfig



     def __init__(							self				:Optional[Any]	,					snake_case				:Optional[int]	,					**snake_case				:Optional[int]     ):





        '''simple docstring'''




        super().__init__(**_A     )
        A_    : List[str]								=	config
        A_    : Dict								=	TFRegNetEmbeddings(_A	,					name="embedder"     )
        A_    : List[str]								=	TFRegNetEncoder(_A	,					name="encoder"     )
        A_    : Tuple								=	tf.keras.layers.GlobalAveragePoolingaD(keepdims=_A	,					name="pooler"     )





     @unpack_inputs
     def   SCREAMING_SNAKE_CASE						(							self				:Any	,					snake_case				:List[Any]	,					snake_case				:List[Any] = None	,					snake_case				:Optional[Any] = None	,					snake_case				:str = False	,					):





        '''simple docstring'''




        A_    : str								=	(
            output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        )
        A_    : List[str]								=	return_dict if return_dict is not None else self.config.use_return_dict

        A_    : Union[str, Any]								=	self.embedder(_A	,					training=_A     )

        A_    : Dict								=	self.encoder(
            _A	,					output_hidden_states=_A	,					return_dict=_A	,					training=_A     )

        A_    : Tuple								=	encoder_outputs[0]
        A_    : Tuple								=	self.pooler(_A     )

        # Change to NCHW output format have uniformity in the modules
        A_    : Optional[int]								=	tf.transpose(_A	,					perm=(0, 3, 1, 2)     )
        A_    : List[Any]								=	tf.transpose(_A	,					perm=(0, 3, 1, 2)     )

        # Change the other hidden state outputs to NCHW as well
        if output_hidden_states:
           A_    : Optional[Any]								=	tuple([tf.transpose(_A	,					perm=(0, 3, 1, 2)     ) for h in encoder_outputs[1]]     )

        if not return_dict:
           return (last_hidden_state, pooled_output) + encoder_outputs[1:]

        return TFBaseModelOutputWithPoolingAndNoAttention(
            last_hidden_state=_A	,					pooler_output=_A	,					hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states	,					)




class     __magic_name__     (							a_							):





     """simple docstring"""



     __UpperCamelCase    			=   RegNetConfig
     __UpperCamelCase    			=   '''regnet'''
     __UpperCamelCase    			=   '''pixel_values'''



     @property
     def   SCREAMING_SNAKE_CASE						(							self				:Union[str, Any]     ):





        '''simple docstring'''




        return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 224, 224)	,					dtype=tf.floataa     )}




_lowerCAmelCase							:					str		    =		R"\n    Parameters:\n    This model is a Tensorflow\n    [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a\n    regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and\n    behavior.\n        config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.\n"

_lowerCAmelCase							:					List[Any]		    =		R"\n    Args:\n        pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`ConveNextImageProcessor.__call__`] for details.\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n            more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n"


@add_start_docstrings(
    '''The bare RegNet model outputting raw features without any specific head on top.'''			, a_			, )
class     __magic_name__     (							a_							):





     """simple docstring"""



     def __init__(							self				:str	,					snake_case				:Dict	,					*snake_case				:Union[str, Any]	,					**snake_case				:Optional[int]     ):





        '''simple docstring'''




        super().__init__(_A	,					*_A	,					**_A     )
        A_    : Tuple								=	TFRegNetMainLayer(_A	,					name="regnet"     )





     @unpack_inputs
     @add_start_docstrings_to_model_forward(_A     )
     @add_code_sample_docstrings(
         checkpoint=_CHECKPOINT_FOR_DOC	,					output_type=_A	,					config_class=_CONFIG_FOR_DOC	,					modality="vision"	,					expected_output=_EXPECTED_OUTPUT_SHAPE	,					)
     def   SCREAMING_SNAKE_CASE						(							self				:Any	,					snake_case				:Optional[Any]	,					snake_case				:int = None	,					snake_case				:int = None	,					snake_case				:List[Any]=False	,					):





        '''simple docstring'''




        A_    : List[str]								=	(
            output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        )
        A_    : Dict								=	return_dict if return_dict is not None else self.config.use_return_dict

        A_    : Optional[int]								=	self.regnet(
            pixel_values=_A	,					output_hidden_states=_A	,					return_dict=_A	,					training=_A	,					)
        if not return_dict:
           return (outputs[0],) + outputs[1:]

        return TFBaseModelOutputWithPoolingAndNoAttention(
            last_hidden_state=outputs.last_hidden_state	,					pooler_output=outputs.pooler_output	,					hidden_states=outputs.hidden_states	,					)




@add_start_docstrings(
    '''
    RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    '''			, a_			, )
class     __magic_name__     (							a_			, a_							):





     """simple docstring"""



     def __init__(							self				:Optional[int]	,					snake_case				:Optional[int]	,					*snake_case				:Optional[Any]	,					**snake_case				:Optional[int]     ):





        '''simple docstring'''




        super().__init__(_A	,					*_A	,					**_A     )
        A_    : Optional[int]								=	config.num_labels
        A_    : Tuple								=	TFRegNetMainLayer(_A	,					name="regnet"     )
        # classification head
        A_    : Optional[Any]								=	[
            tf.keras.layers.Flatten(),
            tf.keras.layers.Dense(config.num_labels	,					name="classifier.1"     ) if config.num_labels > 0 else tf.identity,
        ]





     @unpack_inputs
     @add_start_docstrings_to_model_forward(_A     )
     @add_code_sample_docstrings(
         checkpoint=_IMAGE_CLASS_CHECKPOINT	,					output_type=_A	,					config_class=_CONFIG_FOR_DOC	,					expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT	,					)
     def   SCREAMING_SNAKE_CASE						(							self				:Union[str, Any]	,					snake_case				:List[str] = None	,					snake_case				:Any = None	,					snake_case				:Dict = None	,					snake_case				:List[Any] = None	,					snake_case				:str=False	,					):





        '''simple docstring'''




        A_    : int								=	(
            output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        )
        A_    : Union[str, Any]								=	return_dict if return_dict is not None else self.config.use_return_dict

        A_    : Optional[Any]								=	self.regnet(
            _A	,					output_hidden_states=_A	,					return_dict=_A	,					training=_A     )

        A_    : Optional[int]								=	outputs.pooler_output if return_dict else outputs[1]

        A_    : str								=	self.classifier[0](_A     )
        A_    : str								=	self.classifier[1](_A     )

        A_    : Tuple								=	None if labels is None else self.hf_compute_loss(labels=_A	,					logits=_A     )

        if not return_dict:
           A_    : str								=	(logits,) + outputs[2:]
           return ((loss,) + output) if loss is not None else output

        return TFSequenceClassifierOutput(loss=_A	,					logits=_A	,					hidden_states=outputs.hidden_states     )







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_       :
	def __init__(    self							,					_A							,					_A=1_3							,					_A=3_0							,					_A=2							,					_A=3							,					_A=True							,					_A=True							,					_A=3_2							,					_A=2							,					_A=4							,					_A=3_7							,					_A="gelu"							,					_A=0.1							,					_A=0.1							,					_A=1_0							,					_A=0.02							,					_A=3							,					_A=None							,					):






				'''simple docstring'''







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

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






				'''simple docstring'''







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

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

				UpperCAmelCase   		=				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'''







				UpperCAmelCase   		=				TFViTModel(config=_A		)
				UpperCAmelCase   		=				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.
				UpperCAmelCase   		=				self.image_size // 2
				UpperCAmelCase   		=				pixel_values[:, :, :image_size, :image_size]
				UpperCAmelCase   		=				model(_A							,					interpolate_pos_encoding=_A							,					training=_A		)
				UpperCAmelCase   		=				(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'''







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







	def      _lowercase				(    self		):






				'''simple docstring'''







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

@require_tf
class 	A_       (a_     ,							a_     ,							unittest.TestCase			):
	UpperCAmelCase__					=			(TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
	UpperCAmelCase__					=			(
	    {'''feature-extraction''': TFViTModel, '''image-classification''': TFViTForImageClassification}
	    if is_tf_available()
	    else {}
	)

	UpperCAmelCase__					=			False
	UpperCAmelCase__					=			False
	UpperCAmelCase__					=			False
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				TFViTModelTester(self		)
				UpperCAmelCase   		=				ConfigTester(self							,					config_class=_A							,					has_text_modality=_A							,					hidden_size=3_7		)
	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'''







				UpperCAmelCase		,			UpperCAmelCase   		=				self.model_tester.prepare_config_and_inputs_for_common()

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






				'''simple docstring'''







				UpperCAmelCase		,			UpperCAmelCase   		=				self.model_tester.prepare_config_and_inputs_for_common()

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

							UpperCAmelCase   		=				['''pixel_values''']
							self.assertListEqual(arg_names[:1]							,					_A		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*_A		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*_A		)







	@slow
	def      _lowercase				(    self		):






				'''simple docstring'''







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





def 						__SCREAMING_SNAKE_CASE  (				)  ->				Optional[Any]:







			'''simple docstring'''

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






@require_tf
@require_vision
class 	A_       (unittest.TestCase			):
	@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'''







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

				UpperCAmelCase   		=				self.default_image_processor
				UpperCAmelCase   		=				prepare_img()
				UpperCAmelCase   		=				image_processor(images=_A							,					return_tensors='''tf'''		)

				# forward pass
				UpperCAmelCase   		=				model(**_A		)

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

				UpperCAmelCase   		=				tf.constant([-0.27_44, 0.82_15, -0.08_36]		)

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



