



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 			_snake_case     :




				def __init__(  self   :  List[str],   __lowercase   :  Union[str, Any],   __lowercase   :  Tuple=2,   __lowercase   :  Union[str, Any]=32,   __lowercase   :  int=16,   __lowercase   :  List[Any]=3,   __lowercase   :  int=True,   __lowercase   :  int=True,   __lowercase   :  str=32,   __lowercase   :  Any=4,   __lowercase   :  Optional[int]=[0, 1, 2, 3],   __lowercase   :  Union[str, Any]=4,   __lowercase   :  List[str]=37,   __lowercase   :  int="gelu",   __lowercase   :  Optional[int]=0.1,   __lowercase   :  Union[str, Any]=0.1,   __lowercase   :  List[Any]=0.02,   __lowercase   :  Union[str, Any]=3,   __lowercase   :  Tuple=[1, 384, 24, 24],   __lowercase   :  Tuple=True,   __lowercase   :  Optional[int]=None,   ):
							lowercase__   					=			parent
							lowercase__   					=			batch_size
							lowercase__   					=			image_size
							lowercase__   					=			patch_size
							lowercase__   					=			num_channels
							lowercase__   					=			is_training
							lowercase__   					=			use_labels
							lowercase__   					=			hidden_size
							lowercase__   					=			num_hidden_layers
							lowercase__   					=			backbone_out_indices
							lowercase__   					=			num_attention_heads
							lowercase__   					=			intermediate_size
							lowercase__   					=			hidden_act
							lowercase__   					=			hidden_dropout_prob
							lowercase__   					=			attention_probs_dropout_prob
							lowercase__   					=			initializer_range
							lowercase__   					=			num_labels
							lowercase__   					=			backbone_featmap_shape
							lowercase__   					=			scope
							lowercase__   					=			is_hybrid
							# sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token)
							lowercase__   					=			(image_size // patch_size) ** 2
							lowercase__   					=			num_patches + 1




				def 							A__ (  self   :  List[Any]  ):
							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.image_size, self.image_size],   self.num_labels  )

							lowercase__   					=			self.get_config()

							return config, pixel_values, labels




				def 							A__ (  self   :  Dict  ):
							lowercase__   					=			{
							    "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=__lowercase,   initializer_range=self.initializer_range,   is_hybrid=self.is_hybrid,   backbone_config=__lowercase,   backbone_featmap_shape=self.backbone_featmap_shape,   )




				def 							A__ (  self   :  List[Any],   __lowercase   :  Optional[Any],   __lowercase   :  Optional[Any],   __lowercase   :  Optional[Any]  ):
							lowercase__   					=			DPTModel(config=__lowercase  )
							model.to(__lowercase  )
							model.eval()
							lowercase__   					=			model(__lowercase  )
							self.parent.assertEqual(result.last_hidden_state.shape,   (self.batch_size, self.seq_length, self.hidden_size)  )




				def 							A__ (  self   :  Optional[Any],   __lowercase   :  int,   __lowercase   :  Optional[Any],   __lowercase   :  Union[str, Any]  ):
							lowercase__   					=			self.num_labels
							lowercase__   					=			DPTForDepthEstimation(__lowercase  )
							model.to(__lowercase  )
							model.eval()
							lowercase__   					=			model(__lowercase  )
							self.parent.assertEqual(result.predicted_depth.shape,   (self.batch_size, self.image_size, self.image_size)  )




				def 							A__ (  self   :  Optional[Any],   __lowercase   :  List[str],   __lowercase   :  Union[str, Any],   __lowercase   :  Dict  ):
							lowercase__   					=			self.num_labels
							lowercase__   					=			DPTForSemanticSegmentation(__lowercase  )
							model.to(__lowercase  )
							model.eval()
							lowercase__   					=			model(__lowercase,   labels=__lowercase  )
							self.parent.assertEqual(
							    result.logits.shape,   (self.batch_size, self.num_labels, self.image_size, self.image_size)  )




				def 							A__ (  self   :  Optional[int]  ):
							lowercase__   					=			self.prepare_config_and_inputs()
							lowercase__					,    lowercase__					,    lowercase__   					=			config_and_inputs
							lowercase__   					=			{"pixel_values": pixel_values}
							return config, inputs_dict
@require_torch
class 			_snake_case     (   lowercase__      ,		lowercase__      ,		unittest.TestCase):
				UpperCamelCase__			:   Tuple       						=(DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else ()
				UpperCamelCase__			:   Tuple       						=(
				    {
				        """depth-estimation""": DPTForDepthEstimation,
				        """feature-extraction""": DPTModel,
				        """image-segmentation""": DPTForSemanticSegmentation,
				    }
				    if is_torch_available()
				    else {}
				)

				UpperCamelCase__			:   List[str]       						=False
				UpperCamelCase__			:   str       						=False
				UpperCamelCase__			:   Tuple       						=False




				def 							A__ (  self   :  Dict  ):
							lowercase__   					=			DPTModelTester(self  )
							lowercase__   					=			ConfigTester(self,   config_class=__lowercase,   has_text_modality=__lowercase,   hidden_size=37  )




				def 							A__ (  self   :  Any  ):
							self.config_tester.run_common_tests()




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




				def 							A__ (  self   :  List[str]  ):
							lowercase__					,    lowercase__   					=			self.model_tester.prepare_config_and_inputs_for_common()

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




				def 							A__ (  self   :  int  ):
							lowercase__					,    lowercase__   					=			self.model_tester.prepare_config_and_inputs_for_common()

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




				def 							A__ (  self   :  int  ):
							lowercase__   					=			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__lowercase  )




				def 							A__ (  self   :  Dict  ):
							lowercase__   					=			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_depth_estimation(*__lowercase  )




				def 							A__ (  self   :  Union[str, Any]  ):
							lowercase__   					=			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_semantic_segmentation(*__lowercase  )




				def 							A__ (  self   :  Optional[int]  ):
							for model_class in self.all_model_classes:
										if model_class.__name__ == "DPTForDepthEstimation":
													continue

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

										if model_class in get_values(__lowercase  ):
													continue

										lowercase__   					=			model_class(__lowercase  )
										model.to(__lowercase  )
										model.train()
										lowercase__   					=			self._prepare_for_class(__lowercase,   __lowercase,   return_labels=__lowercase  )
										lowercase__   					=			model(**__lowercase  ).loss
										loss.backward()




				def 							A__ (  self   :  Dict  ):
							for model_class in self.all_model_classes:
										if model_class.__name__ == "DPTForDepthEstimation":
													continue

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

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




				def 							A__ (  self   :  List[Any]  ):
							lowercase__					,    lowercase__   					=			self.model_tester.prepare_config_and_inputs_for_common()

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




				@slow
				def 							A__ (  self   :  List[str]  ):
							for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]:
										lowercase__   					=			DPTModel.from_pretrained(__lowercase  )
										self.assertIsNotNone(__lowercase  )




				def 							A__ (  self   :  Tuple  ):
							# We do this test only for DPTForDepthEstimation since it is the only model that uses readout_type
							lowercase__					,    lowercase__   					=			self.model_tester.prepare_config_and_inputs_for_common()
							lowercase__   					=			"add"
							with self.assertRaises(__lowercase  ):
										lowercase__   					=			DPTForDepthEstimation(__lowercase  )



