


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


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

# General docstring
_lowercase							:   int						=						"RegNetConfig"

# Base docstring
_lowercase							:   Any						=						"facebook/regnet-y-040"
_lowercase							:   Union[str, Any]						=						[1, 1088, 7, 7]

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

_lowercase							:   List[str]						=						[
    "facebook/regnet-y-040",
    # See all regnet models at https://huggingface.co/models?filter=regnet
]






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

				"""simple docstring"""







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

				def   _UpperCAmelCase       (    self					, a__ )						->					Union[str, Any]:
					A             =					self.convolution(self.padding(a__ ) )
					A             =					self.normalization(a__ )
					A             =					self.activation(a__ )
					return hidden_state






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

				"""simple docstring"""







				def __init__(    self					, a__					, **a__ )						->					Dict:
					super().__init__(**a__ )
					A             =					config.num_channels
					A             =					TFRegNetConvLayer(
					    out_channels=config.embedding_size					, kernel_size=3					, stride=2					, activation=config.hidden_act					, name="""embedder"""					, )

				def   _UpperCAmelCase       (    self					, a__ )						->					List[Any]:
					A             =					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             =					tf.transpose(a__					, perm=(0, 2, 3, 1) )
					A             =					self.embedder(a__ )
					return hidden_state






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

				"""simple docstring"""







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

				def   _UpperCAmelCase       (    self					, a__					, a__ = False )						->					tf.Tensor:
					return self.normalization(self.convolution(a__ )					, training=a__ )






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

				"""simple docstring"""







				def __init__(    self					, a__					, a__					, **a__ )						->					Tuple:
					super().__init__(**a__ )
					A             =					tf.keras.layers.GlobalAveragePoolingaD(keepdims=a__					, name="""pooler""" )
					A             =					[
					    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   _UpperCAmelCase       (    self					, a__ )						->					Optional[Any]:
					# [batch_size, h, w, num_channels] -> [batch_size, 1, 1, num_channels]
					A             =					self.pooler(a__ )
					for layer_module in self.attention:
						A             =					layer_module(a__ )
					A             =					hidden_state * pooled
					return hidden_state






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

				"""simple docstring"""







				def __init__(    self					, a__					, a__					, a__					, a__ = 1					, **a__ )						->					List[Any]:
					super().__init__(**a__ )
					A             =					in_channels != out_channels or stride != 1
					A             =					max(1					, out_channels // config.groups_width )
					A             =					(
					    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             =					[
					    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             =					ACTaFN[config.hidden_act]

				def   _UpperCAmelCase       (    self					, a__ )						->					Dict:
					A             =					hidden_state
					for layer_module in self.layers:
						A             =					layer_module(a__ )
					A             =					self.shortcut(a__ )
					hidden_state += residual
					A             =					self.activation(a__ )
					return hidden_state






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

				"""simple docstring"""







				def __init__(    self					, a__					, a__					, a__					, a__ = 1					, **a__ )						->					Tuple:
					super().__init__(**a__ )
					A             =					in_channels != out_channels or stride != 1
					A             =					max(1					, out_channels // config.groups_width )
					A             =					(
					    TFRegNetShortCut(a__					, stride=a__					, name="""shortcut""" )
					    if should_apply_shortcut
					    else tf.keras.layers.Activation("""linear"""					, name="""shortcut""" )
					)
					A             =					[
					    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             =					ACTaFN[config.hidden_act]

				def   _UpperCAmelCase       (    self					, a__ )						->					List[str]:
					A             =					hidden_state
					for layer_module in self.layers:
						A             =					layer_module(a__ )
					A             =					self.shortcut(a__ )
					hidden_state += residual
					A             =					self.activation(a__ )
					return hidden_state






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

				"""simple docstring"""







				def __init__(    self					, a__					, a__					, a__					, a__ = 2					, a__ = 2					, **a__ )						->					Union[str, Any]:
					super().__init__(**a__ )

					A             =					TFRegNetXLayer if config.layer_type == """x""" else TFRegNetYLayer
					A             =					[
					    # 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   _UpperCAmelCase       (    self					, a__ )						->					Dict:
					for layer_module in self.layers:
						A             =					layer_module(a__ )
					return hidden_state






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

				"""simple docstring"""







				def __init__(    self					, a__					, **a__ )						->					Dict:
					super().__init__(**a__ )
					A             =					[]
					# 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             =					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   _UpperCAmelCase       (    self					, a__					, a__ = False					, a__ = True )						->					TFBaseModelOutputWithNoAttention:
					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(a__ )

					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=a__					, hidden_states=a__ )






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

				"""simple docstring"""







				lowerCAmelCase					=			RegNetConfig

				def __init__(    self					, a__					, **a__ )						->					str:
					super().__init__(**a__ )
					A             =					config
					A             =					TFRegNetEmbeddings(a__					, name="""embedder""" )
					A             =					TFRegNetEncoder(a__					, name="""encoder""" )
					A             =					tf.keras.layers.GlobalAveragePoolingaD(keepdims=a__					, name="""pooler""" )

				@unpack_inputs
				def   _UpperCAmelCase       (    self					, a__					, a__ = None					, a__ = None					, a__ = False					, )						->					TFBaseModelOutputWithPoolingAndNoAttention:
					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.embedder(a__					, training=a__ )

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

					A             =					encoder_outputs[0]
					A             =					self.pooler(a__ )

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

					# Change the other hidden state outputs to NCHW as well
					if output_hidden_states:
						A             =					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    _UpperCamelCase     (       __snake_case					):

				"""simple docstring"""







				lowerCAmelCase					=			RegNetConfig
				lowerCAmelCase					=			'regnet'
				lowerCAmelCase					=			'pixel_values'

				@property
				def   _UpperCAmelCase       (    self )						->					int:
					return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 224, 224)					, dtype=tf.floataa )}
_lowercase							:   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"

_lowercase							:   Optional[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.'     ,		__snake_case     ,		)
class    _UpperCamelCase     (       __snake_case					):

				"""simple docstring"""







				def __init__(    self					, a__					, *a__					, **a__ )						->					Optional[Any]:
					super().__init__(a__					, *a__					, **a__ )
					A             =					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   _UpperCAmelCase       (    self					, a__					, a__ = None					, a__ = None					, a__=False					, )						->					Union[TFBaseModelOutputWithPoolingAndNoAttention, Tuple[tf.Tensor]]:
					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.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(
    '\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    '     ,		__snake_case     ,		)
class    _UpperCamelCase     (       __snake_case     ,		__snake_case					):

				"""simple docstring"""







				def __init__(    self					, a__					, *a__					, **a__ )						->					str:
					super().__init__(a__					, *a__					, **a__ )
					A             =					config.num_labels
					A             =					TFRegNetMainLayer(a__					, name="""regnet""" )
					# classification head
					A             =					[
					    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   _UpperCAmelCase       (    self					, a__ = None					, a__ = None					, a__ = None					, a__ = None					, a__=False					, )						->					Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]:
					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.regnet(
					    a__					, output_hidden_states=a__					, return_dict=a__					, training=a__ )

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

					A             =					self.classifier[0](a__ )
					A             =					self.classifier[1](a__ )

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

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

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







import os
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils import AddedToken
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
				from .tokenization_albert import AlbertTokenizer
else:
				_lowercase							:   List[str]						=						None

_lowercase							:   List[str]						=						logging.get_logger(__name__)
_lowercase							:   List[Any]						=						{"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"}

_lowercase							:   str						=						{
    "vocab_file": {
        "albert-base-v1": "https://huggingface.co/albert-base-v1/resolve/main/spiece.model",
        "albert-large-v1": "https://huggingface.co/albert-large-v1/resolve/main/spiece.model",
        "albert-xlarge-v1": "https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model",
        "albert-xxlarge-v1": "https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model",
        "albert-base-v2": "https://huggingface.co/albert-base-v2/resolve/main/spiece.model",
        "albert-large-v2": "https://huggingface.co/albert-large-v2/resolve/main/spiece.model",
        "albert-xlarge-v2": "https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model",
        "albert-xxlarge-v2": "https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model",
    },
    "tokenizer_file": {
        "albert-base-v1": "https://huggingface.co/albert-base-v1/resolve/main/tokenizer.json",
        "albert-large-v1": "https://huggingface.co/albert-large-v1/resolve/main/tokenizer.json",
        "albert-xlarge-v1": "https://huggingface.co/albert-xlarge-v1/resolve/main/tokenizer.json",
        "albert-xxlarge-v1": "https://huggingface.co/albert-xxlarge-v1/resolve/main/tokenizer.json",
        "albert-base-v2": "https://huggingface.co/albert-base-v2/resolve/main/tokenizer.json",
        "albert-large-v2": "https://huggingface.co/albert-large-v2/resolve/main/tokenizer.json",
        "albert-xlarge-v2": "https://huggingface.co/albert-xlarge-v2/resolve/main/tokenizer.json",
        "albert-xxlarge-v2": "https://huggingface.co/albert-xxlarge-v2/resolve/main/tokenizer.json",
    },
}

_lowercase							:   List[str]						=						{
    "albert-base-v1": 512,
    "albert-large-v1": 512,
    "albert-xlarge-v1": 512,
    "albert-xxlarge-v1": 512,
    "albert-base-v2": 512,
    "albert-large-v2": 512,
    "albert-xlarge-v2": 512,
    "albert-xxlarge-v2": 512,
}

_lowercase							:   Dict						=						"▁"






class    _UpperCamelCase     (       __snake_case					):

				"""simple docstring"""







				lowerCAmelCase					=			VOCAB_FILES_NAMES
				lowerCAmelCase					=			PRETRAINED_VOCAB_FILES_MAP
				lowerCAmelCase					=			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				lowerCAmelCase					=			AlbertTokenizer

				def __init__(    self					, a__=None					, a__=None					, a__=True					, a__=True					, a__=False					, a__="[CLS]"					, a__="[SEP]"					, a__="<unk>"					, a__="[SEP]"					, a__="<pad>"					, a__="[CLS]"					, a__="[MASK]"					, **a__					, )						->					Dict:
					# Mask token behave like a normal word, i.e. include the space before it and
					# is included in the raw text, there should be a match in a non-normalized sentence.
					A             =					(
					    AddedToken(a__					, lstrip=a__					, rstrip=a__					, normalized=a__ )
					    if isinstance(a__					, a__ )
					    else mask_token
					)

					super().__init__(
					    a__					, tokenizer_file=a__					, do_lower_case=a__					, remove_space=a__					, keep_accents=a__					, bos_token=a__					, eos_token=a__					, unk_token=a__					, sep_token=a__					, pad_token=a__					, cls_token=a__					, mask_token=a__					, **a__					, )

					A             =					do_lower_case
					A             =					remove_space
					A             =					keep_accents
					A             =					vocab_file
					A             =					False if not self.vocab_file else True

				def   _UpperCAmelCase       (    self					, a__					, a__ = None )						->					List[int]:
					A             =					[self.sep_token_id]
					A             =					[self.cls_token_id]
					if token_ids_a is None:
						return cls + token_ids_a + sep
					return cls + token_ids_a + sep + token_ids_a + sep

				def   _UpperCAmelCase       (    self					, a__					, a__ = None )						->					List[int]:
					A             =					[self.sep_token_id]
					A             =					[self.cls_token_id]

					if token_ids_a is None:
						return len(cls + token_ids_a + sep ) * [0]
					return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1]

				def   _UpperCAmelCase       (    self					, a__					, a__ = None )						->					Tuple[str]:
					if not self.can_save_slow_tokenizer:
						raise ValueError(
						    """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """
						    """tokenizer.""" )

					if not os.path.isdir(a__ ):
						logger.error(f'Vocabulary path ({save_directory}) should be a directory' )
						return
					A             =					os.path.join(
					    a__					, (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] )

					if os.path.abspath(self.vocab_file ) != os.path.abspath(a__ ):
						copyfile(self.vocab_file					, a__ )

					return (out_vocab_file,)



