






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


UpperCamelCase_	:				Any										= logging.get_logger(__name__)

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

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

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

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


class      _lowercase (     nn.Module						):


      def __init__(    self				:	List[str]							,   a				:	int							,   a				:	int							,   a				:	int = 3							,   a				:	int = 1							,   a				:	str = "relu"						):





            """simple docstring"""







            super().__init__()
            __snake_case      :    str  			=nn.Convad(
                a							,   a							,   kernel_size=a							,   stride=a							,   padding=kernel_size // 2							,   bias=a						)
            __snake_case      :    int  			=nn.BatchNormad(a						)
            __snake_case      :    Optional[int]  			=ACTaFN[activation] if activation is not None else nn.Identity()







      def _UpperCamelCase						(    self				:	int							,   a				:	Tensor						):





            """simple docstring"""







            __snake_case      :    int  			=self.convolution(a						)
            __snake_case      :    str  			=self.normalization(a						)
            __snake_case      :    int  			=self.activation(a						)
            return hidden_state



class      _lowercase (     nn.Module						):


      def __init__(    self				:	Union[str, Any]							,   a				:	ResNetConfig						):





            """simple docstring"""







            super().__init__()
            __snake_case      :    Dict  			=ResNetConvLayer(
                config.num_channels							,   config.embedding_size							,   kernel_size=7							,   stride=2							,   activation=config.hidden_act						)
            __snake_case      :    Optional[Any]  			=nn.MaxPoolad(kernel_size=3							,   stride=2							,   padding=1						)
            __snake_case      :    Tuple  			=config.num_channels







      def _UpperCamelCase						(    self				:	Tuple							,   a				:	Tensor						):





            """simple docstring"""







            __snake_case      :    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.'''						)
            __snake_case      :    List[Any]  			=self.embedder(a						)
            __snake_case      :    Tuple  			=self.pooler(a						)
            return embedding



class      _lowercase (     nn.Module						):


      def __init__(    self				:	Union[str, Any]							,   a				:	int							,   a				:	int							,   a				:	int = 2						):





            """simple docstring"""







            super().__init__()
            __snake_case      :    Dict  			=nn.Convad(a							,   a							,   kernel_size=1							,   stride=a							,   bias=a						)
            __snake_case      :    Optional[Any]  			=nn.BatchNormad(a						)







      def _UpperCamelCase						(    self				:	Dict							,   a				:	Tensor						):





            """simple docstring"""







            __snake_case      :    List[Any]  			=self.convolution(a						)
            __snake_case      :    Dict  			=self.normalization(a						)
            return hidden_state



class      _lowercase (     nn.Module						):


      def __init__(    self				:	Dict							,   a				:	int							,   a				:	int							,   a				:	int = 1							,   a				:	str = "relu"						):





            """simple docstring"""







            super().__init__()
            __snake_case      :    Union[str, Any]  			=in_channels != out_channels or stride != 1
            __snake_case      :    List[Any]  			=(
                ResNetShortCut(a							,   a							,   stride=a						) if should_apply_shortcut else nn.Identity()
            )
            __snake_case      :    List[str]  			=nn.Sequential(
                ResNetConvLayer(a							,   a							,   stride=a						)							,   ResNetConvLayer(a							,   a							,   activation=a						)							,   )
            __snake_case      :    Dict  			=ACTaFN[activation]







      def _UpperCamelCase						(    self				:	Dict							,   a				:	Dict						):





            """simple docstring"""







            __snake_case      :    Optional[Any]  			=hidden_state
            __snake_case      :    str  			=self.layer(a						)
            __snake_case      :    Dict  			=self.shortcut(a						)
            hidden_state += residual
            __snake_case      :    Union[str, Any]  			=self.activation(a						)
            return hidden_state



class      _lowercase (     nn.Module						):


      def __init__(    self				:	int							,   a				:	int							,   a				:	int							,   a				:	int = 1							,   a				:	str = "relu"							,   a				:	int = 4						):





            """simple docstring"""







            super().__init__()
            __snake_case      :    Optional[int]  			=in_channels != out_channels or stride != 1
            __snake_case      :    Tuple  			=out_channels // reduction
            __snake_case      :    Dict  			=(
                ResNetShortCut(a							,   a							,   stride=a						) if should_apply_shortcut else nn.Identity()
            )
            __snake_case      :    Optional[int]  			=nn.Sequential(
                ResNetConvLayer(a							,   a							,   kernel_size=1						)							,   ResNetConvLayer(a							,   a							,   stride=a						)							,   ResNetConvLayer(a							,   a							,   kernel_size=1							,   activation=a						)							,   )
            __snake_case      :    List[str]  			=ACTaFN[activation]







      def _UpperCamelCase						(    self				:	Optional[int]							,   a				:	Dict						):





            """simple docstring"""







            __snake_case      :    List[Any]  			=hidden_state
            __snake_case      :    Union[str, Any]  			=self.layer(a						)
            __snake_case      :    Optional[int]  			=self.shortcut(a						)
            hidden_state += residual
            __snake_case      :    Optional[Any]  			=self.activation(a						)
            return hidden_state



class      _lowercase (     nn.Module						):


      def __init__(    self				:	Optional[int]							,   a				:	ResNetConfig							,   a				:	int							,   a				:	int							,   a				:	int = 2							,   a				:	int = 2							,   ):





            """simple docstring"""







            super().__init__()

            __snake_case      :    Dict  			=ResNetBottleNeckLayer if config.layer_type == '''bottleneck''' else ResNetBasicLayer

            __snake_case      :    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 _UpperCamelCase						(    self				:	int							,   a				:	Tensor						):





            """simple docstring"""







            __snake_case      :    str  			=input
            for layer in self.layers:
                  __snake_case      :    Optional[int]  			=layer(a						)
            return hidden_state



class      _lowercase (     nn.Module						):


      def __init__(    self				:	List[Any]							,   a				:	ResNetConfig						):





            """simple docstring"""







            super().__init__()
            __snake_case      :    Tuple  			=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]							,   )						)
            __snake_case      :    Optional[int]  			=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				:	Union[str, Any]							,   a				:	Tensor							,   a				:	bool = False							,   a				:	bool = True						):





            """simple docstring"""







            __snake_case      :    Optional[int]  			=() if output_hidden_states else None

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

                  __snake_case      :    Any  			=stage_module(a						)

            if output_hidden_states:
                  __snake_case      :    Optional[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=a							,   hidden_states=a							,   )



class      _lowercase (     lowerCAmelCase						):
      _a   :     Dict								=   ResNetConfig
      _a   :     List[Any]								=   '''resnet'''
      _a   :     Any								=   '''pixel_values'''
      _a   :     Union[str, Any]								=   True


      def _UpperCamelCase						(    self				:	Any							,   a				:	Any						):





            """simple docstring"""







            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				:	Optional[int]							,   a				:	Dict							,   a				:	str=False						):





            """simple docstring"""







            if isinstance(a							,   a						):
                  __snake_case      :    int  			=value



UpperCamelCase_	:				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 ([`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.
"""

UpperCamelCase_	:				Optional[Any]										= 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.'''		,  lowerCAmelCase		,  )
class      _lowercase (     lowerCAmelCase						):


      def __init__(    self				:	Union[str, Any]							,   a				:	int						):





            """simple docstring"""







            super().__init__(a						)
            __snake_case      :    Tuple  			=config
            __snake_case      :    Tuple  			=ResNetEmbeddings(a						)
            __snake_case      :    Tuple  			=ResNetEncoder(a						)
            __snake_case      :    Optional[int]  			=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[Any]							,   a				:	Tensor							,   a				:	Optional[bool] = None							,   a				:	Optional[bool] = None						):





            """simple docstring"""







            __snake_case      :    int  			=(
                output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
            )
            __snake_case      :    Tuple  			=return_dict if return_dict is not None else self.config.use_return_dict

            __snake_case      :    Optional[int]  			=self.embedder(a						)

            __snake_case      :    Tuple  			=self.encoder(
                a							,   output_hidden_states=a							,   return_dict=a						)

            __snake_case      :    Dict  			=encoder_outputs[0]

            __snake_case      :    Tuple  			=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.
    '''		,  lowerCAmelCase		,  )
class      _lowercase (     lowerCAmelCase						):


      def __init__(    self				:	List[str]							,   a				:	Union[str, Any]						):





            """simple docstring"""







            super().__init__(a						)
            __snake_case      :    List[str]  			=config.num_labels
            __snake_case      :    int  			=ResNetModel(a						)
            # classification head
            __snake_case      :    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 _UpperCamelCase						(    self				:	str							,   a				:	Optional[torch.FloatTensor] = None							,   a				:	Optional[torch.LongTensor] = None							,   a				:	Optional[bool] = None							,   a				:	Optional[bool] = None							,   ):





            """simple docstring"""







            __snake_case      :    Optional[Any]  			=return_dict if return_dict is not None else self.config.use_return_dict

            __snake_case      :    Optional[int]  			=self.resnet(a							,   output_hidden_states=a							,   return_dict=a						)

            __snake_case      :    Dict  			=outputs.pooler_output if return_dict else outputs[1]

            __snake_case      :    Any  			=self.classifier(a						)

            __snake_case      :    Dict  			=None

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

            if not return_dict:
                  __snake_case      :    List[str]  			=(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.
    '''		,  lowerCAmelCase		,  )
