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


__magic_name__								=logging.get_logger(__name__)

# General docstring
__magic_name__								='''ResNetConfig'''

# Base docstring
__magic_name__								='''microsoft/resnet-50'''
__magic_name__								=[1, 2048, 7, 7]

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

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



class     _A   (    nn.Module							):






  def __init__(self   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_ = 3   ,				SCREAMING_SNAKE_CASE_ = 1   ,				SCREAMING_SNAKE_CASE_ = "relu"	)		->      Optional[int]:





       '''simple docstring'''





       super().__init__()
       UpperCamelCase__										=      nn.Convad(
           SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				kernel_size=SCREAMING_SNAKE_CASE_   ,				stride=SCREAMING_SNAKE_CASE_   ,				padding=kernel_size // 2   ,				bias=SCREAMING_SNAKE_CASE_	)
       UpperCamelCase__										=      nn.BatchNormad(SCREAMING_SNAKE_CASE_	)
       UpperCamelCase__										=      ACTaFN[activation] if activation is not None else nn.Identity()






  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_	)		->      Tensor:





       '''simple docstring'''





       UpperCamelCase__										=      self.convolution(SCREAMING_SNAKE_CASE_	)
       UpperCamelCase__										=      self.normalization(SCREAMING_SNAKE_CASE_	)
       UpperCamelCase__										=      self.activation(SCREAMING_SNAKE_CASE_	)
       return hidden_state



class     _A   (    nn.Module							):






  def __init__(self   ,				SCREAMING_SNAKE_CASE_	)		->      Union[str, Any]:





       '''simple docstring'''





       super().__init__()
       UpperCamelCase__										=      ResNetConvLayer(
           config.num_channels   ,				config.embedding_size   ,				kernel_size=7   ,				stride=2   ,				activation=config.hidden_act	)
       UpperCamelCase__										=      nn.MaxPoolad(kernel_size=3   ,				stride=2   ,				padding=1	)
       UpperCamelCase__										=      config.num_channels






  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_	)		->      Tensor:





       '''simple docstring'''





       UpperCamelCase__										=      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.'''	)
       UpperCamelCase__										=      self.embedder(SCREAMING_SNAKE_CASE_	)
       UpperCamelCase__										=      self.pooler(SCREAMING_SNAKE_CASE_	)
       return embedding



class     _A   (    nn.Module							):






  def __init__(self   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_ = 2	)		->      Optional[int]:





       '''simple docstring'''





       super().__init__()
       UpperCamelCase__										=      nn.Convad(SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				kernel_size=1   ,				stride=SCREAMING_SNAKE_CASE_   ,				bias=SCREAMING_SNAKE_CASE_	)
       UpperCamelCase__										=      nn.BatchNormad(SCREAMING_SNAKE_CASE_	)






  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_	)		->      Tensor:





       '''simple docstring'''





       UpperCamelCase__										=      self.convolution(SCREAMING_SNAKE_CASE_	)
       UpperCamelCase__										=      self.normalization(SCREAMING_SNAKE_CASE_	)
       return hidden_state



class     _A   (    nn.Module							):






  def __init__(self   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_ = 1   ,				SCREAMING_SNAKE_CASE_ = "relu"	)		->      int:





       '''simple docstring'''





       super().__init__()
       UpperCamelCase__										=      in_channels != out_channels or stride != 1
       UpperCamelCase__										=      (
           ResNetShortCut(SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				stride=SCREAMING_SNAKE_CASE_	) if should_apply_shortcut else nn.Identity()
       )
       UpperCamelCase__										=      nn.Sequential(
           ResNetConvLayer(SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				stride=SCREAMING_SNAKE_CASE_	)   ,				ResNetConvLayer(SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				activation=SCREAMING_SNAKE_CASE_	)   ,				)
       UpperCamelCase__										=      ACTaFN[activation]






  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_	)		->      List[Any]:





       '''simple docstring'''





       UpperCamelCase__										=      hidden_state
       UpperCamelCase__										=      self.layer(SCREAMING_SNAKE_CASE_	)
       UpperCamelCase__										=      self.shortcut(SCREAMING_SNAKE_CASE_	)
       hidden_state += residual
       UpperCamelCase__										=      self.activation(SCREAMING_SNAKE_CASE_	)
       return hidden_state



class     _A   (    nn.Module							):






  def __init__(self   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_ = 1   ,				SCREAMING_SNAKE_CASE_ = "relu"   ,				SCREAMING_SNAKE_CASE_ = 4	)		->      Dict:





       '''simple docstring'''





       super().__init__()
       UpperCamelCase__										=      in_channels != out_channels or stride != 1
       UpperCamelCase__										=      out_channels // reduction
       UpperCamelCase__										=      (
           ResNetShortCut(SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				stride=SCREAMING_SNAKE_CASE_	) if should_apply_shortcut else nn.Identity()
       )
       UpperCamelCase__										=      nn.Sequential(
           ResNetConvLayer(SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				kernel_size=1	)   ,				ResNetConvLayer(SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				stride=SCREAMING_SNAKE_CASE_	)   ,				ResNetConvLayer(SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				kernel_size=1   ,				activation=SCREAMING_SNAKE_CASE_	)   ,				)
       UpperCamelCase__										=      ACTaFN[activation]






  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_	)		->      Tuple:





       '''simple docstring'''





       UpperCamelCase__										=      hidden_state
       UpperCamelCase__										=      self.layer(SCREAMING_SNAKE_CASE_	)
       UpperCamelCase__										=      self.shortcut(SCREAMING_SNAKE_CASE_	)
       hidden_state += residual
       UpperCamelCase__										=      self.activation(SCREAMING_SNAKE_CASE_	)
       return hidden_state



class     _A   (    nn.Module							):






  def __init__(self   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_ = 2   ,				SCREAMING_SNAKE_CASE_ = 2   ,				)		->      Optional[int]:





       '''simple docstring'''





       super().__init__()

       UpperCamelCase__										=      ResNetBottleNeckLayer if config.layer_type == '''bottleneck''' else ResNetBasicLayer

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






  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_	)		->      Tensor:





       '''simple docstring'''





       UpperCamelCase__										=      input
       for layer in self.layers:
            UpperCamelCase__										=      layer(SCREAMING_SNAKE_CASE_	)
       return hidden_state



class     _A   (    nn.Module							):






  def __init__(self   ,				SCREAMING_SNAKE_CASE_	)		->      Optional[Any]:





       '''simple docstring'''





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






  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_ = False   ,				SCREAMING_SNAKE_CASE_ = True	)		->      BaseModelOutputWithNoAttention:





       '''simple docstring'''





       UpperCamelCase__										=      () if output_hidden_states else None

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

            UpperCamelCase__										=      stage_module(SCREAMING_SNAKE_CASE_	)

       if output_hidden_states:
            UpperCamelCase__										=      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=SCREAMING_SNAKE_CASE_   ,				hidden_states=SCREAMING_SNAKE_CASE_   ,				)



class     _A   (    __UpperCamelCase							):
  SCREAMING_SNAKE_CASE_	:	List[Any]						=ResNetConfig
  SCREAMING_SNAKE_CASE_	:	Optional[Any]						="resnet"
  SCREAMING_SNAKE_CASE_	:	int						="pixel_values"
  SCREAMING_SNAKE_CASE_	:	Tuple						=True






  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_	)		->      Tuple:





       '''simple docstring'''





       if isinstance(SCREAMING_SNAKE_CASE_   ,				nn.Convad	):
            nn.init.kaiming_normal_(module.weight   ,				mode='''fan_out'''   ,				nonlinearity='''relu'''	)
       elif isinstance(SCREAMING_SNAKE_CASE_   ,				(nn.BatchNormad, nn.GroupNorm)	):
            nn.init.constant_(module.weight   ,				1	)
            nn.init.constant_(module.bias   ,				0	)






  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_=False	)		->      Dict:





       '''simple docstring'''





       if isinstance(SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_	):
            UpperCamelCase__										=      value



__magic_name__								=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.
'''

