'''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		:		Optional[Any]  ,    a		:		Optional[Any]  ,    a		:		Optional[int]=2  ,    a		:		str=32  ,    a		:		Dict=16  ,    a		:		Optional[Any]=3  ,    a		:		Optional[int]=True  ,    a		:		List[str]=True  ,    a		:		Optional[Any]=32  ,    a		:		int=4  ,    a		:		Optional[int]=[0, 1, 2, 3]  ,    a		:		Union[str, Any]=4  ,    a		:		Optional[int]=37  ,    a		:		Optional[Any]="gelu"  ,    a		:		Tuple=0.1  ,    a		:		Optional[int]=0.1  ,    a		:		List[str]=0.02  ,    a		:		Any=3  ,    a		:		Tuple=[1, 3_84, 24, 24]  ,    a		:		Any=True  ,    a		:		List[Any]=None  ,    ):





         """simple docstring"""



         __lowerCamelCase          =  parent
         __lowerCamelCase          =  batch_size
         __lowerCamelCase          =  image_size
         __lowerCamelCase          =  patch_size
         __lowerCamelCase          =  num_channels
         __lowerCamelCase          =  is_training
         __lowerCamelCase          =  use_labels
         __lowerCamelCase          =  hidden_size
         __lowerCamelCase          =  num_hidden_layers
         __lowerCamelCase          =  backbone_out_indices
         __lowerCamelCase          =  num_attention_heads
         __lowerCamelCase          =  intermediate_size
         __lowerCamelCase          =  hidden_act
         __lowerCamelCase          =  hidden_dropout_prob
         __lowerCamelCase          =  attention_probs_dropout_prob
         __lowerCamelCase          =  initializer_range
         __lowerCamelCase          =  num_labels
         __lowerCamelCase          =  backbone_featmap_shape
         __lowerCamelCase          =  scope
         __lowerCamelCase          =  is_hybrid
         # sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token)
         __lowerCamelCase          =  (image_size // patch_size) ** 2
         __lowerCamelCase          =  num_patches + 1






     def        SCREAMING_SNAKE_CASE__       (			self		:		List[Any]   ):





         """simple docstring"""



         __lowerCamelCase          =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

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

         __lowerCamelCase          =  self.get_config()

         return config, pixel_values, labels






     def        SCREAMING_SNAKE_CASE__       (			self		:		int   ):





         """simple docstring"""



         __lowerCamelCase          =  {
             '''global_padding''': '''same''',
             '''layer_type''': '''bottleneck''',
             '''depths''': [3, 4, 9],
             '''out_features''': ['''stage1''', '''stage2''', '''stage3'''],
             '''embedding_dynamic_padding''': True,
             '''hidden_sizes''': [96, 1_92, 3_84, 7_68],
             '''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=UpperCamelCase_  ,    initializer_range=self.initializer_range  ,    is_hybrid=self.is_hybrid  ,    backbone_config=UpperCamelCase_  ,    backbone_featmap_shape=self.backbone_featmap_shape  ,    )






     def        SCREAMING_SNAKE_CASE__       (			self		:		Dict  ,    a		:		List[str]  ,    a		:		List[Any]  ,    a		:		str   ):





         """simple docstring"""



         __lowerCamelCase          =  DPTModel(config=UpperCamelCase_   )
         model.to(UpperCamelCase_   )
         model.eval()
         __lowerCamelCase          =  model(UpperCamelCase_   )
         self.parent.assertEqual(result.last_hidden_state.shape  ,    (self.batch_size, self.seq_length, self.hidden_size)   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		List[str]  ,    a		:		Tuple  ,    a		:		Any  ,    a		:		Tuple   ):





         """simple docstring"""



         __lowerCamelCase          =  self.num_labels
         __lowerCamelCase          =  DPTForDepthEstimation(UpperCamelCase_   )
         model.to(UpperCamelCase_   )
         model.eval()
         __lowerCamelCase          =  model(UpperCamelCase_   )
         self.parent.assertEqual(result.predicted_depth.shape  ,    (self.batch_size, self.image_size, self.image_size)   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		Any  ,    a		:		List[str]  ,    a		:		Union[str, Any]  ,    a		:		List[str]   ):





         """simple docstring"""



         __lowerCamelCase          =  self.num_labels
         __lowerCamelCase          =  DPTForSemanticSegmentation(UpperCamelCase_   )
         model.to(UpperCamelCase_   )
         model.eval()
         __lowerCamelCase          =  model(UpperCamelCase_  ,    labels=UpperCamelCase_   )
         self.parent.assertEqual(
             result.logits.shape  ,    (self.batch_size, self.num_labels, self.image_size, self.image_size)   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		List[str]   ):





         """simple docstring"""



         __lowerCamelCase          =  self.prepare_config_and_inputs()
         __lowerCamelCase     ,  __lowerCamelCase     ,  __lowerCamelCase          =  config_and_inputs
         __lowerCamelCase          =  {'''pixel_values''': pixel_values}
         return config, inputs_dict




