





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


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

# General docstring
lowerCAmelCase :					Tuple 							='''RegNetConfig'''

# Base docstring
lowerCAmelCase :					Any 							='''facebook/regnet-y-040'''
lowerCAmelCase :					Dict 							=[1, 1_088, 7, 7]

# Image classification docstring
lowerCAmelCase :					Tuple 							='''facebook/regnet-y-040'''
lowerCAmelCase :					Dict 							='''tabby, tabby cat'''

lowerCAmelCase :					Optional[int] 							=[
    '''facebook/regnet-y-040''',
    # See all regnet models at https://huggingface.co/models?filter=regnet
]
class 				a_       (    nn.Module							):





     def __init__(			self		: Optional[Any]     ,				lowercase		: int     ,				lowercase		: int     ,				lowercase		: int = 3     ,				lowercase		: int = 1     ,				lowercase		: int = 1     ,				lowercase		: Optional[str] = "relu"     ,				):





        """simple docstring"""




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







     def 							lowercase__				(			self		: Union[str, Any]     ,				lowercase		: Optional[int]						):





        """simple docstring"""




        lowercase_      :str         =			self.convolution(lowercase						)
        lowercase_      :Union[str, Any]         =			self.normalization(lowercase						)
        lowercase_      :List[str]         =			self.activation(lowercase						)
        return hidden_state







class 				a_       (    nn.Module							):





     def __init__(			self		: Tuple     ,				lowercase		: RegNetConfig						):





        """simple docstring"""




        super().__init__()
        lowercase_      :Dict         =			RegNetConvLayer(
            config.num_channels     ,				config.embedding_size     ,				kernel_size=3     ,				stride=2     ,				activation=config.hidden_act						)
        lowercase_      :Dict         =			config.num_channels







     def 							lowercase__				(			self		: Union[str, Any]     ,				lowercase		: Any						):





        """simple docstring"""




        lowercase_      :List[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."						)
        lowercase_      :Optional[int]         =			self.embedder(lowercase						)
        return hidden_state







class 				a_       (    nn.Module							):





     def __init__(			self		: Union[str, Any]     ,				lowercase		: int     ,				lowercase		: int     ,				lowercase		: int = 2						):





        """simple docstring"""




        super().__init__()
        lowercase_      :Optional[int]         =			nn.Convad(lowercase     ,				lowercase     ,				kernel_size=1     ,				stride=lowercase     ,				bias=lowercase						)
        lowercase_      :str         =			nn.BatchNormad(lowercase						)







     def 							lowercase__				(			self		: List[str]     ,				lowercase		: Tensor						):





        """simple docstring"""




        lowercase_      :Optional[int]         =			self.convolution(lowercase						)
        lowercase_      :Union[str, Any]         =			self.normalization(lowercase						)
        return hidden_state







class 				a_       (    nn.Module							):





     def __init__(			self		: List[Any]     ,				lowercase		: int     ,				lowercase		: int						):





        """simple docstring"""




        super().__init__()

        lowercase_      :Tuple         =			nn.AdaptiveAvgPoolad((1, 1)						)
        lowercase_      :str         =			nn.Sequential(
            nn.Convad(lowercase     ,				lowercase     ,				kernel_size=1						)     ,				nn.ReLU()     ,				nn.Convad(lowercase     ,				lowercase     ,				kernel_size=1						)     ,				nn.Sigmoid()     ,				)







     def 							lowercase__				(			self		: Any     ,				lowercase		: Union[str, Any]						):





        """simple docstring"""




        lowercase_      :Any         =			self.pooler(lowercase						)
        lowercase_      :Optional[Any]         =			self.attention(lowercase						)
        lowercase_      :Union[str, Any]         =			hidden_state * attention
        return hidden_state







class 				a_       (    nn.Module							):





     def __init__(			self		: int     ,				lowercase		: RegNetConfig     ,				lowercase		: int     ,				lowercase		: int     ,				lowercase		: int = 1						):





        """simple docstring"""




        super().__init__()
        lowercase_      :List[str]         =			in_channels != out_channels or stride != 1
        lowercase_      :Dict         =			max(1     ,				out_channels // config.groups_width						)
        lowercase_      :List[Any]         =			(
            RegNetShortCut(lowercase     ,				lowercase     ,				stride=lowercase						) if should_apply_shortcut else nn.Identity()
        )
        lowercase_      :Optional[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						)     ,				)
        lowercase_      :int         =			ACTaFN[config.hidden_act]







     def 							lowercase__				(			self		: Optional[int]     ,				lowercase		: str						):





        """simple docstring"""




        lowercase_      :Union[str, Any]         =			hidden_state
        lowercase_      :Dict         =			self.layer(lowercase						)
        lowercase_      :Tuple         =			self.shortcut(lowercase						)
        hidden_state += residual
        lowercase_      :List[str]         =			self.activation(lowercase						)
        return hidden_state







class 				a_       (    nn.Module							):





     def __init__(			self		: str     ,				lowercase		: RegNetConfig     ,				lowercase		: int     ,				lowercase		: int     ,				lowercase		: int = 1						):





        """simple docstring"""




        super().__init__()
        lowercase_      :List[Any]         =			in_channels != out_channels or stride != 1
        lowercase_      :Union[str, Any]         =			max(1     ,				out_channels // config.groups_width						)
        lowercase_      :List[str]         =			(
            RegNetShortCut(lowercase     ,				lowercase     ,				stride=lowercase						) if should_apply_shortcut else nn.Identity()
        )
        lowercase_      :Tuple         =			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						)     ,				)
        lowercase_      :Optional[int]         =			ACTaFN[config.hidden_act]







     def 							lowercase__				(			self		: List[str]     ,				lowercase		: int						):





        """simple docstring"""




        lowercase_      :Optional[Any]         =			hidden_state
        lowercase_      :Union[str, Any]         =			self.layer(lowercase						)
        lowercase_      :Tuple         =			self.shortcut(lowercase						)
        hidden_state += residual
        lowercase_      :List[Any]         =			self.activation(lowercase						)
        return hidden_state







