
# This model implementation is heavily inspired by https://github.com/haofanwang/ControlNet-for-Diffusers/

import gc
import random
import tempfile
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    ControlNetModel,
    DDIMScheduler,
    StableDiffusionControlNetImgaImgPipeline,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet import MultiControlNetModel
from diffusers.utils import floats_tensor, load_image, load_numpy, randn_tensor, slow, torch_device
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
    IMAGE_TO_IMAGE_IMAGE_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import (
    PipelineKarrasSchedulerTesterMixin,
    PipelineLatentTesterMixin,
    PipelineTesterMixin,
)


enable_full_determinism()



class    a_ (			_a					,			_a					,			_a					,			unittest.TestCase    ):
			a	:			Dict					   =							StableDiffusionControlNetImgaImgPipeline
			a	:			Any					   =							TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'''height''', '''width'''}
			a	:			Dict					   =							TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
			a	:			Tuple					   =							IMAGE_TO_IMAGE_IMAGE_PARAMS.union({'''control_image'''}    )
			a	:			Optional[int]					   =							IMAGE_TO_IMAGE_IMAGE_PARAMS






			def    UpperCamelCase_				(      self  ):
									torch.manual_seed(0  )
									_lowercase					       =					UNetaDConditionModel(
									    block_out_channels=(32, 64)				,			layers_per_block=2				,			sample_size=32				,			in_channels=4				,			out_channels=4				,			down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")				,			up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")				,			cross_attention_dim=32				,			)
									torch.manual_seed(0  )
									_lowercase					       =					ControlNetModel(
									    block_out_channels=(32, 64)				,			layers_per_block=2				,			in_channels=4				,			down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")				,			cross_attention_dim=32				,			conditioning_embedding_out_channels=(16, 32)				,			)
									torch.manual_seed(0  )
									_lowercase					       =					DDIMScheduler(
									    beta_start=0.0_00_85				,			beta_end=0.0_12				,			beta_schedule="""scaled_linear"""				,			clip_sample=__UpperCamelCase				,			set_alpha_to_one=__UpperCamelCase				,			)
									torch.manual_seed(0  )
									_lowercase					       =					AutoencoderKL(
									    block_out_channels=[32, 64]				,			in_channels=3				,			out_channels=3				,			down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]				,			up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]				,			latent_channels=4				,			)
									torch.manual_seed(0  )
									_lowercase					       =					CLIPTextConfig(
									    bos_token_id=0				,			eos_token_id=2				,			hidden_size=32				,			intermediate_size=37				,			layer_norm_eps=1E-05				,			num_attention_heads=4				,			num_hidden_layers=5				,			pad_token_id=1				,			vocab_size=1_000				,			)
									_lowercase					       =					CLIPTextModel(__UpperCamelCase  )
									_lowercase					       =					CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""  )

									_lowercase					       =					{
									    """unet""": unet,
									    """controlnet""": controlnet,
									    """scheduler""": scheduler,
									    """vae""": vae,
									    """text_encoder""": text_encoder,
									    """tokenizer""": tokenizer,
									    """safety_checker""": None,
									    """feature_extractor""": None,
									}
									return components






			def    UpperCamelCase_				(      self				,			__UpperCamelCase				,			__UpperCamelCase=0  ):
									if str(__UpperCamelCase  ).startswith("""mps"""  ):
															_lowercase					       =					torch.manual_seed(__UpperCamelCase  )
									else:
															_lowercase					       =					torch.Generator(device=__UpperCamelCase  ).manual_seed(__UpperCamelCase  )

									_lowercase					       =					2
									_lowercase					       =					randn_tensor(
									    (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor)				,			generator=__UpperCamelCase				,			device=torch.device(__UpperCamelCase  )				,			)
									_lowercase					       =					floats_tensor(control_image.shape				,			rng=random.Random(__UpperCamelCase  )  ).to(__UpperCamelCase  )
									_lowercase					       =					image.cpu().permute(0				,			2				,			3				,			1  )[0]
									_lowercase					       =					Image.fromarray(np.uinta(__UpperCamelCase  )  ).convert("""RGB"""  ).resize((64, 64)  )
									_lowercase					       =					{
									    """prompt""": """A painting of a squirrel eating a burger""",
									    """generator""": generator,
									    """num_inference_steps""": 2,
									    """guidance_scale""": 6.0,
									    """output_type""": """numpy""",
									    """image""": image,
									    """control_image""": control_image,
									}

									return inputs






			def    UpperCamelCase_				(      self  ):
									return self._test_attention_slicing_forward_pass(expected_max_diff=2E-3  )






			@unittest.skipIf(
			    torch_device != """cuda""" or not is_xformers_available()				,			reason="""XFormers attention is only available with CUDA and `xformers` installed"""				,			)
			def    UpperCamelCase_				(      self  ):
									self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2E-3  )






			def    UpperCamelCase_				(      self  ):
									self._test_inference_batch_single_identical(expected_max_diff=2E-3  )







