


'''simple docstring'''
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     _lowerCamelCase       :




 '''simple docstring'''


 def __init__(   self   ,   __lowercase   ,   __lowercase=13   ,   __lowercase=32   ,   __lowercase=3   ,   __lowercase=4   ,   __lowercase=[10, 20, 30, 40]   ,   __lowercase=[2, 2, 3, 2]   ,   __lowercase=True   ,   __lowercase=True   ,   __lowercase=37   ,   __lowercase="gelu"   ,   __lowercase=10   ,   __lowercase=0.0_2   ,   __lowercase=["stage2", "stage3", "stage4"]   ,   __lowercase=3   ,   __lowercase=None   ,   ):

  """simple docstring"""







  __A	:		int	  =      parent
  __A	:		List[Any]	  =      batch_size
  __A	:		Any	  =      image_size
  __A	:		str	  =      num_channels
  __A	:		Any	  =      num_stages
  __A	:		Tuple	  =      hidden_sizes
  __A	:		Tuple	  =      depths
  __A	:		Optional[int]	  =      is_training
  __A	:		str	  =      use_labels
  __A	:		Optional[Any]	  =      intermediate_size
  __A	:		str	  =      hidden_act
  __A	:		List[str]	  =      type_sequence_label_size
  __A	:		Dict	  =      initializer_range
  __A	:		Optional[Any]	  =      out_features
  __A	:		Any	  =      num_labels
  __A	:		Dict	  =      scope
  __A	:		Any	  =      num_stages


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Union[str, Any]	  =      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

  __A	:		Tuple	  =      None
  if self.use_labels:
   __A	:		Tuple	  =      ids_tensor([self.batch_size]   ,   self.type_sequence_label_size    )

  __A	:		List[Any]	  =      self.get_config()

  return config, pixel_values, labels


 def 			snake_case__ (   self    ):

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

  """simple docstring"""







  return UperNetConfig(
      backbone_config=self.get_backbone_config()   ,   hidden_size=512   ,   pool_scales=[1, 2, 3, 6]   ,   use_auxiliary_head=__lowercase   ,   auxiliary_loss_weight=0.4   ,   auxiliary_in_channels=40   ,   auxiliary_channels=256   ,   auxiliary_num_convs=1   ,   auxiliary_concat_input=__lowercase   ,   loss_ignore_index=255   ,   num_labels=self.num_labels   ,   )


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase   ,   __lowercase    ):

  """simple docstring"""







  __A	:		Any	  =      UperNetForSemanticSegmentation(config=__lowercase    )
  model.to(__lowercase    )
  model.eval()
  __A	:		List[Any]	  =      model(__lowercase    )
  self.parent.assertEqual(
      result.logits.shape   ,   (self.batch_size, self.num_labels, self.image_size, self.image_size)    )







 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Dict	  =      self.prepare_config_and_inputs()
  (
      (
      __A
  )  ,(
      __A
  )  ,(
      __A
  )  ,
  )	:		Union[str, Any]	  =      config_and_inputs
  __A	:		List[str]	  =      {'pixel_values': pixel_values}
  return config, inputs_dict


