




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


_UpperCAmelCase	:						Optional[Any]			=     logging.get_logger(__name__)

# General docstring
_UpperCAmelCase	:						Union[str, Any]			=     "ResNetConfig"

# Base docstring
_UpperCAmelCase	:						str			=     "microsoft/resnet-50"
_UpperCAmelCase	:						int			=     [1, 2048, 7, 7]

# Image classification docstring
_UpperCAmelCase	:						int			=     "microsoft/resnet-50"
_UpperCAmelCase	:						Optional[Any]			=     "tiger cat"

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




class    __lowerCAmelCase       (							nn.Module):






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






       def 					SCREAMING_SNAKE_CASE    (   self:					Union[str, Any]       ,					_lowerCAmelCase:					Tensor				):
         lowercase    :List[Any]      	=						self.convolution(_lowerCAmelCase				)
         lowercase    :str      	=						self.normalization(_lowerCAmelCase				)
         lowercase    :Optional[int]      	=						self.activation(_lowerCAmelCase				)
         return hidden_state







class    __lowerCAmelCase       (							nn.Module):






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






       def 					SCREAMING_SNAKE_CASE    (   self:					Dict       ,					_lowerCAmelCase:					Tensor				):
         lowercase    :Union[str, 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."				)
         lowercase    :Optional[Any]      	=						self.embedder(_lowerCAmelCase				)
         lowercase    :Union[str, Any]      	=						self.pooler(_lowerCAmelCase				)
         return embedding







class    __lowerCAmelCase       (							nn.Module):






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






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[int]       ,					_lowerCAmelCase:					Tensor				):
         lowercase    :Any      	=						self.convolution(_lowerCAmelCase				)
         lowercase    :Union[str, Any]      	=						self.normalization(_lowerCAmelCase				)
         return hidden_state







class    __lowerCAmelCase       (							nn.Module):






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






       def 					SCREAMING_SNAKE_CASE    (   self:					List[str]       ,					_lowerCAmelCase:					Dict				):
         lowercase    :List[Any]      	=						hidden_state
         lowercase    :Union[str, Any]      	=						self.layer(_lowerCAmelCase				)
         lowercase    :Optional[Any]      	=						self.shortcut(_lowerCAmelCase				)
         hidden_state += residual
         lowercase    :List[Any]      	=						self.activation(_lowerCAmelCase				)
         return hidden_state







class    __lowerCAmelCase       (							nn.Module):






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






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]       ,					_lowerCAmelCase:					str				):
         lowercase    :int      	=						hidden_state
         lowercase    :List[str]      	=						self.layer(_lowerCAmelCase				)
         lowercase    :Union[str, Any]      	=						self.shortcut(_lowerCAmelCase				)
         hidden_state += residual
         lowercase    :List[str]      	=						self.activation(_lowerCAmelCase				)
         return hidden_state







class    __lowerCAmelCase       (							nn.Module):






       def __init__(   self:					Optional[Any]       ,					_lowerCAmelCase:					ResNetConfig       ,					_lowerCAmelCase:					int       ,					_lowerCAmelCase:					int       ,					_lowerCAmelCase:					int = 2       ,					_lowerCAmelCase:					int = 2       ,					):
         super().__init__()

         lowercase    :List[Any]      	=						ResNetBottleNeckLayer if config.layer_type == "bottleneck" else ResNetBasicLayer

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






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]       ,					_lowerCAmelCase:					Tensor				):
         lowercase    :str      	=						input
         for layer in self.layers:
           lowercase    :str      	=						layer(_lowerCAmelCase				)
         return hidden_state







class    __lowerCAmelCase       (							nn.Module):






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






       def 					SCREAMING_SNAKE_CASE    (   self:					Tuple       ,					_lowerCAmelCase:					Tensor       ,					_lowerCAmelCase:					bool = False       ,					_lowerCAmelCase:					bool = True				):
         lowercase    :Union[str, Any]      	=						() if output_hidden_states else None

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

           lowercase    :Tuple      	=						stage_module(_lowerCAmelCase				)

         if output_hidden_states:
           lowercase    :int      	=						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    __lowerCAmelCase       (							lowerCAmelCase):
       _a 					=						ResNetConfig
       _a 					=						'''resnet'''
       _a 					=						'''pixel_values'''
       _a 					=						True






       def 					SCREAMING_SNAKE_CASE    (   self:					int       ,					_lowerCAmelCase:					str				):
         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 					SCREAMING_SNAKE_CASE    (   self:					int       ,					_lowerCAmelCase:					Optional[int]       ,					_lowerCAmelCase:					Tuple=False				):
         if isinstance(_lowerCAmelCase       ,					_lowerCAmelCase				):
           lowercase    :Optional[int]      	=						value







_UpperCAmelCase	:						Tuple			=     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"

