




"""simple docstring"""




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, 1_0_8_8, 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   UpperCAmelCase_    (			tf.keras.layers.Layer		):
		def __init__(						self				,						UpperCamelCase_				,						UpperCamelCase_ = 3				,						UpperCamelCase_ = 1				,						UpperCamelCase_ = 1				,						UpperCamelCase_ = "relu"				,						**UpperCamelCase_				,						)						->       List[str]:
					super().__init__(**A_       )
					# The padding and conv has been verified in
					# https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb
					__lowercase			:  Union[str, Any]								=       tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2       )
					__lowercase			:  Dict								=       tf.keras.layers.ConvaD(
					    filters=A_				,						kernel_size=A_				,						strides=A_				,						padding='''VALID'''				,						groups=A_				,						use_bias=A_				,						name='''convolution'''				,						)
					__lowercase			:  Dict								=       tf.keras.layers.BatchNormalization(epsilon=1E-5				,						momentum=0.9				,						name='''normalization'''       )
					__lowercase			:  int								=       ACTaFN[activation] if activation is not None else tf.identity
		def 	_lowerCamelCase							(						self				,						UpperCamelCase_       )						->       List[Any]:
					__lowercase			:  Tuple								=       self.convolution(self.padding(A_       )       )
					__lowercase			:  Any								=       self.normalization(A_       )
					__lowercase			:  Tuple								=       self.activation(A_       )
					return hidden_state


class   UpperCAmelCase_    (			tf.keras.layers.Layer		):
		def __init__(						self				,						UpperCamelCase_				,						**UpperCamelCase_       )						->       Optional[int]:
					super().__init__(**A_       )
					__lowercase			:  Dict								=       config.num_channels
					__lowercase			:  Optional[int]								=       TFRegNetConvLayer(
					    out_channels=config.embedding_size				,						kernel_size=3				,						stride=2				,						activation=config.hidden_act				,						name='''embedder'''				,						)
		def 	_lowerCamelCase							(						self				,						UpperCamelCase_       )						->       List[str]:
					__lowercase			:  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)
					__lowercase			:  Optional[Any]								=       tf.transpose(A_				,						perm=(0, 2, 3, 1)       )
					__lowercase			:  Tuple								=       self.embedder(A_       )
					return hidden_state


class   UpperCAmelCase_    (			tf.keras.layers.Layer		):
		def __init__(						self				,						UpperCamelCase_				,						UpperCamelCase_ = 2				,						**UpperCamelCase_       )						->       int:
					super().__init__(**A_       )
					__lowercase			:  Optional[Any]								=       tf.keras.layers.ConvaD(
					    filters=A_				,						kernel_size=1				,						strides=A_				,						use_bias=A_				,						name='''convolution'''       )
					__lowercase			:  Dict								=       tf.keras.layers.BatchNormalization(epsilon=1E-5				,						momentum=0.9				,						name='''normalization'''       )
		def 	_lowerCamelCase							(						self				,						UpperCamelCase_				,						UpperCamelCase_ = False       )						->       Tuple:
					return self.normalization(self.convolution(A_       )				,						training=A_       )


class   UpperCAmelCase_    (			tf.keras.layers.Layer		):
		def __init__(						self				,						UpperCamelCase_				,						UpperCamelCase_				,						**UpperCamelCase_       )						->       List[str]:
					super().__init__(**A_       )
					__lowercase			:  Tuple								=       tf.keras.layers.GlobalAveragePoolingaD(keepdims=A_				,						name='''pooler'''       )
					__lowercase			:  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 	_lowerCamelCase							(						self				,						UpperCamelCase_       )						->       Dict:
					__lowercase			:  Optional[int]								=       self.pooler(A_       )
					for layer_module in self.attention:
								__lowercase			:  List[str]								=       layer_module(A_       )
					__lowercase			:  Tuple								=       hidden_state * pooled
					return hidden_state


