




"""simple docstring"""






import inspect
import tempfile
import unittest

from huggingface_hub import hf_hub_download

from transformers import is_torch_available
from transformers.testing_utils import is_flaky, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


UpperCamelCase__						=			1e-4

if is_torch_available():
				import torch

				from transformers import AutoformerConfig, AutoformerForPrediction, AutoformerModel
				from transformers.models.autoformer.modeling_autoformer import AutoformerDecoder, AutoformerEncoder




@require_torch
class   a__			:





						def __init__(							self      :	str     ,a__      :	Optional[int]     ,a__      :	int=16     ,a__      :	Any=13     ,a__      :	int=7     ,a__      :	int=14     ,a__      :	List[Any]=10     ,a__      :	List[Any]=19     ,a__      :	Dict=5     ,a__      :	int=4     ,a__      :	Any=True     ,a__      :	Dict=16     ,a__      :	Optional[int]=2     ,a__      :	int=4     ,a__      :	Any=4     ,a__      :	Optional[int]="gelu"     ,a__      :	Any=0.1     ,a__      :	int=0.1     ,a__      :	Optional[Any]=[1, 2, 3, 4, 5]     ,a__      :	str=25     ,a__      :	Dict=5     ,)						->       List[str]:




											"""simple docstring"""







											_lowerCAmelCase:List[str]									=  d_model
											_lowerCAmelCase:Optional[int]									=  parent
											_lowerCAmelCase:Any									=  batch_size
											_lowerCAmelCase:Union[str, Any]									=  prediction_length
											_lowerCAmelCase:Dict									=  context_length
											_lowerCAmelCase:Union[str, Any]									=  cardinality
											_lowerCAmelCase:List[Any]									=  num_time_features
											_lowerCAmelCase:List[str]									=  lags_sequence
											_lowerCAmelCase:str									=  embedding_dimension
											_lowerCAmelCase:Union[str, Any]									=  is_training
											_lowerCAmelCase:str									=  hidden_size
											_lowerCAmelCase:List[Any]									=  num_hidden_layers
											_lowerCAmelCase:Tuple									=  num_attention_heads
											_lowerCAmelCase:List[str]									=  intermediate_size
											_lowerCAmelCase:Any									=  hidden_act
											_lowerCAmelCase:int									=  hidden_dropout_prob
											_lowerCAmelCase:Union[str, Any]									=  attention_probs_dropout_prob

											_lowerCAmelCase:Optional[Any]									=  context_length
											_lowerCAmelCase:Tuple									=  prediction_length + label_length
											_lowerCAmelCase:Any									=  label_length

											_lowerCAmelCase:Any									=  moving_average
											_lowerCAmelCase:Optional[int]									=  autocorrelation_factor





						def       __UpperCamelCase     (							self      :	str)						->       Union[str, Any]:




											"""simple docstring"""







											return AutoformerConfig(
											    d_model=self.d_model     ,encoder_layers=self.num_hidden_layers     ,decoder_layers=self.num_hidden_layers     ,encoder_attention_heads=self.num_attention_heads     ,decoder_attention_heads=self.num_attention_heads     ,encoder_ffn_dim=self.intermediate_size     ,decoder_ffn_dim=self.intermediate_size     ,dropout=self.hidden_dropout_prob     ,attention_dropout=self.attention_probs_dropout_prob     ,prediction_length=self.prediction_length     ,context_length=self.context_length     ,label_length=self.label_length     ,lags_sequence=self.lags_sequence     ,num_time_features=self.num_time_features     ,num_static_categorical_features=1     ,cardinality=[self.cardinality]     ,embedding_dimension=[self.embedding_dimension]     ,moving_average=self.moving_average     ,)





						def       __UpperCamelCase     (							self      :	str     ,a__      :	Tuple)						->       Optional[int]:




											"""simple docstring"""







											_lowerCAmelCase:Dict									=  config.context_length + max(config.lags_sequence)

											_lowerCAmelCase:int									=  ids_tensor([self.batch_size, 1]     ,config.cardinality[0])
											_lowerCAmelCase:Any									=  floats_tensor([self.batch_size, _past_length, config.num_time_features])
											_lowerCAmelCase:Optional[int]									=  floats_tensor([self.batch_size, _past_length])
											_lowerCAmelCase:Optional[Any]									=  floats_tensor([self.batch_size, _past_length]) > 0.5

											# decoder inputs
											_lowerCAmelCase:List[str]									=  floats_tensor([self.batch_size, config.prediction_length, config.num_time_features])
											_lowerCAmelCase:Tuple									=  floats_tensor([self.batch_size, config.prediction_length])

											_lowerCAmelCase:Dict									=  {
											    '''past_values''': past_values,
											    '''static_categorical_features''': static_categorical_features,
											    '''past_time_features''': past_time_features,
											    '''past_observed_mask''': past_observed_mask,
											    '''future_time_features''': future_time_features,
											    '''future_values''': future_values,
											}
											return inputs_dict





						def       __UpperCamelCase     (							self      :	Union[str, Any])						->       List[str]:




											"""simple docstring"""







											_lowerCAmelCase:Union[str, Any]									=  self.get_config()
											_lowerCAmelCase:List[Any]									=  self.prepare_autoformer_inputs_dict(a__)
											return config, inputs_dict





						def       __UpperCamelCase     (							self      :	Optional[int])						->       str:




											"""simple docstring"""







											_lowerCAmelCase							,   _lowerCAmelCase:Tuple									=  self.prepare_config_and_inputs()
											return config, inputs_dict







						def       __UpperCamelCase     (							self      :	Tuple     ,a__      :	Any     ,a__      :	Optional[Any])						->       Any:




											"""simple docstring"""







											_lowerCAmelCase:Optional[int]									=  AutoformerModel(config=a__).to(a__).eval()
											_lowerCAmelCase:str									=  model(**a__)

											_lowerCAmelCase:Union[str, Any]									=  outputs.encoder_last_hidden_state
											_lowerCAmelCase:Tuple									=  outputs.last_hidden_state

											with tempfile.TemporaryDirectory() as tmpdirname:
																_lowerCAmelCase:Optional[int]									=  model.get_encoder()
																encoder.save_pretrained(a__)
																_lowerCAmelCase:Union[str, Any]									=  AutoformerEncoder.from_pretrained(a__).to(a__)

											_lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase:Union[str, Any]									=  model.create_network_inputs(**a__)
											_lowerCAmelCase							,   _lowerCAmelCase:List[Any]									=  model.decomposition_layer(transformer_inputs[:, : config.context_length, ...])

											_lowerCAmelCase:Dict									=  torch.cat(
											    (transformer_inputs[:, : config.context_length, ...], feature[:, : config.context_length, ...])     ,dim=-1     ,)
											_lowerCAmelCase:List[str]									=  encoder(inputs_embeds=a__)[0]
											self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1E-3)

											_lowerCAmelCase:Any									=  (
											    torch.mean(transformer_inputs[:, : config.context_length, ...]     ,dim=1)
											    .unsqueeze(1)
											    .repeat(1     ,config.prediction_length     ,1)
											)
											_lowerCAmelCase:Optional[int]									=  torch.zeros(
											    [transformer_inputs.shape[0], config.prediction_length, transformer_inputs.shape[2]]     ,device=enc_input.device     ,)

											_lowerCAmelCase:Any									=  torch.cat(
											    (
											        torch.cat((seasonal_input[:, -config.label_length :, ...], zeros)     ,dim=1),
											        feature[:, config.context_length - config.label_length :, ...],
											    )     ,dim=-1     ,)
											_lowerCAmelCase:List[Any]									=  torch.cat(
											    (
											        torch.cat((trend_input[:, -config.label_length :, ...], mean)     ,dim=1),
											        feature[:, config.context_length - config.label_length :, ...],
											    )     ,dim=-1     ,)

											with tempfile.TemporaryDirectory() as tmpdirname:
																_lowerCAmelCase:Tuple									=  model.get_decoder()
																decoder.save_pretrained(a__)
																_lowerCAmelCase:Any									=  AutoformerDecoder.from_pretrained(a__).to(a__)

											_lowerCAmelCase:Tuple									=  decoder(
											    trend=a__     ,inputs_embeds=a__     ,encoder_hidden_states=a__     ,)[0]

											self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1E-3)




