

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






			def __init__(  self			,_snake_case			,_snake_case=13			,_snake_case=32			,_snake_case=3			,_snake_case=4			,_snake_case=[10, 20, 30, 40]			,_snake_case=[2, 2, 3, 2]			,_snake_case=True			,_snake_case=True			,_snake_case=37			,_snake_case="gelu"			,_snake_case=10			,_snake_case=0.02			,_snake_case=["stage2", "stage3", "stage4"]			,_snake_case=3			,_snake_case=None			,):
				UpperCAmelCase_     :      Dict						=  parent
				UpperCAmelCase_     :      List[str]						=  batch_size
				UpperCAmelCase_     :      Any						=  image_size
				UpperCAmelCase_     :      Optional[int]						=  num_channels
				UpperCAmelCase_     :      List[Any]						=  num_stages
				UpperCAmelCase_     :      List[str]						=  hidden_sizes
				UpperCAmelCase_     :      Tuple						=  depths
				UpperCAmelCase_     :      Any						=  is_training
				UpperCAmelCase_     :      Optional[int]						=  use_labels
				UpperCAmelCase_     :      Optional[Any]						=  intermediate_size
				UpperCAmelCase_     :      Optional[int]						=  hidden_act
				UpperCAmelCase_     :      Dict						=  type_sequence_label_size
				UpperCAmelCase_     :      str						=  initializer_range
				UpperCAmelCase_     :      Optional[int]						=  out_features
				UpperCAmelCase_     :      List[str]						=  num_labels
				UpperCAmelCase_     :      Optional[Any]						=  scope
				UpperCAmelCase_     :      Optional[int]						=  num_stages






			def 		UpperCamelCase__							(  self     ):
				UpperCAmelCase_     :      Any						=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

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

				UpperCAmelCase_     :      Any						=  self.get_config()

				return config, pixel_values, labels






			def 		UpperCamelCase__							(  self     ):
				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     ):
				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 		UpperCamelCase__							(  self			,_snake_case			,_snake_case			,_snake_case     ):
				UpperCAmelCase_     :      Dict						=  UperNetForSemanticSegmentation(config=_a     )
				model.to(_a     )
				model.eval()
				UpperCAmelCase_     :      List[str]						=  model(_a     )
				self.parent.assertEqual(
				    result.logits.shape			,(self.batch_size, self.num_labels, self.image_size, self.image_size)     )






			def 		UpperCamelCase__							(  self     ):
				UpperCAmelCase_     :      Tuple						=  self.prepare_config_and_inputs()
				(
				    UpperCAmelCase_   
				)     :      Dict						=  config_and_inputs
				UpperCAmelCase_     :      Union[str, Any]						=  {'''pixel_values''': pixel_values}
				return config, inputs_dict




@require_torch
class 					_snake_case		(__lowercase			,			__lowercase			,			unittest.TestCase):
			__A  :	Tuple        =(UperNetForSemanticSegmentation,) if is_torch_available() else ()
			__A  :	Union[str, Any]        ={"image-segmentation": UperNetForSemanticSegmentation} if is_torch_available() else {}
			__A  :	Tuple        =False
			__A  :	int        =False
			__A  :	List[Any]        =False
			__A  :	List[Any]        =False
			__A  :	Tuple        =False
			__A  :	Tuple        =False






			def 		UpperCamelCase__							(  self     ):
				UpperCAmelCase_     :      Dict						=  UperNetModelTester(self     )
				UpperCAmelCase_     :      Tuple						=  ConfigTester(self			,config_class=_a			,has_text_modality=_a			,hidden_size=37     )






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






			def 		UpperCamelCase__							(  self     ):
				UpperCAmelCase_     :      int						=  self.model_tester.prepare_config_and_inputs_for_common()

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

					UpperCAmelCase_     :      Any						=  ['''pixel_values''']
					self.assertListEqual(arg_names[:1]			,_a     )






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






			@unittest.skip(reason="UperNet does not support input and output embeddings"     )
			def 		UpperCamelCase__							(  self     ):
				pass






			@unittest.skip(reason="UperNet does not have a base model"     )
			def 		UpperCamelCase__							(  self     ):
				pass






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






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






			def 		UpperCamelCase__							(  self     ):
				def check_hidden_states_output(_snake_case			,_snake_case			,_snake_case     ):
					UpperCAmelCase_     :      Union[str, Any]						=  model_class(_a     )
					model.to(_a     )
					model.eval()

					with torch.no_grad():
						UpperCAmelCase_     :      Any						=  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[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]			,)

				UpperCAmelCase_     :      Optional[Any]						=  self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
					UpperCAmelCase_     :      int						=  True
					check_hidden_states_output(_a			,_a			,_a     )

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

					check_hidden_states_output(_a			,_a			,_a     )






			def 		UpperCamelCase__							(  self     ):
				UpperCAmelCase_     :      int						=  self.model_tester.prepare_config_and_inputs_for_common()

				UpperCAmelCase_     :      List[str]						=  _config_zero_init(_a     )
				UpperCAmelCase_     :      Tuple						=  _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     ):
				pass






			@slow
			def 		UpperCamelCase__							(  self     ):
				for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					UpperCAmelCase_     :      Tuple						=  UperNetForSemanticSegmentation.from_pretrained(_a     )
					self.assertIsNotNone(_a     )




