






"""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_							:







				def __init__(							self							:						Dict						,    __lowerCamelCase							:						Optional[int]						,    __lowerCamelCase							:						int=1_3						,    __lowerCamelCase							:						Optional[int]=3_2						,    __lowerCamelCase							:						str=3						,    __lowerCamelCase							:						Optional[int]=4						,    __lowerCamelCase							:						int=[1_0, 2_0, 3_0, 4_0]						,    __lowerCamelCase							:						List[str]=[2, 2, 3, 2]						,    __lowerCamelCase							:						Union[str, Any]=True						,    __lowerCamelCase							:						List[str]=True						,    __lowerCamelCase							:						str=3_7						,    __lowerCamelCase							:						List[Any]="gelu"						,    __lowerCamelCase							:						int=1_0						,    __lowerCamelCase							:						List[Any]=0.02						,    __lowerCamelCase							:						Optional[Any]=["stage2", "stage3", "stage4"]						,    __lowerCamelCase							:						List[Any]=3						,    __lowerCamelCase							:						Union[str, Any]=None						,    )				->					Optional[Any]:
										__magic_name__     :   Union[str, Any]            =     parent
										__magic_name__     :   Tuple            =     batch_size
										__magic_name__     :   Tuple            =     image_size
										__magic_name__     :   Union[str, Any]            =     num_channels
										__magic_name__     :   Optional[int]            =     num_stages
										__magic_name__     :   Dict            =     hidden_sizes
										__magic_name__     :   Any            =     depths
										__magic_name__     :   Union[str, Any]            =     is_training
										__magic_name__     :   Union[str, Any]            =     use_labels
										__magic_name__     :   List[Any]            =     intermediate_size
										__magic_name__     :   Any            =     hidden_act
										__magic_name__     :   str            =     type_sequence_label_size
										__magic_name__     :   List[Any]            =     initializer_range
										__magic_name__     :   Dict            =     out_features
										__magic_name__     :   Dict            =     num_labels
										__magic_name__     :   str            =     scope
										__magic_name__     :   str            =     num_stages







				def     _snake_case					(							self							:						int	)				->					List[str]:
										__magic_name__     :   Tuple            =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

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

										__magic_name__     :   List[str]            =     self.get_config()

										return config, pixel_values, labels







				def     _snake_case					(							self							:						Dict	)				->					Dict:
										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							:						List[Any]	)				->					Union[str, Any]:
										return UperNetConfig(
										    backbone_config=self.get_backbone_config()						,    hidden_size=5_1_2						,    pool_scales=[1, 2, 3, 6]						,    use_auxiliary_head=_UpperCAmelCase						,    auxiliary_loss_weight=0.4						,    auxiliary_in_channels=4_0						,    auxiliary_channels=2_5_6						,    auxiliary_num_convs=1						,    auxiliary_concat_input=_UpperCAmelCase						,    loss_ignore_index=2_5_5						,    num_labels=self.num_labels						,    )







				def     _snake_case					(							self							:						Optional[Any]						,    __lowerCamelCase							:						Dict						,    __lowerCamelCase							:						Optional[int]						,    __lowerCamelCase							:						Dict	)				->					Optional[Any]:
										__magic_name__     :   Any            =     UperNetForSemanticSegmentation(config=_UpperCAmelCase	)
										model.to(_UpperCAmelCase	)
										model.eval()
										__magic_name__     :   List[str]            =     model(_UpperCAmelCase	)
										self.parent.assertEqual(
										    result.logits.shape						,    (self.batch_size, self.num_labels, self.image_size, self.image_size)	)







				def     _snake_case					(							self							:						int	)				->					Optional[Any]:
										__magic_name__     :   List[str]            =     self.prepare_config_and_inputs()
										(
										    (
										    __magic_name__					
										)      ,    (
										    __magic_name__					
										)      ,    (
										    __magic_name__					
										)      ,    					
										)     :   List[str]            =     config_and_inputs
										__magic_name__     :   Tuple            =     {"pixel_values": pixel_values}
										return config, inputs_dict





@require_torch
class 						A_							(      snake_case_					,							snake_case_					,							unittest.TestCase  ):
				UpperCAmelCase__        =				(UperNetForSemanticSegmentation,) if is_torch_available() else ()
				UpperCAmelCase__        =				{'''image-segmentation''': UperNetForSemanticSegmentation} if is_torch_available() else {}
				UpperCAmelCase__        =				False
				UpperCAmelCase__        =				False
				UpperCAmelCase__        =				False
				UpperCAmelCase__        =				False
				UpperCAmelCase__        =				False
				UpperCAmelCase__        =				False







				def     _snake_case					(							self							:						List[str]	)				->					Optional[int]:
										__magic_name__     :   str            =     UperNetModelTester(self	)
										__magic_name__     :   str            =     ConfigTester(self						,    config_class=_UpperCAmelCase						,    has_text_modality=_UpperCAmelCase						,    hidden_size=3_7	)







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







				def     _snake_case					(							self							:						Optional[int]	)				->					List[Any]:
										__magic_name__      ,    __magic_name__     :   Dict            =     self.model_tester.prepare_config_and_inputs_for_common()

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

																__magic_name__     :   str            =     ["pixel_values"]
																self.assertListEqual(arg_names[:1]						,    _UpperCAmelCase	)







				def     _snake_case					(							self							:						List[str]	)				->					str:
										__magic_name__     :   Dict            =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_semantic_segmentation(*_UpperCAmelCase	)







				@unittest.skip(reason="UperNet does not use inputs_embeds"	)
				def     _snake_case					(							self							:						List[str]	)				->					Optional[int]:
										pass







				@unittest.skip(reason="UperNet does not support input and output embeddings"	)
				def     _snake_case					(							self							:						List[str]	)				->					Any:
										pass







				@unittest.skip(reason="UperNet does not have a base model"	)
				def     _snake_case					(							self							:						str	)				->					Tuple:
										pass







				@unittest.skip(reason="UperNet does not have a base model"	)
				def     _snake_case					(							self							:						Optional[Any]	)				->					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     _snake_case					(							self							:						List[Any]	)				->					Dict:
										pass







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







				def     _snake_case					(							self							:						int	)				->					Tuple:




										def check_hidden_states_output(__lowerCamelCase							:						Any						,    __lowerCamelCase							:						Optional[Any]						,    __lowerCamelCase							:						str	):
																__magic_name__     :   int            =     model_class(_UpperCAmelCase	)
																model.to(_UpperCAmelCase	)
																model.eval()

																with torch.no_grad():
																						__magic_name__     :   List[Any]            =     model(**self._prepare_for_class(_UpperCAmelCase						,    _UpperCAmelCase	)	)

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

																__magic_name__     :   Dict            =     self.model_tester.num_stages
																self.assertEqual(len(_UpperCAmelCase	)						,    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__      ,    __magic_name__     :   Union[str, Any]            =     self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																__magic_name__     :   Any            =     True
																check_hidden_states_output(_UpperCAmelCase						,    _UpperCAmelCase						,    _UpperCAmelCase	)

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

																check_hidden_states_output(_UpperCAmelCase						,    _UpperCAmelCase						,    _UpperCAmelCase	)







				def     _snake_case					(							self							:						Union[str, Any]	)				->					Optional[int]:
										__magic_name__      ,    __magic_name__     :   Optional[int]            =     self.model_tester.prepare_config_and_inputs_for_common()

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







				@slow
				def     _snake_case					(							self							:						Union[str, Any]	)				->					Dict:




										for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__magic_name__     :   List[Any]            =     UperNetForSemanticSegmentation.from_pretrained(_UpperCAmelCase	)
																self.assertIsNotNone(_UpperCAmelCase	)






def _lowerCAmelCase	(  ):

						'''simple docstring'''






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


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







				def     _snake_case					(							self							:						Optional[int]	)				->					Dict:
										__magic_name__     :   Any            =     AutoImageProcessor.from_pretrained("openmmlab/upernet-swin-tiny"	)
										__magic_name__     :   List[Any]            =     UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-swin-tiny"	).to(_UpperCAmelCase	)

										__magic_name__     :   List[Any]            =     prepare_img()
										__magic_name__     :   Union[str, Any]            =     processor(images=_UpperCAmelCase						,    return_tensors="pt"	).to(_UpperCAmelCase	)

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

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

										__magic_name__     :   List[Any]            =     torch.tensor(
										    [[-7.5958, -7.5958, -7.4302], [-7.5958, -7.5958, -7.4302], [-7.4797, -7.4797, -7.3068]]	).to(_UpperCAmelCase	)
										self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]						,    _UpperCAmelCase						,    atol=1e-4	)	)







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

										__magic_name__     :   List[Any]            =     prepare_img()
										__magic_name__     :   Union[str, Any]            =     processor(images=_UpperCAmelCase						,    return_tensors="pt"	).to(_UpperCAmelCase	)

										with torch.no_grad():
																__magic_name__     :   Dict            =     model(**_UpperCAmelCase	)

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

										__magic_name__     :   int            =     torch.tensor(
										    [[-8.8110, -8.8110, -8.6521], [-8.8110, -8.8110, -8.6521], [-8.7746, -8.7746, -8.6130]]	).to(_UpperCAmelCase	)
										self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]						,    _UpperCAmelCase						,    atol=1e-4	)	)











"""simple docstring"""

def _lowerCAmelCase	(  __lowerCamelCase:int   ):

						'''simple docstring'''






						__magic_name__            =     n ** (1 / 3)
						return (val * val * val) == n


if __name__ == "__main__":
			print(perfect_cube(27))
			print(perfect_cube(4))