@require_torch
class   a__			(       UpperCamelCase_					,	UpperCamelCase_					,	unittest.TestCase  ):
						snake_case__             = (AutoformerModel, AutoformerForPrediction) if is_torch_available() else ()
						snake_case__             = (AutoformerForPrediction,) if is_torch_available() else ()
						snake_case__             = {'''feature-extraction''': AutoformerModel} if is_torch_available() else {}
						snake_case__             = False
						snake_case__             = False
						snake_case__             = False
						snake_case__             = False
						snake_case__             = False
						snake_case__             = False





						def       __UpperCamelCase     (							self      :	List[Any])						->       Dict:




											"""simple docstring"""







											_lowerCAmelCase:Dict									=  AutoformerModelTester(self)
											_lowerCAmelCase:List[str]									=  ConfigTester(self     ,config_class=a__     ,has_text_modality=a__)





						def       __UpperCamelCase     (							self      :	Dict)						->       str:




											"""simple docstring"""







											self.config_tester.run_common_tests()





						def       __UpperCamelCase     (							self      :	Any)						->       Optional[int]:




											"""simple docstring"""







											_lowerCAmelCase							,   _lowerCAmelCase:Optional[int]									=  self.model_tester.prepare_config_and_inputs()
											for model_class in self.all_model_classes:
																_lowerCAmelCase:List[str]									=  model_class(a__)

																with tempfile.TemporaryDirectory() as tmpdirname:
																					model.save_pretrained(a__)
																					_lowerCAmelCase							,   _lowerCAmelCase:Any									=  model_class.from_pretrained(a__     ,output_loading_info=a__)
																self.assertEqual(info['''missing_keys''']     ,[])





						def       __UpperCamelCase     (							self      :	int)						->       Optional[int]:




											"""simple docstring"""







											_lowerCAmelCase:Tuple									=  self.model_tester.prepare_config_and_inputs_for_common()
											self.model_tester.check_encoder_decoder_model_standalone(*a__)





						@unittest.skip(reason='''Model has no tokens embeddings''')
						def       __UpperCamelCase     (							self      :	Any)						->       Any:




											"""simple docstring"""







											pass





						def       __UpperCamelCase     (							self      :	Optional[Any])						->       Dict:




											"""simple docstring"""







											_lowerCAmelCase:Optional[Any]									=  inspect.signature(getattr(a__     ,'''forward'''))
											# The main input is the name of the argument after `self`
											_lowerCAmelCase:Optional[int]									=  list(model_signature.parameters.keys())[1]
											self.assertEqual(AutoformerModel.main_input_name     ,a__)





						def       __UpperCamelCase     (							self      :	Optional[int])						->       Union[str, Any]:




											"""simple docstring"""







											_lowerCAmelCase							,   _lowerCAmelCase:List[Any]									=  self.model_tester.prepare_config_and_inputs_for_common()

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

																_lowerCAmelCase:Optional[Any]									=  [
																    '''past_values''',
																    '''past_time_features''',
																    '''past_observed_mask''',
																    '''static_categorical_features''',
																    '''static_real_features''',
																    '''future_values''',
																    '''future_time_features''',
																]

																if model.__class__.__name__ in ["AutoformerForPrediction"]:
																					expected_arg_names.append('''future_observed_mask''')

																expected_arg_names.extend(
																    [
																        '''decoder_attention_mask''',
																        '''head_mask''',
																        '''decoder_head_mask''',
																        '''cross_attn_head_mask''',
																        '''encoder_outputs''',
																        '''past_key_values''',
																        '''output_hidden_states''',
																        '''output_attentions''',
																        '''use_cache''',
																        '''return_dict''',
																    ])

																self.assertListEqual(arg_names[: len(a__)]     ,a__)





						def       __UpperCamelCase     (							self      :	Dict)						->       Optional[Any]:




											"""simple docstring"""







											_lowerCAmelCase							,   _lowerCAmelCase:List[str]									=  self.model_tester.prepare_config_and_inputs_for_common()
											_lowerCAmelCase:Optional[Any]									=  True

											_lowerCAmelCase:Tuple									=  getattr(self.model_tester     ,'''seq_length'''     ,a__)
											_lowerCAmelCase:Optional[int]									=  getattr(self.model_tester     ,'''decoder_seq_length'''     ,a__)
											_lowerCAmelCase:int									=  getattr(self.model_tester     ,'''encoder_seq_length'''     ,a__)
											_lowerCAmelCase:int									=  getattr(self.model_tester     ,'''d_model'''     ,a__)
											_lowerCAmelCase:Union[str, Any]									=  getattr(self.model_tester     ,'''num_attention_heads'''     ,a__)
											_lowerCAmelCase:List[Any]									=  d_model // num_attention_heads

											for model_class in self.all_model_classes:
																_lowerCAmelCase:Dict									=  True
																_lowerCAmelCase:List[Any]									=  False
																_lowerCAmelCase:List[Any]									=  True
																_lowerCAmelCase:Any									=  model_class(a__)
																model.to(a__)
																model.eval()
																with torch.no_grad():
																					_lowerCAmelCase:int									=  model(**self._prepare_for_class(a__     ,a__))
																_lowerCAmelCase:Optional[int]									=  outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
																self.assertEqual(len(a__)     ,self.model_tester.num_hidden_layers)

																# check that output_attentions also work using config
																del inputs_dict["output_attentions"]
																_lowerCAmelCase:Optional[int]									=  True
																_lowerCAmelCase:List[str]									=  model_class(a__)
																model.to(a__)
																model.eval()
																with torch.no_grad():
																					_lowerCAmelCase:Optional[int]									=  model(**self._prepare_for_class(a__     ,a__))
																_lowerCAmelCase:List[Any]									=  outputs.encoder_attentions
																self.assertEqual(len(a__)     ,self.model_tester.num_hidden_layers)

																self.assertListEqual(
																    list(attentions[0].shape[-3:])     ,[self.model_tester.num_attention_heads, encoder_seq_length, dim]     ,)
																_lowerCAmelCase:Union[str, Any]									=  len(a__)

																_lowerCAmelCase:Union[str, Any]									=  7

																if "last_hidden_state" in outputs:
																					correct_outlen += 1

																if "trend" in outputs:
																					correct_outlen += 1

																if "past_key_values" in outputs:
																					correct_outlen += 1  # past_key_values have been returned

																if "loss" in outputs:
																					correct_outlen += 1

																if "params" in outputs:
																					correct_outlen += 1

																self.assertEqual(a__     ,a__)

																# decoder attentions
																_lowerCAmelCase:Optional[Any]									=  outputs.decoder_attentions
																self.assertIsInstance(a__     ,(list, tuple))
																self.assertEqual(len(a__)     ,self.model_tester.num_hidden_layers)
																self.assertListEqual(
																    list(decoder_attentions[0].shape[-3:])     ,[self.model_tester.num_attention_heads, decoder_seq_length, dim]     ,)

																# cross attentions
																_lowerCAmelCase:Optional[Any]									=  outputs.cross_attentions
																self.assertIsInstance(a__     ,(list, tuple))
																self.assertEqual(len(a__)     ,self.model_tester.num_hidden_layers)
																self.assertListEqual(
																    list(cross_attentions[0].shape[-3:])     ,[self.model_tester.num_attention_heads, decoder_seq_length, dim]     ,)

											# Check attention is always last and order is fine
											_lowerCAmelCase:str									=  True
											_lowerCAmelCase:Any									=  True
											_lowerCAmelCase:Optional[int]									=  model_class(a__)
											model.to(a__)
											model.eval()
											with torch.no_grad():
																_lowerCAmelCase:List[str]									=  model(**self._prepare_for_class(a__     ,a__))

											self.assertEqual(out_len + 2     ,len(a__))

											_lowerCAmelCase:Optional[int]									=  outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions

											self.assertEqual(len(a__)     ,self.model_tester.num_hidden_layers)
											self.assertListEqual(
											    list(self_attentions[0].shape[-3:])     ,[self.model_tester.num_attention_heads, encoder_seq_length, dim]     ,)







						@is_flaky()
						def       __UpperCamelCase     (							self      :	str)						->       Union[str, Any]:




											"""simple docstring"""







											super().test_retain_grad_hidden_states_attentions()





