


'''simple docstring'''
from typing import Optional

import torch
import torch.utils.checkpoint
from torch import Tensor, nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward
from ...modeling_outputs import (
    BaseModelOutputWithNoAttention,
    BaseModelOutputWithPoolingAndNoAttention,
    ImageClassifierOutputWithNoAttention,
)
from ...modeling_utils import PreTrainedModel
from ...utils import logging
from .configuration_regnet import RegNetConfig


UpperCAmelCase_ :     Optional[int]								=      logging.get_logger(__name__)

# General docstring
UpperCAmelCase_ :     str								=      'RegNetConfig'

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

# Image classification docstring
UpperCAmelCase_ :     Any								=      'facebook/regnet-y-040'
UpperCAmelCase_ :     int								=      'tabby, tabby cat'

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

class 						_lowerCamelCase					(    nn.Module				):




 '''simple docstring'''


 def __init__(      self      ,      __lowercase      ,      __lowercase      ,      __lowercase = 3      ,      __lowercase = 1      ,      __lowercase = 1      ,      __lowercase = "relu"      ,      ):

  """simple docstring"""







  super().__init__()
  __A  :		Any       =			nn.Convad(
      __lowercase      ,      __lowercase      ,      kernel_size=__lowercase      ,      stride=__lowercase      ,      padding=kernel_size // 2      ,      groups=__lowercase      ,      bias=__lowercase      ,      )
  __A  :		List[str]       =			nn.BatchNormad(__lowercase						)
  __A  :		List[Any]       =			ACTaFN[activation] if activation is not None else nn.Identity()







 def  snake_case__   (      self      ,      __lowercase						):

  """simple docstring"""







  __A  :		Dict       =			self.convolution(__lowercase						)
  __A  :		List[Any]       =			self.normalization(__lowercase						)
  __A  :		List[Any]       =			self.activation(__lowercase						)
  return hidden_state


class 						_lowerCamelCase					(    nn.Module				):




 '''simple docstring'''


 def __init__(      self      ,      __lowercase						):

  """simple docstring"""







  super().__init__()
  __A  :		int       =			RegNetConvLayer(
      config.num_channels      ,      config.embedding_size      ,      kernel_size=3      ,      stride=2      ,      activation=config.hidden_act						)
  __A  :		Tuple       =			config.num_channels







 def  snake_case__   (      self      ,      __lowercase						):

  """simple docstring"""







  __A  :		List[str]       =			pixel_values.shape[1]
  if 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.'						)
  __A  :		List[Any]       =			self.embedder(__lowercase						)
  return hidden_state


class 						_lowerCamelCase					(    nn.Module				):




 '''simple docstring'''


 def __init__(      self      ,      __lowercase      ,      __lowercase      ,      __lowercase = 2						):

  """simple docstring"""







  super().__init__()
  __A  :		Any       =			nn.Convad(__lowercase      ,      __lowercase      ,      kernel_size=1      ,      stride=__lowercase      ,      bias=__lowercase						)
  __A  :		str       =			nn.BatchNormad(__lowercase						)







 def  snake_case__   (      self      ,      __lowercase						):

  """simple docstring"""







  __A  :		Tuple       =			self.convolution(__lowercase						)
  __A  :		Tuple       =			self.normalization(__lowercase						)
  return hidden_state


class 						_lowerCamelCase					(    nn.Module				):




 '''simple docstring'''


 def __init__(      self      ,      __lowercase      ,      __lowercase						):

  """simple docstring"""







  super().__init__()

  __A  :		Dict       =			nn.AdaptiveAvgPoolad((1, 1)						)
  __A  :		List[Any]       =			nn.Sequential(
      nn.Convad(__lowercase      ,      __lowercase      ,      kernel_size=1						)      ,      nn.ReLU()      ,      nn.Convad(__lowercase      ,      __lowercase      ,      kernel_size=1						)      ,      nn.Sigmoid()      ,      )







 def  snake_case__   (      self      ,      __lowercase						):

  """simple docstring"""







  __A  :		Union[str, Any]       =			self.pooler(__lowercase						)
  __A  :		Optional[int]       =			self.attention(__lowercase						)
  __A  :		List[str]       =			hidden_state * attention
  return hidden_state


class 						_lowerCamelCase					(    nn.Module				):




 '''simple docstring'''


 def __init__(      self      ,      __lowercase      ,      __lowercase      ,      __lowercase      ,      __lowercase = 1						):

  """simple docstring"""







  super().__init__()
  __A  :		List[Any]       =			in_channels != out_channels or stride != 1
  __A  :		Optional[int]       =			max(1      ,      out_channels // config.groups_width						)
  __A  :		Optional[Any]       =			(
      RegNetShortCut(__lowercase      ,      __lowercase      ,      stride=__lowercase						) if should_apply_shortcut else nn.Identity()
  )
  __A  :		Union[str, Any]       =			nn.Sequential(
      RegNetConvLayer(__lowercase      ,      __lowercase      ,      kernel_size=1      ,      activation=config.hidden_act						)      ,      RegNetConvLayer(__lowercase      ,      __lowercase      ,      stride=__lowercase      ,      groups=__lowercase      ,      activation=config.hidden_act						)      ,      RegNetConvLayer(__lowercase      ,      __lowercase      ,      kernel_size=1      ,      activation=__lowercase						)      ,      )
  __A  :		List[Any]       =			ACTaFN[config.hidden_act]







 def  snake_case__   (      self      ,      __lowercase						):

  """simple docstring"""







  __A  :		Union[str, Any]       =			hidden_state
  __A  :		Tuple       =			self.layer(__lowercase						)
  __A  :		int       =			self.shortcut(__lowercase						)
  hidden_state += residual
  __A  :		List[Any]       =			self.activation(__lowercase						)
  return hidden_state


class 						_lowerCamelCase					(    nn.Module				):




 '''simple docstring'''


 def __init__(      self      ,      __lowercase      ,      __lowercase      ,      __lowercase      ,      __lowercase = 1						):

  """simple docstring"""







  super().__init__()
  __A  :		Tuple       =			in_channels != out_channels or stride != 1
  __A  :		List[Any]       =			max(1      ,      out_channels // config.groups_width						)
  __A  :		int       =			(
      RegNetShortCut(__lowercase      ,      __lowercase      ,      stride=__lowercase						) if should_apply_shortcut else nn.Identity()
  )
  __A  :		List[str]       =			nn.Sequential(
      RegNetConvLayer(__lowercase      ,      __lowercase      ,      kernel_size=1      ,      activation=config.hidden_act						)      ,      RegNetConvLayer(__lowercase      ,      __lowercase      ,      stride=__lowercase      ,      groups=__lowercase      ,      activation=config.hidden_act						)      ,      RegNetSELayer(__lowercase      ,      reduced_channels=int(round(in_channels / 4						)						)						)      ,      RegNetConvLayer(__lowercase      ,      __lowercase      ,      kernel_size=1      ,      activation=__lowercase						)      ,      )
  __A  :		str       =			ACTaFN[config.hidden_act]







 def  snake_case__   (      self      ,      __lowercase						):

  """simple docstring"""







  __A  :		Optional[int]       =			hidden_state
  __A  :		int       =			self.layer(__lowercase						)
  __A  :		Any       =			self.shortcut(__lowercase						)
  hidden_state += residual
  __A  :		List[Any]       =			self.activation(__lowercase						)
  return hidden_state


class 						_lowerCamelCase					(    nn.Module				):




 '''simple docstring'''


 def __init__(      self      ,      __lowercase      ,      __lowercase      ,      __lowercase      ,      __lowercase = 2      ,      __lowercase = 2      ,      ):

  """simple docstring"""







  super().__init__()

  __A  :		str       =			RegNetXLayer if config.layer_type == 'x' else RegNetYLayer

  __A  :		List[Any]       =			nn.Sequential(
      # downsampling is done in the first layer with stride of 2
      layer(
          __lowercase      ,      __lowercase      ,      __lowercase      ,      stride=__lowercase      ,      )      ,      *[layer(__lowercase      ,      __lowercase      ,      __lowercase						) for _ in range(depth - 1						)]      ,      )







 def  snake_case__   (      self      ,      __lowercase						):

  """simple docstring"""







  __A  :		List[str]       =			self.layers(__lowercase						)
  return hidden_state


class 						_lowerCamelCase					(    nn.Module				):




 '''simple docstring'''


 def __init__(      self      ,      __lowercase						):

  """simple docstring"""







  super().__init__()
  __A  :		Optional[Any]       =			nn.ModuleList([]						)
  # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input
  self.stages.append(
      RegNetStage(
          __lowercase      ,      config.embedding_size      ,      config.hidden_sizes[0]      ,      stride=2 if config.downsample_in_first_stage else 1      ,      depth=config.depths[0]      ,      )						)
  __A  :		List[str]       =			zip(config.hidden_sizes      ,      config.hidden_sizes[1:]						)
  for (in_channels, out_channels), depth in zip(__lowercase      ,      config.depths[1:]						):
   self.stages.append(RegNetStage(__lowercase      ,      __lowercase      ,      __lowercase      ,      depth=__lowercase						)						)







 def  snake_case__   (      self      ,      __lowercase      ,      __lowercase = False      ,      __lowercase = True						):

  """simple docstring"""







  __A  :		Optional[int]       =			() if output_hidden_states else None

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

   __A  :		str       =			stage_module(__lowercase						)

  if output_hidden_states:
   __A  :		List[Any]       =			hidden_states + (hidden_state,)

  if not return_dict:
   return tuple(v for v in [hidden_state, hidden_states] if v is not None						)

  return BaseModelOutputWithNoAttention(last_hidden_state=__lowercase      ,      hidden_states=__lowercase						)


class 						_lowerCamelCase					(    snake_case_				):




 '''simple docstring'''
 __lowercase				:		Union[str, Any]    							=						RegNetConfig
 __lowercase				:		Optional[int]    							=						'''regnet'''
 __lowercase				:		Any    							=						'''pixel_values'''
 __lowercase				:		Dict    							=						True


 def  snake_case__   (      self      ,      __lowercase						):

  """simple docstring"""







  if isinstance(__lowercase      ,      nn.Convad						):
   nn.init.kaiming_normal_(module.weight      ,      mode='fan_out'      ,      nonlinearity='relu'						)
  elif isinstance(__lowercase      ,      (nn.BatchNormad, nn.GroupNorm)						):
   nn.init.constant_(module.weight      ,      1						)
   nn.init.constant_(module.bias      ,      0						)







 def  snake_case__   (      self      ,      __lowercase      ,      __lowercase=False						):

  """simple docstring"""







  if isinstance(__lowercase      ,      __lowercase						):
   __A  :		Any       =			value


UpperCAmelCase_ :     List[str]								=      R'\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n\n    Parameters:\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 [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n'

UpperCAmelCase_ :     str								=      R'\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`ConvNextImageProcessor.__call__`] for details.\n\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 [`~file_utils.ModelOutput`] instead of a plain tuple.\n'

@add_start_docstrings(
    '''The bare RegNet model outputting raw features without any specific head on top.'''    ,		snake_case_    ,		)
# Copied from transformers.models.resnet.modeling_resnet.ResNetModel with RESNET->REGNET,ResNet->RegNet
class 						_lowerCamelCase					(    snake_case_				):




 '''simple docstring'''


 def __init__(      self      ,      __lowercase						):

  """simple docstring"""







  super().__init__(__lowercase						)
  __A  :		Tuple       =			config
  __A  :		int       =			RegNetEmbeddings(__lowercase						)
  __A  :		Union[str, Any]       =			RegNetEncoder(__lowercase						)
  __A  :		List[str]       =			nn.AdaptiveAvgPoolad((1, 1)						)
  # Initialize weights and apply final processing
  self.post_init()







 @add_start_docstrings_to_model_forward(__lowercase						)
 @add_code_sample_docstrings(
     checkpoint=_CHECKPOINT_FOR_DOC      ,      output_type=__lowercase      ,      config_class=_CONFIG_FOR_DOC      ,      modality='vision'      ,      expected_output=_EXPECTED_OUTPUT_SHAPE      ,      )
 def  snake_case__   (      self      ,      __lowercase      ,      __lowercase = None      ,      __lowercase = None						):

  """simple docstring"""







  __A  :		List[Any]       =			(
      output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
  )
  __A  :		int       =			return_dict if return_dict is not None else self.config.use_return_dict

  __A  :		Any       =			self.embedder(__lowercase						)

  __A  :		str       =			self.encoder(
      __lowercase      ,      output_hidden_states=__lowercase      ,      return_dict=__lowercase						)

  __A  :		Optional[int]       =			encoder_outputs[0]

  __A  :		Tuple       =			self.pooler(__lowercase						)

  if not return_dict:
   return (last_hidden_state, pooled_output) + encoder_outputs[1:]

  return BaseModelOutputWithPoolingAndNoAttention(
      last_hidden_state=__lowercase      ,      pooler_output=__lowercase      ,      hidden_states=encoder_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_    ,		)
# Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet
class 						_lowerCamelCase					(    snake_case_				):




 '''simple docstring'''


 def __init__(      self      ,      __lowercase						):

  """simple docstring"""







  super().__init__(__lowercase						)
  __A  :		Dict       =			config.num_labels
  __A  :		Union[str, Any]       =			RegNetModel(__lowercase						)
  # classification head
  __A  :		Any       =			nn.Sequential(
      nn.Flatten()      ,      nn.Linear(config.hidden_sizes[-1]      ,      config.num_labels						) if config.num_labels > 0 else nn.Identity()      ,      )
  # initialize weights and apply final processing
  self.post_init()







 @add_start_docstrings_to_model_forward(__lowercase						)
 @add_code_sample_docstrings(
     checkpoint=_IMAGE_CLASS_CHECKPOINT      ,      output_type=__lowercase      ,      config_class=_CONFIG_FOR_DOC      ,      expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT      ,      )
 def  snake_case__   (      self      ,      __lowercase = None      ,      __lowercase = None      ,      __lowercase = None      ,      __lowercase = None      ,      ):

  """simple docstring"""







  __A  :		List[str]       =			return_dict if return_dict is not None else self.config.use_return_dict

  __A  :		List[Any]       =			self.regnet(__lowercase      ,      output_hidden_states=__lowercase      ,      return_dict=__lowercase						)

  __A  :		Tuple       =			outputs.pooler_output if return_dict else outputs[1]

  __A  :		Union[str, Any]       =			self.classifier(__lowercase						)

  __A  :		Union[str, Any]       =			None

  if labels is not None:
   if self.config.problem_type is None:
    if self.num_labels == 1:
     __A  :		Optional[int]       =			'regression'
    elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
     __A  :		Optional[Any]       =			'single_label_classification'
    else:
     __A  :		List[str]       =			'multi_label_classification'
   if self.config.problem_type == "regression":
    __A  :		Union[str, Any]       =			MSELoss()
    if self.num_labels == 1:
     __A  :		Tuple       =			loss_fct(logits.squeeze()      ,      labels.squeeze()						)
    else:
     __A  :		Optional[Any]       =			loss_fct(__lowercase      ,      __lowercase						)
   elif self.config.problem_type == "single_label_classification":
    __A  :		str       =			CrossEntropyLoss()
    __A  :		Any       =			loss_fct(logits.view(-1      ,      self.num_labels						)      ,      labels.view(-1						)						)
   elif self.config.problem_type == "multi_label_classification":
    __A  :		Union[str, Any]       =			BCEWithLogitsLoss()
    __A  :		Any       =			loss_fct(__lowercase      ,      __lowercase						)

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

  return ImageClassifierOutputWithNoAttention(loss=__lowercase      ,      logits=__lowercase      ,      hidden_states=outputs.hidden_states						)










'''simple docstring'''
from heapq import heappop, heappush

import numpy as np


def 			_lowercase       (				UpperCamelCase__       :					np.ndarray,				UpperCamelCase__       :					tuple[int, int],				UpperCamelCase__       :					tuple[int, int],				UpperCamelCase__       :					bool,				):
 __A     ,__A  :		Optional[Any]       =			grid.shape
 __A  :		List[Any]       =			[-1, 1, 0, 0]
 __A  :		Optional[int]       =			[0, 0, -1, 1]
 if allow_diagonal:
  dx += [-1, -1, 1, 1]
  dy += [-1, 1, -1, 1]

 __A     ,__A  :		int       =			[(0, source)], set()
 __A  :		Any       =			np.full((rows, cols),				np.inf			)
 __A  :		int       =			0
 __A  :		Any       =			np.empty((rows, cols),				dtype=UpperCamelCase__			)
 __A  :		List[Any]       =			None

 while queue:
  ((__A)     ,(__A))  :		List[Any]       =			heappop(UpperCamelCase__			)
  if (x, y) in visited:
   continue
  visited.add((x, y)			)

  if (x, y) == destination:
   __A  :		Tuple       =			[]
   while (x, y) != source:
    path.append((x, y)			)
    __A     ,__A  :		int       =			predecessors[x, y]
   path.append(UpperCamelCase__			)  # add the source manually
   path.reverse()
   return matrix[destination], path

  for i in range(len(UpperCamelCase__			)			):
   __A     ,__A  :		List[Any]       =			x + dx[i], y + dy[i]
   if 0 <= nx < rows and 0 <= ny < cols:
    __A  :		str       =			grid[nx][ny]
    if next_node == 1 and matrix[nx, ny] > dist + 1:
     heappush(UpperCamelCase__,				(dist + 1, (nx, ny))			)
     __A  :		int       =			dist + 1
     __A  :		List[str]       =			(x, y)

 return np.inf, []


if __name__ == "__main__":
 import doctest

 doctest.testmod()






