


'''simple docstring'''
import inspect
import unittest

from transformers import DPTConfig
from transformers.file_utils import is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

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 torch import nn

		from transformers import MODEL_MAPPING, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel
		from transformers.models.dpt.modeling_dpt import DPT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image

		from transformers import DPTImageProcessor
class    lowercase	:

		"""simple docstring"""

		def __init__(		self    ,   UpperCamelCase_    ,   UpperCamelCase_=2    ,   UpperCamelCase_=32    ,   UpperCamelCase_=16    ,   UpperCamelCase_=3    ,   UpperCamelCase_=True    ,   UpperCamelCase_=True    ,   UpperCamelCase_=32    ,   UpperCamelCase_=4    ,   UpperCamelCase_=[0, 1, 2, 3]    ,   UpperCamelCase_=4    ,   UpperCamelCase_=37    ,   UpperCamelCase_="gelu"    ,   UpperCamelCase_=0.1    ,   UpperCamelCase_=0.1    ,   UpperCamelCase_=0.02    ,   UpperCamelCase_=3    ,   UpperCamelCase_=[1, 384, 24, 24]    ,   UpperCamelCase_=True    ,   UpperCamelCase_=None    ,   ):

							'''simple docstring'''


							UpperCamelCase__   :Any												= parent
							UpperCamelCase__   :Optional[int]												= batch_size
							UpperCamelCase__   :Optional[int]												= image_size
							UpperCamelCase__   :Union[str, Any]												= patch_size
							UpperCamelCase__   :List[Any]												= num_channels
							UpperCamelCase__   :Union[str, Any]												= is_training
							UpperCamelCase__   :Optional[Any]												= use_labels
							UpperCamelCase__   :List[Any]												= hidden_size
							UpperCamelCase__   :str												= num_hidden_layers
							UpperCamelCase__   :int												= backbone_out_indices
							UpperCamelCase__   :Optional[Any]												= num_attention_heads
							UpperCamelCase__   :Any												= intermediate_size
							UpperCamelCase__   :Union[str, Any]												= hidden_act
							UpperCamelCase__   :Optional[Any]												= hidden_dropout_prob
							UpperCamelCase__   :Optional[int]												= attention_probs_dropout_prob
							UpperCamelCase__   :List[str]												= initializer_range
							UpperCamelCase__   :int												= num_labels
							UpperCamelCase__   :Optional[int]												= backbone_featmap_shape
							UpperCamelCase__   :Optional[int]												= scope
							UpperCamelCase__   :Optional[int]												= is_hybrid
							# sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token)
							UpperCamelCase__   :Dict												= (image_size // patch_size) ** 2
							UpperCamelCase__   :List[str]												= num_patches + 1




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :Dict												= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

							UpperCamelCase__   :Tuple												= None
							if self.use_labels:
												UpperCamelCase__   :int												= ids_tensor([self.batch_size, self.image_size, self.image_size]    ,   self.num_labels							)

							UpperCamelCase__   :Any												= self.get_config()

							return config, pixel_values, labels




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :Any												= {
							    '''global_padding''': '''same''',
							    '''layer_type''': '''bottleneck''',
							    '''depths''': [3, 4, 9],
							    '''out_features''': ['''stage1''', '''stage2''', '''stage3'''],
							    '''embedding_dynamic_padding''': True,
							    '''hidden_sizes''': [96, 192, 384, 768],
							    '''num_groups''': 2,
							}

							return DPTConfig(
							    image_size=self.image_size    ,   patch_size=self.patch_size    ,   num_channels=self.num_channels    ,   hidden_size=self.hidden_size    ,   num_hidden_layers=self.num_hidden_layers    ,   backbone_out_indices=self.backbone_out_indices    ,   num_attention_heads=self.num_attention_heads    ,   intermediate_size=self.intermediate_size    ,   hidden_act=self.hidden_act    ,   hidden_dropout_prob=self.hidden_dropout_prob    ,   attention_probs_dropout_prob=self.attention_probs_dropout_prob    ,   is_decoder=a_    ,   initializer_range=self.initializer_range    ,   is_hybrid=self.is_hybrid    ,   backbone_config=a_    ,   backbone_featmap_shape=self.backbone_featmap_shape    ,   )




		def     lowerCAmelCase__				(		self    ,   UpperCamelCase_    ,   UpperCamelCase_    ,   UpperCamelCase_							):

							'''simple docstring'''


							UpperCamelCase__   :Dict												= DPTModel(config=a_							)
							model.to(a_							)
							model.eval()
							UpperCamelCase__   :Optional[Any]												= model(a_							)
							self.parent.assertEqual(result.last_hidden_state.shape    ,   (self.batch_size, self.seq_length, self.hidden_size)							)




		def     lowerCAmelCase__				(		self    ,   UpperCamelCase_    ,   UpperCamelCase_    ,   UpperCamelCase_							):

							'''simple docstring'''


							UpperCamelCase__   :str												= self.num_labels
							UpperCamelCase__   :Any												= DPTForDepthEstimation(a_							)
							model.to(a_							)
							model.eval()
							UpperCamelCase__   :Any												= model(a_							)
							self.parent.assertEqual(result.predicted_depth.shape    ,   (self.batch_size, self.image_size, self.image_size)							)




		def     lowerCAmelCase__				(		self    ,   UpperCamelCase_    ,   UpperCamelCase_    ,   UpperCamelCase_							):

							'''simple docstring'''


							UpperCamelCase__   :Tuple												= self.num_labels
							UpperCamelCase__   :Optional[Any]												= DPTForSemanticSegmentation(a_							)
							model.to(a_							)
							model.eval()
							UpperCamelCase__   :List[Any]												= model(a_    ,   labels=a_							)
							self.parent.assertEqual(
							    result.logits.shape    ,   (self.batch_size, self.num_labels, self.image_size, self.image_size)							)




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


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



@require_torch
class    lowercase	(							__snake_case	,		__snake_case	,		unittest.TestCase					):

		"""simple docstring"""

		_a     =				(DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else ()
		_a     =				(
		    {
		        'depth-estimation': DPTForDepthEstimation,
		        'feature-extraction': DPTModel,
		        'image-segmentation': DPTForSemanticSegmentation,
		    }
		    if is_torch_available()
		    else {}
		)

		_a     =				False
		_a     =				False
		_a     =				False




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :Union[str, Any]												= DPTModelTester(self							)
							UpperCamelCase__   :Tuple												= ConfigTester(self    ,   config_class=a_    ,   has_text_modality=a_    ,   hidden_size=37							)




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							self.config_tester.run_common_tests()




		@unittest.skip(reason='''DPT does not use inputs_embeds'''							)
		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							pass




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :Optional[int]												= self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
												UpperCamelCase__   :str												= model_class(a_							)
												self.assertIsInstance(model.get_input_embeddings()    ,   (nn.Module)							)
												UpperCamelCase__   :Union[str, Any]												= model.get_output_embeddings()
												self.assertTrue(x is None or isinstance(a_    ,   nn.Linear							)							)




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :int												= self.model_tester.prepare_config_and_inputs_for_common()

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

												UpperCamelCase__   :Union[str, Any]												= ['''pixel_values''']
												self.assertListEqual(arg_names[:1]    ,   a_							)




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :int												= self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*a_							)




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :Any												= self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_depth_estimation(*a_							)




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :Any												= self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_semantic_segmentation(*a_							)




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							for model_class in self.all_model_classes:
												if model_class.__name__ == "DPTForDepthEstimation":
																	continue

												UpperCamelCase__   :Any												= self.model_tester.prepare_config_and_inputs_for_common()
												UpperCamelCase__   :List[str]												= True

												if model_class in get_values(a_							):
																	continue

												UpperCamelCase__   :Any												= model_class(a_							)
												model.to(a_							)
												model.train()
												UpperCamelCase__   :Any												= self._prepare_for_class(a_    ,   a_    ,   return_labels=a_							)
												UpperCamelCase__   :str												= model(**a_							).loss
												loss.backward()




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							for model_class in self.all_model_classes:
												if model_class.__name__ == "DPTForDepthEstimation":
																	continue

												UpperCamelCase__   :Union[str, Any]												= self.model_tester.prepare_config_and_inputs_for_common()
												UpperCamelCase__   :str												= False
												UpperCamelCase__   :str												= True

												if model_class in get_values(a_							) or not model_class.supports_gradient_checkpointing:
																	continue
												UpperCamelCase__   :List[Any]												= model_class(a_							)
												model.to(a_							)
												model.gradient_checkpointing_enable()
												model.train()
												UpperCamelCase__   :Optional[int]												= self._prepare_for_class(a_    ,   a_    ,   return_labels=a_							)
												UpperCamelCase__   :Tuple												= model(**a_							).loss
												loss.backward()




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :Optional[int]												= self.model_tester.prepare_config_and_inputs_for_common()

							UpperCamelCase__   :Dict												= _config_zero_init(a_							)
							for model_class in self.all_model_classes:
												UpperCamelCase__   :Optional[Any]												= model_class(config=a_							)
												# Skip the check for the backbone
												UpperCamelCase__   :Any												= []
												for name, module in model.named_modules():
																	if module.__class__.__name__ == "DPTViTHybridEmbeddings":
																						UpperCamelCase__   :Optional[Any]												= [F'''{name}.{key}''' for key in module.state_dict().keys()]
																						break

												for name, param in model.named_parameters():
																	if param.requires_grad:
																						if name in backbone_params:
																											continue
																						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('''Will be fixed soon by reducing the size of the model used for common tests.'''							)
		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							pass




		@slow
		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]:
												UpperCamelCase__   :Optional[Any]												= DPTModel.from_pretrained(a_							)
												self.assertIsNotNone(a_							)




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :Any												= self.model_tester.prepare_config_and_inputs_for_common()
							UpperCamelCase__   :str												= '''add'''
							with self.assertRaises(a_							):
												UpperCamelCase__   :List[Any]												= DPTForDepthEstimation(a_							)