def __lowerCAmelCase	(       ):
			lowercase__   					=			Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"					)
			return image




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




				def 							A__ (  self   :  str  ):
							lowercase__   					=			DPTImageProcessor.from_pretrained("Intel/dpt-hybrid-midas"  )
							lowercase__   					=			DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas"  ).to(__lowercase  )

							lowercase__   					=			prepare_img()
							lowercase__   					=			image_processor(images=__lowercase,   return_tensors="pt"  ).to(__lowercase  )

							# forward pass
							with torch.no_grad():
										lowercase__   					=			model(**__lowercase  )
										lowercase__   					=			outputs.predicted_depth

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

							lowercase__   					=			torch.tensor(
							    [[[5.6437, 5.6146, 5.6511], [5.4371, 5.5649, 5.5958], [5.5215, 5.5184, 5.5293]]]  ).to(__lowercase  )

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









from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxSeqaSeqConfigWithPast
from ...utils import logging


if TYPE_CHECKING:
							from ...feature_extraction_utils import FeatureExtractionMixin
							from ...tokenization_utils_base import PreTrainedTokenizerBase
							from ...utils import TensorType

lowercase_						=       logging.get_logger(__name__)

lowercase_						=       {
    """openai/whisper-base""": """https://huggingface.co/openai/whisper-base/resolve/main/config.json""",
}