class   UpperCAmelCase_    (			tf.keras.layers.Layer		):
		def __init__(						self				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_ = 1				,						**UpperCamelCase_       )						->       Dict:
					super().__init__(**A_       )
					__lowercase			:  Tuple								=       in_channels != out_channels or stride != 1
					__lowercase			:  str								=       max(1				,						out_channels // config.groups_width       )
					__lowercase			:  Union[str, Any]								=       (
					    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.
					__lowercase			:  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'''       ),
					]
					__lowercase			:  Tuple								=       ACTaFN[config.hidden_act]
		def 	_lowerCamelCase							(						self				,						UpperCamelCase_       )						->       Dict:
					__lowercase			:  str								=       hidden_state
					for layer_module in self.layers:
								__lowercase			:  Optional[int]								=       layer_module(A_       )
					__lowercase			:  Optional[Any]								=       self.shortcut(A_       )
					hidden_state += residual
					__lowercase			:  int								=       self.activation(A_       )
					return hidden_state


class   UpperCAmelCase_    (			tf.keras.layers.Layer		):
		def __init__(						self				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_ = 1				,						**UpperCamelCase_       )						->       List[Any]:
					super().__init__(**A_       )
					__lowercase			:  Tuple								=       in_channels != out_channels or stride != 1
					__lowercase			:  Optional[int]								=       max(1				,						out_channels // config.groups_width       )
					__lowercase			:  Union[str, Any]								=       (
					    TFRegNetShortCut(A_				,						stride=A_				,						name='''shortcut'''       )
					    if should_apply_shortcut
					    else tf.keras.layers.Activation('''linear'''				,						name='''shortcut'''       )
					)
					__lowercase			:  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'''       ),
					]
					__lowercase			:  Optional[Any]								=       ACTaFN[config.hidden_act]
		def 	_lowerCamelCase							(						self				,						UpperCamelCase_       )						->       Dict:
					__lowercase			:  Dict								=       hidden_state
					for layer_module in self.layers:
								__lowercase			:  Optional[Any]								=       layer_module(A_       )
					__lowercase			:  str								=       self.shortcut(A_       )
					hidden_state += residual
					__lowercase			:  Any								=       self.activation(A_       )
					return hidden_state


