


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







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




      '''simple docstring'''




      _lowerCAmelCase					     =		parent
      _lowerCAmelCase					     =		batch_size
      _lowerCAmelCase					     =		image_size
      _lowerCAmelCase					     =		patch_size
      _lowerCAmelCase					     =		num_channels
      _lowerCAmelCase					     =		is_training
      _lowerCAmelCase					     =		use_labels
      _lowerCAmelCase					     =		hidden_size
      _lowerCAmelCase					     =		num_hidden_layers
      _lowerCAmelCase					     =		backbone_out_indices
      _lowerCAmelCase					     =		num_attention_heads
      _lowerCAmelCase					     =		intermediate_size
      _lowerCAmelCase					     =		hidden_act
      _lowerCAmelCase					     =		hidden_dropout_prob
      _lowerCAmelCase					     =		attention_probs_dropout_prob
      _lowerCAmelCase					     =		initializer_range
      _lowerCAmelCase					     =		num_labels
      _lowerCAmelCase					     =		scope
      _lowerCAmelCase					     =		is_hybrid
      # sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token)
      _lowerCAmelCase					     =		(image_size // patch_size) ** 2
      _lowerCAmelCase					     =		num_patches + 1







   def 							A__						(self				):




      '''simple docstring'''




      _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.image_size, self.image_size]       ,	self.num_labels				)

      _lowerCAmelCase					     =		self.get_config()

      return config, pixel_values, labels







   def 							A__						(self				):




      '''simple docstring'''




      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       ,	)







   def 							A__						(self       ,	lowerCamelCase       ,	lowerCamelCase       ,	lowerCamelCase				):




      '''simple docstring'''




      _lowerCAmelCase					     =		DPTModel(config=lowerCamelCase				)
      model.to(lowerCamelCase				)
      model.eval()
      _lowerCAmelCase					     =		model(lowerCamelCase				)
      self.parent.assertEqual(result.last_hidden_state.shape       ,	(self.batch_size, self.seq_length, self.hidden_size)				)







   def 							A__						(self       ,	lowerCamelCase       ,	lowerCamelCase       ,	lowerCamelCase				):




      '''simple docstring'''




      _lowerCAmelCase					     =		self.num_labels
      _lowerCAmelCase					     =		DPTForDepthEstimation(lowerCamelCase				)
      model.to(lowerCamelCase				)
      model.eval()
      _lowerCAmelCase					     =		model(lowerCamelCase				)
      self.parent.assertEqual(result.predicted_depth.shape       ,	(self.batch_size, self.image_size, self.image_size)				)







   def 							A__						(self       ,	lowerCamelCase       ,	lowerCamelCase       ,	lowerCamelCase				):




      '''simple docstring'''




      _lowerCAmelCase					     =		self.num_labels
      _lowerCAmelCase					     =		DPTForSemanticSegmentation(lowerCamelCase				)
      model.to(lowerCamelCase				)
      model.eval()
      _lowerCAmelCase					     =		model(lowerCamelCase       ,	labels=lowerCamelCase				)
      self.parent.assertEqual(
          result.logits.shape       ,	(self.batch_size, self.num_labels, self.image_size, self.image_size)				)







   def 							A__						(self				):




      '''simple docstring'''




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





