





from __future__ import annotations

import copy
import inspect
import unittest

import numpy as np

from transformers import is_tf_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_tf, slow
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
      import tensorflow as tf

      from transformers import (
          TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
          TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
          TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
          TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
          TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
          LayoutLMvaConfig,
          TFLayoutLMvaForQuestionAnswering,
          TFLayoutLMvaForSequenceClassification,
          TFLayoutLMvaForTokenClassification,
          TFLayoutLMvaModel,
      )

if is_vision_available():
      from PIL import Image

      from transformers import LayoutLMvaImageProcessor

class    _UpperCAmelCase							:
      def __init__(		self      ,       a__      ,       a__=2      ,       a__=3      ,       a__=4      ,       a__=2      ,       a__=7      ,       a__=True      ,       a__=True      ,       a__=True      ,       a__=True      ,       a__=99      ,       a__=36      ,       a__=2      ,       a__=4      ,       a__=37      ,       a__="gelu"      ,       a__=0.1      ,       a__=0.1      ,       a__=512      ,       a__=16      ,       a__=2      ,       a__=0.02      ,       a__=6      ,       a__=6      ,       a__=3      ,       a__=4      ,       a__=None      ,       a__=1000      ,       ):
         A_     :				str			      =	parent
         A_     :				Optional[Any]			      =	batch_size
         A_     :				List[Any]			      =	num_channels
         A_     :				str			      =	image_size
         A_     :				Optional[int]			      =	patch_size
         A_     :				List[Any]			      =	is_training
         A_     :				Optional[Any]			      =	use_input_mask
         A_     :				str			      =	use_token_type_ids
         A_     :				Tuple			      =	use_labels
         A_     :				Tuple			      =	vocab_size
         A_     :				List[str]			      =	hidden_size
         A_     :				List[str]			      =	num_hidden_layers
         A_     :				Optional[int]			      =	num_attention_heads
         A_     :				str			      =	intermediate_size
         A_     :				Any			      =	hidden_act
         A_     :				Tuple			      =	hidden_dropout_prob
         A_     :				List[Any]			      =	attention_probs_dropout_prob
         A_     :				Any			      =	max_position_embeddings
         A_     :				Union[str, Any]			      =	type_vocab_size
         A_     :				str			      =	type_sequence_label_size
         A_     :				Tuple			      =	initializer_range
         A_     :				Dict			      =	coordinate_size
         A_     :				Union[str, Any]			      =	shape_size
         A_     :				Tuple			      =	num_labels
         A_     :				Any			      =	num_choices
         A_     :				List[Any]			      =	scope
         A_     :				Dict			      =	range_bbox

         # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token)
         A_     :				Union[str, Any]			      =	text_seq_length
         A_     :				Dict			      =	(image_size // patch_size) ** 2 + 1
         A_     :				int			      =	self.text_seq_length + self.image_seq_length
      def 		_lowerCamelCase      (		self       ):
         A_     :				Union[str, Any]			      =	ids_tensor([self.batch_size, self.text_seq_length]      ,       self.vocab_size       )

         A_     :				List[str]			      =	ids_tensor([self.batch_size, self.text_seq_length, 4]      ,       self.range_bbox       )
         A_     :				Optional[Any]			      =	bbox.numpy()
         # Ensure that bbox is legal
         for i in range(bbox.shape[0]       ):
            for j in range(bbox.shape[1]       ):
               if bbox[i, j, 3] < bbox[i, j, 1]:
                  A_     :				Any			      =	bbox[i, j, 3]
                  A_     :				List[Any]			      =	bbox[i, j, 1]
                  A_     :				int			      =	tmp_coordinate
               if bbox[i, j, 2] < bbox[i, j, 0]:
                  A_     :				List[Any]			      =	bbox[i, j, 2]
                  A_     :				int			      =	bbox[i, j, 0]
                  A_     :				Any			      =	tmp_coordinate
         A_     :				int			      =	tf.constant(a__       )

         A_     :				Optional[Any]			      =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

         A_     :				str			      =	None
         if self.use_input_mask:
            A_     :				str			      =	random_attention_mask([self.batch_size, self.text_seq_length]       )

         A_     :				Any			      =	None
         if self.use_token_type_ids:
            A_     :				Optional[int]			      =	ids_tensor([self.batch_size, self.text_seq_length]      ,       self.type_vocab_size       )

         A_     :				Tuple			      =	None
         A_     :				Tuple			      =	None
         if self.use_labels:
            A_     :				Dict			      =	ids_tensor([self.batch_size]      ,       self.type_sequence_label_size       )
            A_     :				Optional[Any]			      =	ids_tensor([self.batch_size, self.text_seq_length]      ,       self.num_labels       )

         A_     :				Any			      =	LayoutLMvaConfig(
             vocab_size=self.vocab_size      ,       hidden_size=self.hidden_size      ,       num_hidden_layers=self.num_hidden_layers      ,       num_attention_heads=self.num_attention_heads      ,       intermediate_size=self.intermediate_size      ,       hidden_act=self.hidden_act      ,       hidden_dropout_prob=self.hidden_dropout_prob      ,       attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,       max_position_embeddings=self.max_position_embeddings      ,       type_vocab_size=self.type_vocab_size      ,       initializer_range=self.initializer_range      ,       coordinate_size=self.coordinate_size      ,       shape_size=self.shape_size      ,       input_size=self.image_size      ,       patch_size=self.patch_size      ,       )

         return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__       ):
         A_     :				Optional[Any]			      =	TFLayoutLMvaModel(config=a__       )

         # text + image
         A_     :				Union[str, Any]			      =	model(a__      ,       pixel_values=a__      ,       training=a__       )
         A_     :				Dict			      =	model(
             a__      ,       bbox=a__      ,       pixel_values=a__      ,       attention_mask=a__      ,       token_type_ids=a__      ,       training=a__      ,       )
         A_     :				Tuple			      =	model(a__      ,       bbox=a__      ,       pixel_values=a__      ,       training=a__       )

         self.parent.assertEqual(result.last_hidden_state.shape      ,       (self.batch_size, self.seq_length, self.hidden_size)       )

         # text only
         A_     :				Union[str, Any]			      =	model(a__      ,       training=a__       )
         self.parent.assertEqual(
             result.last_hidden_state.shape      ,       (self.batch_size, self.text_seq_length, self.hidden_size)       )

         # image only
         A_     :				Tuple			      =	model({"""pixel_values""": pixel_values}      ,       training=a__       )
         self.parent.assertEqual(
             result.last_hidden_state.shape      ,       (self.batch_size, self.image_seq_length, self.hidden_size)       )
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__       ):
         A_     :				Optional[int]			      =	self.num_labels
         A_     :				Tuple			      =	TFLayoutLMvaForSequenceClassification(config=a__       )
         A_     :				Optional[Any]			      =	model(
             a__      ,       bbox=a__      ,       pixel_values=a__      ,       attention_mask=a__      ,       token_type_ids=a__      ,       labels=a__      ,       training=a__      ,       )
         self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.num_labels)       )
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__       ):
         A_     :				Union[str, Any]			      =	self.num_labels
         A_     :				Union[str, Any]			      =	TFLayoutLMvaForTokenClassification(config=a__       )
         A_     :				int			      =	model(
             a__      ,       bbox=a__      ,       pixel_values=a__      ,       attention_mask=a__      ,       token_type_ids=a__      ,       labels=a__      ,       training=a__      ,       )
         self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.text_seq_length, self.num_labels)       )
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__       ):
         A_     :				Union[str, Any]			      =	2
         A_     :				List[str]			      =	TFLayoutLMvaForQuestionAnswering(config=a__       )
         A_     :				Any			      =	model(
             a__      ,       bbox=a__      ,       pixel_values=a__      ,       attention_mask=a__      ,       token_type_ids=a__      ,       start_positions=a__      ,       end_positions=a__      ,       training=a__      ,       )
         self.parent.assertEqual(result.start_logits.shape      ,       (self.batch_size, self.seq_length)       )
         self.parent.assertEqual(result.end_logits.shape      ,       (self.batch_size, self.seq_length)       )






      def 		_lowerCamelCase      (		self       ):
         A_     :				List[str]			      =	self.prepare_config_and_inputs()
         ((A_)      ,				(A_)      ,				(A_)      ,				(A_)      ,				(A_)      ,				(A_)      ,				(A_)      ,				(A_))     :				List[str]			      =	config_and_inputs
         A_     :				Any			      =	{
             """input_ids""": input_ids,
             """bbox""": bbox,
             """pixel_values""": pixel_values,
             """token_type_ids""": token_type_ids,
             """attention_mask""": input_mask,
         }
         return config, inputs_dict






