




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





       '''simple docstring'''






       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'''







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







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







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

             SCREAMING_SNAKE_CASE   :      int    					=						None
             if self.use_labels:
                   SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						ids_tensor([self.batch_size],							self.type_sequence_label_size  )

             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						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'''







             SCREAMING_SNAKE_CASE   :      int    					=						UperNetForSemanticSegmentation(config=A  )
             model.to(A  )
             model.eval()
             SCREAMING_SNAKE_CASE   :      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'''







             SCREAMING_SNAKE_CASE   :      List[Any]    					=						self.prepare_config_and_inputs()
             (
                 (
                 SCREAMING_SNAKE_CASE
             )					,    (
                 SCREAMING_SNAKE_CASE
             )					,    (
                 SCREAMING_SNAKE_CASE
             )					,    
             )   :      Dict    					=						config_and_inputs
             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						{'pixel_values': pixel_values}
             return config, inputs_dict






@require_torch
class 							_a				(			SCREAMING_SNAKE_CASE ,      SCREAMING_SNAKE_CASE ,      unittest.TestCase ):





       '''simple docstring'''






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







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						UperNetModelTester(self  )
             SCREAMING_SNAKE_CASE   :      Any    					=						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'''







             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      str    					=						self.model_tester.prepare_config_and_inputs_for_common()

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

                   SCREAMING_SNAKE_CASE   :      Any    					=						['pixel_values']
                   self.assertListEqual(arg_names[:1],							A  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      str    					=						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  ):
                   SCREAMING_SNAKE_CASE   :      str    					=						model_class(A  )
                   model.to(A  )
                   model.eval()

                   with torch.no_grad():
                         SCREAMING_SNAKE_CASE   :      Optional[int]    					=						model(**self._prepare_for_class(A,							A  )  )

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

                   SCREAMING_SNAKE_CASE   :      Optional[int]    					=						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],							)

             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Dict    					=						self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                   SCREAMING_SNAKE_CASE   :      List[str]    					=						True
                   check_hidden_states_output(A,							A,							A  )

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

                   check_hidden_states_output(A,							A,							A  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      List[str]    					=						self.model_tester.prepare_config_and_inputs_for_common()

             SCREAMING_SNAKE_CASE   :      Any    					=						_config_zero_init(A  )
             SCREAMING_SNAKE_CASE   :      Tuple    					=						_config_zero_init(configs_no_init.backbone_config  )
             for model_class in self.all_model_classes:
                   SCREAMING_SNAKE_CASE   :      str    					=						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]:
                   SCREAMING_SNAKE_CASE   :      Dict    					=						UperNetForSemanticSegmentation.from_pretrained(A  )
                   self.assertIsNotNone(A  )




def   lowercase__(			):

      """simple docstring"""






      SCREAMING_SNAKE_CASE   :      int    					=						hf_hub_download(
          repo_id='hf-internal-testing/fixtures_ade20k'      ,repo_type='dataset'      ,filename='ADE_val_00000001.jpg'    )
      SCREAMING_SNAKE_CASE   :      Optional[int]    					=						Image.open(__UpperCamelCase    ).convert('RGB'    )
      return image






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





       '''simple docstring'''






       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      int    					=						AutoImageProcessor.from_pretrained('openmmlab/upernet-swin-tiny'  )
             SCREAMING_SNAKE_CASE   :      Any    					=						UperNetForSemanticSegmentation.from_pretrained('openmmlab/upernet-swin-tiny'  ).to(A  )

             SCREAMING_SNAKE_CASE   :      str    					=						prepare_img()
             SCREAMING_SNAKE_CASE   :      str    					=						processor(images=A,							return_tensors='pt'  ).to(A  )

             with torch.no_grad():
                   SCREAMING_SNAKE_CASE   :      Any    					=						model(**A  )

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

             SCREAMING_SNAKE_CASE   :      Tuple    					=						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'''







             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						AutoImageProcessor.from_pretrained('openmmlab/upernet-convnext-tiny'  )
             SCREAMING_SNAKE_CASE   :      Dict    					=						UperNetForSemanticSegmentation.from_pretrained('openmmlab/upernet-convnext-tiny'  ).to(A  )

             SCREAMING_SNAKE_CASE   :      Tuple    					=						prepare_img()
             SCREAMING_SNAKE_CASE   :      int    					=						processor(images=A,							return_tensors='pt'  ).to(A  )

             with torch.no_grad():
                   SCREAMING_SNAKE_CASE   :      str    					=						model(**A  )

             SCREAMING_SNAKE_CASE   :      Any    					=						torch.Size((1, model.config.num_labels, 512, 512)  )
             self.assertEqual(outputs.logits.shape,							A  )

             SCREAMING_SNAKE_CASE   :      Any    					=						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'''



