


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      ,						_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      ,						):
       a		:List[Any]											= parent
       a		:str											= batch_size
       a		:Dict											= image_size
       a		:List[str]											= patch_size
       a		:Dict											= num_channels
       a		:List[str]											= is_training
       a		:int											= use_labels
       a		:Optional[Any]											= hidden_size
       a		:Union[str, Any]											= num_hidden_layers
       a		:List[Any]											= backbone_out_indices
       a		:Any											= num_attention_heads
       a		:List[Any]											= intermediate_size
       a		:int											= hidden_act
       a		:int											= hidden_dropout_prob
       a		:List[Any]											= attention_probs_dropout_prob
       a		:List[Any]											= initializer_range
       a		:str											= num_labels
       a		:List[Any]											= scope
       a		:Any											= is_hybrid
       # sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token)
       a		:Any											= (image_size // patch_size) ** 2
       a		:str											= num_patches + 1




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:int											= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

       a		:Union[str, Any]											= None
       if self.use_labels:
             a		:Optional[Any]											= ids_tensor([self.batch_size, self.image_size, self.image_size]      ,						self.num_labels					)

       a		:List[Any]											= self.get_config()

       return config, pixel_values, labels




 def  SCREAMING_SNAKE_CASE__       (	self					):
       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      ,						)




 def  SCREAMING_SNAKE_CASE__       (	self      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase					):
       a		:str											= DPTModel(config=a_					)
       model.to(a_					)
       model.eval()
       a		:Any											= model(a_					)
       self.parent.assertEqual(result.last_hidden_state.shape      ,						(self.batch_size, self.seq_length, self.hidden_size)					)




 def  SCREAMING_SNAKE_CASE__       (	self      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase					):
       a		:str											= self.num_labels
       a		:Union[str, Any]											= DPTForDepthEstimation(a_					)
       model.to(a_					)
       model.eval()
       a		:Union[str, Any]											= model(a_					)
       self.parent.assertEqual(result.predicted_depth.shape      ,						(self.batch_size, self.image_size, self.image_size)					)




 def  SCREAMING_SNAKE_CASE__       (	self      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase					):
       a		:Tuple											= self.num_labels
       a		:Optional[int]											= DPTForSemanticSegmentation(a_					)
       model.to(a_					)
       model.eval()
       a		:Union[str, Any]											= model(a_      ,						labels=a_					)
       self.parent.assertEqual(
           result.logits.shape      ,						(self.batch_size, self.num_labels, self.image_size, self.image_size)					)




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Dict											= self.prepare_config_and_inputs()
       a							,       a							,       a		:Optional[int]											= config_and_inputs
       a		:Tuple											= {'''pixel_values''': pixel_values}
       return config, inputs_dict







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

 SCREAMING_SNAKE_CASE__   = False
 SCREAMING_SNAKE_CASE__   = False
 SCREAMING_SNAKE_CASE__   = False




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:int											= DPTModelTester(self					)
       a		:Tuple											= ConfigTester(self      ,						config_class=a_      ,						has_text_modality=a_      ,						hidden_size=37					)




 def  SCREAMING_SNAKE_CASE__       (	self					):
       self.config_tester.run_common_tests()




 @unittest.skip(reason='''DPT does not use inputs_embeds'''					)
 def  SCREAMING_SNAKE_CASE__       (	self					):
       pass




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a							,       a		:List[str]											= self.model_tester.prepare_config_and_inputs_for_common()

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




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a							,       a		:Tuple											= self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
             a		:str											= model_class(a_					)
             a		:List[str]											= inspect.signature(model.forward					)
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             a		:Optional[Any]											= [*signature.parameters.keys()]

             a		:Union[str, Any]											= ['''pixel_values''']
             self.assertListEqual(arg_names[:1]      ,						a_					)




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Optional[Any]											= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*a_					)




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Any											= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_depth_estimation(*a_					)




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:int											= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_semantic_segmentation(*a_					)




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

             a							,       a		:Tuple											= self.model_tester.prepare_config_and_inputs_for_common()
             a		:Any											= True

             if model_class in get_values(a_					):
                   continue

             a		:Dict											= model_class(a_					)
             model.to(a_					)
             model.train()
             a		:Union[str, Any]											= self._prepare_for_class(a_      ,						a_      ,						return_labels=a_					)
             a		:Tuple											= model(**a_					).loss
             loss.backward()




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

             a							,       a		:List[str]											= self.model_tester.prepare_config_and_inputs_for_common()
             a		:Any											= False
             a		:Dict											= True

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




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a							,       a		:List[str]											= self.model_tester.prepare_config_and_inputs_for_common()

       a		:List[Any]											= _config_zero_init(a_					)
       for model_class in self.all_model_classes:
             a		:Union[str, Any]											= model_class(config=a_					)
             # Skip the check for the backbone
             a		:List[str]											= []
             for name, module in model.named_modules():
                   if module.__class__.__name__ == "DPTViTHybridEmbeddings":
                         a		: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  SCREAMING_SNAKE_CASE__       (	self					):
       pass




 @slow
 def  SCREAMING_SNAKE_CASE__       (	self					):
       for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             a		:Any											= DPTModel.from_pretrained(a_					)
             self.assertIsNotNone(a_					)







