






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


						'''simple docstring'''




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






						def       _lowerCAmelCase       (	self				) ->				int:
								snake_case_				:							Optional[Any]						 =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

								snake_case_				:							List[Any]						 =       None
								if self.use_labels:
										snake_case_				:							List[str]						 =       ids_tensor([self.batch_size, self.image_size, self.image_size]		,				self.num_labels				)

								snake_case_				:							Optional[int]						 =       self.get_config()

								return config, pixel_values, labels






						def       _lowerCAmelCase       (	self				) ->				Any:
								snake_case_				:							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=_SCREAMING_SNAKE_CASE		,				initializer_range=self.initializer_range		,				is_hybrid=self.is_hybrid		,				backbone_config=_SCREAMING_SNAKE_CASE		,				backbone_featmap_shape=self.backbone_featmap_shape		,				)






						def       _lowerCAmelCase       (	self		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE				) ->				Dict:
								snake_case_				:							Tuple						 =       DPTModel(config=_SCREAMING_SNAKE_CASE				)
								model.to(_SCREAMING_SNAKE_CASE				)
								model.eval()
								snake_case_				:							Dict						 =       model(_SCREAMING_SNAKE_CASE				)
								self.parent.assertEqual(result.last_hidden_state.shape		,				(self.batch_size, self.seq_length, self.hidden_size)				)






						def       _lowerCAmelCase       (	self		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE				) ->				List[str]:
								snake_case_				:							int						 =       self.num_labels
								snake_case_				:							List[str]						 =       DPTForDepthEstimation(_SCREAMING_SNAKE_CASE				)
								model.to(_SCREAMING_SNAKE_CASE				)
								model.eval()
								snake_case_				:							Tuple						 =       model(_SCREAMING_SNAKE_CASE				)
								self.parent.assertEqual(result.predicted_depth.shape		,				(self.batch_size, self.image_size, self.image_size)				)






						def       _lowerCAmelCase       (	self		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE				) ->				List[str]:
								snake_case_				:							int						 =       self.num_labels
								snake_case_				:							Tuple						 =       DPTForSemanticSegmentation(_SCREAMING_SNAKE_CASE				)
								model.to(_SCREAMING_SNAKE_CASE				)
								model.eval()
								snake_case_				:							str						 =       model(_SCREAMING_SNAKE_CASE		,				labels=_SCREAMING_SNAKE_CASE				)
								self.parent.assertEqual(
								    result.logits.shape		,				(self.batch_size, self.num_labels, self.image_size, self.image_size)				)






						def       _lowerCAmelCase       (	self				) ->				int:
								snake_case_				:							List[str]						 =       self.prepare_config_and_inputs()
								snake_case_ ,       snake_case_ ,       snake_case_				:							Tuple						 =       config_and_inputs
								snake_case_				:							List[Any]						 =       {"pixel_values": pixel_values}
								return config, inputs_dict




