





'''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 ...modeling_outputs import (
    BackboneOutput,
    BaseModelOutputWithNoAttention,
    BaseModelOutputWithPoolingAndNoAttention,
    ImageClassifierOutputWithNoAttention,
)
from ...modeling_utils import PreTrainedModel
from ...utils import (
    add_code_sample_docstrings,
    add_start_docstrings,
    add_start_docstrings_to_model_forward,
    logging,
    replace_return_docstrings,
)
from ...utils.backbone_utils import BackboneMixin
from .configuration_resnet import ResNetConfig


__lowerCamelCase : Optional[int]											=			logging.get_logger(__name__)

# General docstring
__lowerCamelCase : List[str]											=			"ResNetConfig"

# Base docstring
__lowerCamelCase : Any											=			"microsoft/resnet-50"
__lowerCamelCase : Optional[int]											=			[1, 2048, 7, 7]

# Image classification docstring
__lowerCamelCase : List[Any]											=			"microsoft/resnet-50"
__lowerCamelCase : Dict											=			"tiger cat"

__lowerCamelCase : Optional[int]											=			[
    "microsoft/resnet-50",
    # See all resnet models at https://huggingface.co/models?filter=resnet
]

class       UpperCAmelCase (						nn.Module  ):




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





		def 				UpperCAmelCase__   (self      :	Tuple							,			A__      :	Tensor				)					-> Tensor:
						lowercase         = self.convolution(A__				)
						lowercase         = self.normalization(A__				)
						lowercase         = self.activation(A__				)
						return hidden_state




class       UpperCAmelCase (						nn.Module  ):




		def __init__(self      :	Optional[Any]							,			A__      :	ResNetConfig				)					-> List[Any]:
						super().__init__()
						lowercase         = ResNetConvLayer(
						    config.num_channels							,			config.embedding_size							,			kernel_size=7							,			stride=2							,			activation=config.hidden_act				)
						lowercase         = nn.MaxPoolad(kernel_size=3							,			stride=2							,			padding=1				)
						lowercase         = config.num_channels





		def 				UpperCAmelCase__   (self      :	Tuple							,			A__      :	Tensor				)					-> Tensor:
						lowercase         = 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         = self.embedder(A__				)
						lowercase         = self.pooler(A__				)
						return embedding




class       UpperCAmelCase (						nn.Module  ):




		def __init__(self      :	Optional[int]							,			A__      :	int							,			A__      :	int							,			A__      :	int = 2				)					-> List[Any]:
						super().__init__()
						lowercase         = nn.Convad(A__							,			A__							,			kernel_size=1							,			stride=A__							,			bias=A__				)
						lowercase         = nn.BatchNormad(A__				)





		def 				UpperCAmelCase__   (self      :	Union[str, Any]							,			A__      :	Tensor				)					-> Tensor:
						lowercase         = self.convolution(A__				)
						lowercase         = self.normalization(A__				)
						return hidden_state




class       UpperCAmelCase (						nn.Module  ):




		def __init__(self      :	str							,			A__      :	int							,			A__      :	int							,			A__      :	int = 1							,			A__      :	str = "relu"				)					-> Dict:
						super().__init__()
						lowercase         = in_channels != out_channels or stride != 1
						lowercase         = (
						    ResNetShortCut(A__							,			A__							,			stride=A__				) if should_apply_shortcut else nn.Identity()
						)
						lowercase         = nn.Sequential(
						    ResNetConvLayer(A__							,			A__							,			stride=A__				)							,			ResNetConvLayer(A__							,			A__							,			activation=A__				)							,			)
						lowercase         = ACTaFN[activation]





		def 				UpperCAmelCase__   (self      :	Tuple							,			A__      :	List[str]				)					-> Dict:
						lowercase         = hidden_state
						lowercase         = self.layer(A__				)
						lowercase         = self.shortcut(A__				)
						hidden_state += residual
						lowercase         = self.activation(A__				)
						return hidden_state




class       UpperCAmelCase (						nn.Module  ):




		def __init__(self      :	Optional[int]							,			A__      :	int							,			A__      :	int							,			A__      :	int = 1							,			A__      :	str = "relu"							,			A__      :	int = 4				)					-> Dict:
						super().__init__()
						lowercase         = in_channels != out_channels or stride != 1
						lowercase         = out_channels // reduction
						lowercase         = (
						    ResNetShortCut(A__							,			A__							,			stride=A__				) if should_apply_shortcut else nn.Identity()
						)
						lowercase         = nn.Sequential(
						    ResNetConvLayer(A__							,			A__							,			kernel_size=1				)							,			ResNetConvLayer(A__							,			A__							,			stride=A__				)							,			ResNetConvLayer(A__							,			A__							,			kernel_size=1							,			activation=A__				)							,			)
						lowercase         = ACTaFN[activation]





		def 				UpperCAmelCase__   (self      :	Tuple							,			A__      :	Optional[Any]				)					-> str:
						lowercase         = hidden_state
						lowercase         = self.layer(A__				)
						lowercase         = self.shortcut(A__				)
						hidden_state += residual
						lowercase         = self.activation(A__				)
						return hidden_state




class       UpperCAmelCase (						nn.Module  ):




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

						lowercase         = ResNetBottleNeckLayer if config.layer_type == "bottleneck" else ResNetBasicLayer

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





		def 				UpperCAmelCase__   (self      :	Tuple							,			A__      :	Tensor				)					-> Tensor:
						lowercase         = input
						for layer in self.layers:
										lowercase         = layer(A__				)
						return hidden_state




class       UpperCAmelCase (						nn.Module  ):




		def __init__(self      :	Tuple							,			A__      :	ResNetConfig				)					-> List[Any]:
						super().__init__()
						lowercase         = 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(
						    ResNetStage(
						        A__							,			config.embedding_size							,			config.hidden_sizes[0]							,			stride=2 if config.downsample_in_first_stage else 1							,			depth=config.depths[0]							,			)				)
						lowercase         = zip(config.hidden_sizes							,			config.hidden_sizes[1:]				)
						for (in_channels, out_channels), depth in zip(A__							,			config.depths[1:]				):
										self.stages.append(ResNetStage(A__							,			A__							,			A__							,			depth=A__				)				)





		def 				UpperCAmelCase__   (self      :	Optional[Any]							,			A__      :	Tensor							,			A__      :	bool = False							,			A__      :	bool = True				)					-> BaseModelOutputWithNoAttention:
						lowercase         = () if output_hidden_states else None

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

										lowercase         = stage_module(A__				)

						if output_hidden_states:
										lowercase         = 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=A__							,			hidden_states=A__							,			)




class       UpperCAmelCase (						_lowercase  ):
		UpperCAmelCase					:	Dict 			=		ResNetConfig
		UpperCAmelCase					:	List[Any] 			=		'''resnet'''
		UpperCAmelCase					:	List[str] 			=		'''pixel_values'''
		UpperCAmelCase					:	int 			=		True




		def 				UpperCAmelCase__   (self      :	List[str]							,			A__      :	Optional[int]				)					-> Tuple:
						if isinstance(A__							,			nn.Convad				):
										nn.init.kaiming_normal_(module.weight							,			mode="fan_out"							,			nonlinearity="relu"				)
						elif isinstance(A__							,			(nn.BatchNormad, nn.GroupNorm)				):
										nn.init.constant_(module.weight							,			1				)
										nn.init.constant_(module.bias							,			0				)





		def 				UpperCAmelCase__   (self      :	Union[str, Any]							,			A__      :	Optional[Any]							,			A__      :	Any=False				)					-> Optional[int]:
						if isinstance(A__							,			A__				):
										lowercase         = value




__lowerCamelCase : Dict											=			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 ([`ResNetConfig`]): 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 : Optional[Any]											=			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 [`~utils.ModelOutput`] instead of a plain tuple.\n"

@add_start_docstrings(
    '''The bare ResNet model outputting raw features without any specific head on top.'''			,       _lowercase			,       )
class       UpperCAmelCase (						_lowercase  ):




		def __init__(self      :	int							,			A__      :	Tuple				)					-> int:
						super().__init__(A__				)
						lowercase         = config
						lowercase         = ResNetEmbeddings(A__				)
						lowercase         = ResNetEncoder(A__				)
						lowercase         = nn.AdaptiveAvgPoolad((1, 1)				)
						# Initialize weights and apply final processing
						self.post_init()





		@add_start_docstrings_to_model_forward(A__				)
		@add_code_sample_docstrings(
		    checkpoint=_CHECKPOINT_FOR_DOC							,			output_type=A__							,			config_class=_CONFIG_FOR_DOC							,			modality="vision"							,			expected_output=_EXPECTED_OUTPUT_SHAPE							,			)
		def 				UpperCAmelCase__   (self      :	Optional[int]							,			A__      :	Tensor							,			A__      :	Optional[bool] = None							,			A__      :	Optional[bool] = None				)					-> BaseModelOutputWithPoolingAndNoAttention:
						lowercase         = (
						    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
						)
						lowercase         = return_dict if return_dict is not None else self.config.use_return_dict

						lowercase         = self.embedder(A__				)

						lowercase         = self.encoder(
						    A__							,			output_hidden_states=A__							,			return_dict=A__				)

						lowercase         = encoder_outputs[0]

						lowercase         = self.pooler(A__				)

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

						return BaseModelOutputWithPoolingAndNoAttention(
						    last_hidden_state=A__							,			pooler_output=A__							,			hidden_states=encoder_outputs.hidden_states							,			)




@add_start_docstrings(
    '''
    ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    '''			,       _lowercase			,       )
class       UpperCAmelCase (						_lowercase  ):




		def __init__(self      :	Any							,			A__      :	Any				)					-> List[Any]:
						super().__init__(A__				)
						lowercase         = config.num_labels
						lowercase         = ResNetModel(A__				)
						# classification head
						lowercase         = 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(A__				)
		@add_code_sample_docstrings(
		    checkpoint=_IMAGE_CLASS_CHECKPOINT							,			output_type=A__							,			config_class=_CONFIG_FOR_DOC							,			expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT							,			)
		def 				UpperCAmelCase__   (self      :	int							,			A__      :	Optional[torch.FloatTensor] = None							,			A__      :	Optional[torch.LongTensor] = None							,			A__      :	Optional[bool] = None							,			A__      :	Optional[bool] = None							,			)					-> ImageClassifierOutputWithNoAttention:
						lowercase         = return_dict if return_dict is not None else self.config.use_return_dict

						lowercase         = self.resnet(A__							,			output_hidden_states=A__							,			return_dict=A__				)

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

						lowercase         = self.classifier(A__				)

						lowercase         = None

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

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

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




@add_start_docstrings(
    '''
    ResNet backbone, to be used with frameworks like DETR and MaskFormer.
    '''			,       _lowercase			,       )
class       UpperCAmelCase (						_lowercase			,       _lowercase  ):




		def __init__(self      :	Tuple							,			A__      :	int				)					-> str:
						super().__init__(A__				)
						super()._init_backbone(A__				)

						lowercase         = [config.embedding_size] + config.hidden_sizes
						lowercase         = ResNetEmbeddings(A__				)
						lowercase         = ResNetEncoder(A__				)

						# initialize weights and apply final processing
						self.post_init()





		@add_start_docstrings_to_model_forward(A__				)
		@replace_return_docstrings(output_type=A__							,			config_class=_CONFIG_FOR_DOC				)
		def 				UpperCAmelCase__   (self      :	Optional[Any]							,			A__      :	Tensor							,			A__      :	Optional[bool] = None							,			A__      :	Optional[bool] = None				)					-> BackboneOutput:
						lowercase         = return_dict if return_dict is not None else self.config.use_return_dict
						lowercase         = (
						    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
						)

						lowercase         = self.embedder(A__				)

						lowercase         = self.encoder(A__							,			output_hidden_states=A__							,			return_dict=A__				)

						lowercase         = outputs.hidden_states

						lowercase         = ()
						for idx, stage in enumerate(self.stage_names				):
										if stage in self.out_features:
														feature_maps += (hidden_states[idx],)

						if not return_dict:
										lowercase         = (feature_maps,)
										if output_hidden_states:
														output += (outputs.hidden_states,)
										return output

						return BackboneOutput(
						    feature_maps=A__							,			hidden_states=outputs.hidden_states if output_hidden_states else None							,			attentions=A__							,			)











'''simple docstring'''

# This is the module that test_patching.py uses to test patch_submodule()

import os  # noqa: this is just for tests
import os as renamed_os  # noqa: this is just for tests
from os import path  # noqa: this is just for tests
from os import path as renamed_path  # noqa: this is just for tests
from os.path import join  # noqa: this is just for tests
from os.path import join as renamed_join  # noqa: this is just for tests


__lowerCamelCase : Any											=			open  # noqa: we just need to have a builtin inside this module to test it properly




