'''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: List[str]          =   logging.get_logger(__name__)

# General docstring
__a: str          =   """RegNetConfig"""

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

# Image classification docstring
__a: List[Any]          =   """facebook/regnet-y-040"""
__a: Any          =   """tabby, tabby cat"""

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


       def _lowerCAmelCase(						self	,   __lowerCAmelCase )  ->  Union[str, Any]:
        lowercase__				:    Optional[int]  =					self.convolution(self.padding(__lowerCAmelCase ) )
        lowercase__				:    Optional[Any]  =					self.normalization(__lowerCAmelCase )
        lowercase__				:    str  =					self.activation(__lowerCAmelCase )
        return hidden_state



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





       '''simple docstring'''



       def __init__(						self	,   __lowerCAmelCase	,   **__lowerCAmelCase )  ->  Any:
        super().__init__(**__lowerCAmelCase )
        lowercase__				:    str  =					config.num_channels
        lowercase__				:    List[Any]  =					TFRegNetConvLayer(
            out_channels=config.embedding_size	,   kernel_size=3	,   stride=2	,   activation=config.hidden_act	,   name='''embedder'''	,   )


       def _lowerCAmelCase(						self	,   __lowerCAmelCase )  ->  Any:
        lowercase__				:    Dict  =					shape_list(__lowerCAmelCase )[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__				:    List[Any]  =					tf.transpose(__lowerCAmelCase	,   perm=(0, 2, 3, 1) )
        lowercase__				:    Union[str, Any]  =					self.embedder(__lowerCAmelCase )
        return hidden_state



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





       '''simple docstring'''



       def __init__(						self	,   __lowerCAmelCase	,   __lowerCAmelCase = 2	,   **__lowerCAmelCase )  ->  Tuple:
        super().__init__(**__lowerCAmelCase )
        lowercase__				:    List[str]  =					tf.keras.layers.ConvaD(
            filters=__lowerCAmelCase	,   kernel_size=1	,   strides=__lowerCAmelCase	,   use_bias=__lowerCAmelCase	,   name='''convolution''' )
        lowercase__				:    Union[str, Any]  =					tf.keras.layers.BatchNormalization(epsilon=1E-5	,   momentum=0.9	,   name='''normalization''' )


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



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





       '''simple docstring'''



       def __init__(						self	,   __lowerCAmelCase	,   __lowerCAmelCase	,   **__lowerCAmelCase )  ->  Union[str, Any]:
        super().__init__(**__lowerCAmelCase )
        lowercase__				:    Any  =					tf.keras.layers.GlobalAveragePoolingaD(keepdims=__lowerCAmelCase	,   name='''pooler''' )
        lowercase__				:    List[str]  =					[
            tf.keras.layers.ConvaD(filters=__lowerCAmelCase	,   kernel_size=1	,   activation='''relu'''	,   name='''attention.0''' ),
            tf.keras.layers.ConvaD(filters=__lowerCAmelCase	,   kernel_size=1	,   activation='''sigmoid'''	,   name='''attention.2''' ),
        ]


       def _lowerCAmelCase(						self	,   __lowerCAmelCase )  ->  Dict:
        # [batch_size, h, w, num_channels] -> [batch_size, 1, 1, num_channels]
        lowercase__				:    List[str]  =					self.pooler(__lowerCAmelCase )
        for layer_module in self.attention:
         lowercase__				:    Union[str, Any]  =					layer_module(__lowerCAmelCase )
        lowercase__				:    Dict  =					hidden_state * pooled
        return hidden_state



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





       '''simple docstring'''



       def __init__(						self	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase = 1	,   **__lowerCAmelCase )  ->  List[Any]:
        super().__init__(**__lowerCAmelCase )
        lowercase__				:    List[str]  =					in_channels != out_channels or stride != 1
        lowercase__				:    Optional[Any]  =					max(1	,   out_channels // config.groups_width )
        lowercase__				:    Tuple  =					(
            TFRegNetShortCut(__lowerCAmelCase	,   stride=__lowerCAmelCase	,   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__				:    List[str]  =					[
            TFRegNetConvLayer(__lowerCAmelCase	,   kernel_size=1	,   activation=config.hidden_act	,   name='''layer.0''' ),
            TFRegNetConvLayer(
                __lowerCAmelCase	,   stride=__lowerCAmelCase	,   groups=__lowerCAmelCase	,   activation=config.hidden_act	,   name='''layer.1''' ),
            TFRegNetConvLayer(__lowerCAmelCase	,   kernel_size=1	,   activation=__lowerCAmelCase	,   name='''layer.2''' ),
        ]
        lowercase__				:    List[Any]  =					ACTaFN[config.hidden_act]


       def _lowerCAmelCase(						self	,   __lowerCAmelCase )  ->  List[Any]:
        lowercase__				:    Optional[int]  =					hidden_state
        for layer_module in self.layers:
         lowercase__				:    Union[str, Any]  =					layer_module(__lowerCAmelCase )
        lowercase__				:    Optional[Any]  =					self.shortcut(__lowerCAmelCase )
        hidden_state += residual
        lowercase__				:    Any  =					self.activation(__lowerCAmelCase )
        return hidden_state



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





       '''simple docstring'''



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


       def _lowerCAmelCase(						self	,   __lowerCAmelCase )  ->  Dict:
        lowercase__				:    List[Any]  =					hidden_state
        for layer_module in self.layers:
         lowercase__				:    Optional[int]  =					layer_module(__lowerCAmelCase )
        lowercase__				:    List[Any]  =					self.shortcut(__lowerCAmelCase )
        hidden_state += residual
        lowercase__				:    Dict  =					self.activation(__lowerCAmelCase )
        return hidden_state



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





       '''simple docstring'''



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

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


       def _lowerCAmelCase(						self	,   __lowerCAmelCase )  ->  str:
        for layer_module in self.layers:
         lowercase__				:    str  =					layer_module(__lowerCAmelCase )
        return hidden_state



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





       '''simple docstring'''



       def __init__(						self	,   __lowerCAmelCase	,   **__lowerCAmelCase )  ->  Any:
        super().__init__(**__lowerCAmelCase )
        lowercase__				:    int  =					[]
        # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input
        self.stages.append(
            TFRegNetStage(
                __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'''	,   ) )
        lowercase__				:    List[str]  =					zip(config.hidden_sizes	,   config.hidden_sizes[1:] )
        for i, ((in_channels, out_channels), depth) in enumerate(zip(__lowerCAmelCase	,   config.depths[1:] ) ):
         self.stages.append(TFRegNetStage(__lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   depth=__lowerCAmelCase	,   name=F"""stages.{i+1}""" ) )


       def _lowerCAmelCase(						self	,   __lowerCAmelCase	,   __lowerCAmelCase = False	,   __lowerCAmelCase = True )  ->  TFBaseModelOutputWithNoAttention:
        lowercase__				:    Dict  =					() if output_hidden_states else None

        for stage_module in self.stages:
         if output_hidden_states:
          lowercase__				:    Union[str, Any]  =					hidden_states + (hidden_state,)

         lowercase__				:    Any  =					stage_module(__lowerCAmelCase )

        if output_hidden_states:
         lowercase__				:    List[str]  =					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 )



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





       '''simple docstring'''



       SCREAMING_SNAKE_CASE							=    RegNetConfig

       def __init__(						self	,   __lowerCAmelCase	,   **__lowerCAmelCase )  ->  List[Any]:
        super().__init__(**__lowerCAmelCase )
        lowercase__				:    Any  =					config
        lowercase__				:    Dict  =					TFRegNetEmbeddings(__lowerCAmelCase	,   name='''embedder''' )
        lowercase__				:    Tuple  =					TFRegNetEncoder(__lowerCAmelCase	,   name='''encoder''' )
        lowercase__				:    Dict  =					tf.keras.layers.GlobalAveragePoolingaD(keepdims=__lowerCAmelCase	,   name='''pooler''' )


       @unpack_inputs
       def _lowerCAmelCase(						self	,   __lowerCAmelCase	,   __lowerCAmelCase = None	,   __lowerCAmelCase = None	,   __lowerCAmelCase = False	,   )  ->  TFBaseModelOutputWithPoolingAndNoAttention:
        lowercase__				:    Union[str, Any]  =					(
            output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        )
        lowercase__				:    int  =					return_dict if return_dict is not None else self.config.use_return_dict

        lowercase__				:    Dict  =					self.embedder(__lowerCAmelCase	,   training=__lowerCAmelCase )

        lowercase__				:    Tuple  =					self.encoder(
            __lowerCAmelCase	,   output_hidden_states=__lowerCAmelCase	,   return_dict=__lowerCAmelCase	,   training=__lowerCAmelCase )

        lowercase__				:    int  =					encoder_outputs[0]
        lowercase__				:    Dict  =					self.pooler(__lowerCAmelCase )

        # Change to NCHW output format have uniformity in the modules
        lowercase__				:    List[str]  =					tf.transpose(__lowerCAmelCase	,   perm=(0, 3, 1, 2) )
        lowercase__				:    Dict  =					tf.transpose(__lowerCAmelCase	,   perm=(0, 3, 1, 2) )

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



