





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

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

      def 						SCREAMING_SNAKE_CASE_			(			self   :int						):
       __SCREAMING_SNAKE_CASE :			Optional[int]												=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

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

       __SCREAMING_SNAKE_CASE :			Tuple												=       self.get_config()

       return config, pixel_values, labels

      def 						SCREAMING_SNAKE_CASE_			(			self   :str						):
       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=_lowerCamelCase						,							initializer_range=self.initializer_range						,							is_hybrid=self.is_hybrid						,							)

      def 						SCREAMING_SNAKE_CASE_			(			self   :List[Any]						,							_lowerCamelCase   :List[str]						,							_lowerCamelCase   :Optional[Any]						,							_lowerCamelCase   :Tuple						):
       __SCREAMING_SNAKE_CASE :			Dict												=       DPTModel(config=_lowerCamelCase						)
       model.to(_lowerCamelCase						)
       model.eval()
       __SCREAMING_SNAKE_CASE :			Optional[Any]												=       model(_lowerCamelCase						)
       self.parent.assertEqual(result.last_hidden_state.shape						,							(self.batch_size, self.seq_length, self.hidden_size)						)

      def 						SCREAMING_SNAKE_CASE_			(			self   :Optional[Any]						,							_lowerCamelCase   :Optional[int]						,							_lowerCamelCase   :Any						,							_lowerCamelCase   :List[str]						):
       __SCREAMING_SNAKE_CASE :			List[str]												=       self.num_labels
       __SCREAMING_SNAKE_CASE :			Optional[int]												=       DPTForDepthEstimation(_lowerCamelCase						)
       model.to(_lowerCamelCase						)
       model.eval()
       __SCREAMING_SNAKE_CASE :			Tuple												=       model(_lowerCamelCase						)
       self.parent.assertEqual(result.predicted_depth.shape						,							(self.batch_size, self.image_size, self.image_size)						)

      def 						SCREAMING_SNAKE_CASE_			(			self   :Any						,							_lowerCamelCase   :List[Any]						,							_lowerCamelCase   :List[Any]						,							_lowerCamelCase   :Optional[Any]						):
       __SCREAMING_SNAKE_CASE :			Optional[Any]												=       self.num_labels
       __SCREAMING_SNAKE_CASE :			Any												=       DPTForSemanticSegmentation(_lowerCamelCase						)
       model.to(_lowerCamelCase						)
       model.eval()
       __SCREAMING_SNAKE_CASE :			List[Any]												=       model(_lowerCamelCase						,							labels=_lowerCamelCase						)
       self.parent.assertEqual(
           result.logits.shape						,							(self.batch_size, self.num_labels, self.image_size, self.image_size)						)



      def 						SCREAMING_SNAKE_CASE_			(			self   :Any						):
       __SCREAMING_SNAKE_CASE :			Optional[Any]												=       self.prepare_config_and_inputs()
       __SCREAMING_SNAKE_CASE							,      __SCREAMING_SNAKE_CASE							,      __SCREAMING_SNAKE_CASE :			List[Any]												=       config_and_inputs
       __SCREAMING_SNAKE_CASE :			Dict												=       {'''pixel_values''': pixel_values}
       return config, inputs_dict







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

      lowerCamelCase__						= False
      lowerCamelCase__						= False
      lowerCamelCase__						= False

      def 						SCREAMING_SNAKE_CASE_			(			self   :Any						):
       __SCREAMING_SNAKE_CASE :			List[Any]												=       DPTModelTester(self						)
       __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       ConfigTester(self						,							config_class=_lowerCamelCase						,							has_text_modality=_lowerCamelCase						,							hidden_size=3_7						)

      def 						SCREAMING_SNAKE_CASE_			(			self   :Any						):
       self.config_tester.run_common_tests()

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

      def 						SCREAMING_SNAKE_CASE_			(			self   :List[Any]						):
       __SCREAMING_SNAKE_CASE							,      __SCREAMING_SNAKE_CASE :			Tuple												=       self.model_tester.prepare_config_and_inputs_for_common()

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

      def 						SCREAMING_SNAKE_CASE_			(			self   :str						):
       __SCREAMING_SNAKE_CASE							,      __SCREAMING_SNAKE_CASE :			str												=       self.model_tester.prepare_config_and_inputs_for_common()

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

        __SCREAMING_SNAKE_CASE :			Dict												=       ['''pixel_values''']
        self.assertListEqual(arg_names[:1]						,							_lowerCamelCase						)

      def 						SCREAMING_SNAKE_CASE_			(			self   :List[str]						):
       __SCREAMING_SNAKE_CASE :			Any												=       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_lowerCamelCase						)

      def 						SCREAMING_SNAKE_CASE_			(			self   :Optional[Any]						):
       __SCREAMING_SNAKE_CASE :			Any												=       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_depth_estimation(*_lowerCamelCase						)

      def 						SCREAMING_SNAKE_CASE_			(			self   :Union[str, Any]						):
       __SCREAMING_SNAKE_CASE :			List[str]												=       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_semantic_segmentation(*_lowerCamelCase						)

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

        __SCREAMING_SNAKE_CASE							,      __SCREAMING_SNAKE_CASE :			str												=       self.model_tester.prepare_config_and_inputs_for_common()
        __SCREAMING_SNAKE_CASE :			str												=       True

        if model_class in get_values(_lowerCamelCase						):
         continue

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

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

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

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

      def 						SCREAMING_SNAKE_CASE_			(			self   :Optional[Any]						):
       __SCREAMING_SNAKE_CASE							,      __SCREAMING_SNAKE_CASE :			str												=       self.model_tester.prepare_config_and_inputs_for_common()

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



      @slow
      def 						SCREAMING_SNAKE_CASE_			(			self   :Optional[Any]						):
       for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        __SCREAMING_SNAKE_CASE :			Dict												=       DPTModel.from_pretrained(_lowerCamelCase						)
        self.assertIsNotNone(_lowerCamelCase						)







