


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    UpperCAmelCase_	:
       '''simple docstring'''






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




              """simple docstring"""




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






       def  _lowercase   (     self						):




              """simple docstring"""




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

              a_    :	Any   	=		None
              if self.use_labels:
                     a_    :	List[Any]   	=		ids_tensor([self.batch_size] ,					self.type_sequence_label_size						)

              a_    :	Optional[Any]   	=		self.get_config()

              return config, pixel_values, labels






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




              """simple docstring"""




              return UperNetConfig(
                  backbone_config=self.get_backbone_config() ,					hidden_size=512 ,					pool_scales=[1, 2, 3, 6] ,					use_auxiliary_head=lowerCamelCase__ ,					auxiliary_loss_weight=0.4 ,					auxiliary_in_channels=40 ,					auxiliary_channels=256 ,					auxiliary_num_convs=1 ,					auxiliary_concat_input=lowerCamelCase__ ,					loss_ignore_index=255 ,					num_labels=self.num_labels ,					)






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




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






       def  _lowercase   (     self						):




              """simple docstring"""




              a_    :	List[Any]   	=		self.prepare_config_and_inputs()
              (
                  a_    
              )    :	int   	=		config_and_inputs
              a_    :	Tuple   	=		{"pixel_values": pixel_values}
              return config, inputs_dict




@require_torch
class    UpperCAmelCase_	(							UpperCAmelCase__,     UpperCAmelCase__,     unittest.TestCase):
       '''simple docstring'''
       __UpperCamelCase      :     str							=					(UperNetForSemanticSegmentation,) if is_torch_available() else ()
       __UpperCamelCase      :     Dict							=					{"image-segmentation": UperNetForSemanticSegmentation} if is_torch_available() else {}
       __UpperCamelCase      :     Optional[Any]							=					False
       __UpperCamelCase      :     List[Any]							=					False
       __UpperCamelCase      :     Optional[int]							=					False
       __UpperCamelCase      :     Optional[int]							=					False
       __UpperCamelCase      :     Any							=					False
       __UpperCamelCase      :     Optional[int]							=					False






       def  _lowercase   (     self						):




              """simple docstring"""




              a_    :	List[Any]   	=		UperNetModelTester(self						)
              a_    :	Tuple   	=		ConfigTester(self ,					config_class=lowerCamelCase__ ,					has_text_modality=lowerCamelCase__ ,					hidden_size=37						)






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




              """simple docstring"""




              return






       def  _lowercase   (     self						):




              """simple docstring"""




              a_    :	Any   	=		self.model_tester.prepare_config_and_inputs_for_common()

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

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






       def  _lowercase   (     self						):




              """simple docstring"""




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




              """simple docstring"""




              pass






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




              """simple docstring"""




              pass






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




              """simple docstring"""




              pass






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




              """simple docstring"""




              pass






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




              """simple docstring"""




              pass






       def  _lowercase   (     self						):




              """simple docstring"""




              def check_hidden_states_output(__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):
                     a_    :	Dict   	=		model_class(lowerCamelCase__						)
                     model.to(lowerCamelCase__						)
                     model.eval()

                     with torch.no_grad():
                            a_    :	List[str]   	=		model(**self._prepare_for_class(lowerCamelCase__ ,					lowerCamelCase__						)						)

                     a_    :	int   	=		outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

                     a_    :	List[str]   	=		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] ,					)

              a_    :	str   	=		self.model_tester.prepare_config_and_inputs_for_common()

              for model_class in self.all_model_classes:
                     a_    :	int   	=		True
                     check_hidden_states_output(lowerCamelCase__ ,					lowerCamelCase__ ,					lowerCamelCase__						)

                     # check that output_hidden_states also work using config
                     del inputs_dict["output_hidden_states"]
                     a_    :	Optional[Any]   	=		True

                     check_hidden_states_output(lowerCamelCase__ ,					lowerCamelCase__ ,					lowerCamelCase__						)






       def  _lowercase   (     self						):




              """simple docstring"""




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

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




              """simple docstring"""




              pass






       @slow
       def  _lowercase   (     self						):




              """simple docstring"""




              for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                     a_    :	str   	=		UperNetForSemanticSegmentation.from_pretrained(lowerCamelCase__						)
                     self.assertIsNotNone(lowerCamelCase__						)




def      a	(							):






       """simple docstring"""





       a_    :	Any   	=		hf_hub_download(
           repo_id='''hf-internal-testing/fixtures_ade20k'''							,			repo_type='''dataset'''							,			filename='''ADE_val_00000001.jpg'''       )
       a_    :	int   	=		Image.open(__lowerCAmelCase       ).convert('''RGB'''       )
       return image



