
"""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_0_8_8, 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		):




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




	def       _UpperCamelCase   (			self		,			UpperCAmelCase_ )		->							int:
			lowerCamelCase					:      Optional[Any]          =   self.convolution(UpperCAmelCase_ )
			lowerCamelCase					:      Any          =   self.normalization(UpperCAmelCase_ )
			lowerCamelCase					:      Any          =   self.activation(UpperCAmelCase_ )
			return hidden_state






class 	_lowercase       (    nn.Module		):




	def __init__(			self		,			UpperCAmelCase_ )		->							Optional[Any]:
			super().__init__()
			lowerCamelCase					:      Optional[Any]          =   RegNetConvLayer(
			    config.num_channels		,			config.embedding_size		,			kernel_size=3		,			stride=2		,			activation=config.hidden_act )
			lowerCamelCase					:      Optional[int]          =   config.num_channels




	def       _UpperCamelCase   (			self		,			UpperCAmelCase_ )		->							int:
			lowerCamelCase					:      str          =   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.' )
			lowerCamelCase					:      Tuple          =   self.embedder(UpperCAmelCase_ )
			return hidden_state






class 	_lowercase       (    nn.Module		):




	def __init__(			self		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_ = 2 )		->							Union[str, Any]:
			super().__init__()
			lowerCamelCase					:      Any          =   nn.Convad(UpperCAmelCase_		,			UpperCAmelCase_		,			kernel_size=1		,			stride=UpperCAmelCase_		,			bias=UpperCAmelCase_ )
			lowerCamelCase					:      Dict          =   nn.BatchNormad(UpperCAmelCase_ )




	def       _UpperCamelCase   (			self		,			UpperCAmelCase_ )		->							Tensor:
			lowerCamelCase					:      Any          =   self.convolution(UpperCAmelCase_ )
			lowerCamelCase					:      Optional[Any]          =   self.normalization(UpperCAmelCase_ )
			return hidden_state






class 	_lowercase       (    nn.Module		):




	def __init__(			self		,			UpperCAmelCase_		,			UpperCAmelCase_ )		->							Tuple:
			super().__init__()

			lowerCamelCase					:      List[str]          =   nn.AdaptiveAvgPoolad((1, 1) )
			lowerCamelCase					:      Optional[Any]          =   nn.Sequential(
			    nn.Convad(UpperCAmelCase_		,			UpperCAmelCase_		,			kernel_size=1 )		,			nn.ReLU()		,			nn.Convad(UpperCAmelCase_		,			UpperCAmelCase_		,			kernel_size=1 )		,			nn.Sigmoid()		,			)




	def       _UpperCamelCase   (			self		,			UpperCAmelCase_ )		->							str:
			# b c h w -> b c 1 1
			lowerCamelCase					:      str          =   self.pooler(UpperCAmelCase_ )
			lowerCamelCase					:      List[Any]          =   self.attention(UpperCAmelCase_ )
			lowerCamelCase					:      Tuple          =   hidden_state * attention
			return hidden_state






class 	_lowercase       (    nn.Module		):




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




	def       _UpperCamelCase   (			self		,			UpperCAmelCase_ )		->							Dict:
			lowerCamelCase					:      int          =   hidden_state
			lowerCamelCase					:      Tuple          =   self.layer(UpperCAmelCase_ )
			lowerCamelCase					:      Any          =   self.shortcut(UpperCAmelCase_ )
			hidden_state += residual
			lowerCamelCase					:      Dict          =   self.activation(UpperCAmelCase_ )
			return hidden_state






class 	_lowercase       (    nn.Module		):




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




	def       _UpperCamelCase   (			self		,			UpperCAmelCase_ )		->							Tuple:
			lowerCamelCase					:      Optional[int]          =   hidden_state
			lowerCamelCase					:      Any          =   self.layer(UpperCAmelCase_ )
			lowerCamelCase					:      Any          =   self.shortcut(UpperCAmelCase_ )
			hidden_state += residual
			lowerCamelCase					:      str          =   self.activation(UpperCAmelCase_ )
			return hidden_state






class 	_lowercase       (    nn.Module		):




	def __init__(			self		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_ = 2		,			UpperCAmelCase_ = 2		,			)		->							Union[str, Any]:
			super().__init__()

			lowerCamelCase					:      Tuple          =   RegNetXLayer if config.layer_type == 'x' else RegNetYLayer

			lowerCamelCase					:      str          =   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       _UpperCamelCase   (			self		,			UpperCAmelCase_ )		->							Dict:
			lowerCamelCase					:      Dict          =   self.layers(UpperCAmelCase_ )
			return hidden_state






class 	_lowercase       (    nn.Module		):




	def __init__(			self		,			UpperCAmelCase_ )		->							str:
			super().__init__()
			lowerCamelCase					:      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(
			        UpperCAmelCase_		,			config.embedding_size		,			config.hidden_sizes[0]		,			stride=2 if config.downsample_in_first_stage else 1		,			depth=config.depths[0]		,			) )
			lowerCamelCase					:      Dict          =   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       _UpperCamelCase   (			self		,			UpperCAmelCase_		,			UpperCAmelCase_ = False		,			UpperCAmelCase_ = True )		->							BaseModelOutputWithNoAttention:
			lowerCamelCase					:      Optional[Any]          =   () if output_hidden_states else None

			for stage_module in self.stages:
					if output_hidden_states:
							lowerCamelCase					:      Optional[Any]          =   hidden_states + (hidden_state,)

					lowerCamelCase					:      Any          =   stage_module(UpperCAmelCase_ )

			if output_hidden_states:
					lowerCamelCase					:      Dict          =   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       (    __UpperCAmelCase		):
	lowercase_				=     RegNetConfig
	lowercase_				=     'regnet'
	lowercase_				=     'pixel_values'
	lowercase_				=     True




	def       _UpperCamelCase   (			self		,			UpperCAmelCase_ )		->							Optional[int]:
			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       _UpperCamelCase   (			self		,			UpperCAmelCase_		,			UpperCAmelCase_=False )		->							List[Any]:
			if isinstance(UpperCAmelCase_		,			UpperCAmelCase_ ):
					lowerCamelCase					:      str          =   value


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

_A						=			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.'    ,	__UpperCAmelCase    ,	)
# Copied from transformers.models.resnet.modeling_resnet.ResNetModel with RESNET->REGNET,ResNet->RegNet
class 	_lowercase       (    __UpperCAmelCase		):




	def __init__(			self		,			UpperCAmelCase_ )		->							Any:
			super().__init__(UpperCAmelCase_ )
			lowerCamelCase					:      Tuple          =   config
			lowerCamelCase					:      Dict          =   RegNetEmbeddings(UpperCAmelCase_ )
			lowerCamelCase					:      List[str]          =   RegNetEncoder(UpperCAmelCase_ )
			lowerCamelCase					:      List[Any]          =   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       _UpperCamelCase   (			self		,			UpperCAmelCase_		,			UpperCAmelCase_ = None		,			UpperCAmelCase_ = None )		->							BaseModelOutputWithPoolingAndNoAttention:
			lowerCamelCase					:      List[str]          =   (
			    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
			)
			lowerCamelCase					:      Dict          =   return_dict if return_dict is not None else self.config.use_return_dict

			lowerCamelCase					:      Tuple          =   self.embedder(UpperCAmelCase_ )

			lowerCamelCase					:      Any          =   self.encoder(
			    UpperCAmelCase_		,			output_hidden_states=UpperCAmelCase_		,			return_dict=UpperCAmelCase_ )

			lowerCamelCase					:      List[Any]          =   encoder_outputs[0]

			lowerCamelCase					:      str          =   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(
    '\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 	_lowercase       (    __UpperCAmelCase		):




	def __init__(			self		,			UpperCAmelCase_ )		->							Dict:
			super().__init__(UpperCAmelCase_ )
			lowerCamelCase					:      Tuple          =   config.num_labels
			lowerCamelCase					:      Dict          =   RegNetModel(UpperCAmelCase_ )
			# classification head
			lowerCamelCase					:      Union[str, 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(UpperCAmelCase_ )
	@add_code_sample_docstrings(
	    checkpoint=_IMAGE_CLASS_CHECKPOINT		,			output_type=UpperCAmelCase_		,			config_class=_CONFIG_FOR_DOC		,			expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT		,			)
	def       _UpperCamelCase   (			self		,			UpperCAmelCase_ = None		,			UpperCAmelCase_ = None		,			UpperCAmelCase_ = None		,			UpperCAmelCase_ = None		,			)		->							ImageClassifierOutputWithNoAttention:
			lowerCamelCase					:      Any          =   return_dict if return_dict is not None else self.config.use_return_dict

			lowerCamelCase					:      Tuple          =   self.regnet(UpperCAmelCase_		,			output_hidden_states=UpperCAmelCase_		,			return_dict=UpperCAmelCase_ )

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

			lowerCamelCase					:      int          =   self.classifier(UpperCAmelCase_ )

			lowerCamelCase					:      Dict          =   None

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

			if not return_dict:
					lowerCamelCase					:      Optional[Any]          =   (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"""

def 		UpperCAmelCase (		a_ = 10      ):





		'''simple docstring'''


		if not isinstance(a_,						a_      ) or n < 0:
				raise ValueError('Invalid input'      )
		lowerCamelCase					:      Union[str, Any]          =   10**n
		lowerCamelCase					:      int          =   2_8433 * (pow(2,						783_0457,						a_      )) + 1
		return str(number % modulus      )


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

					testmod()
					print(F"""{solution(1_0) = }""")






