





'''simple docstring'''


from typing import Optional, Tuple, Union

import tensorflow as tf

from ...activations_tf import ACTaFN
from ...modeling_tf_outputs import (
    TFBaseModelOutputWithNoAttention,
    TFBaseModelOutputWithPoolingAndNoAttention,
    TFImageClassifierOutputWithNoAttention,
)
from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs
from ...tf_utils import shape_list
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_resnet import ResNetConfig


_lowerCamelCase					:						Dict					     =							logging.get_logger(__name__)

# General docstring
_lowerCamelCase					:						Union[str, Any]					     =							'ResNetConfig'

# Base docstring
_lowerCamelCase					:						Optional[int]					     =							'microsoft/resnet-50'
_lowerCamelCase					:						List[Any]					     =							[1, 2048, 7, 7]

# Image classification docstring
_lowerCamelCase					:						Any					     =							'microsoft/resnet-50'
_lowerCamelCase					:						List[str]					     =							'tiger cat'

_lowerCamelCase					:						int					     =							[
    'microsoft/resnet-50',
    # See all resnet models at https://huggingface.co/models?filter=resnet
]



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







		'''simple docstring'''







		def __init__(self    :   Tuple   ,   _lowerCAmelCase    :   int   ,   _lowerCAmelCase    :   int = 3   ,   _lowerCAmelCase    :   int = 1   ,   _lowerCAmelCase    :   str = "relu"   ,   **_lowerCAmelCase    :   Optional[Any]   ):
			super().__init__(**_lowerCAmelCase   )
			A					=   kernel_size // 2
			A					=   tf.keras.layers.ConvaD(
			    _lowerCAmelCase   ,   kernel_size=_lowerCAmelCase   ,   strides=_lowerCAmelCase   ,   padding="""valid"""   ,   use_bias=_lowerCAmelCase   ,   name="""convolution"""   )
			# Use same default momentum and epsilon as PyTorch equivalent
			A					=   tf.keras.layers.BatchNormalization(epsilon=1e-5   ,   momentum=0.9   ,   name="""normalization"""   )
			A					=   ACTaFN[activation] if activation is not None else tf.keras.layers.Activation("""linear"""   )







		def 							A  (self    :   Union[str, Any]   ,   _lowerCAmelCase    :   tf.Tensor   ):
			# Pad to match that done in the PyTorch Conv2D model
			A					=   A					=   (self.pad_value, self.pad_value)
			A					=   tf.pad(_lowerCAmelCase   ,   [(0, 0), height_pad, width_pad, (0, 0)]   )
			A					=   self.conv(_lowerCAmelCase   )
			return hidden_state







		def 							A  (self    :   List[str]   ,   _lowerCAmelCase    :   tf.Tensor   ,   _lowerCAmelCase    :   bool = False   ):
			A					=   self.convolution(_lowerCAmelCase   )
			A					=   self.normalization(_lowerCAmelCase   ,   training=_lowerCAmelCase   )
			A					=   self.activation(_lowerCAmelCase   )
			return hidden_state






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







		'''simple docstring'''







		def __init__(self    :   str   ,   _lowerCAmelCase    :   ResNetConfig   ,   **_lowerCAmelCase    :   int   ):
			super().__init__(**_lowerCAmelCase   )
			A					=   TFResNetConvLayer(
			    config.embedding_size   ,   kernel_size=7   ,   stride=2   ,   activation=config.hidden_act   ,   name="""embedder"""   ,   )
			A					=   tf.keras.layers.MaxPoolaD(pool_size=3   ,   strides=2   ,   padding="""valid"""   ,   name="""pooler"""   )
			A					=   config.num_channels







		def 							A  (self    :   List[str]   ,   _lowerCAmelCase    :   tf.Tensor   ,   _lowerCAmelCase    :   bool = False   ):
			A	,     A	,     A	,     A					=   shape_list(_lowerCAmelCase   )
			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."""   )
			A					=   pixel_values
			A					=   self.embedder(_lowerCAmelCase   )
			A					=   tf.pad(_lowerCAmelCase   ,   [[0, 0], [1, 1], [1, 1], [0, 0]]   )
			A					=   self.pooler(_lowerCAmelCase   )
			return hidden_state






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







		'''simple docstring'''







		def __init__(self    :   List[Any]   ,   _lowerCAmelCase    :   int   ,   _lowerCAmelCase    :   int = 2   ,   **_lowerCAmelCase    :   Dict   ):
			super().__init__(**_lowerCAmelCase   )
			A					=   tf.keras.layers.ConvaD(
			    _lowerCAmelCase   ,   kernel_size=1   ,   strides=_lowerCAmelCase   ,   use_bias=_lowerCAmelCase   ,   name="""convolution"""   )
			# Use same default momentum and epsilon as PyTorch equivalent
			A					=   tf.keras.layers.BatchNormalization(epsilon=1e-5   ,   momentum=0.9   ,   name="""normalization"""   )







		def 							A  (self    :   List[str]   ,   _lowerCAmelCase    :   tf.Tensor   ,   _lowerCAmelCase    :   bool = False   ):
			A					=   x
			A					=   self.convolution(_lowerCAmelCase   )
			A					=   self.normalization(_lowerCAmelCase   ,   training=_lowerCAmelCase   )
			return hidden_state






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







		'''simple docstring'''







		def __init__(self    :   Tuple   ,   _lowerCAmelCase    :   int   ,   _lowerCAmelCase    :   int   ,   _lowerCAmelCase    :   int = 1   ,   _lowerCAmelCase    :   str = "relu"   ,   **_lowerCAmelCase    :   List[str]   ):
			super().__init__(**_lowerCAmelCase   )
			A					=   in_channels != out_channels or stride != 1
			A					=   TFResNetConvLayer(_lowerCAmelCase   ,   stride=_lowerCAmelCase   ,   name="""layer.0"""   )
			A					=   TFResNetConvLayer(_lowerCAmelCase   ,   activation=_lowerCAmelCase   ,   name="""layer.1"""   )
			A					=   (
			    TFResNetShortCut(_lowerCAmelCase   ,   stride=_lowerCAmelCase   ,   name="""shortcut"""   )
			    if should_apply_shortcut
			    else tf.keras.layers.Activation("""linear"""   ,   name="""shortcut"""   )
			)
			A					=   ACTaFN[activation]







		def 							A  (self    :   Optional[int]   ,   _lowerCAmelCase    :   tf.Tensor   ,   _lowerCAmelCase    :   bool = False   ):
			A					=   hidden_state
			A					=   self.conva(_lowerCAmelCase   ,   training=_lowerCAmelCase   )
			A					=   self.conva(_lowerCAmelCase   ,   training=_lowerCAmelCase   )
			A					=   self.shortcut(_lowerCAmelCase   ,   training=_lowerCAmelCase   )
			hidden_state += residual
			A					=   self.activation(_lowerCAmelCase   )
			return hidden_state






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







		'''simple docstring'''







		def __init__(self    :   Union[str, Any]   ,   _lowerCAmelCase    :   int   ,   _lowerCAmelCase    :   int   ,   _lowerCAmelCase    :   int = 1   ,   _lowerCAmelCase    :   str = "relu"   ,   _lowerCAmelCase    :   int = 4   ,   **_lowerCAmelCase    :   Optional[Any]   ,   ):
			super().__init__(**_lowerCAmelCase   )
			A					=   in_channels != out_channels or stride != 1
			A					=   out_channels // reduction
			A					=   TFResNetConvLayer(_lowerCAmelCase   ,   kernel_size=1   ,   name="""layer.0"""   )
			A					=   TFResNetConvLayer(_lowerCAmelCase   ,   stride=_lowerCAmelCase   ,   name="""layer.1"""   )
			A					=   TFResNetConvLayer(_lowerCAmelCase   ,   kernel_size=1   ,   activation=_lowerCAmelCase   ,   name="""layer.2"""   )
			A					=   (
			    TFResNetShortCut(_lowerCAmelCase   ,   stride=_lowerCAmelCase   ,   name="""shortcut"""   )
			    if should_apply_shortcut
			    else tf.keras.layers.Activation("""linear"""   ,   name="""shortcut"""   )
			)
			A					=   ACTaFN[activation]







		def 							A  (self    :   Optional[int]   ,   _lowerCAmelCase    :   tf.Tensor   ,   _lowerCAmelCase    :   bool = False   ):
			A					=   hidden_state
			A					=   self.conva(_lowerCAmelCase   ,   training=_lowerCAmelCase   )
			A					=   self.conva(_lowerCAmelCase   ,   training=_lowerCAmelCase   )
			A					=   self.conva(_lowerCAmelCase   ,   training=_lowerCAmelCase   )
			A					=   self.shortcut(_lowerCAmelCase   ,   training=_lowerCAmelCase   )
			hidden_state += residual
			A					=   self.activation(_lowerCAmelCase   )
			return hidden_state






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







		'''simple docstring'''







		def __init__(self    :   Optional[int]   ,   _lowerCAmelCase    :   ResNetConfig   ,   _lowerCAmelCase    :   int   ,   _lowerCAmelCase    :   int   ,   _lowerCAmelCase    :   int = 2   ,   _lowerCAmelCase    :   int = 2   ,   **_lowerCAmelCase    :   List[str]   ):
			super().__init__(**_lowerCAmelCase   )

			A					=   TFResNetBottleNeckLayer if config.layer_type == """bottleneck""" else TFResNetBasicLayer

			A					=   [layer(_lowerCAmelCase   ,   _lowerCAmelCase   ,   stride=_lowerCAmelCase   ,   activation=config.hidden_act   ,   name="""layers.0"""   )]
			layers += [
			    layer(_lowerCAmelCase   ,   _lowerCAmelCase   ,   activation=config.hidden_act   ,   name=F"""layers.{i + 1}"""   )
			    for i in range(depth - 1   )
			]
			A					=   layers







		def 							A  (self    :   Union[str, Any]   ,   _lowerCAmelCase    :   tf.Tensor   ,   _lowerCAmelCase    :   bool = False   ):
			for layer in self.stage_layers:
				A					=   layer(_lowerCAmelCase   ,   training=_lowerCAmelCase   )
			return hidden_state






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







		'''simple docstring'''







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







		def 							A  (self    :   Union[str, Any]   ,   _lowerCAmelCase    :   tf.Tensor   ,   _lowerCAmelCase    :   bool = False   ,   _lowerCAmelCase    :   bool = True   ,   _lowerCAmelCase    :   bool = False   ,   ):
			A					=   () if output_hidden_states else None

			for stage_module in self.stages:
				if output_hidden_states:
					A					=   hidden_states + (hidden_state,)

				A					=   stage_module(_lowerCAmelCase   ,   training=_lowerCAmelCase   )

			if output_hidden_states:
				A					=   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=_lowerCAmelCase   ,   hidden_states=_lowerCAmelCase   )






class 				__UpperCAmelCase	(							A__    ):







		'''simple docstring'''







		__lowerCAmelCase								=     ResNetConfig
		__lowerCAmelCase								=     '''resnet'''
		__lowerCAmelCase								=     '''pixel_values'''







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






_lowerCamelCase					:						Any					     =							R'\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\n    Parameters:\n        config ([`ResNetConfig`]): 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					:						Tuple					     =							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            [`ConvNextImageProcessor.__call__`] for details.\n\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'



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







		'''simple docstring'''







		__lowerCAmelCase								=     ResNetConfig







		def __init__(self    :   Dict   ,   _lowerCAmelCase    :   ResNetConfig   ,   **_lowerCAmelCase    :   Union[str, Any]   ):
			super().__init__(**_lowerCAmelCase   )
			A					=   config
			A					=   TFResNetEmbeddings(_lowerCAmelCase   ,   name="""embedder"""   )
			A					=   TFResNetEncoder(_lowerCAmelCase   ,   name="""encoder"""   )
			A					=   tf.keras.layers.GlobalAveragePoolingaD(keepdims=_lowerCAmelCase   )







		@unpack_inputs
		def 							A  (self    :   Dict   ,   _lowerCAmelCase    :   tf.Tensor   ,   _lowerCAmelCase    :   Optional[bool] = None   ,   _lowerCAmelCase    :   Optional[bool] = None   ,   _lowerCAmelCase    :   bool = False   ,   ):
			A					=   (
			    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
			)
			A					=   return_dict if return_dict is not None else self.config.use_return_dict

			# TF 2.0 image layers can't use NCHW format when running on CPU.
			# We transpose to NHWC format and then transpose back after the full forward pass.
			# (batch_size, num_channels, height, width) -> (batch_size, height, width, num_channels)
			A					=   tf.transpose(_lowerCAmelCase   ,   perm=[0, 2, 3, 1]   )
			A					=   self.embedder(_lowerCAmelCase   ,   training=_lowerCAmelCase   )

			A					=   self.encoder(
			    _lowerCAmelCase   ,   output_hidden_states=_lowerCAmelCase   ,   return_dict=_lowerCAmelCase   ,   training=_lowerCAmelCase   )

			A					=   encoder_outputs[0]

			A					=   self.pooler(_lowerCAmelCase   )

			# Transpose all the outputs to the NCHW format
			# (batch_size, height, width, num_channels) -> (batch_size, num_channels, height, width)
			A					=   tf.transpose(_lowerCAmelCase   ,   (0, 3, 1, 2)   )
			A					=   tf.transpose(_lowerCAmelCase   ,   (0, 3, 1, 2)   )
			A					=   ()
			for hidden_state in encoder_outputs[1:]:
				A					=   hidden_states + tuple(tf.transpose(_lowerCAmelCase   ,   (0, 3, 1, 2)   ) for h in hidden_state   )

			if not return_dict:
				return (last_hidden_state, pooled_output) + hidden_states

			A					=   hidden_states if output_hidden_states else None

			return TFBaseModelOutputWithPoolingAndNoAttention(
			    last_hidden_state=_lowerCAmelCase   ,   pooler_output=_lowerCAmelCase   ,   hidden_states=_lowerCAmelCase   ,   )






@add_start_docstrings(
    '''The bare ResNet model outputting raw features without any specific head on top.'''		,       A__		,       )
class 				__UpperCAmelCase	(							A__    ):







		'''simple docstring'''







		def __init__(self    :   Optional[Any]   ,   _lowerCAmelCase    :   ResNetConfig   ,   **_lowerCAmelCase    :   Dict   ):
			super().__init__(_lowerCAmelCase   ,   **_lowerCAmelCase   )
			A					=   TFResNetMainLayer(config=_lowerCAmelCase   ,   name="""resnet"""   )







		@add_start_docstrings_to_model_forward(_lowerCAmelCase   )
		@add_code_sample_docstrings(
		    checkpoint=_CHECKPOINT_FOR_DOC   ,   output_type=_lowerCAmelCase   ,   config_class=_CONFIG_FOR_DOC   ,   modality="""vision"""   ,   expected_output=_EXPECTED_OUTPUT_SHAPE   ,   )
		@unpack_inputs
		def 							A  (self    :   Any   ,   _lowerCAmelCase    :   tf.Tensor   ,   _lowerCAmelCase    :   Optional[bool] = None   ,   _lowerCAmelCase    :   Optional[bool] = None   ,   _lowerCAmelCase    :   bool = False   ,   ):
			A					=   (
			    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
			)
			A					=   return_dict if return_dict is not None else self.config.use_return_dict

			A					=   self.resnet(
			    pixel_values=_lowerCAmelCase   ,   output_hidden_states=_lowerCAmelCase   ,   return_dict=_lowerCAmelCase   ,   training=_lowerCAmelCase   ,   )
			return resnet_outputs






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







		'''simple docstring'''







		def __init__(self    :   Optional[Any]   ,   _lowerCAmelCase    :   ResNetConfig   ,   **_lowerCAmelCase    :   int   ):
			super().__init__(_lowerCAmelCase   ,   **_lowerCAmelCase   )
			A					=   config.num_labels
			A					=   TFResNetMainLayer(_lowerCAmelCase   ,   name="""resnet"""   )
			# classification head
			A					=   (
			    tf.keras.layers.Dense(config.num_labels   ,   name="""classifier.1"""   )
			    if config.num_labels > 0
			    else tf.keras.layers.Activation("""linear"""   ,   name="""classifier.1"""   )
			)







		def 							A  (self    :   List[str]   ,   _lowerCAmelCase    :   tf.Tensor   ):
			A					=   tf.keras.layers.Flatten()(_lowerCAmelCase   )
			A					=   self.classifier_layer(_lowerCAmelCase   )
			return logits







		@add_start_docstrings_to_model_forward(_lowerCAmelCase   )
		@add_code_sample_docstrings(
		    checkpoint=_IMAGE_CLASS_CHECKPOINT   ,   output_type=_lowerCAmelCase   ,   config_class=_CONFIG_FOR_DOC   ,   expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT   ,   )
		@unpack_inputs
		def 							A  (self    :   List[str]   ,   _lowerCAmelCase    :   tf.Tensor = None   ,   _lowerCAmelCase    :   tf.Tensor = None   ,   _lowerCAmelCase    :   bool = None   ,   _lowerCAmelCase    :   bool = None   ,   _lowerCAmelCase    :   bool = False   ,   ):
			A					=   return_dict if return_dict is not None else self.config.use_return_dict

			A					=   self.resnet(
			    _lowerCAmelCase   ,   output_hidden_states=_lowerCAmelCase   ,   return_dict=_lowerCAmelCase   ,   training=_lowerCAmelCase   )

			A					=   outputs.pooler_output if return_dict else outputs[1]

			A					=   self.classifier(_lowerCAmelCase   )

			A					=   None if labels is None else self.hf_compute_loss(_lowerCAmelCase   ,   _lowerCAmelCase   )

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

			return TFImageClassifierOutputWithNoAttention(loss=_lowerCAmelCase   ,   logits=_lowerCAmelCase   ,   hidden_states=outputs.hidden_states   )








'''simple docstring'''


from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_torch_available


_lowerCamelCase					:						str					     =							{
    'configuration_ernie_m': ['ERNIE_M_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ErnieMConfig'],
}

try:
				if not is_sentencepiece_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				_lowerCamelCase					:						List[str]					     =							['ErnieMTokenizer']

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				_lowerCamelCase					:						Union[str, Any]					     =							[
				    'ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST',
				    'ErnieMForMultipleChoice',
				    'ErnieMForQuestionAnswering',
				    'ErnieMForSequenceClassification',
				    'ErnieMForTokenClassification',
				    'ErnieMModel',
				    'ErnieMPreTrainedModel',
				    'ErnieMForInformationExtraction',
				]


if TYPE_CHECKING:
				from .configuration_ernie_m import ERNIE_M_PRETRAINED_CONFIG_ARCHIVE_MAP, ErnieMConfig

				try:
								if not is_sentencepiece_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_ernie_m import ErnieMTokenizer

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_ernie_m import (
								    ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST,
								    ErnieMForInformationExtraction,
								    ErnieMForMultipleChoice,
								    ErnieMForQuestionAnswering,
								    ErnieMForSequenceClassification,
								    ErnieMForTokenClassification,
								    ErnieMModel,
								    ErnieMPreTrainedModel,
								)


else:
				import sys

				_lowerCamelCase					:						int					     =							_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)