class    a_ (			_a					,			_a					,			unittest.TestCase    ):
			a	:			str					   =							StableDiffusionControlNetImgaImgPipeline
			a	:			int					   =							TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'''height''', '''width'''}
			a	:			Optional[int]					   =							TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
			a	:			Any					   =							frozenset([]    )  # TO_DO: add image_params once refactored VaeImageProcessor.preprocess






			def    UpperCamelCase_				(      self  ):
									torch.manual_seed(0  )
									_lowercase					       =					UNetaDConditionModel(
									    block_out_channels=(32, 64)				,			layers_per_block=2				,			sample_size=32				,			in_channels=4				,			out_channels=4				,			down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")				,			up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")				,			cross_attention_dim=32				,			)
									torch.manual_seed(0  )

									def init_weights(__UpperCamelCase  ):
															if isinstance(__UpperCamelCase				,			torch.nn.Convad  ):
																					torch.nn.init.normal(m.weight  )
																					m.bias.data.fill_(1.0  )

									_lowercase					       =					ControlNetModel(
									    block_out_channels=(32, 64)				,			layers_per_block=2				,			in_channels=4				,			down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")				,			cross_attention_dim=32				,			conditioning_embedding_out_channels=(16, 32)				,			)
									controlneta.controlnet_down_blocks.apply(__UpperCamelCase  )

									torch.manual_seed(0  )
									_lowercase					       =					ControlNetModel(
									    block_out_channels=(32, 64)				,			layers_per_block=2				,			in_channels=4				,			down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")				,			cross_attention_dim=32				,			conditioning_embedding_out_channels=(16, 32)				,			)
									controlneta.controlnet_down_blocks.apply(__UpperCamelCase  )

									torch.manual_seed(0  )
									_lowercase					       =					DDIMScheduler(
									    beta_start=0.0_00_85				,			beta_end=0.0_12				,			beta_schedule="""scaled_linear"""				,			clip_sample=__UpperCamelCase				,			set_alpha_to_one=__UpperCamelCase				,			)
									torch.manual_seed(0  )
									_lowercase					       =					AutoencoderKL(
									    block_out_channels=[32, 64]				,			in_channels=3				,			out_channels=3				,			down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]				,			up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]				,			latent_channels=4				,			)
									torch.manual_seed(0  )
									_lowercase					       =					CLIPTextConfig(
									    bos_token_id=0				,			eos_token_id=2				,			hidden_size=32				,			intermediate_size=37				,			layer_norm_eps=1E-05				,			num_attention_heads=4				,			num_hidden_layers=5				,			pad_token_id=1				,			vocab_size=1_000				,			)
									_lowercase					       =					CLIPTextModel(__UpperCamelCase  )
									_lowercase					       =					CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""  )

									_lowercase					       =					MultiControlNetModel([controlneta, controlneta]  )

									_lowercase					       =					{
									    """unet""": unet,
									    """controlnet""": controlnet,
									    """scheduler""": scheduler,
									    """vae""": vae,
									    """text_encoder""": text_encoder,
									    """tokenizer""": tokenizer,
									    """safety_checker""": None,
									    """feature_extractor""": None,
									}
									return components






			def    UpperCamelCase_				(      self				,			__UpperCamelCase				,			__UpperCamelCase=0  ):
									if str(__UpperCamelCase  ).startswith("""mps"""  ):
															_lowercase					       =					torch.manual_seed(__UpperCamelCase  )
									else:
															_lowercase					       =					torch.Generator(device=__UpperCamelCase  ).manual_seed(__UpperCamelCase  )

									_lowercase					       =					2

									_lowercase					       =					[
									    randn_tensor(
									        (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor)				,			generator=__UpperCamelCase				,			device=torch.device(__UpperCamelCase  )				,			),
									    randn_tensor(
									        (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor)				,			generator=__UpperCamelCase				,			device=torch.device(__UpperCamelCase  )				,			),
									]

									_lowercase					       =					floats_tensor(control_image[0].shape				,			rng=random.Random(__UpperCamelCase  )  ).to(__UpperCamelCase  )
									_lowercase					       =					image.cpu().permute(0				,			2				,			3				,			1  )[0]
									_lowercase					       =					Image.fromarray(np.uinta(__UpperCamelCase  )  ).convert("""RGB"""  ).resize((64, 64)  )
									_lowercase					       =					{
									    """prompt""": """A painting of a squirrel eating a burger""",
									    """generator""": generator,
									    """num_inference_steps""": 2,
									    """guidance_scale""": 6.0,
									    """output_type""": """numpy""",
									    """image""": image,
									    """control_image""": control_image,
									}

									return inputs






			def    UpperCamelCase_				(      self  ):
									_lowercase					       =					self.get_dummy_components()
									_lowercase					       =					self.pipeline_class(**__UpperCamelCase  )
									pipe.to(__UpperCamelCase  )

									_lowercase					       =					10.0
									_lowercase					       =					4

									_lowercase					       =					self.get_dummy_inputs(__UpperCamelCase  )
									_lowercase					       =					steps
									_lowercase					       =					scale
									_lowercase					       =					pipe(**__UpperCamelCase  )[0]

									_lowercase					       =					self.get_dummy_inputs(__UpperCamelCase  )
									_lowercase					       =					steps
									_lowercase					       =					scale
									_lowercase					       =					pipe(**__UpperCamelCase				,			control_guidance_start=0.1				,			control_guidance_end=0.2  )[0]

									_lowercase					       =					self.get_dummy_inputs(__UpperCamelCase  )
									_lowercase					       =					steps
									_lowercase					       =					scale
									_lowercase					       =					pipe(**__UpperCamelCase				,			control_guidance_start=[0.1, 0.3]				,			control_guidance_end=[0.2, 0.7]  )[0]

									_lowercase					       =					self.get_dummy_inputs(__UpperCamelCase  )
									_lowercase					       =					steps
									_lowercase					       =					scale
									_lowercase					       =					pipe(**__UpperCamelCase				,			control_guidance_start=0.4				,			control_guidance_end=[0.5, 0.8]  )[0]

									# make sure that all outputs are different
									assert np.sum(np.abs(output_a - output_a  )  ) > 1E-3
									assert np.sum(np.abs(output_a - output_a  )  ) > 1E-3
									assert np.sum(np.abs(output_a - output_a  )  ) > 1E-3






			def    UpperCamelCase_				(      self  ):
									return self._test_attention_slicing_forward_pass(expected_max_diff=2E-3  )






			@unittest.skipIf(
			    torch_device != """cuda""" or not is_xformers_available()				,			reason="""XFormers attention is only available with CUDA and `xformers` installed"""				,			)
			def    UpperCamelCase_				(      self  ):
									self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2E-3  )






			def    UpperCamelCase_				(      self  ):
									self._test_inference_batch_single_identical(expected_max_diff=2E-3  )






			def    UpperCamelCase_				(      self  ):
									_lowercase					       =					self.get_dummy_components()
									_lowercase					       =					self.pipeline_class(**__UpperCamelCase  )
									pipe.to(__UpperCamelCase  )
									pipe.set_progress_bar_config(disable=__UpperCamelCase  )
									with tempfile.TemporaryDirectory() as tmpdir:
															try:
																					# save_pretrained is not implemented for Multi-ControlNet
																					pipe.save_pretrained(__UpperCamelCase  )
															except NotImplementedError:
																					pass







