




"""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


a         =      logging.get_logger(__name__)

# General docstring
a         =      '''RegNetConfig'''

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

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

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





class     lowercase_	(					nn.Module							):



 '''simple docstring'''

 def __init__(			self	:	str   ,							_UpperCAmelCase	:	int   ,							_UpperCAmelCase	:	int   ,							_UpperCAmelCase	:	int = 3   ,							_UpperCAmelCase	:	int = 1   ,							_UpperCAmelCase	:	int = 1   ,							_UpperCAmelCase	:	Optional[str] = "relu"   ,							):
       super().__init__()
       _A        =       nn.Convad(
           _UpperCAmelCase   ,							_UpperCAmelCase   ,							kernel_size=_UpperCAmelCase   ,							stride=_UpperCAmelCase   ,							padding=kernel_size // 2   ,							groups=_UpperCAmelCase   ,							bias=_UpperCAmelCase   ,							)
       _A        =       nn.BatchNormad(_UpperCAmelCase		)
       _A        =       ACTaFN[activation] if activation is not None else nn.Identity()



 def       lowerCAmelCase_							(			self	:	Dict   ,							_UpperCAmelCase	:	int		):
       _A        =       self.convolution(_UpperCAmelCase		)
       _A        =       self.normalization(_UpperCAmelCase		)
       _A        =       self.activation(_UpperCAmelCase		)
       return hidden_state





class     lowercase_	(					nn.Module							):



 '''simple docstring'''

 def __init__(			self	:	Dict   ,							_UpperCAmelCase	:	RegNetConfig		):
       super().__init__()
       _A        =       RegNetConvLayer(
           config.num_channels   ,							config.embedding_size   ,							kernel_size=3   ,							stride=2   ,							activation=config.hidden_act		)
       _A        =       config.num_channels



 def       lowerCAmelCase_							(			self	:	int   ,							_UpperCAmelCase	:	List[Any]		):
       _A        =       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        =       self.embedder(_UpperCAmelCase		)
       return hidden_state





class     lowercase_	(					nn.Module							):



 '''simple docstring'''

 def __init__(			self	:	Optional[Any]   ,							_UpperCAmelCase	:	int   ,							_UpperCAmelCase	:	int   ,							_UpperCAmelCase	:	int = 2		):
       super().__init__()
       _A        =       nn.Convad(_UpperCAmelCase   ,							_UpperCAmelCase   ,							kernel_size=1   ,							stride=_UpperCAmelCase   ,							bias=_UpperCAmelCase		)
       _A        =       nn.BatchNormad(_UpperCAmelCase		)



 def       lowerCAmelCase_							(			self	:	str   ,							_UpperCAmelCase	:	Tensor		):
       _A        =       self.convolution(_UpperCAmelCase		)
       _A        =       self.normalization(_UpperCAmelCase		)
       return hidden_state





class     lowercase_	(					nn.Module							):



 '''simple docstring'''

 def __init__(			self	:	Dict   ,							_UpperCAmelCase	:	int   ,							_UpperCAmelCase	:	int		):
       super().__init__()

       _A        =       nn.AdaptiveAvgPoolad((1, 1)		)
       _A        =       nn.Sequential(
           nn.Convad(_UpperCAmelCase   ,							_UpperCAmelCase   ,							kernel_size=1		)   ,							nn.ReLU()   ,							nn.Convad(_UpperCAmelCase   ,							_UpperCAmelCase   ,							kernel_size=1		)   ,							nn.Sigmoid()   ,							)



 def       lowerCAmelCase_							(			self	:	List[str]   ,							_UpperCAmelCase	:	Any		):
       # b c h w -> b c 1 1
       _A        =       self.pooler(_UpperCAmelCase		)
       _A        =       self.attention(_UpperCAmelCase		)
       _A        =       hidden_state * attention
       return hidden_state





class     lowercase_	(					nn.Module							):



 '''simple docstring'''

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



 def       lowerCAmelCase_							(			self	:	str   ,							_UpperCAmelCase	:	List[str]		):
       _A        =       hidden_state
       _A        =       self.layer(_UpperCAmelCase		)
       _A        =       self.shortcut(_UpperCAmelCase		)
       hidden_state += residual
       _A        =       self.activation(_UpperCAmelCase		)
       return hidden_state





class     lowercase_	(					nn.Module							):



 '''simple docstring'''

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



 def       lowerCAmelCase_							(			self	:	Dict   ,							_UpperCAmelCase	:	List[str]		):
       _A        =       hidden_state
       _A        =       self.layer(_UpperCAmelCase		)
       _A        =       self.shortcut(_UpperCAmelCase		)
       hidden_state += residual
       _A        =       self.activation(_UpperCAmelCase		)
       return hidden_state





class     lowercase_	(					nn.Module							):



 '''simple docstring'''

 def __init__(			self	:	List[Any]   ,							_UpperCAmelCase	:	RegNetConfig   ,							_UpperCAmelCase	:	int   ,							_UpperCAmelCase	:	int   ,							_UpperCAmelCase	:	int = 2   ,							_UpperCAmelCase	:	int = 2   ,							):
       super().__init__()

       _A        =       RegNetXLayer if config.layer_type == 'x' else RegNetYLayer

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



 def       lowerCAmelCase_							(			self	:	Dict   ,							_UpperCAmelCase	:	Optional[Any]		):
       _A        =       self.layers(_UpperCAmelCase		)
       return hidden_state





class     lowercase_	(					nn.Module							):



 '''simple docstring'''

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



 def       lowerCAmelCase_							(			self	:	Any   ,							_UpperCAmelCase	:	Tensor   ,							_UpperCAmelCase	:	bool = False   ,							_UpperCAmelCase	:	bool = True		):
       _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(_UpperCAmelCase		)

       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 BaseModelOutputWithNoAttention(last_hidden_state=_UpperCAmelCase   ,							hidden_states=_UpperCAmelCase		)





class     lowercase_	(					__lowerCAmelCase							):



 '''simple docstring'''
 UpperCAmelCase   :					Any				  =     RegNetConfig
 UpperCAmelCase   :					int				  =     '''regnet'''
 UpperCAmelCase   :					int				  =     '''pixel_values'''
 UpperCAmelCase   :					Any				  =     True

 def       lowerCAmelCase_							(			self	:	str   ,							_UpperCAmelCase	:	List[Any]		):
       if isinstance(_UpperCAmelCase   ,							nn.Convad		):
             nn.init.kaiming_normal_(module.weight   ,							mode='fan_out'   ,							nonlinearity='relu'		)
       elif isinstance(_UpperCAmelCase   ,							(nn.BatchNormad, nn.GroupNorm)		):
             nn.init.constant_(module.weight   ,							1		)
             nn.init.constant_(module.bias   ,							0		)



 def       lowerCAmelCase_							(			self	:	Dict   ,							_UpperCAmelCase	:	Dict   ,							_UpperCAmelCase	:	Optional[int]=False		):
       if isinstance(_UpperCAmelCase   ,							_UpperCAmelCase		):
             _A        =       value



a         =      r'''
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it
    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        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 [`~PreTrainedModel.from_pretrained`] method to load the model weights.
'''

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





@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	:	List[Any]   ,							_UpperCAmelCase	:	Dict		):
       super().__init__(_UpperCAmelCase		)
       _A        =       config
       _A        =       RegNetEmbeddings(_UpperCAmelCase		)
       _A        =       RegNetEncoder(_UpperCAmelCase		)
       _A        =       nn.AdaptiveAvgPoolad((1, 1)		)
       # Initialize weights and apply final processing
       self.post_init()



 @add_start_docstrings_to_model_forward(_UpperCAmelCase		)
 @add_code_sample_docstrings(
     checkpoint=_CHECKPOINT_FOR_DOC   ,							output_type=_UpperCAmelCase   ,							config_class=_CONFIG_FOR_DOC   ,							modality='vision'   ,							expected_output=_EXPECTED_OUTPUT_SHAPE   ,							)
 def       lowerCAmelCase_							(			self	:	Any   ,							_UpperCAmelCase	:	Tensor   ,							_UpperCAmelCase	:	Optional[bool] = None   ,							_UpperCAmelCase	:	Optional[bool] = None		):
       _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(_UpperCAmelCase		)

       _A        =       self.encoder(
           _UpperCAmelCase   ,							output_hidden_states=_UpperCAmelCase   ,							return_dict=_UpperCAmelCase		)

       _A        =       encoder_outputs[0]

       _A        =       self.pooler(_UpperCAmelCase		)

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

       return BaseModelOutputWithPoolingAndNoAttention(
           last_hidden_state=_UpperCAmelCase   ,							pooler_output=_UpperCAmelCase   ,							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.
    '''      ,     __lowerCAmelCase      ,     )
# Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet
class     lowercase_	(					__lowerCAmelCase							):



 '''simple docstring'''

 def __init__(			self	:	List[Any]   ,							_UpperCAmelCase	:	int		):
       super().__init__(_UpperCAmelCase		)
       _A        =       config.num_labels
       _A        =       RegNetModel(_UpperCAmelCase		)
       # classification head
       _A        =       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(_UpperCAmelCase		)
 @add_code_sample_docstrings(
     checkpoint=_IMAGE_CLASS_CHECKPOINT   ,							output_type=_UpperCAmelCase   ,							config_class=_CONFIG_FOR_DOC   ,							expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT   ,							)
 def       lowerCAmelCase_							(			self	:	Dict   ,							_UpperCAmelCase	:	Optional[torch.FloatTensor] = None   ,							_UpperCAmelCase	:	Optional[torch.LongTensor] = None   ,							_UpperCAmelCase	:	Optional[bool] = None   ,							_UpperCAmelCase	:	Optional[bool] = None   ,							):
       _A        =       return_dict if return_dict is not None else self.config.use_return_dict

       _A        =       self.regnet(_UpperCAmelCase   ,							output_hidden_states=_UpperCAmelCase   ,							return_dict=_UpperCAmelCase		)

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

       _A        =       self.classifier(_UpperCAmelCase		)

       _A        =       None

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

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

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







"""simple docstring"""

import enum
import os
from hashlib import shaaaa
from typing import Optional

from .. import config
from .logging import get_logger


a         =      get_logger(__name__)





class     lowercase_	(					enum.Enum							):



 '''simple docstring'''
 UpperCAmelCase   :					Optional[int]				  =     '''all_checks'''
 UpperCAmelCase   :					List[Any]				  =     '''basic_checks'''
 UpperCAmelCase   :					Any				  =     '''no_checks'''





class     lowercase_	(					__lowerCAmelCase							):



 '''simple docstring'''





class     lowercase_	(					__lowerCAmelCase							):



 '''simple docstring'''





class     lowercase_	(					__lowerCAmelCase							):



 '''simple docstring'''





class     lowercase_	(					__lowerCAmelCase							):



 '''simple docstring'''






def 						_snake_case					( _snake_case							: Optional[dict]    ,  _snake_case							: dict    ,  _snake_case							: Dict=None							)   -> Dict:






      '''simple docstring'''


      if expected_checksums is None:
            logger.info('Unable to verify checksums.'							)
            return
      if len(set(_snake_case							) - set(_snake_case							)							) > 0:
            raise ExpectedMoreDownloadedFiles(str(set(_snake_case							) - set(_snake_case							)							)							)
      if len(set(_snake_case							) - set(_snake_case							)							) > 0:
            raise UnexpectedDownloadedFile(str(set(_snake_case							) - set(_snake_case							)							)							)
      _A        =       [url for url in expected_checksums if expected_checksums[url] != recorded_checksums[url]]
      _A        =       ' for ' + verification_name if verification_name is not None else ''
      if len(_snake_case							) > 0:
            raise NonMatchingChecksumError(
                F'''Checksums didn\'t match{for_verification_name}:\n'''
                F'''{bad_urls}\n'''
                'Set `verification_mode=\'no_checks\'` to skip checksums verification and ignore this error'							)
      logger.info('All the checksums matched successfully' + for_verification_name							)





class     lowercase_	(					__lowerCAmelCase							):



 '''simple docstring'''





class     lowercase_	(					__lowerCAmelCase							):



 '''simple docstring'''





class     lowercase_	(					__lowerCAmelCase							):



 '''simple docstring'''





class     lowercase_	(					__lowerCAmelCase							):



 '''simple docstring'''






def 						_snake_case					( _snake_case							: Optional[dict]    ,  _snake_case							: dict							)   -> List[str]:






      '''simple docstring'''


      if expected_splits is None:
            logger.info('Unable to verify splits sizes.'							)
            return
      if len(set(_snake_case							) - set(_snake_case							)							) > 0:
            raise ExpectedMoreSplits(str(set(_snake_case							) - set(_snake_case							)							)							)
      if len(set(_snake_case							) - set(_snake_case							)							) > 0:
            raise UnexpectedSplits(str(set(_snake_case							) - set(_snake_case							)							)							)
      _A        =       [
          {'expected': expected_splits[name], 'recorded': recorded_splits[name]}
          for name in expected_splits
          if expected_splits[name].num_examples != recorded_splits[name].num_examples
      ]
      if len(_snake_case							) > 0:
            raise NonMatchingSplitsSizesError(str(_snake_case							)							)
      logger.info('All the splits matched successfully.'							)






def 						_snake_case					( _snake_case							: str    ,  _snake_case							: bool = True							)   -> dict:






      '''simple docstring'''


      if record_checksum:
            _A        =       shaaaa()
            with open(_snake_case    ,  'rb'							) as f:
                  for chunk in iter(lambda: f.read(1 << 20							)    ,  B''							):
                        m.update(_snake_case							)
                  _A        =       m.hexdigest()
      else:
            _A        =       None
      return {"num_bytes": os.path.getsize(_snake_case							), "checksum": checksum}







def 						_snake_case					( _snake_case							: int							)   -> int:






      '''simple docstring'''


      if dataset_size and config.IN_MEMORY_MAX_SIZE:
            return dataset_size < config.IN_MEMORY_MAX_SIZE
      else:
            return False

