

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


snake_case_			  = logging.get_logger(__name__)

# General docstring
snake_case_			  = '''ResNetConfig'''

# Base docstring
snake_case_			  = '''microsoft/resnet-50'''
snake_case_			  = [1, 2_048, 7, 7]

# Image classification docstring
snake_case_			  = '''microsoft/resnet-50'''
snake_case_			  = '''tiger cat'''

snake_case_			  = [
    '''microsoft/resnet-50''',
    # See all resnet models at https://huggingface.co/models?filter=resnet
]


class       SCREAMING_SNAKE_CASE__					(nn.Module     ):





    def __init__(      self						,   a						,   a						,   a = 3						,   a = 1						,   a = "relu"):
         super().__init__()
         lowercase__    :   List[Any]      =       nn.Convad(
             a						,   a						,   kernel_size=a						,   stride=a						,   padding=kernel_size // 2						,   bias=a)
         lowercase__    :   Tuple      =       nn.BatchNormad(a)
         lowercase__    :   Dict      =       ACTaFN[activation] if activation is not None else nn.Identity()





    def       snake_case_				(      self						,   a):
         lowercase__    :   Any      =       self.convolution(a)
         lowercase__    :   Union[str, Any]      =       self.normalization(a)
         lowercase__    :   Optional[Any]      =       self.activation(a)
         return hidden_state



class       SCREAMING_SNAKE_CASE__					(nn.Module     ):





    def __init__(      self						,   a):
         super().__init__()
         lowercase__    :   str      =       ResNetConvLayer(
             config.num_channels						,   config.embedding_size						,   kernel_size=7						,   stride=2						,   activation=config.hidden_act)
         lowercase__    :   Optional[Any]      =       nn.MaxPoolad(kernel_size=3						,   stride=2						,   padding=1)
         lowercase__    :   str      =       config.num_channels





    def       snake_case_				(      self						,   a):
         lowercase__    :   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.')
         lowercase__    :   Dict      =       self.embedder(a)
         lowercase__    :   Optional[int]      =       self.pooler(a)
         return embedding



class       SCREAMING_SNAKE_CASE__					(nn.Module     ):





    def __init__(      self						,   a						,   a						,   a = 2):
         super().__init__()
         lowercase__    :   Any      =       nn.Convad(a						,   a						,   kernel_size=1						,   stride=a						,   bias=a)
         lowercase__    :   str      =       nn.BatchNormad(a)





    def       snake_case_				(      self						,   a):
         lowercase__    :   Union[str, Any]      =       self.convolution(a)
         lowercase__    :   Union[str, Any]      =       self.normalization(a)
         return hidden_state



class       SCREAMING_SNAKE_CASE__					(nn.Module     ):





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





    def       snake_case_				(      self						,   a):
         lowercase__    :   Optional[int]      =       hidden_state
         lowercase__    :   Any      =       self.layer(a)
         lowercase__    :   List[str]      =       self.shortcut(a)
         hidden_state += residual
         lowercase__    :   Dict      =       self.activation(a)
         return hidden_state



class       SCREAMING_SNAKE_CASE__					(nn.Module     ):





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





    def       snake_case_				(      self						,   a):
         lowercase__    :   int      =       hidden_state
         lowercase__    :   Tuple      =       self.layer(a)
         lowercase__    :   Dict      =       self.shortcut(a)
         hidden_state += residual
         lowercase__    :   List[Any]      =       self.activation(a)
         return hidden_state



class       SCREAMING_SNAKE_CASE__					(nn.Module     ):





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

         lowercase__    :   int      =       ResNetBottleNeckLayer if config.layer_type == 'bottleneck' else ResNetBasicLayer

         lowercase__    :   Dict      =       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       snake_case_				(      self						,   a):
         lowercase__    :   Union[str, Any]      =       input
         for layer in self.layers:
              lowercase__    :   List[str]      =       layer(a)
         return hidden_state



class       SCREAMING_SNAKE_CASE__					(nn.Module     ):





    def __init__(      self						,   a):
         super().__init__()
         lowercase__    :   Optional[int]      =       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__    :   str      =       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       snake_case_				(      self						,   a						,   a = False						,   a = True):
         lowercase__    :   Optional[Any]      =       () if output_hidden_states else None

         for stage_module in self.stages:
              if output_hidden_states:
                   lowercase__    :   Optional[int]      =       hidden_states + (hidden_state,)

              lowercase__    :   List[Any]      =       stage_module(a)

         if output_hidden_states:
              lowercase__    :   Tuple      =       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       SCREAMING_SNAKE_CASE__					(__snake_case     ):
    __lowerCamelCase						:       Union[str, Any]														=     ResNetConfig
    __lowerCamelCase						:       Tuple														=     """resnet"""
    __lowerCamelCase						:       Union[str, Any]														=     """pixel_values"""
    __lowerCamelCase						:       int														=     True





    def       snake_case_				(      self						,   a):
         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       snake_case_				(      self						,   a						,   a=False):
         if isinstance(a						,   a):
              lowercase__    :   Optional[Any]      =       value



snake_case_			  = 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 ([`ResNetConfig`]): 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.
'''

snake_case_			  = 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 [`~utils.ModelOutput`] instead of a plain tuple.
'''


@add_start_docstrings(
    """The bare ResNet model outputting raw features without any specific head on top."""		,       __snake_case		,       )
class       SCREAMING_SNAKE_CASE__					(__snake_case     ):





    def __init__(      self						,   a):
         super().__init__(a)
         lowercase__    :   Tuple      =       config
         lowercase__    :   Dict      =       ResNetEmbeddings(a)
         lowercase__    :   Any      =       ResNetEncoder(a)
         lowercase__    :   Any      =       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       snake_case_				(      self						,   a						,   a = None						,   a = None):
         lowercase__    :   Dict      =       (
             output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
         )
         lowercase__    :   Union[str, Any]      =       return_dict if return_dict is not None else self.config.use_return_dict

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

         lowercase__    :   Any      =       self.encoder(
             a						,   output_hidden_states=a						,   return_dict=a)

         lowercase__    :   List[Any]      =       encoder_outputs[0]

         lowercase__    :   Any      =       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.
    """		,       __snake_case		,       )
class       SCREAMING_SNAKE_CASE__					(__snake_case     ):





    def __init__(      self						,   a):
         super().__init__(a)
         lowercase__    :   Optional[int]      =       config.num_labels
         lowercase__    :   Union[str, Any]      =       ResNetModel(a)
         # classification head
         lowercase__    :   List[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(a)
    @add_code_sample_docstrings(
        checkpoint=_IMAGE_CLASS_CHECKPOINT						,   output_type=a						,   config_class=_CONFIG_FOR_DOC						,   expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT						,   )
    def       snake_case_				(      self						,   a = None						,   a = None						,   a = None						,   a = None						,   ):
         lowercase__    :   Optional[Any]      =       return_dict if return_dict is not None else self.config.use_return_dict

         lowercase__    :   Any      =       self.resnet(a						,   output_hidden_states=a						,   return_dict=a)

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

         lowercase__    :   Optional[Any]      =       self.classifier(a)

         lowercase__    :   str      =       None

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

         if not return_dict:
              lowercase__    :   Union[str, Any]      =       (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.
    """		,       __snake_case		,       )
