


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


_A     =  logging.get_logger(__name__)

# General docstring
_A     =  'RegNetConfig'

# Base docstring
_A     =  'facebook/regnet-y-040'
_A     =  [1, 10_88, 7, 7]

# Image classification docstring
_A     =  'facebook/regnet-y-040'
_A     =  'tabby, tabby cat'

_A     =  [
    'facebook/regnet-y-040',
    # See all regnet models at https://huggingface.co/models?filter=regnet
]





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




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




				def   __a		(      self       ,       _UpperCamelCase  )       ->   List[Any]:
							lowerCAmelCase_      			=			self.convolution(self.padding(UpperCAmelCase__  )  )
							lowerCAmelCase_      			=			self.normalization(UpperCAmelCase__  )
							lowerCAmelCase_      			=			self.activation(UpperCAmelCase__  )
							return hidden_state





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




				def __init__(      self       ,       _UpperCamelCase       ,       **_UpperCamelCase  )       ->   Tuple:
							super().__init__(**UpperCAmelCase__  )
							lowerCAmelCase_      			=			config.num_channels
							lowerCAmelCase_      			=			TFRegNetConvLayer(
							    out_channels=config.embedding_size       ,       kernel_size=3       ,       stride=2       ,       activation=config.hidden_act       ,       name="embedder"       ,       )




				def   __a		(      self       ,       _UpperCamelCase  )       ->   Optional[int]:
							lowerCAmelCase_      			=			shape_list(UpperCAmelCase__  )[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)
							lowerCAmelCase_      			=			tf.transpose(UpperCAmelCase__       ,       perm=(0, 2, 3, 1)  )
							lowerCAmelCase_      			=			self.embedder(UpperCAmelCase__  )
							return hidden_state





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




				def __init__(      self       ,       _UpperCamelCase       ,       _UpperCamelCase = 2       ,       **_UpperCamelCase  )       ->   Optional[int]:
							super().__init__(**UpperCAmelCase__  )
							lowerCAmelCase_      			=			tf.keras.layers.ConvaD(
							    filters=UpperCAmelCase__       ,       kernel_size=1       ,       strides=UpperCAmelCase__       ,       use_bias=UpperCAmelCase__       ,       name="convolution"  )
							lowerCAmelCase_      			=			tf.keras.layers.BatchNormalization(epsilon=1e-5       ,       momentum=0.9       ,       name="normalization"  )




				def   __a		(      self       ,       _UpperCamelCase       ,       _UpperCamelCase = False  )       ->   Any:
							return self.normalization(self.convolution(UpperCAmelCase__  )       ,       training=UpperCAmelCase__  )





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




				def __init__(      self       ,       _UpperCamelCase       ,       _UpperCamelCase       ,       **_UpperCamelCase  )       ->   int:
							super().__init__(**UpperCAmelCase__  )
							lowerCAmelCase_      			=			tf.keras.layers.GlobalAveragePoolingaD(keepdims=UpperCAmelCase__       ,       name="pooler"  )
							lowerCAmelCase_      			=			[
							    tf.keras.layers.ConvaD(filters=UpperCAmelCase__       ,       kernel_size=1       ,       activation="relu"       ,       name="attention.0"  ),
							    tf.keras.layers.ConvaD(filters=UpperCAmelCase__       ,       kernel_size=1       ,       activation="sigmoid"       ,       name="attention.2"  ),
							]




				def   __a		(      self       ,       _UpperCamelCase  )       ->   Optional[Any]:
							lowerCAmelCase_      			=			self.pooler(UpperCAmelCase__  )
							for layer_module in self.attention:
										lowerCAmelCase_      			=			layer_module(UpperCAmelCase__  )
							lowerCAmelCase_      			=			hidden_state * pooled
							return hidden_state





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




				def __init__(      self       ,       _UpperCamelCase       ,       _UpperCamelCase       ,       _UpperCamelCase       ,       _UpperCamelCase = 1       ,       **_UpperCamelCase  )       ->   int:
							super().__init__(**UpperCAmelCase__  )
							lowerCAmelCase_      			=			in_channels != out_channels or stride != 1
							lowerCAmelCase_      			=			max(1       ,       out_channels // config.groups_width  )
							lowerCAmelCase_      			=			(
							    TFRegNetShortCut(UpperCAmelCase__       ,       stride=UpperCAmelCase__       ,       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.
							lowerCAmelCase_      			=			[
							    TFRegNetConvLayer(UpperCAmelCase__       ,       kernel_size=1       ,       activation=config.hidden_act       ,       name="layer.0"  ),
							    TFRegNetConvLayer(
							        UpperCAmelCase__       ,       stride=UpperCAmelCase__       ,       groups=UpperCAmelCase__       ,       activation=config.hidden_act       ,       name="layer.1"  ),
							    TFRegNetConvLayer(UpperCAmelCase__       ,       kernel_size=1       ,       activation=UpperCAmelCase__       ,       name="layer.2"  ),
							]
							lowerCAmelCase_      			=			ACTaFN[config.hidden_act]




				def   __a		(      self       ,       _UpperCamelCase  )       ->   Dict:
							lowerCAmelCase_      			=			hidden_state
							for layer_module in self.layers:
										lowerCAmelCase_      			=			layer_module(UpperCAmelCase__  )
							lowerCAmelCase_      			=			self.shortcut(UpperCAmelCase__  )
							hidden_state += residual
							lowerCAmelCase_      			=			self.activation(UpperCAmelCase__  )
							return hidden_state





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




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




				def   __a		(      self       ,       _UpperCamelCase  )       ->   List[str]:
							lowerCAmelCase_      			=			hidden_state
							for layer_module in self.layers:
										lowerCAmelCase_      			=			layer_module(UpperCAmelCase__  )
							lowerCAmelCase_      			=			self.shortcut(UpperCAmelCase__  )
							hidden_state += residual
							lowerCAmelCase_      			=			self.activation(UpperCAmelCase__  )
							return hidden_state





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




				def __init__(      self       ,       _UpperCamelCase       ,       _UpperCamelCase       ,       _UpperCamelCase       ,       _UpperCamelCase = 2       ,       _UpperCamelCase = 2       ,       **_UpperCamelCase  )       ->   List[str]:
							super().__init__(**UpperCAmelCase__  )

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




				def   __a		(      self       ,       _UpperCamelCase  )       ->   int:
							for layer_module in self.layers:
										lowerCAmelCase_      			=			layer_module(UpperCAmelCase__  )
							return hidden_state





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




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




				def   __a		(      self       ,       _UpperCamelCase       ,       _UpperCamelCase = False       ,       _UpperCamelCase = True  )       ->   Dict:
							lowerCAmelCase_      			=			() if output_hidden_states else None

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

										lowerCAmelCase_      			=			stage_module(UpperCAmelCase__  )

							if output_hidden_states:
										lowerCAmelCase_      			=			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=UpperCAmelCase__       ,       hidden_states=UpperCAmelCase__  )





@keras_serializable
class 			_lowerCAmelCase      (			tf.keras.layers.Layer ):
				_lowercase    	=RegNetConfig




				def __init__(      self       ,       _UpperCamelCase       ,       **_UpperCamelCase  )       ->   Tuple:
							super().__init__(**UpperCAmelCase__  )
							lowerCAmelCase_      			=			config
							lowerCAmelCase_      			=			TFRegNetEmbeddings(UpperCAmelCase__       ,       name="embedder"  )
							lowerCAmelCase_      			=			TFRegNetEncoder(UpperCAmelCase__       ,       name="encoder"  )
							lowerCAmelCase_      			=			tf.keras.layers.GlobalAveragePoolingaD(keepdims=UpperCAmelCase__       ,       name="pooler"  )




				@unpack_inputs
				def   __a		(      self       ,       _UpperCamelCase       ,       _UpperCamelCase = None       ,       _UpperCamelCase = None       ,       _UpperCamelCase = False       ,       )       ->   Union[str, Any]:
							lowerCAmelCase_      			=			(
							    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
							)
							lowerCAmelCase_      			=			return_dict if return_dict is not None else self.config.use_return_dict

							lowerCAmelCase_      			=			self.embedder(UpperCAmelCase__       ,       training=UpperCAmelCase__  )

							lowerCAmelCase_      			=			self.encoder(
							    UpperCAmelCase__       ,       output_hidden_states=UpperCAmelCase__       ,       return_dict=UpperCAmelCase__       ,       training=UpperCAmelCase__  )

							lowerCAmelCase_      			=			encoder_outputs[0]
							lowerCAmelCase_      			=			self.pooler(UpperCAmelCase__  )

							# Change to NCHW output format have uniformity in the modules
							lowerCAmelCase_      			=			tf.transpose(UpperCAmelCase__       ,       perm=(0, 3, 1, 2)  )
							lowerCAmelCase_      			=			tf.transpose(UpperCAmelCase__       ,       perm=(0, 3, 1, 2)  )

							# Change the other hidden state outputs to NCHW as well
							if output_hidden_states:
										lowerCAmelCase_      			=			tuple([tf.transpose(UpperCAmelCase__       ,       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=UpperCAmelCase__       ,       pooler_output=UpperCAmelCase__       ,       hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states       ,       )





class 			_lowerCAmelCase      (			__a ):
				_lowercase    	=RegNetConfig
				_lowercase    	='regnet'
				_lowercase    	='pixel_values'




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


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

_A     =  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 			_lowerCAmelCase      (			__a ):




				def __init__(      self       ,       _UpperCamelCase       ,       *_UpperCamelCase       ,       **_UpperCamelCase  )       ->   Any:
							super().__init__(UpperCAmelCase__       ,       *UpperCAmelCase__       ,       **UpperCAmelCase__  )
							lowerCAmelCase_      			=			TFRegNetMainLayer(UpperCAmelCase__       ,       name="regnet"  )




				@unpack_inputs
				@add_start_docstrings_to_model_forward(UpperCAmelCase__  )
				@add_code_sample_docstrings(
				    checkpoint=_CHECKPOINT_FOR_DOC       ,       output_type=UpperCAmelCase__       ,       config_class=_CONFIG_FOR_DOC       ,       modality="vision"       ,       expected_output=_EXPECTED_OUTPUT_SHAPE       ,       )
				def   __a		(      self       ,       _UpperCamelCase       ,       _UpperCamelCase = None       ,       _UpperCamelCase = None       ,       _UpperCamelCase=False       ,       )       ->   Any:
							lowerCAmelCase_      			=			(
							    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
							)
							lowerCAmelCase_      			=			return_dict if return_dict is not None else self.config.use_return_dict

							lowerCAmelCase_      			=			self.regnet(
							    pixel_values=UpperCAmelCase__       ,       output_hidden_states=UpperCAmelCase__       ,       return_dict=UpperCAmelCase__       ,       training=UpperCAmelCase__       ,       )
							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(
    '''\n    RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    '''      ,   __a      ,   )
class 			_lowerCAmelCase      (			__a      ,   __a ):




				def __init__(      self       ,       _UpperCamelCase       ,       *_UpperCamelCase       ,       **_UpperCamelCase  )       ->   List[str]:
							super().__init__(UpperCAmelCase__       ,       *UpperCAmelCase__       ,       **UpperCAmelCase__  )
							lowerCAmelCase_      			=			config.num_labels
							lowerCAmelCase_      			=			TFRegNetMainLayer(UpperCAmelCase__       ,       name="regnet"  )
							# classification head
							lowerCAmelCase_      			=			[
							    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(UpperCAmelCase__  )
				@add_code_sample_docstrings(
				    checkpoint=_IMAGE_CLASS_CHECKPOINT       ,       output_type=UpperCAmelCase__       ,       config_class=_CONFIG_FOR_DOC       ,       expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT       ,       )
				def   __a		(      self       ,       _UpperCamelCase = None       ,       _UpperCamelCase = None       ,       _UpperCamelCase = None       ,       _UpperCamelCase = None       ,       _UpperCamelCase=False       ,       )       ->   Dict:
							lowerCAmelCase_      			=			(
							    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
							)
							lowerCAmelCase_      			=			return_dict if return_dict is not None else self.config.use_return_dict

							lowerCAmelCase_      			=			self.regnet(
							    UpperCAmelCase__       ,       output_hidden_states=UpperCAmelCase__       ,       return_dict=UpperCAmelCase__       ,       training=UpperCAmelCase__  )

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

							lowerCAmelCase_      			=			self.classifier[0](UpperCAmelCase__  )
							lowerCAmelCase_      			=			self.classifier[1](UpperCAmelCase__  )

							lowerCAmelCase_      			=			None if labels is None else self.hf_compute_loss(labels=UpperCAmelCase__       ,       logits=UpperCAmelCase__  )

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

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





from string import ascii_lowercase, ascii_uppercase






def      lowerCamelCase__       (       __lowerCAmelCase						:						str       ):




			"""simple docstring"""


			if not sentence:
						return ""
			lowerCAmelCase_      			=			dict(zip(__lowerCAmelCase					,							__lowerCAmelCase       )       )
			return lower_to_upper.get(sentence[0]					,							sentence[0]       ) + sentence[1:]


if __name__ == "__main__":
			from doctest import testmod

			testmod()



