





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       ,							A_       ,							A_=13       ,							A_=32       ,							A_=3       ,							A_=4       ,							A_=[10, 20, 30, 40]       ,							A_=[2, 2, 3, 2]       ,							A_=True       ,							A_=True       ,							A_=37       ,							A_="gelu"       ,							A_=10       ,							A_=0.02       ,							A_=["stage2", "stage3", "stage4"]       ,							A_=3       ,							A_=None       ,							):
       '''simple docstring'''




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





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Any       	=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

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

       UpperCamelCase	:					Optional[int]       	=   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=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        __UpperCamelCase( self       ,							A_       ,							A_       ,							A_   ):
       '''simple docstring'''




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





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Optional[Any]       	=   self.prepare_config_and_inputs()
       (
           (
           UpperCamelCase
       )	,    (
           UpperCamelCase
       )	,    (
           UpperCamelCase
       )	,    
       )	:					Optional[int]       	=   config_and_inputs
       UpperCamelCase	:					str       	=   {"pixel_values": pixel_values}
       return config, inputs_dict



@require_torch
class 					A__   (       __snake_case       ,   __snake_case       ,   unittest.TestCase			):
      _UpperCAmelCase	:Optional[Any]						    =   (UperNetForSemanticSegmentation,) if is_torch_available() else ()
      _UpperCAmelCase	:List[str]						    =   {'image-segmentation': UperNetForSemanticSegmentation} if is_torch_available() else {}
      _UpperCAmelCase	:Dict						    =   False
      _UpperCAmelCase	:Dict						    =   False
      _UpperCAmelCase	:Optional[int]						    =   False
      _UpperCAmelCase	:Dict						    =   False
      _UpperCAmelCase	:Optional[Any]						    =   False
      _UpperCAmelCase	:Dict						    =   False





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Union[str, Any]       	=   UperNetModelTester(self   )
       UpperCamelCase	:					Dict       	=   ConfigTester(self       ,							config_class=A_       ,							has_text_modality=A_       ,							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'''




       UpperCamelCase	,    UpperCamelCase	:					Union[str, Any]       	=   self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        UpperCamelCase	:					Dict       	=   model_class(A_   )
        UpperCamelCase	:					Tuple       	=   inspect.signature(model.forward   )
        # signature.parameters is an OrderedDict => so arg_names order is deterministic
        UpperCamelCase	:					Optional[int]       	=   [*signature.parameters.keys()]

        UpperCamelCase	:					List[Any]       	=   ["pixel_values"]
        self.assertListEqual(arg_names[:1]       ,							A_   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Dict       	=   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        __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(A_       ,							A_       ,							A_   ):
        UpperCamelCase	:					Any       	=   model_class(A_   )
        model.to(A_   )
        model.eval()

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

        UpperCamelCase	:					Optional[Any]       	=   outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

        UpperCamelCase	:					Optional[Any]       	=   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	:					int       	=   self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        UpperCamelCase	:					Tuple       	=   True
        check_hidden_states_output(A_       ,							A_       ,							A_   )

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

        check_hidden_states_output(A_       ,							A_       ,							A_   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	,    UpperCamelCase	:					List[Any]       	=   self.model_tester.prepare_config_and_inputs_for_common()

       UpperCamelCase	:					Tuple       	=   _config_zero_init(A_   )
       UpperCamelCase	:					Optional[int]       	=   _config_zero_init(configs_no_init.backbone_config   )
       for model_class in self.all_model_classes:
        UpperCamelCase	:					Dict       	=   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        __UpperCamelCase( self   ):
       '''simple docstring'''




       pass





      @slow
      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        UpperCamelCase	:					int       	=   UperNetForSemanticSegmentation.from_pretrained(A_   )
        self.assertIsNotNone(A_   )



def 		A_				(     )						-> Optional[Any]:
 UpperCamelCase	:					Optional[int]       	=   hf_hub_download(
     repo_id="hf-internal-testing/fixtures_ade20k"							,   repo_type="dataset"							,   filename="ADE_val_00000001.jpg"     )
 UpperCamelCase	:					Dict       	=   Image.open(_lowerCAmelCase     ).convert("RGB"     )
 return image



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





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					str       	=   AutoImageProcessor.from_pretrained("openmmlab/upernet-swin-tiny"   )
       UpperCamelCase	:					Dict       	=   UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-swin-tiny"   ).to(A_   )

       UpperCamelCase	:					Tuple       	=   prepare_img()
       UpperCamelCase	:					Union[str, Any]       	=   processor(images=A_       ,							return_tensors="pt"   ).to(A_   )

       with torch.no_grad():
        UpperCamelCase	:					Union[str, Any]       	=   model(**A_   )

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

       UpperCamelCase	:					Optional[Any]       	=   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        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Optional[int]       	=   AutoImageProcessor.from_pretrained("openmmlab/upernet-convnext-tiny"   )
       UpperCamelCase	:					str       	=   UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-convnext-tiny"   ).to(A_   )

       UpperCamelCase	:					Any       	=   prepare_img()
       UpperCamelCase	:					str       	=   processor(images=A_       ,							return_tensors="pt"   ).to(A_   )

       with torch.no_grad():
        UpperCamelCase	:					List[str]       	=   model(**A_   )

       UpperCamelCase	:					List[str]       	=   torch.Size((1, model.config.num_labels, 512, 512)   )
       self.assertEqual(outputs.logits.shape       ,							A_   )

       UpperCamelCase	:					int       	=   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   )   )







from collections import OrderedDict
from typing import Mapping

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


__lowerCamelCase       :     List[Any]   =     logging.get_logger(__name__)

__lowerCamelCase       :     str   =     {
    """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/config.json""",
    """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/config.json""",
    """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/config.json""",
    """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/config.json""",
    """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/config.json""",
    """roberta-large-openai-detector""": """https://huggingface.co/roberta-large-openai-detector/resolve/main/config.json""",
}



class 					A__   (       __snake_case			):
      _UpperCAmelCase	:Union[str, Any]						    =   'roberta'





      def __init__( self       ,							A_=5_0265       ,							A_=768       ,							A_=12       ,							A_=12       ,							A_=3072       ,							A_="gelu"       ,							A_=0.1       ,							A_=0.1       ,							A_=512       ,							A_=2       ,							A_=0.02       ,							A_=1e-12       ,							A_=1       ,							A_=0       ,							A_=2       ,							A_="absolute"       ,							A_=True       ,							A_=None       ,							**A_       ,							):
       '''simple docstring'''




       super().__init__(pad_token_id=A_       ,							bos_token_id=A_       ,							eos_token_id=A_       ,							**A_   )

       UpperCamelCase	:					Optional[int]       	=   vocab_size
       UpperCamelCase	:					Dict       	=   hidden_size
       UpperCamelCase	:					str       	=   num_hidden_layers
       UpperCamelCase	:					Any       	=   num_attention_heads
       UpperCamelCase	:					List[str]       	=   hidden_act
       UpperCamelCase	:					Optional[Any]       	=   intermediate_size
       UpperCamelCase	:					Tuple       	=   hidden_dropout_prob
       UpperCamelCase	:					Tuple       	=   attention_probs_dropout_prob
       UpperCamelCase	:					Tuple       	=   max_position_embeddings
       UpperCamelCase	:					Any       	=   type_vocab_size
       UpperCamelCase	:					int       	=   initializer_range
       UpperCamelCase	:					str       	=   layer_norm_eps
       UpperCamelCase	:					Dict       	=   position_embedding_type
       UpperCamelCase	:					Any       	=   use_cache
       UpperCamelCase	:					Union[str, Any]       	=   classifier_dropout



class 					A__   (       __snake_case			):





      @property
      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       if self.task == "multiple-choice":
        UpperCamelCase	:					Optional[int]       	=   {0: "batch", 1: "choice", 2: "sequence"}
       else:
        UpperCamelCase	:					Optional[int]       	=   {0: "batch", 1: "sequence"}
       return OrderedDict(
           [
               ("input_ids", dynamic_axis),
               ("attention_mask", dynamic_axis),
           ]   )
