



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



	def 		UpperCamelCase				(    self				:     List[str])->				Dict:
						__lowerCAmelCase						 =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

						__lowerCAmelCase						 =self.get_config()

						return config, pixel_values, labels



	def 		UpperCamelCase				(    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 		UpperCamelCase				(    self				:     Tuple)->				str:
						return UperNetConfig(
						    backbone_config=self.get_backbone_config()					,						hidden_size=5_12					,						pool_scales=[1, 2, 3, 6]					,						use_auxiliary_head=snake_case_					,						auxiliary_loss_weight=0.4					,						auxiliary_in_channels=40					,						auxiliary_channels=2_56					,						auxiliary_num_convs=1					,						auxiliary_concat_input=snake_case_					,						loss_ignore_index=2_55					,						num_labels=self.num_labels					,						)



	def 		UpperCamelCase				(    self				:     List[Any]					,						snake_case_				:     List[str]					,						snake_case_				:     str					,						snake_case_				:     Any)->				Union[str, Any]:
						__lowerCAmelCase						 =UperNetForSemanticSegmentation(config=snake_case_)
						model.to(snake_case_)
						model.eval()
						__lowerCAmelCase						 =model(snake_case_)
						self.parent.assertEqual(
						    result.logits.shape					,						(self.batch_size, self.num_labels, self.image_size, self.image_size))



	def 		UpperCamelCase				(    self				:     Dict)->				str:
						__lowerCAmelCase						 =self.prepare_config_and_inputs()
						(
						    (
						    __lowerCAmelCase
						) ,						(
						    __lowerCAmelCase
						) ,						(
						    __lowerCAmelCase
						) ,						
						)						 =config_and_inputs
						__lowerCAmelCase						 ={"""pixel_values""": pixel_values}
						return config, inputs_dict






@require_torch
class     __a			(  SCREAMING_SNAKE_CASE						,     SCREAMING_SNAKE_CASE						,     unittest.TestCase					):
	SCREAMING_SNAKE_CASE  	=							(UperNetForSemanticSegmentation,) if is_torch_available() else ()
	SCREAMING_SNAKE_CASE  	=							{"image-segmentation": UperNetForSemanticSegmentation} if is_torch_available() else {}
	SCREAMING_SNAKE_CASE  	=							False
	SCREAMING_SNAKE_CASE  	=							False
	SCREAMING_SNAKE_CASE  	=							False
	SCREAMING_SNAKE_CASE  	=							False
	SCREAMING_SNAKE_CASE  	=							False
	SCREAMING_SNAKE_CASE  	=							False



	def 		UpperCamelCase				(    self				:     Union[str, Any])->				Tuple:
						__lowerCAmelCase						 =UperNetModelTester(self)
						__lowerCAmelCase						 =ConfigTester(self					,						config_class=snake_case_					,						has_text_modality=snake_case_					,						hidden_size=37)



	def 		UpperCamelCase				(    self				:     Any)->				int:
						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				:     int)->				List[str]:
						return



	def 		UpperCamelCase				(    self				:     Union[str, Any])->				int:
						__lowerCAmelCase ,						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs_for_common()

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

											__lowerCAmelCase						 =["""pixel_values"""]
											self.assertListEqual(arg_names[:1]					,						snake_case_)



	def 		UpperCamelCase				(    self				:     int)->				str:
						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_semantic_segmentation(*snake_case_)



	@unittest.skip(reason="""UperNet does not use inputs_embeds""")
	def 		UpperCamelCase				(    self				:     List[str])->				Union[str, Any]:
						pass



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



	@unittest.skip(reason="""UperNet does not have a base model""")
	def 		UpperCamelCase				(    self				:     Optional[int])->				Optional[Any]:
						pass



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



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



	def 		UpperCamelCase				(    self				:     str)->				Optional[Any]:
						def check_hidden_states_output(snake_case_				:     Dict					,						snake_case_				:     Any					,						snake_case_				:     Union[str, Any]):
											__lowerCAmelCase						 =model_class(snake_case_)
											model.to(snake_case_)
											model.eval()

											with torch.no_grad():
																__lowerCAmelCase						 =model(**self._prepare_for_class(snake_case_					,						snake_case_))

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

											__lowerCAmelCase						 =self.model_tester.num_stages
											self.assertEqual(len(snake_case_)					,						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]					,						)

						__lowerCAmelCase ,						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
											__lowerCAmelCase						 =True
											check_hidden_states_output(snake_case_					,						snake_case_					,						snake_case_)

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

											check_hidden_states_output(snake_case_					,						snake_case_					,						snake_case_)



	def 		UpperCamelCase				(    self				:     Dict)->				str:
						__lowerCAmelCase ,						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs_for_common()

						__lowerCAmelCase						 =_config_zero_init(snake_case_)
						__lowerCAmelCase						 =_config_zero_init(configs_no_init.backbone_config)
						for model_class in self.all_model_classes:
											__lowerCAmelCase						 =model_class(config=snake_case_)
											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				:     Dict)->				List[str]:
						pass



	@slow
	def 		UpperCamelCase				(    self				:     str)->				Optional[Any]:
						for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__lowerCAmelCase						 =UperNetForSemanticSegmentation.from_pretrained(snake_case_)
											self.assertIsNotNone(snake_case_)






