






"""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       =				logging.get_logger(__name__)

# General docstring
_lowerCAmelCase       =				"RegNetConfig"

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

# Image classification docstring
_lowerCAmelCase       =				"facebook/regnet-y-040"
_lowerCAmelCase       =				"tabby, tabby cat"

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



class   SCREAMING_SNAKE_CASE	(					nn.Module    ):





	"""simple docstring"""




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





	def  lowerCamelCase(self  ,  lowerCAmelCase_				):
		A_		:    List[str]						     = self.convolution(lowerCAmelCase_				)
		A_		:    List[Any]						     = self.normalization(lowerCAmelCase_				)
		A_		:    Tuple						     = self.activation(lowerCAmelCase_				)
		return hidden_state






class   SCREAMING_SNAKE_CASE	(					nn.Module    ):





	"""simple docstring"""




	def __init__(self  ,  lowerCAmelCase_				):
		super().__init__()
		A_		:    List[str]						     = RegNetConvLayer(
		    config.num_channels  ,  config.embedding_size  ,  kernel_size=3  ,  stride=2  ,  activation=config.hidden_act				)
		A_		:    str						     = config.num_channels





	def  lowerCamelCase(self  ,  lowerCAmelCase_				):
		A_		:    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."""				)
		A_		:    Optional[Any]						     = self.embedder(lowerCAmelCase_				)
		return hidden_state






class   SCREAMING_SNAKE_CASE	(					nn.Module    ):





	"""simple docstring"""




	def __init__(self  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  lowerCAmelCase_ = 2				):
		super().__init__()
		A_		:    int						     = nn.Convad(lowerCAmelCase_  ,  lowerCAmelCase_  ,  kernel_size=1  ,  stride=lowerCAmelCase_  ,  bias=lowerCAmelCase_				)
		A_		:    List[str]						     = nn.BatchNormad(lowerCAmelCase_				)





	def  lowerCamelCase(self  ,  lowerCAmelCase_				):
		A_		:    List[str]						     = self.convolution(lowerCAmelCase_				)
		A_		:    Any						     = self.normalization(lowerCAmelCase_				)
		return hidden_state






class   SCREAMING_SNAKE_CASE	(					nn.Module    ):





	"""simple docstring"""




	def __init__(self  ,  lowerCAmelCase_  ,  lowerCAmelCase_				):
		super().__init__()

		A_		:    List[Any]						     = nn.AdaptiveAvgPoolad((1, 1)				)
		A_		:    List[str]						     = nn.Sequential(
		    nn.Convad(lowerCAmelCase_  ,  lowerCAmelCase_  ,  kernel_size=1				)  ,  nn.ReLU()  ,  nn.Convad(lowerCAmelCase_  ,  lowerCAmelCase_  ,  kernel_size=1				)  ,  nn.Sigmoid()  ,  )





	def  lowerCamelCase(self  ,  lowerCAmelCase_				):
		# b c h w -> b c 1 1
		A_		:    Any						     = self.pooler(lowerCAmelCase_				)
		A_		:    Dict						     = self.attention(lowerCAmelCase_				)
		A_		:    Optional[int]						     = hidden_state * attention
		return hidden_state






class   SCREAMING_SNAKE_CASE	(					nn.Module    ):





	"""simple docstring"""




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





	def  lowerCamelCase(self  ,  lowerCAmelCase_				):
		A_		:    Any						     = hidden_state
		A_		:    Any						     = self.layer(lowerCAmelCase_				)
		A_		:    int						     = self.shortcut(lowerCAmelCase_				)
		hidden_state += residual
		A_		:    List[str]						     = self.activation(lowerCAmelCase_				)
		return hidden_state






class   SCREAMING_SNAKE_CASE	(					nn.Module    ):





	"""simple docstring"""




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





	def  lowerCamelCase(self  ,  lowerCAmelCase_				):
		A_		:    Union[str, Any]						     = hidden_state
		A_		:    Any						     = self.layer(lowerCAmelCase_				)
		A_		:    List[str]						     = self.shortcut(lowerCAmelCase_				)
		hidden_state += residual
		A_		:    Any						     = self.activation(lowerCAmelCase_				)
		return hidden_state






