import collections
from typing import List, Optional, Union

from ...tokenization_utils_base import BatchEncoding
from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging
from ..bert.tokenization_bert import BertTokenizer


_lowerCAmelCase  :Any  				=    logging.get_logger(__name__)

_lowerCAmelCase  :List[Any]  				=    {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""}

_lowerCAmelCase  :List[str]  				=    {
    """vocab_file""": {
        """facebook/dpr-ctx_encoder-single-nq-base""": (
            """https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt"""
        ),
        """facebook/dpr-ctx_encoder-multiset-base""": (
            """https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt"""
        ),
    },
    """tokenizer_file""": {
        """facebook/dpr-ctx_encoder-single-nq-base""": (
            """https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json"""
        ),
        """facebook/dpr-ctx_encoder-multiset-base""": (
            """https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json"""
        ),
    },
}
_lowerCAmelCase  :str  				=    {
    """vocab_file""": {
        """facebook/dpr-question_encoder-single-nq-base""": (
            """https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt"""
        ),
        """facebook/dpr-question_encoder-multiset-base""": (
            """https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt"""
        ),
    },
    """tokenizer_file""": {
        """facebook/dpr-question_encoder-single-nq-base""": (
            """https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json"""
        ),
        """facebook/dpr-question_encoder-multiset-base""": (
            """https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json"""
        ),
    },
}
_lowerCAmelCase  :Any  				=    {
    """vocab_file""": {
        """facebook/dpr-reader-single-nq-base""": (
            """https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt"""
        ),
        """facebook/dpr-reader-multiset-base""": (
            """https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt"""
        ),
    },
    """tokenizer_file""": {
        """facebook/dpr-reader-single-nq-base""": (
            """https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json"""
        ),
        """facebook/dpr-reader-multiset-base""": (
            """https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json"""
        ),
    },
}

_lowerCAmelCase  :str  				=    {
    """facebook/dpr-ctx_encoder-single-nq-base""": 512,
    """facebook/dpr-ctx_encoder-multiset-base""": 512,
}
_lowerCAmelCase  :Union[str, Any]  				=    {
    """facebook/dpr-question_encoder-single-nq-base""": 512,
    """facebook/dpr-question_encoder-multiset-base""": 512,
}
_lowerCAmelCase  :Optional[Any]  				=    {
    """facebook/dpr-reader-single-nq-base""": 512,
    """facebook/dpr-reader-multiset-base""": 512,
}


_lowerCAmelCase  :int  				=    {
    """facebook/dpr-ctx_encoder-single-nq-base""": {"""do_lower_case""": True},
    """facebook/dpr-ctx_encoder-multiset-base""": {"""do_lower_case""": True},
}
_lowerCAmelCase  :int  				=    {
    """facebook/dpr-question_encoder-single-nq-base""": {"""do_lower_case""": True},
    """facebook/dpr-question_encoder-multiset-base""": {"""do_lower_case""": True},
}
_lowerCAmelCase  :List[str]  				=    {
    """facebook/dpr-reader-single-nq-base""": {"""do_lower_case""": True},
    """facebook/dpr-reader-multiset-base""": {"""do_lower_case""": True},
}






class 				UpperCAmelCase (       _SCREAMING_SNAKE_CASE ):




    '''simple docstring'''







    snake_case__							:   Union[str, Any]							=						VOCAB_FILES_NAMES
    snake_case__							:   Tuple							=						CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP
    snake_case__							:   List[Any]							=						CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
    snake_case__							:   Tuple							=						CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION






class 				UpperCAmelCase (       _SCREAMING_SNAKE_CASE ):




    '''simple docstring'''







    snake_case__							:   str							=						VOCAB_FILES_NAMES
    snake_case__							:   Tuple							=						QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP
    snake_case__							:   Tuple							=						QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
    snake_case__							:   int							=						QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION






_lowerCAmelCase  :Any  				=    collections.namedtuple(
    """DPRSpanPrediction""", ["""span_score""", """relevance_score""", """doc_id""", """start_index""", """end_index""", """text"""]
)

_lowerCAmelCase  :List[str]  				=    collections.namedtuple("""DPRReaderOutput""", ["""start_logits""", """end_logits""", """relevance_logits"""])