def        __lowerCamelCase		(				):




      """simple docstring"""





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







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




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Tuple											= DPTImageProcessor.from_pretrained('''Intel/dpt-large'''					)
       a		:Optional[int]											= DPTForDepthEstimation.from_pretrained('''Intel/dpt-large'''					).to(a_					)

       a		:Dict											= prepare_img()
       a		:List[Any]											= image_processor(images=a_      ,						return_tensors='''pt'''					).to(a_					)

       # forward pass
       with torch.no_grad():
             a		:Union[str, Any]											= model(**a_					)
             a		:List[Any]											= outputs.predicted_depth

       # verify the predicted depth
       a		:Optional[Any]											= torch.Size((1, 384, 384)					)
       self.assertEqual(predicted_depth.shape      ,						a_					)

       a		:List[Any]											= torch.tensor(
           [[6.3199, 6.3629, 6.4148], [6.3850, 6.3615, 6.4166], [6.3519, 6.3176, 6.3575]]					).to(a_					)

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




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:str											= DPTImageProcessor.from_pretrained('''Intel/dpt-large-ade'''					)
       a		:Union[str, Any]											= DPTForSemanticSegmentation.from_pretrained('''Intel/dpt-large-ade'''					).to(a_					)

       a		:int											= prepare_img()
       a		:int											= image_processor(images=a_      ,						return_tensors='''pt'''					).to(a_					)

       # forward pass
       with torch.no_grad():
             a		:List[str]											= model(**a_					)

       # verify the logits
       a		:List[str]											= torch.Size((1, 150, 480, 480)					)
       self.assertEqual(outputs.logits.shape      ,						a_					)

       a		:Tuple											= torch.tensor(
           [[4.0480, 4.2420, 4.4360], [4.3124, 4.5693, 4.8261], [4.5768, 4.8965, 5.2163]]					).to(a_					)

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




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Optional[int]											= DPTImageProcessor.from_pretrained('''Intel/dpt-large-ade'''					)
       a		:str											= DPTForSemanticSegmentation.from_pretrained('''Intel/dpt-large-ade'''					).to(a_					)

       a		:List[Any]											= prepare_img()
       a		:List[str]											= image_processor(images=a_      ,						return_tensors='''pt'''					).to(a_					)

       # forward pass
       with torch.no_grad():
             a		:List[str]											= model(**a_					)

       a		:Any											= outputs.logits.detach().cpu()

       a		:Dict											= image_processor.post_process_semantic_segmentation(outputs=a_      ,						target_sizes=[(500, 300)]					)
       a		:Optional[Any]											= torch.Size((500, 300)					)
       self.assertEqual(segmentation[0].shape      ,						a_					)

       a		:int											= image_processor.post_process_semantic_segmentation(outputs=a_					)
       a		:Optional[int]											= torch.Size((480, 480)					)
       self.assertEqual(segmentation[0].shape      ,						a_					)