def        __lowerCAmelCase		(	)	->    int:
					__lowerCAmelCase						 =hf_hub_download(
					    repo_id="""hf-internal-testing/fixtures_ade20k"""					, repo_type="""dataset"""					, filename="""ADE_val_00000001.jpg"""						)
					__lowerCAmelCase						 =Image.open(__lowerCamelCase						).convert("""RGB"""						)
					return image






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



	def 		UpperCamelCase				(    self				:     int)->				int:
						__lowerCAmelCase						 =AutoImageProcessor.from_pretrained("""openmmlab/upernet-swin-tiny""")
						__lowerCAmelCase						 =UperNetForSemanticSegmentation.from_pretrained("""openmmlab/upernet-swin-tiny""").to(snake_case_)

						__lowerCAmelCase						 =prepare_img()
						__lowerCAmelCase						 =processor(images=snake_case_					,						return_tensors="""pt""").to(snake_case_)

						with torch.no_grad():
											__lowerCAmelCase						 =model(**snake_case_)

						__lowerCAmelCase						 =torch.Size((1, model.config.num_labels, 5_12, 5_12))
						self.assertEqual(outputs.logits.shape					,						snake_case_)

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



	def 		UpperCamelCase				(    self				:     Optional[int])->				Dict:
						__lowerCAmelCase						 =AutoImageProcessor.from_pretrained("""openmmlab/upernet-convnext-tiny""")
						__lowerCAmelCase						 =UperNetForSemanticSegmentation.from_pretrained("""openmmlab/upernet-convnext-tiny""").to(snake_case_)

						__lowerCAmelCase						 =prepare_img()
						__lowerCAmelCase						 =processor(images=snake_case_					,						return_tensors="""pt""").to(snake_case_)

						with torch.no_grad():
											__lowerCAmelCase						 =model(**snake_case_)

						__lowerCAmelCase						 =torch.Size((1, model.config.num_labels, 5_12, 5_12))
						self.assertEqual(outputs.logits.shape					,						snake_case_)

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









from manim import *






