






"""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			:    Union[str, Any]				 =		logging.get_logger(__name__)

# General docstring
a			:    Tuple				 =		"""RegNetConfig"""

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

# Image classification docstring
a			:    int				 =		"""facebook/regnet-y-040"""
a			:    Optional[int]				 =		"""tabby, tabby cat"""

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


class    __UpperCAmelCase(       nn.Module							):




						"""simple docstring"""




						def __init__(       self	,  snake_case__	,  snake_case__	,  snake_case__ = 3	,  snake_case__ = 1	,  snake_case__ = 1	,  snake_case__ = "relu"	,  ):
												'''simple docstring'''
												super().__init__()
												lowercase__    :				Optional[int]=	nn.Convad(
												    snake_case__	,  snake_case__	,  kernel_size=snake_case__	,  stride=snake_case__	,  padding=kernel_size // 2	,  groups=snake_case__	,  bias=snake_case__	,  )
												lowercase__    :				Optional[Any]=	nn.BatchNormad(snake_case__      )
												lowercase__    :				List[str]=	ACTaFN[activation] if activation is not None else nn.Identity()




						def 		UpperCAmelCase_	(       self	,  snake_case__      ):
												'''simple docstring'''
												lowercase__    :				List[Any]=	self.convolution(snake_case__      )
												lowercase__    :				str=	self.normalization(snake_case__      )
												lowercase__    :				Dict=	self.activation(snake_case__      )
												return hidden_state


class    __UpperCAmelCase(       nn.Module							):




						"""simple docstring"""




						def __init__(       self	,  snake_case__      ):
												'''simple docstring'''
												super().__init__()
												lowercase__    :				Union[str, Any]=	RegNetConvLayer(
												    config.num_channels	,  config.embedding_size	,  kernel_size=3	,  stride=2	,  activation=config.hidden_act      )
												lowercase__    :				Optional[Any]=	config.num_channels




						def 		UpperCAmelCase_	(       self	,  snake_case__      ):
												'''simple docstring'''
												lowercase__    :				int=	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__    :				List[str]=	self.embedder(snake_case__      )
												return hidden_state


class    __UpperCAmelCase(       nn.Module							):




						"""simple docstring"""




						def __init__(       self	,  snake_case__	,  snake_case__	,  snake_case__ = 2      ):
												'''simple docstring'''
												super().__init__()
												lowercase__    :				Optional[int]=	nn.Convad(snake_case__	,  snake_case__	,  kernel_size=1	,  stride=snake_case__	,  bias=snake_case__      )
												lowercase__    :				Dict=	nn.BatchNormad(snake_case__      )




						def 		UpperCAmelCase_	(       self	,  snake_case__      ):
												'''simple docstring'''
												lowercase__    :				Optional[int]=	self.convolution(snake_case__      )
												lowercase__    :				List[str]=	self.normalization(snake_case__      )
												return hidden_state


class    __UpperCAmelCase(       nn.Module							):




						"""simple docstring"""




						def __init__(       self	,  snake_case__	,  snake_case__      ):
												'''simple docstring'''
												super().__init__()

												lowercase__    :				Any=	nn.AdaptiveAvgPoolad((1, 1)      )
												lowercase__    :				Optional[int]=	nn.Sequential(
												    nn.Convad(snake_case__	,  snake_case__	,  kernel_size=1      )	,  nn.ReLU()	,  nn.Convad(snake_case__	,  snake_case__	,  kernel_size=1      )	,  nn.Sigmoid()	,  )




						def 		UpperCAmelCase_	(       self	,  snake_case__      ):
												'''simple docstring'''
												# b c h w -> b c 1 1
												lowercase__    :				Any=	self.pooler(snake_case__      )
												lowercase__    :				Optional[Any]=	self.attention(snake_case__      )
												lowercase__    :				Dict=	hidden_state * attention
												return hidden_state


class    __UpperCAmelCase(       nn.Module							):




						"""simple docstring"""




						def __init__(       self	,  snake_case__	,  snake_case__	,  snake_case__	,  snake_case__ = 1      ):
												'''simple docstring'''
												super().__init__()
												lowercase__    :				Union[str, Any]=	in_channels != out_channels or stride != 1
												lowercase__    :				Dict=	max(1	,  out_channels // config.groups_width      )
												lowercase__    :				Optional[int]=	(
												    RegNetShortCut(snake_case__	,  snake_case__	,  stride=snake_case__      ) if should_apply_shortcut else nn.Identity()
												)
												lowercase__    :				Optional[Any]=	nn.Sequential(
												    RegNetConvLayer(snake_case__	,  snake_case__	,  kernel_size=1	,  activation=config.hidden_act      )	,  RegNetConvLayer(snake_case__	,  snake_case__	,  stride=snake_case__	,  groups=snake_case__	,  activation=config.hidden_act      )	,  RegNetConvLayer(snake_case__	,  snake_case__	,  kernel_size=1	,  activation=snake_case__      )	,  )
												lowercase__    :				List[str]=	ACTaFN[config.hidden_act]




						def 		UpperCAmelCase_	(       self	,  snake_case__      ):
												'''simple docstring'''
												lowercase__    :				Union[str, Any]=	hidden_state
												lowercase__    :				Tuple=	self.layer(snake_case__      )
												lowercase__    :				List[str]=	self.shortcut(snake_case__      )
												hidden_state += residual
												lowercase__    :				Union[str, Any]=	self.activation(snake_case__      )
												return hidden_state


class    __UpperCAmelCase(       nn.Module							):




						"""simple docstring"""




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




						def 		UpperCAmelCase_	(       self	,  snake_case__      ):
												'''simple docstring'''
												lowercase__    :				Any=	hidden_state
												lowercase__    :				List[str]=	self.layer(snake_case__      )
												lowercase__    :				Dict=	self.shortcut(snake_case__      )
												hidden_state += residual
												lowercase__    :				Tuple=	self.activation(snake_case__      )
												return hidden_state


class    __UpperCAmelCase(       nn.Module							):




						"""simple docstring"""




						def __init__(       self	,  snake_case__	,  snake_case__	,  snake_case__	,  snake_case__ = 2	,  snake_case__ = 2	,  ):
												'''simple docstring'''
												super().__init__()

												lowercase__    :				Any=	RegNetXLayer if config.layer_type == "x" else RegNetYLayer

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




						def 		UpperCAmelCase_	(       self	,  snake_case__      ):
												'''simple docstring'''
												lowercase__    :				Optional[Any]=	self.layers(snake_case__      )
												return hidden_state


class    __UpperCAmelCase(       nn.Module							):




						"""simple docstring"""




						def __init__(       self	,  snake_case__      ):
												'''simple docstring'''
												super().__init__()
												lowercase__    :				Dict=	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(
												        snake_case__	,  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(snake_case__	,  config.depths[1:]      ):
																		self.stages.append(RegNetStage(snake_case__	,  snake_case__	,  snake_case__	,  depth=snake_case__      )      )




						def 		UpperCAmelCase_	(       self	,  snake_case__	,  snake_case__ = False	,  snake_case__ = True      ):
												'''simple docstring'''
												lowercase__    :				int=	() if output_hidden_states else None

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

																		lowercase__    :				int=	stage_module(snake_case__      )

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


class    __UpperCAmelCase(       SCREAMING_SNAKE_CASE__							):




						"""simple docstring"""




						__lowerCamelCase    	= RegNetConfig
						__lowerCamelCase    	= "regnet"
						__lowerCamelCase    	= "pixel_values"
						__lowerCamelCase    	= True




						def 		UpperCAmelCase_	(       self	,  snake_case__      ):
												'''simple docstring'''
												if isinstance(snake_case__	,  nn.Convad      ):
																		nn.init.kaiming_normal_(module.weight	,  mode="fan_out"	,  nonlinearity="relu"      )
												elif isinstance(snake_case__	,  (nn.BatchNormad, nn.GroupNorm)      ):
																		nn.init.constant_(module.weight	,  1      )
																		nn.init.constant_(module.bias	,  0      )




						def 		UpperCAmelCase_	(       self	,  snake_case__	,  snake_case__=False      ):
												'''simple docstring'''
												if isinstance(snake_case__	,  snake_case__      ):
																		lowercase__    :				Optional[Any]=	value


a			:    Optional[Any]				 =		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.
"""

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




						"""simple docstring"""




						def __init__(       self	,  snake_case__      ):
												'''simple docstring'''
												super().__init__(snake_case__      )
												lowercase__    :				int=	config
												lowercase__    :				Optional[Any]=	RegNetEmbeddings(snake_case__      )
												lowercase__    :				Optional[Any]=	RegNetEncoder(snake_case__      )
												lowercase__    :				Optional[int]=	nn.AdaptiveAvgPoolad((1, 1)      )
												# Initialize weights and apply final processing
												self.post_init()




						@add_start_docstrings_to_model_forward(snake_case__      )
						@add_code_sample_docstrings(
						    checkpoint=_CHECKPOINT_FOR_DOC	,  output_type=snake_case__	,  config_class=_CONFIG_FOR_DOC	,  modality="vision"	,  expected_output=_EXPECTED_OUTPUT_SHAPE	,  )
						def 		UpperCAmelCase_	(       self	,  snake_case__	,  snake_case__ = None	,  snake_case__ = None      ):
												'''simple docstring'''
												lowercase__    :				str=	(
												    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
												)
												lowercase__    :				Optional[int]=	return_dict if return_dict is not None else self.config.use_return_dict

												lowercase__    :				Optional[Any]=	self.embedder(snake_case__      )

												lowercase__    :				List[str]=	self.encoder(
												    snake_case__	,  output_hidden_states=snake_case__	,  return_dict=snake_case__      )

												lowercase__    :				Tuple=	encoder_outputs[0]

												lowercase__    :				str=	self.pooler(snake_case__      )

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

												return BaseModelOutputWithPoolingAndNoAttention(
												    last_hidden_state=snake_case__	,  pooler_output=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    "  ,  SCREAMING_SNAKE_CASE__  ,  )
# Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet
class    __UpperCAmelCase(       SCREAMING_SNAKE_CASE__							):




						"""simple docstring"""




						def __init__(       self	,  snake_case__      ):
												'''simple docstring'''
												super().__init__(snake_case__      )
												lowercase__    :				Optional[int]=	config.num_labels
												lowercase__    :				Any=	RegNetModel(snake_case__      )
												# classification head
												lowercase__    :				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(snake_case__      )
						@add_code_sample_docstrings(
						    checkpoint=_IMAGE_CLASS_CHECKPOINT	,  output_type=snake_case__	,  config_class=_CONFIG_FOR_DOC	,  expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT	,  )
						def 		UpperCAmelCase_	(       self	,  snake_case__ = None	,  snake_case__ = None	,  snake_case__ = None	,  snake_case__ = None	,  ):
												'''simple docstring'''
												lowercase__    :				Dict=	return_dict if return_dict is not None else self.config.use_return_dict

												lowercase__    :				List[Any]=	self.regnet(snake_case__	,  output_hidden_states=snake_case__	,  return_dict=snake_case__      )

												lowercase__    :				Optional[Any]=	outputs.pooler_output if return_dict else outputs[1]

												lowercase__    :				Tuple=	self.classifier(snake_case__      )

												lowercase__    :				Tuple=	None

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

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

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














"""simple docstring"""







from __future__ import annotations

from collections.abc import Iterator
from typing import Any


class    __UpperCAmelCase:




						"""simple docstring"""




						def __init__(       self	,  snake_case__      ):
												'''simple docstring'''
												lowercase__    :				Any=	data
												lowercase__    :				Node | None=	None


class    __UpperCAmelCase:




						"""simple docstring"""




						def __init__(       self      ):
												'''simple docstring'''
												lowercase__    :				List[str]=	None
												lowercase__    :				int=	None




						def __iter__(       self      ):
												'''simple docstring'''
												lowercase__    :				Any=	self.head
												while self.head:
																		yield node.data
																		lowercase__    :				str=	node.next
																		if node == self.head:
																								break




						def __len__(       self      ):
												'''simple docstring'''
												return sum(1 for _ in self      )




						def __repr__(       self      ):
												'''simple docstring'''
												return "->".join(str(snake_case__      ) for item in iter(self      )      )




						def 		UpperCAmelCase_	(       self	,  snake_case__      ):
												'''simple docstring'''
												self.insert_nth(len(self      )	,  snake_case__      )




						def 		UpperCAmelCase_	(       self	,  snake_case__      ):
												'''simple docstring'''
												self.insert_nth(0	,  snake_case__      )




						def 		UpperCAmelCase_	(       self	,  snake_case__	,  snake_case__      ):
												'''simple docstring'''
												if index < 0 or index > len(self      ):
																		raise IndexError("list index out of range."      )
												lowercase__    :				Optional[Any]=	Node(snake_case__      )
												if self.head is None:
																		lowercase__    :				Tuple=	new_node  # first node points itself
																		lowercase__    :				str=	new_node
												elif index == 0:  # insert at head
																		lowercase__    :				Dict=	self.head
																		lowercase__    :				int=	new_node
												else:
																		lowercase__    :				Dict=	self.head
																		for _ in range(index - 1      ):
																								lowercase__    :				Any=	temp.next
																		lowercase__    :				str=	temp.next
																		lowercase__    :				Dict=	new_node
																		if index == len(self      ) - 1:  # insert at tail
																								lowercase__    :				int=	new_node




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												return self.delete_nth(0      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												return self.delete_nth(len(self      ) - 1      )




						def 		UpperCAmelCase_	(       self	,  snake_case__ = 0      ):
												'''simple docstring'''
												if not 0 <= index < len(self      ):
																		raise IndexError("list index out of range."      )
												lowercase__    :				int=	self.head
												if self.head == self.tail:  # just one node
																		lowercase__    :				Union[str, Any]=	None
												elif index == 0:  # delete head node
																		lowercase__    :				Tuple=	self.tail.next.next
																		lowercase__    :				Union[str, Any]=	self.head.next
												else:
																		lowercase__    :				Union[str, Any]=	self.head
																		for _ in range(index - 1      ):
																								lowercase__    :				Dict=	temp.next
																		lowercase__    :				List[str]=	temp.next
																		lowercase__    :				Optional[Any]=	temp.next.next
																		if index == len(self      ) - 1:  # delete at tail
																								lowercase__    :				Optional[Any]=	temp
												return delete_node.data




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												return len(self      ) == 0





def 						lowercase__()    ->None:




						"""simple docstring"""



						lowercase__    :				Dict=	CircularLinkedList()
						assert len(A						) == 0
						assert circular_linked_list.is_empty() is True
						assert str(A						) == ""

						try:
												circular_linked_list.delete_front()
												raise AssertionError  # This should not happen
						except IndexError:
												assert True  # This should happen

						try:
												circular_linked_list.delete_tail()
												raise AssertionError  # This should not happen
						except IndexError:
												assert True  # This should happen

						try:
												circular_linked_list.delete_nth(-1						)
												raise AssertionError
						except IndexError:
												assert True

						try:
												circular_linked_list.delete_nth(0						)
												raise AssertionError
						except IndexError:
												assert True

						assert circular_linked_list.is_empty() is True
						for i in range(5						):
												assert len(A						) == i
												circular_linked_list.insert_nth(A ,		i + 1						)
						assert str(A						) == "->".join(str(A						) for i in range(1 ,		6						)						)

						circular_linked_list.insert_tail(6						)
						assert str(A						) == "->".join(str(A						) for i in range(1 ,		7						)						)
						circular_linked_list.insert_head(0						)
						assert str(A						) == "->".join(str(A						) for i in range(0 ,		7						)						)

						assert circular_linked_list.delete_front() == 0
						assert circular_linked_list.delete_tail() == 6
						assert str(A						) == "->".join(str(A						) for i in range(1 ,		6						)						)
						assert circular_linked_list.delete_nth(2						) == 3

						circular_linked_list.insert_nth(2 ,		3						)
						assert str(A						) == "->".join(str(A						) for i in range(1 ,		6						)						)

						assert circular_linked_list.is_empty() is False


if __name__ == "__main__":
			import doctest

			doctest.testmod()






