




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


_snake_case										=       logging.get_logger(__name__)

# General docstring
_snake_case										=       "RegNetConfig"

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

# Image classification docstring
_snake_case										=       "facebook/regnet-y-040"
_snake_case										=       "tabby, tabby cat"

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







class 						lowercase							(   nn.Module		):

     def __init__(  self		,     _a		,     _a		,     _a = 3		,     _a = 1		,     _a = 1		,     _a = "relu"		,     )						->							Any:
           super().__init__()
           _A							:     Dict			=						nn.Convad(
               __SCREAMING_SNAKE_CASE		,     __SCREAMING_SNAKE_CASE		,     kernel_size=__SCREAMING_SNAKE_CASE		,     stride=__SCREAMING_SNAKE_CASE		,     padding=kernel_size // 2		,     groups=__SCREAMING_SNAKE_CASE		,     bias=__SCREAMING_SNAKE_CASE		,     )
           _A							:     int			=						nn.BatchNormad(__SCREAMING_SNAKE_CASE				)
           _A							:     List[Any]			=						ACTaFN[activation] if activation is not None else nn.Identity()



     def 					a__	(  self		,     _a				)						->							Any:
           _A							:     Tuple			=						self.convolution(__SCREAMING_SNAKE_CASE				)
           _A							:     Dict			=						self.normalization(__SCREAMING_SNAKE_CASE				)
           _A							:     Optional[Any]			=						self.activation(__SCREAMING_SNAKE_CASE				)
           return hidden_state







class 						lowercase							(   nn.Module		):

     def __init__(  self		,     _a				)						->							Any:
           super().__init__()
           _A							:     Dict			=						RegNetConvLayer(
               config.num_channels		,     config.embedding_size		,     kernel_size=3		,     stride=2		,     activation=config.hidden_act				)
           _A							:     Union[str, Any]			=						config.num_channels



     def 					a__	(  self		,     _a				)						->							Union[str, Any]:
           _A							:     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."""				)
           _A							:     Any			=						self.embedder(__SCREAMING_SNAKE_CASE				)
           return hidden_state







class 						lowercase							(   nn.Module		):

     def __init__(  self		,     _a		,     _a		,     _a = 2				)						->							List[Any]:
           super().__init__()
           _A							:     Optional[Any]			=						nn.Convad(__SCREAMING_SNAKE_CASE		,     __SCREAMING_SNAKE_CASE		,     kernel_size=1		,     stride=__SCREAMING_SNAKE_CASE		,     bias=__SCREAMING_SNAKE_CASE				)
           _A							:     List[str]			=						nn.BatchNormad(__SCREAMING_SNAKE_CASE				)



     def 					a__	(  self		,     _a				)						->							List[str]:
           _A							:     str			=						self.convolution(__SCREAMING_SNAKE_CASE				)
           _A							:     List[Any]			=						self.normalization(__SCREAMING_SNAKE_CASE				)
           return hidden_state







class 						lowercase							(   nn.Module		):

     def __init__(  self		,     _a		,     _a				)						->							Optional[int]:
           super().__init__()

           _A							:     Any			=						nn.AdaptiveAvgPoolad((1, 1)				)
           _A							:     Tuple			=						nn.Sequential(
               nn.Convad(__SCREAMING_SNAKE_CASE		,     __SCREAMING_SNAKE_CASE		,     kernel_size=1				)		,     nn.ReLU()		,     nn.Convad(__SCREAMING_SNAKE_CASE		,     __SCREAMING_SNAKE_CASE		,     kernel_size=1				)		,     nn.Sigmoid()		,     )



     def 					a__	(  self		,     _a				)						->							Optional[int]:
           _A							:     Optional[Any]			=						self.pooler(__SCREAMING_SNAKE_CASE				)
           _A							:     Optional[Any]			=						self.attention(__SCREAMING_SNAKE_CASE				)
           _A							:     Tuple			=						hidden_state * attention
           return hidden_state







class 						lowercase							(   nn.Module		):

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



     def 					a__	(  self		,     _a				)						->							Tuple:
           _A							:     Dict			=						hidden_state
           _A							:     int			=						self.layer(__SCREAMING_SNAKE_CASE				)
           _A							:     Optional[int]			=						self.shortcut(__SCREAMING_SNAKE_CASE				)
           hidden_state += residual
           _A							:     str			=						self.activation(__SCREAMING_SNAKE_CASE				)
           return hidden_state







class 						lowercase							(   nn.Module		):

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



     def 					a__	(  self		,     _a				)						->							str:
           _A							:     Optional[int]			=						hidden_state
           _A							:     Optional[Any]			=						self.layer(__SCREAMING_SNAKE_CASE				)
           _A							:     Tuple			=						self.shortcut(__SCREAMING_SNAKE_CASE				)
           hidden_state += residual
           _A							:     Union[str, Any]			=						self.activation(__SCREAMING_SNAKE_CASE				)
           return hidden_state







class 						lowercase							(   nn.Module		):

     def __init__(  self		,     _a		,     _a		,     _a		,     _a = 2		,     _a = 2		,     )						->							List[Any]:
           super().__init__()

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

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



     def 					a__	(  self		,     _a				)						->							Optional[int]:
           _A							:     Dict			=						self.layers(__SCREAMING_SNAKE_CASE				)
           return hidden_state







class 						lowercase							(   nn.Module		):

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



     def 					a__	(  self		,     _a		,     _a = False		,     _a = True				)						->							List[str]:
           _A							:     int			=						() if output_hidden_states else None

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

                 _A							:     List[str]			=						stage_module(__SCREAMING_SNAKE_CASE				)

           if output_hidden_states:
                 _A							:     Optional[int]			=						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=__SCREAMING_SNAKE_CASE		,     hidden_states=__SCREAMING_SNAKE_CASE				)







class 						lowercase							(   lowerCamelCase_		):
     _a							 =						RegNetConfig
     _a							 =						"regnet"
     _a							 =						"pixel_values"
     _a							 =						True

     def 					a__	(  self		,     _a				)						->							Optional[Any]:





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



     def 					a__	(  self		,     _a		,     _a=False				)						->							Optional[int]:





           if isinstance(__SCREAMING_SNAKE_CASE		,     __SCREAMING_SNAKE_CASE				):
                 _A							:     Union[str, Any]			=						value


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

_snake_case										=       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_		):

     def __init__(  self		,     _a				)						->							Optional[Any]:
           super().__init__(__SCREAMING_SNAKE_CASE				)
           _A							:     Optional[int]			=						config
           _A							:     Tuple			=						RegNetEmbeddings(__SCREAMING_SNAKE_CASE				)
           _A							:     List[Any]			=						RegNetEncoder(__SCREAMING_SNAKE_CASE				)
           _A							:     Tuple			=						nn.AdaptiveAvgPoolad((1, 1)				)
           # Initialize weights and apply final processing
           self.post_init()



     @add_start_docstrings_to_model_forward(__SCREAMING_SNAKE_CASE				)
     @add_code_sample_docstrings(
         checkpoint=_CHECKPOINT_FOR_DOC		,     output_type=__SCREAMING_SNAKE_CASE		,     config_class=_CONFIG_FOR_DOC		,     modality="""vision"""		,     expected_output=_EXPECTED_OUTPUT_SHAPE		,     )
     def 					a__	(  self		,     _a		,     _a = None		,     _a = None				)						->							Any:
           _A							:     Tuple			=						(
               output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
           )
           _A							:     str			=						return_dict if return_dict is not None else self.config.use_return_dict

           _A							:     int			=						self.embedder(__SCREAMING_SNAKE_CASE				)

           _A							:     Tuple			=						self.encoder(
               __SCREAMING_SNAKE_CASE		,     output_hidden_states=__SCREAMING_SNAKE_CASE		,     return_dict=__SCREAMING_SNAKE_CASE				)

           _A							:     Optional[Any]			=						encoder_outputs[0]

           _A							:     List[Any]			=						self.pooler(__SCREAMING_SNAKE_CASE				)

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

           return BaseModelOutputWithPoolingAndNoAttention(
               last_hidden_state=__SCREAMING_SNAKE_CASE		,     pooler_output=__SCREAMING_SNAKE_CASE		,     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_		):

     def __init__(  self		,     _a				)						->							str:
           super().__init__(__SCREAMING_SNAKE_CASE				)
           _A							:     str			=						config.num_labels
           _A							:     Optional[int]			=						RegNetModel(__SCREAMING_SNAKE_CASE				)
           # classification head
           _A							:     List[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(__SCREAMING_SNAKE_CASE				)
     @add_code_sample_docstrings(
         checkpoint=_IMAGE_CLASS_CHECKPOINT		,     output_type=__SCREAMING_SNAKE_CASE		,     config_class=_CONFIG_FOR_DOC		,     expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT		,     )
     def 					a__	(  self		,     _a = None		,     _a = None		,     _a = None		,     _a = None		,     )						->							str:
           _A							:     Optional[int]			=						return_dict if return_dict is not None else self.config.use_return_dict

           _A							:     str			=						self.regnet(__SCREAMING_SNAKE_CASE		,     output_hidden_states=__SCREAMING_SNAKE_CASE		,     return_dict=__SCREAMING_SNAKE_CASE				)

           _A							:     Optional[int]			=						outputs.pooler_output if return_dict else outputs[1]

           _A							:     Any			=						self.classifier(__SCREAMING_SNAKE_CASE				)

           _A							:     Any			=						None

           if labels is not None:
                 if self.config.problem_type is None:
                       if self.num_labels == 1:
                             _A							:     str			=						'''regression'''
                       elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
                             _A							:     List[Any]			=						'''single_label_classification'''
                       else:
                             _A							:     Optional[int]			=						'''multi_label_classification'''
                 if self.config.problem_type == "regression":
                       _A							:     int			=						MSELoss()
                       if self.num_labels == 1:
                             _A							:     Dict			=						loss_fct(logits.squeeze()		,     labels.squeeze()				)
                       else:
                             _A							:     List[str]			=						loss_fct(__SCREAMING_SNAKE_CASE		,     __SCREAMING_SNAKE_CASE				)
                 elif self.config.problem_type == "single_label_classification":
                       _A							:     List[str]			=						CrossEntropyLoss()
                       _A							:     str			=						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							:     Optional[int]			=						loss_fct(__SCREAMING_SNAKE_CASE		,     __SCREAMING_SNAKE_CASE				)

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

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



'''simple docstring'''







from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


_lowercase :  int    							=       logging.get_logger(__name__)

_lowercase :  List[Any]    							=       {
    "shi-labs/nat-mini-in1k-224": "https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json",
    # See all Nat models at https://huggingface.co/models?filter=nat
}






class  lowerCAmelCase__   (  lowerCamelCase_    ,						lowerCamelCase_     ):
					lowerCAmelCase_        =     '''nat'''

					lowerCAmelCase_        =     {
					    '''num_attention_heads''': '''num_heads''',
					    '''num_hidden_layers''': '''num_layers''',
					}






					def __init__(   self				,							__SCREAMING_SNAKE_CASE=4				,							__SCREAMING_SNAKE_CASE=3				,							__SCREAMING_SNAKE_CASE=64				,							__SCREAMING_SNAKE_CASE=[3, 4, 6, 5]				,							__SCREAMING_SNAKE_CASE=[2, 4, 8, 16]				,							__SCREAMING_SNAKE_CASE=7				,							__SCREAMING_SNAKE_CASE=3.0				,							__SCREAMING_SNAKE_CASE=True				,							__SCREAMING_SNAKE_CASE=0.0				,							__SCREAMING_SNAKE_CASE=0.0				,							__SCREAMING_SNAKE_CASE=0.1				,							__SCREAMING_SNAKE_CASE="gelu"				,							__SCREAMING_SNAKE_CASE=0.02				,							__SCREAMING_SNAKE_CASE=1E-5				,							__SCREAMING_SNAKE_CASE=0.0				,							__SCREAMING_SNAKE_CASE=None				,							__SCREAMING_SNAKE_CASE=None				,							**__SCREAMING_SNAKE_CASE				,							):


											"""simple docstring"""





											super().__init__(**__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Optional[int]													=       patch_size
											lowercase_	:     List[Any]													=       num_channels
											lowercase_	:     str													=       embed_dim
											lowercase_	:     List[str]													=       depths
											lowercase_	:     str													=       len(__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Tuple													=       num_heads
											lowercase_	:     int													=       kernel_size
											lowercase_	:     Union[str, Any]													=       mlp_ratio
											lowercase_	:     Optional[int]													=       qkv_bias
											lowercase_	:     List[Any]													=       hidden_dropout_prob
											lowercase_	:     Optional[int]													=       attention_probs_dropout_prob
											lowercase_	:     List[Any]													=       drop_path_rate
											lowercase_	:     List[Any]													=       hidden_act
											lowercase_	:     int													=       layer_norm_eps
											lowercase_	:     int													=       initializer_range
											# we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel
											# this indicates the channel dimension after the last stage of the model
											lowercase_	:     Dict													=       int(embed_dim * 2 ** (len(__SCREAMING_SNAKE_CASE		) - 1)		)
											lowercase_	:     Tuple													=       layer_scale_init_value
											lowercase_	:     Union[str, Any]													=       ['''stem'''] + [F'''stage{idx}''' for idx in range(1				,							len(__SCREAMING_SNAKE_CASE		) + 1		)]
											lowercase_						, lowercase_	:     int													=       get_aligned_output_features_output_indices(
											    out_features=__SCREAMING_SNAKE_CASE				,							out_indices=__SCREAMING_SNAKE_CASE				,							stage_names=self.stage_names		)