@require_torch
@require_vision
@slow
class    UpperCAmelCase_	(							unittest.TestCase):
       '''simple docstring'''






       def  _lowercase   (     self						):




              """simple docstring"""




              a_    :	List[Any]   	=		AutoImageProcessor.from_pretrained('''openmmlab/upernet-swin-tiny'''						)
              a_    :	Union[str, Any]   	=		UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-swin-tiny'''						).to(lowerCamelCase__						)

              a_    :	str   	=		prepare_img()
              a_    :	Any   	=		processor(images=lowerCamelCase__ ,					return_tensors='''pt'''						).to(lowerCamelCase__						)

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

              a_    :	List[Any]   	=		torch.Size((1, model.config.num_labels, 512, 512)						)
              self.assertEqual(outputs.logits.shape ,					lowerCamelCase__						)

              a_    :	Any   	=		torch.tensor(
                  [[-7.5_958, -7.5_958, -7.4_302], [-7.5_958, -7.5_958, -7.4_302], [-7.4_797, -7.4_797, -7.3_068]]						).to(lowerCamelCase__						)
              self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3] ,					lowerCamelCase__ ,					atol=1e-4						)						)






       def  _lowercase   (     self						):




              """simple docstring"""




              a_    :	Dict   	=		AutoImageProcessor.from_pretrained('''openmmlab/upernet-convnext-tiny'''						)
              a_    :	Dict   	=		UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-convnext-tiny'''						).to(lowerCamelCase__						)

              a_    :	int   	=		prepare_img()
              a_    :	List[Any]   	=		processor(images=lowerCamelCase__ ,					return_tensors='''pt'''						).to(lowerCamelCase__						)

              with torch.no_grad():
                     a_    :	Dict   	=		model(**lowerCamelCase__						)

              a_    :	int   	=		torch.Size((1, model.config.num_labels, 512, 512)						)
              self.assertEqual(outputs.logits.shape ,					lowerCamelCase__						)

              a_    :	Dict   	=		torch.tensor(
                  [[-8.8_110, -8.8_110, -8.6_521], [-8.8_110, -8.8_110, -8.6_521], [-8.7_746, -8.7_746, -8.6_130]]						).to(lowerCamelCase__						)
              self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3] ,					lowerCamelCase__ ,					atol=1e-4						)						)





import json
from typing import List, Optional, Tuple

from tokenizers import normalizers
from tokenizers.pre_tokenizers import BertPreTokenizer, PreTokenizer

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_roformer import RoFormerTokenizer
from .tokenization_utils import JiebaPreTokenizer


__UpperCAmelCase : Dict 			=				logging.get_logger(__name__)

__UpperCAmelCase : Optional[Any] 			=				{"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"}

__UpperCAmelCase : Dict 			=				{
    "vocab_file": {
        "junnyu/roformer_chinese_small": "https://huggingface.co/junnyu/roformer_chinese_small/resolve/main/vocab.txt",
        "junnyu/roformer_chinese_base": "https://huggingface.co/junnyu/roformer_chinese_base/resolve/main/vocab.txt",
        "junnyu/roformer_chinese_char_small": (
            "https://huggingface.co/junnyu/roformer_chinese_char_small/resolve/main/vocab.txt"
        ),
        "junnyu/roformer_chinese_char_base": (
            "https://huggingface.co/junnyu/roformer_chinese_char_base/resolve/main/vocab.txt"
        ),
        "junnyu/roformer_small_discriminator": (
            "https://huggingface.co/junnyu/roformer_small_discriminator/resolve/main/vocab.txt"
        ),
        "junnyu/roformer_small_generator": (
            "https://huggingface.co/junnyu/roformer_small_generator/resolve/main/vocab.txt"
        ),
    }
}

__UpperCAmelCase : Tuple 			=				{
    "junnyu/roformer_chinese_small": 1536,
    "junnyu/roformer_chinese_base": 1536,
    "junnyu/roformer_chinese_char_small": 512,
    "junnyu/roformer_chinese_char_base": 512,
    "junnyu/roformer_small_discriminator": 128,
    "junnyu/roformer_small_generator": 128,
}


__UpperCAmelCase : Any 			=				{
    "junnyu/roformer_chinese_small": {"do_lower_case": True},
    "junnyu/roformer_chinese_base": {"do_lower_case": True},
    "junnyu/roformer_chinese_char_small": {"do_lower_case": True},
    "junnyu/roformer_chinese_char_base": {"do_lower_case": True},
    "junnyu/roformer_small_discriminator": {"do_lower_case": True},
    "junnyu/roformer_small_generator": {"do_lower_case": True},
}



