




import inspect
import unittest

from transformers import ConvNextConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch

  from transformers import ConvNextBackbone, ConvNextForImageClassification, ConvNextModel
  from transformers.models.convnext.modeling_convnext import CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import AutoImageProcessor


class 							UpperCamelCase_       :

    '''simple docstring'''






    def __init__(     self :      str   ,					UpperCAmelCase__ :      List[Any]   ,					UpperCAmelCase__ :      int=13   ,					UpperCAmelCase__ :      Dict=32   ,					UpperCAmelCase__ :      Any=3   ,					UpperCAmelCase__ :      Optional[Any]=4   ,					UpperCAmelCase__ :      int=[10, 20, 30, 40]   ,					UpperCAmelCase__ :      Tuple=[2, 2, 3, 2]   ,					UpperCAmelCase__ :      Union[str, Any]=True   ,					UpperCAmelCase__ :      Dict=True   ,					UpperCAmelCase__ :      List[str]=37   ,					UpperCAmelCase__ :      Optional[Any]="gelu"   ,					UpperCAmelCase__ :      Optional[int]=10   ,					UpperCAmelCase__ :      str=0.02   ,					UpperCAmelCase__ :      Optional[Any]=["stage2", "stage3", "stage4"]   ,					UpperCAmelCase__ :      Tuple=[2, 3, 4]   ,					UpperCAmelCase__ :      Any=None   ,					)		->Dict:





           '''simple docstring'''

           A__ 					=       parent
           A__ 					=       batch_size
           A__ 					=       image_size
           A__ 					=       num_channels
           A__ 					=       num_stages
           A__ 					=       hidden_sizes
           A__ 					=       depths
           A__ 					=       is_training
           A__ 					=       use_labels
           A__ 					=       intermediate_size
           A__ 					=       hidden_act
           A__ 					=       num_labels
           A__ 					=       initializer_range
           A__ 					=       out_features
           A__ 					=       out_indices
           A__ 					=       scope


    def 			SCREAMING_SNAKE_CASE				(     self :      List[Any])		->Union[str, Any]:





           '''simple docstring'''

           A__ 					=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

           A__ 					=       None
           if self.use_labels:
                  A__ 					=       ids_tensor([self.batch_size]   ,					self.num_labels)

           A__ 					=       self.get_config()
           return config, pixel_values, labels


    def 			SCREAMING_SNAKE_CASE				(     self :      Union[str, Any])		->Dict:





           '''simple docstring'''

           return ConvNextConfig(
               num_channels=self.num_channels   ,					hidden_sizes=self.hidden_sizes   ,					depths=self.depths   ,					num_stages=self.num_stages   ,					hidden_act=self.hidden_act   ,					is_decoder=UpperCAmelCase__   ,					initializer_range=self.initializer_range   ,					out_features=self.out_features   ,					out_indices=self.out_indices   ,					num_labels=self.num_labels   ,					)


    def 			SCREAMING_SNAKE_CASE				(     self :      Tuple   ,					UpperCAmelCase__ :      Dict   ,					UpperCAmelCase__ :      Optional[int]   ,					UpperCAmelCase__ :      Dict)		->Optional[int]:





           '''simple docstring'''

           A__ 					=       ConvNextModel(config=UpperCAmelCase__)
           model.to(UpperCAmelCase__)
           model.eval()
           A__ 					=       model(UpperCAmelCase__)
           # expected last hidden states: B, C, H // 32, W // 32
           self.parent.assertEqual(
               result.last_hidden_state.shape   ,					(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)   ,					)


    def 			SCREAMING_SNAKE_CASE				(     self :      List[str]   ,					UpperCAmelCase__ :      List[str]   ,					UpperCAmelCase__ :      str   ,					UpperCAmelCase__ :      List[str])		->str:





           '''simple docstring'''

           A__ 					=       ConvNextForImageClassification(UpperCAmelCase__)
           model.to(UpperCAmelCase__)
           model.eval()
           A__ 					=       model(UpperCAmelCase__   ,					labels=UpperCAmelCase__)
           self.parent.assertEqual(result.logits.shape   ,					(self.batch_size, self.num_labels))


    def 			SCREAMING_SNAKE_CASE				(     self :      Tuple   ,					UpperCAmelCase__ :      Tuple   ,					UpperCAmelCase__ :      Union[str, Any]   ,					UpperCAmelCase__ :      str)		->Optional[Any]:





           '''simple docstring'''

           A__ 					=       ConvNextBackbone(config=UpperCAmelCase__)
           model.to(UpperCAmelCase__)
           model.eval()
           A__ 					=       model(UpperCAmelCase__)

           # verify hidden states
           self.parent.assertEqual(len(result.feature_maps)   ,					len(config.out_features))
           self.parent.assertListEqual(list(result.feature_maps[0].shape)   ,					[self.batch_size, self.hidden_sizes[1], 4, 4])

           # verify channels
           self.parent.assertEqual(len(model.channels)   ,					len(config.out_features))
           self.parent.assertListEqual(model.channels   ,					config.hidden_sizes[1:])

           # verify backbone works with out_features=None
           A__ 					=       None
           A__ 					=       ConvNextBackbone(config=UpperCAmelCase__)
           model.to(UpperCAmelCase__)
           model.eval()
           A__ 					=       model(UpperCAmelCase__)

           # verify feature maps
           self.parent.assertEqual(len(result.feature_maps)   ,					1)
           self.parent.assertListEqual(list(result.feature_maps[0].shape)   ,					[self.batch_size, self.hidden_sizes[-1], 1, 1])

           # verify channels
           self.parent.assertEqual(len(model.channels)   ,					1)
           self.parent.assertListEqual(model.channels   ,					[config.hidden_sizes[-1]])





    def 			SCREAMING_SNAKE_CASE				(     self :      Dict)		->Optional[int]:





           '''simple docstring'''

           A__ 					=       self.prepare_config_and_inputs()
           A__					,				A__					,				A__ 					=       config_and_inputs
           A__ 					=       {'''pixel_values''': pixel_values}
           return config, inputs_dict