class     __a			(  SCREAMING_SNAKE_CASE					):



	def 		UpperCamelCase				(    self				:     Tuple)->				Dict:
						__lowerCAmelCase						 =Rectangle(height=0.5					,						width=0.5)
						__lowerCAmelCase						 =Rectangle(height=0.4_6					,						width=0.4_6).set_stroke(width=0)

						__lowerCAmelCase						 =[mem.copy() for i in range(6)]
						__lowerCAmelCase						 =[mem.copy() for i in range(6)]
						__lowerCAmelCase						 =VGroup(*snake_case_).arrange(snake_case_					,						buff=0)
						__lowerCAmelCase						 =VGroup(*snake_case_).arrange(snake_case_					,						buff=0)
						__lowerCAmelCase						 =VGroup(snake_case_					,						snake_case_).arrange(snake_case_					,						buff=0)
						__lowerCAmelCase						 =Text("""CPU"""					,						font_size=24)
						__lowerCAmelCase						 =Group(snake_case_					,						snake_case_).arrange(snake_case_					,						buff=0.5					,						aligned_edge=snake_case_)
						cpu.move_to([-2.5, -0.5, 0])
						self.add(snake_case_)

						__lowerCAmelCase						 =[mem.copy() for i in range(1)]
						__lowerCAmelCase						 =VGroup(*snake_case_).arrange(snake_case_					,						buff=0)
						__lowerCAmelCase						 =Text("""GPU"""					,						font_size=24)
						__lowerCAmelCase						 =Group(snake_case_					,						snake_case_).arrange(snake_case_					,						buff=0.5					,						aligned_edge=snake_case_)
						gpu.align_to(snake_case_					,						snake_case_)
						gpu.set_x(gpu.get_x() - 1)

						self.add(snake_case_)

						__lowerCAmelCase						 =[mem.copy() for i in range(6)]
						__lowerCAmelCase						 =VGroup(*snake_case_).arrange(snake_case_					,						buff=0)

						__lowerCAmelCase						 =Text("""Model"""					,						font_size=24)
						__lowerCAmelCase						 =Group(snake_case_					,						snake_case_).arrange(snake_case_					,						buff=0.5					,						aligned_edge=snake_case_)
						model.move_to([3, -1.0, 0])

						self.play(
						    Create(snake_case_					,						run_time=1)					,						Create(snake_case_					,						run_time=1)					,						Create(snake_case_					,						run_time=1)					,						)

						__lowerCAmelCase						 =MarkupText(
						    F"""First, an empty model skeleton is loaded\ninto <span fgcolor='{YELLOW}'>memory</span> without using much RAM."""					,						font_size=24					,						)

						__lowerCAmelCase						 =Square(side_length=2.2)
						key.move_to([-5, 2, 0])

						__lowerCAmelCase						 =MarkupText(
						    F"""<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model"""					,						font_size=18					,						)

						key_text.move_to([-5, 2.4, 0])

						step_a.move_to([2, 2, 0])
						self.play(Write(snake_case_					,						run_time=2.5)					,						Write(snake_case_)					,						Write(snake_case_))

						self.add(snake_case_)

						__lowerCAmelCase						 =[]
						__lowerCAmelCase						 =[]
						__lowerCAmelCase						 =[]
						for i, rect in enumerate(snake_case_):
											__lowerCAmelCase						 =Rectangle(height=0.4_6					,						width=0.4_6).set_stroke(width=0.0).set_fill(snake_case_					,						opacity=0.7)
											cpu_target.move_to(snake_case_)
											cpu_target.generate_target()
											__lowerCAmelCase						 =0.4_6 / 4
											__lowerCAmelCase						 =0.4_6 / 3

											if i == 0:
																cpu_target.target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT)					,						buff=0.0_2					,						direction=snake_case_)
																cpu_target.target.set_x(cpu_target.target.get_x() + 0.1)
											elif i == 3:
																cpu_target.target.next_to(cpu_targs[0].target					,						direction=snake_case_					,						buff=0.0)
											else:
																cpu_target.target.next_to(cpu_targs[i - 1].target					,						direction=snake_case_					,						buff=0.0)
											cpu_targs.append(snake_case_)

											first_animations.append(rect.animate(run_time=0.5).set_stroke(snake_case_))
											second_animations.append(MoveToTarget(snake_case_					,						run_time=1.5))

						self.play(*snake_case_)
						self.play(*snake_case_)

						self.wait()