_UpperCAmelCase	:						Union[str, 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.'''      , lowerCAmelCase      , )
class    __lowerCAmelCase       (							lowerCAmelCase):






       def __init__(   self:					str       ,					_lowerCAmelCase:					List[Any]				):
         super().__init__(_lowerCAmelCase				)
         lowercase    :Dict      	=						config
         lowercase    :str      	=						ResNetEmbeddings(_lowerCAmelCase				)
         lowercase    :Optional[int]      	=						ResNetEncoder(_lowerCAmelCase				)
         lowercase    :Optional[int]      	=						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 					SCREAMING_SNAKE_CASE    (   self:					int       ,					_lowerCAmelCase:					Tensor       ,					_lowerCAmelCase:					Optional[bool] = None       ,					_lowerCAmelCase:					Optional[bool] = None				):
         lowercase    :Dict      	=						(
             output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
         )
         lowercase    :Dict      	=						return_dict if return_dict is not None else self.config.use_return_dict

         lowercase    :List[str]      	=						self.embedder(_lowerCAmelCase				)

         lowercase    :List[Any]      	=						self.encoder(
             _lowerCAmelCase       ,					output_hidden_states=_lowerCAmelCase       ,					return_dict=_lowerCAmelCase				)

         lowercase    :List[Any]      	=						encoder_outputs[0]

         lowercase    :List[Any]      	=						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(
    '''
    ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    '''      , lowerCAmelCase      , )
class    __lowerCAmelCase       (							lowerCAmelCase):






       def __init__(   self:					Any       ,					_lowerCAmelCase:					List[str]				):
         super().__init__(_lowerCAmelCase				)
         lowercase    :str      	=						config.num_labels
         lowercase    :str      	=						ResNetModel(_lowerCAmelCase				)
         # classification head
         lowercase    :str      	=						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 					SCREAMING_SNAKE_CASE    (   self:					List[str]       ,					_lowerCAmelCase:					Optional[torch.FloatTensor] = None       ,					_lowerCAmelCase:					Optional[torch.LongTensor] = None       ,					_lowerCAmelCase:					Optional[bool] = None       ,					_lowerCAmelCase:					Optional[bool] = None       ,					):
         lowercase    :Optional[Any]      	=						return_dict if return_dict is not None else self.config.use_return_dict

         lowercase    :List[Any]      	=						self.resnet(_lowerCAmelCase       ,					output_hidden_states=_lowerCAmelCase       ,					return_dict=_lowerCAmelCase				)

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

         lowercase    :Union[str, Any]      	=						self.classifier(_lowerCAmelCase				)

         lowercase    :List[str]      	=						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    :str      	=						"single_label_classification"
             else:
               lowercase    :Any      	=						"multi_label_classification"
           if self.config.problem_type == "regression":
             lowercase    :Tuple      	=						MSELoss()
             if self.num_labels == 1:
               lowercase    :str      	=						loss_fct(logits.squeeze()       ,					labels.squeeze()				)
             else:
               lowercase    :List[str]      	=						loss_fct(_lowerCAmelCase       ,					_lowerCAmelCase				)
           elif self.config.problem_type == "single_label_classification":
             lowercase    :int      	=						CrossEntropyLoss()
             lowercase    :int      	=						loss_fct(logits.view(-1       ,					self.num_labels				)       ,					labels.view(-1				)				)
           elif self.config.problem_type == "multi_label_classification":
             lowercase    :int      	=						BCEWithLogitsLoss()
             lowercase    :Tuple      	=						loss_fct(_lowerCAmelCase       ,					_lowerCAmelCase				)

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

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







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






       def __init__(   self:					Union[str, Any]       ,					_lowerCAmelCase:					Dict				):
         super().__init__(_lowerCAmelCase				)
         super()._init_backbone(_lowerCAmelCase				)

         lowercase    :Dict      	=						[config.embedding_size] + config.hidden_sizes
         lowercase    :List[str]      	=						ResNetEmbeddings(_lowerCAmelCase				)
         lowercase    :str      	=						ResNetEncoder(_lowerCAmelCase				)

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






       @add_start_docstrings_to_model_forward(_lowerCAmelCase				)
       @replace_return_docstrings(output_type=_lowerCAmelCase       ,					config_class=_CONFIG_FOR_DOC				)
       def 					SCREAMING_SNAKE_CASE    (   self:					Dict       ,					_lowerCAmelCase:					Tensor       ,					_lowerCAmelCase:					Optional[bool] = None       ,					_lowerCAmelCase:					Optional[bool] = None				):
         lowercase    :Optional[Any]      	=						return_dict if return_dict is not None else self.config.use_return_dict
         lowercase    :List[Any]      	=						(
             output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
         )

         lowercase    :List[Any]      	=						self.embedder(_lowerCAmelCase				)

         lowercase    :Tuple      	=						self.encoder(_lowerCAmelCase       ,					output_hidden_states=_lowerCAmelCase       ,					return_dict=_lowerCAmelCase				)

         lowercase    :List[Any]      	=						outputs.hidden_states

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

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

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









def       UpperCAmelCase__  (     lowerCamelCase,			lowerCamelCase   ):
  if discount_rate < 0:
    raise ValueError("Discount rate cannot be negative"   )
  if not cash_flows:
    raise ValueError("Cash flows list cannot be empty"   )
  lowercase    :Dict      	=						sum(
      cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(lowerCamelCase   )   )
  return round(lowerCamelCase,			ndigits=2   )


if __name__ == "__main__":
     import doctest

     doctest.testmod()