__magic_name__								=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."	, __UpperCamelCase	, )
class     _A   (    __UpperCamelCase							):






  def __init__(self   ,				SCREAMING_SNAKE_CASE_	)		->      Tuple:





       '''simple docstring'''





       super().__init__(SCREAMING_SNAKE_CASE_	)
       UpperCamelCase__										=      config
       UpperCamelCase__										=      ResNetEmbeddings(SCREAMING_SNAKE_CASE_	)
       UpperCamelCase__										=      ResNetEncoder(SCREAMING_SNAKE_CASE_	)
       UpperCamelCase__										=      nn.AdaptiveAvgPoolad((1, 1)	)
       # Initialize weights and apply final processing
       self.post_init()






  @add_start_docstrings_to_model_forward(SCREAMING_SNAKE_CASE_	)
  @add_code_sample_docstrings(
      checkpoint=_CHECKPOINT_FOR_DOC   ,				output_type=SCREAMING_SNAKE_CASE_   ,				config_class=_CONFIG_FOR_DOC   ,				modality='''vision'''   ,				expected_output=_EXPECTED_OUTPUT_SHAPE   ,				)
  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_ = None   ,				SCREAMING_SNAKE_CASE_ = None	)		->      BaseModelOutputWithPoolingAndNoAttention:





       '''simple docstring'''





       UpperCamelCase__										=      (
           output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
       )
       UpperCamelCase__										=      return_dict if return_dict is not None else self.config.use_return_dict

       UpperCamelCase__										=      self.embedder(SCREAMING_SNAKE_CASE_	)

       UpperCamelCase__										=      self.encoder(
           SCREAMING_SNAKE_CASE_   ,				output_hidden_states=SCREAMING_SNAKE_CASE_   ,				return_dict=SCREAMING_SNAKE_CASE_	)

       UpperCamelCase__										=      encoder_outputs[0]

       UpperCamelCase__										=      self.pooler(SCREAMING_SNAKE_CASE_	)

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

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



@add_start_docstrings(
    "\n    ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    "	, __UpperCamelCase	, )
