

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 :       str,lowercase_ :       str,lowercase_ :       Tuple=1_3,lowercase_ :       List[Any]=3_2,lowercase_ :       Union[str, Any]=3,lowercase_ :       Dict=4,lowercase_ :       Dict=[1_0, 2_0, 3_0, 4_0],lowercase_ :       Any=[2, 2, 3, 2],lowercase_ :       str=True,lowercase_ :       Optional[Any]=True,lowercase_ :       Optional[Any]=3_7,lowercase_ :       Optional[int]="gelu",lowercase_ :       Optional[Any]=1_0,lowercase_ :       Dict=0.02,lowercase_ :       Dict=["stage2", "stage3", "stage4"],lowercase_ :       Union[str, Any]=3,lowercase_ :       Optional[Any]=None,)->    Optional[int]:





        '''simple docstring'''







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


    def 						snake_case__     (      self :       Tuple		)->    Optional[Any]:





        '''simple docstring'''







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

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

        A__          =   self.get_config()

        return config, pixel_values, labels


    def 						snake_case__     (      self :       Dict		)->    Union[str, 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 						snake_case__     (      self :       Tuple		)->    Union[str, Any]:





        '''simple docstring'''







        return UperNetConfig(
            backbone_config=self.get_backbone_config(),hidden_size=5_1_2,pool_scales=[1, 2, 3, 6],use_auxiliary_head=lowercase_,auxiliary_loss_weight=0.4,auxiliary_in_channels=4_0,auxiliary_channels=2_5_6,auxiliary_num_convs=1,auxiliary_concat_input=lowercase_,loss_ignore_index=2_5_5,num_labels=self.num_labels,)


    def 						snake_case__     (      self :       Optional[Any],lowercase_ :       Dict,lowercase_ :       int,lowercase_ :       Optional[int]		)->    Dict:





        '''simple docstring'''







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


    def 						snake_case__     (      self :       int		)->    Dict:





        '''simple docstring'''







        A__          =   self.prepare_config_and_inputs()
        (
            (
            A__
        )      ,						(
            A__
        )      ,						(
            A__
        )      ,						
        )          =   config_and_inputs
        A__          =   {'pixel_values': pixel_values}
        return config, inputs_dict




@require_torch
class  A						(							_UpperCAmelCase    ,							_UpperCAmelCase    ,							unittest.TestCase ):







    """simple docstring"""





    lowerCamelCase			=							(UperNetForSemanticSegmentation,) if is_torch_available() else ()
    lowerCamelCase			=							{'image-segmentation': UperNetForSemanticSegmentation} if is_torch_available() else {}
    lowerCamelCase			=							False
    lowerCamelCase			=							False
    lowerCamelCase			=							False
    lowerCamelCase			=							False
    lowerCamelCase			=							False
    lowerCamelCase			=							False


    def 						snake_case__     (      self :       Optional[Any]		)->    List[Any]:





        '''simple docstring'''







        A__          =   UperNetModelTester(self		)
        A__          =   ConfigTester(self,config_class=lowercase_,has_text_modality=lowercase_,hidden_size=3_7		)


    def 						snake_case__     (      self :       Optional[Any]		)->    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 						snake_case__     (      self :       int		)->    Tuple:





        '''simple docstring'''







        return


    def 						snake_case__     (      self :       List[str]		)->    List[str]:





        '''simple docstring'''







        A__      ,						A__          =   self.model_tester.prepare_config_and_inputs_for_common()

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

            A__          =   ['pixel_values']
            self.assertListEqual(arg_names[:1],lowercase_		)


    def 						snake_case__     (      self :       List[str]		)->    Union[str, Any]:





        '''simple docstring'''







        A__          =   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 						snake_case__     (      self :       Any		)->    List[Any]:





        '''simple docstring'''







        pass


    @unittest.skip(reason='UperNet does not support input and output embeddings'		)
    def 						snake_case__     (      self :       str		)->    int:





        '''simple docstring'''







        pass


    @unittest.skip(reason='UperNet does not have a base model'		)
    def 						snake_case__     (      self :       int		)->    Any:





        '''simple docstring'''







        pass


    @unittest.skip(reason='UperNet does not have a base model'		)
    def 						snake_case__     (      self :       Union[str, Any]		)->    Dict:





        '''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 						snake_case__     (      self :       Any		)->    Tuple:





        '''simple docstring'''







        pass


    @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'		)
    def 						snake_case__     (      self :       Union[str, Any]		)->    Tuple:





        '''simple docstring'''







        pass


    def 						snake_case__     (      self :       int		)->    Any:





        '''simple docstring'''







        def check_hidden_states_output(lowercase_ :       str,lowercase_ :       Union[str, Any],lowercase_ :       Any		):
            A__          =   model_class(lowercase_		)
            model.to(lowercase_		)
            model.eval()

            with torch.no_grad():
                A__          =   model(**self._prepare_for_class(lowercase_,lowercase_		)		)

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

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

        A__      ,						A__          =   self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
            A__          =   True
            check_hidden_states_output(lowercase_,lowercase_,lowercase_		)

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

            check_hidden_states_output(lowercase_,lowercase_,lowercase_		)


    def 						snake_case__     (      self :       Optional[Any]		)->    Union[str, Any]:





        '''simple docstring'''







        A__      ,						A__          =   self.model_tester.prepare_config_and_inputs_for_common()

        A__          =   _config_zero_init(lowercase_		)
        A__          =   _config_zero_init(configs_no_init.backbone_config		)
        for model_class in self.all_model_classes:
            A__          =   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 						snake_case__     (      self :       Tuple		)->    Optional[int]:





        '''simple docstring'''







        pass


    @slow
    def 						snake_case__     (      self :       Dict		)->    Dict:





        '''simple docstring'''







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




def 						_snake_case(    )							->      int:





    '''simple docstring'''






    A__          =   hf_hub_download(
        repo_id='hf-internal-testing/fixtures_ade20k'    ,			repo_type='dataset'    ,			filename='ADE_val_00000001.jpg'      )
    A__          =   Image.open(SCREAMING_SNAKE_CASE__      ).convert('RGB'      )
    return image


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







    """simple docstring"""





    def 						snake_case__     (      self :       Optional[int]		)->    List[Any]:





        '''simple docstring'''







        A__          =   AutoImageProcessor.from_pretrained('openmmlab/upernet-swin-tiny'		)
        A__          =   UperNetForSemanticSegmentation.from_pretrained('openmmlab/upernet-swin-tiny'		).to(lowercase_		)

        A__          =   prepare_img()
        A__          =   processor(images=lowercase_,return_tensors='pt'		).to(lowercase_		)

        with torch.no_grad():
            A__          =   model(**lowercase_		)

        A__          =   torch.Size((1, model.config.num_labels, 5_1_2, 5_1_2)		)
        self.assertEqual(outputs.logits.shape,lowercase_		)

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


    def 						snake_case__     (      self :       Any		)->    Any:





        '''simple docstring'''







        A__          =   AutoImageProcessor.from_pretrained('openmmlab/upernet-convnext-tiny'		)
        A__          =   UperNetForSemanticSegmentation.from_pretrained('openmmlab/upernet-convnext-tiny'		).to(lowercase_		)

        A__          =   prepare_img()
        A__          =   processor(images=lowercase_,return_tensors='pt'		).to(lowercase_		)

        with torch.no_grad():
            A__          =   model(**lowercase_		)

        A__          =   torch.Size((1, model.config.num_labels, 5_1_2, 5_1_2)		)
        self.assertEqual(outputs.logits.shape,lowercase_		)

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









import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


lowerCamelCase       :       int        =				'''▁'''

lowerCamelCase       :       Optional[int]        =				{'''vocab_file''': '''spiece.model'''}

lowerCamelCase       :       str        =				{
    '''vocab_file''': {'''google/pegasus-xsum''': '''https://huggingface.co/google/pegasus-xsum/resolve/main/spiece.model'''}
}