@require_torch
class 							UpperCamelCase_       (					UpperCAmelCase__						,   UpperCAmelCase__						,   unittest.TestCase	):

    '''simple docstring'''






    UpperCAmelCase__				    = (
        (
            ConvNextModel,
            ConvNextForImageClassification,
            ConvNextBackbone,
        )
        if is_torch_available()
        else ()
    )
    UpperCAmelCase__				    = (
        {'''feature-extraction''': ConvNextModel, '''image-classification''': ConvNextForImageClassification}
        if is_torch_available()
        else {}
    )

    UpperCAmelCase__				    = True
    UpperCAmelCase__				    = False
    UpperCAmelCase__				    = False
    UpperCAmelCase__				    = False
    UpperCAmelCase__				    = False


    def 			SCREAMING_SNAKE_CASE				(     self :      Dict)		->Any:





           '''simple docstring'''

           A__ 					=       ConvNextModelTester(self)
           A__ 					=       ConfigTester(self   ,					config_class=UpperCAmelCase__   ,					has_text_modality=UpperCAmelCase__   ,					hidden_size=37)


    def 			SCREAMING_SNAKE_CASE				(     self :      int)		->Dict:





           '''simple docstring'''

           self.create_and_test_config_common_properties()
           self.config_tester.create_and_test_config_to_json_string()
           self.config_tester.create_and_test_config_to_json_file()
           self.config_tester.create_and_test_config_from_and_save_pretrained()
           self.config_tester.create_and_test_config_with_num_labels()
           self.config_tester.check_config_can_be_init_without_params()
           self.config_tester.check_config_arguments_init()


    def 			SCREAMING_SNAKE_CASE				(     self :      Tuple)		->str:





           '''simple docstring'''

           return


    @unittest.skip(reason='''ConvNext does not use inputs_embeds''')
    def 			SCREAMING_SNAKE_CASE				(     self :      str)		->str:





           '''simple docstring'''

           pass


    @unittest.skip(reason='''ConvNext does not support input and output embeddings''')
    def 			SCREAMING_SNAKE_CASE				(     self :      Tuple)		->List[Any]:





           '''simple docstring'''

           pass


    @unittest.skip(reason='''ConvNext does not use feedforward chunking''')
    def 			SCREAMING_SNAKE_CASE				(     self :      int)		->List[Any]:





           '''simple docstring'''

           pass


    def 			SCREAMING_SNAKE_CASE				(     self :      List[str])		->List[Any]:





           '''simple docstring'''

           A__					,				A__ 					=       self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                  A__ 					=       model_class(UpperCAmelCase__)
                  A__ 					=       inspect.signature(model.forward)
                  # signature.parameters is an OrderedDict => so arg_names order is deterministic
                  A__ 					=       [*signature.parameters.keys()]

                  A__ 					=       ['''pixel_values''']
                  self.assertListEqual(arg_names[:1]   ,					UpperCAmelCase__)


    def 			SCREAMING_SNAKE_CASE				(     self :      int)		->List[str]:





           '''simple docstring'''

           A__ 					=       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCAmelCase__)


    def 			SCREAMING_SNAKE_CASE				(     self :      List[Any])		->int:





           '''simple docstring'''

           A__ 					=       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_backbone(*UpperCAmelCase__)


    def 			SCREAMING_SNAKE_CASE				(     self :      Any)		->Tuple:





           '''simple docstring'''

           def check_hidden_states_output(UpperCAmelCase__ :      Optional[int]   ,					UpperCAmelCase__ :      Tuple   ,					UpperCAmelCase__ :      int):
                  A__ 					=       model_class(UpperCAmelCase__)
                  model.to(UpperCAmelCase__)
                  model.eval()

                  with torch.no_grad():
                         A__ 					=       model(**self._prepare_for_class(UpperCAmelCase__   ,					UpperCAmelCase__))

                  A__ 					=       outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

                  A__ 					=       self.model_tester.num_stages
                  self.assertEqual(len(UpperCAmelCase__)   ,					expected_num_stages + 1)

                  # ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
                  self.assertListEqual(
                      list(hidden_states[0].shape[-2:])   ,					[self.model_tester.image_size // 4, self.model_tester.image_size // 4]   ,					)

           A__					,				A__ 					=       self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                  A__ 					=       True
                  check_hidden_states_output(UpperCAmelCase__   ,					UpperCAmelCase__   ,					UpperCAmelCase__)

                  # check that output_hidden_states also work using config
                  del inputs_dict["output_hidden_states"]
                  A__ 					=       True

                  check_hidden_states_output(UpperCAmelCase__   ,					UpperCAmelCase__   ,					UpperCAmelCase__)


    def 			SCREAMING_SNAKE_CASE				(     self :      Tuple)		->int:





           '''simple docstring'''

           A__ 					=       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase__)





    @slow
    def 			SCREAMING_SNAKE_CASE				(     self :      List[str])		->Optional[int]:





           '''simple docstring'''

           for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  A__ 					=       ConvNextModel.from_pretrained(UpperCAmelCase__)
                  self.assertIsNotNone(UpperCAmelCase__)







def SCREAMING_SNAKE_CASE							(			)  ->			Tuple:



       """simple docstring"""

       A__ 					=       Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
       return image


@require_torch
@require_vision
class 							UpperCamelCase_       (					unittest.TestCase	):

    '''simple docstring'''






    @cached_property
    def 			SCREAMING_SNAKE_CASE				(     self :      Optional[Any])		->Tuple:





           '''simple docstring'''

           return AutoImageProcessor.from_pretrained('''facebook/convnext-tiny-224''') if is_vision_available() else None





    @slow
    def 			SCREAMING_SNAKE_CASE				(     self :      List[Any])		->Dict:





           '''simple docstring'''

           A__ 					=       ConvNextForImageClassification.from_pretrained('''facebook/convnext-tiny-224''').to(UpperCAmelCase__)

           A__ 					=       self.default_image_processor
           A__ 					=       prepare_img()
           A__ 					=       image_processor(images=UpperCAmelCase__   ,					return_tensors='''pt''').to(UpperCAmelCase__)

           # forward pass
           with torch.no_grad():
                  A__ 					=       model(**UpperCAmelCase__)

           # verify the logits
           A__ 					=       torch.Size((1, 1_000))
           self.assertEqual(outputs.logits.shape   ,					UpperCAmelCase__)

           A__ 					=       torch.tensor([-0.0260, -0.4739, 0.1911]).to(UpperCAmelCase__)

           self.assertTrue(torch.allclose(outputs.logits[0, :3]   ,					UpperCAmelCase__   ,					atol=1e-4))


@require_torch
class 							UpperCamelCase_       (					unittest.TestCase						,   UpperCAmelCase__	):

    '''simple docstring'''






    UpperCAmelCase__				    = (ConvNextBackbone,) if is_torch_available() else ()
    UpperCAmelCase__				    = ConvNextConfig

    UpperCAmelCase__				    = False


    def 			SCREAMING_SNAKE_CASE				(     self :      Any)		->Optional[int]:





           '''simple docstring'''

           A__ 					=       ConvNextModelTester(self)










from __future__ import annotations
class 		SCREAMING_SNAKE_CASE__	:

  '''simple docstring'''







  def __init__(      self		:			Optional[Any]       ,				lowerCamelCase		:			list[list[int]]							)						->	Any:


         """simple docstring"""
         _UpperCAmelCase							=    TypeError(
             """Matrices must be formed from a list of zero or more lists containing at """
             """least one and the same number of values, each of which must be of type """
             """int or float."""							)
         if len(lowerCamelCase							) != 0:
                _UpperCAmelCase							=    len(rows[0]							)
                if cols == 0:
                       raise error
                for row in rows:
                       if len(lowerCamelCase							) != cols:
                              raise error
                       for value in row:
                              if not isinstance(lowerCamelCase       ,				(int, float)							):
                                     raise error
                _UpperCAmelCase							=    rows
         else:
                _UpperCAmelCase							=    []


  def  lowerCamelCase       (      self		:			Optional[Any]							)						->	list[list[int]]:


         """simple docstring"""
         return [[row[i] for row in self.rows] for i in range(len(self.rows[0]							)							)]


  @property
  def  lowerCamelCase       (      self		:			int							)						->	int:


         """simple docstring"""
         return len(self.rows							)


  @property
  def  lowerCamelCase       (      self		:			int							)						->	int:


         """simple docstring"""
         return len(self.rows[0]							)


  @property
  def  lowerCamelCase       (      self		:			Tuple							)						->	tuple[int, int]:


         """simple docstring"""
         return (self.num_rows, self.num_columns)


  @property
  def  lowerCamelCase       (      self		:			Tuple							)						->	bool:


         """simple docstring"""
         return self.order[0] == self.order[1]


  def  lowerCamelCase       (      self		:			Optional[int]							)						->	Matrix:


         """simple docstring"""
         _UpperCAmelCase							=    [
             [0 if column_num != row_num else 1 for column_num in range(self.num_rows							)]
             for row_num in range(self.num_rows							)
         ]
         return Matrix(lowerCamelCase							)


  def  lowerCamelCase       (      self		:			Optional[Any]							)						->	int:


         """simple docstring"""
         if not self.is_square:
                return 0
         if self.order == (0, 0):
                return 1
         if self.order == (1, 1):
                return int(self.rows[0][0]							)
         if self.order == (2, 2):
                return int(
                    (self.rows[0][0] * self.rows[1][1])
                    - (self.rows[0][1] * self.rows[1][0])							)
         else:
                return sum(
                    self.rows[0][column] * self.cofactors().rows[0][column]
                    for column in range(self.num_columns							)							)


  def  lowerCamelCase       (      self		:			Optional[Any]							)						->	bool:


         """simple docstring"""
         return bool(self.determinant()							)


  def  lowerCamelCase       (      self		:			Tuple       ,				lowerCamelCase		:			int       ,				lowerCamelCase		:			int							)						->	int:


         """simple docstring"""
         _UpperCAmelCase							=    [
             [
                 self.rows[other_row][other_column]
                 for other_column in range(self.num_columns							)
                 if other_column != column
             ]
             for other_row in range(self.num_rows							)
             if other_row != row
         ]
         return Matrix(lowerCamelCase							).determinant()


  def  lowerCamelCase       (      self		:			int       ,				lowerCamelCase		:			int       ,				lowerCamelCase		:			int							)						->	int:


         """simple docstring"""
         if (row + column) % 2 == 0:
                return self.get_minor(lowerCamelCase       ,				lowerCamelCase							)
         return -1 * self.get_minor(lowerCamelCase       ,				lowerCamelCase							)


  def  lowerCamelCase       (      self		:			str							)						->	Matrix:


         """simple docstring"""
         return Matrix(
             [
                 [self.get_minor(lowerCamelCase       ,				lowerCamelCase							) for column in range(self.num_columns							)]
                 for row in range(self.num_rows							)
             ]							)


  def  lowerCamelCase       (      self		:			Dict							)						->	Matrix:


         """simple docstring"""
         return Matrix(
             [
                 [
                     self.minors().rows[row][column]
                     if (row + column) % 2 == 0
                     else self.minors().rows[row][column] * -1
                     for column in range(self.minors().num_columns							)
                 ]
                 for row in range(self.minors().num_rows							)
             ]							)


  def  lowerCamelCase       (      self		:			Optional[Any]							)						->	Matrix:


         """simple docstring"""
         _UpperCAmelCase							=    [
             [self.cofactors().rows[column][row] for column in range(self.num_columns							)]
             for row in range(self.num_rows							)
         ]
         return Matrix(lowerCamelCase							)


  def  lowerCamelCase       (      self		:			Any							)						->	Matrix:


         """simple docstring"""
         _UpperCAmelCase							=    self.determinant()
         if not determinant:
                raise TypeError("""Only matrices with a non-zero determinant have an inverse"""							)
         return self.adjugate() * (1 / determinant)


  def __repr__(      self		:			List[str]							)						->	str:


         """simple docstring"""
         return str(self.rows							)


  def __str__(      self		:			Optional[Any]							)						->	str:


         """simple docstring"""
         if self.num_rows == 0:
                return "[]"
         if self.num_rows == 1:
                return "[[" + ". ".join(str(self.rows[0]							)							) + "]]"
         return (
             "["
             + "\n ".join(
                 [
                     """[""" + """. """.join([str(lowerCamelCase							) for value in row]							) + """.]"""
                     for row in self.rows
                 ]							)
             + "]"
         )


  def  lowerCamelCase       (      self		:			Tuple       ,				lowerCamelCase		:			list[int]       ,				lowerCamelCase		:			int | None = None							)						->	None:


         """simple docstring"""
         _UpperCAmelCase							=    TypeError("""Row must be a list containing all ints and/or floats"""							)
         if not isinstance(lowerCamelCase       ,				lowerCamelCase							):
                raise type_error
         for value in row:
                if not isinstance(lowerCamelCase       ,				(int, float)							):
                       raise type_error
         if len(lowerCamelCase							) != self.num_columns:
                raise ValueError(
                    """Row must be equal in length to the other rows in the matrix"""							)
         if position is None:
                self.rows.append(lowerCamelCase							)
         else:
                _UpperCAmelCase							=    self.rows[0:position] + [row] + self.rows[position:]


  def  lowerCamelCase       (      self		:			List[Any]       ,				lowerCamelCase		:			list[int]       ,				lowerCamelCase		:			int | None = None							)						->	None:


         """simple docstring"""
         _UpperCAmelCase							=    TypeError(
             """Column must be a list containing all ints and/or floats"""							)
         if not isinstance(lowerCamelCase       ,				lowerCamelCase							):
                raise type_error
         for value in column:
                if not isinstance(lowerCamelCase       ,				(int, float)							):
                       raise type_error
         if len(lowerCamelCase							) != self.num_rows:
                raise ValueError(
                    """Column must be equal in length to the other columns in the matrix"""							)
         if position is None:
                _UpperCAmelCase							=    [self.rows[i] + [column[i]] for i in range(self.num_rows							)]
         else:
                _UpperCAmelCase							=    [
                    self.rows[i][0:position] + [column[i]] + self.rows[i][position:]
                    for i in range(self.num_rows							)
                ]


  def __eq__(      self		:			Dict       ,				lowerCamelCase		:			object							)						->	bool:


         """simple docstring"""
         if not isinstance(lowerCamelCase       ,				lowerCamelCase							):
                return NotImplemented
         return self.rows == other.rows


  def __ne__(      self		:			List[str]       ,				lowerCamelCase		:			object							)						->	bool:


         """simple docstring"""
         return not self == other


  def __neg__(      self		:			Union[str, Any]							)						->	Matrix:


         """simple docstring"""
         return self * -1


  def __add__(      self		:			Any       ,				lowerCamelCase		:			Matrix							)						->	Matrix:


         """simple docstring"""
         if self.order != other.order:
                raise ValueError("""Addition requires matrices of the same order"""							)
         return Matrix(
             [
                 [self.rows[i][j] + other.rows[i][j] for j in range(self.num_columns							)]
                 for i in range(self.num_rows							)
             ]							)


  def __sub__(      self		:			Optional[Any]       ,				lowerCamelCase		:			Matrix							)						->	Matrix:


         """simple docstring"""
         if self.order != other.order:
                raise ValueError("""Subtraction requires matrices of the same order"""							)
         return Matrix(
             [
                 [self.rows[i][j] - other.rows[i][j] for j in range(self.num_columns							)]
                 for i in range(self.num_rows							)
             ]							)


  def __mul__(      self		:			List[Any]       ,				lowerCamelCase		:			Matrix | int | float							)						->	Matrix:


         """simple docstring"""
         if isinstance(lowerCamelCase       ,				(int, float)							):
                return Matrix(
                    [[int(element * other							) for element in row] for row in self.rows]							)
         elif isinstance(lowerCamelCase       ,				lowerCamelCase							):
                if self.num_columns != other.num_rows:
                       raise ValueError(
                           """The number of columns in the first matrix must """
                           """be equal to the number of rows in the second"""							)
                return Matrix(
                    [
                        [Matrix.dot_product(lowerCamelCase       ,				lowerCamelCase							) for column in other.columns()]
                        for row in self.rows
                    ]							)
         else:
                raise TypeError(
                    """A Matrix can only be multiplied by an int, float, or another matrix"""							)


  def __pow__(      self		:			Any       ,				lowerCamelCase		:			int							)						->	Matrix:


         """simple docstring"""
         if not isinstance(lowerCamelCase       ,				lowerCamelCase							):
                raise TypeError("""A Matrix can only be raised to the power of an int"""							)
         if not self.is_square:
                raise ValueError("""Only square matrices can be raised to a power"""							)
         if other == 0:
                return self.identity()
         if other < 0:
                if self.is_invertable():
                       return self.inverse() ** (-other)
                raise ValueError(
                    """Only invertable matrices can be raised to a negative power"""							)
         _UpperCAmelCase							=    self
         for _ in range(other - 1							):
                result *= self
         return result





  @classmethod
  def  lowerCamelCase       (      cls		:			Tuple       ,				lowerCamelCase		:			list[int]       ,				lowerCamelCase		:			list[int]							)						->	int:


         """simple docstring"""
         return sum(row[i] * column[i] for i in range(len(lowerCamelCase							)							)							)


if __name__ == "__main__":
  import doctest

  doctest.testmod()