


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     lowercase__  :






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




         """simple docstring"""







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






  def 			_UpperCAmelCase  (					self      :					Any ):




         """simple docstring"""







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

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

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






  def 			_UpperCAmelCase  (					self      :					Optional[int] ):




         """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=_a		,						initializer_range=self.initializer_range		,						out_features=self.out_features		,						out_indices=self.out_indices		,						num_labels=self.num_labels		,						)






  def 			_UpperCAmelCase  (					self      :					List[str]		,						_lowercase      :					Union[str, Any]		,						_lowercase      :					Optional[int]		,						_lowercase      :					int ):




         """simple docstring"""







         UpperCAmelCase__										=     ConvNextModel(config=_a )
         model.to(_a )
         model.eval()
         UpperCAmelCase__										=     model(_a )
         # 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 			_UpperCAmelCase  (					self      :					Union[str, Any]		,						_lowercase      :					Union[str, Any]		,						_lowercase      :					List[Any]		,						_lowercase      :					Optional[Any] ):




         """simple docstring"""







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






  def 			_UpperCAmelCase  (					self      :					Dict		,						_lowercase      :					Optional[int]		,						_lowercase      :					Optional[Any]		,						_lowercase      :					Union[str, Any] ):




         """simple docstring"""







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

         # 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
         UpperCAmelCase__										=     None
         UpperCAmelCase__										=     ConvNextBackbone(config=_a )
         model.to(_a )
         model.eval()
         UpperCAmelCase__										=     model(_a )

         # 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 			_UpperCAmelCase  (					self      :					str ):




         """simple docstring"""







         UpperCAmelCase__										=     self.prepare_config_and_inputs()
         UpperCAmelCase__										=     config_and_inputs
         UpperCAmelCase__										=     {"""pixel_values""": pixel_values}
         return config, inputs_dict




@require_torch
class     lowercase__  (					UpperCamelCase__					,							UpperCamelCase__					,							unittest.TestCase		):
  A__=  (
      (
          ConvNextModel,
          ConvNextForImageClassification,
          ConvNextBackbone,
      )
      if is_torch_available()
      else ()
  )
  A__=  (
      {'feature-extraction': ConvNextModel, 'image-classification': ConvNextForImageClassification}
      if is_torch_available()
      else {}
  )

  A__=  True
  A__=  False
  A__=  False
  A__=  False
  A__=  False






  def 			_UpperCAmelCase  (					self      :					Union[str, Any] ):




         """simple docstring"""







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






  def 			_UpperCAmelCase  (					self      :					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 			_UpperCAmelCase  (					self      :					Dict ):




         """simple docstring"""







         return






  @unittest.skip(reason="ConvNext does not use inputs_embeds" )
  def 			_UpperCAmelCase  (					self      :					str ):




         """simple docstring"""







         pass






  @unittest.skip(reason="ConvNext does not support input and output embeddings" )
  def 			_UpperCAmelCase  (					self      :					Optional[int] ):




         """simple docstring"""







         pass






  @unittest.skip(reason="ConvNext does not use feedforward chunking" )
  def 			_UpperCAmelCase  (					self      :					Tuple ):




         """simple docstring"""







         pass






  def 			_UpperCAmelCase  (					self      :					Tuple ):




         """simple docstring"""







         UpperCAmelCase__										=     self.model_tester.prepare_config_and_inputs_for_common()

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

                UpperCAmelCase__										=     ["""pixel_values"""]
                self.assertListEqual(arg_names[:1]		,						_a )






  def 			_UpperCAmelCase  (					self      :					Dict ):




         """simple docstring"""







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






  def 			_UpperCAmelCase  (					self      :					Dict ):




         """simple docstring"""







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






  def 			_UpperCAmelCase  (					self      :					Tuple ):




         """simple docstring"""







         def check_hidden_states_output(_lowercase      :					List[Any]		,						_lowercase      :					Dict		,						_lowercase      :					Optional[int] ):
                UpperCAmelCase__										=     model_class(_a )
                model.to(_a )
                model.eval()

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

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

                UpperCAmelCase__										=     self.model_tester.num_stages
                self.assertEqual(len(_a )		,						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]		,						)

         UpperCAmelCase__										=     self.model_tester.prepare_config_and_inputs_for_common()

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

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

                check_hidden_states_output(_a		,						_a		,						_a )






  def 			_UpperCAmelCase  (					self      :					List[Any] ):




         """simple docstring"""







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






  @slow
  def 			_UpperCAmelCase  (					self      :					Dict ):




         """simple docstring"""







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





def __UpperCAmelCase       (		)						->					Optional[int]:



       '''simple docstring'''





       UpperCAmelCase__										=     Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"			)
       return image


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






  @cached_property
  def 			_UpperCAmelCase  (					self      :					int ):




         """simple docstring"""







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






  @slow
  def 			_UpperCAmelCase  (					self      :					List[Any] ):




         """simple docstring"""







         UpperCAmelCase__										=     ConvNextForImageClassification.from_pretrained("facebook/convnext-tiny-224" ).to(_a )

         UpperCAmelCase__										=     self.default_image_processor
         UpperCAmelCase__										=     prepare_img()
         UpperCAmelCase__										=     image_processor(images=_a		,						return_tensors="pt" ).to(_a )

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

         # verify the logits
         UpperCAmelCase__										=     torch.Size((1, 10_00) )
         self.assertEqual(outputs.logits.shape		,						_a )

         UpperCAmelCase__										=     torch.tensor([-0.0_2_6_0, -0.4_7_3_9, 0.1_9_1_1] ).to(_a )

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




@require_torch
class     lowercase__  (					unittest.TestCase					,							UpperCamelCase__		):
  A__=  (ConvNextBackbone,) if is_torch_available() else ()
  A__=  ConvNextConfig

  A__=  False






  def 			_UpperCAmelCase  (					self      :					List[str] ):




         """simple docstring"""







         UpperCAmelCase__										=     ConvNextModelTester(self )









from __future__ import annotations


class     lowercase__  :






  def __init__(					self      :					int		,						_lowercase      :					list[list[int]] ):




         """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(_lowercase ) != 0:
                UpperCAmelCase__										=     len(rows[0] )
                if cols == 0:
                       raise error
                for row in rows:
                       if len(_lowercase ) != cols:
                              raise error
                       for value in row:
                              if not isinstance(_lowercase		,						(int, float) ):
                                     raise error
                UpperCAmelCase__										=     rows
         else:
                UpperCAmelCase__										=     []






  def 			_UpperCAmelCase  (					self      :					Dict ):




         """simple docstring"""







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






  @property
  def 			_UpperCAmelCase  (					self      :					str ):




         """simple docstring"""







         return len(self.rows )






  @property
  def 			_UpperCAmelCase  (					self      :					Optional[Any] ):




         """simple docstring"""







         return len(self.rows[0] )






  @property
  def 			_UpperCAmelCase  (					self      :					Optional[Any] ):




         """simple docstring"""







         return (self.num_rows, self.num_columns)






  @property
  def 			_UpperCAmelCase  (					self      :					Tuple ):




         """simple docstring"""







         return self.order[0] == self.order[1]






  def 			_UpperCAmelCase  (					self      :					Optional[Any] ):




         """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(_lowercase )






  def 			_UpperCAmelCase  (					self      :					Tuple ):




         """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 			_UpperCAmelCase  (					self      :					Any ):




         """simple docstring"""







         return bool(self.determinant() )






  def 			_UpperCAmelCase  (					self      :					Optional[Any]		,						_lowercase      :					int		,						_lowercase      :					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(_lowercase ).determinant()






  def 			_UpperCAmelCase  (					self      :					str		,						_lowercase      :					int		,						_lowercase      :					int ):




         """simple docstring"""







         if (row + column) % 2 == 0:
                return self.get_minor(_lowercase		,						_lowercase )
         return -1 * self.get_minor(_lowercase		,						_lowercase )






  def 			_UpperCAmelCase  (					self      :					Optional[Any] ):




         """simple docstring"""







         return Matrix(
             [
                 [self.get_minor(_lowercase		,						_lowercase ) for column in range(self.num_columns )]
                 for row in range(self.num_rows )
             ] )






  def 			_UpperCAmelCase  (					self      :					List[str] ):




         """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 			_UpperCAmelCase  (					self      :					str ):




         """simple docstring"""







         UpperCAmelCase__										=     [
             [self.cofactors().rows[column][row] for column in range(self.num_columns )]
             for row in range(self.num_rows )
         ]
         return Matrix(_lowercase )






  def 			_UpperCAmelCase  (					self      :					List[str] ):




         """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      :					Dict ):




         """simple docstring"""







         return str(self.rows )






  def __str__(					self      :					Tuple ):




         """simple docstring"""







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






  def 			_UpperCAmelCase  (					self      :					Any		,						_lowercase      :					list[int]		,						_lowercase      :					int | None = None ):




         """simple docstring"""







         UpperCAmelCase__										=     TypeError("Row must be a list containing all ints and/or floats" )
         if not isinstance(_lowercase		,						_lowercase ):
                raise type_error
         for value in row:
                if not isinstance(_lowercase		,						(int, float) ):
                       raise type_error
         if len(_lowercase ) != 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(_lowercase )
         else:
                UpperCAmelCase__										=     self.rows[0:position] + [row] + self.rows[position:]






  def 			_UpperCAmelCase  (					self      :					Optional[Any]		,						_lowercase      :					list[int]		,						_lowercase      :					int | None = None ):




         """simple docstring"""







         UpperCAmelCase__										=     TypeError(
             "Column must be a list containing all ints and/or floats" )
         if not isinstance(_lowercase		,						_lowercase ):
                raise type_error
         for value in column:
                if not isinstance(_lowercase		,						(int, float) ):
                       raise type_error
         if len(_lowercase ) != 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		,						_lowercase      :					object ):




         """simple docstring"""







         if not isinstance(_lowercase		,						_lowercase ):
                return NotImplemented
         return self.rows == other.rows






  def __ne__(					self      :					List[Any]		,						_lowercase      :					object ):




         """simple docstring"""







         return not self == other






  def __neg__(					self      :					int ):




         """simple docstring"""







         return self * -1






  def __add__(					self      :					List[Any]		,						_lowercase      :					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[int]		,						_lowercase      :					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      :					int		,						_lowercase      :					Matrix | int | float ):




         """simple docstring"""







         if isinstance(_lowercase		,						(int, float) ):
                return Matrix(
                    [[int(element * other ) for element in row] for row in self.rows] )
         elif isinstance(_lowercase		,						_lowercase ):
                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(_lowercase		,						_lowercase ) 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      :					Optional[Any]		,						_lowercase      :					int ):




         """simple docstring"""







         if not isinstance(_lowercase		,						_lowercase ):
                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 			_UpperCAmelCase  (					cls      :					List[Any]		,						_lowercase      :					list[int]		,						_lowercase      :					list[int] ):




         """simple docstring"""







         return sum(row[i] * column[i] for i in range(len(_lowercase ) ) )


if __name__ == "__main__":
    import doctest

    doctest.testmod()