def 			a      (			)  ->	Tuple:



					'''simple docstring'''






					UpperCamelCase__   :Tuple												= Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''   )
					return image







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

		"""simple docstring"""

		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :Optional[Any]												= DPTImageProcessor.from_pretrained('''Intel/dpt-hybrid-midas'''							)
							UpperCamelCase__   :Dict												= DPTForDepthEstimation.from_pretrained('''Intel/dpt-hybrid-midas'''							).to(a_							)

							UpperCamelCase__   :str												= prepare_img()
							UpperCamelCase__   :Optional[Any]												= image_processor(images=a_    ,   return_tensors='''pt'''							).to(a_							)

							# forward pass
							with torch.no_grad():
												UpperCamelCase__   :int												= model(**a_							)
												UpperCamelCase__   :int												= outputs.predicted_depth

							# verify the predicted depth
							UpperCamelCase__   :Any												= torch.Size((1, 384, 384)							)
							self.assertEqual(predicted_depth.shape    ,   a_							)

							UpperCamelCase__   :str												= torch.tensor(
							    [[[5.6437, 5.6146, 5.6511], [5.4371, 5.5649, 5.5958], [5.5215, 5.5184, 5.5293]]]							).to(a_							)

							self.assertTrue(torch.allclose(outputs.predicted_depth[:3, :3, :3] / 100    ,   a_    ,   atol=1e-4							)							)