_lowerCAmelCase  :Union[str, Any]  				=    R"""
    Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`.
    It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers),
    using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)`
    with the format:

    ```
    [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids>
    ```

    Args:
        questions (`str` or `List[str]`):
            The questions to be encoded. You can specify one question for many passages. In this case, the question
            will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in
            `titles` or `texts`.
        titles (`str` or `List[str]`):
            The passages titles to be encoded. This can be a string or a list of strings if there are several passages.
        texts (`str` or `List[str]`):
            The passages texts to be encoded. This can be a string or a list of strings if there are several passages.
        padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`):
            Activates and controls padding. Accepts the following values:

            - `True` or `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single sequence
              if provided).
            - `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum
              acceptable input length for the model if that argument is not provided.
            - `False` or `'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of different
              lengths).
        truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`):
            Activates and controls truncation. Accepts the following values:

            - `True` or `'longest_first'`: Truncate to a maximum length specified with the argument `max_length` or to
              the maximum acceptable input length for the model if that argument is not provided. This will truncate
              token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch
              of pairs) is provided.
            - `'only_first'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum
              acceptable input length for the model if that argument is not provided. This will only truncate the first
              sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
            - `'only_second'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum
              acceptable input length for the model if that argument is not provided. This will only truncate the
              second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
            - `False` or `'do_not_truncate'` (default): No truncation (i.e., can output batch with sequence lengths
              greater than the model maximum admissible input size).
        max_length (`int`, *optional*):
                Controls the maximum length to use by one of the truncation/padding parameters.

                If left unset or set to `None`, this will use the predefined model maximum length if a maximum length
                is required by one of the truncation/padding parameters. If the model has no specific maximum input
                length (like XLNet) truncation/padding to a maximum length will be deactivated.
        return_tensors (`str` or [`~utils.TensorType`], *optional*):
                If set, will return tensors instead of list of python integers. Acceptable values are:

                - `'tf'`: Return TensorFlow `tf.constant` objects.
                - `'pt'`: Return PyTorch `torch.Tensor` objects.
                - `'np'`: Return Numpy `np.ndarray` objects.
        return_attention_mask (`bool`, *optional*):
            Whether or not to return the attention mask. If not set, will return the attention mask according to the
            specific tokenizer's default, defined by the `return_outputs` attribute.

            [What are attention masks?](../glossary#attention-mask)

    Returns:
        `Dict[str, List[List[int]]]`: A dictionary with the following keys:

        - `input_ids`: List of token ids to be fed to a model.
        - `attention_mask`: List of indices specifying which tokens should be attended to by the model.
    """






