



"""simple docstring"""




import unittest

from transformers import AlbertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

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


if is_torch_available():
 import torch

 from transformers import (
     MODEL_FOR_PRETRAINING_MAPPING,
     AlbertForMaskedLM,
     AlbertForMultipleChoice,
     AlbertForPreTraining,
     AlbertForQuestionAnswering,
     AlbertForSequenceClassification,
     AlbertForTokenClassification,
     AlbertModel,
 )
 from transformers.models.albert.modeling_albert import ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST






class      UpperCAmelCase_  :



      def __init__(					self					:     Optional[int]					,     A					:     Optional[int]					,     A					:     Any=1_3					,     A					:     List[str]=7					,     A					:     Dict=True					,     A					:     List[str]=True					,     A					:     List[Any]=True					,     A					:     Optional[int]=True					,     A					:     str=9_9					,     A					:     Tuple=1_6					,     A					:     Optional[Any]=3_6					,     A					:     Any=6					,     A					:     List[Any]=6					,     A					:     Optional[Any]=6					,     A					:     List[str]=3_7					,     A					:     List[str]="gelu"					,     A					:     Optional[int]=0.1					,     A					:     List[str]=0.1					,     A					:     List[Any]=5_1_2					,     A					:     Dict=1_6					,     A					:     List[Any]=2					,     A					:     Dict=0.02					,     A					:     Optional[int]=3					,     A					:     Dict=4					,     A					:     int=None					,     ):
           _UpperCAmelCase      :     Optional[Any]        =   parent
           _UpperCAmelCase      :     List[str]        =   batch_size
           _UpperCAmelCase      :     Optional[int]        =   seq_length
           _UpperCAmelCase      :     Optional[int]        =   is_training
           _UpperCAmelCase      :     Optional[Any]        =   use_input_mask
           _UpperCAmelCase      :     Optional[int]        =   use_token_type_ids
           _UpperCAmelCase      :     Dict        =   use_labels
           _UpperCAmelCase      :     Dict        =   vocab_size
           _UpperCAmelCase      :     Any        =   embedding_size
           _UpperCAmelCase      :     Any        =   hidden_size
           _UpperCAmelCase      :     Optional[int]        =   num_hidden_layers
           _UpperCAmelCase      :     int        =   num_hidden_groups
           _UpperCAmelCase      :     str        =   num_attention_heads
           _UpperCAmelCase      :     Optional[Any]        =   intermediate_size
           _UpperCAmelCase      :     Optional[Any]        =   hidden_act
           _UpperCAmelCase      :     List[str]        =   hidden_dropout_prob
           _UpperCAmelCase      :     Any        =   attention_probs_dropout_prob
           _UpperCAmelCase      :     Optional[Any]        =   max_position_embeddings
           _UpperCAmelCase      :     List[str]        =   type_vocab_size
           _UpperCAmelCase      :     Union[str, Any]        =   type_sequence_label_size
           _UpperCAmelCase      :     Dict        =   initializer_range
           _UpperCAmelCase      :     Tuple        =   num_labels
           _UpperCAmelCase      :     List[str]        =   num_choices
           _UpperCAmelCase      :     List[Any]        =   scope



      def 						snake_case_ (					self					:     Union[str, Any]						):
           _UpperCAmelCase      :     int        =   ids_tensor([self.batch_size, self.seq_length]					,     self.vocab_size						)

           _UpperCAmelCase      :     Any        =   None
           if self.use_input_mask:
                _UpperCAmelCase      :     Dict        =   random_attention_mask([self.batch_size, self.seq_length]						)

           _UpperCAmelCase      :     Dict        =   None
           if self.use_token_type_ids:
                _UpperCAmelCase      :     Union[str, Any]        =   ids_tensor([self.batch_size, self.seq_length]					,     self.type_vocab_size						)

           _UpperCAmelCase      :     int        =   None
           _UpperCAmelCase      :     List[Any]        =   None
           _UpperCAmelCase      :     Dict        =   None
           if self.use_labels:
                _UpperCAmelCase      :     Dict        =   ids_tensor([self.batch_size]					,     self.type_sequence_label_size						)
                _UpperCAmelCase      :     List[str]        =   ids_tensor([self.batch_size, self.seq_length]					,     self.num_labels						)
                _UpperCAmelCase      :     Optional[int]        =   ids_tensor([self.batch_size]					,     self.num_choices						)

           _UpperCAmelCase      :     List[str]        =   self.get_config()

           return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels



      def 						snake_case_ (					self					:     Optional[int]						):
           return AlbertConfig(
               vocab_size=self.vocab_size					,     hidden_size=self.hidden_size					,     num_hidden_layers=self.num_hidden_layers					,     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					,     max_position_embeddings=self.max_position_embeddings					,     type_vocab_size=self.type_vocab_size					,     initializer_range=self.initializer_range					,     num_hidden_groups=self.num_hidden_groups					,     )



      def 						snake_case_ (					self					:     Optional[int]					,     A					:     Tuple					,     A					:     int					,     A					:     int					,     A					:     Optional[int]					,     A					:     List[str]					,     A					:     Any					,     A					:     Tuple						):
           _UpperCAmelCase      :     Optional[Any]        =   AlbertModel(config=A__						)
           model.to(A__						)
           model.eval()
           _UpperCAmelCase      :     List[str]        =   model(A__					,     attention_mask=A__					,     token_type_ids=A__						)
           _UpperCAmelCase      :     Any        =   model(A__					,     token_type_ids=A__						)
           _UpperCAmelCase      :     Union[str, Any]        =   model(A__						)
           self.parent.assertEqual(result.last_hidden_state.shape					,     (self.batch_size, self.seq_length, self.hidden_size)						)
           self.parent.assertEqual(result.pooler_output.shape					,     (self.batch_size, self.hidden_size)						)



      def 						snake_case_ (					self					:     Optional[int]					,     A					:     Union[str, Any]					,     A					:     List[str]					,     A					:     str					,     A					:     int					,     A					:     Dict					,     A					:     Union[str, Any]					,     A					:     List[str]						):
           _UpperCAmelCase      :     Dict        =   AlbertForPreTraining(config=A__						)
           model.to(A__						)
           model.eval()
           _UpperCAmelCase      :     str        =   model(
               A__					,     attention_mask=A__					,     token_type_ids=A__					,     labels=A__					,     sentence_order_label=A__					,     )
           self.parent.assertEqual(result.prediction_logits.shape					,     (self.batch_size, self.seq_length, self.vocab_size)						)
           self.parent.assertEqual(result.sop_logits.shape					,     (self.batch_size, config.num_labels)						)



      def 						snake_case_ (					self					:     Optional[int]					,     A					:     List[str]					,     A					:     Optional[Any]					,     A					:     int					,     A					:     Tuple					,     A					:     Optional[Any]					,     A					:     str					,     A					:     List[str]						):
           _UpperCAmelCase      :     Optional[int]        =   AlbertForMaskedLM(config=A__						)
           model.to(A__						)
           model.eval()
           _UpperCAmelCase      :     int        =   model(A__					,     attention_mask=A__					,     token_type_ids=A__					,     labels=A__						)
           self.parent.assertEqual(result.logits.shape					,     (self.batch_size, self.seq_length, self.vocab_size)						)



      def 						snake_case_ (					self					:     List[str]					,     A					:     Tuple					,     A					:     List[Any]					,     A					:     Any					,     A					:     List[str]					,     A					:     List[Any]					,     A					:     Tuple					,     A					:     int						):
           _UpperCAmelCase      :     Union[str, Any]        =   AlbertForQuestionAnswering(config=A__						)
           model.to(A__						)
           model.eval()
           _UpperCAmelCase      :     int        =   model(
               A__					,     attention_mask=A__					,     token_type_ids=A__					,     start_positions=A__					,     end_positions=A__					,     )
           self.parent.assertEqual(result.start_logits.shape					,     (self.batch_size, self.seq_length)						)
           self.parent.assertEqual(result.end_logits.shape					,     (self.batch_size, self.seq_length)						)



      def 						snake_case_ (					self					:     Optional[int]					,     A					:     List[str]					,     A					:     List[Any]					,     A					:     str					,     A					:     Optional[int]					,     A					:     int					,     A					:     Tuple					,     A					:     Optional[int]						):
           _UpperCAmelCase      :     Union[str, Any]        =   self.num_labels
           _UpperCAmelCase      :     List[Any]        =   AlbertForSequenceClassification(A__						)
           model.to(A__						)
           model.eval()
           _UpperCAmelCase      :     Union[str, Any]        =   model(A__					,     attention_mask=A__					,     token_type_ids=A__					,     labels=A__						)
           self.parent.assertEqual(result.logits.shape					,     (self.batch_size, self.num_labels)						)



      def 						snake_case_ (					self					:     int					,     A					:     Dict					,     A					:     List[str]					,     A					:     Optional[Any]					,     A					:     Tuple					,     A					:     List[Any]					,     A					:     Optional[int]					,     A					:     List[Any]						):
           _UpperCAmelCase      :     List[str]        =   self.num_labels
           _UpperCAmelCase      :     Dict        =   AlbertForTokenClassification(config=A__						)
           model.to(A__						)
           model.eval()
           _UpperCAmelCase      :     int        =   model(A__					,     attention_mask=A__					,     token_type_ids=A__					,     labels=A__						)
           self.parent.assertEqual(result.logits.shape					,     (self.batch_size, self.seq_length, self.num_labels)						)



      def 						snake_case_ (					self					:     str					,     A					:     List[Any]					,     A					:     Dict					,     A					:     int					,     A					:     Dict					,     A					:     int					,     A					:     List[str]					,     A					:     Tuple						):
           _UpperCAmelCase      :     Union[str, Any]        =   self.num_choices
           _UpperCAmelCase      :     Union[str, Any]        =   AlbertForMultipleChoice(config=A__						)
           model.to(A__						)
           model.eval()
           _UpperCAmelCase      :     str        =   input_ids.unsqueeze(1						).expand(-1					,     self.num_choices					,     -1						).contiguous()
           _UpperCAmelCase      :     Tuple        =   token_type_ids.unsqueeze(1						).expand(-1					,     self.num_choices					,     -1						).contiguous()
           _UpperCAmelCase      :     Optional[Any]        =   input_mask.unsqueeze(1						).expand(-1					,     self.num_choices					,     -1						).contiguous()
           _UpperCAmelCase      :     int        =   model(
               A__					,     attention_mask=A__					,     token_type_ids=A__					,     labels=A__					,     )
           self.parent.assertEqual(result.logits.shape					,     (self.batch_size, self.num_choices)						)



      def 						snake_case_ (					self					:     List[str]						):
           _UpperCAmelCase      :     Any        =   self.prepare_config_and_inputs()
           (
               (
               _UpperCAmelCase						
           )    ,	(
               _UpperCAmelCase						
           )    ,	(
               _UpperCAmelCase						
           )    ,	(
               _UpperCAmelCase						
           )    ,	(
               _UpperCAmelCase						
           )    ,	(
               _UpperCAmelCase						
           )    ,	(
               _UpperCAmelCase						
           )    ,							
           )      :     Dict        =   config_and_inputs
           _UpperCAmelCase      :     Dict        =   {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
           return config, inputs_dict







@require_torch
class      UpperCAmelCase_  (    SCREAMING_SNAKE_CASE__	,      SCREAMING_SNAKE_CASE__	,      unittest.TestCase						):
      __SCREAMING_SNAKE_CASE						:       Union[str, Any]								=			(
          (
              AlbertModel,
              AlbertForPreTraining,
              AlbertForMaskedLM,
              AlbertForMultipleChoice,
              AlbertForSequenceClassification,
              AlbertForTokenClassification,
              AlbertForQuestionAnswering,
          )
          if is_torch_available()
          else ()
      )
      __SCREAMING_SNAKE_CASE						:       int								=			(
          {
              '''feature-extraction''': AlbertModel,
              '''fill-mask''': AlbertForMaskedLM,
              '''question-answering''': AlbertForQuestionAnswering,
              '''text-classification''': AlbertForSequenceClassification,
              '''token-classification''': AlbertForTokenClassification,
              '''zero-shot''': AlbertForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      __SCREAMING_SNAKE_CASE						:       str								=			True



      def 						snake_case_ (					self					:     Optional[int]					,     A					:     Optional[Any]					,     A					:     List[Any]					,     A					:     Any=False						):
           _UpperCAmelCase      :     Tuple        =   super()._prepare_for_class(A__					,     A__					,     return_labels=A__						)

           if return_labels:
                if model_class in get_values(A__						):
                     _UpperCAmelCase      :     Union[str, Any]        =   torch.zeros(
                         (self.model_tester.batch_size, self.model_tester.seq_length)					,     dtype=torch.long					,     device=A__						)
                     _UpperCAmelCase      :     Optional[Any]        =   torch.zeros(
                         self.model_tester.batch_size					,     dtype=torch.long					,     device=A__						)
           return inputs_dict



      def 						snake_case_ (					self					:     List[str]						):
           _UpperCAmelCase      :     List[Any]        =   AlbertModelTester(self						)
           _UpperCAmelCase      :     int        =   ConfigTester(self					,     config_class=A__					,     hidden_size=3_7						)



      def 						snake_case_ (					self					:     List[str]						):
           self.config_tester.run_common_tests()



      def 						snake_case_ (					self					:     List[Any]						):
           _UpperCAmelCase      :     List[str]        =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*A__						)



      def 						snake_case_ (					self					:     List[str]						):
           _UpperCAmelCase      :     Dict        =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_pretraining(*A__						)



      def 						snake_case_ (					self					:     List[str]						):
           _UpperCAmelCase      :     Optional[Any]        =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_lm(*A__						)



      def 						snake_case_ (					self					:     Dict						):
           _UpperCAmelCase      :     Optional[int]        =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_multiple_choice(*A__						)



      def 						snake_case_ (					self					:     Dict						):
           _UpperCAmelCase      :     Dict        =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*A__						)



      def 						snake_case_ (					self					:     Tuple						):
           _UpperCAmelCase      :     Tuple        =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(*A__						)



      def 						snake_case_ (					self					:     List[str]						):
           _UpperCAmelCase      :     Optional[int]        =   self.model_tester.prepare_config_and_inputs()
           for type in ["absolute", "relative_key", "relative_key_query"]:
                _UpperCAmelCase      :     Tuple        =   type
                self.model_tester.create_and_check_model(*A__						)



      @slow
      def 						snake_case_ (					self					:     Optional[Any]						):
           for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                _UpperCAmelCase      :     Optional[Any]        =   AlbertModel.from_pretrained(A__						)
                self.assertIsNotNone(A__						)







@require_torch
class      UpperCAmelCase_  (    unittest.TestCase						):



      @slow
      def 						snake_case_ (					self					:     Optional[Any]						):
           _UpperCAmelCase      :     str        =   AlbertModel.from_pretrained("albert-base-v2"						)
           _UpperCAmelCase      :     List[str]        =   torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]						)
           _UpperCAmelCase      :     Dict        =   torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
           with torch.no_grad():
                _UpperCAmelCase      :     List[str]        =   model(A__					,     attention_mask=A__						)[0]
           _UpperCAmelCase      :     Optional[Any]        =   torch.Size((1, 1_1, 7_6_8)						)
           self.assertEqual(output.shape					,     A__						)
           _UpperCAmelCase      :     List[str]        =   torch.tensor(
               [[[-0.6_513, 1.5_035, -0.2_766], [-0.6_515, 1.5_046, -0.2_780], [-0.6_512, 1.5_049, -0.2_784]]]						)

           self.assertTrue(torch.allclose(output[:, 1:4, 1:4]					,     A__					,     atol=1e-4						)						)





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



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



      def 						snake_case_ (					self					:     List[str]						):
           _UpperCAmelCase      :     Tuple        =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

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

           _UpperCAmelCase      :     str        =   self.get_config()

           return config, pixel_values, labels



      def 						snake_case_ (					self					:     str						):
           _UpperCAmelCase      :     Tuple        =   {
               "global_padding": "same",
               "layer_type": "bottleneck",
               "depths": [3, 4, 9],
               "out_features": ["stage1", "stage2", "stage3"],
               "embedding_dynamic_padding": True,
               "hidden_sizes": [9_6, 1_9_2, 3_8_4, 7_6_8],
               "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 						snake_case_ (					self					:     Any					,     A					:     Optional[Any]					,     A					:     str					,     A					:     Dict						):
           _UpperCAmelCase      :     List[str]        =   DPTModel(config=A						)
           model.to(A						)
           model.eval()
           _UpperCAmelCase      :     List[str]        =   model(A						)
           self.parent.assertEqual(result.last_hidden_state.shape					,     (self.batch_size, self.seq_length, self.hidden_size)						)



      def 						snake_case_ (					self					:     List[str]					,     A					:     str					,     A					:     Any					,     A					:     List[Any]						):
           _UpperCAmelCase      :     str        =   self.num_labels
           _UpperCAmelCase      :     Any        =   DPTForDepthEstimation(A						)
           model.to(A						)
           model.eval()
           _UpperCAmelCase      :     Optional[int]        =   model(A						)
           self.parent.assertEqual(result.predicted_depth.shape					,     (self.batch_size, self.image_size, self.image_size)						)



      def 						snake_case_ (					self					:     List[Any]					,     A					:     Any					,     A					:     Optional[int]					,     A					:     Union[str, Any]						):
           _UpperCAmelCase      :     List[Any]        =   self.num_labels
           _UpperCAmelCase      :     Union[str, Any]        =   DPTForSemanticSegmentation(A						)
           model.to(A						)
           model.eval()
           _UpperCAmelCase      :     int        =   model(A					,     labels=A						)
           self.parent.assertEqual(
               result.logits.shape					,     (self.batch_size, self.num_labels, self.image_size, self.image_size)						)



      def 						snake_case_ (					self					:     Union[str, Any]						):
           _UpperCAmelCase      :     Union[str, Any]        =   self.prepare_config_and_inputs()
           _UpperCAmelCase    ,	_UpperCAmelCase    ,	_UpperCAmelCase      :     Dict        =   config_and_inputs
           _UpperCAmelCase      :     int        =   {"pixel_values": pixel_values}
           return config, inputs_dict







@require_torch
class      UpperCAmelCase_  (    _UpperCamelCase	,      _UpperCamelCase	,      unittest.TestCase						):
      __SCREAMING_SNAKE_CASE						:       List[str]								=			(DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else ()
      __SCREAMING_SNAKE_CASE						:       Any								=			(
          {
              'depth-estimation': DPTForDepthEstimation,
              'feature-extraction': DPTModel,
              'image-segmentation': DPTForSemanticSegmentation,
          }
          if is_torch_available()
          else {}
      )

      __SCREAMING_SNAKE_CASE						:       Dict								=			False
      __SCREAMING_SNAKE_CASE						:       Optional[int]								=			False
      __SCREAMING_SNAKE_CASE						:       Optional[Any]								=			False



      def 						snake_case_ (					self					:     int						):
           _UpperCAmelCase      :     List[str]        =   DPTModelTester(self						)
           _UpperCAmelCase      :     Union[str, Any]        =   ConfigTester(self					,     config_class=A					,     has_text_modality=A					,     hidden_size=3_7						)



      def 						snake_case_ (					self					:     int						):
           self.config_tester.run_common_tests()



      @unittest.skip(reason="DPT does not use inputs_embeds"						)
      def 						snake_case_ (					self					:     Union[str, Any]						):
           pass



      def 						snake_case_ (					self					:     Tuple						):
           _UpperCAmelCase    ,	_UpperCAmelCase      :     List[Any]        =   self.model_tester.prepare_config_and_inputs_for_common()

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



      def 						snake_case_ (					self					:     Optional[Any]						):
           _UpperCAmelCase    ,	_UpperCAmelCase      :     List[str]        =   self.model_tester.prepare_config_and_inputs_for_common()

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

                _UpperCAmelCase      :     Any        =   ["pixel_values"]
                self.assertListEqual(arg_names[:1]					,     A						)



      def 						snake_case_ (					self					:     List[str]						):
           _UpperCAmelCase      :     Dict        =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*A						)



      def 						snake_case_ (					self					:     Dict						):
           _UpperCAmelCase      :     Optional[Any]        =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_depth_estimation(*A						)



      def 						snake_case_ (					self					:     Dict						):
           _UpperCAmelCase      :     Tuple        =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_semantic_segmentation(*A						)



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

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

                if model_class in get_values(A						):
                     continue

                _UpperCAmelCase      :     int        =   model_class(A						)
                model.to(A						)
                model.train()
                _UpperCAmelCase      :     Optional[Any]        =   self._prepare_for_class(A					,     A					,     return_labels=A						)
                _UpperCAmelCase      :     Optional[Any]        =   model(**A						).loss
                loss.backward()



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

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

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



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

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



      @slow
      def 						snake_case_ (					self					:     Dict						):
           for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]:
                _UpperCAmelCase      :     Any        =   DPTModel.from_pretrained(A						)
                self.assertIsNotNone(A						)



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







def 						__snake_case						(				)  ->       Optional[int]:





     '''simple docstring'''

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






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



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

           _UpperCAmelCase      :     str        =   prepare_img()
           _UpperCAmelCase      :     Tuple        =   image_processor(images=A					,     return_tensors="pt"						).to(A						)

           # forward pass
           with torch.no_grad():
                _UpperCAmelCase      :     int        =   model(**A						)
                _UpperCAmelCase      :     List[str]        =   outputs.predicted_depth

           # verify the predicted depth
           _UpperCAmelCase      :     int        =   torch.Size((1, 3_8_4, 3_8_4)						)
           self.assertEqual(predicted_depth.shape					,     A						)

           _UpperCAmelCase      :     int        =   torch.tensor(
               [[[5.6_437, 5.6_146, 5.6_511], [5.4_371, 5.5_649, 5.5_958], [5.5_215, 5.5_184, 5.5_293]]]						).to(A						)

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