@require_tf
class    _UpperCAmelCase							(		_lowerCamelCase			,       _lowerCamelCase			,       unittest.TestCase     ):
      a						       =							(
          (
              TFLayoutLMvaModel,
              TFLayoutLMvaForQuestionAnswering,
              TFLayoutLMvaForSequenceClassification,
              TFLayoutLMvaForTokenClassification,
          )
          if is_tf_available()
          else ()
      )
      a						       =							(
          {'''document-question-answering''': TFLayoutLMvaForQuestionAnswering, '''feature-extraction''': TFLayoutLMvaModel}
          if is_tf_available()
          else {}
      )

      a						       =							False
      a						       =							False
      a						       =							False
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__       ):
         return True
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__=False       ):
         A_     :				List[str]			      =	copy.deepcopy(a__       )

         if model_class in get_values(a__       ):
            A_     :				List[str]			      =	{
                k: tf.tile(tf.expand_dims(a__      ,       1       )      ,       (1, self.model_tester.num_choices) + (1,) * (v.ndim - 1)       )
                if isinstance(a__      ,       tf.Tensor       ) and v.ndim > 0
                else v
                for k, v in inputs_dict.items()
            }

         if return_labels:
            if model_class in get_values(a__       ):
               A_     :				List[str]			      =	tf.ones(self.model_tester.batch_size      ,       dtype=tf.intaa       )
            elif model_class in get_values(a__       ):
               A_     :				Tuple			      =	tf.zeros(self.model_tester.batch_size      ,       dtype=tf.intaa       )
               A_     :				int			      =	tf.zeros(self.model_tester.batch_size      ,       dtype=tf.intaa       )
            elif model_class in get_values(a__       ):
               A_     :				Tuple			      =	tf.zeros(self.model_tester.batch_size      ,       dtype=tf.intaa       )
            elif model_class in get_values(a__       ):
               A_     :				Optional[int]			      =	tf.zeros(
                   (self.model_tester.batch_size, self.model_tester.text_seq_length)      ,       dtype=tf.intaa       )

         return inputs_dict
      def 		_lowerCamelCase      (		self       ):
         A_     :				List[Any]			      =	TFLayoutLMvaModelTester(self       )
         A_     :				str			      =	ConfigTester(self      ,       config_class=a__      ,       hidden_size=37       )
      def 		_lowerCamelCase      (		self       ):
         self.config_tester.run_common_tests()
      def 		_lowerCamelCase      (		self       ):
         A_      ,				A_     :				Optional[int]			      =	self.model_tester.prepare_config_and_inputs_for_common()
         for model_class in self.all_model_classes:
            A_     :				List[Any]			      =	model_class(a__       )
            if getattr(a__      ,       """hf_compute_loss"""      ,       a__       ):
               # The number of elements in the loss should be the same as the number of elements in the label
               A_     :				str			      =	self._prepare_for_class(inputs_dict.copy()      ,       a__      ,       return_labels=a__       )
               A_     :				Tuple			      =	prepared_for_class[
                   sorted(prepared_for_class.keys() - inputs_dict.keys()      ,       reverse=a__       )[0]
               ]
               A_     :				Dict			      =	added_label.shape.as_list()[:1]

               # Test that model correctly compute the loss with kwargs
               A_     :				Optional[int]			      =	self._prepare_for_class(inputs_dict.copy()      ,       a__      ,       return_labels=a__       )
               A_     :				str			      =	prepared_for_class.pop("""input_ids"""       )

               A_     :				int			      =	model(a__      ,       **a__       )[0]
               self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]       )

               # Test that model correctly compute the loss when we mask some positions
               A_     :				List[str]			      =	self._prepare_for_class(inputs_dict.copy()      ,       a__      ,       return_labels=a__       )
               A_     :				Dict			      =	prepared_for_class.pop("""input_ids"""       )
               if "labels" in prepared_for_class:
                  A_     :				int			      =	prepared_for_class["""labels"""].numpy()
                  if len(labels.shape       ) > 1 and labels.shape[1] != 1:
                     A_     :				str			      =	-100
                     A_     :				List[str]			      =	tf.convert_to_tensor(a__       )
                     A_     :				Dict			      =	model(a__      ,       **a__       )[0]
                     self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]       )
                     self.assertTrue(not np.any(np.isnan(loss.numpy()       )       )       )

                # Test that model correctly compute the loss with a dict
               A_     :				str			      =	self._prepare_for_class(inputs_dict.copy()      ,       a__      ,       return_labels=a__       )
               A_     :				int			      =	model(a__       )[0]
               self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]       )

               # Test that model correctly compute the loss with a tuple
               A_     :				Any			      =	self._prepare_for_class(inputs_dict.copy()      ,       a__      ,       return_labels=a__       )

               # Get keys that were added with the _prepare_for_class function
               A_     :				int			      =	prepared_for_class.keys() - inputs_dict.keys()
               A_     :				Optional[int]			      =	inspect.signature(model.call       ).parameters
               A_     :				Optional[int]			      =	list(signature.keys()       )

               # Create a dictionary holding the location of the tensors in the tuple
               A_     :				Any			      =	{0: """input_ids"""}
               for label_key in label_keys:
                  A_     :				Any			      =	signature_names.index(a__       )
                  A_     :				Optional[int]			      =	label_key
               A_     :				Any			      =	sorted(tuple_index_mapping.items()       )
               # Initialize a list with their default values, update the values and convert to a tuple
               A_     :				Optional[int]			      =	[]

               for name in signature_names:
                  if name != "kwargs":
                     list_input.append(signature[name].default       )

               for index, value in sorted_tuple_index_mapping:
                  A_     :				Union[str, Any]			      =	prepared_for_class[value]

               A_     :				List[str]			      =	tuple(a__       )

               # Send to model
               A_     :				Tuple			      =	model(tuple_input[:-1]       )[0]

               self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]       )
      def 		_lowerCamelCase      (		self       ):
         (
             (
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				
         )     :				int			      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__       )
      def 		_lowerCamelCase      (		self       ):
         (
             (
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				
         )     :				List[str]			      =	self.model_tester.prepare_config_and_inputs()
         for type in ["absolute", "relative_key", "relative_key_query"]:
            A_     :				Union[str, Any]			      =	type
            self.model_tester.create_and_check_model(a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__       )
      def 		_lowerCamelCase      (		self       ):
         (
             (
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				
         )     :				Tuple			      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(
             a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__       )
      def 		_lowerCamelCase      (		self       ):
         (
             (
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				
         )     :				Dict			      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(
             a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__       )
      def 		_lowerCamelCase      (		self       ):
         (
             (
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				
         )     :				Union[str, Any]			      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(
             a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__       )






      @slow
      def 		_lowerCamelCase      (		self       ):
         for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            A_     :				List[str]			      =	TFLayoutLMvaModel.from_pretrained(a__       )
            self.assertIsNotNone(a__       )






def     _lowerCAmelCase		(  ):
   '''simple docstring'''


   A_     :				Union[str, Any]			      =	Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""     )
   return image







@require_tf
class    _UpperCAmelCase							(		unittest.TestCase     ):
      @cached_property
      def 		_lowerCamelCase      (		self       ):
         return LayoutLMvaImageProcessor(apply_ocr=a__       ) if is_vision_available() else None






      @slow
      def 		_lowerCamelCase      (		self       ):
         A_     :				int			      =	TFLayoutLMvaModel.from_pretrained("""microsoft/layoutlmv3-base"""       )

         A_     :				str			      =	self.default_image_processor
         A_     :				Any			      =	prepare_img()
         A_     :				Optional[int]			      =	image_processor(images=a__      ,       return_tensors="""tf"""       ).pixel_values

         A_     :				List[Any]			      =	tf.constant([[1, 2]]       )
         A_     :				Optional[int]			      =	tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]]       )      ,       axis=0       )

         # forward pass
         A_     :				Optional[Any]			      =	model(input_ids=a__      ,       bbox=a__      ,       pixel_values=a__      ,       training=a__       )

         # verify the logits
         A_     :				int			      =	(1, 199, 768)
         self.assertEqual(outputs.last_hidden_state.shape      ,       a__       )

         A_     :				Dict			      =	tf.constant(
             [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]]       )

         self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3]      ,       a__      ,       atol=1E-4       )       )













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