@add_start_docstrings(_SCREAMING_SNAKE_CASE )
class 				UpperCAmelCase :




    '''simple docstring'''







    def __call__(       self    ,							lowercase__    ,							lowercase__ = None    ,							lowercase__ = None    ,							lowercase__ = False    ,							lowercase__ = False    ,							lowercase__ = None    ,							lowercase__ = None    ,							lowercase__ = None    ,							**lowercase__    ,							)							-> BatchEncoding:
         if titles is None and texts is None:
              return super().__call__(
                  lowercase__    ,							padding=lowercase__    ,							truncation=lowercase__    ,							max_length=lowercase__    ,							return_tensors=lowercase__    ,							return_attention_mask=lowercase__    ,							**lowercase__    ,							)
         elif titles is None or texts is None:
              SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       titles if texts is None else texts
              return super().__call__(
                  lowercase__    ,							lowercase__    ,							padding=lowercase__    ,							truncation=lowercase__    ,							max_length=lowercase__    ,							return_tensors=lowercase__    ,							return_attention_mask=lowercase__    ,							**lowercase__    ,							)
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       titles if not isinstance(lowercase__    ,							lowercase__ ) else [titles]
         SCREAMING_SNAKE_CASE					:			Dict    	=       texts if not isinstance(lowercase__    ,							lowercase__ ) else [texts]
         SCREAMING_SNAKE_CASE					:			Any    	=       len(lowercase__ )
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       questions if not isinstance(lowercase__    ,							lowercase__ ) else [questions] * n_passages
         if len(lowercase__ ) != len(lowercase__ ):
              raise ValueError(
                  F"""There should be as many titles than texts but got {len(lowercase__ )} titles and {len(lowercase__ )} texts.""" )
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       super().__call__(lowercase__    ,							lowercase__    ,							padding=lowercase__    ,							truncation=lowercase__ )['input_ids']
         SCREAMING_SNAKE_CASE					:			List[str]    	=       super().__call__(lowercase__    ,							add_special_tokens=lowercase__    ,							padding=lowercase__    ,							truncation=lowercase__ )['input_ids']
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       {
             'input_ids': [
                 (encoded_question_and_title + encoded_text)[:max_length]
                 if max_length is not None and truncation
                 else encoded_question_and_title + encoded_text
                 for encoded_question_and_title, encoded_text in zip(lowercase__    ,							lowercase__ )
             ]
         }
         if return_attention_mask is not False:
              SCREAMING_SNAKE_CASE					:			int    	=       []
              for input_ids in encoded_inputs["input_ids"]:
                   attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] )
              SCREAMING_SNAKE_CASE					:			Optional[int]    	=       attention_mask
         return self.pad(lowercase__    ,							padding=lowercase__    ,							max_length=lowercase__    ,							return_tensors=lowercase__ )






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__    ,							lowercase__ = 16    ,							lowercase__ = 64    ,							lowercase__ = 4    ,							)							-> List[DPRSpanPrediction]:
         SCREAMING_SNAKE_CASE					:			Any    	=       reader_input['input_ids']
         SCREAMING_SNAKE_CASE					:			int    	=       reader_output[:3]
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       len(lowercase__ )
         SCREAMING_SNAKE_CASE					:			Tuple    	=       sorted(range(lowercase__ )    ,							reverse=lowercase__    ,							key=relevance_logits.__getitem__ )
         SCREAMING_SNAKE_CASE					:			List[DPRReaderOutput]    	=       []
         for doc_id in sorted_docs:
              SCREAMING_SNAKE_CASE					:			int    	=       list(input_ids[doc_id] )
              # assuming question & title information is at the beginning of the sequence
              SCREAMING_SNAKE_CASE					:			List[str]    	=       sequence_ids.index(self.sep_token_id    ,							2 ) + 1  # second sep id
              if sequence_ids[-1] == self.pad_token_id:
                   SCREAMING_SNAKE_CASE					:			Optional[int]    	=       sequence_ids.index(self.pad_token_id )
              else:
                   SCREAMING_SNAKE_CASE					:			Tuple    	=       len(lowercase__ )

              SCREAMING_SNAKE_CASE					:			List[Any]    	=       self._get_best_spans(
                  start_logits=start_logits[doc_id][passage_offset:sequence_len]    ,							end_logits=end_logits[doc_id][passage_offset:sequence_len]    ,							max_answer_length=lowercase__    ,							top_spans=lowercase__    ,							)
              for start_index, end_index in best_spans:
                   start_index += passage_offset
                   end_index += passage_offset
                   nbest_spans_predictions.append(
                       DPRSpanPrediction(
                           span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index]    ,							relevance_score=relevance_logits[doc_id]    ,							doc_id=lowercase__    ,							start_index=lowercase__    ,							end_index=lowercase__    ,							text=self.decode(sequence_ids[start_index : end_index + 1] )    ,							) )
              if len(lowercase__ ) >= num_spans:
                   break
         return nbest_spans_predictions[:num_spans]






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							)							-> List[DPRSpanPrediction]:
         SCREAMING_SNAKE_CASE					:			Dict    	=       []
         for start_index, start_score in enumerate(lowercase__ ):
              for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ):
                   scores.append(((start_index, start_index + answer_length), start_score + end_score) )
         SCREAMING_SNAKE_CASE					:			int    	=       sorted(lowercase__    ,							key=lambda lowercase__ : x[1]    ,							reverse=lowercase__ )
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       []
         for (start_index, end_index), score in scores:
              if start_index > end_index:
                   raise ValueError(F"""Wrong span indices: [{start_index}:{end_index}]""" )
              SCREAMING_SNAKE_CASE					:			Optional[int]    	=       end_index - start_index + 1
              if length > max_answer_length:
                   raise ValueError(F"""Span is too long: {length} > {max_answer_length}""" )
              if any(
                  start_index <= prev_start_index <= prev_end_index <= end_index
                  or prev_start_index <= start_index <= end_index <= prev_end_index
                  for (prev_start_index, prev_end_index) in chosen_span_intervals ):
                   continue
              chosen_span_intervals.append((start_index, end_index) )

              if len(lowercase__ ) == top_spans:
                   break
         return chosen_span_intervals