@require_torch
class   a__				(       __lowerCamelCase							,       __lowerCamelCase							,       unittest.TestCase			):
     lowerCamelCase  :					List[str]							=(DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else ()
     lowerCamelCase  :					Dict							=(
         {
             'depth-estimation': DPTForDepthEstimation,
             'feature-extraction': DPTModel,
             'image-segmentation': DPTForSemanticSegmentation,
         }
         if is_torch_available()
         else {}
     )

     lowerCamelCase  :					Optional[int]							=False
     lowerCamelCase  :					str							=False
     lowerCamelCase  :					List[Any]							=False






     def        SCREAMING_SNAKE_CASE__       (			self		:		Tuple   ):





         """simple docstring"""



         __lowerCamelCase          =  DPTModelTester(self   )
         __lowerCamelCase          =  ConfigTester(self  ,    config_class=UpperCamelCase_  ,    has_text_modality=UpperCamelCase_  ,    hidden_size=37   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		List[Any]   ):





         """simple docstring"""



         self.config_tester.run_common_tests()






     @unittest.skip(reason='''DPT does not use inputs_embeds'''   )
     def        SCREAMING_SNAKE_CASE__       (			self		:		List[Any]   ):





         """simple docstring"""



         pass






     def        SCREAMING_SNAKE_CASE__       (			self		:		Optional[Any]   ):





         """simple docstring"""



         __lowerCamelCase     ,  __lowerCamelCase          =  self.model_tester.prepare_config_and_inputs_for_common()

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






     def        SCREAMING_SNAKE_CASE__       (			self		:		Union[str, Any]   ):





         """simple docstring"""



         __lowerCamelCase     ,  __lowerCamelCase          =  self.model_tester.prepare_config_and_inputs_for_common()

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

             __lowerCamelCase          =  ['''pixel_values''']
             self.assertListEqual(arg_names[:1]  ,    UpperCamelCase_   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		Any   ):





         """simple docstring"""



         __lowerCamelCase          =  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*UpperCamelCase_   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		Dict   ):





         """simple docstring"""



         __lowerCamelCase          =  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_depth_estimation(*UpperCamelCase_   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		int   ):





         """simple docstring"""



         __lowerCamelCase          =  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_semantic_segmentation(*UpperCamelCase_   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		str   ):





         """simple docstring"""



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

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

             if model_class in get_values(UpperCamelCase_   ):
                 continue

             __lowerCamelCase          =  model_class(UpperCamelCase_   )
             model.to(UpperCamelCase_   )
             model.train()
             __lowerCamelCase          =  self._prepare_for_class(UpperCamelCase_  ,    UpperCamelCase_  ,    return_labels=UpperCamelCase_   )
             __lowerCamelCase          =  model(**UpperCamelCase_   ).loss
             loss.backward()






     def        SCREAMING_SNAKE_CASE__       (			self		:		Optional[Any]   ):





         """simple docstring"""



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

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

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






     def        SCREAMING_SNAKE_CASE__       (			self		:		Optional[Any]   ):





         """simple docstring"""



         __lowerCamelCase     ,  __lowerCamelCase          =  self.model_tester.prepare_config_and_inputs_for_common()

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





         """simple docstring"""



         pass






     @slow
     def        SCREAMING_SNAKE_CASE__       (			self		:		str   ):





         """simple docstring"""



         for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]:
             __lowerCamelCase          =  DPTModel.from_pretrained(UpperCamelCase_   )
             self.assertIsNotNone(UpperCamelCase_   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		Dict   ):





         """simple docstring"""



         __lowerCamelCase     ,  __lowerCamelCase          =  self.model_tester.prepare_config_and_inputs_for_common()
         __lowerCamelCase          =  '''add'''
         with self.assertRaises(UpperCamelCase_   ):
             __lowerCamelCase          =  DPTForDepthEstimation(UpperCamelCase_   )