def 		lowerCAmelCase_	(  ):

 '''simple docstring'''






 __SCREAMING_SNAKE_CASE :			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   :Optional[Any]						):
       __SCREAMING_SNAKE_CASE :			Any												=       DPTImageProcessor.from_pretrained('''Intel/dpt-large'''						)
       __SCREAMING_SNAKE_CASE :			Tuple												=       DPTForDepthEstimation.from_pretrained('''Intel/dpt-large'''						).to(_lowerCamelCase						)

       __SCREAMING_SNAKE_CASE :			Optional[Any]												=       prepare_img()
       __SCREAMING_SNAKE_CASE :			List[str]												=       image_processor(images=_lowerCamelCase						,							return_tensors='''pt'''						).to(_lowerCamelCase						)

       # forward pass
       with torch.no_grad():
        __SCREAMING_SNAKE_CASE :			Optional[int]												=       model(**_lowerCamelCase						)
        __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       outputs.predicted_depth

       # verify the predicted depth
       __SCREAMING_SNAKE_CASE :			Dict												=       torch.Size((1, 3_8_4, 3_8_4)						)
       self.assertEqual(predicted_depth.shape						,							_lowerCamelCase						)

       __SCREAMING_SNAKE_CASE :			int												=       torch.tensor(
           [[6.3_1_9_9, 6.3_6_2_9, 6.4_1_4_8], [6.3_8_5_0, 6.3_6_1_5, 6.4_1_6_6], [6.3_5_1_9, 6.3_1_7_6, 6.3_5_7_5]]						).to(_lowerCamelCase						)

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

      def 						SCREAMING_SNAKE_CASE_			(			self   :Dict						):
       __SCREAMING_SNAKE_CASE :			List[Any]												=       DPTImageProcessor.from_pretrained('''Intel/dpt-large-ade'''						)
       __SCREAMING_SNAKE_CASE :			List[Any]												=       DPTForSemanticSegmentation.from_pretrained('''Intel/dpt-large-ade'''						).to(_lowerCamelCase						)

       __SCREAMING_SNAKE_CASE :			str												=       prepare_img()
       __SCREAMING_SNAKE_CASE :			Any												=       image_processor(images=_lowerCamelCase						,							return_tensors='''pt'''						).to(_lowerCamelCase						)

       # forward pass
       with torch.no_grad():
        __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       model(**_lowerCamelCase						)

       # verify the logits
       __SCREAMING_SNAKE_CASE :			Any												=       torch.Size((1, 1_5_0, 4_8_0, 4_8_0)						)
       self.assertEqual(outputs.logits.shape						,							_lowerCamelCase						)

       __SCREAMING_SNAKE_CASE :			List[str]												=       torch.tensor(
           [[4.0_4_8_0, 4.2_4_2_0, 4.4_3_6_0], [4.3_1_2_4, 4.5_6_9_3, 4.8_2_6_1], [4.5_7_6_8, 4.8_9_6_5, 5.2_1_6_3]]						).to(_lowerCamelCase						)

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



      def 						SCREAMING_SNAKE_CASE_			(			self   :Any						):
       __SCREAMING_SNAKE_CASE :			str												=       DPTImageProcessor.from_pretrained('''Intel/dpt-large-ade'''						)
       __SCREAMING_SNAKE_CASE :			int												=       DPTForSemanticSegmentation.from_pretrained('''Intel/dpt-large-ade'''						).to(_lowerCamelCase						)

       __SCREAMING_SNAKE_CASE :			List[str]												=       prepare_img()
       __SCREAMING_SNAKE_CASE :			Dict												=       image_processor(images=_lowerCamelCase						,							return_tensors='''pt'''						).to(_lowerCamelCase						)

       # forward pass
       with torch.no_grad():
        __SCREAMING_SNAKE_CASE :			int												=       model(**_lowerCamelCase						)

       __SCREAMING_SNAKE_CASE :			Dict												=       outputs.logits.detach().cpu()

       __SCREAMING_SNAKE_CASE :			Tuple												=       image_processor.post_process_semantic_segmentation(outputs=_lowerCamelCase						,							target_sizes=[(5_0_0, 3_0_0)]						)
       __SCREAMING_SNAKE_CASE :			Any												=       torch.Size((5_0_0, 3_0_0)						)
       self.assertEqual(segmentation[0].shape						,							_lowerCamelCase						)

       __SCREAMING_SNAKE_CASE :			List[Any]												=       image_processor.post_process_semantic_segmentation(outputs=_lowerCamelCase						)
       __SCREAMING_SNAKE_CASE :			Any												=       torch.Size((4_8_0, 4_8_0)						)
       self.assertEqual(segmentation[0].shape						,							_lowerCamelCase						)