def  a__       (    )     ->					Dict:


	"""simple docstring"""





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





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






			def 		UpperCamelCase__							(  self     ):
				UpperCAmelCase_     :      List[str]						=  AutoImageProcessor.from_pretrained("openmmlab/upernet-swin-tiny"     )
				UpperCAmelCase_     :      List[str]						=  UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-swin-tiny"     ).to(_a     )

				UpperCAmelCase_     :      List[str]						=  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, 5_12, 5_12)     )
				self.assertEqual(outputs.logits.shape			,_a     )

				UpperCAmelCase_     :      Tuple						=  torch.tensor(
				    [[-7.5958, -7.5958, -7.4302], [-7.5958, -7.5958, -7.4302], [-7.4797, -7.4797, -7.3068]]     ).to(_a     )
				self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]			,_a			,atol=1E-4     )     )






			def 		UpperCamelCase__							(  self     ):
				UpperCAmelCase_     :      Tuple						=  AutoImageProcessor.from_pretrained("openmmlab/upernet-convnext-tiny"     )
				UpperCAmelCase_     :      Union[str, Any]						=  UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-convnext-tiny"     ).to(_a     )

				UpperCAmelCase_     :      Dict						=  prepare_img()
				UpperCAmelCase_     :      Union[str, Any]						=  processor(images=_a			,return_tensors="pt"     ).to(_a     )

				with torch.no_grad():
					UpperCAmelCase_     :      Dict						=  model(**_a     )

				UpperCAmelCase_     :      int						=  torch.Size((1, model.config.num_labels, 5_12, 5_12)     )
				self.assertEqual(outputs.logits.shape			,_a     )

				UpperCAmelCase_     :      Any						=  torch.tensor(
				    [[-8.8110, -8.8110, -8.6521], [-8.8110, -8.8110, -8.6521], [-8.7746, -8.7746, -8.6130]]     ).to(_a     )
				self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]			,_a			,atol=1E-4     )     )







import json
import os
import unittest

from transformers import MgpstrTokenizer
from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin

@require_tokenizers
class 							UpperCAmelCase_						(     __lowercase							,			unittest.TestCase  ):
							"""simple docstring"""







							UpperCAmelCase__			:		List[Any]             =       MgpstrTokenizer
							UpperCAmelCase__			:		int             =       False
							UpperCAmelCase__			:		Union[str, Any]             =       {}
							UpperCAmelCase__			:		List[Any]             =       False


							def 							__lowercase							(     self			)       ->       Any:
												super().setUp()

												# fmt: off
												_a      :       Tuple   =						['''[GO]''', '''[s]''', '''0''', '''1''', '''2''', '''3''', '''4''', '''5''', '''6''', '''7''', '''8''', '''9''', '''a''', '''b''', '''c''', '''d''', '''e''', '''f''', '''g''', '''h''', '''i''', '''j''', '''k''', '''l''', '''m''', '''n''', '''o''', '''p''', '''q''', '''r''', '''s''', '''t''', '''u''', '''v''', '''w''', '''x''', '''y''', '''z''']
												# fmt: on
												_a      :       Optional[int]   =						dict(zip(_a			,				range(len(_a			)			)			)			)

												_a      :       Any   =						os.path.join(self.tmpdirname			,				VOCAB_FILES_NAMES['''vocab_file''']			)
												with open(self.vocab_file			,				'''w'''			,				encoding='''utf-8'''			) as fp:
																	fp.write(json.dumps(_a			) + '''\n'''			)


							def 							__lowercase							(     self			,				**_a			)       ->       Dict:
												return MgpstrTokenizer.from_pretrained(self.tmpdirname			,				**_a			)


							def 							__lowercase							(     self			,				_a			)       ->       Tuple:
												_a      :       List[str]   =						'''tester'''
												_a      :       Optional[Any]   =						'''tester'''
												return input_text, output_text


							@unittest.skip('''MGP-STR always lower cases letters.'''			)
							def 							__lowercase							(     self			)       ->       Any:
												pass


							def 							__lowercase							(     self			)       ->       Any:
												_a      :       Union[str, Any]   =						self.get_tokenizers(do_lower_case=_a			)
												for tokenizer in tokenizers:
																	with self.subTest(F"""{tokenizer.__class__.__name__}"""			):
																						_a      :       int   =						'''[SPECIAL_TOKEN]'''

																						tokenizer.add_special_tokens({'''cls_token''': special_token}			)
																						_a      :       Tuple   =						tokenizer.encode([special_token]			,				add_special_tokens=_a			)
																						self.assertEqual(len(_a			)			,				1			)

																						_a      :       Tuple   =						tokenizer.decode(_a			,				skip_special_tokens=_a			)
																						self.assertTrue(special_token not in decoded			)


							def 							__lowercase							(     self			)       ->       Tuple:
												_a      :       List[Any]   =						self.get_tokenizers()
												for tokenizer in tokenizers:
																	with self.subTest(F"""{tokenizer.__class__.__name__}"""			):
																						_a ,   _a      :       int   =						self.get_input_output_texts(_a			)

																						_a      :       List[str]   =						tokenizer.tokenize(_a			)
																						_a      :       Optional[int]   =						tokenizer.convert_tokens_to_ids(_a			)
																						_a      :       Tuple   =						tokenizer.encode(_a			,				add_special_tokens=_a			)
																						self.assertListEqual(_a			,				_a			)

																						_a      :       Optional[int]   =						tokenizer.convert_ids_to_tokens(_a			)
																						self.assertNotEqual(len(_a			)			,				0			)
																						_a      :       int   =						tokenizer.decode(_a			)
																						self.assertIsInstance(_a			,				_a			)

																						self.assertEqual(text_a.replace(''' '''			,				''''''			)			,				_a			)


							@unittest.skip('''MGP-STR tokenizer only handles one sequence.'''			)
							def 							__lowercase							(     self			)       ->       List[str]:
												pass




							@unittest.skip('''inputs cannot be pretokenized in MgpstrTokenizer'''			)
							def 							__lowercase							(     self			)       ->       Optional[Any]:
												pass