class 			UpperCAmelCase  (						a__					):





       '''simple docstring'''



       SCREAMING_SNAKE_CASE							=    RegNetConfig
       SCREAMING_SNAKE_CASE							=    "regnet"
       SCREAMING_SNAKE_CASE							=    "pixel_values"

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


__a: Any          =   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: Dict          =   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."    ,						a__    ,						)
class 			UpperCAmelCase  (						a__					):





       '''simple docstring'''



       def __init__(						self	,   __lowerCAmelCase	,   *__lowerCAmelCase	,   **__lowerCAmelCase )  ->  str:
        super().__init__(__lowerCAmelCase	,   *__lowerCAmelCase	,   **__lowerCAmelCase )
        lowercase__				:    Optional[Any]  =					TFRegNetMainLayer(__lowerCAmelCase	,   name='''regnet''' )


       @unpack_inputs
       @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	,   )
       def _lowerCAmelCase(						self	,   __lowerCAmelCase	,   __lowerCAmelCase = None	,   __lowerCAmelCase = None	,   __lowerCAmelCase=False	,   )  ->  Union[TFBaseModelOutputWithPoolingAndNoAttention, Tuple[tf.Tensor]]:
        lowercase__				:    Optional[int]  =					(
            output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        )
        lowercase__				:    str  =					return_dict if return_dict is not None else self.config.use_return_dict

        lowercase__				:    str  =					self.regnet(
            pixel_values=__lowerCAmelCase	,   output_hidden_states=__lowerCAmelCase	,   return_dict=__lowerCAmelCase	,   training=__lowerCAmelCase	,   )
        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 			UpperCAmelCase  (						a__    ,						a__					):





       '''simple docstring'''



       def __init__(						self	,   __lowerCAmelCase	,   *__lowerCAmelCase	,   **__lowerCAmelCase )  ->  List[str]:
        super().__init__(__lowerCAmelCase	,   *__lowerCAmelCase	,   **__lowerCAmelCase )
        lowercase__				:    Tuple  =					config.num_labels
        lowercase__				:    Union[str, Any]  =					TFRegNetMainLayer(__lowerCAmelCase	,   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(__lowerCAmelCase )
       @add_code_sample_docstrings(
           checkpoint=_IMAGE_CLASS_CHECKPOINT	,   output_type=__lowerCAmelCase	,   config_class=_CONFIG_FOR_DOC	,   expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT	,   )
       def _lowerCAmelCase(						self	,   __lowerCAmelCase = None	,   __lowerCAmelCase = None	,   __lowerCAmelCase = None	,   __lowerCAmelCase = None	,   __lowerCAmelCase=False	,   )  ->  Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]:
        lowercase__				:    Optional[int]  =					(
            output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        )
        lowercase__				:    Dict  =					return_dict if return_dict is not None else self.config.use_return_dict

        lowercase__				:    Optional[Any]  =					self.regnet(
            __lowerCAmelCase	,   output_hidden_states=__lowerCAmelCase	,   return_dict=__lowerCAmelCase	,   training=__lowerCAmelCase )

        lowercase__				:    Optional[int]  =					outputs.pooler_output if return_dict else outputs[1]

        lowercase__				:    Tuple  =					self.classifier[0](__lowerCAmelCase )
        lowercase__				:    str  =					self.classifier[1](__lowerCAmelCase )

        lowercase__				:    Optional[int]  =					None if labels is None else self.hf_compute_loss(labels=__lowerCAmelCase	,   logits=__lowerCAmelCase )

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

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


