



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





	def __init__(			self :    List[str]			,lowerCamelCase__ :    Tuple			,lowerCamelCase__ :    List[Any]=2			,lowerCamelCase__ :    Optional[int]=32			,lowerCamelCase__ :    Optional[Any]=16			,lowerCamelCase__ :    Optional[int]=3			,lowerCamelCase__ :    List[Any]=True			,lowerCamelCase__ :    str=True			,lowerCamelCase__ :    str=32			,lowerCamelCase__ :    Optional[Any]=4			,lowerCamelCase__ :    Union[str, Any]=[0, 1, 2, 3]			,lowerCamelCase__ :    Dict=4			,lowerCamelCase__ :    Union[str, Any]=37			,lowerCamelCase__ :    Optional[Any]="gelu"			,lowerCamelCase__ :    int=0.1			,lowerCamelCase__ :    Dict=0.1			,lowerCamelCase__ :    Union[str, Any]=0.0_2			,lowerCamelCase__ :    Dict=3			,lowerCamelCase__ :    Optional[int]=[1, 384, 24, 24]			,lowerCamelCase__ :    Tuple=True			,lowerCamelCase__ :    Optional[Any]=None			,):







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





	def      UpperCamelCase_  (			self :    str			):







							'''simple docstring'''
							_UpperCamelCase :		List[Any]			    =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

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

							_UpperCamelCase :		int			    =       self.get_config()

							return config, pixel_values, labels





	def      UpperCamelCase_  (			self :    List[Any]			):







							'''simple docstring'''
							_UpperCamelCase :		List[str]			    =       {
							    '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=lowerCamelCase__			,initializer_range=self.initializer_range			,is_hybrid=self.is_hybrid			,backbone_config=lowerCamelCase__			,backbone_featmap_shape=self.backbone_featmap_shape			,)





	def      UpperCamelCase_  (			self :    str			,lowerCamelCase__ :    str			,lowerCamelCase__ :    Optional[int]			,lowerCamelCase__ :    Optional[Any]			):







							'''simple docstring'''
							_UpperCamelCase :		Optional[int]			    =       DPTModel(config=lowerCamelCase__			)
							model.to(lowerCamelCase__			)
							model.eval()
							_UpperCamelCase :		List[Any]			    =       model(lowerCamelCase__			)
							self.parent.assertEqual(result.last_hidden_state.shape			,(self.batch_size, self.seq_length, self.hidden_size)			)





	def      UpperCamelCase_  (			self :    Any			,lowerCamelCase__ :    int			,lowerCamelCase__ :    str			,lowerCamelCase__ :    int			):







							'''simple docstring'''
							_UpperCamelCase :		Tuple			    =       self.num_labels
							_UpperCamelCase :		int			    =       DPTForDepthEstimation(lowerCamelCase__			)
							model.to(lowerCamelCase__			)
							model.eval()
							_UpperCamelCase :		int			    =       model(lowerCamelCase__			)
							self.parent.assertEqual(result.predicted_depth.shape			,(self.batch_size, self.image_size, self.image_size)			)





	def      UpperCamelCase_  (			self :    Optional[Any]			,lowerCamelCase__ :    List[str]			,lowerCamelCase__ :    Tuple			,lowerCamelCase__ :    List[str]			):







							'''simple docstring'''
							_UpperCamelCase :		Tuple			    =       self.num_labels
							_UpperCamelCase :		Tuple			    =       DPTForSemanticSegmentation(lowerCamelCase__			)
							model.to(lowerCamelCase__			)
							model.eval()
							_UpperCamelCase :		str			    =       model(lowerCamelCase__			,labels=lowerCamelCase__			)
							self.parent.assertEqual(
							    result.logits.shape			,(self.batch_size, self.num_labels, self.image_size, self.image_size)			)





	def      UpperCamelCase_  (			self :    Union[str, Any]			):







							'''simple docstring'''
							_UpperCamelCase :		Optional[int]			    =       self.prepare_config_and_inputs()
							_UpperCamelCase :		List[Any]			    =       config_and_inputs
							_UpperCamelCase :		Optional[int]			    =       {'pixel_values': pixel_values}
							return config, inputs_dict






@require_torch
class 							lowercase__     (							lowercase						, lowercase						, unittest.TestCase       ):
	lowercase__      =      (DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else ()
	lowercase__      =      (
	    {
	        """depth-estimation""": DPTForDepthEstimation,
	        """feature-extraction""": DPTModel,
	        """image-segmentation""": DPTForSemanticSegmentation,
	    }
	    if is_torch_available()
	    else {}
	)

	lowercase__      =      False
	lowercase__      =      False
	lowercase__      =      False





	def      UpperCamelCase_  (			self :    str			):







							'''simple docstring'''
							_UpperCamelCase :		Tuple			    =       DPTModelTester(self			)
							_UpperCamelCase :		Dict			    =       ConfigTester(self			,config_class=lowerCamelCase__			,has_text_modality=lowerCamelCase__			,hidden_size=37			)





	def      UpperCamelCase_  (			self :    Optional[int]			):







							'''simple docstring'''
							self.config_tester.run_common_tests()





	@unittest.skip(reason='DPT does not use inputs_embeds'			)
	def      UpperCamelCase_  (			self :    Union[str, Any]			):







							'''simple docstring'''
							pass





	def      UpperCamelCase_  (			self :    str			):







							'''simple docstring'''
							_UpperCamelCase :		Any			    =       self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
													_UpperCamelCase :		int			    =       model_class(lowerCamelCase__			)
													self.assertIsInstance(model.get_input_embeddings()			,(nn.Module)			)
													_UpperCamelCase :		List[Any]			    =       model.get_output_embeddings()
													self.assertTrue(x is None or isinstance(lowerCamelCase__			,nn.Linear			)			)





	def      UpperCamelCase_  (			self :    Union[str, Any]			):







							'''simple docstring'''
							_UpperCamelCase :		Any			    =       self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
													_UpperCamelCase :		Optional[int]			    =       model_class(lowerCamelCase__			)
													_UpperCamelCase :		int			    =       inspect.signature(model.forward			)
													# signature.parameters is an OrderedDict => so arg_names order is deterministic
													_UpperCamelCase :		Union[str, Any]			    =       [*signature.parameters.keys()]

													_UpperCamelCase :		Tuple			    =       ['pixel_values']
													self.assertListEqual(arg_names[:1]			,lowerCamelCase__			)





	def      UpperCamelCase_  (			self :    List[Any]			):







							'''simple docstring'''
							_UpperCamelCase :		Optional[Any]			    =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowerCamelCase__			)





	def      UpperCamelCase_  (			self :    List[str]			):







							'''simple docstring'''
							_UpperCamelCase :		Optional[int]			    =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_depth_estimation(*lowerCamelCase__			)





	def      UpperCamelCase_  (			self :    Dict			):







							'''simple docstring'''
							_UpperCamelCase :		Optional[int]			    =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_semantic_segmentation(*lowerCamelCase__			)





	def      UpperCamelCase_  (			self :    List[str]			):







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

													_UpperCamelCase :		Optional[int]			    =       self.model_tester.prepare_config_and_inputs_for_common()
													_UpperCamelCase :		Union[str, Any]			    =       True

													if model_class in get_values(lowerCamelCase__			):
																			continue

													_UpperCamelCase :		int			    =       model_class(lowerCamelCase__			)
													model.to(lowerCamelCase__			)
													model.train()
													_UpperCamelCase :		List[str]			    =       self._prepare_for_class(lowerCamelCase__			,lowerCamelCase__			,return_labels=lowerCamelCase__			)
													_UpperCamelCase :		Any			    =       model(**lowerCamelCase__			).loss
													loss.backward()





	def      UpperCamelCase_  (			self :    str			):







							'''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[Any]			    =       False
													_UpperCamelCase :		Optional[Any]			    =       True

													if model_class in get_values(lowerCamelCase__			) or not model_class.supports_gradient_checkpointing:
																			continue
													_UpperCamelCase :		Optional[Any]			    =       model_class(lowerCamelCase__			)
													model.to(lowerCamelCase__			)
													model.gradient_checkpointing_enable()
													model.train()
													_UpperCamelCase :		Union[str, Any]			    =       self._prepare_for_class(lowerCamelCase__			,lowerCamelCase__			,return_labels=lowerCamelCase__			)
													_UpperCamelCase :		Tuple			    =       model(**lowerCamelCase__			).loss
													loss.backward()





	def      UpperCamelCase_  (			self :    List[str]			):







							'''simple docstring'''
							_UpperCamelCase :		List[Any]			    =       self.model_tester.prepare_config_and_inputs_for_common()

							_UpperCamelCase :		Optional[int]			    =       _config_zero_init(lowerCamelCase__			)
							for model_class in self.all_model_classes:
													_UpperCamelCase :		Any			    =       model_class(config=lowerCamelCase__			)
													# Skip the check for the backbone
													_UpperCamelCase :		List[str]			    =       []
													for name, module in model.named_modules():
																			if module.__class__.__name__ == "DPTViTHybridEmbeddings":
																									_UpperCamelCase :		Optional[int]			    =       [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      UpperCamelCase_  (			self :    Dict			):







							'''simple docstring'''
							pass





	@slow
	def      UpperCamelCase_  (			self :    Optional[int]			):







							'''simple docstring'''
							for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]:
													_UpperCamelCase :		Dict			    =       DPTModel.from_pretrained(lowerCamelCase__			)
													self.assertIsNotNone(lowerCamelCase__			)





	def      UpperCamelCase_  (			self :    Optional[Any]			):







							'''simple docstring'''
							_UpperCamelCase :		Any			    =       self.model_tester.prepare_config_and_inputs_for_common()
							_UpperCamelCase :		int			    =       'add'
							with self.assertRaises(lowerCamelCase__			):
													_UpperCamelCase :		Optional[int]			    =       DPTForDepthEstimation(lowerCamelCase__			)






def A__   (					):






						"""simple docstring"""




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






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





	def      UpperCamelCase_  (			self :    Tuple			):







							'''simple docstring'''
							_UpperCamelCase :		Dict			    =       DPTImageProcessor.from_pretrained('Intel/dpt-hybrid-midas'			)
							_UpperCamelCase :		Optional[Any]			    =       DPTForDepthEstimation.from_pretrained('Intel/dpt-hybrid-midas'			).to(lowerCamelCase__			)

							_UpperCamelCase :		Union[str, Any]			    =       prepare_img()
							_UpperCamelCase :		Dict			    =       image_processor(images=lowerCamelCase__			,return_tensors='pt'			).to(lowerCamelCase__			)

							# forward pass
							with torch.no_grad():
													_UpperCamelCase :		Tuple			    =       model(**lowerCamelCase__			)
													_UpperCamelCase :		List[str]			    =       outputs.predicted_depth

							# verify the predicted depth
							_UpperCamelCase :		Optional[int]			    =       torch.Size((1, 384, 384)			)
							self.assertEqual(predicted_depth.shape			,lowerCamelCase__			)

							_UpperCamelCase :		List[str]			    =       torch.tensor(
							    [[[5.6_4_3_7, 5.6_1_4_6, 5.6_5_1_1], [5.4_3_7_1, 5.5_6_4_9, 5.5_9_5_8], [5.5_2_1_5, 5.5_1_8_4, 5.5_2_9_3]]]			).to(lowerCamelCase__			)

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







'''simple docstring'''
import gc
import unittest

import numpy as np
import torch

from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNetaDModel
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps

from ..pipeline_params import UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS, UNCONDITIONAL_AUDIO_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()






class 							lowercase__     (							lowercase						, unittest.TestCase       ):
	lowercase__      =      DanceDiffusionPipeline
	lowercase__      =      UNCONDITIONAL_AUDIO_GENERATION_PARAMS
	lowercase__      =      PipelineTesterMixin.required_optional_params - {
	    """callback""",
	    """latents""",
	    """callback_steps""",
	    """output_type""",
	    """num_images_per_prompt""",
	}
	lowercase__      =      UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS
	lowercase__      =      False
	lowercase__      =      False





	def      UpperCamelCase_  (			self :    Union[str, Any]			):







							'''simple docstring'''
							torch.manual_seed(0			)
							_UpperCamelCase :		str			    =       UNetaDModel(
							    block_out_channels=(32, 32, 64)			,extra_in_channels=16			,sample_size=512			,sample_rate=16000			,in_channels=2			,out_channels=2			,flip_sin_to_cos=lowerCamelCase__			,use_timestep_embedding=lowerCamelCase__			,time_embedding_type='fourier'			,mid_block_type='UNetMidBlock1D'			,down_block_types=('DownBlock1DNoSkip', 'DownBlock1D', 'AttnDownBlock1D')			,up_block_types=('AttnUpBlock1D', 'UpBlock1D', 'UpBlock1DNoSkip')			,)
							_UpperCamelCase :		int			    =       IPNDMScheduler()

							_UpperCamelCase :		List[str]			    =       {
							    'unet': unet,
							    'scheduler': scheduler,
							}
							return components





	def      UpperCamelCase_  (			self :    Tuple			,lowerCamelCase__ :    Tuple			,lowerCamelCase__ :    int=0			):







							'''simple docstring'''
							if str(lowerCamelCase__			).startswith('mps'			):
													_UpperCamelCase :		Union[str, Any]			    =       torch.manual_seed(lowerCamelCase__			)
							else:
													_UpperCamelCase :		str			    =       torch.Generator(device=lowerCamelCase__			).manual_seed(lowerCamelCase__			)
							_UpperCamelCase :		str			    =       {
							    'batch_size': 1,
							    'generator': generator,
							    'num_inference_steps': 4,
							}
							return inputs





	def      UpperCamelCase_  (			self :    int			):







							'''simple docstring'''
							_UpperCamelCase :		Union[str, Any]			    =       'cpu'  # ensure determinism for the device-dependent torch.Generator
							_UpperCamelCase :		List[str]			    =       self.get_dummy_components()
							_UpperCamelCase :		int			    =       DanceDiffusionPipeline(**lowerCamelCase__			)
							_UpperCamelCase :		List[str]			    =       pipe.to(lowerCamelCase__			)
							pipe.set_progress_bar_config(disable=lowerCamelCase__			)

							_UpperCamelCase :		Union[str, Any]			    =       self.get_dummy_inputs(lowerCamelCase__			)
							_UpperCamelCase :		List[Any]			    =       pipe(**lowerCamelCase__			)
							_UpperCamelCase :		Union[str, Any]			    =       output.audios

							_UpperCamelCase :		List[str]			    =       audio[0, -3:, -3:]

							assert audio.shape == (1, 2, components["unet"].sample_size)
							_UpperCamelCase :		Dict			    =       np.array([-0.7_2_6_5, 1.0_0_0_0, -0.8_3_8_8, 0.1_1_7_5, 0.9_4_9_8, -1.0_0_0_0]			)
							assert np.abs(audio_slice.flatten() - expected_slice			).max() < 1E-2





	@skip_mps
	def      UpperCamelCase_  (			self :    str			):







							'''simple docstring'''
							return super().test_save_load_local()





	@skip_mps
	def      UpperCamelCase_  (			self :    Dict			):







							'''simple docstring'''
							return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3			)





	@skip_mps
	def      UpperCamelCase_  (			self :    str			):







							'''simple docstring'''
							return super().test_save_load_optional_components()





	@skip_mps
	def      UpperCamelCase_  (			self :    List[Any]			):







							'''simple docstring'''
							return super().test_attention_slicing_forward_pass()





	def      UpperCamelCase_  (			self :    List[str]			):







							'''simple docstring'''
							super().test_inference_batch_single_identical(expected_max_diff=3E-3			)






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





	def      UpperCamelCase_  (			self :    List[str]			):







							'''simple docstring'''
							# clean up the VRAM after each test
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()





	def      UpperCamelCase_  (			self :    int			):







							'''simple docstring'''
							_UpperCamelCase :		Union[str, Any]			    =       torch_device

							_UpperCamelCase :		Dict			    =       DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k'			)
							_UpperCamelCase :		str			    =       pipe.to(lowerCamelCase__			)
							pipe.set_progress_bar_config(disable=lowerCamelCase__			)

							_UpperCamelCase :		int			    =       torch.manual_seed(0			)
							_UpperCamelCase :		Optional[Any]			    =       pipe(generator=lowerCamelCase__			,num_inference_steps=100			,audio_length_in_s=4.0_9_6			)
							_UpperCamelCase :		Optional[int]			    =       output.audios

							_UpperCamelCase :		int			    =       audio[0, -3:, -3:]

							assert audio.shape == (1, 2, pipe.unet.sample_size)
							_UpperCamelCase :		Tuple			    =       np.array([-0.0_1_9_2, -0.0_2_3_1, -0.0_3_1_8, -0.0_0_5_9, 0.0_0_0_2, -0.0_0_2_0]			)

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





	def      UpperCamelCase_  (			self :    Union[str, Any]			):







							'''simple docstring'''
							_UpperCamelCase :		List[Any]			    =       torch_device

							_UpperCamelCase :		int			    =       DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k'			,torch_dtype=torch.floataa			)
							_UpperCamelCase :		Any			    =       pipe.to(lowerCamelCase__			)
							pipe.set_progress_bar_config(disable=lowerCamelCase__			)

							_UpperCamelCase :		Union[str, Any]			    =       torch.manual_seed(0			)
							_UpperCamelCase :		Union[str, Any]			    =       pipe(generator=lowerCamelCase__			,num_inference_steps=100			,audio_length_in_s=4.0_9_6			)
							_UpperCamelCase :		Any			    =       output.audios

							_UpperCamelCase :		str			    =       audio[0, -3:, -3:]

							assert audio.shape == (1, 2, pipe.unet.sample_size)
							_UpperCamelCase :		Any			    =       np.array([-0.0_3_6_7, -0.0_4_8_8, -0.0_7_7_1, -0.0_5_2_5, -0.0_4_4_4, -0.0_3_4_1]			)

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