@require_torch
class     _lowerCamelCase       (     snake_case_     ,		snake_case_     ,		unittest.TestCase					):




 '''simple docstring'''
 __lowercase					:		Union[str, Any]     				=    (UperNetForSemanticSegmentation,) if is_torch_available() else ()
 __lowercase					:		Any     				=    {'''image-segmentation''': UperNetForSemanticSegmentation} if is_torch_available() else {}
 __lowercase					:		Any     				=    False
 __lowercase					:		List[Any]     				=    False
 __lowercase					:		List[Any]     				=    False
 __lowercase					:		Dict     				=    False
 __lowercase					:		Optional[Any]     				=    False
 __lowercase					:		Any     				=    False


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		List[str]	  =      UperNetModelTester(self    )
  __A	:		Optional[int]	  =      ConfigTester(self   ,   config_class=__lowercase   ,   has_text_modality=__lowercase   ,   hidden_size=37    )


 def 			snake_case__ (   self    ):

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

  """simple docstring"""







  return


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A  ,__A	:		Any	  =      self.model_tester.prepare_config_and_inputs_for_common()

  for model_class in self.all_model_classes:
   __A	:		Union[str, Any]	  =      model_class(__lowercase    )
   __A	:		Dict	  =      inspect.signature(model.forward    )
   # signature.parameters is an OrderedDict => so arg_names order is deterministic
   __A	:		Union[str, Any]	  =      [*signature.parameters.keys()]

   __A	:		Optional[Any]	  =      ['pixel_values']
   self.assertListEqual(arg_names[:1]   ,   __lowercase    )


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Optional[int]	  =      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 			snake_case__ (   self    ):

  """simple docstring"""







  pass


 @unittest.skip(reason='UperNet does not support input and output embeddings'    )
 def 			snake_case__ (   self    ):

  """simple docstring"""







  pass


 @unittest.skip(reason='UperNet does not have a base model'    )
 def 			snake_case__ (   self    ):

  """simple docstring"""







  pass


 @unittest.skip(reason='UperNet does not have a base model'    )
 def 			snake_case__ (   self    ):

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

  """simple docstring"""







  pass


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

  """simple docstring"""







  pass


 def 			snake_case__ (   self    ):

  """simple docstring"""







  def check_hidden_states_output(__lowercase   ,   __lowercase   ,   __lowercase    ):
   __A	:		str	  =      model_class(__lowercase    )
   model.to(__lowercase    )
   model.eval()

   with torch.no_grad():
    __A	:		List[Any]	  =      model(**self._prepare_for_class(__lowercase   ,   __lowercase    )    )

   __A	:		Dict	  =      outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

   __A	:		Any	  =      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]   ,   )

  __A  ,__A	:		str	  =      self.model_tester.prepare_config_and_inputs_for_common()

  for model_class in self.all_model_classes:
   __A	:		Optional[Any]	  =      True
   check_hidden_states_output(__lowercase   ,   __lowercase   ,   __lowercase    )

   # check that output_hidden_states also work using config
   del inputs_dict["output_hidden_states"]
   __A	:		List[Any]	  =      True

   check_hidden_states_output(__lowercase   ,   __lowercase   ,   __lowercase    )


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A  ,__A	:		str	  =      self.model_tester.prepare_config_and_inputs_for_common()

  __A	:		List[str]	  =      _config_zero_init(__lowercase    )
  __A	:		List[str]	  =      _config_zero_init(configs_no_init.backbone_config    )
  for model_class in self.all_model_classes:
   __A	:		Tuple	  =      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 			snake_case__ (   self    ):

  """simple docstring"""







  pass







 @slow
 def 			snake_case__ (   self    ):

  """simple docstring"""







  for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
   __A	:		Union[str, Any]	  =      UperNetForSemanticSegmentation.from_pretrained(__lowercase    )
   self.assertIsNotNone(__lowercase    )


def       _lowercase							(					):
 __A	:		List[str]	  =      hf_hub_download(
     repo_id='hf-internal-testing/fixtures_ade20k',					repo_type='dataset',					filename='ADE_val_00000001.jpg'      )
 __A	:		Optional[int]	  =      Image.open(UpperCamelCase__      ).convert('RGB'      )
 return image


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




 '''simple docstring'''


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Tuple	  =      AutoImageProcessor.from_pretrained('openmmlab/upernet-swin-tiny'    )
  __A	:		str	  =      UperNetForSemanticSegmentation.from_pretrained('openmmlab/upernet-swin-tiny'    ).to(__lowercase    )

  __A	:		List[str]	  =      prepare_img()
  __A	:		Optional[int]	  =      processor(images=__lowercase   ,   return_tensors='pt'    ).to(__lowercase    )

  with torch.no_grad():
   __A	:		str	  =      model(**__lowercase    )

  __A	:		int	  =      torch.Size((1, model.config.num_labels, 512, 512)    )
  self.assertEqual(outputs.logits.shape   ,   __lowercase    )

  __A	:		List[Any]	  =      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 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Any	  =      AutoImageProcessor.from_pretrained('openmmlab/upernet-convnext-tiny'    )
  __A	:		Tuple	  =      UperNetForSemanticSegmentation.from_pretrained('openmmlab/upernet-convnext-tiny'    ).to(__lowercase    )

  __A	:		Optional[int]	  =      prepare_img()
  __A	:		Optional[int]	  =      processor(images=__lowercase   ,   return_tensors='pt'    ).to(__lowercase    )

  with torch.no_grad():
   __A	:		Union[str, Any]	  =      model(**__lowercase    )

  __A	:		Tuple	  =      torch.Size((1, model.config.num_labels, 512, 512)    )
  self.assertEqual(outputs.logits.shape   ,   __lowercase    )

  __A	:		List[Any]	  =      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    )    )










