






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 	snake_case__ :
	def __init__(    self      ,       lowerCAmelCase__      ,       lowerCAmelCase__=13      ,       lowerCAmelCase__=32      ,       lowerCAmelCase__=3      ,       lowerCAmelCase__=4      ,       lowerCAmelCase__=[10, 20, 30, 40]      ,       lowerCAmelCase__=[2, 2, 3, 2]      ,       lowerCAmelCase__=True      ,       lowerCAmelCase__=True      ,       lowerCAmelCase__=37      ,       lowerCAmelCase__="gelu"      ,       lowerCAmelCase__=10      ,       lowerCAmelCase__=0.0_2      ,       lowerCAmelCase__=["stage2", "stage3", "stage4"]      ,       lowerCAmelCase__=3      ,       lowerCAmelCase__=None      ,       )							->				Optional[int]:
							__magic_name__      :       Any       						= parent
							__magic_name__      :       Union[str, Any]       						= batch_size
							__magic_name__      :       int       						= image_size
							__magic_name__      :       List[Any]       						= num_channels
							__magic_name__      :       Tuple       						= num_stages
							__magic_name__      :       Union[str, Any]       						= hidden_sizes
							__magic_name__      :       int       						= depths
							__magic_name__      :       Tuple       						= is_training
							__magic_name__      :       Optional[int]       						= use_labels
							__magic_name__      :       int       						= intermediate_size
							__magic_name__      :       Any       						= hidden_act
							__magic_name__      :       Optional[Any]       						= type_sequence_label_size
							__magic_name__      :       Optional[int]       						= initializer_range
							__magic_name__      :       Any       						= out_features
							__magic_name__      :       List[str]       						= num_labels
							__magic_name__      :       str       						= scope
							__magic_name__      :       Union[str, Any]       						= num_stages
	def       __magic_name__    (    self			)							->				int:
							__magic_name__      :       str       						= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

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

							__magic_name__      :       Optional[int]       						= self.get_config()

							return config, pixel_values, labels
	def       __magic_name__    (    self			)							->				Optional[int]:
							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       __magic_name__    (    self			)							->				Optional[int]:
							return UperNetConfig(
							    backbone_config=self.get_backbone_config()      ,       hidden_size=5_12      ,       pool_scales=[1, 2, 3, 6]      ,       use_auxiliary_head=__A      ,       auxiliary_loss_weight=0.4      ,       auxiliary_in_channels=40      ,       auxiliary_channels=2_56      ,       auxiliary_num_convs=1      ,       auxiliary_concat_input=__A      ,       loss_ignore_index=2_55      ,       num_labels=self.num_labels      ,       )
	def       __magic_name__    (    self      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__			)							->				Optional[Any]:
							__magic_name__      :       Optional[int]       						= UperNetForSemanticSegmentation(config=__A			)
							model.to(__A			)
							model.eval()
							__magic_name__      :       Union[str, Any]       						= model(__A			)
							self.parent.assertEqual(
							    result.logits.shape      ,       (self.batch_size, self.num_labels, self.image_size, self.image_size)			)







	def       __magic_name__    (    self			)							->				str:
							__magic_name__      :       Dict       						= self.prepare_config_and_inputs()
							(
							    __magic_name__						
							)      :       List[Any]       						= config_and_inputs
							__magic_name__      :       str       						= {'''pixel_values''': pixel_values}
							return config, inputs_dict





