





"""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 ...modeling_outputs import (
    BackboneOutput,
    BaseModelOutputWithNoAttention,
    BaseModelOutputWithPoolingAndNoAttention,
    ImageClassifierOutputWithNoAttention,
)
from ...modeling_utils import PreTrainedModel
from ...utils import (
    add_code_sample_docstrings,
    add_start_docstrings,
    add_start_docstrings_to_model_forward,
    logging,
    replace_return_docstrings,
)
from ...utils.backbone_utils import BackboneMixin
from .configuration_resnet import ResNetConfig


__UpperCamelCase     :						Union[str, Any]						       =  logging.get_logger(__name__)

# General docstring
__UpperCamelCase     :						Optional[Any]						       =  "ResNetConfig"

# Base docstring
__UpperCamelCase     :						Any						       =  "microsoft/resnet-50"
__UpperCamelCase     :						str						       =  [1, 2048, 7, 7]

# Image classification docstring
__UpperCamelCase     :						Tuple						       =  "microsoft/resnet-50"
__UpperCamelCase     :						Union[str, Any]						       =  "tiger cat"

__UpperCamelCase     :						Optional[Any]						       =  [
    "microsoft/resnet-50",
    # See all resnet models at https://huggingface.co/models?filter=resnet
]







class       a							(   nn.Module  ):



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







  """simple docstring"""






  super().__init__()
  lowerCAmelCase    				= nn.Convad(
      _snake_case				,       _snake_case				,       kernel_size=_snake_case				,       stride=_snake_case				,       padding=kernel_size // 2				,       bias=_snake_case     )
  lowerCAmelCase    				= nn.BatchNormad(_snake_case     )
  lowerCAmelCase    				= ACTaFN[activation] if activation is not None else nn.Identity()



 def    UpperCamelCase__  (     self				,       _snake_case     ):







  """simple docstring"""






  lowerCAmelCase    				= self.convolution(_snake_case     )
  lowerCAmelCase    				= self.normalization(_snake_case     )
  lowerCAmelCase    				= self.activation(_snake_case     )
  return hidden_state







class       a							(   nn.Module  ):



 def __init__(     self				,       _snake_case     ):







  """simple docstring"""






  super().__init__()
  lowerCAmelCase    				= ResNetConvLayer(
      config.num_channels				,       config.embedding_size				,       kernel_size=7				,       stride=2				,       activation=config.hidden_act     )
  lowerCAmelCase    				= nn.MaxPoolad(kernel_size=3				,       stride=2				,       padding=1     )
  lowerCAmelCase    				= config.num_channels



 def    UpperCamelCase__  (     self				,       _snake_case     ):







  """simple docstring"""






  lowerCAmelCase    				= 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.'     )
  lowerCAmelCase    				= self.embedder(_snake_case     )
  lowerCAmelCase    				= self.pooler(_snake_case     )
  return embedding







class       a							(   nn.Module  ):



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







  """simple docstring"""






  super().__init__()
  lowerCAmelCase    				= nn.Convad(_snake_case				,       _snake_case				,       kernel_size=1				,       stride=_snake_case				,       bias=_snake_case     )
  lowerCAmelCase    				= nn.BatchNormad(_snake_case     )



 def    UpperCamelCase__  (     self				,       _snake_case     ):







  """simple docstring"""






  lowerCAmelCase    				= self.convolution(_snake_case     )
  lowerCAmelCase    				= self.normalization(_snake_case     )
  return hidden_state







class       a							(   nn.Module  ):



 def __init__(     self				,       _snake_case				,       _snake_case				,       _snake_case = 1				,       _snake_case = "relu"     ):







  """simple docstring"""






  super().__init__()
  lowerCAmelCase    				= in_channels != out_channels or stride != 1
  lowerCAmelCase    				= (
      ResNetShortCut(_snake_case				,       _snake_case				,       stride=_snake_case     ) if should_apply_shortcut else nn.Identity()
  )
  lowerCAmelCase    				= nn.Sequential(
      ResNetConvLayer(_snake_case				,       _snake_case				,       stride=_snake_case     )				,       ResNetConvLayer(_snake_case				,       _snake_case				,       activation=_snake_case     )				,       )
  lowerCAmelCase    				= ACTaFN[activation]



 def    UpperCamelCase__  (     self				,       _snake_case     ):







  """simple docstring"""






  lowerCAmelCase    				= hidden_state
  lowerCAmelCase    				= self.layer(_snake_case     )
  lowerCAmelCase    				= self.shortcut(_snake_case     )
  hidden_state += residual
  lowerCAmelCase    				= self.activation(_snake_case     )
  return hidden_state







class       a							(   nn.Module  ):



 def __init__(     self				,       _snake_case				,       _snake_case				,       _snake_case = 1				,       _snake_case = "relu"				,       _snake_case = 4     ):







  """simple docstring"""






  super().__init__()
  lowerCAmelCase    				= in_channels != out_channels or stride != 1
  lowerCAmelCase    				= out_channels // reduction
  lowerCAmelCase    				= (
      ResNetShortCut(_snake_case				,       _snake_case				,       stride=_snake_case     ) if should_apply_shortcut else nn.Identity()
  )
  lowerCAmelCase    				= nn.Sequential(
      ResNetConvLayer(_snake_case				,       _snake_case				,       kernel_size=1     )				,       ResNetConvLayer(_snake_case				,       _snake_case				,       stride=_snake_case     )				,       ResNetConvLayer(_snake_case				,       _snake_case				,       kernel_size=1				,       activation=_snake_case     )				,       )
  lowerCAmelCase    				= ACTaFN[activation]



 def    UpperCamelCase__  (     self				,       _snake_case     ):







  """simple docstring"""






  lowerCAmelCase    				= hidden_state
  lowerCAmelCase    				= self.layer(_snake_case     )
  lowerCAmelCase    				= self.shortcut(_snake_case     )
  hidden_state += residual
  lowerCAmelCase    				= self.activation(_snake_case     )
  return hidden_state







class       a							(   nn.Module  ):



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







  """simple docstring"""






  super().__init__()

  lowerCAmelCase    				= ResNetBottleNeckLayer if config.layer_type == 'bottleneck' else ResNetBasicLayer

  lowerCAmelCase    				= nn.Sequential(
      # downsampling is done in the first layer with stride of 2
      layer(_snake_case				,       _snake_case				,       stride=_snake_case				,       activation=config.hidden_act     )				,       *[layer(_snake_case				,       _snake_case				,       activation=config.hidden_act     ) for _ in range(depth - 1     )]				,       )



 def    UpperCamelCase__  (     self				,       _snake_case     ):







  """simple docstring"""






  lowerCAmelCase    				= input
  for layer in self.layers:
   lowerCAmelCase    				= layer(_snake_case     )
  return hidden_state







class       a							(   nn.Module  ):



 def __init__(     self				,       _snake_case     ):







  """simple docstring"""






  super().__init__()
  lowerCAmelCase    				= 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(
      ResNetStage(
          _snake_case				,       config.embedding_size				,       config.hidden_sizes[0]				,       stride=2 if config.downsample_in_first_stage else 1				,       depth=config.depths[0]				,       )     )
  lowerCAmelCase    				= zip(config.hidden_sizes				,       config.hidden_sizes[1:]     )
  for (in_channels, out_channels), depth in zip(_snake_case				,       config.depths[1:]     ):
   self.stages.append(ResNetStage(_snake_case				,       _snake_case				,       _snake_case				,       depth=_snake_case     )     )



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case = False				,       _snake_case = True     ):







  """simple docstring"""






  lowerCAmelCase    				= () if output_hidden_states else None

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

   lowerCAmelCase    				= stage_module(_snake_case     )

  if output_hidden_states:
   lowerCAmelCase    				= 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=_snake_case				,       hidden_states=_snake_case				,       )







class       a							(   a_  ):
 snake_case__             =       ResNetConfig
 snake_case__             =       "resnet"
 snake_case__             =       "pixel_values"
 snake_case__             =       True



 def    UpperCamelCase__  (     self				,       _snake_case     ):







  """simple docstring"""






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



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case=False     ):







  """simple docstring"""






  if isinstance(_snake_case				,       _snake_case     ):
   lowerCAmelCase    				= value


__UpperCamelCase     :						int						       =  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 ([`ResNetConfig`]): 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     :						Union[str, Any]						       =  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 [`~utils.ModelOutput`] instead of a plain tuple.\n"







@add_start_docstrings(
    '''The bare ResNet model outputting raw features without any specific head on top.'''   , a_   , )
class       a							(   a_  ):



 def __init__(     self				,       _snake_case     ):







  """simple docstring"""






  super().__init__(_snake_case     )
  lowerCAmelCase    				= config
  lowerCAmelCase    				= ResNetEmbeddings(_snake_case     )
  lowerCAmelCase    				= ResNetEncoder(_snake_case     )
  lowerCAmelCase    				= nn.AdaptiveAvgPoolad((1, 1)     )
  # Initialize weights and apply final processing
  self.post_init()



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







  """simple docstring"""






  lowerCAmelCase    				= (
      output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
  )
  lowerCAmelCase    				= return_dict if return_dict is not None else self.config.use_return_dict

  lowerCAmelCase    				= self.embedder(_snake_case     )

  lowerCAmelCase    				= self.encoder(
      _snake_case				,       output_hidden_states=_snake_case				,       return_dict=_snake_case     )

  lowerCAmelCase    				= encoder_outputs[0]

  lowerCAmelCase    				= self.pooler(_snake_case     )

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

  return BaseModelOutputWithPoolingAndNoAttention(
      last_hidden_state=_snake_case				,       pooler_output=_snake_case				,       hidden_states=encoder_outputs.hidden_states				,       )







@add_start_docstrings(
    '''\n    ResNet 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       a							(   a_  ):



 def __init__(     self				,       _snake_case     ):







  """simple docstring"""






  super().__init__(_snake_case     )
  lowerCAmelCase    				= config.num_labels
  lowerCAmelCase    				= ResNetModel(_snake_case     )
  # classification head
  lowerCAmelCase    				= 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(_snake_case     )
 @add_code_sample_docstrings(
     checkpoint=_IMAGE_CLASS_CHECKPOINT				,       output_type=_snake_case				,       config_class=_CONFIG_FOR_DOC				,       expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT				,       )
 def    UpperCamelCase__  (     self				,       _snake_case = None				,       _snake_case = None				,       _snake_case = None				,       _snake_case = None				,       ):







  """simple docstring"""






  lowerCAmelCase    				= return_dict if return_dict is not None else self.config.use_return_dict

  lowerCAmelCase    				= self.resnet(_snake_case				,       output_hidden_states=_snake_case				,       return_dict=_snake_case     )

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

  lowerCAmelCase    				= self.classifier(_snake_case     )

  lowerCAmelCase    				= None

  if labels is not None:
   if self.config.problem_type is None:
    if self.num_labels == 1:
     lowerCAmelCase    				= 'regression'
    elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
     lowerCAmelCase    				= 'single_label_classification'
    else:
     lowerCAmelCase    				= 'multi_label_classification'
   if self.config.problem_type == "regression":
    lowerCAmelCase    				= MSELoss()
    if self.num_labels == 1:
     lowerCAmelCase    				= loss_fct(logits.squeeze()				,       labels.squeeze()     )
    else:
     lowerCAmelCase    				= loss_fct(_snake_case				,       _snake_case     )
   elif self.config.problem_type == "single_label_classification":
    lowerCAmelCase    				= CrossEntropyLoss()
    lowerCAmelCase    				= loss_fct(logits.view(-1				,       self.num_labels     )				,       labels.view(-1     )     )
   elif self.config.problem_type == "multi_label_classification":
    lowerCAmelCase    				= BCEWithLogitsLoss()
    lowerCAmelCase    				= loss_fct(_snake_case				,       _snake_case     )

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

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







@add_start_docstrings(
    '''\n    ResNet backbone, to be used with frameworks like DETR and MaskFormer.\n    '''   , a_   , )
class       a							(   a_   , a_  ):



 def __init__(     self				,       _snake_case     ):







  """simple docstring"""






  super().__init__(_snake_case     )
  super()._init_backbone(_snake_case     )

  lowerCAmelCase    				= [config.embedding_size] + config.hidden_sizes
  lowerCAmelCase    				= ResNetEmbeddings(_snake_case     )
  lowerCAmelCase    				= ResNetEncoder(_snake_case     )

  # initialize weights and apply final processing
  self.post_init()



 @add_start_docstrings_to_model_forward(_snake_case     )
 @replace_return_docstrings(output_type=_snake_case				,       config_class=_CONFIG_FOR_DOC     )
 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case = None				,       _snake_case = None     ):







  """simple docstring"""






  lowerCAmelCase    				= return_dict if return_dict is not None else self.config.use_return_dict
  lowerCAmelCase    				= (
      output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
  )

  lowerCAmelCase    				= self.embedder(_snake_case     )

  lowerCAmelCase    				= self.encoder(_snake_case				,       output_hidden_states=_snake_case				,       return_dict=_snake_case     )

  lowerCAmelCase    				= outputs.hidden_states

  lowerCAmelCase    				= ()
  for idx, stage in enumerate(self.stage_names     ):
   if stage in self.out_features:
    feature_maps += (hidden_states[idx],)

  if not return_dict:
   lowerCAmelCase    				= (feature_maps,)
   if output_hidden_states:
    output += (outputs.hidden_states,)
   return output

  return BackboneOutput(
      feature_maps=_snake_case				,       hidden_states=outputs.hidden_states if output_hidden_states else None				,       attentions=_snake_case				,       )









import math

import tensorflow as tf
from packaging import version






def 	__lowerCamelCase						(							lowerCamelCase__		:						Any      ):





			'''simple docstring'''




			lowerCamelCase    		=  tf.convert_to_tensor(lowerCamelCase__      )
			lowerCamelCase    		=  0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0      )  ,							x.dtype      )      ))

			return x * cdf






def 	__lowerCamelCase						(							lowerCamelCase__		:						List[str]      ):





			'''simple docstring'''




			lowerCamelCase    		=  tf.convert_to_tensor(lowerCamelCase__      )
			lowerCamelCase    		=  tf.cast(math.pi  ,							x.dtype      )
			lowerCamelCase    		=  tf.cast(0.0_4_4_7_1_5  ,							x.dtype      )
			lowerCamelCase    		=  0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi      ) * (x + coeff * tf.pow(lowerCamelCase__  ,							3      ))      ))

			return x * cdf






def 	__lowerCamelCase						(							lowerCamelCase__		:						int      ):





			'''simple docstring'''




			lowerCamelCase    		=  tf.convert_to_tensor(lowerCamelCase__      )

			return x * tf.tanh(tf.math.softplus(lowerCamelCase__      )      )






def 	__lowerCamelCase						(							lowerCamelCase__		:						Union[str, Any]      ):





			'''simple docstring'''




			lowerCamelCase    		=  tf.convert_to_tensor(lowerCamelCase__      )
			lowerCamelCase    		=  tf.cast(0.0_4_4_7_1_5  ,							x.dtype      )
			lowerCamelCase    		=  tf.cast(0.7_9_7_8_8_4_5_6_0_8  ,							x.dtype      )

			return 0.5 * x * (1.0 + tf.tanh(x * coeffa * (1.0 + coeffa * x * x)      ))






def 	__lowerCamelCase						(							lowerCamelCase__		:						Optional[int]      ):





			'''simple docstring'''




			lowerCamelCase    		=  tf.convert_to_tensor(lowerCamelCase__      )
			lowerCamelCase    		=  tf.cast(1.7_0_2  ,							x.dtype      )
			return x * tf.math.sigmoid(coeff * x      )






def 	__lowerCamelCase						(							lowerCamelCase__		:						Dict      ):





			'''simple docstring'''




			return tf.clip_by_value(_gelu(lowerCamelCase__      )  ,							-10  ,							10      )






def 	__lowerCamelCase						(							lowerCamelCase__		:						int  ,							lowerCamelCase__		:						Dict=-1      ):





			'''simple docstring'''




			lowerCamelCase		,		lowerCamelCase    		=  tf.split(lowerCamelCase__  ,							2  ,							axis=lowerCamelCase__      )
			return a * tf.math.sigmoid(lowerCamelCase__      )


if version.parse(tf.version.VERSION) >= version.parse("2.4"):






		def 	__lowerCamelCase						(							lowerCamelCase__		:						Union[str, Any]      ):





					'''simple docstring'''




					return tf.keras.activations.gelu(lowerCamelCase__  ,							approximate=lowerCamelCase__      )

		UpperCAmelCase   :    Union[str, Any]     =		tf.keras.activations.gelu



		UpperCAmelCase   :    int     =		approximate_gelu_wrap
else:
		UpperCAmelCase   :    Dict     =		_gelu
		UpperCAmelCase   :    Dict     =		_gelu_new


UpperCAmelCase   :    List[str]     =		{
    "gelu": gelu,
    "gelu_10": gelu_aa,
    "gelu_fast": gelu_fast,
    "gelu_new": gelu_new,
    "glu": glu,
    "mish": mish,
    "quick_gelu": quick_gelu,
    "relu": tf.keras.activations.relu,
    "sigmoid": tf.keras.activations.sigmoid,
    "silu": tf.keras.activations.swish,
    "swish": tf.keras.activations.swish,
    "tanh": tf.keras.activations.tanh,
}






def 	__lowerCamelCase						(							lowerCamelCase__		:						Tuple      ):





			'''simple docstring'''




			if activation_string in ACTaFN:
						return ACTaFN[activation_string]
			else:
						raise KeyError(f'function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()      )}'      )

