





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_     							= logging.get_logger(__name__)

# General docstring
UpperCAmelCase_     							= 'RegNetConfig'

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

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

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






class     lowercase__       (	nn.Module				):

   '''simple docstring'''


   def __init__(    self,		__magic_name__,		__magic_name__,		__magic_name__ = 3,		__magic_name__ = 1,		__magic_name__ = 1,		__magic_name__ = "relu",		) ->      int:






       """simple docstring"""
       super().__init__()
       UpperCamelCase__							:				str          =      nn.Convad(
           __magic_name__,		__magic_name__,		kernel_size=__magic_name__,		stride=__magic_name__,		padding=kernel_size // 2,		groups=__magic_name__,		bias=__magic_name__,		)
       UpperCamelCase__							:				Optional[Any]          =      nn.BatchNormad(__magic_name__							)
       UpperCamelCase__							:				Dict          =      ACTaFN[activation] if activation is not None else nn.Identity()




   def       UpperCamelCase__  (    self,		__magic_name__							) ->      str:






       """simple docstring"""
       UpperCamelCase__							:				Dict          =      self.convolution(__magic_name__							)
       UpperCamelCase__							:				Optional[int]          =      self.normalization(__magic_name__							)
       UpperCamelCase__							:				Tuple          =      self.activation(__magic_name__							)
       return hidden_state






class     lowercase__       (	nn.Module				):

   '''simple docstring'''


   def __init__(    self,		__magic_name__							) ->      Any:






       """simple docstring"""
       super().__init__()
       UpperCamelCase__							:				List[Any]          =      RegNetConvLayer(
           config.num_channels,		config.embedding_size,		kernel_size=3,		stride=2,		activation=config.hidden_act							)
       UpperCamelCase__							:				Union[str, Any]          =      config.num_channels




   def       UpperCamelCase__  (    self,		__magic_name__							) ->      List[Any]:






       """simple docstring"""
       UpperCamelCase__							:				Union[str, Any]          =      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.'''							)
       UpperCamelCase__							:				int          =      self.embedder(__magic_name__							)
       return hidden_state






class     lowercase__       (	nn.Module				):

   '''simple docstring'''


   def __init__(    self,		__magic_name__,		__magic_name__,		__magic_name__ = 2							) ->      Dict:






       """simple docstring"""
       super().__init__()
       UpperCamelCase__							:				Dict          =      nn.Convad(__magic_name__,		__magic_name__,		kernel_size=1,		stride=__magic_name__,		bias=__magic_name__							)
       UpperCamelCase__							:				Any          =      nn.BatchNormad(__magic_name__							)




   def       UpperCamelCase__  (    self,		__magic_name__							) ->      Tensor:






       """simple docstring"""
       UpperCamelCase__							:				Dict          =      self.convolution(__magic_name__							)
       UpperCamelCase__							:				Dict          =      self.normalization(__magic_name__							)
       return hidden_state






class     lowercase__       (	nn.Module				):

   '''simple docstring'''


   def __init__(    self,		__magic_name__,		__magic_name__							) ->      Any:






       """simple docstring"""
       super().__init__()

       UpperCamelCase__							:				Optional[int]          =      nn.AdaptiveAvgPoolad((1, 1)							)
       UpperCamelCase__							:				Tuple          =      nn.Sequential(
           nn.Convad(__magic_name__,		__magic_name__,		kernel_size=1							),		nn.ReLU(),		nn.Convad(__magic_name__,		__magic_name__,		kernel_size=1							),		nn.Sigmoid(),		)




   def       UpperCamelCase__  (    self,		__magic_name__							) ->      Optional[int]:






       """simple docstring"""
       # b c h w -> b c 1 1
       UpperCamelCase__							:				Any          =      self.pooler(__magic_name__							)
       UpperCamelCase__							:				Union[str, Any]          =      self.attention(__magic_name__							)
       UpperCamelCase__							:				Optional[Any]          =      hidden_state * attention
       return hidden_state






class     lowercase__       (	nn.Module				):

   '''simple docstring'''


   def __init__(    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__ = 1							) ->      List[Any]:






       """simple docstring"""
       super().__init__()
       UpperCamelCase__							:				Optional[int]          =      in_channels != out_channels or stride != 1
       UpperCamelCase__							:				Dict          =      max(1,		out_channels // config.groups_width							)
       UpperCamelCase__							:				Any          =      (
           RegNetShortCut(__magic_name__,		__magic_name__,		stride=__magic_name__							) if should_apply_shortcut else nn.Identity()
       )
       UpperCamelCase__							:				Dict          =      nn.Sequential(
           RegNetConvLayer(__magic_name__,		__magic_name__,		kernel_size=1,		activation=config.hidden_act							),		RegNetConvLayer(__magic_name__,		__magic_name__,		stride=__magic_name__,		groups=__magic_name__,		activation=config.hidden_act							),		RegNetConvLayer(__magic_name__,		__magic_name__,		kernel_size=1,		activation=__magic_name__							),		)
       UpperCamelCase__							:				List[Any]          =      ACTaFN[config.hidden_act]




   def       UpperCamelCase__  (    self,		__magic_name__							) ->      int:






       """simple docstring"""
       UpperCamelCase__							:				Union[str, Any]          =      hidden_state
       UpperCamelCase__							:				Dict          =      self.layer(__magic_name__							)
       UpperCamelCase__							:				Optional[Any]          =      self.shortcut(__magic_name__							)
       hidden_state += residual
       UpperCamelCase__							:				Union[str, Any]          =      self.activation(__magic_name__							)
       return hidden_state






class     lowercase__       (	nn.Module				):

   '''simple docstring'''


   def __init__(    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__ = 1							) ->      Union[str, Any]:






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




   def       UpperCamelCase__  (    self,		__magic_name__							) ->      Dict:






       """simple docstring"""
       UpperCamelCase__							:				int          =      hidden_state
       UpperCamelCase__							:				Dict          =      self.layer(__magic_name__							)
       UpperCamelCase__							:				str          =      self.shortcut(__magic_name__							)
       hidden_state += residual
       UpperCamelCase__							:				Tuple          =      self.activation(__magic_name__							)
       return hidden_state






class     lowercase__       (	nn.Module				):

   '''simple docstring'''


   def __init__(    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__ = 2,		__magic_name__ = 2,		) ->      Optional[int]:






       """simple docstring"""
       super().__init__()

       UpperCamelCase__							:				Union[str, Any]          =      RegNetXLayer if config.layer_type == '''x''' else RegNetYLayer

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




   def       UpperCamelCase__  (    self,		__magic_name__							) ->      Optional[int]:






       """simple docstring"""
       UpperCamelCase__							:				Dict          =      self.layers(__magic_name__							)
       return hidden_state






class     lowercase__       (	nn.Module				):

   '''simple docstring'''


   def __init__(    self,		__magic_name__							) ->      Union[str, Any]:






       """simple docstring"""
       super().__init__()
       UpperCamelCase__							:				Dict          =      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(
               __magic_name__,		config.embedding_size,		config.hidden_sizes[0],		stride=2 if config.downsample_in_first_stage else 1,		depth=config.depths[0],		)							)
       UpperCamelCase__							:				List[Any]          =      zip(config.hidden_sizes,		config.hidden_sizes[1:]							)
       for (in_channels, out_channels), depth in zip(__magic_name__,		config.depths[1:]							):
           self.stages.append(RegNetStage(__magic_name__,		__magic_name__,		__magic_name__,		depth=__magic_name__							)							)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__ = False,		__magic_name__ = True							) ->      BaseModelOutputWithNoAttention:






       """simple docstring"""
       UpperCamelCase__							:				Any          =      () if output_hidden_states else None

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

           UpperCamelCase__							:				str          =      stage_module(__magic_name__							)

       if output_hidden_states:
           UpperCamelCase__							:				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 BaseModelOutputWithNoAttention(last_hidden_state=__magic_name__,		hidden_states=__magic_name__							)






class     lowercase__       (	__lowerCamelCase				):

   '''simple docstring'''


   a						:							Any   		=		RegNetConfig
   a						:							Tuple   		=		"regnet"
   a						:							int   		=		"pixel_values"
   a						:							int   		=		True




   def       UpperCamelCase__  (    self,		__magic_name__							) ->      Optional[int]:






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




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__=False							) ->      Dict:






       """simple docstring"""
       if isinstance(__magic_name__,		__magic_name__							):
           UpperCamelCase__							:				Tuple          =      value




UpperCAmelCase_     							= 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_     							= 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."			,				__lowerCamelCase			,				)
# Copied from transformers.models.resnet.modeling_resnet.ResNetModel with RESNET->REGNET,ResNet->RegNet
class     lowercase__       (	__lowerCamelCase				):

   '''simple docstring'''


   def __init__(    self,		__magic_name__							) ->      Tuple:






       """simple docstring"""
       super().__init__(__magic_name__							)
       UpperCamelCase__							:				int          =      config
       UpperCamelCase__							:				Any          =      RegNetEmbeddings(__magic_name__							)
       UpperCamelCase__							:				Dict          =      RegNetEncoder(__magic_name__							)
       UpperCamelCase__							:				Union[str, Any]          =      nn.AdaptiveAvgPoolad((1, 1)							)
       # Initialize weights and apply final processing
       self.post_init()




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






       """simple docstring"""
       UpperCamelCase__							:				Any          =      (
           output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
       )
       UpperCamelCase__							:				List[str]          =      return_dict if return_dict is not None else self.config.use_return_dict

       UpperCamelCase__							:				Optional[Any]          =      self.embedder(__magic_name__							)

       UpperCamelCase__							:				Tuple          =      self.encoder(
           __magic_name__,		output_hidden_states=__magic_name__,		return_dict=__magic_name__							)

       UpperCamelCase__							:				Union[str, Any]          =      encoder_outputs[0]

       UpperCamelCase__							:				Optional[int]          =      self.pooler(__magic_name__							)

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

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

   '''simple docstring'''


   def __init__(    self,		__magic_name__							) ->      Any:






       """simple docstring"""
       super().__init__(__magic_name__							)
       UpperCamelCase__							:				Dict          =      config.num_labels
       UpperCamelCase__							:				Any          =      RegNetModel(__magic_name__							)
       # classification head
       UpperCamelCase__							:				Tuple          =      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(__magic_name__							)
   @add_code_sample_docstrings(
       checkpoint=_IMAGE_CLASS_CHECKPOINT,		output_type=__magic_name__,		config_class=_CONFIG_FOR_DOC,		expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT,		)
   def       UpperCamelCase__  (    self,		__magic_name__ = None,		__magic_name__ = None,		__magic_name__ = None,		__magic_name__ = None,		) ->      ImageClassifierOutputWithNoAttention:






       """simple docstring"""
       UpperCamelCase__							:				str          =      return_dict if return_dict is not None else self.config.use_return_dict

       UpperCamelCase__							:				List[str]          =      self.regnet(__magic_name__,		output_hidden_states=__magic_name__,		return_dict=__magic_name__							)

       UpperCamelCase__							:				str          =      outputs.pooler_output if return_dict else outputs[1]

       UpperCamelCase__							:				List[Any]          =      self.classifier(__magic_name__							)

       UpperCamelCase__							:				List[str]          =      None

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

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

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








from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


UpperCAmelCase_     							= {
    'configuration_convbert': ['CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ConvBertConfig', 'ConvBertOnnxConfig'],
    'tokenization_convbert': ['ConvBertTokenizer'],
}

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_     							= ['ConvBertTokenizerFast']

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_     							= [
      'CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST',
      'ConvBertForMaskedLM',
      'ConvBertForMultipleChoice',
      'ConvBertForQuestionAnswering',
      'ConvBertForSequenceClassification',
      'ConvBertForTokenClassification',
      'ConvBertLayer',
      'ConvBertModel',
      'ConvBertPreTrainedModel',
      'load_tf_weights_in_convbert',
  ]


try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_     							= [
      'TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST',
      'TFConvBertForMaskedLM',
      'TFConvBertForMultipleChoice',
      'TFConvBertForQuestionAnswering',
      'TFConvBertForSequenceClassification',
      'TFConvBertForTokenClassification',
      'TFConvBertLayer',
      'TFConvBertModel',
      'TFConvBertPreTrainedModel',
  ]


if TYPE_CHECKING:
  from .configuration_convbert import CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ConvBertConfig, ConvBertOnnxConfig
  from .tokenization_convbert import ConvBertTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_convbert_fast import ConvBertTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_convbert import (
        CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
        ConvBertForMaskedLM,
        ConvBertForMultipleChoice,
        ConvBertForQuestionAnswering,
        ConvBertForSequenceClassification,
        ConvBertForTokenClassification,
        ConvBertLayer,
        ConvBertModel,
        ConvBertPreTrainedModel,
        load_tf_weights_in_convbert,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_convbert import (
        TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFConvBertForMaskedLM,
        TFConvBertForMultipleChoice,
        TFConvBertForQuestionAnswering,
        TFConvBertForSequenceClassification,
        TFConvBertForTokenClassification,
        TFConvBertLayer,
        TFConvBertModel,
        TFConvBertPreTrainedModel,
    )


else:
  import sys

  UpperCAmelCase_     							= _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)

