



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




  def __init__(					self	:		Any		,   __lowerCAmelCase	:		str		,   __lowerCAmelCase	:		Optional[int]=1_3		,   __lowerCAmelCase	:		List[Any]=3_2		,   __lowerCAmelCase	:		Optional[int]=3		,   __lowerCAmelCase	:		Dict=4		,   __lowerCAmelCase	:		Any=[1_0, 2_0, 3_0, 4_0]		,   __lowerCAmelCase	:		List[Any]=[2, 2, 3, 2]		,   __lowerCAmelCase	:		Tuple=True		,   __lowerCAmelCase	:		Optional[Any]=True		,   __lowerCAmelCase	:		Tuple=3_7		,   __lowerCAmelCase	:		str="gelu"		,   __lowerCAmelCase	:		List[Any]=1_0		,   __lowerCAmelCase	:		Optional[int]=0.02		,   __lowerCAmelCase	:		Any=["stage2", "stage3", "stage4"]		,   __lowerCAmelCase	:		Any=3		,   __lowerCAmelCase	:		int=None		,   ):






         """simple docstring"""






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




  def 		SCREAMING_SNAKE_CASE							(					self	:		int						):






         """simple docstring"""






         _lowerCamelCase		:							str            =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

         _lowerCamelCase		:							Any            =					None
         if self.use_labels:
                _lowerCamelCase		:							Optional[int]            =					ids_tensor([self.batch_size]		,   self.type_sequence_label_size						)

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

         return config, pixel_values, labels




  def 		SCREAMING_SNAKE_CASE							(					self	:		int						):






         """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	:		List[str]						):






         """simple docstring"""






         return UperNetConfig(
             backbone_config=self.get_backbone_config()		,   hidden_size=5_1_2		,   pool_scales=[1, 2, 3, 6]		,   use_auxiliary_head=__lowerCAmelCase		,   auxiliary_loss_weight=0.4		,   auxiliary_in_channels=4_0		,   auxiliary_channels=2_5_6		,   auxiliary_num_convs=1		,   auxiliary_concat_input=__lowerCAmelCase		,   loss_ignore_index=2_5_5		,   num_labels=self.num_labels		,   )




  def 		SCREAMING_SNAKE_CASE							(					self	:		Any		,   __lowerCAmelCase	:		Tuple		,   __lowerCAmelCase	:		int		,   __lowerCAmelCase	:		Optional[Any]						):






         """simple docstring"""






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




  def 		SCREAMING_SNAKE_CASE							(					self	:		Dict						):






         """simple docstring"""






         _lowerCamelCase		:							Tuple            =					self.prepare_config_and_inputs()
         (
             (
             _lowerCamelCase
         )     ,       (
             _lowerCamelCase
         )     ,       (
             _lowerCamelCase
         )     ,       
         )		:							int            =					config_and_inputs
         _lowerCamelCase		:							Union[str, Any]            =					{'''pixel_values''': pixel_values}
         return config, inputs_dict






@require_torch
class 			__snake_case			(      _lowercase    ,	_lowercase    ,	unittest.TestCase):
  snake_case__							:  Union[str, Any]         =		(UperNetForSemanticSegmentation,) if is_torch_available() else ()
  snake_case__							:  Optional[Any]         =		{"image-segmentation": UperNetForSemanticSegmentation} if is_torch_available() else {}
  snake_case__							:  Union[str, Any]         =		False
  snake_case__							:  Optional[int]         =		False
  snake_case__							:  Union[str, Any]         =		False
  snake_case__							:  Union[str, Any]         =		False
  snake_case__							:  int         =		False
  snake_case__							:  Union[str, Any]         =		False




  def 		SCREAMING_SNAKE_CASE							(					self	:		List[Any]						):






         """simple docstring"""






         _lowerCamelCase		:							Union[str, Any]            =					UperNetModelTester(self						)
         _lowerCamelCase		:							Union[str, Any]            =					ConfigTester(self		,   config_class=__lowerCAmelCase		,   has_text_modality=__lowerCAmelCase		,   hidden_size=3_7						)




  def 		SCREAMING_SNAKE_CASE							(					self	:		Optional[int]						):






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






         """simple docstring"""






         return




  def 		SCREAMING_SNAKE_CASE							(					self	:		Tuple						):






         """simple docstring"""






         _lowerCamelCase     ,       _lowerCamelCase		:							List[str]            =					self.model_tester.prepare_config_and_inputs_for_common()

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

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




  def 		SCREAMING_SNAKE_CASE							(					self	:		Optional[Any]						):






         """simple docstring"""






         _lowerCamelCase		:							int            =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_semantic_segmentation(*__lowerCAmelCase						)




  @unittest.skip(reason='''UperNet does not use inputs_embeds'''						)
  def 		SCREAMING_SNAKE_CASE							(					self	:		Dict						):






         """simple docstring"""






         pass




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






         """simple docstring"""






         pass




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






         """simple docstring"""






         pass




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






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






         """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	:		List[str]						):






         """simple docstring"""






         pass




  def 		SCREAMING_SNAKE_CASE							(					self	:		Optional[int]						):






         """simple docstring"""






         def check_hidden_states_output(__lowerCAmelCase	:		Dict		,   __lowerCAmelCase	:		Optional[int]		,   __lowerCAmelCase	:		Dict						):
                _lowerCamelCase		:							Tuple            =					model_class(__lowerCAmelCase						)
                model.to(__lowerCAmelCase						)
                model.eval()

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

                _lowerCamelCase		:							Optional[int]            =					outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

                _lowerCamelCase		:							Optional[int]            =					self.model_tester.num_stages
                self.assertEqual(len(__lowerCAmelCase						)		,   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]		,   )

         _lowerCamelCase     ,       _lowerCamelCase		:							Tuple            =					self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
                _lowerCamelCase		:							Union[str, Any]            =					True
                check_hidden_states_output(__lowerCAmelCase		,   __lowerCAmelCase		,   __lowerCAmelCase						)

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

                check_hidden_states_output(__lowerCAmelCase		,   __lowerCAmelCase		,   __lowerCAmelCase						)




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






         """simple docstring"""






         _lowerCamelCase     ,       _lowerCamelCase		:							int            =					self.model_tester.prepare_config_and_inputs_for_common()

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






         """simple docstring"""






         pass




  @slow
  def 		SCREAMING_SNAKE_CASE							(					self	:		Optional[Any]						):






         """simple docstring"""






         for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                _lowerCamelCase		:							Optional[Any]            =					UperNetForSemanticSegmentation.from_pretrained(__lowerCAmelCase						)
                self.assertIsNotNone(__lowerCAmelCase						)






