





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


_snake_case     :		List[Any]       						= logging.get_logger(__name__)

# General docstring
_snake_case     :		Any       						= 'RegNetConfig'

# Base docstring
_snake_case     :		Dict       						= 'facebook/regnet-y-040'
_snake_case     :		Optional[Any]       						= [1, 1088, 7, 7]

# Image classification docstring
_snake_case     :		Any       						= 'facebook/regnet-y-040'
_snake_case     :		List[Any]       						= 'tabby, tabby cat'

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






class 					_UpperCAmelCase   (	nn.Module ):


					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			:int     ,       __UpperCamelCase			:List[Any]		):
												A		=  self.convolution(__UpperCamelCase		)
												A		=  self.normalization(__UpperCamelCase		)
												A		=  self.activation(__UpperCamelCase		)
												return hidden_state







class 					_UpperCAmelCase   (	nn.Module ):


					def __init__(					self			:Any     ,       __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			:str     ,       __UpperCamelCase			:str		):
												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 					_UpperCAmelCase   (	nn.Module ):


					def __init__(					self			:List[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			:Union[str, Any]     ,       __UpperCamelCase			:Tensor		):
												A		=  self.convolution(__UpperCamelCase		)
												A		=  self.normalization(__UpperCamelCase		)
												return hidden_state







class 					_UpperCAmelCase   (	nn.Module ):


					def __init__(					self			:Optional[int]     ,       __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			:Any     ,       __UpperCamelCase			:Optional[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 					_UpperCAmelCase   (	nn.Module ):


					def __init__(					self			:Optional[int]     ,       __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			:Union[str, Any]     ,       __UpperCamelCase			:int		):
												A		=  hidden_state
												A		=  self.layer(__UpperCamelCase		)
												A		=  self.shortcut(__UpperCamelCase		)
												hidden_state += residual
												A		=  self.activation(__UpperCamelCase		)
												return hidden_state







class 					_UpperCAmelCase   (	nn.Module ):


					def __init__(					self			: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			:Optional[Any]     ,       __UpperCamelCase			:Union[str, Any]		):
												A		=  hidden_state
												A		=  self.layer(__UpperCamelCase		)
												A		=  self.shortcut(__UpperCamelCase		)
												hidden_state += residual
												A		=  self.activation(__UpperCamelCase		)
												return hidden_state







class 					_UpperCAmelCase   (	nn.Module ):


					def __init__(					self			:Optional[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			:int     ,       __UpperCamelCase			:Tuple		):
												A		=  self.layers(__UpperCamelCase		)
												return hidden_state







class 					_UpperCAmelCase   (	nn.Module ):


					def __init__(					self			:Optional[int]     ,       __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			:Union[str, 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 					_UpperCAmelCase   (	lowercase_ ):
					UpperCamelCase      =							RegNetConfig
					UpperCamelCase      =							'''regnet'''
					UpperCamelCase      =							'''pixel_values'''
					UpperCamelCase      =							True


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







_snake_case     :		Optional[Any]       						= 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     :		Tuple       						= 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.'''     ,							lowercase_     ,							)
# Copied from transformers.models.resnet.modeling_resnet.ResNetModel with RESNET->REGNET,ResNet->RegNet
class 					_UpperCAmelCase   (	lowercase_ ):


					def __init__(					self			:Optional[Any]     ,       __UpperCamelCase			:Optional[int]		):
												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			:Tuple     ,       __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.
    '''     ,							lowercase_     ,							)
# Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet
class 					_UpperCAmelCase   (	lowercase_ ):


					def __init__(					self			:Optional[int]     ,       __UpperCamelCase			:Optional[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			:int     ,       __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"""
def 					A__				(			UpperCamelCase       ):
							A		=  generate_pascal_triangle(UpperCamelCase       )
							for row_idx in range(UpperCamelCase       ):
														# Print left spaces
														for _ in range(num_rows - row_idx - 1       ):
																					print(end=" "       )
														# Print row values
														for col_idx in range(row_idx + 1       ):
																					if col_idx != row_idx:
																												print(triangle[row_idx][col_idx]					,  end=" "       )
																					else:
																												print(triangle[row_idx][col_idx]					,  end=""       )
														print()






def 					A__				(			UpperCamelCase       ):

							if not isinstance(UpperCamelCase					,  UpperCamelCase       ):
														raise TypeError("The input value of 'num_rows' should be 'int'"       )

							if num_rows == 0:
														return []
							elif num_rows < 0:
														raise ValueError(
														    "The input value of 'num_rows' should be greater than or equal to 0"       )

							A		=  []
							for current_row_idx in range(UpperCamelCase       ):
														A		=  populate_current_row(UpperCamelCase					,  UpperCamelCase       )
														triangle.append(UpperCamelCase       )
							return triangle






def 					A__				(			UpperCamelCase					,  UpperCamelCase       ):
							A		=  [-1] * (current_row_idx + 1)
							# first and last elements of current row are equal to 1
							A,	A		=  1, 1
							for current_col_idx in range(1					,  UpperCamelCase       ):
														calculate_current_element(
														    UpperCamelCase					,  UpperCamelCase					,  UpperCamelCase					,  UpperCamelCase       )
							return current_row






def 					A__				(			UpperCamelCase					,  UpperCamelCase					,  UpperCamelCase					,  UpperCamelCase					,  ):
							A		=  triangle[current_row_idx - 1][current_col_idx - 1]
							A		=  triangle[current_row_idx - 1][current_col_idx]
							A		=  above_to_left_elt + above_to_right_elt






def 					A__				(			UpperCamelCase       ):

							if not isinstance(UpperCamelCase					,  UpperCamelCase       ):
														raise TypeError("The input value of 'num_rows' should be 'int'"       )

							if num_rows == 0:
														return []
							elif num_rows < 0:
														raise ValueError(
														    "The input value of 'num_rows' should be greater than or equal to 0"       )

							A		=  [[1]]

							for row_index in range(1					,  UpperCamelCase       ):
														A		=  [0] + result[-1] + [0]
														A		=  row_index + 1
														# Calculate the number of distinct elements in a row
														A		=  sum(divmod(UpperCamelCase					,  2       )       )
														A		=  [
														    temp_row[i - 1] + temp_row[i] for i in range(1					,  distinct_elements + 1       )
														]
														A		=  row_first_half[: (row_index + 1) // 2]
														row_second_half.reverse()
														A		=  row_first_half + row_second_half
														result.append(UpperCamelCase       )

							return result






def 					A__				(			):
							from collections.abc import Callable
							from timeit import timeit

							def benchmark_a_function(UpperCamelCase					,  UpperCamelCase       ) -> None:
														A		=  F"{func.__name__}({value})"
														A		=  timeit(F"__main__.{call}"					,  setup="import __main__"       )
														# print(f"{call:38} = {func(value)} -- {timing:.4f} seconds")
														print(F"{call:38} -- {timing:.4f} seconds"       )

							for value in range(15       ):  # (1, 7, 14):
														for func in (generate_pascal_triangle, generate_pascal_triangle_optimized):
																					benchmark_a_function(UpperCamelCase					,  UpperCamelCase       )
														print()


if __name__ == "__main__":
		import doctest

		doctest.testmod()
		benchmark()