def __lowerCAmelCase			(							)     ->   Any:
    __lowerCamelCase          =  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''    )
    return image




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






     def        SCREAMING_SNAKE_CASE__       (			self		:		List[str]   ):





         """simple docstring"""



         __lowerCamelCase          =  DPTImageProcessor.from_pretrained('''Intel/dpt-hybrid-midas'''   )
         __lowerCamelCase          =  DPTForDepthEstimation.from_pretrained('''Intel/dpt-hybrid-midas'''   ).to(UpperCamelCase_   )

         __lowerCamelCase          =  prepare_img()
         __lowerCamelCase          =  image_processor(images=UpperCamelCase_  ,    return_tensors='''pt'''   ).to(UpperCamelCase_   )

         # forward pass
         with torch.no_grad():
             __lowerCamelCase          =  model(**UpperCamelCase_   )
             __lowerCamelCase          =  outputs.predicted_depth

         # verify the predicted depth
         __lowerCamelCase          =  torch.Size((1, 3_84, 3_84)   )
         self.assertEqual(predicted_depth.shape  ,    UpperCamelCase_   )

         __lowerCamelCase          =  torch.tensor(
             [[[5.64_37, 5.61_46, 5.65_11], [5.43_71, 5.56_49, 5.59_58], [5.52_15, 5.51_84, 5.52_93]]]   ).to(UpperCamelCase_   )

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

'''simple docstring'''




import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


__UpperCAmelCase												=logging.get_logger(__name__)

__UpperCAmelCase												={
    "microsoft/unispeech-sat-base-100h-libri-ft": (
        "https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json"
    ),
    # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat
}




