

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


snake_case_			  = logging.get_logger(__name__)

# General docstring
snake_case_			  = '''RegNetConfig'''

# Base docstring
snake_case_			  = '''facebook/regnet-y-040'''
snake_case_			  = [1, 1_088, 7, 7]

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

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


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





    def __init__(      self						,   a						,   a = 3						,   a = 1						,   a = 1						,   a = "relu"						,   **a						,   ):
         super().__init__(**a)
         # 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__    :   Union[str, Any]      =       tf.keras.layers.ConvaD(
             filters=a						,   kernel_size=a						,   strides=a						,   padding='VALID'						,   groups=a						,   use_bias=a						,   name='convolution'						,   )
         lowercase__    :   Optional[Any]      =       tf.keras.layers.BatchNormalization(epsilon=1e-5						,   momentum=0.9						,   name='normalization')
         lowercase__    :   List[Any]      =       ACTaFN[activation] if activation is not None else tf.identity





    def       snake_case_				(      self						,   a):
         lowercase__    :   Optional[Any]      =       self.convolution(self.padding(a))
         lowercase__    :   Dict      =       self.normalization(a)
         lowercase__    :   Optional[Any]      =       self.activation(a)
         return hidden_state



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





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





    def       snake_case_				(      self						,   a):
         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__    :   int      =       self.embedder(a)
         return hidden_state



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





    def __init__(      self						,   a						,   a = 2						,   **a):
         super().__init__(**a)
         lowercase__    :   List[Any]      =       tf.keras.layers.ConvaD(
             filters=a						,   kernel_size=1						,   strides=a						,   use_bias=a						,   name='convolution')
         lowercase__    :   Optional[int]      =       tf.keras.layers.BatchNormalization(epsilon=1e-5						,   momentum=0.9						,   name='normalization')





    def       snake_case_				(      self						,   a						,   a = False):
         return self.normalization(self.convolution(a)						,   training=a)



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





    def __init__(      self						,   a						,   a						,   **a):
         super().__init__(**a)
         lowercase__    :   Tuple      =       tf.keras.layers.GlobalAveragePoolingaD(keepdims=a						,   name='pooler')
         lowercase__    :   List[str]      =       [
             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       snake_case_				(      self						,   a):
         # [batch_size, h, w, num_channels] -> [batch_size, 1, 1, num_channels]
         lowercase__    :   Union[str, Any]      =       self.pooler(a)
         for layer_module in self.attention:
              lowercase__    :   int      =       layer_module(a)
         lowercase__    :   Dict      =       hidden_state * pooled
         return hidden_state



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





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





    def       snake_case_				(      self						,   a):
         lowercase__    :   Any      =       hidden_state
         for layer_module in self.layers:
              lowercase__    :   Optional[Any]      =       layer_module(a)
         lowercase__    :   List[str]      =       self.shortcut(a)
         hidden_state += residual
         lowercase__    :   List[Any]      =       self.activation(a)
         return hidden_state



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





    def __init__(      self						,   a						,   a						,   a						,   a = 1						,   **a):
         super().__init__(**a)
         lowercase__    :   Dict      =       in_channels != out_channels or stride != 1
         lowercase__    :   str      =       max(1						,   out_channels // config.groups_width)
         lowercase__    :   List[Any]      =       (
             TFRegNetShortCut(a						,   stride=a						,   name='shortcut')
             if should_apply_shortcut
             else tf.keras.layers.Activation('linear'						,   name='shortcut')
         )
         lowercase__    :   Optional[Any]      =       [
             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__    :   Union[str, Any]      =       ACTaFN[config.hidden_act]





    def       snake_case_				(      self						,   a):
         lowercase__    :   Optional[Any]      =       hidden_state
         for layer_module in self.layers:
              lowercase__    :   Optional[int]      =       layer_module(a)
         lowercase__    :   Tuple      =       self.shortcut(a)
         hidden_state += residual
         lowercase__    :   Optional[int]      =       self.activation(a)
         return hidden_state



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





    def __init__(      self						,   a						,   a						,   a						,   a = 2						,   a = 2						,   **a):
         super().__init__(**a)

         lowercase__    :   Any      =       TFRegNetXLayer if config.layer_type == 'x' else TFRegNetYLayer
         lowercase__    :   Tuple      =       [
             # 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       snake_case_				(      self						,   a):
         for layer_module in self.layers:
              lowercase__    :   Union[str, Any]      =       layer_module(a)
         return hidden_state



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





    def __init__(      self						,   a						,   **a):
         super().__init__(**a)
         lowercase__    :   str      =       []
         # 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__    :   Union[str, Any]      =       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       snake_case_				(      self						,   a						,   a = False						,   a = True):
         lowercase__    :   Tuple      =       () if output_hidden_states else None

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

              lowercase__    :   str      =       stage_module(a)

         if output_hidden_states:
              lowercase__    :   Dict      =       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       SCREAMING_SNAKE_CASE__					(tf.keras.layers.Layer     ):
    __lowerCamelCase						:       List[str]														=     RegNetConfig





    def __init__(      self						,   a						,   **a):
         super().__init__(**a)
         lowercase__    :   str      =       config
         lowercase__    :   List[str]      =       TFRegNetEmbeddings(a						,   name='embedder')
         lowercase__    :   List[str]      =       TFRegNetEncoder(a						,   name='encoder')
         lowercase__    :   List[str]      =       tf.keras.layers.GlobalAveragePoolingaD(keepdims=a						,   name='pooler')





    @unpack_inputs
    def       snake_case_				(      self						,   a						,   a = None						,   a = None						,   a = False						,   ):
         lowercase__    :   Tuple      =       (
             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__    :   str      =       self.embedder(a						,   training=a)

         lowercase__    :   Optional[Any]      =       self.encoder(
             a						,   output_hidden_states=a						,   return_dict=a						,   training=a)

         lowercase__    :   Optional[Any]      =       encoder_outputs[0]
         lowercase__    :   str      =       self.pooler(a)

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

         # Change the other hidden state outputs to NCHW as well
         if output_hidden_states:
              lowercase__    :   List[str]      =       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       SCREAMING_SNAKE_CASE__					(__snake_case     ):
    __lowerCamelCase						:       Dict														=     RegNetConfig
    __lowerCamelCase						:       str														=     """regnet"""
    __lowerCamelCase						:       Any														=     """pixel_values"""





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



snake_case_			  = 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.
'''

snake_case_			  = 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."""		,       __snake_case		,       )
class       SCREAMING_SNAKE_CASE__					(__snake_case     ):





    def __init__(      self						,   a						,   *a						,   **a):
         super().__init__(a						,   *a						,   **a)
         lowercase__    :   Any      =       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       snake_case_				(      self						,   a						,   a = None						,   a = None						,   a=False						,   ):
         lowercase__    :   str      =       (
             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__    :   List[Any]      =       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(
    """
    RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    """		,       __snake_case		,       )
class       SCREAMING_SNAKE_CASE__					(__snake_case		,       __snake_case     ):





    def __init__(      self						,   a						,   *a						,   **a):
         super().__init__(a						,   *a						,   **a)
         lowercase__    :   List[str]      =       config.num_labels
         lowercase__    :   Union[str, Any]      =       TFRegNetMainLayer(a						,   name='regnet')
         # classification head
         lowercase__    :   Optional[Any]      =       [
             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       snake_case_				(      self						,   a = None						,   a = None						,   a = None						,   a = None						,   a=False						,   ):
         lowercase__    :   Union[str, Any]      =       (
             output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
         )
         lowercase__    :   Optional[int]      =       return_dict if return_dict is not None else self.config.use_return_dict

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

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

         lowercase__    :   Dict      =       self.classifier[0](a)
         lowercase__    :   List[Any]      =       self.classifier[1](a)

         lowercase__    :   str      =       None if labels is None else self.hf_compute_loss(labels=a						,   logits=a)

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

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




from __future__ import annotations

from collections.abc import Callable
from typing import Generic, TypeVar

snake_case_			  = TypeVar('''T''')
snake_case_			  = TypeVar('''U''')


class       SCREAMING_SNAKE_CASE__					(Generic[T, U]     ):





    def __init__(      self						,   a						,   a):
         lowercase__    :   List[Any]      =       key
         lowercase__    :   List[Any]      =       val
         lowercase__    :   DoubleLinkedListNode[T, U] | None      =       None
         lowercase__    :   DoubleLinkedListNode[T, U] | None      =       None





    def __repr__(      self):
         return (
             f"""Node: key: {self.key}, val: {self.val}, """
             f"""has next: {bool(self.next)}, has prev: {bool(self.prev)}"""
         )



class       SCREAMING_SNAKE_CASE__					(Generic[T, U]     ):





    def __init__(      self):
         lowercase__    :   DoubleLinkedListNode[T, U]      =       DoubleLinkedListNode(a						,   a)
         lowercase__    :   DoubleLinkedListNode[T, U]      =       DoubleLinkedListNode(a						,   a)
         lowercase__      , lowercase__    :   Union[str, Any]      =       self.rear, self.head





    def __repr__(      self):
         lowercase__    :   Any      =       ['DoubleLinkedList']
         lowercase__    :   List[str]      =       self.head
         while node.next is not None:
              rep.append(str(a))
              lowercase__    :   Tuple      =       node.next
         rep.append(str(self.rear))
         return ",\n    ".join(a)





    def       snake_case_				(      self						,   a):
         lowercase__    :   Optional[Any]      =       self.rear.prev

         # All nodes other than self.head are guaranteed to have non-None previous
         assert previous is not None

         lowercase__    :   Dict      =       node
         lowercase__    :   int      =       previous
         lowercase__    :   Union[str, Any]      =       node
         lowercase__    :   Optional[int]      =       self.rear





    def       snake_case_				(      self						,   a):

         if node.prev is None or node.next is None:
              return None

         lowercase__    :   Union[str, Any]      =       node.next
         lowercase__    :   Tuple      =       node.prev
         lowercase__    :   Union[str, Any]      =       None
         lowercase__    :   List[Any]      =       None
         return node



class       SCREAMING_SNAKE_CASE__					(Generic[T, U]     ):
    __lowerCamelCase						:       dict[Callable[[T], U], LRUCache[T, U]]														=     {}





    def __init__(      self						,   a):
         lowercase__    :   DoubleLinkedList[T, U]      =       DoubleLinkedList()
         lowercase__    :   Optional[Any]      =       capacity
         lowercase__    :   Union[str, Any]      =       0
         lowercase__    :   Tuple      =       0
         lowercase__    :   int      =       0
         lowercase__    :   dict[T, DoubleLinkedListNode[T, U]]      =       {}





    def __repr__(      self):
         return (
             f"""CacheInfo(hits={self.hits}, misses={self.miss}, """
             f"""capacity={self.capacity}, current size={self.num_keys})"""
         )





    def __contains__(      self						,   a):
         return key in self.cache





    def       snake_case_				(      self						,   a):
         # Note: pythonic interface would throw KeyError rather than return None

         if key in self.cache:
              self.hits += 1
              lowercase__    :   DoubleLinkedListNode[T, U]      =       self.cache[key]
              lowercase__    :   str      =       self.list.remove(self.cache[key])
              assert node == value_node

              # node is guaranteed not None because it is in self.cache
              assert node is not None
              self.list.add(a)
              return node.val
         self.miss += 1
         return None





    def       snake_case_				(      self						,   a						,   a):

         if key not in self.cache:
              if self.num_keys >= self.capacity:
                   # delete first node (oldest) when over capacity
                   lowercase__    :   Optional[int]      =       self.list.head.next

                   # guaranteed to have a non-None first node when num_keys > 0
                   # explain to type checker via assertions
                   assert first_node is not None
                   assert first_node.key is not None
                   assert (
                       self.list.remove(a) is not None
                   )  # node guaranteed to be in list assert node.key is not None

                   del self.cache[first_node.key]
                   self.num_keys -= 1
              lowercase__    :   Optional[Any]      =       DoubleLinkedListNode(a						,   a)
              self.list.add(self.cache[key])
              self.num_keys += 1

         else:
              # bump node to the end of the list, update value
              lowercase__    :   Any      =       self.list.remove(self.cache[key])
              assert node is not None  # node guaranteed to be in list
              lowercase__    :   Union[str, Any]      =       value
              self.list.add(a)





    @classmethod
    def       snake_case_				(      cls						,   a = 128):

         def cache_decorator_inner(a) -> Callable[..., U]:
              def cache_decorator_wrapper(*a) -> U:
                   if func not in cls.decorator_function_to_instance_map:
                        lowercase__    :   Dict      =       LRUCache(a)

                   lowercase__    :   str      =       cls.decorator_function_to_instance_map[func].get(args[0])
                   if result is None:
                        lowercase__    :   str      =       func(*a)
                        cls.decorator_function_to_instance_map[func].put(args[0]						,   a)
                   return result

              def cache_info() -> LRUCache[T, U]:
                   return cls.decorator_function_to_instance_map[func]

              setattr(a						,   'cache_info'						,   a)  # noqa: B010

              return cache_decorator_wrapper

         return cache_decorator_inner


if __name__ == "__main__":
       import doctest

       doctest.testmod()