'''simple docstring'''

import unittest

from knapsack import knapsack as k



class 			UpperCAmelCase  (						unittest.TestCase					):





       '''simple docstring'''



       def _lowerCAmelCase(						self )  ->  Tuple:
        lowercase__				:    Optional[Any]  =					0
        lowercase__				:    Any  =					[0]
        lowercase__				:    List[Any]  =					[0]
        lowercase__				:    Optional[Any]  =					len(__lowerCAmelCase )
        self.assertEqual(k.knapsack(__lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase )	,   0 )

        lowercase__				:    List[str]  =					[60]
        lowercase__				:    List[str]  =					[10]
        lowercase__				:    List[str]  =					len(__lowerCAmelCase )
        self.assertEqual(k.knapsack(__lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase )	,   0 )

       def _lowerCAmelCase(						self )  ->  Optional[Any]:
        lowercase__				:    Optional[Any]  =					3
        lowercase__				:    List[Any]  =					[1, 2, 3]
        lowercase__				:    Union[str, Any]  =					[3, 2, 1]
        lowercase__				:    List[Any]  =					len(__lowerCAmelCase )
        self.assertEqual(k.knapsack(__lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase )	,   5 )


       def _lowerCAmelCase(						self )  ->  Optional[Any]:
        lowercase__				:    int  =					50
        lowercase__				:    Any  =					[60, 100, 120]
        lowercase__				:    int  =					[10, 20, 30]
        lowercase__				:    Optional[Any]  =					len(__lowerCAmelCase )
        self.assertEqual(k.knapsack(__lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase )	,   220 )


if __name__ == "__main__":
  unittest.main()