class    UpperCAmelCase_	(							_a):
       '''simple docstring'''
       __UpperCamelCase      :     str							=					VOCAB_FILES_NAMES
       __UpperCamelCase      :     Optional[int]							=					PRETRAINED_VOCAB_FILES_MAP
       __UpperCamelCase      :     Union[str, Any]							=					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
       __UpperCamelCase      :     int							=					PRETRAINED_INIT_CONFIGURATION
       __UpperCamelCase      :     Any							=					RoFormerTokenizer






       def __init__(     self ,					__SCREAMING_SNAKE_CASE=None ,					__SCREAMING_SNAKE_CASE=None ,					__SCREAMING_SNAKE_CASE=True ,					__SCREAMING_SNAKE_CASE="[UNK]" ,					__SCREAMING_SNAKE_CASE="[SEP]" ,					__SCREAMING_SNAKE_CASE="[PAD]" ,					__SCREAMING_SNAKE_CASE="[CLS]" ,					__SCREAMING_SNAKE_CASE="[MASK]" ,					__SCREAMING_SNAKE_CASE=True ,					__SCREAMING_SNAKE_CASE=None ,					**__SCREAMING_SNAKE_CASE ,					):




              """simple docstring"""




              super().__init__(
                  __SCREAMING_SNAKE_CASE ,					tokenizer_file=__SCREAMING_SNAKE_CASE ,					do_lower_case=__SCREAMING_SNAKE_CASE ,					unk_token=__SCREAMING_SNAKE_CASE ,					sep_token=__SCREAMING_SNAKE_CASE ,					pad_token=__SCREAMING_SNAKE_CASE ,					cls_token=__SCREAMING_SNAKE_CASE ,					mask_token=__SCREAMING_SNAKE_CASE ,					tokenize_chinese_chars=__SCREAMING_SNAKE_CASE ,					strip_accents=__SCREAMING_SNAKE_CASE ,					**__SCREAMING_SNAKE_CASE ,					)

              UpperCamelCase    :	List[Any]   	=		json.loads(self.backend_tokenizer.normalizer.__getstate__()						)
              if (
                  pre_tok_state.get('''lowercase''' ,					__SCREAMING_SNAKE_CASE						) != do_lower_case
                  or pre_tok_state.get('''strip_accents''' ,					__SCREAMING_SNAKE_CASE						) != strip_accents
              ):
                     UpperCamelCase    :	List[Any]   	=		getattr(__SCREAMING_SNAKE_CASE ,					pre_tok_state.pop('''type'''						)						)
                     UpperCamelCase    :	Optional[int]   	=		do_lower_case
                     UpperCamelCase    :	Optional[Any]   	=		strip_accents
                     UpperCamelCase    :	List[Any]   	=		pre_tok_class(**__SCREAMING_SNAKE_CASE						)

              UpperCamelCase    :	List[Any]   	=		do_lower_case






       def __getstate__(     self						):




              """simple docstring"""




              UpperCamelCase    :	Optional[int]   	=		self.__dict__.copy()
              UpperCamelCase    :	Any   	=		BertPreTokenizer()
              return state






       def __setstate__(     self ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              UpperCamelCase    :	Any   	=		d
              UpperCamelCase    :	List[str]   	=		self.__dict__['''_tokenizer'''].get_vocab()
              UpperCamelCase    :	Any   	=		PreTokenizer.custom(JiebaPreTokenizer(__SCREAMING_SNAKE_CASE						)						)






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE=None						):




              """simple docstring"""




              UpperCamelCase    :	Any   	=		[self.cls_token_id] + token_ids_a + [self.sep_token_id]

              if token_ids_a:
                     output += token_ids_a + [self.sep_token_id]

              return output






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE = None						):




              """simple docstring"""




              UpperCamelCase    :	Dict   	=		[self.sep_token_id]
              UpperCamelCase    :	int   	=		[self.cls_token_id]
              if token_ids_a is None:
                     return len(cls + token_ids_a + sep						) * [0]
              return len(cls + token_ids_a + sep						) * [0] + len(token_ids_a + sep						) * [1]






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE = None						):




              """simple docstring"""




              UpperCamelCase    :	List[Any]   	=		self._tokenizer.model.save(__SCREAMING_SNAKE_CASE ,					name=__SCREAMING_SNAKE_CASE						)
              return tuple(__SCREAMING_SNAKE_CASE						)






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE=None ,					__SCREAMING_SNAKE_CASE=None ,					__SCREAMING_SNAKE_CASE=False ,					**__SCREAMING_SNAKE_CASE ,					):




              """simple docstring"""




              UpperCamelCase    :	Any   	=		BertPreTokenizer()
              return super().save_pretrained(__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					**__SCREAMING_SNAKE_CASE						)