@require_torch
class       __lowerCamelCase	(   SCREAMING_SNAKE_CASE__   ,							SCREAMING_SNAKE_CASE__   ,							unittest.TestCase    ):
   __UpperCamelCase									=   (DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else ()
   __UpperCamelCase									=   (
       {
           'depth-estimation': DPTForDepthEstimation,
           'feature-extraction': DPTModel,
           'image-segmentation': DPTForSemanticSegmentation,
       }
       if is_torch_available()
       else {}
   )

   __UpperCamelCase									=   False
   __UpperCamelCase									=   False
   __UpperCamelCase									=   False







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase					     =		DPTModelTester(self				)
      _lowerCAmelCase					     =		ConfigTester(self       ,	config_class=lowerCamelCase       ,	has_text_modality=lowerCamelCase       ,	hidden_size=37				)







   def 							A__						(self				):




      '''simple docstring'''




      self.config_tester.run_common_tests()







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




      '''simple docstring'''




      pass







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase     ,						_lowerCAmelCase					     =		self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
         _lowerCAmelCase					     =		model_class(lowerCamelCase				)
         self.assertIsInstance(model.get_input_embeddings()       ,	(nn.Module)				)
         _lowerCAmelCase					     =		model.get_output_embeddings()
         self.assertTrue(x is None or isinstance(lowerCamelCase       ,	nn.Linear				)				)







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase     ,						_lowerCAmelCase					     =		self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
         _lowerCAmelCase					     =		model_class(lowerCamelCase				)
         _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]       ,	lowerCamelCase				)







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase					     =		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*lowerCamelCase				)







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase					     =		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_depth_estimation(*lowerCamelCase				)







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase					     =		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_semantic_segmentation(*lowerCamelCase				)







   def 							A__						(self				):




      '''simple docstring'''




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

         _lowerCAmelCase     ,						_lowerCAmelCase					     =		self.model_tester.prepare_config_and_inputs_for_common()
         _lowerCAmelCase					     =		True

         if model_class in get_values(lowerCamelCase				):
            continue

         _lowerCAmelCase					     =		model_class(lowerCamelCase				)
         model.to(lowerCamelCase				)
         model.train()
         _lowerCAmelCase					     =		self._prepare_for_class(lowerCamelCase       ,	lowerCamelCase       ,	return_labels=lowerCamelCase				)
         _lowerCAmelCase					     =		model(**lowerCamelCase				).loss
         loss.backward()







   def 							A__						(self				):




      '''simple docstring'''




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

         _lowerCAmelCase     ,						_lowerCAmelCase					     =		self.model_tester.prepare_config_and_inputs_for_common()
         _lowerCAmelCase					     =		False
         _lowerCAmelCase					     =		True

         if model_class in get_values(lowerCamelCase				) or not model_class.supports_gradient_checkpointing:
            continue
         _lowerCAmelCase					     =		model_class(lowerCamelCase				)
         model.to(lowerCamelCase				)
         model.gradient_checkpointing_enable()
         model.train()
         _lowerCAmelCase					     =		self._prepare_for_class(lowerCamelCase       ,	lowerCamelCase       ,	return_labels=lowerCamelCase				)
         _lowerCAmelCase					     =		model(**lowerCamelCase				).loss
         loss.backward()







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase     ,						_lowerCAmelCase					     =		self.model_tester.prepare_config_and_inputs_for_common()

      _lowerCAmelCase					     =		_config_zero_init(lowerCamelCase				)
      for model_class in self.all_model_classes:
         _lowerCAmelCase					     =		model_class(config=lowerCamelCase				)
         # Skip the check for the backbone
         _lowerCAmelCase					     =		[]
         for name, module in model.named_modules():
            if module.__class__.__name__ == "DPTViTHybridEmbeddings":
               _lowerCAmelCase					     =		[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 							A__						(self				):




      '''simple docstring'''




      pass







   @slow
   def 							A__						(self				):




      '''simple docstring'''




      for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         _lowerCAmelCase					     =		DPTModel.from_pretrained(lowerCamelCase				)
         self.assertIsNotNone(lowerCamelCase				)






def 	__UpperCAmelCase					(       )							->	List[str]:






   """simple docstring"""


   _lowerCAmelCase					     =		Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""   )
   return image



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







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase					     =		DPTImageProcessor.from_pretrained("""Intel/dpt-large"""				)
      _lowerCAmelCase					     =		DPTForDepthEstimation.from_pretrained("""Intel/dpt-large"""				).to(lowerCamelCase				)

      _lowerCAmelCase					     =		prepare_img()
      _lowerCAmelCase					     =		image_processor(images=lowerCamelCase       ,	return_tensors="""pt"""				).to(lowerCamelCase				)

      # forward pass
      with torch.no_grad():
         _lowerCAmelCase					     =		model(**lowerCamelCase				)
         _lowerCAmelCase					     =		outputs.predicted_depth

      # verify the predicted depth
      _lowerCAmelCase					     =		torch.Size((1, 384, 384)				)
      self.assertEqual(predicted_depth.shape       ,	lowerCamelCase				)

      _lowerCAmelCase					     =		torch.tensor(
          [[6.3199, 6.3629, 6.4148], [6.3850, 6.3615, 6.4166], [6.3519, 6.3176, 6.3575]]				).to(lowerCamelCase				)

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







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase					     =		DPTImageProcessor.from_pretrained("""Intel/dpt-large-ade"""				)
      _lowerCAmelCase					     =		DPTForSemanticSegmentation.from_pretrained("""Intel/dpt-large-ade"""				).to(lowerCamelCase				)

      _lowerCAmelCase					     =		prepare_img()
      _lowerCAmelCase					     =		image_processor(images=lowerCamelCase       ,	return_tensors="""pt"""				).to(lowerCamelCase				)

      # forward pass
      with torch.no_grad():
         _lowerCAmelCase					     =		model(**lowerCamelCase				)

      # verify the logits
      _lowerCAmelCase					     =		torch.Size((1, 150, 480, 480)				)
      self.assertEqual(outputs.logits.shape       ,	lowerCamelCase				)

      _lowerCAmelCase					     =		torch.tensor(
          [[4.0480, 4.2420, 4.4360], [4.3124, 4.5693, 4.8261], [4.5768, 4.8965, 5.2163]]				).to(lowerCamelCase				)

      self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]       ,	lowerCamelCase       ,	atol=1e-4				)				)







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase					     =		DPTImageProcessor.from_pretrained("""Intel/dpt-large-ade"""				)
      _lowerCAmelCase					     =		DPTForSemanticSegmentation.from_pretrained("""Intel/dpt-large-ade"""				).to(lowerCamelCase				)

      _lowerCAmelCase					     =		prepare_img()
      _lowerCAmelCase					     =		image_processor(images=lowerCamelCase       ,	return_tensors="""pt"""				).to(lowerCamelCase				)

      # forward pass
      with torch.no_grad():
         _lowerCAmelCase					     =		model(**lowerCamelCase				)

      _lowerCAmelCase					     =		outputs.logits.detach().cpu()

      _lowerCAmelCase					     =		image_processor.post_process_semantic_segmentation(outputs=lowerCamelCase       ,	target_sizes=[(500, 300)]				)
      _lowerCAmelCase					     =		torch.Size((500, 300)				)
      self.assertEqual(segmentation[0].shape       ,	lowerCamelCase				)

      _lowerCAmelCase					     =		image_processor.post_process_semantic_segmentation(outputs=lowerCamelCase				)
      _lowerCAmelCase					     =		torch.Size((480, 480)				)
      self.assertEqual(segmentation[0].shape       ,	lowerCamelCase				)




