






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






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






												"""simple docstring"""
												lowerCAmelCase__    						=						parent
												lowerCAmelCase__    						=						batch_size
												lowerCAmelCase__    						=						image_size
												lowerCAmelCase__    						=						num_channels
												lowerCAmelCase__    						=						num_stages
												lowerCAmelCase__    						=						hidden_sizes
												lowerCAmelCase__    						=						depths
												lowerCAmelCase__    						=						is_training
												lowerCAmelCase__    						=						use_labels
												lowerCAmelCase__    						=						intermediate_size
												lowerCAmelCase__    						=						hidden_act
												lowerCAmelCase__    						=						type_sequence_label_size
												lowerCAmelCase__    						=						initializer_range
												lowerCAmelCase__    						=						out_features
												lowerCAmelCase__    						=						num_labels
												lowerCAmelCase__    						=						scope
												lowerCAmelCase__    						=						num_stages






						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

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

												lowerCAmelCase__    						=						self.get_config()

												return config, pixel_values, labels






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






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






						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase     ,						_UpperCamelCase   ):






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







						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						self.prepare_config_and_inputs()
												(
												    (
												    lowerCAmelCase__
												)						,       (
												    lowerCAmelCase__
												)						,       (
												    lowerCAmelCase__
												)						,       
												)    						=						config_and_inputs
												lowerCAmelCase__    						=						{'pixel_values': pixel_values}
												return config, inputs_dict




@require_torch
class     __SCREAMING_SNAKE_CASE					(   __lowercase		,					__lowercase		,					unittest.TestCase):
						_SCREAMING_SNAKE_CASE	:       str   =					(UperNetForSemanticSegmentation,) if is_torch_available() else ()
						_SCREAMING_SNAKE_CASE	:       str   =					{'''image-segmentation''': UperNetForSemanticSegmentation} if is_torch_available() else {}
						_SCREAMING_SNAKE_CASE	:       Any   =					False
						_SCREAMING_SNAKE_CASE	:       Union[str, Any]   =					False
						_SCREAMING_SNAKE_CASE	:       str   =					False
						_SCREAMING_SNAKE_CASE	:       Optional[Any]   =					False
						_SCREAMING_SNAKE_CASE	:       Union[str, Any]   =					False
						_SCREAMING_SNAKE_CASE	:       Union[str, Any]   =					False






						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						UperNetModelTester(self   )
												lowerCAmelCase__    						=						ConfigTester(self     ,						config_class=_UpperCamelCase     ,						has_text_modality=_UpperCamelCase     ,						hidden_size=37   )






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






												"""simple docstring"""
												return






						def       UpperCamelCase__      (	self   ):






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

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

																		lowerCAmelCase__    						=						['pixel_values']
																		self.assertListEqual(arg_names[:1]     ,						_UpperCamelCase   )






						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						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       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												pass






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






												"""simple docstring"""
												pass






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






												"""simple docstring"""
												pass






						@unittest.skip(reason='UperNet does not have a base model'   )
						def       UpperCamelCase__      (	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       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												pass






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






												"""simple docstring"""
												pass






						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												def check_hidden_states_output(_UpperCamelCase     ,						_UpperCamelCase     ,						_UpperCamelCase   ):
																		lowerCAmelCase__    						=						model_class(_UpperCamelCase   )
																		model.to(_UpperCamelCase   )
																		model.eval()

																		with torch.no_grad():
																								lowerCAmelCase__    						=						model(**self._prepare_for_class(_UpperCamelCase     ,						_UpperCamelCase   )   )

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

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

												lowerCAmelCase__						,       lowerCAmelCase__    						=						self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																		lowerCAmelCase__    						=						True
																		check_hidden_states_output(_UpperCamelCase     ,						_UpperCamelCase     ,						_UpperCamelCase   )

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

																		check_hidden_states_output(_UpperCamelCase     ,						_UpperCamelCase     ,						_UpperCamelCase   )






						def       UpperCamelCase__      (	self   ):






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

												lowerCAmelCase__    						=						_config_zero_init(_UpperCamelCase   )
												lowerCAmelCase__    						=						_config_zero_init(configs_no_init.backbone_config   )
												for model_class in self.all_model_classes:
																		lowerCAmelCase__    						=						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       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												pass







						@slow
						def       UpperCamelCase__      (	self   ):






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







def 	_UpperCamelCase   (    )  ->   Optional[Any]:

						"""simple docstring"""



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


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






						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						AutoImageProcessor.from_pretrained('openmmlab/upernet-swin-tiny'   )
												lowerCAmelCase__    						=						UperNetForSemanticSegmentation.from_pretrained('openmmlab/upernet-swin-tiny'   ).to(_UpperCamelCase   )

												lowerCAmelCase__    						=						prepare_img()
												lowerCAmelCase__    						=						processor(images=_UpperCamelCase     ,						return_tensors='pt'   ).to(_UpperCamelCase   )

												with torch.no_grad():
																		lowerCAmelCase__    						=						model(**_UpperCamelCase   )

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

												lowerCAmelCase__    						=						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(_UpperCamelCase   )
												self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]     ,						_UpperCamelCase     ,						atol=1E-4   )   )







						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						AutoImageProcessor.from_pretrained('openmmlab/upernet-convnext-tiny'   )
												lowerCAmelCase__    						=						UperNetForSemanticSegmentation.from_pretrained('openmmlab/upernet-convnext-tiny'   ).to(_UpperCamelCase   )

												lowerCAmelCase__    						=						prepare_img()
												lowerCAmelCase__    						=						processor(images=_UpperCamelCase     ,						return_tensors='pt'   ).to(_UpperCamelCase   )

												with torch.no_grad():
																		lowerCAmelCase__    						=						model(**_UpperCamelCase   )

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

												lowerCAmelCase__    						=						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(_UpperCamelCase   )
												self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]     ,						_UpperCamelCase     ,						atol=1E-4   )   )













from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available


__snake_case    :    Dict         =     {"""configuration_speech_encoder_decoder""": ["""SpeechEncoderDecoderConfig"""]}

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__snake_case    :    Union[str, Any]         =     ["""SpeechEncoderDecoderModel"""]

try:
					if not is_flax_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__snake_case    :    str         =     ["""FlaxSpeechEncoderDecoderModel"""]

if TYPE_CHECKING:
					from .configuration_speech_encoder_decoder import SpeechEncoderDecoderConfig

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_speech_encoder_decoder import SpeechEncoderDecoderModel

					try:
										if not is_flax_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_flax_speech_encoder_decoder import FlaxSpeechEncoderDecoderModel

else:
					import sys

					__snake_case    :    Tuple         =     _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)