class       SCREAMING_SNAKE_CASE__					(__snake_case		,       __snake_case     ):





    def __init__(      self						,   a):
         super().__init__(a)
         super()._init_backbone(a)

         lowercase__    :   int      =       [config.embedding_size] + config.hidden_sizes
         lowercase__    :   Optional[int]      =       ResNetEmbeddings(a)
         lowercase__    :   List[Any]      =       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       snake_case_				(      self						,   a						,   a = None						,   a = None):
         lowercase__    :   Optional[Any]      =       return_dict if return_dict is not None else self.config.use_return_dict
         lowercase__    :   Optional[Any]      =       (
             output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
         )

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

         lowercase__    :   Optional[int]      =       self.encoder(a						,   output_hidden_states=a						,   return_dict=a)

         lowercase__    :   str      =       outputs.hidden_states

         lowercase__    :   Union[str, 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__    :   int      =       (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						,   )




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


snake_case_			  = {
    '''configuration_instructblip''': [
        '''INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''InstructBlipConfig''',
        '''InstructBlipQFormerConfig''',
        '''InstructBlipVisionConfig''',
    ],
    '''processing_instructblip''': ['''InstructBlipProcessor'''],
}

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       snake_case_			  = [
           '''INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST''',
           '''InstructBlipQFormerModel''',
           '''InstructBlipPreTrainedModel''',
           '''InstructBlipForConditionalGeneration''',
           '''InstructBlipVisionModel''',
       ]

if TYPE_CHECKING:
       from .configuration_instructblip import (
           INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
           InstructBlipConfig,
           InstructBlipQFormerConfig,
           InstructBlipVisionConfig,
       )
       from .processing_instructblip import InstructBlipProcessor

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_instructblip import (
                  INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
                  InstructBlipForConditionalGeneration,
                  InstructBlipPreTrainedModel,
                  InstructBlipQFormerModel,
                  InstructBlipVisionModel,
              )

else:
       import sys

       snake_case_			  = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)