# fmt: off
lowercase_						=       [
    1, 2, 7, 8, 9, 10, 14, 25,
    26, 27, 28, 29, 31, 58, 59, 60, 61, 62,
    63, 90, 91, 92, 93, 357, 366, 438, 532, 685,
    705, 796, 930, 1058, 1220, 1267, 1279, 1303, 1343, 1377,
    1391, 1635, 1782, 1875, 2162, 2361, 2488, 3467, 4008, 4211,
    4600, 4808, 5299, 5855, 6329, 7203, 9609, 9959, 1_0563, 1_0786,
    1_1420, 1_1709, 1_1907, 1_3163, 1_3697, 1_3700, 1_4808, 1_5306, 1_6410, 1_6791,
    1_7992, 1_9203, 1_9510, 2_0724, 2_2305, 2_2935, 2_7007, 3_0109, 3_0420, 3_3409,
    3_4949, 4_0283, 4_0493, 4_0549, 4_7282, 4_9146, 5_0257, 5_0359, 5_0360, 5_0361
]
lowercase_						=       [
    1, 2, 7, 8, 9, 10, 14, 25,
    26, 27, 28, 29, 31, 58, 59, 60, 61, 62,
    63, 90, 91, 92, 93, 359, 503, 522, 542, 873,
    893, 902, 918, 922, 931, 1350, 1853, 1982, 2460, 2627,
    3246, 3253, 3268, 3536, 3846, 3961, 4183, 4667, 6585, 6647,
    7273, 9061, 9383, 1_0428, 1_0929, 1_1938, 1_2033, 1_2331, 1_2562, 1_3793,
    1_4157, 1_4635, 1_5265, 1_5618, 1_6553, 1_6604, 1_8362, 1_8956, 2_0075, 2_1675,
    2_2520, 2_6130, 2_6161, 2_6435, 2_8279, 2_9464, 3_1650, 3_2302, 3_2470, 3_6865,
    4_2863, 4_7425, 4_9870, 5_0254, 5_0258, 5_0360, 5_0361, 5_0362
]
class 			_snake_case     (   lowercase__):
				UpperCamelCase__			:   Optional[int]       						="""whisper"""
				UpperCamelCase__			:   Optional[int]       						=["""past_key_values"""]
				UpperCamelCase__			:   Optional[Any]       						={"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}




				def __init__(  self   :  List[Any],   __lowercase   :  List[str]=5_1865,   __lowercase   :  Dict=80,   __lowercase   :  List[Any]=6,   __lowercase   :  Union[str, Any]=4,   __lowercase   :  Tuple=6,   __lowercase   :  Dict=4,   __lowercase   :  List[Any]=1536,   __lowercase   :  Tuple=1536,   __lowercase   :  Any=0.0,   __lowercase   :  List[str]=0.0,   __lowercase   :  List[Any]=5_0257,   __lowercase   :  List[str]=True,   __lowercase   :  str=True,   __lowercase   :  int="gelu",   __lowercase   :  Tuple=256,   __lowercase   :  Tuple=0.0,   __lowercase   :  List[Any]=0.0,   __lowercase   :  Optional[int]=0.0,   __lowercase   :  List[Any]=0.02,   __lowercase   :  Union[str, Any]=False,   __lowercase   :  str=1500,   __lowercase   :  Optional[int]=448,   __lowercase   :  Optional[Any]=5_0256,   __lowercase   :  Tuple=5_0256,   __lowercase   :  Any=5_0256,   __lowercase   :  Union[str, Any]=None,   __lowercase   :  Any=[220, 5_0256],   __lowercase   :  List[Any]=False,   __lowercase   :  int=256,   __lowercase   :  int=False,   __lowercase   :  Tuple=0.05,   __lowercase   :  int=10,   __lowercase   :  Dict=2,   __lowercase   :  List[Any]=0.0,   __lowercase   :  Optional[int]=10,   __lowercase   :  Union[str, Any]=0,   __lowercase   :  Tuple=7,   **__lowercase   :  Union[str, Any],   ):
							lowercase__   					=			vocab_size
							lowercase__   					=			num_mel_bins
							lowercase__   					=			d_model
							lowercase__   					=			encoder_layers
							lowercase__   					=			encoder_attention_heads
							lowercase__   					=			decoder_layers
							lowercase__   					=			decoder_attention_heads
							lowercase__   					=			decoder_ffn_dim
							lowercase__   					=			encoder_ffn_dim
							lowercase__   					=			dropout
							lowercase__   					=			attention_dropout
							lowercase__   					=			activation_dropout
							lowercase__   					=			activation_function
							lowercase__   					=			init_std
							lowercase__   					=			encoder_layerdrop
							lowercase__   					=			decoder_layerdrop
							lowercase__   					=			use_cache
							lowercase__   					=			encoder_layers
							lowercase__   					=			scale_embedding  # scale factor will be sqrt(d_model) if True
							lowercase__   					=			max_source_positions
							lowercase__   					=			max_target_positions

							# Audio Classification-specific parameters. Feel free to ignore for other classes.
							lowercase__   					=			classifier_proj_size
							lowercase__   					=			use_weighted_layer_sum

							# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
							lowercase__   					=			apply_spec_augment
							lowercase__   					=			mask_time_prob
							lowercase__   					=			mask_time_length
							lowercase__   					=			mask_time_min_masks
							lowercase__   					=			mask_feature_prob
							lowercase__   					=			mask_feature_length
							lowercase__   					=			mask_feature_min_masks

							lowercase__   					=			median_filter_width

							super().__init__(
							    pad_token_id=__lowercase,   bos_token_id=__lowercase,   eos_token_id=__lowercase,   is_encoder_decoder=__lowercase,   decoder_start_token_id=__lowercase,   suppress_tokens=__lowercase,   begin_suppress_tokens=__lowercase,   **__lowercase,   )
class 			_snake_case     (   lowercase__):




				@property
				def 							A__ (  self   :  str  ):
							lowercase__   					=			OrderedDict(
							    [
							        ("input_features", {0: "batch", 1: "feature_size", 2: "encoder_sequence"}),
							    ]  )
							if self.use_past:
										lowercase__   					=			{0: "batch"}
							else:
										lowercase__   					=			{0: "batch", 1: "decoder_sequence"}

							if self.use_past:
										self.fill_with_past_key_values_(__lowercase,   direction="inputs"  )

							return common_inputs




				def 							A__ (  self   :  int,   __lowercase   :  Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"],   __lowercase   :  int = -1,   __lowercase   :  int = -1,   __lowercase   :  bool = False,   __lowercase   :  Optional["TensorType"] = None,   __lowercase   :  int = 2_2050,   __lowercase   :  float = 5.0,   __lowercase   :  int = 220,   ):
							lowercase__   					=			OrderedDict()
							lowercase__   					=			OnnxConfig.generate_dummy_inputs(
							    self,   preprocessor=preprocessor.feature_extractor,   batch_size=__lowercase,   framework=__lowercase,   sampling_rate=__lowercase,   time_duration=__lowercase,   frequency=__lowercase,   )
							lowercase__   					=			encoder_inputs["input_features"].shape[2]
							lowercase__   					=			encoder_sequence_length // 2 if self.use_past else seq_length

							lowercase__   					=			super().generate_dummy_inputs(
							    preprocessor.tokenizer,   __lowercase,   __lowercase,   __lowercase,   __lowercase  )

							lowercase__   					=			encoder_inputs.pop("input_features"  )
							lowercase__   					=			decoder_inputs.pop("decoder_input_ids"  )

							if "past_key_values" in decoder_inputs:
										lowercase__   					=			decoder_inputs.pop("past_key_values"  )

							return dummy_inputs




				@property
				def 							A__ (  self   :  int  ):
							return 1e-3




