
"""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   __a  :






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







              """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 	_SCREAMING_SNAKE_CASE       (  self :							Tuple			)->  Optional[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.type_sequence_label_size			)

              UpperCamelCase					 =		self.get_config()

              return config, pixel_values, labels






       def 	_SCREAMING_SNAKE_CASE       (  self :							Optional[Any]			)->  Optional[Any]:







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







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






       def 	_SCREAMING_SNAKE_CASE       (  self :							Any       ,      UpperCAmelCase_ :							List[str]       ,      UpperCAmelCase_ :							Union[str, Any]       ,      UpperCAmelCase_ :							Tuple			)->  Tuple:







              """simple docstring"""
              UpperCamelCase					 =		UperNetForSemanticSegmentation(config=UpperCAmelCase_			)
              model.to(UpperCAmelCase_			)
              model.eval()
              UpperCamelCase					 =		model(UpperCAmelCase_			)
              self.parent.assertEqual(
                  result.logits.shape       ,      (self.batch_size, self.num_labels, self.image_size, self.image_size)			)






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







              """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   __a  (      _lowerCAmelCase    ,     _lowerCAmelCase    ,     unittest.TestCase	):
       UpperCamelCase_		:					Any   =			(UperNetForSemanticSegmentation,) if is_torch_available() else ()
       UpperCamelCase_		:					Optional[int]   =			{'''image-segmentation''': UperNetForSemanticSegmentation} if is_torch_available() else {}
       UpperCamelCase_		:					Tuple   =			False
       UpperCamelCase_		:					str   =			False
       UpperCamelCase_		:					int   =			False
       UpperCamelCase_		:					Union[str, Any]   =			False
       UpperCamelCase_		:					str   =			False
       UpperCamelCase_		:					Optional[int]   =			False






       def 	_SCREAMING_SNAKE_CASE       (  self :							int			)->  str:







              """simple docstring"""
              UpperCamelCase					 =		UperNetModelTester(self			)
              UpperCamelCase					 =		ConfigTester(self       ,      config_class=UpperCAmelCase_       ,      has_text_modality=UpperCAmelCase_       ,      hidden_size=37			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							int			)->  Union[str, Any]:







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







              """simple docstring"""
              return






       def 	_SCREAMING_SNAKE_CASE       (  self :							Any			)->  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(UpperCAmelCase_			)
                     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]       ,      UpperCAmelCase_			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							Optional[Any]			)->  Tuple:







              """simple docstring"""
              UpperCamelCase					 =		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_semantic_segmentation(*UpperCAmelCase_			)






       @unittest.skip(reason="UperNet does not use inputs_embeds"			)
       def 	_SCREAMING_SNAKE_CASE       (  self :							Union[str, Any]			)->  Union[str, Any]:







              """simple docstring"""
              pass






       @unittest.skip(reason="UperNet does not support input and output embeddings"			)
       def 	_SCREAMING_SNAKE_CASE       (  self :							Union[str, Any]			)->  Union[str, Any]:







              """simple docstring"""
              pass






       @unittest.skip(reason="UperNet does not have a base model"			)
       def 	_SCREAMING_SNAKE_CASE       (  self :							Optional[Any]			)->  List[str]:







              """simple docstring"""
              pass






       @unittest.skip(reason="UperNet does not have a base model"			)
       def 	_SCREAMING_SNAKE_CASE       (  self :							Optional[int]			)->  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 	_SCREAMING_SNAKE_CASE       (  self :							Optional[Any]			)->  Tuple:







              """simple docstring"""
              pass






       @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."			)
       def 	_SCREAMING_SNAKE_CASE       (  self :							Optional[int]			)->  Dict:







              """simple docstring"""
              pass






       def 	_SCREAMING_SNAKE_CASE       (  self :							int			)->  str:







              """simple docstring"""
              def check_hidden_states_output(UpperCAmelCase_ :							Dict       ,      UpperCAmelCase_ :							str       ,      UpperCAmelCase_ :							str			):
                     UpperCamelCase					 =		model_class(UpperCAmelCase_			)
                     model.to(UpperCAmelCase_			)
                     model.eval()

                     with torch.no_grad():
                            UpperCamelCase					 =		model(**self._prepare_for_class(UpperCAmelCase_       ,      UpperCAmelCase_			)			)

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

                     UpperCamelCase					 =		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]       ,      )

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

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

                     check_hidden_states_output(UpperCAmelCase_       ,      UpperCAmelCase_       ,      UpperCAmelCase_			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							List[Any]			)->  List[Any]:







              """simple docstring"""
              UpperCamelCase ,       UpperCamelCase					 =		self.model_tester.prepare_config_and_inputs_for_common()

              UpperCamelCase					 =		_config_zero_init(UpperCAmelCase_			)
              UpperCamelCase					 =		_config_zero_init(configs_no_init.backbone_config			)
              for model_class in self.all_model_classes:
                     UpperCamelCase					 =		model_class(config=UpperCAmelCase_			)
                     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 	_SCREAMING_SNAKE_CASE       (  self :							Union[str, Any]			)->  Union[str, Any]:







              """simple docstring"""
              pass






       @slow
       def 	_SCREAMING_SNAKE_CASE       (  self :							Union[str, Any]			)->  List[Any]:







              """simple docstring"""
              for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                     UpperCamelCase					 =		UperNetForSemanticSegmentation.from_pretrained(UpperCAmelCase_			)
                     self.assertIsNotNone(UpperCAmelCase_			)





def 	lowerCamelCase__			(							)->							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(UpperCAmelCase_				).convert("RGB"				)
       return image



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






       def 	_SCREAMING_SNAKE_CASE       (  self :							Tuple			)->  List[Any]:







              """simple docstring"""
              UpperCamelCase					 =		AutoImageProcessor.from_pretrained("openmmlab/upernet-swin-tiny"			)
              UpperCamelCase					 =		UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-swin-tiny"			).to(UpperCAmelCase_			)

              UpperCamelCase					 =		prepare_img()
              UpperCamelCase					 =		processor(images=UpperCAmelCase_       ,      return_tensors="pt"			).to(UpperCAmelCase_			)

              with torch.no_grad():
                     UpperCamelCase					 =		model(**UpperCAmelCase_			)

              UpperCamelCase					 =		torch.Size((1, model.config.num_labels, 512, 512)			)
              self.assertEqual(outputs.logits.shape       ,      UpperCAmelCase_			)

              UpperCamelCase					 =		torch.tensor(
                  [[-7.5958, -7.5958, -7.4302], [-7.5958, -7.5958, -7.4302], [-7.4797, -7.4797, -7.3068]]			).to(UpperCAmelCase_			)
              self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]       ,      UpperCAmelCase_       ,      atol=1e-4			)			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							Tuple			)->  int:







              """simple docstring"""
              UpperCamelCase					 =		AutoImageProcessor.from_pretrained("openmmlab/upernet-convnext-tiny"			)
              UpperCamelCase					 =		UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-convnext-tiny"			).to(UpperCAmelCase_			)

              UpperCamelCase					 =		prepare_img()
              UpperCamelCase					 =		processor(images=UpperCAmelCase_       ,      return_tensors="pt"			).to(UpperCAmelCase_			)

              with torch.no_grad():
                     UpperCamelCase					 =		model(**UpperCAmelCase_			)

              UpperCamelCase					 =		torch.Size((1, model.config.num_labels, 512, 512)			)
              self.assertEqual(outputs.logits.shape       ,      UpperCAmelCase_			)

              UpperCamelCase					 =		torch.tensor(
                  [[-8.8110, -8.8110, -8.6521], [-8.8110, -8.8110, -8.6521], [-8.7746, -8.7746, -8.6130]]			).to(UpperCAmelCase_			)
              self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]       ,      UpperCAmelCase_       ,      atol=1e-4			)			)







"""simple docstring"""


import inspect
import os
import unittest

import torch

import accelerate
from accelerate import debug_launcher
from accelerate.test_utils import (
    execute_subprocess_async,
    require_cpu,
    require_huggingface_suite,
    require_multi_gpu,
    require_single_gpu,
)
from accelerate.utils import patch_environment



@require_huggingface_suite
class   __a  (      unittest.TestCase	):






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







              """simple docstring"""
              UpperCamelCase					 =		inspect.getfile(accelerate.test_utils			)
              UpperCamelCase					 =		os.path.sep.join(
                  mod_file.split(os.path.sep			)[:-1] + ["scripts", "external_deps", "test_metrics.py"]			)

              from accelerate.test_utils.scripts.external_deps import test_metrics  # noqa: F401

              UpperCamelCase					 =		test_metrics






       @require_cpu
       def 	_SCREAMING_SNAKE_CASE       (  self :							Dict			)->  Union[str, Any]:







              """simple docstring"""
              debug_launcher(self.test_metrics.main       ,      num_processes=1			)






       @require_cpu
       def 	_SCREAMING_SNAKE_CASE       (  self :							Tuple			)->  List[str]:







              """simple docstring"""
              debug_launcher(self.test_metrics.main			)






       @require_single_gpu
       def 	_SCREAMING_SNAKE_CASE       (  self :							List[Any]			)->  str:







              """simple docstring"""
              self.test_metrics.main()






       @require_multi_gpu
       def 	_SCREAMING_SNAKE_CASE       (  self :							Optional[Any]			)->  Optional[Any]:







              """simple docstring"""
              print(f"Found {torch.cuda.device_count()} devices."			)
              UpperCamelCase					 =		["torchrun", f"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path]
              with patch_environment(omp_num_threads=1			):
                     execute_subprocess_async(UpperCAmelCase_       ,      env=os.environ.copy()			)