_lowerCAmelCase 				=     logging.get_logger(__name__)

# General docstring
_lowerCAmelCase 				=     """ResNetConfig"""

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

# Image classification docstring
_lowerCAmelCase 				=     """microsoft/resnet-50"""
_lowerCAmelCase 				=     """tiger cat"""

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

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






      def 		_lowerCamelCase      (		self      ,       a__       ):
         A_     :				List[str]			      =	self.convolution(a__       )
         A_     :				Optional[int]			      =	self.normalization(a__       )
         A_     :				int			      =	self.activation(a__       )
         return hidden_state






class    _UpperCAmelCase							(		nn.Module     ):
      def __init__(		self      ,       a__       ):
         super().__init__()
         A_     :				Union[str, Any]			      =	ResNetConvLayer(
             config.num_channels      ,       config.embedding_size      ,       kernel_size=7      ,       stride=2      ,       activation=config.hidden_act       )
         A_     :				Any			      =	nn.MaxPoolad(kernel_size=3      ,       stride=2      ,       padding=1       )
         A_     :				Optional[Any]			      =	config.num_channels






      def 		_lowerCamelCase      (		self      ,       a__       ):
         A_     :				Tuple			      =	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."""       )
         A_     :				str			      =	self.embedder(a__       )
         A_     :				Dict			      =	self.pooler(a__       )
         return embedding






class    _UpperCAmelCase							(		nn.Module     ):
      def __init__(		self      ,       a__      ,       a__      ,       a__ = 2       ):
         super().__init__()
         A_     :				Union[str, Any]			      =	nn.Convad(a__      ,       a__      ,       kernel_size=1      ,       stride=a__      ,       bias=a__       )
         A_     :				int			      =	nn.BatchNormad(a__       )






      def 		_lowerCamelCase      (		self      ,       a__       ):
         A_     :				List[str]			      =	self.convolution(a__       )
         A_     :				List[str]			      =	self.normalization(a__       )
         return hidden_state






class    _UpperCAmelCase							(		nn.Module     ):
      def __init__(		self      ,       a__      ,       a__      ,       a__ = 1      ,       a__ = "relu"       ):
         super().__init__()
         A_     :				Union[str, Any]			      =	in_channels != out_channels or stride != 1
         A_     :				List[Any]			      =	(
             ResNetShortCut(a__      ,       a__      ,       stride=a__       ) if should_apply_shortcut else nn.Identity()
         )
         A_     :				List[str]			      =	nn.Sequential(
             ResNetConvLayer(a__      ,       a__      ,       stride=a__       )      ,       ResNetConvLayer(a__      ,       a__      ,       activation=a__       )      ,       )
         A_     :				Any			      =	ACTaFN[activation]






      def 		_lowerCamelCase      (		self      ,       a__       ):
         A_     :				Any			      =	hidden_state
         A_     :				Optional[Any]			      =	self.layer(a__       )
         A_     :				Any			      =	self.shortcut(a__       )
         hidden_state += residual
         A_     :				Optional[Any]			      =	self.activation(a__       )
         return hidden_state






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






      def 		_lowerCamelCase      (		self      ,       a__       ):
         A_     :				int			      =	hidden_state
         A_     :				Dict			      =	self.layer(a__       )
         A_     :				Optional[Any]			      =	self.shortcut(a__       )
         hidden_state += residual
         A_     :				Dict			      =	self.activation(a__       )
         return hidden_state






class    _UpperCAmelCase							(		nn.Module     ):
      def __init__(		self      ,       a__      ,       a__      ,       a__      ,       a__ = 2      ,       a__ = 2      ,       ):
         super().__init__()

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

         A_     :				Optional[int]			      =	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 		_lowerCamelCase      (		self      ,       a__       ):
         A_     :				List[Any]			      =	input
         for layer in self.layers:
            A_     :				List[str]			      =	layer(a__       )
         return hidden_state






class    _UpperCAmelCase							(		nn.Module     ):
      def __init__(		self      ,       a__       ):
         super().__init__()
         A_     :				Any			      =	nn.ModuleList([]       )
         # based on `downsample_in_first_stage` the first layer of the first stage may or may not downsample the input
         self.stages.append(
             ResNetStage(
                 a__      ,       config.embedding_size      ,       config.hidden_sizes[0]      ,       stride=2 if config.downsample_in_first_stage else 1      ,       depth=config.depths[0]      ,       )       )
         A_     :				Any			      =	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 		_lowerCamelCase      (		self      ,       a__      ,       a__ = False      ,       a__ = True       ):
         A_     :				Union[str, Any]			      =	() if output_hidden_states else None

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

            A_     :				str			      =	stage_module(a__       )

         if output_hidden_states:
            A_     :				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    _UpperCAmelCase							(		_lowerCamelCase     ):
      a						       =							ResNetConfig
      a						       =							'''resnet'''
      a						       =							'''pixel_values'''
      a						       =							True
      def 		_lowerCamelCase      (		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 		_lowerCamelCase      (		self      ,       a__      ,       a__=False       ):
         if isinstance(a__      ,       a__       ):
            A_     :				Dict			      =	value






_lowerCAmelCase 				=     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.
"""