class     _A   (    __UpperCamelCase							):






  def __init__(self   ,				SCREAMING_SNAKE_CASE_	)		->      List[Any]:





       '''simple docstring'''





       super().__init__(SCREAMING_SNAKE_CASE_	)
       UpperCamelCase__										=      config.num_labels
       UpperCamelCase__										=      ResNetModel(SCREAMING_SNAKE_CASE_	)
       # classification head
       UpperCamelCase__										=      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(SCREAMING_SNAKE_CASE_	)
  @add_code_sample_docstrings(
      checkpoint=_IMAGE_CLASS_CHECKPOINT   ,				output_type=SCREAMING_SNAKE_CASE_   ,				config_class=_CONFIG_FOR_DOC   ,				expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT   ,				)
  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_ = None   ,				SCREAMING_SNAKE_CASE_ = None   ,				SCREAMING_SNAKE_CASE_ = None   ,				SCREAMING_SNAKE_CASE_ = None   ,				)		->      ImageClassifierOutputWithNoAttention:





       '''simple docstring'''





       UpperCamelCase__										=      return_dict if return_dict is not None else self.config.use_return_dict

       UpperCamelCase__										=      self.resnet(SCREAMING_SNAKE_CASE_   ,				output_hidden_states=SCREAMING_SNAKE_CASE_   ,				return_dict=SCREAMING_SNAKE_CASE_	)

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

       UpperCamelCase__										=      self.classifier(SCREAMING_SNAKE_CASE_	)

       UpperCamelCase__										=      None

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

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

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



@add_start_docstrings(
    "\n    ResNet backbone, to be used with frameworks like DETR and MaskFormer.\n    "	, __UpperCamelCase	, )
class     _A   (    __UpperCamelCase	, __UpperCamelCase							):






  def __init__(self   ,				SCREAMING_SNAKE_CASE_	)		->      Union[str, Any]:





       '''simple docstring'''





       super().__init__(SCREAMING_SNAKE_CASE_	)
       super()._init_backbone(SCREAMING_SNAKE_CASE_	)

       UpperCamelCase__										=      [config.embedding_size] + config.hidden_sizes
       UpperCamelCase__										=      ResNetEmbeddings(SCREAMING_SNAKE_CASE_	)
       UpperCamelCase__										=      ResNetEncoder(SCREAMING_SNAKE_CASE_	)

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






  @add_start_docstrings_to_model_forward(SCREAMING_SNAKE_CASE_	)
  @replace_return_docstrings(output_type=SCREAMING_SNAKE_CASE_   ,				config_class=_CONFIG_FOR_DOC	)
  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_ = None   ,				SCREAMING_SNAKE_CASE_ = None	)		->      BackboneOutput:





       '''simple docstring'''





       UpperCamelCase__										=      return_dict if return_dict is not None else self.config.use_return_dict
       UpperCamelCase__										=      (
           output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
       )

       UpperCamelCase__										=      self.embedder(SCREAMING_SNAKE_CASE_	)

       UpperCamelCase__										=      self.encoder(SCREAMING_SNAKE_CASE_   ,				output_hidden_states=SCREAMING_SNAKE_CASE_   ,				return_dict=SCREAMING_SNAKE_CASE_	)

       UpperCamelCase__										=      outputs.hidden_states

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

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

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




from __future__ import annotations







def  __UpperCamelCase    (						A ,							A							):
     UpperCamelCase__										=      get_failure_array(A							)

     # 2) Step through text searching for pattern
     UpperCamelCase__						,       UpperCamelCase__										=      0, 0  # index into text, pattern
     while i < len(A							):
          if pattern[j] == text[i]:
               if j == (len(A							) - 1):
                    return True
               j += 1

          # if this is a prefix in our pattern
          # just go back far enough to continue
          elif j > 0:
               UpperCamelCase__										=      failure[j - 1]
               continue
          i += 1
     return False







def  __UpperCamelCase    (						A							):
     UpperCamelCase__										=      [0]
     UpperCamelCase__										=      0
     UpperCamelCase__										=      1
     while j < len(A							):
          if pattern[i] == pattern[j]:
               i += 1
          elif i > 0:
               UpperCamelCase__										=      failure[i - 1]
               continue
          j += 1
          failure.append(A							)
     return failure


if __name__ == "__main__":
     # Test 1)
     __magic_name__								='''abc1abc12'''
     __magic_name__								='''alskfjaldsabc1abc1abc12k23adsfabcabc'''
     __magic_name__								='''alskfjaldsk23adsfabcabc'''
     assert kmp(pattern, texta) and not kmp(pattern, texta)

     # Test 2)
     __magic_name__								='''ABABX'''
     __magic_name__								='''ABABZABABYABABX'''
     assert kmp(pattern, text)

     # Test 3)
     __magic_name__								='''AAAB'''
     __magic_name__								='''ABAAAAAB'''
     assert kmp(pattern, text)

     # Test 4)
     __magic_name__								='''abcdabcy'''
     __magic_name__								='''abcxabcdabxabcdabcdabcy'''
     assert kmp(pattern, text)

     # Test 5)
     __magic_name__								='''aabaabaaa'''
     assert get_failure_array(pattern) == [0, 1, 0, 1, 2, 3, 4, 5, 2]



