



from typing import Optional, Tuple, Union

import tensorflow as tf

from ...activations_tf import ACTaFN
from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward
from ...modeling_tf_outputs import (
    TFBaseModelOutputWithNoAttention,
    TFBaseModelOutputWithPoolingAndNoAttention,
    TFSequenceClassifierOutput,
)
from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs
from ...tf_utils import shape_list
from ...utils import logging
from .configuration_regnet import RegNetConfig


lowercase							       =			logging.get_logger(__name__)

# General docstring
lowercase							       =			'''RegNetConfig'''

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

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

lowercase							       =			[
    '''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_							,  ) ->      int:



      """simple docstring"""







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



      """simple docstring"""







      a_       		=self.convolution(self.padding(lowerCAmelCase_))
      a_       		=self.normalization(lowerCAmelCase_)
      a_       		=self.activation(lowerCAmelCase_)
      return hidden_state







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


 '''simple docstring'''




 def __init__(   self							,  lowerCAmelCase_							,  **lowerCAmelCase_) ->      Any:



      """simple docstring"""







      super().__init__(**lowerCAmelCase_)
      a_       		=config.num_channels
      a_       		=TFRegNetConvLayer(
          out_channels=config.embedding_size							,  kernel_size=3							,  stride=2							,  activation=config.hidden_act							,  name="embedder"							,  )




 def       lowercase_  (   self							,  lowerCAmelCase_) ->      List[str]:



      """simple docstring"""







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







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


 '''simple docstring'''




 def __init__(   self							,  lowerCAmelCase_							,  lowerCAmelCase_ = 2							,  **lowerCAmelCase_) ->      int:



      """simple docstring"""







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




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_ = False) ->      Any:



      """simple docstring"""







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







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


 '''simple docstring'''




 def __init__(   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  **lowerCAmelCase_) ->      Dict:



      """simple docstring"""







      super().__init__(**lowerCAmelCase_)
      a_       		=tf.keras.layers.GlobalAveragePoolingaD(keepdims=lowerCAmelCase_							,  name="pooler")
      a_       		=[
          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       lowercase_  (   self							,  lowerCAmelCase_) ->      str:



      """simple docstring"""







      a_       		=self.pooler(lowerCAmelCase_)
      for layer_module in self.attention:
           a_       		=layer_module(lowerCAmelCase_)
      a_       		=hidden_state * pooled
      return hidden_state







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


 '''simple docstring'''




 def __init__(   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_ = 1							,  **lowerCAmelCase_) ->      List[str]:



      """simple docstring"""







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




 def       lowercase_  (   self							,  lowerCAmelCase_) ->      int:



      """simple docstring"""







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







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


 '''simple docstring'''




 def __init__(   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_ = 1							,  **lowerCAmelCase_) ->      Optional[int]:



      """simple docstring"""







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




 def       lowercase_  (   self							,  lowerCAmelCase_) ->      Optional[int]:



      """simple docstring"""







      a_       		=hidden_state
      for layer_module in self.layers:
           a_       		=layer_module(lowerCAmelCase_)
      a_       		=self.shortcut(lowerCAmelCase_)
      hidden_state += residual
      a_       		=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_) ->      str:



      """simple docstring"""







      super().__init__(**lowerCAmelCase_)

      a_       		=TFRegNetXLayer if config.layer_type == "x" else TFRegNetYLayer
      a_       		=[
          # 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       lowercase_  (   self							,  lowerCAmelCase_) ->      Tuple:



      """simple docstring"""







      for layer_module in self.layers:
           a_       		=layer_module(lowerCAmelCase_)
      return hidden_state







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


 '''simple docstring'''




 def __init__(   self							,  lowerCAmelCase_							,  **lowerCAmelCase_) ->      Any:



      """simple docstring"""







      super().__init__(**lowerCAmelCase_)
      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(
              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"							,  ))
      a_       		=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       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_ = False							,  lowerCAmelCase_ = True) ->      List[Any]:



      """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(lowerCAmelCase_)

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







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


 '''simple docstring'''




 __magic_name__					: Tuple  =      RegNetConfig




 def __init__(   self							,  lowerCAmelCase_							,  **lowerCAmelCase_) ->      int:



      """simple docstring"""







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




 @unpack_inputs
 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_ = None							,  lowerCAmelCase_ = None							,  lowerCAmelCase_ = False							,  ) ->      Optional[Any]:



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

      a_       		=self.encoder(
          lowerCAmelCase_							,  output_hidden_states=lowerCAmelCase_							,  return_dict=lowerCAmelCase_							,  training=lowerCAmelCase_)

      a_       		=encoder_outputs[0]
      a_       		=self.pooler(lowerCAmelCase_)

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

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


 '''simple docstring'''




 __magic_name__					: Tuple  =      RegNetConfig
 __magic_name__					: Union[str, Any]  =      'regnet'
 __magic_name__					: Optional[int]  =      'pixel_values'




 @property
 def       lowercase_  (   self) ->      List[str]:



      """simple docstring"""







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



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

lowercase							       =			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."					,	lowercase__					,	)
class 	UpperCAmelCase	(		lowercase__):


 '''simple docstring'''




 def __init__(   self							,  lowerCAmelCase_							,  *lowerCAmelCase_							,  **lowerCAmelCase_) ->      Optional[Any]:



      """simple docstring"""







      super().__init__(lowerCAmelCase_							,  *lowerCAmelCase_							,  **lowerCAmelCase_)
      a_       		=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       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_ = None							,  lowerCAmelCase_ = None							,  lowerCAmelCase_=False							,  ) ->      List[str]:



      """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=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    "					,	lowercase__					,	)
class 	UpperCAmelCase	(		lowercase__					,	lowercase__):


 '''simple docstring'''




 def __init__(   self							,  lowerCAmelCase_							,  *lowerCAmelCase_							,  **lowerCAmelCase_) ->      int:



      """simple docstring"""







      super().__init__(lowerCAmelCase_							,  *lowerCAmelCase_							,  **lowerCAmelCase_)
      a_       		=config.num_labels
      a_       		=TFRegNetMainLayer(lowerCAmelCase_							,  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(lowerCAmelCase_)
 @add_code_sample_docstrings(
     checkpoint=_IMAGE_CLASS_CHECKPOINT							,  output_type=lowerCAmelCase_							,  config_class=_CONFIG_FOR_DOC							,  expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT							,  )
 def       lowercase_  (   self							,  lowerCAmelCase_ = None							,  lowerCAmelCase_ = None							,  lowerCAmelCase_ = None							,  lowerCAmelCase_ = None							,  lowerCAmelCase_=False							,  ) ->      List[Any]:



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

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

      a_       		=self.classifier[0](lowerCAmelCase_)
      a_       		=self.classifier[1](lowerCAmelCase_)

      a_       		=None if labels is None else self.hf_compute_loss(labels=lowerCAmelCase_							,  logits=lowerCAmelCase_)

      if not return_dict:
           a_       		=(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'''


from __future__ import annotations






def  UpperCAmelCase_       (		lowercase__       ,	lowercase__       ):


     '''simple docstring'''







     print(F"""Vertex\tShortest Distance from vertex {src}"""       )
     for i, d in enumerate(lowercase__       ):
          print(F"""{i}\t\t{d}"""       )






def  UpperCAmelCase_       (		lowercase__       ,	lowercase__       ,	lowercase__       ):


     '''simple docstring'''







     for j in range(lowercase__       ):
          a_		,						a_		,						a_       		=(graph[j][k] for k in ["src", "dst", "weight"])
          if distance[u] != float("inf"       ) and distance[u] + w < distance[v]:
               return True
     return False






def  UpperCAmelCase_       (		lowercase__       ,	lowercase__       ,	lowercase__       ,	lowercase__       ):


     '''simple docstring'''







     a_       		=[float("inf"       )] * vertex_count
     a_       		=0.0

     for _ in range(vertex_count - 1       ):
          for j in range(lowercase__       ):
               a_		,						a_		,						a_       		=(graph[j][k] for k in ["src", "dst", "weight"])

               if distance[u] != float("inf"       ) and distance[u] + w < distance[v]:
                    a_       		=distance[u] + w

     a_       		=check_negative_cycle(lowercase__       ,	lowercase__       ,	lowercase__       )
     if negative_cycle_exists:
          raise Exception("Negative cycle found"       )

     return distance


if __name__ == "__main__":
  import doctest

  doctest.testmod()

  lowercase							       =			int(input('''Enter number of vertices: ''').strip())
  lowercase							       =			int(input('''Enter number of edges: ''').strip())

  lowercase							       =			[{} for _ in range(E)]

  for i in range(E):
    print('''Edge ''', i + 1)
    lowercase       ,      lowercase       ,      lowercase							       =			(
        int(x)
        for x in input('''Enter source, destination, weight: ''').strip().split(''' ''')
    )
    lowercase							       =			{'''src''': src, '''dst''': dest, '''weight''': weight}

  lowercase							       =			int(input('''\nEnter shortest path source:''').strip())
  lowercase							       =			bellman_ford(graph, V, E, source)
  print_distance(shortest_distance, 0)