_lowerCAmelCase 				=     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    _UpperCAmelCase							(		_lowerCamelCase     ):
      def __init__(		self      ,       a__       ):
         super().__init__(a__       )
         A_     :				Tuple			      =	config
         A_     :				List[str]			      =	ResNetEmbeddings(a__       )
         A_     :				Optional[int]			      =	ResNetEncoder(a__       )
         A_     :				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 		_lowerCamelCase      (		self      ,       a__      ,       a__ = None      ,       a__ = None       ):
         A_     :				str			      =	(
             output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
         )
         A_     :				Dict			      =	return_dict if return_dict is not None else self.config.use_return_dict

         A_     :				Any			      =	self.embedder(a__       )

         A_     :				int			      =	self.encoder(
             a__      ,       output_hidden_states=a__      ,       return_dict=a__       )

         A_     :				Optional[Any]			      =	encoder_outputs[0]

         A_     :				List[str]			      =	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    _UpperCAmelCase							(		_lowerCamelCase     ):
      def __init__(		self      ,       a__       ):
         super().__init__(a__       )
         A_     :				Optional[Any]			      =	config.num_labels
         A_     :				Tuple			      =	ResNetModel(a__       )
         # classification head
         A_     :				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 		_lowerCamelCase      (		self      ,       a__ = None      ,       a__ = None      ,       a__ = None      ,       a__ = None      ,       ):
         A_     :				Optional[int]			      =	return_dict if return_dict is not None else self.config.use_return_dict

         A_     :				Any			      =	self.resnet(a__      ,       output_hidden_states=a__      ,       return_dict=a__       )

         A_     :				List[Any]			      =	outputs.pooler_output if return_dict else outputs[1]

         A_     :				Dict			      =	self.classifier(a__       )

         A_     :				Optional[int]			      =	None

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

         if not return_dict:
            A_     :				Optional[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.
    '''			,       _lowerCamelCase			,       )
class    _UpperCAmelCase							(		_lowerCamelCase			,       _lowerCamelCase     ):
      def __init__(		self      ,       a__       ):
         super().__init__(a__       )
         super()._init_backbone(a__       )

         A_     :				Optional[Any]			      =	[config.embedding_size] + config.hidden_sizes
         A_     :				Any			      =	ResNetEmbeddings(a__       )
         A_     :				Tuple			      =	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 		_lowerCamelCase      (		self      ,       a__      ,       a__ = None      ,       a__ = None       ):
         A_     :				List[str]			      =	return_dict if return_dict is not None else self.config.use_return_dict
         A_     :				List[str]			      =	(
             output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
         )

         A_     :				Dict			      =	self.embedder(a__       )

         A_     :				Tuple			      =	self.encoder(a__      ,       output_hidden_states=a__      ,       return_dict=a__       )

         A_     :				Dict			      =	outputs.hidden_states

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

         if not return_dict:
            A_     :				Tuple			      =	(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__      ,       )