class      _lowercase (     lowerCAmelCase		,  lowerCAmelCase						):


      def __init__(    self				:	List[Any]							,   a				:	Dict						):





            """simple docstring"""







            super().__init__(a						)
            super()._init_backbone(a						)

            __snake_case      :    Optional[int]  			=[config.embedding_size] + config.hidden_sizes
            __snake_case      :    Optional[Any]  			=ResNetEmbeddings(a						)
            __snake_case      :    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 _UpperCamelCase						(    self				:	Any							,   a				:	Tensor							,   a				:	Optional[bool] = None							,   a				:	Optional[bool] = None						):





            """simple docstring"""







            __snake_case      :    int  			=return_dict if return_dict is not None else self.config.use_return_dict
            __snake_case      :    Tuple  			=(
                output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
            )

            __snake_case      :    Optional[int]  			=self.embedder(a						)

            __snake_case      :    Tuple  			=self.encoder(a							,   output_hidden_states=a							,   return_dict=a						)

            __snake_case      :    List[str]  			=outputs.hidden_states

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

            if not return_dict:
                  __snake_case      :    Any  			=(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"""





from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


UpperCamelCase_	:				Dict										= logging.get_logger(__name__)

UpperCamelCase_	:				Dict										= {
    """facebook/xmod-base""": """https://huggingface.co/facebook/xmod-base/resolve/main/config.json""",
    """facebook/xmod-large-prenorm""": """https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json""",
    """facebook/xmod-base-13-125k""": """https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json""",
    """facebook/xmod-base-30-125k""": """https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json""",
    """facebook/xmod-base-30-195k""": """https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json""",
    """facebook/xmod-base-60-125k""": """https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json""",
    """facebook/xmod-base-60-265k""": """https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json""",
    """facebook/xmod-base-75-125k""": """https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json""",
    """facebook/xmod-base-75-269k""": """https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json""",
}