"""simple docstring"""





from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import TensorType, is_torch_available, logging


_lowerCamelCase       =	logging.get_logger(__name__)

_lowerCamelCase       =	{
    '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/config.json''',
    # See all M2M100 models at https://huggingface.co/models?filter=m2m_100
}





class 			snake_case		(   __UpperCAmelCase   ):
      lowerCamelCase__						= '''m2m_100'''
      lowerCamelCase__						= ['''past_key_values''']
      lowerCamelCase__						= {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}

      def __init__(			self   :int						,							_lowerCamelCase   :Union[str, Any]=1_2_8_1_1_2						,							_lowerCamelCase   :str=1_0_2_4						,							_lowerCamelCase   :Any=1_2						,							_lowerCamelCase   :Dict=4_0_9_6						,							_lowerCamelCase   :List[str]=1_6						,							_lowerCamelCase   :Union[str, Any]=1_2						,							_lowerCamelCase   :Any=4_0_9_6						,							_lowerCamelCase   :str=1_6						,							_lowerCamelCase   :Union[str, Any]=0.0_5						,							_lowerCamelCase   :Any=0.0_5						,							_lowerCamelCase   :int=True						,							_lowerCamelCase   :Dict=True						,							_lowerCamelCase   :List[str]="relu"						,							_lowerCamelCase   :List[Any]=1_0_2_4						,							_lowerCamelCase   :Dict=0.1						,							_lowerCamelCase   :List[Any]=0.1						,							_lowerCamelCase   :Any=0.0						,							_lowerCamelCase   :Tuple=0.0_2						,							_lowerCamelCase   :Dict=2						,							_lowerCamelCase   :List[str]=True						,							_lowerCamelCase   :Any=1						,							_lowerCamelCase   :Dict=0						,							_lowerCamelCase   :List[Any]=2						,							**_lowerCamelCase   :Optional[Any]						,							):
       __SCREAMING_SNAKE_CASE :			Any												=       vocab_size
       __SCREAMING_SNAKE_CASE :			Any												=       max_position_embeddings
       __SCREAMING_SNAKE_CASE :			int												=       d_model
       __SCREAMING_SNAKE_CASE :			List[Any]												=       encoder_ffn_dim
       __SCREAMING_SNAKE_CASE :			Dict												=       encoder_layers
       __SCREAMING_SNAKE_CASE :			int												=       encoder_attention_heads
       __SCREAMING_SNAKE_CASE :			Dict												=       decoder_ffn_dim
       __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       decoder_layers
       __SCREAMING_SNAKE_CASE :			Optional[Any]												=       decoder_attention_heads
       __SCREAMING_SNAKE_CASE :			Optional[Any]												=       dropout
       __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       attention_dropout
       __SCREAMING_SNAKE_CASE :			List[str]												=       activation_dropout
       __SCREAMING_SNAKE_CASE :			List[Any]												=       activation_function
       __SCREAMING_SNAKE_CASE :			Any												=       init_std
       __SCREAMING_SNAKE_CASE :			List[Any]												=       encoder_layerdrop
       __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       decoder_layerdrop
       __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       use_cache
       __SCREAMING_SNAKE_CASE :			List[str]												=       encoder_layers
       __SCREAMING_SNAKE_CASE :			str												=       scale_embedding  # scale factor will be sqrt(d_model) if True

       super().__init__(
           pad_token_id=_lowerCamelCase						,							bos_token_id=_lowerCamelCase						,							eos_token_id=_lowerCamelCase						,							is_encoder_decoder=_lowerCamelCase						,							decoder_start_token_id=_lowerCamelCase						,							**_lowerCamelCase						,							)