lowerCamelCase       :       str        =				{
    '''google/pegasus-xsum''': 5_12,
}


lowerCamelCase       :       Optional[Any]        =				logging.get_logger(__name__)

class 						lowerCAmelCase   (       __a	):

					'''simple docstring'''

					_A  :	Tuple     =					VOCAB_FILES_NAMES

					_A  :	Tuple     =					VOCAB_FILES_NAMES
					_A  :	Optional[int]     =					PRETRAINED_VOCAB_FILES_MAP
					_A  :	str     =					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
					_A  :	Optional[int]     =					['''input_ids''', '''attention_mask''']



					def __init__(     self :		Union[str, Any]						,   __a :		int						,   __a :		Any="<pad>"						,   __a :		Optional[int]="</s>"						,   __a :		Union[str, Any]="<unk>"						,   __a :		Optional[int]="<mask_2>"						,   __a :		Optional[int]="<mask_1>"						,   __a :		Dict=None						,   __a :		List[str]=103						,   __a :		Optional[Dict[str, Any]] = None						,   **__a :		List[Any]						,   )	->     None:


												"""simple docstring"""

												__lowercase					:							Tuple			=   offset
												if additional_special_tokens is not None:
																			if not isinstance(__a						,   __a							):
																										raise TypeError(
																										    F"additional_special_tokens should be of type {type(__a							)}, but is"
																										    F" {type(__a							)}"							)

																			__lowercase					:							Dict			=   (
																			    ([mask_token_sent] + additional_special_tokens)
																			    if mask_token_sent not in additional_special_tokens and mask_token_sent is not None
																			    else additional_special_tokens
																			)
																			# fill additional tokens with ..., <unk_token_102> in case not all additional tokens are already taken
																			additional_special_tokens_extended += [
																			    F"<unk_{i}>" for i in range(len(__a							)						,   self.offset - 1							)
																			]

																			if len(set(__a							)							) != len(__a							):
																										raise ValueError(
																										    """Please make sure that the provided additional_special_tokens do not contain an incorrectly"""
																										    F" shifted list of <unk_x> tokens. Found {additional_special_tokens_extended}."							)
																			__lowercase					:							Optional[Any]			=   additional_special_tokens_extended
												else:
																			__lowercase					:							int			=   [mask_token_sent] if mask_token_sent is not None else []
																			additional_special_tokens += [F"<unk_{i}>" for i in range(2						,   self.offset							)]

												__lowercase					:							str			=   {} if sp_model_kwargs is None else sp_model_kwargs

												super().__init__(
												    eos_token=__a						,   unk_token=__a						,   mask_token=__a						,   pad_token=__a						,   mask_token_sent=__a						,   offset=__a						,   additional_special_tokens=__a						,   sp_model_kwargs=self.sp_model_kwargs						,   **__a						,   )
												__lowercase					:							Optional[Any]			=   mask_token_sent
												__lowercase					:							Dict			=   vocab_file
												__lowercase					:							Optional[Any]			=   spm.SentencePieceProcessor(**self.sp_model_kwargs							)
												self.sp_model.Load(__a							)

												# add special tokens to encoder dict
												__lowercase					:							Dict[int, str]			=   {
												    0: self.pad_token,
												    1: self.eos_token,
												}

												if self.mask_token_sent is not None:
																			self.encoder.update(
																			    {
																			        2: self.mask_token_sent,
																			        3: self.mask_token,
																			    }							)

												if self.offset > 0:
																			# entries 2-104 are only used for pretraining and called <mask_1>, <mask_2>, unk_2, ...unk_102
																			# mask_token_sent is already added to list -> so start at 1
																			self.encoder.update({i + 3: additional_special_tokens[i] for i in range(1						,   self.offset - 1							)}							)

												__lowercase					:							Dict[str, int]			=   {v: k for k, v in self.encoder.items()}



					@property
					def 					lowerCAmelCase (     self :		Dict							)	->     int:


												"""simple docstring"""

												return len(self.sp_model							) + self.offset



					def 					lowerCAmelCase (     self :		int							)	->     Dict[str, int]:


												"""simple docstring"""

												__lowercase					:							Any			=   {self.convert_ids_to_tokens(__a							): i for i in range(self.vocab_size							)}
												vocab.update(self.added_tokens_encoder							)
												return vocab



					def __getstate__(     self :		List[str]							)	->     List[str]:


												"""simple docstring"""

												__lowercase					:							Union[str, Any]			=   self.__dict__.copy()
												__lowercase					:							Optional[Any]			=   None
												return state



					def __setstate__(     self :		Tuple						,   __a :		Any							)	->     Tuple:


												"""simple docstring"""

												__lowercase					:							List[str]			=   d

												# for backward compatibility
												if not hasattr(self						,   """sp_model_kwargs"""							):
																			__lowercase					:							List[str]			=   {}

												__lowercase					:							Optional[int]			=   spm.SentencePieceProcessor(**self.sp_model_kwargs							)
												self.sp_model.Load(self.vocab_file							)



					def 					lowerCAmelCase (     self :		Dict						,   __a :		str							)	->     List[str]:


												"""simple docstring"""

												return self.sp_model.encode(__a						,   out_type=__a							)



					def 					lowerCAmelCase (     self :		List[str]						,   __a :		str							)	->     int:


												"""simple docstring"""

												if token in self.decoder:
																			return self.decoder[token]
												elif token in self.added_tokens_decoder:
																			return self.added_tokens_decoder[token]
												__lowercase					:							Union[str, Any]			=   self.sp_model.piece_to_id(__a							)
												return sp_id + self.offset



					def 					lowerCAmelCase (     self :		Dict						,   __a :		int							)	->     str:


												"""simple docstring"""

												if index in self.encoder:
																			return self.encoder[index]
												elif index in self.added_tokens_encoder:
																			return self.added_tokens_encoder[index]
												else:
																			__lowercase					:							List[Any]			=   self.sp_model.IdToPiece(index - self.offset							)
												return token



					def 					lowerCAmelCase (     self :		Union[str, Any]						,   __a :		int							)	->     Dict:


												"""simple docstring"""

												__lowercase					:							Optional[int]			=   []
												__lowercase					:							Tuple			=   """"""
												for token in tokens:
																			# make sure that special tokens are not decoded using sentencepiece model
																			if token in self.all_special_tokens:
																										out_string += self.sp_model.decode(__a							) + token
																										__lowercase					:							str			=   []
																			else:
																										current_sub_tokens.append(__a							)
												out_string += self.sp_model.decode(__a							)
												return out_string.strip()



					def 					lowerCAmelCase (     self :		int						,   __a :		Optional[Any]=False							)	->     int:


												"""simple docstring"""

												return 1



					def 					lowerCAmelCase (     self :		Optional[int]						,   __a :		List[str]							)	->     Optional[Any]:


												"""simple docstring"""

												__lowercase					:							List[Any]			=   set(self.all_special_ids							)  # call it once instead of inside list comp
												all_special_ids.remove(self.unk_token_id							)  # <unk> is only sometimes special

												return [1 if x in all_special_ids else 0 for x in seq]



					def 					lowerCAmelCase (     self :		Union[str, Any]						,   __a :		List						,   __a :		Optional[List] = None						,   __a :		bool = False							)	->     List[int]:


												"""simple docstring"""

												if already_has_special_tokens:
																			return self._special_token_mask(__a							)
												elif token_ids_a is None:
																			return self._special_token_mask(__a							) + [1]
												else:
																			return self._special_token_mask(token_ids_a + token_ids_a							) + [1]



					def 					lowerCAmelCase (     self :		Optional[int]						,   __a :		Dict						,   __a :		Tuple=None							)	->     List[int]:


												"""simple docstring"""

												if token_ids_a is None:
																			return token_ids_a + [self.eos_token_id]
												# We don't expect to process pairs, but leave the pair logic for API consistency
												return token_ids_a + token_ids_a + [self.eos_token_id]



					def 					lowerCAmelCase (     self :		Tuple						,   __a :		str						,   __a :		Optional[str] = None							)	->     Tuple[str]:


												"""simple docstring"""

												if not os.path.isdir(__a							):
																			logger.error(F"Vocabulary path ({save_directory}) should be a directory"							)
																			return
												__lowercase					:							Optional[int]			=   os.path.join(
												    __a						,   (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]							)

												if os.path.abspath(self.vocab_file							) != os.path.abspath(__a							) and os.path.isfile(self.vocab_file							):
																			copyfile(self.vocab_file						,   __a							)
												elif not os.path.isfile(self.vocab_file							):
																			with open(__a						,   """wb"""							) as fi:
																										__lowercase					:							Any			=   self.sp_model.serialized_model_proto()
																										fi.write(__a							)

												return (out_vocab_file,)