@require_torch
class 		UpperCAmelCase_						(	SCREAMING_SNAKE_CASE__					,  SCREAMING_SNAKE_CASE__					,  unittest.TestCase					):


						'''simple docstring'''




						A     :   int						=					(DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else ()
						A     :   Optional[Any]						=					(
						    {
						        'depth-estimation': DPTForDepthEstimation,
						        'feature-extraction': DPTModel,
						        'image-segmentation': DPTForSemanticSegmentation,
						    }
						    if is_torch_available()
						    else {}
						)

						A     :   Optional[int]						=					False
						A     :   Dict						=					False
						A     :   Dict						=					False






						def       _lowerCAmelCase       (	self				) ->				List[str]:
								snake_case_				:							List[Any]						 =       DPTModelTester(self				)
								snake_case_				:							Optional[int]						 =       ConfigTester(self		,				config_class=_SCREAMING_SNAKE_CASE		,				has_text_modality=_SCREAMING_SNAKE_CASE		,				hidden_size=37				)






						def       _lowerCAmelCase       (	self				) ->				Optional[Any]:
								self.config_tester.run_common_tests()






						@unittest.skip(reason="DPT does not use inputs_embeds"				)
						def       _lowerCAmelCase       (	self				) ->				Optional[int]:
								pass






						def       _lowerCAmelCase       (	self				) ->				Dict:
								snake_case_ ,       snake_case_				:							int						 =       self.model_tester.prepare_config_and_inputs_for_common()

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






						def       _lowerCAmelCase       (	self				) ->				int:
								snake_case_ ,       snake_case_				:							Optional[Any]						 =       self.model_tester.prepare_config_and_inputs_for_common()

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

										snake_case_				:							Any						 =       ["pixel_values"]
										self.assertListEqual(arg_names[:1]		,				_SCREAMING_SNAKE_CASE				)






						def       _lowerCAmelCase       (	self				) ->				Optional[int]:
								snake_case_				:							Dict						 =       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE				)






						def       _lowerCAmelCase       (	self				) ->				Union[str, Any]:
								snake_case_				:							List[str]						 =       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_depth_estimation(*_SCREAMING_SNAKE_CASE				)






						def       _lowerCAmelCase       (	self				) ->				List[str]:
								snake_case_				:							List[str]						 =       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_semantic_segmentation(*_SCREAMING_SNAKE_CASE				)






						def       _lowerCAmelCase       (	self				) ->				int:
								for model_class in self.all_model_classes:
										if model_class.__name__ == "DPTForDepthEstimation":
												continue

										snake_case_ ,       snake_case_				:							Optional[int]						 =       self.model_tester.prepare_config_and_inputs_for_common()
										snake_case_				:							str						 =       True

										if model_class in get_values(_SCREAMING_SNAKE_CASE				):
												continue

										snake_case_				:							int						 =       model_class(_SCREAMING_SNAKE_CASE				)
										model.to(_SCREAMING_SNAKE_CASE				)
										model.train()
										snake_case_				:							Tuple						 =       self._prepare_for_class(_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				return_labels=_SCREAMING_SNAKE_CASE				)
										snake_case_				:							Dict						 =       model(**_SCREAMING_SNAKE_CASE				).loss
										loss.backward()






						def       _lowerCAmelCase       (	self				) ->				Any:
								for model_class in self.all_model_classes:
										if model_class.__name__ == "DPTForDepthEstimation":
												continue

										snake_case_ ,       snake_case_				:							List[Any]						 =       self.model_tester.prepare_config_and_inputs_for_common()
										snake_case_				:							Any						 =       False
										snake_case_				:							List[Any]						 =       True

										if model_class in get_values(_SCREAMING_SNAKE_CASE				) or not model_class.supports_gradient_checkpointing:
												continue
										snake_case_				:							List[str]						 =       model_class(_SCREAMING_SNAKE_CASE				)
										model.to(_SCREAMING_SNAKE_CASE				)
										model.gradient_checkpointing_enable()
										model.train()
										snake_case_				:							str						 =       self._prepare_for_class(_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				return_labels=_SCREAMING_SNAKE_CASE				)
										snake_case_				:							str						 =       model(**_SCREAMING_SNAKE_CASE				).loss
										loss.backward()






						def       _lowerCAmelCase       (	self				) ->				Union[str, Any]:
								snake_case_ ,       snake_case_				:							Tuple						 =       self.model_tester.prepare_config_and_inputs_for_common()

								snake_case_				:							Any						 =       _config_zero_init(_SCREAMING_SNAKE_CASE				)
								for model_class in self.all_model_classes:
										snake_case_				:							Optional[Any]						 =       model_class(config=_SCREAMING_SNAKE_CASE				)
										# Skip the check for the backbone
										snake_case_				:							Tuple						 =       []
										for name, module in model.named_modules():
												if module.__class__.__name__ == "DPTViTHybridEmbeddings":
														snake_case_				:							List[str]						 =       [f'''{name}.{key}''' for key in module.state_dict().keys()]
														break

										for name, param in model.named_parameters():
												if param.requires_grad:
														if name in backbone_params:
																continue
														self.assertIn(
														    ((param.data.mean() * 1e9).round() / 1e9).item()		,				[0.0, 1.0]		,				msg=f'''Parameter {name} of model {model_class} seems not properly initialized'''		,				)






						@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."				)
						def       _lowerCAmelCase       (	self				) ->				List[str]:
								pass






						@slow
						def       _lowerCAmelCase       (	self				) ->				Optional[int]:
								for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]:
										snake_case_				:							Tuple						 =       DPTModel.from_pretrained(_SCREAMING_SNAKE_CASE				)
										self.assertIsNotNone(_SCREAMING_SNAKE_CASE				)






						def       _lowerCAmelCase       (	self				) ->				Optional[int]:
								# We do this test only for DPTForDepthEstimation since it is the only model that uses readout_type
								snake_case_ ,       snake_case_				:							Optional[Any]						 =       self.model_tester.prepare_config_and_inputs_for_common()
								snake_case_				:							Any						 =       "add"
								with self.assertRaises(_SCREAMING_SNAKE_CASE				):
										snake_case_				:							List[str]						 =       DPTForDepthEstimation(_SCREAMING_SNAKE_CASE				)




def      lowerCAmelCase__	(    ):
		snake_case_				:							List[Any]						 =       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"    )
		return image




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


						'''simple docstring'''




						def       _lowerCAmelCase       (	self				) ->				List[Any]:
								snake_case_				:							int						 =       DPTImageProcessor.from_pretrained("Intel/dpt-hybrid-midas"				)
								snake_case_				:							Optional[Any]						 =       DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas"				).to(_SCREAMING_SNAKE_CASE				)

								snake_case_				:							List[str]						 =       prepare_img()
								snake_case_				:							Optional[int]						 =       image_processor(images=_SCREAMING_SNAKE_CASE		,				return_tensors="pt"				).to(_SCREAMING_SNAKE_CASE				)

								# forward pass
								with torch.no_grad():
										snake_case_				:							Tuple						 =       model(**_SCREAMING_SNAKE_CASE				)
										snake_case_				:							List[Any]						 =       outputs.predicted_depth

								# verify the predicted depth
								snake_case_				:							int						 =       torch.Size((1, 384, 384)				)
								self.assertEqual(predicted_depth.shape		,				_SCREAMING_SNAKE_CASE				)

								snake_case_				:							Dict						 =       torch.tensor(
								    [[[5.6437, 5.6146, 5.6511], [5.4371, 5.5649, 5.5958], [5.5215, 5.5184, 5.5293]]]				).to(_SCREAMING_SNAKE_CASE				)

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








from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowercase  :							Optional[int]			  =    {
    '''configuration_nllb_moe''': [
        '''NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''NllbMoeConfig''',
    ]
}

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							lowercase  :							Optional[Any]			  =    [
							    '''NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''NllbMoeForConditionalGeneration''',
							    '''NllbMoeModel''',
							    '''NllbMoePreTrainedModel''',
							    '''NllbMoeTop2Router''',
							    '''NllbMoeSparseMLP''',
							]


if TYPE_CHECKING:
							from .configuration_nllb_moe import (
							    NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP,
							    NllbMoeConfig,
							)

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_nllb_moe import (
														    NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST,
														    NllbMoeForConditionalGeneration,
														    NllbMoeModel,
														    NllbMoePreTrainedModel,
														    NllbMoeSparseMLP,
														    NllbMoeTopaRouter,
														)


else:
							import sys

							lowercase  :							Tuple			  =    _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
