
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


__magic_name__  							=	logging.get_logger(__name__)

# General docstring
__magic_name__  							=	'''RegNetConfig'''

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

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

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




class        _SCREAMING_SNAKE_CASE	(   nn.Module			):
      def __init__(      self		,						lowerCamelCase		,						lowerCamelCase		,						lowerCamelCase = 3		,						lowerCamelCase = 1		,						lowerCamelCase = 1		,						lowerCamelCase = "relu"		,						):
          super().__init__()
          snake_case__    =					nn.Convad(
              lowerCamelCase		,						lowerCamelCase		,						kernel_size=lowerCamelCase		,						stride=lowerCamelCase		,						padding=kernel_size // 2		,						groups=lowerCamelCase		,						bias=lowerCamelCase		,						)
          snake_case__    =					nn.BatchNormad(lowerCamelCase			)
          snake_case__    =					ACTaFN[activation] if activation is not None else nn.Identity()



      def A_      (      self		,						lowerCamelCase			):
          snake_case__    =					self.convolution(lowerCamelCase			)
          snake_case__    =					self.normalization(lowerCamelCase			)
          snake_case__    =					self.activation(lowerCamelCase			)
          return hidden_state





class        _SCREAMING_SNAKE_CASE	(   nn.Module			):
      def __init__(      self		,						lowerCamelCase			):
          super().__init__()
          snake_case__    =					RegNetConvLayer(
              config.num_channels		,						config.embedding_size		,						kernel_size=3		,						stride=2		,						activation=config.hidden_act			)
          snake_case__    =					config.num_channels



      def A_      (      self		,						lowerCamelCase			):
          snake_case__    =					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."			)
          snake_case__    =					self.embedder(lowerCamelCase			)
          return hidden_state





class        _SCREAMING_SNAKE_CASE	(   nn.Module			):
      def __init__(      self		,						lowerCamelCase		,						lowerCamelCase		,						lowerCamelCase = 2			):
          super().__init__()
          snake_case__    =					nn.Convad(lowerCamelCase		,						lowerCamelCase		,						kernel_size=1		,						stride=lowerCamelCase		,						bias=lowerCamelCase			)
          snake_case__    =					nn.BatchNormad(lowerCamelCase			)



      def A_      (      self		,						lowerCamelCase			):
          snake_case__    =					self.convolution(lowerCamelCase			)
          snake_case__    =					self.normalization(lowerCamelCase			)
          return hidden_state





class        _SCREAMING_SNAKE_CASE	(   nn.Module			):
      def __init__(      self		,						lowerCamelCase		,						lowerCamelCase			):
          super().__init__()

          snake_case__    =					nn.AdaptiveAvgPoolad((1, 1)			)
          snake_case__    =					nn.Sequential(
              nn.Convad(lowerCamelCase		,						lowerCamelCase		,						kernel_size=1			)		,						nn.ReLU()		,						nn.Convad(lowerCamelCase		,						lowerCamelCase		,						kernel_size=1			)		,						nn.Sigmoid()		,						)



      def A_      (      self		,						lowerCamelCase			):
          # b c h w -> b c 1 1
          snake_case__    =					self.pooler(lowerCamelCase			)
          snake_case__    =					self.attention(lowerCamelCase			)
          snake_case__    =					hidden_state * attention
          return hidden_state





class        _SCREAMING_SNAKE_CASE	(   nn.Module			):
      def __init__(      self		,						lowerCamelCase		,						lowerCamelCase		,						lowerCamelCase		,						lowerCamelCase = 1			):
          super().__init__()
          snake_case__    =					in_channels != out_channels or stride != 1
          snake_case__    =					max(1		,						out_channels // config.groups_width			)
          snake_case__    =					(
              RegNetShortCut(lowerCamelCase		,						lowerCamelCase		,						stride=lowerCamelCase			) if should_apply_shortcut else nn.Identity()
          )
          snake_case__    =					nn.Sequential(
              RegNetConvLayer(lowerCamelCase		,						lowerCamelCase		,						kernel_size=1		,						activation=config.hidden_act			)		,						RegNetConvLayer(lowerCamelCase		,						lowerCamelCase		,						stride=lowerCamelCase		,						groups=lowerCamelCase		,						activation=config.hidden_act			)		,						RegNetConvLayer(lowerCamelCase		,						lowerCamelCase		,						kernel_size=1		,						activation=lowerCamelCase			)		,						)
          snake_case__    =					ACTaFN[config.hidden_act]



      def A_      (      self		,						lowerCamelCase			):
          snake_case__    =					hidden_state
          snake_case__    =					self.layer(lowerCamelCase			)
          snake_case__    =					self.shortcut(lowerCamelCase			)
          hidden_state += residual
          snake_case__    =					self.activation(lowerCamelCase			)
          return hidden_state





class        _SCREAMING_SNAKE_CASE	(   nn.Module			):
      def __init__(      self		,						lowerCamelCase		,						lowerCamelCase		,						lowerCamelCase		,						lowerCamelCase = 1			):
          super().__init__()
          snake_case__    =					in_channels != out_channels or stride != 1
          snake_case__    =					max(1		,						out_channels // config.groups_width			)
          snake_case__    =					(
              RegNetShortCut(lowerCamelCase		,						lowerCamelCase		,						stride=lowerCamelCase			) if should_apply_shortcut else nn.Identity()
          )
          snake_case__    =					nn.Sequential(
              RegNetConvLayer(lowerCamelCase		,						lowerCamelCase		,						kernel_size=1		,						activation=config.hidden_act			)		,						RegNetConvLayer(lowerCamelCase		,						lowerCamelCase		,						stride=lowerCamelCase		,						groups=lowerCamelCase		,						activation=config.hidden_act			)		,						RegNetSELayer(lowerCamelCase		,						reduced_channels=int(round(in_channels / 4			)			)			)		,						RegNetConvLayer(lowerCamelCase		,						lowerCamelCase		,						kernel_size=1		,						activation=lowerCamelCase			)		,						)
          snake_case__    =					ACTaFN[config.hidden_act]



      def A_      (      self		,						lowerCamelCase			):
          snake_case__    =					hidden_state
          snake_case__    =					self.layer(lowerCamelCase			)
          snake_case__    =					self.shortcut(lowerCamelCase			)
          hidden_state += residual
          snake_case__    =					self.activation(lowerCamelCase			)
          return hidden_state





class        _SCREAMING_SNAKE_CASE	(   nn.Module			):
      def __init__(      self		,						lowerCamelCase		,						lowerCamelCase		,						lowerCamelCase		,						lowerCamelCase = 2		,						lowerCamelCase = 2		,						):
          super().__init__()

          snake_case__    =					RegNetXLayer if config.layer_type == "x" else RegNetYLayer

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



      def A_      (      self		,						lowerCamelCase			):
          snake_case__    =					self.layers(lowerCamelCase			)
          return hidden_state





class        _SCREAMING_SNAKE_CASE	(   nn.Module			):
      def __init__(      self		,						lowerCamelCase			):
          super().__init__()
          snake_case__    =					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(
                  lowerCamelCase		,						config.embedding_size		,						config.hidden_sizes[0]		,						stride=2 if config.downsample_in_first_stage else 1		,						depth=config.depths[0]		,						)			)
          snake_case__    =					zip(config.hidden_sizes		,						config.hidden_sizes[1:]			)
          for (in_channels, out_channels), depth in zip(lowerCamelCase		,						config.depths[1:]			):
              self.stages.append(RegNetStage(lowerCamelCase		,						lowerCamelCase		,						lowerCamelCase		,						depth=lowerCamelCase			)			)



      def A_      (      self		,						lowerCamelCase		,						lowerCamelCase = False		,						lowerCamelCase = True			):
          snake_case__    =					() if output_hidden_states else None

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

              snake_case__    =					stage_module(lowerCamelCase			)

          if output_hidden_states:
              snake_case__    =					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=lowerCamelCase		,						hidden_states=lowerCamelCase			)





class        _SCREAMING_SNAKE_CASE	(   __UpperCamelCase			):
      _A    :		Union[str, Any]     						=  RegNetConfig
      _A    :		Optional[Any]     						=  'regnet'
      _A    :		int     						=  'pixel_values'
      _A    :		int     						=  True
      def A_      (      self		,						lowerCamelCase			):
          if isinstance(lowerCamelCase		,						nn.Convad			):
              nn.init.kaiming_normal_(module.weight		,						mode="fan_out"		,						nonlinearity="relu"			)
          elif isinstance(lowerCamelCase		,						(nn.BatchNormad, nn.GroupNorm)			):
              nn.init.constant_(module.weight		,						1			)
              nn.init.constant_(module.bias		,						0			)



      def A_      (      self		,						lowerCamelCase		,						lowerCamelCase=False			):
          if isinstance(lowerCamelCase		,						lowerCamelCase			):
              snake_case__    =					value





__magic_name__  							=	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.
'''

__magic_name__  							=	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.'							,       __UpperCamelCase							,       )
# Copied from transformers.models.resnet.modeling_resnet.ResNetModel with RESNET->REGNET,ResNet->RegNet
class        _SCREAMING_SNAKE_CASE	(   __UpperCamelCase			):
      def __init__(      self		,						lowerCamelCase			):
          super().__init__(lowerCamelCase			)
          snake_case__    =					config
          snake_case__    =					RegNetEmbeddings(lowerCamelCase			)
          snake_case__    =					RegNetEncoder(lowerCamelCase			)
          snake_case__    =					nn.AdaptiveAvgPoolad((1, 1)			)
          # Initialize weights and apply final processing
          self.post_init()



      @add_start_docstrings_to_model_forward(lowerCamelCase			)
      @add_code_sample_docstrings(
          checkpoint=_CHECKPOINT_FOR_DOC		,						output_type=lowerCamelCase		,						config_class=_CONFIG_FOR_DOC		,						modality="vision"		,						expected_output=_EXPECTED_OUTPUT_SHAPE		,						)
      def A_      (      self		,						lowerCamelCase		,						lowerCamelCase = None		,						lowerCamelCase = None			):
          snake_case__    =					(
              output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
          )
          snake_case__    =					return_dict if return_dict is not None else self.config.use_return_dict

          snake_case__    =					self.embedder(lowerCamelCase			)

          snake_case__    =					self.encoder(
              lowerCamelCase		,						output_hidden_states=lowerCamelCase		,						return_dict=lowerCamelCase			)

          snake_case__    =					encoder_outputs[0]

          snake_case__    =					self.pooler(lowerCamelCase			)

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

          return BaseModelOutputWithPoolingAndNoAttention(
              last_hidden_state=lowerCamelCase		,						pooler_output=lowerCamelCase		,						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    '							,       __UpperCamelCase							,       )
# Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet
class        _SCREAMING_SNAKE_CASE	(   __UpperCamelCase			):
      def __init__(      self		,						lowerCamelCase			):
          super().__init__(lowerCamelCase			)
          snake_case__    =					config.num_labels
          snake_case__    =					RegNetModel(lowerCamelCase			)
          # classification head
          snake_case__    =					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(lowerCamelCase			)
      @add_code_sample_docstrings(
          checkpoint=_IMAGE_CLASS_CHECKPOINT		,						output_type=lowerCamelCase		,						config_class=_CONFIG_FOR_DOC		,						expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT		,						)
      def A_      (      self		,						lowerCamelCase = None		,						lowerCamelCase = None		,						lowerCamelCase = None		,						lowerCamelCase = None		,						):
          snake_case__    =					return_dict if return_dict is not None else self.config.use_return_dict

          snake_case__    =					self.regnet(lowerCamelCase		,						output_hidden_states=lowerCamelCase		,						return_dict=lowerCamelCase			)

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

          snake_case__    =					self.classifier(lowerCamelCase			)

          snake_case__    =					None

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

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

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





from __future__ import annotations

import math





def 						SCREAMING_SNAKE_CASE__     (   __lowerCAmelCase				):

    if 1 < number < 4:
        # 2 and 3 are primes
        return True
    elif number < 2 or number % 2 == 0 or number % 3 == 0:
        # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
        return False

    # All primes number are in format of 6k +/- 1
    for i in range(5     ,		int(math.sqrt(__lowerCAmelCase				) + 1				)     ,		6				):
        if number % i == 0 or number % (i + 2) == 0:
            return False
    return True





def 						SCREAMING_SNAKE_CASE__     (   __lowerCAmelCase				):
    snake_case__    =					str(__lowerCAmelCase				)
    snake_case__    =					[n]
    for i in range(1     ,		len(__lowerCAmelCase				)				):
        list_nums.append(int(str_num[i:]				)				)
        list_nums.append(int(str_num[:-i]				)				)
    return list_nums





def 						SCREAMING_SNAKE_CASE__     (   __lowerCAmelCase				):
    if len(str(__lowerCAmelCase				)				) > 3:
        if not is_prime(int(str(__lowerCAmelCase				)[-3:]				)				) or not is_prime(int(str(__lowerCAmelCase				)[:3]				)				):
            return False
    return True





def 						SCREAMING_SNAKE_CASE__     (   __lowerCAmelCase = 11				):
    snake_case__    =					[]
    snake_case__    =					13
    while len(__lowerCAmelCase				) != count:
        if validate(__lowerCAmelCase				):
            snake_case__    =					list_truncated_nums(__lowerCAmelCase				)
            if all(is_prime(__lowerCAmelCase				) for i in list_nums				):
                list_truncated_primes.append(__lowerCAmelCase				)
        num += 2
    return list_truncated_primes





def 						SCREAMING_SNAKE_CASE__     (   ):
    return sum(compute_truncated_primes(11				)				)


if __name__ == "__main__":
     print(F'''{sum(compute_truncated_primes(11)) = }''')