class   SCREAMING_SNAKE_CASE	(					nn.Module    ):





	"""simple docstring"""




	def __init__(self  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  lowerCAmelCase_ = 2  ,  lowerCAmelCase_ = 2  ,  ):
		super().__init__()

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

		A_		:    int						     = 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  lowerCamelCase(self  ,  lowerCAmelCase_				):
		A_		:    int						     = self.layers(lowerCAmelCase_				)
		return hidden_state






class   SCREAMING_SNAKE_CASE	(					nn.Module    ):





	"""simple docstring"""




	def __init__(self  ,  lowerCAmelCase_				):
		super().__init__()
		A_		:    List[str]						     = 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]  ,  )				)
		A_		:    Optional[Any]						     = 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  lowerCamelCase(self  ,  lowerCAmelCase_  ,  lowerCAmelCase_ = False  ,  lowerCAmelCase_ = True				):
		A_		:    Optional[Any]						     = () if output_hidden_states else None

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

			A_		:    List[Any]						     = stage_module(lowerCAmelCase_				)

		if output_hidden_states:
			A_		:    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=lowerCAmelCase_  ,  hidden_states=lowerCAmelCase_				)






class   SCREAMING_SNAKE_CASE	(					_SCREAMING_SNAKE_CASE    ):





	"""simple docstring"""




	_A							:					Dict 			=					RegNetConfig
	_A							:					int 			=					"""regnet"""
	_A							:					Dict 			=					"""pixel_values"""
	_A							:					Union[str, Any] 			=					True





	def  lowerCamelCase(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  lowerCamelCase(self  ,  lowerCAmelCase_  ,  lowerCAmelCase_=False				):
		if isinstance(lowerCAmelCase_  ,  lowerCAmelCase_				):
			A_		:    Dict						     = value






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

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





	"""simple docstring"""




	def __init__(self  ,  lowerCAmelCase_				):
		super().__init__(lowerCAmelCase_				)
		A_		:    Any						     = config
		A_		:    Union[str, Any]						     = RegNetEmbeddings(lowerCAmelCase_				)
		A_		:    Optional[int]						     = RegNetEncoder(lowerCAmelCase_				)
		A_		:    List[str]						     = 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  lowerCamelCase(self  ,  lowerCAmelCase_  ,  lowerCAmelCase_ = None  ,  lowerCAmelCase_ = None				):
		A_		:    Any						     = (
		    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
		)
		A_		:    Any						     = return_dict if return_dict is not None else self.config.use_return_dict

		A_		:    Union[str, Any]						     = self.embedder(lowerCAmelCase_				)

		A_		:    Any						     = self.encoder(
		    lowerCAmelCase_  ,  output_hidden_states=lowerCAmelCase_  ,  return_dict=lowerCAmelCase_				)

		A_		:    Dict						     = encoder_outputs[0]

		A_		:    List[str]						     = 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(
    """
    RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    """							,							_SCREAMING_SNAKE_CASE							,							)
# Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet
class   SCREAMING_SNAKE_CASE	(					_SCREAMING_SNAKE_CASE    ):





	"""simple docstring"""




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

		A_		:    Union[str, Any]						     = self.regnet(lowerCAmelCase_  ,  output_hidden_states=lowerCAmelCase_  ,  return_dict=lowerCAmelCase_				)

		A_		:    Optional[int]						     = outputs.pooler_output if return_dict else outputs[1]

		A_		:    List[str]						     = self.classifier(lowerCAmelCase_				)

		A_		:    Union[str, Any]						     = None

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

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

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









"""simple docstring"""
class   SCREAMING_SNAKE_CASE	:





	"""simple docstring"""




	def __init__(self  ,  lowerCAmelCase_  ,  lowerCAmelCase_=None  ,  lowerCAmelCase_=None				):
		A_		:    Dict						     = data
		A_		:    Dict						     = previous
		A_		:    int						     = next_node





	def __str__(self				):
		return f"""{self.data}"""





	def  lowerCamelCase(self				):
		return self.data





	def  lowerCamelCase(self				):
		return self.next





	def  lowerCamelCase(self				):
		return self.previous






class   SCREAMING_SNAKE_CASE	:





	"""simple docstring"""




	def __init__(self  ,  lowerCAmelCase_				):
		A_		:    Optional[Any]						     = head





	def __iter__(self				):
		return self





	def  lowerCamelCase(self				):
		if not self.current:
			raise StopIteration
		else:
			A_		:    str						     = self.current.get_data()
			A_		:    List[str]						     = self.current.get_next()
			return value






class   SCREAMING_SNAKE_CASE	:





	"""simple docstring"""




	def __init__(self				):
		A_		:    str						     = None  # First node in list
		A_		:    Tuple						     = None  # Last node in list





	def __str__(self				):
		A_		:    int						     = self.head
		A_		:    str						     = []
		while current is not None:
			nodes.append(current.get_data()				)
			A_		:    Any						     = current.get_next()
		return " ".join(str(lowerCAmelCase_				) for node in nodes				)





	def __contains__(self  ,  lowerCAmelCase_				):
		A_		:    Union[str, Any]						     = self.head
		while current:
			if current.get_data() == value:
				return True
			A_		:    Union[str, Any]						     = current.get_next()
		return False





	def __iter__(self				):
		return LinkedListIterator(self.head				)





	def  lowerCamelCase(self				):
		if self.head:
			return self.head.get_data()
		return None





	def  lowerCamelCase(self				):
		if self.tail:
			return self.tail.get_data()
		return None





	def  lowerCamelCase(self  ,  lowerCAmelCase_				):
		if self.head is None:
			A_		:    str						     = node
			A_		:    Optional[int]						     = node
		else:
			self.insert_before_node(self.head  ,  lowerCAmelCase_				)





	def  lowerCamelCase(self  ,  lowerCAmelCase_				):
		if self.head is None:
			self.set_head(lowerCAmelCase_				)
		else:
			self.insert_after_node(self.tail  ,  lowerCAmelCase_				)





	def  lowerCamelCase(self  ,  lowerCAmelCase_				):
		A_		:    List[Any]						     = Node(lowerCAmelCase_				)
		if self.head is None:
			self.set_head(lowerCAmelCase_				)
		else:
			self.set_tail(lowerCAmelCase_				)





	def  lowerCamelCase(self  ,  lowerCAmelCase_  ,  lowerCAmelCase_				):
		A_		:    str						     = node
		A_		:    Union[str, Any]						     = node.previous

		if node.get_previous() is None:
			A_		:    Dict						     = node_to_insert
		else:
			A_		:    Optional[Any]						     = node_to_insert

		A_		:    Any						     = node_to_insert





	def  lowerCamelCase(self  ,  lowerCAmelCase_  ,  lowerCAmelCase_				):
		A_		:    Dict						     = node
		A_		:    Optional[Any]						     = node.next

		if node.get_next() is None:
			A_		:    Optional[int]						     = node_to_insert
		else:
			A_		:    List[Any]						     = node_to_insert

		A_		:    Optional[Any]						     = node_to_insert





	def  lowerCamelCase(self  ,  lowerCAmelCase_  ,  lowerCAmelCase_				):
		A_		:    str						     = 1
		A_		:    Tuple						     = Node(lowerCAmelCase_				)
		A_		:    Dict						     = self.head
		while node:
			if current_position == position:
				self.insert_before_node(lowerCAmelCase_  ,  lowerCAmelCase_				)
				return
			current_position += 1
			A_		:    List[str]						     = node.next
		self.insert_after_node(self.tail  ,  lowerCAmelCase_				)





	def  lowerCamelCase(self  ,  lowerCAmelCase_				):
		A_		:    Dict						     = self.head
		while node:
			if node.get_data() == item:
				return node
			A_		:    Optional[int]						     = node.get_next()
		raise Exception("""Node not found"""				)





	def  lowerCamelCase(self  ,  lowerCAmelCase_				):
		if (node := self.get_node(lowerCAmelCase_				)) is not None:
			if node == self.head:
				A_		:    Tuple						     = self.head.get_next()

			if node == self.tail:
				A_		:    Dict						     = self.tail.get_previous()

			self.remove_node_pointers(lowerCAmelCase_				)





	@staticmethod
	def  lowerCamelCase(lowerCAmelCase_				):
		if node.get_next():
			A_		:    Any						     = node.previous

		if node.get_previous():
			A_		:    int						     = node.next

		A_		:    str						     = None
		A_		:    int						     = None





	def  lowerCamelCase(self				):
		return self.head is None






def       __UpperCamelCase						(		):
	pass


if __name__ == "__main__":
							import doctest

							doctest.testmod()