import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_distilbert import DistilBertTokenizer


UpperCamelCase_    	=				logging.get_logger(__name__)

UpperCamelCase_    	=				{"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"}

UpperCamelCase_    	=				{
    "vocab_file": {
        "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/vocab.txt",
        "distilbert-base-uncased-distilled-squad": (
            "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/vocab.txt"
        ),
        "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/vocab.txt",
        "distilbert-base-cased-distilled-squad": (
            "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/vocab.txt"
        ),
        "distilbert-base-german-cased": "https://huggingface.co/distilbert-base-german-cased/resolve/main/vocab.txt",
        "distilbert-base-multilingual-cased": (
            "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/vocab.txt"
        ),
    },
    "tokenizer_file": {
        "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/tokenizer.json",
        "distilbert-base-uncased-distilled-squad": (
            "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/tokenizer.json"
        ),
        "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/tokenizer.json",
        "distilbert-base-cased-distilled-squad": (
            "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/tokenizer.json"
        ),
        "distilbert-base-german-cased": (
            "https://huggingface.co/distilbert-base-german-cased/resolve/main/tokenizer.json"
        ),
        "distilbert-base-multilingual-cased": (
            "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/tokenizer.json"
        ),
    },
}

UpperCamelCase_    	=				{
    "distilbert-base-uncased": 5_1_2,
    "distilbert-base-uncased-distilled-squad": 5_1_2,
    "distilbert-base-cased": 5_1_2,
    "distilbert-base-cased-distilled-squad": 5_1_2,
    "distilbert-base-german-cased": 5_1_2,
    "distilbert-base-multilingual-cased": 5_1_2,
}


UpperCamelCase_    	=				{
    "distilbert-base-uncased": {"do_lower_case": True},
    "distilbert-base-uncased-distilled-squad": {"do_lower_case": True},
    "distilbert-base-cased": {"do_lower_case": False},
    "distilbert-base-cased-distilled-squad": {"do_lower_case": False},
    "distilbert-base-german-cased": {"do_lower_case": False},
    "distilbert-base-multilingual-cased": {"do_lower_case": False},
}






class 							_a				(			SCREAMING_SNAKE_CASE ):





       '''simple docstring'''






       A						:				List[Any]        =						VOCAB_FILES_NAMES
       A						:				Dict        =						PRETRAINED_VOCAB_FILES_MAP
       A						:				Optional[int]        =						PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
       A						:				Optional[Any]        =						PRETRAINED_INIT_CONFIGURATION
       A						:				Optional[int]        =						['''input_ids''', '''attention_mask''']
       A						:				List[Any]        =						DistilBertTokenizer







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







             '''simple docstring'''







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

             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						json.loads(self.backend_tokenizer.normalizer.__getstate__()  )
             if (
                 normalizer_state.get('lowercase',							A  ) != do_lower_case
                 or normalizer_state.get('strip_accents',							A  ) != strip_accents
                 or normalizer_state.get('handle_chinese_chars',							A  ) != tokenize_chinese_chars
             ):
                   SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						getattr(A,							normalizer_state.pop('type'  )  )
                   SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						do_lower_case
                   SCREAMING_SNAKE_CASE   :      List[str]    					=						strip_accents
                   SCREAMING_SNAKE_CASE   :      List[str]    					=						tokenize_chinese_chars
                   SCREAMING_SNAKE_CASE   :      Dict    					=						normalizer_class(**A  )

             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						do_lower_case







       def        UpperCamelCase_							(      self,							A,							A=None  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      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        UpperCamelCase_							(      self,							A,							A = None  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      List[str]    					=						[self.sep_token_id]
             SCREAMING_SNAKE_CASE   :      str    					=						[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        UpperCamelCase_							(      self,							A,							A = None  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      List[str]    					=						self._tokenizer.model.save(A,							name=A  )
             return tuple(A  )