@add_end_docstrings(_SCREAMING_SNAKE_CASE )
class 				UpperCAmelCase (       _SCREAMING_SNAKE_CASE   ,    _SCREAMING_SNAKE_CASE ):




    '''simple docstring'''







    snake_case__							:   int							=						VOCAB_FILES_NAMES
    snake_case__							:   Any							=						READER_PRETRAINED_VOCAB_FILES_MAP
    snake_case__							:   List[Any]							=						READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
    snake_case__							:   Optional[int]							=						READER_PRETRAINED_INIT_CONFIGURATION
    snake_case__							:   int							=						["input_ids", "attention_mask"]




'''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  :List[Any]  				=    logging.get_logger(__name__)

# General docstring
_lowerCAmelCase  :Optional[Any]  				=    """RegNetConfig"""

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

# Image classification docstring
_lowerCAmelCase  :Optional[Any]  				=    """facebook/regnet-y-040"""
_lowerCAmelCase  :Optional[int]  				=    """tabby, tabby cat"""

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






class 				UpperCAmelCase (       nn.Module ):




    '''simple docstring'''







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






    def       _UpperCamelCase    (       self    ,							lowercase__ )							-> List[Any]:
         SCREAMING_SNAKE_CASE					:			int    	=       self.convolution(lowercase__ )
         SCREAMING_SNAKE_CASE					:			List[str]    	=       self.normalization(lowercase__ )
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       self.activation(lowercase__ )
         return hidden_state






class 				UpperCAmelCase (       nn.Module ):




    '''simple docstring'''







    def __init__(       self    ,							lowercase__ )							-> str:
         super().__init__()
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       RegNetConvLayer(
             config.num_channels    ,							config.embedding_size    ,							kernel_size=3    ,							stride=2    ,							activation=config.hidden_act )
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       config.num_channels






    def       _UpperCamelCase    (       self    ,							lowercase__ )							-> str:
         SCREAMING_SNAKE_CASE					:			Dict    	=       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.' )
         SCREAMING_SNAKE_CASE					:			str    	=       self.embedder(lowercase__ )
         return hidden_state






class 				UpperCAmelCase (       nn.Module ):




    '''simple docstring'''







    def __init__(       self    ,							lowercase__    ,							lowercase__    ,							lowercase__ = 2 )							-> Optional[int]:
         super().__init__()
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       nn.Convad(lowercase__    ,							lowercase__    ,							kernel_size=1    ,							stride=lowercase__    ,							bias=lowercase__ )
         SCREAMING_SNAKE_CASE					:			Dict    	=       nn.BatchNormad(lowercase__ )






    def       _UpperCamelCase    (       self    ,							lowercase__ )							-> Tensor:
         SCREAMING_SNAKE_CASE					:			int    	=       self.convolution(lowercase__ )
         SCREAMING_SNAKE_CASE					:			int    	=       self.normalization(lowercase__ )
         return hidden_state






class 				UpperCAmelCase (       nn.Module ):




    '''simple docstring'''







    def __init__(       self    ,							lowercase__    ,							lowercase__ )							-> List[str]:
         super().__init__()

         SCREAMING_SNAKE_CASE					:			Any    	=       nn.AdaptiveAvgPoolad((1, 1) )
         SCREAMING_SNAKE_CASE					:			Tuple    	=       nn.Sequential(
             nn.Convad(lowercase__    ,							lowercase__    ,							kernel_size=1 )    ,							nn.ReLU()    ,							nn.Convad(lowercase__    ,							lowercase__    ,							kernel_size=1 )    ,							nn.Sigmoid()    ,							)






    def       _UpperCamelCase    (       self    ,							lowercase__ )							-> Dict:
         # b c h w -> b c 1 1
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       self.pooler(lowercase__ )
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       self.attention(lowercase__ )
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       hidden_state * attention
         return hidden_state






class 				UpperCAmelCase (       nn.Module ):




    '''simple docstring'''







    def __init__(       self    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__ = 1 )							-> Tuple:
         super().__init__()
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       in_channels != out_channels or stride != 1
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       max(1    ,							out_channels // config.groups_width )
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       (
             RegNetShortCut(lowercase__    ,							lowercase__    ,							stride=lowercase__ ) if should_apply_shortcut else nn.Identity()
         )
         SCREAMING_SNAKE_CASE					:			Any    	=       nn.Sequential(
             RegNetConvLayer(lowercase__    ,							lowercase__    ,							kernel_size=1    ,							activation=config.hidden_act )    ,							RegNetConvLayer(lowercase__    ,							lowercase__    ,							stride=lowercase__    ,							groups=lowercase__    ,							activation=config.hidden_act )    ,							RegNetConvLayer(lowercase__    ,							lowercase__    ,							kernel_size=1    ,							activation=lowercase__ )    ,							)
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       ACTaFN[config.hidden_act]






    def       _UpperCamelCase    (       self    ,							lowercase__ )							-> Any:
         SCREAMING_SNAKE_CASE					:			Tuple    	=       hidden_state
         SCREAMING_SNAKE_CASE					:			str    	=       self.layer(lowercase__ )
         SCREAMING_SNAKE_CASE					:			int    	=       self.shortcut(lowercase__ )
         hidden_state += residual
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       self.activation(lowercase__ )
         return hidden_state






class 				UpperCAmelCase (       nn.Module ):




    '''simple docstring'''







    def __init__(       self    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__ = 1 )							-> str:
         super().__init__()
         SCREAMING_SNAKE_CASE					:			Dict    	=       in_channels != out_channels or stride != 1
         SCREAMING_SNAKE_CASE					:			int    	=       max(1    ,							out_channels // config.groups_width )
         SCREAMING_SNAKE_CASE					:			int    	=       (
             RegNetShortCut(lowercase__    ,							lowercase__    ,							stride=lowercase__ ) if should_apply_shortcut else nn.Identity()
         )
         SCREAMING_SNAKE_CASE					:			Tuple    	=       nn.Sequential(
             RegNetConvLayer(lowercase__    ,							lowercase__    ,							kernel_size=1    ,							activation=config.hidden_act )    ,							RegNetConvLayer(lowercase__    ,							lowercase__    ,							stride=lowercase__    ,							groups=lowercase__    ,							activation=config.hidden_act )    ,							RegNetSELayer(lowercase__    ,							reduced_channels=int(round(in_channels / 4 ) ) )    ,							RegNetConvLayer(lowercase__    ,							lowercase__    ,							kernel_size=1    ,							activation=lowercase__ )    ,							)
         SCREAMING_SNAKE_CASE					:			Tuple    	=       ACTaFN[config.hidden_act]






    def       _UpperCamelCase    (       self    ,							lowercase__ )							-> Union[str, Any]:
         SCREAMING_SNAKE_CASE					:			int    	=       hidden_state
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       self.layer(lowercase__ )
         SCREAMING_SNAKE_CASE					:			Tuple    	=       self.shortcut(lowercase__ )
         hidden_state += residual
         SCREAMING_SNAKE_CASE					:			int    	=       self.activation(lowercase__ )
         return hidden_state






class 				UpperCAmelCase (       nn.Module ):




    '''simple docstring'''







    def __init__(       self    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__ = 2    ,							lowercase__ = 2    ,							)							-> Any:
         super().__init__()

         SCREAMING_SNAKE_CASE					:			Tuple    	=       RegNetXLayer if config.layer_type == 'x' else RegNetYLayer

         SCREAMING_SNAKE_CASE					:			str    	=       nn.Sequential(
             # downsampling is done in the first layer with stride of 2
             layer(
                 lowercase__    ,							lowercase__    ,							lowercase__    ,							stride=lowercase__    ,							)    ,							*[layer(lowercase__    ,							lowercase__    ,							lowercase__ ) for _ in range(depth - 1 )]    ,							)






    def       _UpperCamelCase    (       self    ,							lowercase__ )							-> List[Any]:
         SCREAMING_SNAKE_CASE					:			Tuple    	=       self.layers(lowercase__ )
         return hidden_state






class 				UpperCAmelCase (       nn.Module ):




    '''simple docstring'''







    def __init__(       self    ,							lowercase__ )							-> Dict:
         super().__init__()
         SCREAMING_SNAKE_CASE					:			Dict    	=       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(
                 lowercase__    ,							config.embedding_size    ,							config.hidden_sizes[0]    ,							stride=2 if config.downsample_in_first_stage else 1    ,							depth=config.depths[0]    ,							) )
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       zip(config.hidden_sizes    ,							config.hidden_sizes[1:] )
         for (in_channels, out_channels), depth in zip(lowercase__    ,							config.depths[1:] ):
              self.stages.append(RegNetStage(lowercase__    ,							lowercase__    ,							lowercase__    ,							depth=lowercase__ ) )






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__ = False    ,							lowercase__ = True )							-> BaseModelOutputWithNoAttention:
         SCREAMING_SNAKE_CASE					:			str    	=       () if output_hidden_states else None

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

              SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       stage_module(lowercase__ )

         if output_hidden_states:
              SCREAMING_SNAKE_CASE					:			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=lowercase__    ,							hidden_states=lowercase__ )






class 				UpperCAmelCase (       _SCREAMING_SNAKE_CASE ):




    '''simple docstring'''







    snake_case__							:   Any							=						RegNetConfig
    snake_case__							:   List[str]							=						"regnet"
    snake_case__							:   List[Any]							=						"pixel_values"
    snake_case__							:   Optional[Any]							=						True






    def       _UpperCamelCase    (       self    ,							lowercase__ )							-> Any:
         if isinstance(lowercase__    ,							nn.Convad ):
              nn.init.kaiming_normal_(module.weight    ,							mode='fan_out'    ,							nonlinearity='relu' )
         elif isinstance(lowercase__    ,							(nn.BatchNormad, nn.GroupNorm) ):
              nn.init.constant_(module.weight    ,							1 )
              nn.init.constant_(module.bias    ,							0 )






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__=False )							-> Tuple:
         if isinstance(lowercase__    ,							lowercase__ ):
              SCREAMING_SNAKE_CASE					:			Tuple    	=       value






_lowerCAmelCase  :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 ([`RegNetConfig`]): 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.
"""

_lowerCAmelCase  :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 [`~file_utils.ModelOutput`] instead of a plain tuple.
"""






@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 				UpperCAmelCase (       _SCREAMING_SNAKE_CASE ):




    '''simple docstring'''







    def __init__(       self    ,							lowercase__ )							-> Union[str, Any]:
         super().__init__(lowercase__ )
         SCREAMING_SNAKE_CASE					:			str    	=       config
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       RegNetEmbeddings(lowercase__ )
         SCREAMING_SNAKE_CASE					:			List[str]    	=       RegNetEncoder(lowercase__ )
         SCREAMING_SNAKE_CASE					:			int    	=       nn.AdaptiveAvgPoolad((1, 1) )
         # Initialize weights and apply final processing
         self.post_init()






    @add_start_docstrings_to_model_forward(lowercase__ )
    @add_code_sample_docstrings(
        checkpoint=_CHECKPOINT_FOR_DOC    ,							output_type=lowercase__    ,							config_class=_CONFIG_FOR_DOC    ,							modality='vision'    ,							expected_output=_EXPECTED_OUTPUT_SHAPE    ,							)
    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__ = None    ,							lowercase__ = None )							-> BaseModelOutputWithPoolingAndNoAttention:
         SCREAMING_SNAKE_CASE					:			Dict    	=       (
             output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
         )
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       return_dict if return_dict is not None else self.config.use_return_dict

         SCREAMING_SNAKE_CASE					:			List[str]    	=       self.embedder(lowercase__ )

         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       self.encoder(
             lowercase__    ,							output_hidden_states=lowercase__    ,							return_dict=lowercase__ )

         SCREAMING_SNAKE_CASE					:			Dict    	=       encoder_outputs[0]

         SCREAMING_SNAKE_CASE					:			Dict    	=       self.pooler(lowercase__ )

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

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






@add_start_docstrings(
    "\n    RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    "   ,    _SCREAMING_SNAKE_CASE   ,    )
# Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet
class 				UpperCAmelCase (       _SCREAMING_SNAKE_CASE ):




    '''simple docstring'''







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

         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       self.regnet(lowercase__    ,							output_hidden_states=lowercase__    ,							return_dict=lowercase__ )

         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       outputs.pooler_output if return_dict else outputs[1]

         SCREAMING_SNAKE_CASE					:			List[Any]    	=       self.classifier(lowercase__ )

         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       None

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

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

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



