

"""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


SCREAMING_SNAKE_CASE_					:	str							  =						logging.get_logger(__name__)

# General docstring
SCREAMING_SNAKE_CASE_					:	str							  =						'RegNetConfig'

# Base docstring
SCREAMING_SNAKE_CASE_					:	Union[str, Any]							  =						'facebook/regnet-y-040'
SCREAMING_SNAKE_CASE_					:	str							  =						[1, 1_0_8_8, 7, 7]

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

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



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



    """simple docstring"""



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






         """simple docstring"""


         super().__init__(**UpperCamelCase       )
         # 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=UpperCamelCase    ,						kernel_size=UpperCamelCase    ,						strides=UpperCamelCase    ,						padding="""VALID"""    ,						groups=UpperCamelCase    ,						use_bias=UpperCamelCase    ,						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: Optional[Any]    ,						UpperCamelCase: Any       ):






         """simple docstring"""


         A__										=  self.convolution(self.padding(UpperCamelCase       )       )
         A__										=  self.normalization(UpperCamelCase       )
         A__										=  self.activation(UpperCamelCase       )
         return hidden_state



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



    """simple docstring"""



    def __init__(     self: str    ,						UpperCamelCase: RegNetConfig    ,						**UpperCamelCase: Optional[int]       ):






         """simple docstring"""


         super().__init__(**UpperCamelCase       )
         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: Any    ,						UpperCamelCase: Optional[int]       ):






         """simple docstring"""


         A__										=  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)
         A__										=  tf.transpose(UpperCamelCase    ,						perm=(0, 2, 3, 1)       )
         A__										=  self.embedder(UpperCamelCase       )
         return hidden_state



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



    """simple docstring"""



    def __init__(     self: Union[str, Any]    ,						UpperCamelCase: int    ,						UpperCamelCase: int = 2    ,						**UpperCamelCase: Any       ):






         """simple docstring"""


         super().__init__(**UpperCamelCase       )
         A__										=  tf.keras.layers.ConvaD(
             filters=UpperCamelCase    ,						kernel_size=1    ,						strides=UpperCamelCase    ,						use_bias=UpperCamelCase    ,						name="""convolution"""       )
         A__										=  tf.keras.layers.BatchNormalization(epsilon=1e-5    ,						momentum=0.9    ,						name="""normalization"""       )





    def 				UpperCamelCase	(     self: Optional[int]    ,						UpperCamelCase: tf.Tensor    ,						UpperCamelCase: bool = False       ):






         """simple docstring"""


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



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



    """simple docstring"""



    def __init__(     self: Dict    ,						UpperCamelCase: int    ,						UpperCamelCase: int    ,						**UpperCamelCase: Dict       ):






         """simple docstring"""


         super().__init__(**UpperCamelCase       )
         A__										=  tf.keras.layers.GlobalAveragePoolingaD(keepdims=UpperCamelCase    ,						name="""pooler"""       )
         A__										=  [
             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 				UpperCamelCase	(     self: Any    ,						UpperCamelCase: Optional[int]       ):






         """simple docstring"""


         A__										=  self.pooler(UpperCamelCase       )
         for layer_module in self.attention:
              A__										=  layer_module(UpperCamelCase       )
         A__										=  hidden_state * pooled
         return hidden_state



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



    """simple docstring"""



    def __init__(     self: Optional[Any]    ,						UpperCamelCase: RegNetConfig    ,						UpperCamelCase: int    ,						UpperCamelCase: int    ,						UpperCamelCase: int = 1    ,						**UpperCamelCase: Tuple       ):






         """simple docstring"""


         super().__init__(**UpperCamelCase       )
         A__										=  in_channels != out_channels or stride != 1
         A__										=  max(1    ,						out_channels // config.groups_width       )
         A__										=  (
             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.
         A__										=  [
             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"""       ),
         ]
         A__										=  ACTaFN[config.hidden_act]





    def 				UpperCamelCase	(     self: Dict    ,						UpperCamelCase: str       ):






         """simple docstring"""


         A__										=  hidden_state
         for layer_module in self.layers:
              A__										=  layer_module(UpperCamelCase       )
         A__										=  self.shortcut(UpperCamelCase       )
         hidden_state += residual
         A__										=  self.activation(UpperCamelCase       )
         return hidden_state



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



    """simple docstring"""



    def __init__(     self: List[str]    ,						UpperCamelCase: RegNetConfig    ,						UpperCamelCase: int    ,						UpperCamelCase: int    ,						UpperCamelCase: int = 1    ,						**UpperCamelCase: Any       ):






         """simple docstring"""


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





    def 				UpperCamelCase	(     self: Optional[int]    ,						UpperCamelCase: List[Any]       ):






         """simple docstring"""


         A__										=  hidden_state
         for layer_module in self.layers:
              A__										=  layer_module(UpperCamelCase       )
         A__										=  self.shortcut(UpperCamelCase       )
         hidden_state += residual
         A__										=  self.activation(UpperCamelCase       )
         return hidden_state



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



    """simple docstring"""



    def __init__(     self: Tuple    ,						UpperCamelCase: RegNetConfig    ,						UpperCamelCase: int    ,						UpperCamelCase: int    ,						UpperCamelCase: int = 2    ,						UpperCamelCase: int = 2    ,						**UpperCamelCase: Any       ):






         """simple docstring"""


         super().__init__(**UpperCamelCase       )

         A__										=  TFRegNetXLayer if config.layer_type == """x""" else TFRegNetYLayer
         A__										=  [
             # 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 				UpperCamelCase	(     self: Tuple    ,						UpperCamelCase: Optional[Any]       ):






         """simple docstring"""


         for layer_module in self.layers:
              A__										=  layer_module(UpperCamelCase       )
         return hidden_state



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



    """simple docstring"""



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






         """simple docstring"""


         super().__init__(**UpperCamelCase       )
         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(
                 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"""    ,						)       )
         A__										=  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 				UpperCamelCase	(     self: Optional[int]    ,						UpperCamelCase: tf.Tensor    ,						UpperCamelCase: bool = False    ,						UpperCamelCase: bool = True       ):






         """simple docstring"""


         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(UpperCamelCase       )

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



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



    """simple docstring"""



    UpperCAmelCase						 =					RegNetConfig

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






         """simple docstring"""


         super().__init__(**UpperCamelCase       )
         A__										=  config
         A__										=  TFRegNetEmbeddings(UpperCamelCase    ,						name="""embedder"""       )
         A__										=  TFRegNetEncoder(UpperCamelCase    ,						name="""encoder"""       )
         A__										=  tf.keras.layers.GlobalAveragePoolingaD(keepdims=UpperCamelCase    ,						name="""pooler"""       )





    @unpack_inputs
    def 				UpperCamelCase	(     self: Tuple    ,						UpperCamelCase: tf.Tensor    ,						UpperCamelCase: Optional[bool] = None    ,						UpperCamelCase: Optional[bool] = None    ,						UpperCamelCase: bool = False    ,						):






         """simple docstring"""


         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(UpperCamelCase    ,						training=UpperCamelCase       )

         A__										=  self.encoder(
             UpperCamelCase    ,						output_hidden_states=UpperCamelCase    ,						return_dict=UpperCamelCase    ,						training=UpperCamelCase       )

         A__										=  encoder_outputs[0]
         A__										=  self.pooler(UpperCamelCase       )

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

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



    """simple docstring"""



    UpperCAmelCase						 =					RegNetConfig
    UpperCAmelCase						 =					"regnet"
    UpperCAmelCase						 =					"pixel_values"

    @property
    def 				UpperCamelCase	(     self: List[str]       ):






         """simple docstring"""


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

SCREAMING_SNAKE_CASE_					:	Dict							  =						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'

SCREAMING_SNAKE_CASE_					:	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            [`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.",	_lowerCamelCase,	)
class 					a						(       _lowerCamelCase				):



    """simple docstring"""



    def __init__(     self: Optional[int]    ,						UpperCamelCase: RegNetConfig    ,						*UpperCamelCase: Any    ,						**UpperCamelCase: Union[str, Any]       ):






         """simple docstring"""


         super().__init__(UpperCamelCase    ,						*UpperCamelCase    ,						**UpperCamelCase       )
         A__										=  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 				UpperCamelCase	(     self: Optional[int]    ,						UpperCamelCase: tf.Tensor    ,						UpperCamelCase: Optional[bool] = None    ,						UpperCamelCase: Optional[bool] = None    ,						UpperCamelCase: Union[str, Any]=False    ,						):






         """simple docstring"""


         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=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    ",	_lowerCamelCase,	)
class 					a						(       _lowerCamelCase,	_lowerCamelCase				):



    """simple docstring"""



    def __init__(     self: int    ,						UpperCamelCase: RegNetConfig    ,						*UpperCamelCase: Any    ,						**UpperCamelCase: Dict       ):






         """simple docstring"""


         super().__init__(UpperCamelCase    ,						*UpperCamelCase    ,						**UpperCamelCase       )
         A__										=  config.num_labels
         A__										=  TFRegNetMainLayer(UpperCamelCase    ,						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(UpperCamelCase       )
    @add_code_sample_docstrings(
        checkpoint=_IMAGE_CLASS_CHECKPOINT    ,						output_type=UpperCamelCase    ,						config_class=_CONFIG_FOR_DOC    ,						expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT    ,						)
    def 				UpperCamelCase	(     self: Any    ,						UpperCamelCase: tf.Tensor = None    ,						UpperCamelCase: tf.Tensor = None    ,						UpperCamelCase: bool = None    ,						UpperCamelCase: bool = None    ,						UpperCamelCase: Tuple=False    ,						):






         """simple docstring"""


         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(
             UpperCamelCase    ,						output_hidden_states=UpperCamelCase    ,						return_dict=UpperCamelCase    ,						training=UpperCamelCase       )

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

         A__										=  self.classifier[0](UpperCamelCase       )
         A__										=  self.classifier[1](UpperCamelCase       )

         A__										=  None if labels is None else self.hf_compute_loss(labels=UpperCamelCase    ,						logits=UpperCamelCase       )

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

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









"""simple docstring"""



import math
import sys





def  _snake_case     (    UpperCAmelCase_  :					int			):
     if number != int(UpperCAmelCase_			):
          raise ValueError("""the value of input must be a natural number"""			)
     if number < 0:
          raise ValueError("""the value of input must not be a negative number"""			)
     if number == 0:
          return 1
     A__										=  [-1] * (number + 1)
     A__										=  0
     for i in range(1					,   number + 1			):
          A__										=  sys.maxsize
          A__										=  int(math.sqrt(UpperCAmelCase_			)			)
          for j in range(1					,   root + 1			):
               A__										=  1 + answers[i - (j**2)]
               A__										=  min(UpperCAmelCase_					,   UpperCAmelCase_			)
          A__										=  answer
     return answers[number]


if __name__ == "__main__":
 import doctest

 doctest.testmod()