class 			snake_case		(   __UpperCAmelCase   ):

      @property
      def 						SCREAMING_SNAKE_CASE_			(			self   :int						):
       __SCREAMING_SNAKE_CASE :			str												=       OrderedDict(
           [
               ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
               ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
           ]						)

       if self.use_past:
        __SCREAMING_SNAKE_CASE :			Dict												=       {0: '''batch'''}
        __SCREAMING_SNAKE_CASE :			Any												=       {0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
       else:
        __SCREAMING_SNAKE_CASE :			int												=       {0: '''batch''', 1: '''decoder_sequence'''}
        __SCREAMING_SNAKE_CASE :			List[Any]												=       {0: '''batch''', 1: '''decoder_sequence'''}

       if self.use_past:
        self.fill_with_past_key_values_(_lowerCamelCase						,							direction='''inputs'''						)
       return common_inputs

      def 						SCREAMING_SNAKE_CASE_			(			self   :Tuple						,							_lowerCamelCase   :PreTrainedTokenizer						,							_lowerCamelCase   :int = -1						,							_lowerCamelCase   :int = -1						,							_lowerCamelCase   :bool = False						,							_lowerCamelCase   :Optional[TensorType] = None						,							):
       # Copied from OnnxConfig.generate_dummy_inputs
       # Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity.
       # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
       __SCREAMING_SNAKE_CASE :			List[Any]												=       compute_effective_axis_dimension(
           _lowerCamelCase						,							fixed_dimension=OnnxConfig.default_fixed_batch						,							num_token_to_add=0						)

       # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
       __SCREAMING_SNAKE_CASE :			List[str]												=       tokenizer.num_special_tokens_to_add(_lowerCamelCase						)
       __SCREAMING_SNAKE_CASE :			int												=       compute_effective_axis_dimension(
           _lowerCamelCase						,							fixed_dimension=OnnxConfig.default_fixed_sequence						,							num_token_to_add=_lowerCamelCase						)

       # Generate dummy inputs according to compute batch and sequence
       __SCREAMING_SNAKE_CASE :			int												=       [''' '''.join([tokenizer.unk_token]						) * seq_length] * batch_size
       __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       dict(tokenizer(_lowerCamelCase						,							return_tensors=_lowerCamelCase						)						)
       return common_inputs

      def 						SCREAMING_SNAKE_CASE_			(			self   :str						,							_lowerCamelCase   :PreTrainedTokenizer						,							_lowerCamelCase   :int = -1						,							_lowerCamelCase   :int = -1						,							_lowerCamelCase   :bool = False						,							_lowerCamelCase   :Optional[TensorType] = None						,							):
       __SCREAMING_SNAKE_CASE :			Dict												=       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
           _lowerCamelCase						,							_lowerCamelCase						,							_lowerCamelCase						,							_lowerCamelCase						,							_lowerCamelCase						)

       # Generate decoder inputs
       __SCREAMING_SNAKE_CASE :			Dict												=       seq_length if not self.use_past else 1
       __SCREAMING_SNAKE_CASE :			Any												=       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
           _lowerCamelCase						,							_lowerCamelCase						,							_lowerCamelCase						,							_lowerCamelCase						,							_lowerCamelCase						)
       __SCREAMING_SNAKE_CASE :			Optional[int]												=       {f'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
       __SCREAMING_SNAKE_CASE :			Optional[Any]												=       dict(**_lowerCamelCase						,							**_lowerCamelCase						)

       if self.use_past:
        if not is_torch_available():
         raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''						)
        else:
         import torch
        __SCREAMING_SNAKE_CASE							,      __SCREAMING_SNAKE_CASE :			int												=       common_inputs['''input_ids'''].shape
        __SCREAMING_SNAKE_CASE :			Tuple												=       common_inputs['''decoder_input_ids'''].shape[1]
        __SCREAMING_SNAKE_CASE							,      __SCREAMING_SNAKE_CASE :			Optional[Any]												=       self.num_attention_heads
        __SCREAMING_SNAKE_CASE :			List[str]												=       (
            batch,
            num_encoder_attention_heads,
            encoder_seq_length,
            self._config.hidden_size // num_encoder_attention_heads,
        )
        __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       decoder_seq_length + 3
        __SCREAMING_SNAKE_CASE :			List[str]												=       (
            batch,
            num_decoder_attention_heads,
            decoder_past_length,
            self._config.hidden_size // num_decoder_attention_heads,
        )

        __SCREAMING_SNAKE_CASE :			Any												=       torch.cat(
            [common_inputs['''decoder_attention_mask'''], torch.ones(_lowerCamelCase						,							_lowerCamelCase						)]						,							dim=1						)

        __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       []
        # If the number of encoder and decoder layers are present in the model configuration, both are considered
        __SCREAMING_SNAKE_CASE							,      __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       self.num_layers
        __SCREAMING_SNAKE_CASE :			Any												=       min(_lowerCamelCase						,							_lowerCamelCase						)
        __SCREAMING_SNAKE_CASE :			Tuple												=       max(_lowerCamelCase						,							_lowerCamelCase						) - min_num_layers
        __SCREAMING_SNAKE_CASE :			Optional[int]												=       '''encoder''' if num_encoder_layers > num_decoder_layers else '''decoder'''

        for _ in range(_lowerCamelCase						):
         common_inputs["past_key_values"].append(
             (
                 torch.zeros(_lowerCamelCase						),
                 torch.zeros(_lowerCamelCase						),
                 torch.zeros(_lowerCamelCase						),
                 torch.zeros(_lowerCamelCase						),
             )						)
        # TODO: test this.
        __SCREAMING_SNAKE_CASE :			Any												=       encoder_shape if remaining_side_name == '''encoder''' else decoder_shape
        for _ in range(_lowerCamelCase						,							_lowerCamelCase						):
         common_inputs["past_key_values"].append((torch.zeros(_lowerCamelCase						), torch.zeros(_lowerCamelCase						))						)
       return common_inputs



      lowerCamelCase__						= _generate_dummy_inputs_for_default_and_seqaseq_lm