@require_torch
class 	snake_case__ (							lowerCamelCase__						,  lowerCamelCase__						,  unittest.TestCase				):
	lowercase__						:					List[str]											=   (UperNetForSemanticSegmentation,) if is_torch_available() else ()
	lowercase__						:					List[Any]											=   {'''image-segmentation''': UperNetForSemanticSegmentation} if is_torch_available() else {}
	lowercase__						:					int											=   False
	lowercase__						:					Optional[Any]											=   False
	lowercase__						:					List[str]											=   False
	lowercase__						:					Union[str, Any]											=   False
	lowercase__						:					Optional[Any]											=   False
	lowercase__						:					Tuple											=   False
	def       __magic_name__    (    self			)							->				str:
							__magic_name__      :       List[str]       						= UperNetModelTester(self			)
							__magic_name__      :       Tuple       						= ConfigTester(self      ,       config_class=__A      ,       has_text_modality=__A      ,       hidden_size=37			)
	def       __magic_name__    (    self			)							->				str:
							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       __magic_name__    (    self			)							->				Optional[int]:
							return
	def       __magic_name__    (    self			)							->				Union[str, Any]:
							__magic_name__      :       int       						= self.model_tester.prepare_config_and_inputs_for_common()

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

													__magic_name__      :       List[Any]       						= ['''pixel_values''']
													self.assertListEqual(arg_names[:1]      ,       __A			)
	def       __magic_name__    (    self			)							->				Any:
							__magic_name__      :       int       						= 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       __magic_name__    (    self			)							->				Optional[int]:
							pass
	@unittest.skip(reason="""UperNet does not support input and output embeddings"""			)
	def       __magic_name__    (    self			)							->				List[str]:
							pass
	@unittest.skip(reason="""UperNet does not have a base model"""			)
	def       __magic_name__    (    self			)							->				Optional[int]:
							pass
	@unittest.skip(reason="""UperNet does not have a base model"""			)
	def       __magic_name__    (    self			)							->				str:
							pass
	@require_torch_multi_gpu
	@unittest.skip(reason="""UperNet has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`"""			)
	def       __magic_name__    (    self			)							->				List[str]:
							pass
	@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""			)
	def       __magic_name__    (    self			)							->				Union[str, Any]:
							pass
	def       __magic_name__    (    self			)							->				Union[str, Any]:
							def check_hidden_states_output(lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__			):
													__magic_name__      :       Union[str, Any]       						= model_class(__A			)
													model.to(__A			)
													model.eval()

													with torch.no_grad():
																			__magic_name__      :       str       						= model(**self._prepare_for_class(__A      ,       __A			)			)

													__magic_name__      :       List[str]       						= outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

													__magic_name__      :       Union[str, 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]      ,       )

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

							for model_class in self.all_model_classes:
													__magic_name__      :       Optional[Any]       						= True
													check_hidden_states_output(__A      ,       __A      ,       __A			)

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

													check_hidden_states_output(__A      ,       __A      ,       __A			)
	def       __magic_name__    (    self			)							->				List[str]:
							__magic_name__      :       Union[str, Any]       						= self.model_tester.prepare_config_and_inputs_for_common()

							__magic_name__      :       int       						= _config_zero_init(__A			)
							__magic_name__      :       Optional[Any]       						= _config_zero_init(configs_no_init.backbone_config			)
							for model_class in self.all_model_classes:
													__magic_name__      :       Union[str, Any]       						= 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       __magic_name__    (    self			)							->				int:
							pass







	@slow
	def       __magic_name__    (    self			)							->				Dict:
							for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__magic_name__      :       List[Any]       						= UperNetForSemanticSegmentation.from_pretrained(__A			)
													self.assertIsNotNone(__A			)



def    UpperCamelCase						(	):






						"""simple docstring"""







						__magic_name__      :       Tuple       						= hf_hub_download(
						    repo_id="""hf-internal-testing/fixtures_ade20k""",   repo_type="""dataset""",   filename="""ADE_val_00000001.jpg"""			)
						__magic_name__      :       str       						= Image.open(_A			).convert("""RGB"""			)
						return image