class      _lowercase (     lowerCAmelCase						):
      _a   :     Union[str, Any]								=   '''xmod'''


      def __init__(    self				:	Optional[int]							,   a				:	Dict=3_0_5_2_2							,   a				:	Any=7_6_8							,   a				:	str=1_2							,   a				:	Optional[int]=1_2							,   a				:	Any=3_0_7_2							,   a				:	int="gelu"							,   a				:	List[Any]=0.1							,   a				:	Union[str, Any]=0.1							,   a				:	Tuple=5_1_2							,   a				:	str=2							,   a				:	Any=0.0_2							,   a				:	Tuple=1e-12							,   a				:	Optional[int]=1							,   a				:	Any=0							,   a				:	str=2							,   a				:	str="absolute"							,   a				:	List[Any]=True							,   a				:	Optional[Any]=None							,   a				:	Tuple=False							,   a				:	Union[str, Any]=2							,   a				:	Any=False							,   a				:	Dict=True							,   a				:	int=True							,   a				:	str=("en_XX",)							,   a				:	Dict=None							,   **a				:	Tuple							,   ):





            """simple docstring"""







            super().__init__(pad_token_id=a							,   bos_token_id=a							,   eos_token_id=a							,   **a						)

            __snake_case      :    Union[str, Any]  			=vocab_size
            __snake_case      :    Union[str, Any]  			=hidden_size
            __snake_case      :    int  			=num_hidden_layers
            __snake_case      :    Union[str, Any]  			=num_attention_heads
            __snake_case      :    Any  			=hidden_act
            __snake_case      :    Tuple  			=intermediate_size
            __snake_case      :    int  			=hidden_dropout_prob
            __snake_case      :    str  			=attention_probs_dropout_prob
            __snake_case      :    int  			=max_position_embeddings
            __snake_case      :    Tuple  			=type_vocab_size
            __snake_case      :    Union[str, Any]  			=initializer_range
            __snake_case      :    Dict  			=layer_norm_eps
            __snake_case      :    Optional[int]  			=position_embedding_type
            __snake_case      :    int  			=use_cache
            __snake_case      :    int  			=classifier_dropout
            __snake_case      :    int  			=pre_norm
            __snake_case      :    Any  			=adapter_reduction_factor
            __snake_case      :    str  			=adapter_layer_norm
            __snake_case      :    Union[str, Any]  			=adapter_reuse_layer_norm
            __snake_case      :    List[str]  			=ln_before_adapter
            __snake_case      :    Optional[int]  			=list(a						)
            __snake_case      :    int  			=default_language



class      _lowercase (     lowerCAmelCase						):


      @property
      def _UpperCamelCase						(    self				:	Dict						):





            """simple docstring"""







            if self.task == "multiple-choice":
                  __snake_case      :    Tuple  			={0: '''batch''', 1: '''choice''', 2: '''sequence'''}
            else:
                  __snake_case      :    Tuple  			={0: '''batch''', 1: '''sequence'''}
            return OrderedDict(
                [
                    ('''input_ids''', dynamic_axis),
                    ('''attention_mask''', dynamic_axis),
                ]						)


