'''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   :   List[str]       ,      __a   :   Dict       ,      __a   :   Tuple=13       ,      __a   :   int=32       ,      __a   :   int=3       ,      __a   :   Dict=4       ,      __a   :   Any=[10, 20, 30, 40]       ,      __a   :   Union[str, Any]=[2, 2, 3, 2]       ,      __a   :   Dict=True       ,      __a   :   Optional[Any]=True       ,      __a   :   Dict=37       ,      __a   :   Optional[int]="gelu"       ,      __a   :   Any=10       ,      __a   :   Optional[Any]=0.02       ,      __a   :   Optional[Any]=["stage2", "stage3", "stage4"]       ,      __a   :   List[str]=3       ,      __a   :   Tuple=None       ,      ):
         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  _lowercase						(self   :   str			):
         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  _lowercase						(self   :   Any			):
         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  _lowercase						(self   :   Union[str, Any]			):
         return UperNetConfig(
             backbone_config=self.get_backbone_config()       ,      hidden_size=512       ,      pool_scales=[1, 2, 3, 6]       ,      use_auxiliary_head=__a       ,      auxiliary_loss_weight=0.4       ,      auxiliary_in_channels=40       ,      auxiliary_channels=256       ,      auxiliary_num_convs=1       ,      auxiliary_concat_input=__a       ,      loss_ignore_index=255       ,      num_labels=self.num_labels       ,      )



    def  _lowercase						(self   :   Any       ,      __a   :   Any       ,      __a   :   Optional[Any]       ,      __a   :   Optional[int]			):
         UpperCAmelCase_	   =       UperNetForSemanticSegmentation(config=__a			)
         model.to(__a			)
         model.eval()
         UpperCAmelCase_	   =       model(__a			)
         self.parent.assertEqual(
             result.logits.shape       ,      (self.batch_size, self.num_labels, self.image_size, self.image_size)			)






    def  _lowercase						(self   :   Dict			):
         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				(			UpperCamelCase__		,     UpperCamelCase__		,     unittest.TestCase     ):
    a__	:						Union[str, Any]	       =  (UperNetForSemanticSegmentation,) if is_torch_available() else ()
    a__	:						List[str]	       =  {"""image-segmentation""": UperNetForSemanticSegmentation} if is_torch_available() else {}
    a__	:						Dict	       =  False
    a__	:						Dict	       =  False
    a__	:						List[str]	       =  False
    a__	:						Any	       =  False
    a__	:						List[Any]	       =  False
    a__	:						Optional[int]	       =  False



    def  _lowercase						(self   :   int			):
         UpperCAmelCase_	   =       UperNetModelTester(self			)
         UpperCAmelCase_	   =       ConfigTester(self       ,      config_class=__a       ,      has_text_modality=__a       ,      hidden_size=37			)



    def  _lowercase						(self   :   Optional[int]			):
         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  _lowercase						(self   :   Optional[int]			):
         return



    def  _lowercase						(self   :   Dict			):
         UpperCAmelCase_   ,			UpperCAmelCase_	   =       self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
              UpperCAmelCase_	   =       model_class(__a			)
              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]       ,      __a			)



    def  _lowercase						(self   :   Tuple			):
         UpperCAmelCase_	   =       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_semantic_segmentation(*__a			)



    @unittest.skip(reason="UperNet does not use inputs_embeds"			)
    def  _lowercase						(self   :   int			):
         pass



    @unittest.skip(reason="UperNet does not support input and output embeddings"			)
    def  _lowercase						(self   :   Optional[Any]			):
         pass



    @unittest.skip(reason="UperNet does not have a base model"			)
    def  _lowercase						(self   :   Tuple			):
         pass



    @unittest.skip(reason="UperNet does not have a base model"			)
    def  _lowercase						(self   :   Any			):
         pass



    @require_torch_multi_gpu
    @unittest.skip(reason="UperNet has some layers using `add_module` which doesn't work well with `nn.DataParallel`"			)
    def  _lowercase						(self   :   List[Any]			):
         pass



    @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."			)
    def  _lowercase						(self   :   Any			):
         pass



    def  _lowercase						(self   :   Optional[int]			):
         def check_hidden_states_output(__a   :   Optional[Any]       ,      __a   :   List[str]       ,      __a   :   Dict			):
              UpperCAmelCase_	   =       model_class(__a			)
              model.to(__a			)
              model.eval()

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

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

              UpperCAmelCase_	   =       self.model_tester.num_stages
              self.assertEqual(len(__a			)       ,      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(__a       ,      __a       ,      __a			)

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

              check_hidden_states_output(__a       ,      __a       ,      __a			)



    def  _lowercase						(self   :   Tuple			):
         UpperCAmelCase_   ,			UpperCAmelCase_	   =       self.model_tester.prepare_config_and_inputs_for_common()

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






    @slow
    def  _lowercase						(self   :   List[str]			):
         for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              UpperCAmelCase_	   =       UperNetForSemanticSegmentation.from_pretrained(__a			)
              self.assertIsNotNone(__a			)





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






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



    def  _lowercase						(self   :   Optional[Any]			):
         UpperCAmelCase_	   =       AutoImageProcessor.from_pretrained("openmmlab/upernet-swin-tiny"			)
         UpperCAmelCase_	   =       UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-swin-tiny"			).to(__a			)

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

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

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

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






    def  _lowercase						(self   :   Dict			):
         UpperCAmelCase_	   =       AutoImageProcessor.from_pretrained("openmmlab/upernet-convnext-tiny"			)
         UpperCAmelCase_	   =       UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-convnext-tiny"			).to(__a			)

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

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

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

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





'''simple docstring'''





from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxSeqaSeqConfigWithPast
from ...utils import logging


SCREAMING_SNAKE_CASE_:					List[str]  =logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_:					List[Any]  ={
    't5-small': 'https://huggingface.co/t5-small/resolve/main/config.json',
    't5-base': 'https://huggingface.co/t5-base/resolve/main/config.json',
    't5-large': 'https://huggingface.co/t5-large/resolve/main/config.json',
    't5-3b': 'https://huggingface.co/t5-3b/resolve/main/config.json',
    't5-11b': 'https://huggingface.co/t5-11b/resolve/main/config.json',
}




class 	__A				(			UpperCamelCase__     ):
    a__	:						Dict	       =  """t5"""
    a__	:						List[str]	       =  ["""past_key_values"""]
    a__	:						Union[str, Any]	       =  {"""hidden_size""": """d_model""", """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers"""}



    def __init__(self   :   List[str]       ,      __a   :   Any=32128       ,      __a   :   Optional[int]=512       ,      __a   :   Union[str, Any]=64       ,      __a   :   List[str]=2048       ,      __a   :   Optional[int]=6       ,      __a   :   Union[str, Any]=None       ,      __a   :   Union[str, Any]=8       ,      __a   :   Dict=32       ,      __a   :   List[Any]=128       ,      __a   :   Optional[Any]=0.1       ,      __a   :   List[Any]=1E-6       ,      __a   :   str=1.0       ,      __a   :   Dict="relu"       ,      __a   :   int=True       ,      __a   :   int=True       ,      __a   :   Optional[Any]=0       ,      __a   :   Dict=1       ,      **__a   :   List[Any]       ,      ):
         UpperCAmelCase_	   =       vocab_size
         UpperCAmelCase_	   =       d_model
         UpperCAmelCase_	   =       d_kv
         UpperCAmelCase_	   =       d_ff
         UpperCAmelCase_	   =       num_layers
         UpperCAmelCase_	   =       (
             num_decoder_layers if num_decoder_layers is not None else self.num_layers
         )  # default = symmetry
         UpperCAmelCase_	   =       num_heads
         UpperCAmelCase_	   =       relative_attention_num_buckets
         UpperCAmelCase_	   =       relative_attention_max_distance
         UpperCAmelCase_	   =       dropout_rate
         UpperCAmelCase_	   =       layer_norm_epsilon
         UpperCAmelCase_	   =       initializer_factor
         UpperCAmelCase_	   =       feed_forward_proj
         UpperCAmelCase_	   =       use_cache

         UpperCAmelCase_	   =       self.feed_forward_proj.split("-"			)
         UpperCAmelCase_	   =       act_info[-1]
         UpperCAmelCase_	   =       act_info[0] == "gated"

         if len(__a			) > 1 and act_info[0] != "gated" or len(__a			) > 2:
              raise ValueError(
                  f"""`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer."""
                  "Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. "
                  "'gated-gelu' or 'relu'"			)

         # for backwards compatibility
         if feed_forward_proj == "gated-gelu":
              UpperCAmelCase_	   =       "gelu_new"

         super().__init__(
             pad_token_id=__a       ,      eos_token_id=__a       ,      is_encoder_decoder=__a       ,      **__a       ,      )




class 	__A				(			UpperCamelCase__     ):



    @property
    def  _lowercase						(self   :   str			):
         UpperCAmelCase_	   =       {
             "input_ids": {0: "batch", 1: "encoder_sequence"},
             "attention_mask": {0: "batch", 1: "encoder_sequence"},
         }
         if self.use_past:
              UpperCAmelCase_	   =       "past_encoder_sequence + sequence"
              UpperCAmelCase_	   =       {0: "batch"}
              UpperCAmelCase_	   =       {0: "batch", 1: "past_decoder_sequence + sequence"}
         else:
              UpperCAmelCase_	   =       {0: "batch", 1: "decoder_sequence"}
              UpperCAmelCase_	   =       {0: "batch", 1: "decoder_sequence"}

         if self.use_past:
              self.fill_with_past_key_values_(__a       ,      direction="inputs"			)

         return common_inputs






    @property
    def  _lowercase						(self   :   Optional[int]			):
         return 13