"""simple docstring"""
import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()



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






		'''simple docstring'''

		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			super().tearDown()
			gc.collect()
			torch.cuda.empty_cache()



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Tuple						=				StableDiffusionKDiffusionPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4'''					)
			__snake_case	:						List[str]						=				sd_pipe.to(a_					)
			sd_pipe.set_progress_bar_config(disable=a_					)

			sd_pipe.set_scheduler('''sample_euler'''					)

			__snake_case	:						Tuple						=				'''A painting of a squirrel eating a burger'''
			__snake_case	:						str						=				torch.manual_seed(0					)
			__snake_case	:						int						=				sd_pipe([prompt]				,       generator=a_				,       guidance_scale=9.0				,       num_inference_steps=20				,       output_type='''np'''					)

			__snake_case	:						List[str]						=				output.images

			__snake_case	:						Tuple						=				image[0, -3:, -3:, -1]

			assert image.shape == (1, 5_12, 5_12, 3)
			__snake_case	:						Dict						=				np.array([0.0447, 0.0492, 0.0468, 0.0408, 0.0383, 0.0408, 0.0354, 0.0380, 0.0339]					)

			assert np.abs(image_slice.flatten() - expected_slice					).max() < 1E-2



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Optional[Any]						=				StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base'''					)
			__snake_case	:						Union[str, Any]						=				sd_pipe.to(a_					)
			sd_pipe.set_progress_bar_config(disable=a_					)

			sd_pipe.set_scheduler('''sample_euler'''					)

			__snake_case	:						str						=				'''A painting of a squirrel eating a burger'''
			__snake_case	:						List[str]						=				torch.manual_seed(0					)
			__snake_case	:						Dict						=				sd_pipe([prompt]				,       generator=a_				,       guidance_scale=9.0				,       num_inference_steps=20				,       output_type='''np'''					)

			__snake_case	:						Any						=				output.images

			__snake_case	:						Tuple						=				image[0, -3:, -3:, -1]

			assert image.shape == (1, 5_12, 5_12, 3)
			__snake_case	:						Tuple						=				np.array([0.1237, 0.1320, 0.1438, 0.1359, 0.1390, 0.1132, 0.1277, 0.1175, 0.1112]					)

			assert np.abs(image_slice.flatten() - expected_slice					).max() < 5E-1



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Any						=				StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base'''					)
			__snake_case	:						Union[str, Any]						=				sd_pipe.to(a_					)
			sd_pipe.set_progress_bar_config(disable=a_					)

			sd_pipe.set_scheduler('''sample_dpmpp_2m'''					)

			__snake_case	:						int						=				'''A painting of a squirrel eating a burger'''
			__snake_case	:						Optional[Any]						=				torch.manual_seed(0					)
			__snake_case	:						Dict						=				sd_pipe(
			    [prompt]				,       generator=a_				,       guidance_scale=7.5				,       num_inference_steps=15				,       output_type='''np'''				,       use_karras_sigmas=a_				,       )

			__snake_case	:						Optional[int]						=				output.images

			__snake_case	:						Any						=				image[0, -3:, -3:, -1]

			assert image.shape == (1, 5_12, 5_12, 3)
			__snake_case	:						Any						=				np.array(
			    [0.1138_1689, 0.1211_2921, 0.138_9457, 0.1254_9606, 0.124_4964, 0.1083_1517, 0.1156_2866, 0.1086_7816, 0.1049_9048]					)

			assert np.abs(image_slice.flatten() - expected_slice					).max() < 1E-2