def 							UpperCAmelCase		(		snake_case			:					int="train-batch.pt"    ):
					_lowerCAmelCase:int									=  hf_hub_download(repo_id='''hf-internal-testing/tourism-monthly-batch'''      ,       filename=snake_case      ,       repo_type='''dataset'''    )
					_lowerCAmelCase:List[Any]									=  torch.load(snake_case      ,       map_location=snake_case    )
					return batch





@require_torch
@slow
class   a__			(       unittest.TestCase  ):





						def       __UpperCamelCase     (							self      :	List[str])						->       Union[str, Any]:




											"""simple docstring"""







											_lowerCAmelCase:Tuple									=  AutoformerModel.from_pretrained('''huggingface/autoformer-tourism-monthly''').to(a__)
											_lowerCAmelCase:Tuple									=  prepare_batch()

											with torch.no_grad():
																_lowerCAmelCase:Optional[Any]									=  model(
																    past_values=batch['''past_values''']     ,past_time_features=batch['''past_time_features''']     ,past_observed_mask=batch['''past_observed_mask''']     ,static_categorical_features=batch['''static_categorical_features''']     ,future_values=batch['''future_values''']     ,future_time_features=batch['''future_time_features''']     ,)[0]

											_lowerCAmelCase:str									=  torch.Size(
											    (64, model.config.prediction_length + model.config.label_length, model.config.feature_size))
											self.assertEqual(output.shape     ,a__)

											_lowerCAmelCase:Union[str, Any]									=  torch.tensor(
											    [[0.3593, -1.3398, 0.6330], [0.2279, 1.5396, -0.1792], [0.0450, 1.3225, -0.2335]]     ,device=a__)
											self.assertTrue(torch.allclose(output[0, :3, :3]     ,a__     ,atol=a__))





						def       __UpperCamelCase     (							self      :	Union[str, Any])						->       str:




											"""simple docstring"""







											_lowerCAmelCase:List[Any]									=  AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''').to(a__)
											_lowerCAmelCase:str									=  prepare_batch('''val-batch.pt''')
											with torch.no_grad():
																_lowerCAmelCase:Tuple									=  model(
																    past_values=batch['''past_values''']     ,past_time_features=batch['''past_time_features''']     ,past_observed_mask=batch['''past_observed_mask''']     ,static_categorical_features=batch['''static_categorical_features''']     ,).encoder_last_hidden_state
											_lowerCAmelCase:int									=  torch.Size((64, model.config.context_length, model.config.d_model))
											self.assertEqual(output.shape     ,a__)

											_lowerCAmelCase:Dict									=  torch.tensor(
											    [[-0.0734, -0.9036, 0.8358], [4.7186, 2.4113, 1.9581], [1.7953, 2.3558, 1.2970]]     ,device=a__)
											self.assertTrue(torch.allclose(output[0, :3, :3]     ,a__     ,atol=a__))







						def       __UpperCamelCase     (							self      :	Tuple)						->       List[str]:




											"""simple docstring"""







											_lowerCAmelCase:int									=  AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''').to(a__)
											_lowerCAmelCase:List[str]									=  prepare_batch('''val-batch.pt''')
											with torch.no_grad():
																_lowerCAmelCase:Tuple									=  model.generate(
																    static_categorical_features=batch['''static_categorical_features''']     ,past_time_features=batch['''past_time_features''']     ,past_values=batch['''past_values''']     ,future_time_features=batch['''future_time_features''']     ,past_observed_mask=batch['''past_observed_mask''']     ,)
											_lowerCAmelCase:int									=  torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length))
											self.assertEqual(outputs.sequences.shape     ,a__)

											_lowerCAmelCase:Dict									=  torch.tensor([3130.6763, 4056.5293, 7053.0786]     ,device=a__)
											_lowerCAmelCase:List[Any]									=  outputs.sequences.mean(dim=1)
											self.assertTrue(torch.allclose(mean_prediction[0, -3:]     ,a__     ,rtol=1E-1))












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





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




											"""simple docstring"""







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





						def       __UpperCamelCase     (							self      :	Tuple)						->       Dict:




											"""simple docstring"""







											_lowerCAmelCase:List[str]									=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

											_lowerCAmelCase:Optional[Any]									=  self.get_config()

											return config, pixel_values, labels





						def       __UpperCamelCase     (							self      :	Union[str, Any])						->       Dict:




											"""simple docstring"""







											_lowerCAmelCase:Optional[int]									=  {
											    '''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       __UpperCamelCase     (							self      :	int     ,a__      :	Tuple     ,a__      :	Tuple     ,a__      :	Tuple)						->       Dict:




											"""simple docstring"""







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





						def       __UpperCamelCase     (							self      :	str     ,a__      :	Optional[int]     ,a__      :	Union[str, Any]     ,a__      :	int)						->       Optional[int]:




											"""simple docstring"""







											_lowerCAmelCase:Tuple									=  self.num_labels
											_lowerCAmelCase:List[Any]									=  DPTForDepthEstimation(a__)
											model.to(a__)
											model.eval()
											_lowerCAmelCase:int									=  model(a__)
											self.parent.assertEqual(result.predicted_depth.shape     ,(self.batch_size, self.image_size, self.image_size))





						def       __UpperCamelCase     (							self      :	Union[str, Any]     ,a__      :	Dict     ,a__      :	List[Any]     ,a__      :	str)						->       Optional[int]:




											"""simple docstring"""







											_lowerCAmelCase:Dict									=  self.num_labels
											_lowerCAmelCase:str									=  DPTForSemanticSegmentation(a__)
											model.to(a__)
											model.eval()
											_lowerCAmelCase:int									=  model(a__     ,labels=a__)
											self.parent.assertEqual(
											    result.logits.shape     ,(self.batch_size, self.num_labels, self.image_size, self.image_size))







						def       __UpperCamelCase     (							self      :	Optional[int])						->       List[str]:




											"""simple docstring"""







											_lowerCAmelCase:List[str]									=  self.prepare_config_and_inputs()
											_lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase:Any									=  config_and_inputs
											_lowerCAmelCase:Union[str, Any]									=  {'''pixel_values''': pixel_values}
											return config, inputs_dict




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

						snake_case__             = False
						snake_case__             = False
						snake_case__             = False





						def       __UpperCamelCase     (							self      :	Optional[Any])						->       List[str]:




											"""simple docstring"""







											_lowerCAmelCase:List[str]									=  DPTModelTester(self)
											_lowerCAmelCase:List[str]									=  ConfigTester(self     ,config_class=a__     ,has_text_modality=a__     ,hidden_size=37)





						def       __UpperCamelCase     (							self      :	str)						->       Optional[int]:




											"""simple docstring"""







											self.config_tester.run_common_tests()





						@unittest.skip(reason='''DPT does not use inputs_embeds''')
						def       __UpperCamelCase     (							self      :	Optional[int])						->       Dict:




											"""simple docstring"""







											pass





						def       __UpperCamelCase     (							self      :	Optional[Any])						->       List[Any]:




											"""simple docstring"""







											_lowerCAmelCase							,   _lowerCAmelCase:Union[str, Any]									=  self.model_tester.prepare_config_and_inputs_for_common()

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





						def       __UpperCamelCase     (							self      :	Dict)						->       int:




											"""simple docstring"""







											_lowerCAmelCase							,   _lowerCAmelCase:Any									=  self.model_tester.prepare_config_and_inputs_for_common()

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

																_lowerCAmelCase:Any									=  ['''pixel_values''']
																self.assertListEqual(arg_names[:1]     ,a__)





						def       __UpperCamelCase     (							self      :	Tuple)						->       int:




											"""simple docstring"""







											_lowerCAmelCase:Optional[Any]									=  self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*a__)





						def       __UpperCamelCase     (							self      :	Dict)						->       int:




											"""simple docstring"""







											_lowerCAmelCase:Dict									=  self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_depth_estimation(*a__)





						def       __UpperCamelCase     (							self      :	Optional[Any])						->       Union[str, Any]:




											"""simple docstring"""







											_lowerCAmelCase:Union[str, Any]									=  self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_semantic_segmentation(*a__)





						def       __UpperCamelCase     (							self      :	List[Any])						->       List[str]:




											"""simple docstring"""







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

																_lowerCAmelCase							,   _lowerCAmelCase:Dict									=  self.model_tester.prepare_config_and_inputs_for_common()
																_lowerCAmelCase:Union[str, Any]									=  True

																if model_class in get_values(a__):
																					continue

																_lowerCAmelCase:Union[str, Any]									=  model_class(a__)
																model.to(a__)
																model.train()
																_lowerCAmelCase:Optional[int]									=  self._prepare_for_class(a__     ,a__     ,return_labels=a__)
																_lowerCAmelCase:Union[str, Any]									=  model(**a__).loss
																loss.backward()





						def       __UpperCamelCase     (							self      :	Optional[Any])						->       List[str]:




											"""simple docstring"""







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

																_lowerCAmelCase							,   _lowerCAmelCase:Optional[Any]									=  self.model_tester.prepare_config_and_inputs_for_common()
																_lowerCAmelCase:str									=  False
																_lowerCAmelCase:str									=  True

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





						def       __UpperCamelCase     (							self      :	Any)						->       int:




											"""simple docstring"""







											_lowerCAmelCase							,   _lowerCAmelCase:Optional[Any]									=  self.model_tester.prepare_config_and_inputs_for_common()

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




											"""simple docstring"""







											pass





						@slow
						def       __UpperCamelCase     (							self      :	List[Any])						->       List[str]:




											"""simple docstring"""







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







						def       __UpperCamelCase     (							self      :	int)						->       Optional[int]:




											"""simple docstring"""







											_lowerCAmelCase							,   _lowerCAmelCase:List[str]									=  self.model_tester.prepare_config_and_inputs_for_common()
											_lowerCAmelCase:Any									=  '''add'''
											with self.assertRaises(a__):
																_lowerCAmelCase:Any									=  DPTForDepthEstimation(a__)





def 							UpperCAmelCase		(		):
					_lowerCAmelCase:Union[str, Any]									=  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''    )
					return image





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





						def       __UpperCamelCase     (							self      :	Optional[int])						->       Union[str, Any]:




											"""simple docstring"""







											_lowerCAmelCase:List[str]									=  DPTImageProcessor.from_pretrained('''Intel/dpt-hybrid-midas''')
											_lowerCAmelCase:Optional[int]									=  DPTForDepthEstimation.from_pretrained('''Intel/dpt-hybrid-midas''').to(a__)

											_lowerCAmelCase:Tuple									=  prepare_img()
											_lowerCAmelCase:List[str]									=  image_processor(images=a__     ,return_tensors='''pt''').to(a__)

											# forward pass
											with torch.no_grad():
																_lowerCAmelCase:Dict									=  model(**a__)
																_lowerCAmelCase:List[str]									=  outputs.predicted_depth

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

											_lowerCAmelCase:Any									=  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))