"""simple docstring"""





import torch

from diffusers import HeunDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest


class       __lowerCamelCase	(   __lowercase    ):
   __UpperCamelCase									=   (HeunDiscreteScheduler,)
   __UpperCamelCase									=   10







   def 							A__						(self       ,	**lowerCamelCase				):




      '''simple docstring'''




      _lowerCAmelCase					     =		{
          """num_train_timesteps""": 1_100,
          """beta_start""": 0.0001,
          """beta_end""": 0.02,
          """beta_schedule""": """linear""",
      }

      config.update(**lowerCamelCase				)
      return config







   def 							A__						(self				):




      '''simple docstring'''




      for timesteps in [10, 50, 100, 1_000]:
         self.check_over_configs(num_train_timesteps=lowerCamelCase				)







   def 							A__						(self				):




      '''simple docstring'''




      for beta_start, beta_end in zip([0.0_0001, 0.0001, 0.001]       ,	[0.0002, 0.002, 0.02]				):
         self.check_over_configs(beta_start=lowerCamelCase       ,	beta_end=lowerCamelCase				)







   def 							A__						(self				):




      '''simple docstring'''




      for schedule in ["linear", "scaled_linear", "exp"]:
         self.check_over_configs(beta_schedule=lowerCamelCase				)







   def 							A__						(self				):




      '''simple docstring'''




      for clip_sample_range in [1.0, 2.0, 3.0]:
         self.check_over_configs(clip_sample_range=lowerCamelCase       ,	clip_sample=lowerCamelCase				)







   def 							A__						(self				):




      '''simple docstring'''




      for prediction_type in ["epsilon", "v_prediction", "sample"]:
         self.check_over_configs(prediction_type=lowerCamelCase				)







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase					     =		self.scheduler_classes[0]
      _lowerCAmelCase					     =		self.get_scheduler_config()
      _lowerCAmelCase					     =		scheduler_class(**lowerCamelCase				)

      scheduler.set_timesteps(self.num_inference_steps				)

      _lowerCAmelCase					     =		self.dummy_model()
      _lowerCAmelCase					     =		self.dummy_sample_deter * scheduler.init_noise_sigma
      _lowerCAmelCase					     =		sample.to(lowerCamelCase				)

      for i, t in enumerate(scheduler.timesteps				):
         _lowerCAmelCase					     =		scheduler.scale_model_input(lowerCamelCase       ,	lowerCamelCase				)

         _lowerCAmelCase					     =		model(lowerCamelCase       ,	lowerCamelCase				)

         _lowerCAmelCase					     =		scheduler.step(lowerCamelCase       ,	lowerCamelCase       ,	lowerCamelCase				)
         _lowerCAmelCase					     =		output.prev_sample

      _lowerCAmelCase					     =		torch.sum(torch.abs(lowerCamelCase				)				)
      _lowerCAmelCase					     =		torch.mean(torch.abs(lowerCamelCase				)				)

      if torch_device in ["cpu", "mps"]:
         assert abs(result_sum.item() - 0.1233				) < 1e-2
         assert abs(result_mean.item() - 0.0002				) < 1e-3
      else:
         # CUDA
         assert abs(result_sum.item() - 0.1233				) < 1e-2
         assert abs(result_mean.item() - 0.0002				) < 1e-3







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase					     =		self.scheduler_classes[0]
      _lowerCAmelCase					     =		self.get_scheduler_config(prediction_type="""v_prediction"""				)
      _lowerCAmelCase					     =		scheduler_class(**lowerCamelCase				)

      scheduler.set_timesteps(self.num_inference_steps				)

      _lowerCAmelCase					     =		self.dummy_model()
      _lowerCAmelCase					     =		self.dummy_sample_deter * scheduler.init_noise_sigma
      _lowerCAmelCase					     =		sample.to(lowerCamelCase				)

      for i, t in enumerate(scheduler.timesteps				):
         _lowerCAmelCase					     =		scheduler.scale_model_input(lowerCamelCase       ,	lowerCamelCase				)

         _lowerCAmelCase					     =		model(lowerCamelCase       ,	lowerCamelCase				)

         _lowerCAmelCase					     =		scheduler.step(lowerCamelCase       ,	lowerCamelCase       ,	lowerCamelCase				)
         _lowerCAmelCase					     =		output.prev_sample

      _lowerCAmelCase					     =		torch.sum(torch.abs(lowerCamelCase				)				)
      _lowerCAmelCase					     =		torch.mean(torch.abs(lowerCamelCase				)				)

      if torch_device in ["cpu", "mps"]:
         assert abs(result_sum.item() - 4.6934e-07				) < 1e-2
         assert abs(result_mean.item() - 6.1112e-10				) < 1e-3
      else:
         # CUDA
         assert abs(result_sum.item() - 4.693428650170972e-07				) < 1e-2
         assert abs(result_mean.item() - 0.0002				) < 1e-3







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase					     =		self.scheduler_classes[0]
      _lowerCAmelCase					     =		self.get_scheduler_config()
      _lowerCAmelCase					     =		scheduler_class(**lowerCamelCase				)

      scheduler.set_timesteps(self.num_inference_steps       ,	device=lowerCamelCase				)

      _lowerCAmelCase					     =		self.dummy_model()
      _lowerCAmelCase					     =		self.dummy_sample_deter.to(lowerCamelCase				) * scheduler.init_noise_sigma

      for t in scheduler.timesteps:
         _lowerCAmelCase					     =		scheduler.scale_model_input(lowerCamelCase       ,	lowerCamelCase				)

         _lowerCAmelCase					     =		model(lowerCamelCase       ,	lowerCamelCase				)

         _lowerCAmelCase					     =		scheduler.step(lowerCamelCase       ,	lowerCamelCase       ,	lowerCamelCase				)
         _lowerCAmelCase					     =		output.prev_sample

      _lowerCAmelCase					     =		torch.sum(torch.abs(lowerCamelCase				)				)
      _lowerCAmelCase					     =		torch.mean(torch.abs(lowerCamelCase				)				)

      if str(lowerCamelCase				).startswith("""cpu"""				):
         # The following sum varies between 148 and 156 on mps. Why?
         assert abs(result_sum.item() - 0.1233				) < 1e-2
         assert abs(result_mean.item() - 0.0002				) < 1e-3
      elif str(lowerCamelCase				).startswith("""mps"""				):
         # Larger tolerance on mps
         assert abs(result_mean.item() - 0.0002				) < 1e-2
      else:
         # CUDA
         assert abs(result_sum.item() - 0.1233				) < 1e-2
         assert abs(result_mean.item() - 0.0002				) < 1e-3







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase					     =		self.scheduler_classes[0]
      _lowerCAmelCase					     =		self.get_scheduler_config()
      _lowerCAmelCase					     =		scheduler_class(**lowerCamelCase       ,	use_karras_sigmas=lowerCamelCase				)

      scheduler.set_timesteps(self.num_inference_steps       ,	device=lowerCamelCase				)

      _lowerCAmelCase					     =		self.dummy_model()
      _lowerCAmelCase					     =		self.dummy_sample_deter.to(lowerCamelCase				) * scheduler.init_noise_sigma
      _lowerCAmelCase					     =		sample.to(lowerCamelCase				)

      for t in scheduler.timesteps:
         _lowerCAmelCase					     =		scheduler.scale_model_input(lowerCamelCase       ,	lowerCamelCase				)

         _lowerCAmelCase					     =		model(lowerCamelCase       ,	lowerCamelCase				)

         _lowerCAmelCase					     =		scheduler.step(lowerCamelCase       ,	lowerCamelCase       ,	lowerCamelCase				)
         _lowerCAmelCase					     =		output.prev_sample

      _lowerCAmelCase					     =		torch.sum(torch.abs(lowerCamelCase				)				)
      _lowerCAmelCase					     =		torch.mean(torch.abs(lowerCamelCase				)				)

      assert abs(result_sum.item() - 0.0_0015				) < 1e-2
      assert abs(result_mean.item() - 1.9869554535034695e-07				) < 1e-2