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





	'''simple docstring'''



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


				'''simple docstring'''


				_snake_case            =     parent
				_snake_case            =     batch_size
				_snake_case            =     image_size
				_snake_case            =     num_channels
				_snake_case            =     num_stages
				_snake_case            =     hidden_sizes
				_snake_case            =     depths
				_snake_case            =     is_training
				_snake_case            =     use_labels
				_snake_case            =     intermediate_size
				_snake_case            =     hidden_act
				_snake_case            =     type_sequence_label_size
				_snake_case            =     initializer_range
				_snake_case            =     out_features
				_snake_case            =     num_labels
				_snake_case            =     scope
				_snake_case            =     num_stages



	def 					A				(    self      :       Any      ):


				'''simple docstring'''


				_snake_case            =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

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

				_snake_case            =     self.get_config()

				return config, pixel_values, labels



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


				'''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 					A				(    self      :       Tuple     ,    lowercase      :       int     ,    lowercase      :       Union[str, Any]     ,    lowercase      :       Dict      ):


				'''simple docstring'''


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





	def 					A				(    self      :       List[str]      ):


				'''simple docstring'''


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




@require_torch
class   SCREAMING_SNAKE_CASE__	(      UpperCAmelCase				,UpperCAmelCase				,unittest.TestCase   ):





	'''simple docstring'''



	_UpperCAmelCase			:						str         =   (UperNetForSemanticSegmentation,) if is_torch_available() else ()
	_UpperCAmelCase			:						int         =   {'image-segmentation': UperNetForSemanticSegmentation} if is_torch_available() else {}
	_UpperCAmelCase			:						Optional[int]         =   False
	_UpperCAmelCase			:						Dict         =   False
	_UpperCAmelCase			:						List[str]         =   False
	_UpperCAmelCase			:						Optional[int]         =   False
	_UpperCAmelCase			:						int         =   False
	_UpperCAmelCase			:						Tuple         =   False



	def 					A				(    self      :       Any      ):


				'''simple docstring'''


				_snake_case            =     UperNetModelTester(self      )
				_snake_case            =     ConfigTester(self     ,    config_class=lowercase     ,    has_text_modality=lowercase     ,    hidden_size=37      )



	def 					A				(    self      :       Dict      ):


				'''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 					A				(    self      :       str      ):


				'''simple docstring'''


				return



	def 					A				(    self      :       Dict      ):


				'''simple docstring'''


				_snake_case            =     self.model_tester.prepare_config_and_inputs_for_common()

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

							_snake_case            =     ["pixel_values"]
							self.assertListEqual(arg_names[:1]     ,    lowercase      )



	def 					A				(    self      :       Dict      ):


				'''simple docstring'''


				_snake_case            =     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 					A				(    self      :       List[str]      ):


				'''simple docstring'''


				pass



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


				'''simple docstring'''


				pass



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


				'''simple docstring'''


				pass



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


				'''simple docstring'''


				pass



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


				'''simple docstring'''


				pass



	def 					A				(    self      :       Optional[int]      ):


				'''simple docstring'''


				def check_hidden_states_output(lowercase      :       Dict     ,    lowercase      :       str     ,    lowercase      :       Tuple      ):
							_snake_case            =     model_class(lowercase      )
							model.to(lowercase      )
							model.eval()

							with torch.no_grad():
										_snake_case            =     model(**self._prepare_for_class(lowercase     ,    lowercase      )      )

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

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

				_snake_case            =     self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
							_snake_case            =     True
							check_hidden_states_output(lowercase     ,    lowercase     ,    lowercase      )

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

							check_hidden_states_output(lowercase     ,    lowercase     ,    lowercase      )



	def 					A				(    self      :       Dict      ):


				'''simple docstring'''


				_snake_case            =     self.model_tester.prepare_config_and_inputs_for_common()

				_snake_case            =     _config_zero_init(lowercase      )
				_snake_case            =     _config_zero_init(configs_no_init.backbone_config      )
				for model_class in self.all_model_classes:
							_snake_case            =     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 					A				(    self      :       List[str]      ):


				'''simple docstring'''


				pass





	@slow
	def 					A				(    self      :       int      ):


				'''simple docstring'''


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






def 			a_						(			)					->    str:
			_snake_case            =     hf_hub_download(
			    repo_id='hf-internal-testing/fixtures_ade20k'			,   repo_type='dataset'			,   filename='ADE_val_00000001.jpg'					)
			_snake_case            =     Image.open(lowercase__					).convert('RGB'					)
			return image




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





	'''simple docstring'''



	def 					A				(    self      :       Optional[Any]      ):


				'''simple docstring'''


				_snake_case            =     AutoImageProcessor.from_pretrained('openmmlab/upernet-swin-tiny'      )
				_snake_case            =     UperNetForSemanticSegmentation.from_pretrained('openmmlab/upernet-swin-tiny'      ).to(lowercase      )

				_snake_case            =     prepare_img()
				_snake_case            =     processor(images=lowercase     ,    return_tensors='pt'      ).to(lowercase      )

				with torch.no_grad():
							_snake_case            =     model(**lowercase      )

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

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





	def 					A				(    self      :       List[Any]      ):


				'''simple docstring'''


				_snake_case            =     AutoImageProcessor.from_pretrained('openmmlab/upernet-convnext-tiny'      )
				_snake_case            =     UperNetForSemanticSegmentation.from_pretrained('openmmlab/upernet-convnext-tiny'      ).to(lowercase      )

				_snake_case            =     prepare_img()
				_snake_case            =     processor(images=lowercase     ,    return_tensors='pt'      ).to(lowercase      )

				with torch.no_grad():
							_snake_case            =     model(**lowercase      )

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

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


"""simple docstring"""

from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


if is_sentencepiece_available():
    from ..ta.tokenization_ta import TaTokenizer
else:
    from ...utils.dummy_sentencepiece_objects import TaTokenizer

_UpperCamelCase		     =    TaTokenizer

if is_tokenizers_available():
    from ..ta.tokenization_ta_fast import TaTokenizerFast
else:
    from ...utils.dummy_tokenizers_objects import TaTokenizerFast

_UpperCamelCase		     =    TaTokenizerFast

_UpperCamelCase		     =    {"""configuration_mt5""": ["""MT5Config""", """MT5OnnxConfig"""]}

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    _UpperCamelCase		     =    [
        """MT5EncoderModel""",
        """MT5ForConditionalGeneration""",
        """MT5ForQuestionAnswering""",
        """MT5Model""",
        """MT5PreTrainedModel""",
        """MT5Stack""",
    ]

try:
    if not is_tf_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    _UpperCamelCase		     =    ["""TFMT5EncoderModel""", """TFMT5ForConditionalGeneration""", """TFMT5Model"""]

try:
    if not is_flax_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    _UpperCamelCase		     =    ["""FlaxMT5EncoderModel""", """FlaxMT5ForConditionalGeneration""", """FlaxMT5Model"""]


if TYPE_CHECKING:
    from .configuration_mta import MTaConfig, MTaOnnxConfig

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_mta import (
            MTaEncoderModel,
            MTaForConditionalGeneration,
            MTaForQuestionAnswering,
            MTaModel,
            MTaPreTrainedModel,
            MTaStack,
        )

    try:
        if not is_tf_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel

    try:
        if not is_flax_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel

else:
    import sys

    _UpperCamelCase		     =    _LazyModule(
        __name__,
        globals()["""__file__"""],
        _import_structure,
        extra_objects={"""MT5Tokenizer""": MTaTokenizer, """MT5TokenizerFast""": MTaTokenizerFast},
        module_spec=__spec__,
    )