from typing import Optional, Union

from ...file_utils import TensorType
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, TruncationStrategy







class 	_snake_case						(   _snake_case							):
 SCREAMING_SNAKE_CASE__   = 'MarkupLMFeatureExtractor'
 SCREAMING_SNAKE_CASE__   = ('MarkupLMTokenizer', 'MarkupLMTokenizerFast')
 SCREAMING_SNAKE_CASE__   = True




 def __call__(	self      ,						_lowerCamelCase=None      ,						_lowerCamelCase=None      ,						_lowerCamelCase=None      ,						_lowerCamelCase=None      ,						_lowerCamelCase=None      ,						_lowerCamelCase = True      ,						_lowerCamelCase = False      ,						_lowerCamelCase = None      ,						_lowerCamelCase = None      ,						_lowerCamelCase = 0      ,						_lowerCamelCase = None      ,						_lowerCamelCase = None      ,						_lowerCamelCase = None      ,						_lowerCamelCase = False      ,						_lowerCamelCase = False      ,						_lowerCamelCase = False      ,						_lowerCamelCase = False      ,						_lowerCamelCase = True      ,						_lowerCamelCase = None      ,						**_lowerCamelCase      ,						):
       # first, create nodes and xpaths
       if self.parse_html:
             if html_strings is None:
                   raise ValueError('''Make sure to pass HTML strings in case `parse_html` is set to `True`'''					)

             if nodes is not None or xpaths is not None or node_labels is not None:
                   raise ValueError(
                       '''Please don\'t pass nodes, xpaths nor node labels in case `parse_html` is set to `True`'''					)

             a		:int											= self.feature_extractor(_lowerCamelCase					)
             a		:Tuple											= features['''nodes''']
             a		:Dict											= features['''xpaths''']
       else:
             if html_strings is not None:
                   raise ValueError('''You have passed HTML strings but `parse_html` is set to `False`.'''					)
             if nodes is None or xpaths is None:
                   raise ValueError('''Make sure to pass nodes and xpaths in case `parse_html` is set to `False`'''					)

        # # second, apply the tokenizer
       if questions is not None and self.parse_html:
             if isinstance(_lowerCamelCase      ,						_lowerCamelCase					):
                   a		:Tuple											= [questions]  # add batch dimension (as the feature extractor always adds a batch dimension)

       a		:Optional[int]											= self.tokenizer(
           text=questions if questions is not None else nodes      ,						text_pair=nodes if questions is not None else None      ,						xpaths=_lowerCamelCase      ,						node_labels=_lowerCamelCase      ,						add_special_tokens=_lowerCamelCase      ,						padding=_lowerCamelCase      ,						truncation=_lowerCamelCase      ,						max_length=_lowerCamelCase      ,						stride=_lowerCamelCase      ,						pad_to_multiple_of=_lowerCamelCase      ,						return_token_type_ids=_lowerCamelCase      ,						return_attention_mask=_lowerCamelCase      ,						return_overflowing_tokens=_lowerCamelCase      ,						return_special_tokens_mask=_lowerCamelCase      ,						return_offsets_mapping=_lowerCamelCase      ,						return_length=_lowerCamelCase      ,						verbose=_lowerCamelCase      ,						return_tensors=_lowerCamelCase      ,						**_lowerCamelCase      ,						)

       return encoded_inputs




 def  SCREAMING_SNAKE_CASE__       (	self      ,						*_lowerCamelCase      ,						**_lowerCamelCase					):
       return self.tokenizer.batch_decode(*_lowerCamelCase      ,						**_lowerCamelCase					)




 def  SCREAMING_SNAKE_CASE__       (	self      ,						*_lowerCamelCase      ,						**_lowerCamelCase					):
       return self.tokenizer.decode(*_lowerCamelCase      ,						**_lowerCamelCase					)




 @property
 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Any											= self.tokenizer.model_input_names
       return tokenizer_input_names