@require_torch
@require_vision
@slow
class 	snake_case__ (							unittest.TestCase				):
	def       __magic_name__    (    self			)							->				Any:
							__magic_name__      :       int       						= AutoImageProcessor.from_pretrained("""openmmlab/upernet-swin-tiny"""			)
							__magic_name__      :       Union[str, Any]       						= UperNetForSemanticSegmentation.from_pretrained("""openmmlab/upernet-swin-tiny"""			).to(__A			)

							__magic_name__      :       Optional[Any]       						= prepare_img()
							__magic_name__      :       Tuple       						= processor(images=__A      ,       return_tensors="""pt"""			).to(__A			)

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

							__magic_name__      :       Union[str, Any]       						= torch.Size((1, model.config.num_labels, 5_12, 5_12)			)
							self.assertEqual(outputs.logits.shape      ,       __A			)

							__magic_name__      :       Union[str, Any]       						= torch.tensor(
							    [[-7.5_9_5_8, -7.5_9_5_8, -7.4_3_0_2], [-7.5_9_5_8, -7.5_9_5_8, -7.4_3_0_2], [-7.4_7_9_7, -7.4_7_9_7, -7.3_0_6_8]]			).to(__A			)
							self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]      ,       __A      ,       atol=1e-4			)			)







	def       __magic_name__    (    self			)							->				int:
							__magic_name__      :       List[str]       						= AutoImageProcessor.from_pretrained("""openmmlab/upernet-convnext-tiny"""			)
							__magic_name__      :       List[Any]       						= UperNetForSemanticSegmentation.from_pretrained("""openmmlab/upernet-convnext-tiny"""			).to(__A			)

							__magic_name__      :       Any       						= prepare_img()
							__magic_name__      :       Dict       						= processor(images=__A      ,       return_tensors="""pt"""			).to(__A			)

							with torch.no_grad():
													__magic_name__      :       Tuple       						= model(**__A			)

							__magic_name__      :       Dict       						= torch.Size((1, model.config.num_labels, 5_12, 5_12)			)
							self.assertEqual(outputs.logits.shape      ,       __A			)

							__magic_name__      :       Dict       						= torch.tensor(
							    [[-8.8_1_1_0, -8.8_1_1_0, -8.6_5_2_1], [-8.8_1_1_0, -8.8_1_1_0, -8.6_5_2_1], [-8.7_7_4_6, -8.7_7_4_6, -8.6_1_3_0]]			).to(__A			)
							self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]      ,       __A      ,       atol=1e-4			)			)








import os
from typing import Dict, List, Union

import tensorflow as tf
from keras_nlp.tokenizers import BytePairTokenizer
from tensorflow_text import pad_model_inputs

from .tokenization_gpta import GPTaTokenizer





class 	snake_case__ (							tf.keras.layers.Layer				):
	def __init__(    self      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__ = None      ,       lowerCAmelCase__ = None			)							->				int:
							super().__init__()
							__magic_name__      :       Any       						= pad_token_id
							__magic_name__      :       Any       						= max_length
							__magic_name__      :       List[str]       						= vocab
							__magic_name__      :       List[Any]       						= merges
							__magic_name__      :       int       						= BytePairTokenizer(lowerCAmelCase__      ,       lowerCAmelCase__      ,       sequence_length=lowerCAmelCase__			)
	@classmethod
	def       __magic_name__    (    cls      ,       lowerCAmelCase__      ,       *lowerCAmelCase__      ,       **lowerCAmelCase__			)							->				Any:
							__magic_name__      :       Union[str, Any]       						= [""" """.join(lowerCAmelCase__			) for m in tokenizer.bpe_ranks.keys()]
							__magic_name__      :       Union[str, Any]       						= tokenizer.get_vocab()
							return cls(lowerCAmelCase__      ,       lowerCAmelCase__      ,       *lowerCAmelCase__      ,       **lowerCAmelCase__			)
	@classmethod
	def       __magic_name__    (    cls      ,       lowerCAmelCase__      ,       *lowerCAmelCase__      ,       **lowerCAmelCase__			)							->				List[str]:
							__magic_name__      :       Optional[Any]       						= GPTaTokenizer.from_pretrained(lowerCAmelCase__      ,       *lowerCAmelCase__      ,       **lowerCAmelCase__			)
							return cls.from_tokenizer(lowerCAmelCase__      ,       *lowerCAmelCase__      ,       **lowerCAmelCase__			)
	@classmethod
	def       __magic_name__    (    cls      ,       lowerCAmelCase__			)							->				List[Any]:
							return cls(**lowerCAmelCase__			)
	def       __magic_name__    (    self			)							->				int:
							return {
							    "vocab": self.vocab,
							    "merges": self.merges,
							    "max_length": self.max_length,
							    "pad_token_id": self.pad_token_id,
							}







	def       __magic_name__    (    self      ,       lowerCAmelCase__      ,       lowerCAmelCase__ = None			)							->				int:
							__magic_name__      :       Dict       						= self.tf_tokenizer(lowerCAmelCase__			)
							__magic_name__      :       Dict       						= tf.ones_like(lowerCAmelCase__			)

							if self.pad_token_id is not None:
													# pad the tokens up to max length
													__magic_name__      :       List[Any]       						= max_length if max_length is not None else self.max_length

													if max_length is not None:
																			__magic_name__						,__magic_name__      :       List[Any]       						= pad_model_inputs(
																			    lowerCAmelCase__      ,       max_seq_length=lowerCAmelCase__      ,       pad_value=self.pad_token_id			)

							return {"attention_mask": attention_mask, "input_ids": input_ids}