class 				a_       (    nn.Module							):





     def __init__(			self		: Union[str, Any]     ,				lowercase		: RegNetConfig     ,				lowercase		: int     ,				lowercase		: int     ,				lowercase		: int = 2     ,				lowercase		: int = 2     ,				):





        """simple docstring"""




        super().__init__()

        lowercase_      :Tuple         =			RegNetXLayer if config.layer_type == "x" else RegNetYLayer

        lowercase_      :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 							lowercase__				(			self		: int     ,				lowercase		: List[str]						):





        """simple docstring"""




        lowercase_      :Dict         =			self.layers(lowercase						)
        return hidden_state







class 				a_       (    nn.Module							):





     def __init__(			self		: Union[str, Any]     ,				lowercase		: RegNetConfig						):





        """simple docstring"""




        super().__init__()
        lowercase_      :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]     ,				)						)
        lowercase_      :Dict         =			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 							lowercase__				(			self		: Tuple     ,				lowercase		: Tensor     ,				lowercase		: bool = False     ,				lowercase		: bool = True						):





        """simple docstring"""




        lowercase_      :Dict         =			() if output_hidden_states else None

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

           lowercase_      :List[str]         =			stage_module(lowercase						)

        if output_hidden_states:
           lowercase_      :List[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=lowercase     ,				hidden_states=lowercase						)







class 				a_       (    _lowerCAmelCase							):
     __A								=      RegNetConfig
     __A								=      "regnet"
     __A								=      "pixel_values"
     __A								=      True





     def 							lowercase__				(			self		: str     ,				lowercase		: Any						):





        """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 							lowercase__				(			self		: Optional[int]     ,				lowercase		: Dict     ,				lowercase		: List[Any]=False						):





        """simple docstring"""




        if isinstance(lowercase     ,				lowercase						):
           lowercase_      :List[Any]         =			value







lowerCAmelCase :					Optional[int] 							=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.
'''

lowerCAmelCase :					Optional[int] 							=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 				a_       (    _lowerCAmelCase							):





     def __init__(			self		: Tuple     ,				lowercase		: Dict						):





        """simple docstring"""




        super().__init__(lowercase						)
        lowercase_      :Any         =			config
        lowercase_      :Union[str, Any]         =			RegNetEmbeddings(lowercase						)
        lowercase_      :str         =			RegNetEncoder(lowercase						)
        lowercase_      :Tuple         =			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 							lowercase__				(			self		: Any     ,				lowercase		: Tensor     ,				lowercase		: Optional[bool] = None     ,				lowercase		: Optional[bool] = None						):





        """simple docstring"""




        lowercase_      :Union[str, Any]         =			(
            output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        )
        lowercase_      :Tuple         =			return_dict if return_dict is not None else self.config.use_return_dict

        lowercase_      :List[str]         =			self.embedder(lowercase						)

        lowercase_      :Optional[int]         =			self.encoder(
            lowercase     ,				output_hidden_states=lowercase     ,				return_dict=lowercase						)

        lowercase_      :str         =			encoder_outputs[0]

        lowercase_      :str         =			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(
    "\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 				a_       (    _lowerCAmelCase							):





     def __init__(			self		: List[str]     ,				lowercase		: Optional[int]						):





        """simple docstring"""




        super().__init__(lowercase						)
        lowercase_      :Tuple         =			config.num_labels
        lowercase_      :List[Any]         =			RegNetModel(lowercase						)
        # classification head
        lowercase_      :Dict         =			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 							lowercase__				(			self		: Any     ,				lowercase		: Optional[torch.FloatTensor] = None     ,				lowercase		: Optional[torch.LongTensor] = None     ,				lowercase		: Optional[bool] = None     ,				lowercase		: Optional[bool] = None     ,				):





        """simple docstring"""




        lowercase_      :Dict         =			return_dict if return_dict is not None else self.config.use_return_dict

        lowercase_      :Union[str, Any]         =			self.regnet(lowercase     ,				output_hidden_states=lowercase     ,				return_dict=lowercase						)

        lowercase_      :Dict         =			outputs.pooler_output if return_dict else outputs[1]

        lowercase_      :Optional[int]         =			self.classifier(lowercase						)

        lowercase_      :Tuple         =			None

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

        if not return_dict:
           lowercase_      :Any         =			(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'''


def 							UpperCAmelCase_  (						__lowerCamelCase : dict			):
   lowercase_      :set[int]         =			set()
   # To detect a back edge, keep track of vertices currently in the recursion stack
   lowercase_      :set[int]         =			set()
   return any(
       node not in visited and depth_first_search(__lowerCamelCase     ,__lowerCamelCase     ,__lowerCamelCase     ,__lowerCamelCase			)
       for node in graph			)
def 							UpperCAmelCase_  (						__lowerCamelCase : dict     ,__lowerCamelCase : int     ,__lowerCamelCase : set     ,__lowerCamelCase : set			):
   visited.add(__lowerCamelCase			)
   rec_stk.add(__lowerCamelCase			)

   for node in graph[vertex]:
      if node not in visited:
         if depth_first_search(__lowerCamelCase     ,__lowerCamelCase     ,__lowerCamelCase     ,__lowerCamelCase			):
            return True
      elif node in rec_stk:
         return True

    # The node needs to be removed from recursion stack before function ends
   rec_stk.remove(__lowerCamelCase			)
   return False


if __name__ == "__main__":
 from doctest import testmod

 testmod()