@slow
@require_torch_gpu
class    a_ (			unittest.TestCase    ):






			def    UpperCamelCase_				(      self  ):
									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()






			def    UpperCamelCase_				(      self  ):
									_lowercase					       =					ControlNetModel.from_pretrained("""lllyasviel/sd-controlnet-canny"""  )

									_lowercase					       =					StableDiffusionControlNetImgaImgPipeline.from_pretrained(
									    """runwayml/stable-diffusion-v1-5"""				,			safety_checker=__UpperCamelCase				,			controlnet=__UpperCamelCase  )
									pipe.enable_model_cpu_offload()
									pipe.set_progress_bar_config(disable=__UpperCamelCase  )

									_lowercase					       =					torch.Generator(device="""cpu"""  ).manual_seed(0  )
									_lowercase					       =					"""evil space-punk bird"""
									_lowercase					       =					load_image(
									    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png"""  ).resize((512, 512)  )
									_lowercase					       =					load_image(
									    """https://huggingface.co/lllyasviel/sd-controlnet-canny/resolve/main/images/bird.png"""  ).resize((512, 512)  )

									_lowercase					       =					pipe(
									    __UpperCamelCase				,			__UpperCamelCase				,			control_image=__UpperCamelCase				,			generator=__UpperCamelCase				,			output_type="""np"""				,			num_inference_steps=50				,			strength=0.6				,			)

									_lowercase					       =					output.images[0]

									assert image.shape == (512, 512, 3)

									_lowercase					       =					load_numpy(
									    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/img2img.npy"""  )

									assert np.abs(expected_image - image  ).max() < 9E-2

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				,			__UpperCamelCase				,			__UpperCamelCase=13				,			__UpperCamelCase=32				,			__UpperCamelCase=3				,			__UpperCamelCase=4				,			__UpperCamelCase=[10, 20, 30, 40]				,			__UpperCamelCase=[2, 2, 3, 2]				,			__UpperCamelCase=True				,			__UpperCamelCase=True				,			__UpperCamelCase=37				,			__UpperCamelCase="gelu"				,			__UpperCamelCase=10				,			__UpperCamelCase=0.02				,			__UpperCamelCase=["stage2", "stage3", "stage4"]				,			__UpperCamelCase=3				,			__UpperCamelCase=None				,			):
									_lowercase					       =					parent
									_lowercase					       =					batch_size
									_lowercase					       =					image_size
									_lowercase					       =					num_channels
									_lowercase					       =					num_stages
									_lowercase					       =					hidden_sizes
									_lowercase					       =					depths
									_lowercase					       =					is_training
									_lowercase					       =					use_labels
									_lowercase					       =					intermediate_size
									_lowercase					       =					hidden_act
									_lowercase					       =					type_sequence_label_size
									_lowercase					       =					initializer_range
									_lowercase					       =					out_features
									_lowercase					       =					num_labels
									_lowercase					       =					scope
									_lowercase					       =					num_stages






			def    UpperCamelCase_				(      self  ):
									_lowercase					       =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

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

									_lowercase					       =					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=512				,			pool_scales=[1, 2, 3, 6]				,			use_auxiliary_head=__UpperCamelCase				,			auxiliary_loss_weight=0.4				,			auxiliary_in_channels=40				,			auxiliary_channels=256				,			auxiliary_num_convs=1				,			auxiliary_concat_input=__UpperCamelCase				,			loss_ignore_index=255				,			num_labels=self.num_labels				,			)






			def    UpperCamelCase_				(      self				,			__UpperCamelCase				,			__UpperCamelCase				,			__UpperCamelCase  ):
									_lowercase					       =					UperNetForSemanticSegmentation(config=__UpperCamelCase  )
									model.to(__UpperCamelCase  )
									model.eval()
									_lowercase					       =					model(__UpperCamelCase  )
									self.parent.assertEqual(
									    result.logits.shape				,			(self.batch_size, self.num_labels, self.image_size, self.image_size)  )






			def    UpperCamelCase_				(      self  ):
									_lowercase					       =					self.prepare_config_and_inputs()
									(
									    (
									    _lowercase
									)       ,						(
									    _lowercase
									)       ,						(
									    _lowercase
									)       ,						
									)					       =					config_and_inputs
									_lowercase					       =					{"""pixel_values""": pixel_values}
									return config, inputs_dict







@require_torch
class    a_ (			_a					,			_a					,			unittest.TestCase    ):
			a	:			Optional[int]					   =							(UperNetForSemanticSegmentation,) if is_torch_available() else ()
			a	:			Optional[Any]					   =							{'''image-segmentation''': UperNetForSemanticSegmentation} if is_torch_available() else {}
			a	:			Any					   =							False
			a	:			Union[str, Any]					   =							False
			a	:			Optional[Any]					   =							False
			a	:			int					   =							False
			a	:			Optional[int]					   =							False
			a	:			List[str]					   =							False






			def    UpperCamelCase_				(      self  ):
									_lowercase					       =					UperNetModelTester(self  )
									_lowercase					       =					ConfigTester(self				,			config_class=__UpperCamelCase				,			has_text_modality=__UpperCamelCase				,			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  ):
									_lowercase       ,						_lowercase					       =					self.model_tester.prepare_config_and_inputs_for_common()

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

															_lowercase					       =					["""pixel_values"""]
															self.assertListEqual(arg_names[:1]				,			__UpperCamelCase  )






			def    UpperCamelCase_				(      self  ):
									_lowercase					       =					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    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(__UpperCamelCase				,			__UpperCamelCase				,			__UpperCamelCase  ):
															_lowercase					       =					model_class(__UpperCamelCase  )
															model.to(__UpperCamelCase  )
															model.eval()

															with torch.no_grad():
																					_lowercase					       =					model(**self._prepare_for_class(__UpperCamelCase				,			__UpperCamelCase  )  )

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

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

									_lowercase       ,						_lowercase					       =					self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
															_lowercase					       =					True
															check_hidden_states_output(__UpperCamelCase				,			__UpperCamelCase				,			__UpperCamelCase  )

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

															check_hidden_states_output(__UpperCamelCase				,			__UpperCamelCase				,			__UpperCamelCase  )






			def    UpperCamelCase_				(      self  ):
									_lowercase       ,						_lowercase					       =					self.model_tester.prepare_config_and_inputs_for_common()

									_lowercase					       =					_config_zero_init(__UpperCamelCase  )
									_lowercase					       =					_config_zero_init(configs_no_init.backbone_config  )
									for model_class in self.all_model_classes:
															_lowercase					       =					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    UpperCamelCase_				(      self  ):
									pass






			@slow
			def    UpperCamelCase_				(      self  ):
									for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															_lowercase					       =					UperNetForSemanticSegmentation.from_pretrained(__UpperCamelCase  )
															self.assertIsNotNone(__UpperCamelCase  )







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



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






			def    UpperCamelCase_				(      self  ):
									_lowercase					       =					AutoImageProcessor.from_pretrained("""openmmlab/upernet-swin-tiny"""  )
									_lowercase					       =					UperNetForSemanticSegmentation.from_pretrained("""openmmlab/upernet-swin-tiny"""  ).to(__UpperCamelCase  )

									_lowercase					       =					prepare_img()
									_lowercase					       =					processor(images=__UpperCamelCase				,			return_tensors="""pt"""  ).to(__UpperCamelCase  )

									with torch.no_grad():
															_lowercase					       =					model(**__UpperCamelCase  )

									_lowercase					       =					torch.Size((1, model.config.num_labels, 512, 512)  )
									self.assertEqual(outputs.logits.shape				,			__UpperCamelCase  )

									_lowercase					       =					torch.tensor(
									    [[-7.59_58, -7.59_58, -7.43_02], [-7.59_58, -7.59_58, -7.43_02], [-7.47_97, -7.47_97, -7.30_68]]  ).to(__UpperCamelCase  )
									self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]				,			__UpperCamelCase				,			atol=1E-4  )  )






			def    UpperCamelCase_				(      self  ):
									_lowercase					       =					AutoImageProcessor.from_pretrained("""openmmlab/upernet-convnext-tiny"""  )
									_lowercase					       =					UperNetForSemanticSegmentation.from_pretrained("""openmmlab/upernet-convnext-tiny"""  ).to(__UpperCamelCase  )

									_lowercase					       =					prepare_img()
									_lowercase					       =					processor(images=__UpperCamelCase				,			return_tensors="""pt"""  ).to(__UpperCamelCase  )

									with torch.no_grad():
															_lowercase					       =					model(**__UpperCamelCase  )

									_lowercase					       =					torch.Size((1, model.config.num_labels, 512, 512)  )
									self.assertEqual(outputs.logits.shape				,			__UpperCamelCase  )

									_lowercase					       =					torch.tensor(
									    [[-8.81_10, -8.81_10, -8.65_21], [-8.81_10, -8.81_10, -8.65_21], [-8.77_46, -8.77_46, -8.61_30]]  ).to(__UpperCamelCase  )
									self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]				,			__UpperCamelCase				,			atol=1E-4  )  )