class    a__				(       UpperCAmelCase__			):
     lowerCamelCase  :					List[Any]							="unispeech-sat"






     def __init__(			self		:		Dict  ,    a		:		str=32  ,    a		:		Any=7_68  ,    a		:		Optional[Any]=12  ,    a		:		Optional[int]=12  ,    a		:		int=30_72  ,    a		:		int="gelu"  ,    a		:		Dict=0.1  ,    a		:		Dict=0.1  ,    a		:		List[Any]=0.1  ,    a		:		Tuple=0.0  ,    a		:		Optional[Any]=0.0  ,    a		:		Tuple=0.1  ,    a		:		List[Any]=0.1  ,    a		:		str=0.02  ,    a		:		List[Any]=1e-5  ,    a		:		int="group"  ,    a		:		Union[str, Any]="gelu"  ,    a		:		Optional[int]=(5_12, 5_12, 5_12, 5_12, 5_12, 5_12, 5_12)  ,    a		:		List[Any]=(5, 2, 2, 2, 2, 2, 2)  ,    a		:		int=(10, 3, 3, 3, 3, 2, 2)  ,    a		:		Optional[Any]=False  ,    a		:		Any=1_28  ,    a		:		Tuple=16  ,    a		:		str=False  ,    a		:		Optional[Any]=True  ,    a		:		Dict=0.05  ,    a		:		List[Any]=10  ,    a		:		Any=2  ,    a		:		Optional[Any]=0.0  ,    a		:		Optional[Any]=10  ,    a		:		Any=0  ,    a		:		Any=3_20  ,    a		:		str=2  ,    a		:		List[str]=0.1  ,    a		:		List[str]=1_00  ,    a		:		List[str]=2_56  ,    a		:		str=2_56  ,    a		:		Dict=0.1  ,    a		:		Optional[Any]="mean"  ,    a		:		str=False  ,    a		:		Tuple=False  ,    a		:		Optional[Any]=2_56  ,    a		:		int=(5_12, 5_12, 5_12, 5_12, 15_00)  ,    a		:		int=(5, 3, 3, 1, 1)  ,    a		:		Any=(1, 2, 3, 1, 1)  ,    a		:		Union[str, Any]=5_12  ,    a		:		Optional[int]=0  ,    a		:		Optional[int]=1  ,    a		:		Optional[int]=2  ,    a		:		int=5_04  ,    **a		:		Dict  ,    ):





         """simple docstring"""



         super().__init__(**a  ,    pad_token_id=a  ,    bos_token_id=a  ,    eos_token_id=a   )
         __lowerCamelCase          =  hidden_size
         __lowerCamelCase          =  feat_extract_norm
         __lowerCamelCase          =  feat_extract_activation
         __lowerCamelCase          =  list(a   )
         __lowerCamelCase          =  list(a   )
         __lowerCamelCase          =  list(a   )
         __lowerCamelCase          =  conv_bias
         __lowerCamelCase          =  num_conv_pos_embeddings
         __lowerCamelCase          =  num_conv_pos_embedding_groups
         __lowerCamelCase          =  len(self.conv_dim   )
         __lowerCamelCase          =  num_hidden_layers
         __lowerCamelCase          =  intermediate_size
         __lowerCamelCase          =  hidden_act
         __lowerCamelCase          =  num_attention_heads
         __lowerCamelCase          =  hidden_dropout
         __lowerCamelCase          =  attention_dropout
         __lowerCamelCase          =  activation_dropout
         __lowerCamelCase          =  feat_proj_dropout
         __lowerCamelCase          =  final_dropout
         __lowerCamelCase          =  layerdrop
         __lowerCamelCase          =  layer_norm_eps
         __lowerCamelCase          =  initializer_range
         __lowerCamelCase          =  vocab_size
         __lowerCamelCase          =  num_clusters
         __lowerCamelCase          =  do_stable_layer_norm
         __lowerCamelCase          =  use_weighted_layer_sum

         if (
             (len(self.conv_stride   ) != self.num_feat_extract_layers)
             or (len(self.conv_kernel   ) != self.num_feat_extract_layers)
             or (len(self.conv_dim   ) != self.num_feat_extract_layers)
         ):
             raise ValueError(
                 '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =='''
                 ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ='''
                 f""" {len(self.conv_dim   )}`, `len(config.conv_stride) = {len(self.conv_stride   )}`,"""
                 f""" `len(config.conv_kernel) = {len(self.conv_kernel   )}`."""   )

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

         # parameters for pretraining with codevector quantized representations
         __lowerCamelCase          =  num_codevectors_per_group
         __lowerCamelCase          =  num_codevector_groups
         __lowerCamelCase          =  contrastive_logits_temperature
         __lowerCamelCase          =  feat_quantizer_dropout
         __lowerCamelCase          =  num_negatives
         __lowerCamelCase          =  codevector_dim
         __lowerCamelCase          =  proj_codevector_dim
         __lowerCamelCase          =  diversity_loss_weight

         # ctc loss
         __lowerCamelCase          =  ctc_loss_reduction
         __lowerCamelCase          =  ctc_zero_infinity

         # SequenceClassification-specific parameter. Feel free to ignore for other classes.
         __lowerCamelCase          =  classifier_proj_size

         # XVector-specific parameters. Feel free to ignore for other classes.
         __lowerCamelCase          =  list(a   )
         __lowerCamelCase          =  list(a   )
         __lowerCamelCase          =  list(a   )
         __lowerCamelCase          =  xvector_output_dim






     @property
     def        SCREAMING_SNAKE_CASE__       (			self		:		Any   ):





         """simple docstring"""



         return functools.reduce(operator.mul  ,    self.conv_stride  ,    1   )