'''simple docstring'''
import unittest
from typing import Tuple

import torch

from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device
from diffusers.utils.testing_utils import require_torch

@require_torch
class     _lowerCamelCase       :




 '''simple docstring'''


 @property
 def 			snake_case__ (   self    ):

  """simple docstring"""







  return self.get_dummy_input()


 @property
 def 			snake_case__ (   self    ):

  """simple docstring"""







  if self.block_type == "down":
   return (4, 32, 16, 16)
  elif self.block_type == "mid":
   return (4, 32, 32, 32)
  elif self.block_type == "up":
   return (4, 32, 64, 64)

  raise ValueError(F"""'{self.block_type}' is not a supported block_type. Set it to 'up', 'mid', or 'down'."""    )


 def 			snake_case__ (   self   ,   __lowercase=True   ,   __lowercase=False   ,   __lowercase=False   ,   __lowercase=False   ,   ):

  """simple docstring"""







  __A	:		List[str]	  =      4
  __A	:		List[Any]	  =      32
  __A	:		Optional[Any]	  =      (32, 32)

  __A	:		List[str]	  =      torch.manual_seed(0    )
  __A	:		Union[str, Any]	  =      torch.device(__lowercase    )
  __A	:		Optional[int]	  =      (batch_size, num_channels) + sizes
  __A	:		str	  =      randn_tensor(__lowercase   ,   generator=__lowercase   ,   device=__lowercase    )
  __A	:		int	  =      {'hidden_states': hidden_states}

  if include_temb:
   __A	:		Optional[Any]	  =      128
   __A	:		Any	  =      randn_tensor((batch_size, temb_channels)   ,   generator=__lowercase   ,   device=__lowercase    )

  if include_res_hidden_states_tuple:
   __A	:		Any	  =      torch.manual_seed(1    )
   __A	:		List[Any]	  =      (randn_tensor(__lowercase   ,   generator=__lowercase   ,   device=__lowercase    ),)

  if include_encoder_hidden_states:
   __A	:		str	  =      floats_tensor((batch_size, 32, 32)    ).to(__lowercase    )

  if include_skip_sample:
   __A	:		List[Any]	  =      randn_tensor(((batch_size, 3) + sizes)   ,   generator=__lowercase   ,   device=__lowercase    )

  return dummy_input


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Tuple	  =      {
      'in_channels': 32,
      'out_channels': 32,
      'temb_channels': 128,
  }
  if self.block_type == "up":
   __A	:		int	  =      32

  if self.block_type == "mid":
   init_dict.pop('out_channels'    )

  __A	:		Optional[int]	  =      self.dummy_input
  return init_dict, inputs_dict


 def 			snake_case__ (   self   ,   __lowercase    ):

  """simple docstring"""







  __A  ,__A	:		List[Any]	  =      self.prepare_init_args_and_inputs_for_common()
  __A	:		int	  =      self.block_class(**__lowercase    )
  unet_block.to(__lowercase    )
  unet_block.eval()

  with torch.no_grad():
   __A	:		List[Any]	  =      unet_block(**__lowercase    )

  if isinstance(__lowercase   ,   __lowercase    ):
   __A	:		Tuple	  =      output[0]

  self.assertEqual(output.shape   ,   self.output_shape    )

  __A	:		Any	  =      output[0, -1, -3:, -3:]
  __A	:		Dict	  =      torch.tensor(__lowercase    ).to(__lowercase    )
  assert torch_all_close(output_slice.flatten()   ,   __lowercase   ,   atol=5E-3    )







 @unittest.skipIf(torch_device == 'mps'   ,   'Training is not supported in mps'    )
 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A  ,__A	:		str	  =      self.prepare_init_args_and_inputs_for_common()
  __A	:		Optional[Any]	  =      self.block_class(**__lowercase    )
  model.to(__lowercase    )
  model.train()
  __A	:		Any	  =      model(**__lowercase    )

  if isinstance(__lowercase   ,   __lowercase    ):
   __A	:		Any	  =      output[0]

  __A	:		int	  =      torch.device(__lowercase    )
  __A	:		Dict	  =      randn_tensor(output.shape   ,   device=__lowercase    )
  __A	:		Dict	  =      torch.nn.functional.mse_loss(__lowercase   ,   __lowercase    )
  loss.backward()