def 			snake_case_    ( ):


       '''simple docstring'''





       _lowerCamelCase		:							Tuple            =					hf_hub_download(
           repo_id='''hf-internal-testing/fixtures_ade20k''',							repo_type='''dataset''',							filename='''ADE_val_00000001.jpg'''		)
       _lowerCamelCase		:							Tuple            =					Image.open(A_		).convert('''RGB'''		)
       return image







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




  def 		SCREAMING_SNAKE_CASE							(					self	:		Dict						):






         """simple docstring"""






         _lowerCamelCase		:							int            =					AutoImageProcessor.from_pretrained('''openmmlab/upernet-swin-tiny'''						)
         _lowerCamelCase		:							Union[str, Any]            =					UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-swin-tiny'''						).to(__lowerCAmelCase						)

         _lowerCamelCase		:							Optional[int]            =					prepare_img()
         _lowerCamelCase		:							List[Any]            =					processor(images=__lowerCAmelCase		,   return_tensors='''pt'''						).to(__lowerCAmelCase						)

         with torch.no_grad():
                _lowerCamelCase		:							Any            =					model(**__lowerCAmelCase						)

         _lowerCamelCase		:							Optional[Any]            =					torch.Size((1, model.config.num_labels, 5_1_2, 5_1_2)						)
         self.assertEqual(outputs.logits.shape		,   __lowerCAmelCase						)

         _lowerCamelCase		:							int            =					torch.tensor(
             [[-7.59_58, -7.59_58, -7.43_02], [-7.59_58, -7.59_58, -7.43_02], [-7.47_97, -7.47_97, -7.30_68]]						).to(__lowerCAmelCase						)
         self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]		,   __lowerCAmelCase		,   atol=1E-4						)						)




  def 		SCREAMING_SNAKE_CASE							(					self	:		Dict						):






         """simple docstring"""






         _lowerCamelCase		:							Any            =					AutoImageProcessor.from_pretrained('''openmmlab/upernet-convnext-tiny'''						)
         _lowerCamelCase		:							Optional[Any]            =					UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-convnext-tiny'''						).to(__lowerCAmelCase						)

         _lowerCamelCase		:							Union[str, Any]            =					prepare_img()
         _lowerCamelCase		:							Tuple            =					processor(images=__lowerCAmelCase		,   return_tensors='''pt'''						).to(__lowerCAmelCase						)

         with torch.no_grad():
                _lowerCamelCase		:							Optional[Any]            =					model(**__lowerCAmelCase						)

         _lowerCamelCase		:							Union[str, Any]            =					torch.Size((1, model.config.num_labels, 5_1_2, 5_1_2)						)
         self.assertEqual(outputs.logits.shape		,   __lowerCAmelCase						)

         _lowerCamelCase		:							Union[str, Any]            =					torch.tensor(
             [[-8.81_10, -8.81_10, -8.65_21], [-8.81_10, -8.81_10, -8.65_21], [-8.77_46, -8.77_46, -8.61_30]]						).to(__lowerCAmelCase						)
         self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]		,   __lowerCAmelCase		,   atol=1E-4						)						)






'''simple docstring'''


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


UpperCAmelCase_						:  int				  =						{'configuration_wavlm': ['WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP', 'WavLMConfig']}

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCAmelCase_						:  Tuple				  =						[
				    'WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST',
				    'WavLMForAudioFrameClassification',
				    'WavLMForCTC',
				    'WavLMForSequenceClassification',
				    'WavLMForXVector',
				    'WavLMModel',
				    'WavLMPreTrainedModel',
				]

if TYPE_CHECKING:
				from .configuration_wavlm import WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP, WavLMConfig

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_wavlm import (
								    WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST,
								    WavLMForAudioFrameClassification,
								    WavLMForCTC,
								    WavLMForSequenceClassification,
								    WavLMForXVector,
								    WavLMModel,
								    WavLMPreTrainedModel,
								)

else:
				import sys

				UpperCAmelCase_						:  Any				  =						_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