class   UpperCAmelCase_    (			tf.keras.layers.Layer		):
		def __init__(						self				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_ = 2				,						UpperCamelCase_ = 2				,						**UpperCamelCase_       )						->       str:
					super().__init__(**A_       )

					__lowercase			:  List[Any]								=       TFRegNetXLayer if config.layer_type == '''x''' else TFRegNetYLayer
					__lowercase			:  str								=       [
					    # 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 	_lowerCamelCase							(						self				,						UpperCamelCase_       )						->       Union[str, Any]:

					for layer_module in self.layers:
								__lowercase			:  Optional[Any]								=       layer_module(A_       )
					return hidden_state


class   UpperCAmelCase_    (			tf.keras.layers.Layer		):
		def __init__(						self				,						UpperCamelCase_				,						**UpperCamelCase_       )						->       Dict:
					super().__init__(**A_       )
					__lowercase			:  List[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'''				,						)       )
					__lowercase			:  Dict								=       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 	_lowerCamelCase							(						self				,						UpperCamelCase_				,						UpperCamelCase_ = False				,						UpperCamelCase_ = True       )						->       Union[str, Any]:
					__lowercase			:  str								=       () if output_hidden_states else None

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

								__lowercase			:  Any								=       stage_module(A_       )

					if output_hidden_states:
								__lowercase			:  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   UpperCAmelCase_    (			tf.keras.layers.Layer		):
		UpperCamelCase							=RegNetConfig
		def __init__(						self				,						UpperCamelCase_				,						**UpperCamelCase_       )						->       Optional[Any]:
					super().__init__(**A_       )
					__lowercase			:  Optional[int]								=       config
					__lowercase			:  int								=       TFRegNetEmbeddings(A_				,						name='''embedder'''       )
					__lowercase			:  str								=       TFRegNetEncoder(A_				,						name='''encoder'''       )
					__lowercase			:  List[str]								=       tf.keras.layers.GlobalAveragePoolingaD(keepdims=A_				,						name='''pooler'''       )
		@unpack_inputs
		def 	_lowerCamelCase							(						self				,						UpperCamelCase_				,						UpperCamelCase_ = None				,						UpperCamelCase_ = None				,						UpperCamelCase_ = False				,						)						->       Union[str, Any]:
					__lowercase			:  List[str]								=       (
					    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
					)
					__lowercase			:  Tuple								=       return_dict if return_dict is not None else self.config.use_return_dict

					__lowercase			:  Tuple								=       self.embedder(A_				,						training=A_       )

					__lowercase			:  Optional[Any]								=       self.encoder(
					    A_				,						output_hidden_states=A_				,						return_dict=A_				,						training=A_       )

					__lowercase			:  str								=       encoder_outputs[0]
					__lowercase			:  Union[str, Any]								=       self.pooler(A_       )

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

					# Change the other hidden state outputs to NCHW as well
					if output_hidden_states:
								__lowercase			:  Optional[int]								=       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_    (			__SCREAMING_SNAKE_CASE		):
		UpperCamelCase							=RegNetConfig
		UpperCamelCase							="""regnet"""
		UpperCamelCase							="""pixel_values"""
		@property
		def 	_lowerCamelCase							(						self       )						->       Optional[Any]:
					return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 2_24, 2_24)				,						dtype=tf.floataa       )}


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

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

@add_start_docstrings(
    "The bare RegNet model outputting raw features without any specific head on top."   ,					__SCREAMING_SNAKE_CASE   ,					)
class   UpperCAmelCase_    (			__SCREAMING_SNAKE_CASE		):
		def __init__(						self				,						UpperCamelCase_				,						*UpperCamelCase_				,						**UpperCamelCase_       )						->       Union[str, Any]:
					super().__init__(A_				,						*A_				,						**A_       )
					__lowercase			:  Optional[int]								=       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 	_lowerCamelCase							(						self				,						UpperCamelCase_				,						UpperCamelCase_ = None				,						UpperCamelCase_ = None				,						UpperCamelCase_=False				,						)						->       Dict:
					__lowercase			:  Any								=       (
					    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
					)
					__lowercase			:  List[Any]								=       return_dict if return_dict is not None else self.config.use_return_dict

					__lowercase			:  List[str]								=       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    "   ,					__SCREAMING_SNAKE_CASE   ,					)
class   UpperCAmelCase_    (			__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE		):
		def __init__(						self				,						UpperCamelCase_				,						*UpperCamelCase_				,						**UpperCamelCase_       )						->       Tuple:
					super().__init__(A_				,						*A_				,						**A_       )
					__lowercase			:  Tuple								=       config.num_labels
					__lowercase			:  Dict								=       TFRegNetMainLayer(A_				,						name='''regnet'''       )
					# classification head
					__lowercase			:  str								=       [
					    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 	_lowerCamelCase							(						self				,						UpperCamelCase_ = None				,						UpperCamelCase_ = None				,						UpperCamelCase_ = None				,						UpperCamelCase_ = None				,						UpperCamelCase_=False				,						)						->       Union[str, Any]:
					__lowercase			:  Any								=       (
					    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
					)
					__lowercase			:  Optional[Any]								=       return_dict if return_dict is not None else self.config.use_return_dict

					__lowercase			:  Dict								=       self.regnet(
					    A_				,						output_hidden_states=A_				,						return_dict=A_				,						training=A_       )

					__lowercase			:  str								=       outputs.pooler_output if return_dict else outputs[1]

					__lowercase			:  Tuple								=       self.classifier[0](A_       )
					__lowercase			:  List[Any]								=       self.classifier[1](A_       )

					__lowercase			:  Any								=       None if labels is None else self.hf_compute_loss(labels=A_				,						logits=A_       )

					if not return_dict:
								__lowercase			:  Any								=       (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 re

from flax.core.frozen_dict import freeze
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.experimental import PartitionSpec as P


# Sentinels
_A					:				Optional[int]				=			object()

# For specifying empty leaf dict `{}`
_A					:				Tuple				=			object()


def 							__snake_case							(    lowerCAmelCase_ ,							lowerCAmelCase_	)				->						Dict:
    SCREAMING_SNAKE_CASE__											=			tuple((re.compile(x + '''$'''	) for x in qs)	)
    for i in range(len(lowerCAmelCase_	) - len(lowerCAmelCase_	) + 1	):
        SCREAMING_SNAKE_CASE__											=			[x.match(lowerCAmelCase_	) for x, y in zip(lowerCAmelCase_ ,							ks[i:]	)]
        if matches and all(lowerCAmelCase_	):
            return True
    return False


def 							__snake_case							(    lowerCAmelCase_	)				->						int:
    def replace(lowerCAmelCase_ ,							lowerCAmelCase_	):
        for rule, replacement in rules:
            if _match(lowerCAmelCase_ ,							lowerCAmelCase_	):
                return replacement
        return val

    return replace


def 							__snake_case							(    )				->						Optional[int]:
    return [
        # embeddings
        (("transformer", "wpe", "embedding"), P('''mp''' ,							lowerCAmelCase_	)),
        (("transformer", "wte", "embedding"), P('''mp''' ,							lowerCAmelCase_	)),
        # atention
        (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(lowerCAmelCase_ ,							'''mp'''	)),
        (("attention", "out_proj", "kernel"), P('''mp''' ,							lowerCAmelCase_	)),
        (("attention", "out_proj", "bias"), None),
        # mlp
        (("mlp", "c_fc", "kernel"), P(lowerCAmelCase_ ,							'''mp'''	)),
        (("mlp", "c_fc", "bias"), P('''mp'''	)),
        (("mlp", "c_proj", "kernel"), P('''mp''' ,							lowerCAmelCase_	)),
        (("mlp", "c_proj", "bias"), None),
        # layer norms
        ((r"ln_\d+", "bias"), None),
        ((r"\d+", r"ln_\d+", "scale"), None),
        (("ln_f", "bias"), None),
        (("ln_f", "scale"), None),
    ]



def 							__snake_case							(    lowerCAmelCase_	)				->						Optional[Any]:
    SCREAMING_SNAKE_CASE__											=			_get_partition_rules()
    SCREAMING_SNAKE_CASE__											=			_replacement_rules(lowerCAmelCase_	)
    SCREAMING_SNAKE_CASE__											=			{k: _unmatched for k in flatten_dict(lowerCAmelCase_	)}
    SCREAMING_SNAKE_CASE__											=			{k: replace(lowerCAmelCase_ ,							lowerCAmelCase_	) for k, v in initd.items()}
    assert _unmatched not in result.values(), "Incomplete partition spec."
    return freeze(unflatten_dict(lowerCAmelCase_	)	)
