


import inspect
import unittest

from huggingface_hub import hf_hub_download

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

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


if is_torch_available():
    import torch

    from transformers import UperNetForSemanticSegmentation
    from transformers.models.upernet.modeling_upernet import UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image

    from transformers import AutoImageProcessor


class     lowercase__  :






  def __init__(					self      :					Any		,						_lowercase      :					Any		,						_lowercase      :					List[str]=13		,						_lowercase      :					List[str]=32		,						_lowercase      :					Tuple=3		,						_lowercase      :					Dict=4		,						_lowercase      :					Optional[int]=[10, 20, 30, 40]		,						_lowercase      :					List[str]=[2, 2, 3, 2]		,						_lowercase      :					Dict=True		,						_lowercase      :					Optional[Any]=True		,						_lowercase      :					List[Any]=37		,						_lowercase      :					str="gelu"		,						_lowercase      :					int=10		,						_lowercase      :					Any=0.0_2		,						_lowercase      :					Union[str, Any]=["stage2", "stage3", "stage4"]		,						_lowercase      :					Any=3		,						_lowercase      :					int=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__										=     type_sequence_label_size
         UpperCAmelCase__										=     initializer_range
         UpperCAmelCase__										=     out_features
         UpperCAmelCase__										=     num_labels
         UpperCAmelCase__										=     scope
         UpperCAmelCase__										=     num_stages






  def 			_UpperCAmelCase  (					self      :					Optional[int] ):




         """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.type_sequence_label_size )

         UpperCAmelCase__										=     self.get_config()

         return config, pixel_values, labels






  def 			_UpperCAmelCase  (					self      :					List[str] ):




         """simple docstring"""







         return ConvNextConfig(
             num_channels=self.num_channels		,						num_stages=self.num_stages		,						hidden_sizes=self.hidden_sizes		,						depths=self.depths		,						is_training=self.is_training		,						intermediate_size=self.intermediate_size		,						hidden_act=self.hidden_act		,						out_features=self.out_features		,						)






  def 			_UpperCAmelCase  (					self      :					Any ):




         """simple docstring"""







         return UperNetConfig(
             backbone_config=self.get_backbone_config()		,						hidden_size=5_12		,						pool_scales=[1, 2, 3, 6]		,						use_auxiliary_head=_lowercase		,						auxiliary_loss_weight=0.4		,						auxiliary_in_channels=40		,						auxiliary_channels=2_56		,						auxiliary_num_convs=1		,						auxiliary_concat_input=_lowercase		,						loss_ignore_index=2_55		,						num_labels=self.num_labels		,						)






  def 			_UpperCAmelCase  (					self      :					Dict		,						_lowercase      :					List[str]		,						_lowercase      :					Tuple		,						_lowercase      :					List[str] ):




         """simple docstring"""







         UpperCAmelCase__										=     UperNetForSemanticSegmentation(config=_lowercase )
         model.to(_lowercase )
         model.eval()
         UpperCAmelCase__										=     model(_lowercase )
         self.parent.assertEqual(
             result.logits.shape		,						(self.batch_size, self.num_labels, self.image_size, self.image_size) )






  def 			_UpperCAmelCase  (					self      :					List[str] ):




         """simple docstring"""







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




@require_torch
class     lowercase__  (					__SCREAMING_SNAKE_CASE					,							__SCREAMING_SNAKE_CASE					,							unittest.TestCase		):
  A__=  (UperNetForSemanticSegmentation,) if is_torch_available() else ()
  A__=  {'image-segmentation': UperNetForSemanticSegmentation} if is_torch_available() else {}
  A__=  False
  A__=  False
  A__=  False
  A__=  False
  A__=  False
  A__=  False






  def 			_UpperCAmelCase  (					self      :					List[str] ):




         """simple docstring"""







         UpperCAmelCase__										=     UperNetModelTester(self )
         UpperCAmelCase__										=     ConfigTester(self		,						config_class=_lowercase		,						has_text_modality=_lowercase		,						hidden_size=37 )






  def 			_UpperCAmelCase  (					self      :					List[str] ):




         """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      :					Optional[Any] ):




         """simple docstring"""







         return






  def 			_UpperCAmelCase  (					self      :					Any ):




         """simple docstring"""







         UpperCAmelCase__						,			UpperCAmelCase__										=     self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
                UpperCAmelCase__										=     model_class(_lowercase )
                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]		,						_lowercase )






  def 			_UpperCAmelCase  (					self      :					int ):




         """simple docstring"""







         UpperCAmelCase__										=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_semantic_segmentation(*_lowercase )






  @unittest.skip(reason="UperNet does not use inputs_embeds" )
  def 			_UpperCAmelCase  (					self      :					Optional[Any] ):




         """simple docstring"""







         pass






  @unittest.skip(reason="UperNet does not support input and output embeddings" )
  def 			_UpperCAmelCase  (					self      :					Dict ):




         """simple docstring"""







         pass






  @unittest.skip(reason="UperNet does not have a base model" )
  def 			_UpperCAmelCase  (					self      :					Any ):




         """simple docstring"""







         pass






  @unittest.skip(reason="UperNet does not have a base model" )
  def 			_UpperCAmelCase  (					self      :					List[Any] ):




         """simple docstring"""







         pass






  @require_torch_multi_gpu
  @unittest.skip(reason="UperNet has some layers using `add_module` which doesn't work well with `nn.DataParallel`" )
  def 			_UpperCAmelCase  (					self      :					Optional[int] ):




         """simple docstring"""







         pass






  @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." )
  def 			_UpperCAmelCase  (					self      :					str ):




         """simple docstring"""







         pass






  def 			_UpperCAmelCase  (					self      :					Any ):




         """simple docstring"""







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

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

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

                UpperCAmelCase__										=     self.model_tester.num_stages
                self.assertEqual(len(_lowercase )		,						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__						,			UpperCAmelCase__										=     self.model_tester.prepare_config_and_inputs_for_common()

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

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

                check_hidden_states_output(_lowercase		,						_lowercase		,						_lowercase )






  def 			_UpperCAmelCase  (					self      :					Tuple ):




         """simple docstring"""







         UpperCAmelCase__						,			UpperCAmelCase__										=     self.model_tester.prepare_config_and_inputs_for_common()

         UpperCAmelCase__										=     _config_zero_init(_lowercase )
         UpperCAmelCase__										=     _config_zero_init(configs_no_init.backbone_config )
         for model_class in self.all_model_classes:
                UpperCAmelCase__										=     model_class(config=_lowercase )
                for name, param in model.named_parameters():
                       if param.requires_grad:
                              self.assertIn(
                                  ((param.data.mean() * 1E9).round() / 1E9).item()		,						[0.0, 1.0]		,						msg=F"""Parameter {name} of model {model_class} seems not properly initialized"""		,						)






  @unittest.skip(reason="UperNet does not have tied weights" )
  def 			_UpperCAmelCase  (					self      :					List[Any] ):




         """simple docstring"""







         pass






  @slow
  def 			_UpperCAmelCase  (					self      :					int ):




         """simple docstring"""







         for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                UpperCAmelCase__										=     UperNetForSemanticSegmentation.from_pretrained(_lowercase )
                self.assertIsNotNone(_lowercase )





def __UpperCAmelCase       (		)						->					Tuple:



       '''simple docstring'''





       UpperCAmelCase__										=     hf_hub_download(
           repo_id="hf-internal-testing/fixtures_ade20k" ,						repo_type="dataset" ,						filename="ADE_val_00000001.jpg"			)
       UpperCAmelCase__										=     Image.open(__A			).convert("RGB"			)
       return image




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






  def 			_UpperCAmelCase  (					self      :					Optional[int] ):




         """simple docstring"""







         UpperCAmelCase__										=     AutoImageProcessor.from_pretrained("openmmlab/upernet-swin-tiny" )
         UpperCAmelCase__										=     UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-swin-tiny" ).to(_lowercase )

         UpperCAmelCase__										=     prepare_img()
         UpperCAmelCase__										=     processor(images=_lowercase		,						return_tensors="pt" ).to(_lowercase )

         with torch.no_grad():
                UpperCAmelCase__										=     model(**_lowercase )

         UpperCAmelCase__										=     torch.Size((1, model.config.num_labels, 5_12, 5_12) )
         self.assertEqual(outputs.logits.shape		,						_lowercase )

         UpperCAmelCase__										=     torch.tensor(
             [[-7.5_9_5_8, -7.5_9_5_8, -7.4_3_0_2], [-7.5_9_5_8, -7.5_9_5_8, -7.4_3_0_2], [-7.4_7_9_7, -7.4_7_9_7, -7.3_0_6_8]] ).to(_lowercase )
         self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]		,						_lowercase		,						atol=1E-4 ) )






  def 			_UpperCAmelCase  (					self      :					List[Any] ):




         """simple docstring"""







         UpperCAmelCase__										=     AutoImageProcessor.from_pretrained("openmmlab/upernet-convnext-tiny" )
         UpperCAmelCase__										=     UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-convnext-tiny" ).to(_lowercase )

         UpperCAmelCase__										=     prepare_img()
         UpperCAmelCase__										=     processor(images=_lowercase		,						return_tensors="pt" ).to(_lowercase )

         with torch.no_grad():
                UpperCAmelCase__										=     model(**_lowercase )

         UpperCAmelCase__										=     torch.Size((1, model.config.num_labels, 5_12, 5_12) )
         self.assertEqual(outputs.logits.shape		,						_lowercase )

         UpperCAmelCase__										=     torch.tensor(
             [[-8.8_1_1_0, -8.8_1_1_0, -8.6_5_2_1], [-8.8_1_1_0, -8.8_1_1_0, -8.6_5_2_1], [-8.7_7_4_6, -8.7_7_4_6, -8.6_1_3_0]] ).to(_lowercase )
         self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]		,						_lowercase		,						atol=1E-4 ) )









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





