"""simple docstring"""






import copy
import unittest

from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

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


if is_torch_available():
       import torch

       from transformers import (
           MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
           MODEL_FOR_QUESTION_ANSWERING_MAPPING,
           MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
           MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
           LayoutLMvaConfig,
           LayoutLMvaForQuestionAnswering,
           LayoutLMvaForSequenceClassification,
           LayoutLMvaForTokenClassification,
           LayoutLMvaModel,
       )
       from transformers.models.layoutlmva.modeling_layoutlmva import LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
       from PIL import Image

       from transformers import LayoutLMvaImageProcessor





class 				_A		:

      def __init__(		self	, __lowerCAmelCase	, __lowerCAmelCase=2	, __lowerCAmelCase=3	, __lowerCAmelCase=4	, __lowerCAmelCase=2	, __lowerCAmelCase=7	, __lowerCAmelCase=True	, __lowerCAmelCase=True	, __lowerCAmelCase=True	, __lowerCAmelCase=True	, __lowerCAmelCase=99	, __lowerCAmelCase=36	, __lowerCAmelCase=3	, __lowerCAmelCase=4	, __lowerCAmelCase=37	, __lowerCAmelCase="gelu"	, __lowerCAmelCase=0.1	, __lowerCAmelCase=0.1	, __lowerCAmelCase=512	, __lowerCAmelCase=16	, __lowerCAmelCase=2	, __lowerCAmelCase=0.0_2	, __lowerCAmelCase=6	, __lowerCAmelCase=6	, __lowerCAmelCase=3	, __lowerCAmelCase=4	, __lowerCAmelCase=None	, __lowerCAmelCase=1000	, ):







            """simple docstring"""



            lowercase														=			parent
            lowercase														=			batch_size
            lowercase														=			num_channels
            lowercase														=			image_size
            lowercase														=			patch_size
            lowercase														=			text_seq_length
            lowercase														=			is_training
            lowercase														=			use_input_mask
            lowercase														=			use_token_type_ids
            lowercase														=			use_labels
            lowercase														=			vocab_size
            lowercase														=			hidden_size
            lowercase														=			num_hidden_layers
            lowercase														=			num_attention_heads
            lowercase														=			intermediate_size
            lowercase														=			hidden_act
            lowercase														=			hidden_dropout_prob
            lowercase														=			attention_probs_dropout_prob
            lowercase														=			max_position_embeddings
            lowercase														=			type_vocab_size
            lowercase														=			type_sequence_label_size
            lowercase														=			initializer_range
            lowercase														=			coordinate_size
            lowercase														=			shape_size
            lowercase														=			num_labels
            lowercase														=			num_choices
            lowercase														=			scope
            lowercase														=			range_bbox

            # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token)
            lowercase														=			text_seq_length
            lowercase														=			(image_size // patch_size) ** 2 + 1
            lowercase														=			self.text_seq_length + self.image_seq_length

      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			ids_tensor([self.batch_size, self.text_seq_length]	, self.vocab_size		)

            lowercase														=			ids_tensor([self.batch_size, self.text_seq_length, 4]	, self.range_bbox		)
            # Ensure that bbox is legal
            for i in range(bbox.shape[0]		):
                  for j in range(bbox.shape[1]		):
                        if bbox[i, j, 3] < bbox[i, j, 1]:
                              lowercase														=			bbox[i, j, 3]
                              lowercase														=			bbox[i, j, 1]
                              lowercase														=			t
                        if bbox[i, j, 2] < bbox[i, j, 0]:
                              lowercase														=			bbox[i, j, 2]
                              lowercase														=			bbox[i, j, 0]
                              lowercase														=			t

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

            lowercase														=			None
            if self.use_input_mask:
                  lowercase														=			random_attention_mask([self.batch_size, self.text_seq_length]		)

            lowercase														=			None
            if self.use_token_type_ids:
                  lowercase														=			ids_tensor([self.batch_size, self.text_seq_length]	, self.type_vocab_size		)

            lowercase														=			None
            lowercase														=			None
            if self.use_labels:
                  lowercase														=			ids_tensor([self.batch_size]	, self.type_sequence_label_size		)
                  lowercase														=			ids_tensor([self.batch_size, self.text_seq_length]	, self.num_labels		)

            lowercase														=			LayoutLMvaConfig(
                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	, coordinate_size=self.coordinate_size	, shape_size=self.shape_size	, input_size=self.image_size	, patch_size=self.patch_size	, )

            return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels

      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase		):







            """simple docstring"""



            lowercase														=			LayoutLMvaModel(config=lowerCamelCase_		)
            model.to(lowerCamelCase_		)
            model.eval()

            # text + image
            lowercase														=			model(lowerCamelCase_	, pixel_values=lowerCamelCase_		)
            lowercase														=			model(
                lowerCamelCase_	, bbox=lowerCamelCase_	, pixel_values=lowerCamelCase_	, attention_mask=lowerCamelCase_	, token_type_ids=lowerCamelCase_		)
            lowercase														=			model(lowerCamelCase_	, bbox=lowerCamelCase_	, pixel_values=lowerCamelCase_	, token_type_ids=lowerCamelCase_		)
            lowercase														=			model(lowerCamelCase_	, bbox=lowerCamelCase_	, pixel_values=lowerCamelCase_		)

            self.parent.assertEqual(result.last_hidden_state.shape	, (self.batch_size, self.seq_length, self.hidden_size)		)

            # text only
            lowercase														=			model(lowerCamelCase_		)
            self.parent.assertEqual(
                result.last_hidden_state.shape	, (self.batch_size, self.text_seq_length, self.hidden_size)		)

            # image only
            lowercase														=			model(pixel_values=lowerCamelCase_		)
            self.parent.assertEqual(
                result.last_hidden_state.shape	, (self.batch_size, self.image_seq_length, self.hidden_size)		)

      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase		):







            """simple docstring"""



            lowercase														=			self.num_labels
            lowercase														=			LayoutLMvaForSequenceClassification(lowerCamelCase_		)
            model.to(lowerCamelCase_		)
            model.eval()
            lowercase														=			model(
                lowerCamelCase_	, bbox=lowerCamelCase_	, pixel_values=lowerCamelCase_	, attention_mask=lowerCamelCase_	, token_type_ids=lowerCamelCase_	, labels=lowerCamelCase_	, )
            self.parent.assertEqual(result.logits.shape	, (self.batch_size, self.num_labels)		)

      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase		):







            """simple docstring"""



            lowercase														=			self.num_labels
            lowercase														=			LayoutLMvaForTokenClassification(config=lowerCamelCase_		)
            model.to(lowerCamelCase_		)
            model.eval()
            lowercase														=			model(
                lowerCamelCase_	, bbox=lowerCamelCase_	, pixel_values=lowerCamelCase_	, attention_mask=lowerCamelCase_	, token_type_ids=lowerCamelCase_	, labels=lowerCamelCase_	, )
            self.parent.assertEqual(result.logits.shape	, (self.batch_size, self.text_seq_length, self.num_labels)		)

      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase		):







            """simple docstring"""



            lowercase														=			LayoutLMvaForQuestionAnswering(config=lowerCamelCase_		)
            model.to(lowerCamelCase_		)
            model.eval()
            lowercase														=			model(
                lowerCamelCase_	, bbox=lowerCamelCase_	, pixel_values=lowerCamelCase_	, attention_mask=lowerCamelCase_	, token_type_ids=lowerCamelCase_	, start_positions=lowerCamelCase_	, end_positions=lowerCamelCase_	, )
            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        A__ (		self		):







            """simple docstring"""



            lowercase														=			self.prepare_config_and_inputs()
            (
                lowercase					
            )														=			config_and_inputs
            lowercase														=			{
                '''input_ids''': input_ids,
                '''bbox''': bbox,
                '''pixel_values''': pixel_values,
                '''token_type_ids''': token_type_ids,
                '''attention_mask''': input_mask,
            }
            return config, inputs_dict






@require_torch
class 				_A		( _UpperCAmelCase						,					_UpperCAmelCase						,					unittest.TestCase			):
      snake_case__    :     Optional[int]													=			False
      snake_case__    :     Optional[int]													=			False
      snake_case__    :     Optional[int]													=			False

      snake_case__    :     str													=			(
          (
              LayoutLMvaModel,
              LayoutLMvaForSequenceClassification,
              LayoutLMvaForTokenClassification,
              LayoutLMvaForQuestionAnswering,
          )
          if is_torch_available()
          else ()
      )
      snake_case__    :     List[str]													=			(
          {'document-question-answering': LayoutLMvaForQuestionAnswering, 'feature-extraction': LayoutLMvaModel}
          if is_torch_available()
          else {}
      )

      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase		):







            """simple docstring"""



            return True

      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			LayoutLMvaModelTester(self		)
            lowercase														=			ConfigTester(self	, config_class=lowerCamelCase_	, hidden_size=37		)

      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase=False		):







            """simple docstring"""



            lowercase														=			copy.deepcopy(lowerCamelCase_		)
            if model_class in get_values(lowerCamelCase_		):
                  lowercase														=			{
                      k: v.unsqueeze(1		).expand(-1	, self.model_tester.num_choices	, -1		).contiguous()
                      if isinstance(lowerCamelCase_	, torch.Tensor		) and v.ndim > 1
                      else v
                      for k, v in inputs_dict.items()
                  }
            if return_labels:
                  if model_class in get_values(lowerCamelCase_		):
                        lowercase														=			torch.ones(self.model_tester.batch_size	, dtype=torch.long	, device=lowerCamelCase_		)
                  elif model_class in get_values(lowerCamelCase_		):
                        lowercase														=			torch.zeros(
                            self.model_tester.batch_size	, dtype=torch.long	, device=lowerCamelCase_		)
                        lowercase														=			torch.zeros(
                            self.model_tester.batch_size	, dtype=torch.long	, device=lowerCamelCase_		)
                  elif model_class in [
                      *get_values(lowerCamelCase_		),
                  ]:
                        lowercase														=			torch.zeros(
                            self.model_tester.batch_size	, dtype=torch.long	, device=lowerCamelCase_		)
                  elif model_class in [
                      *get_values(lowerCamelCase_		),
                  ]:
                        lowercase														=			torch.zeros(
                            (self.model_tester.batch_size, self.model_tester.text_seq_length)	, dtype=torch.long	, device=lowerCamelCase_	, )

            return inputs_dict

      def        A__ (		self		):







            """simple docstring"""



            self.config_tester.run_common_tests()

      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*lowerCamelCase_		)

      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			self.model_tester.prepare_config_and_inputs()
            for type in ["absolute", "relative_key", "relative_key_query"]:
                  lowercase														=			type
                  self.model_tester.create_and_check_model(*lowerCamelCase_		)

      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase_		)

      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_token_classification(*lowerCamelCase_		)

      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_question_answering(*lowerCamelCase_		)







      @slow
      def        A__ (		self		):







            """simple docstring"""



            for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  lowercase														=			LayoutLMvaModel.from_pretrained(lowerCamelCase_		)
                  self.assertIsNotNone(lowerCamelCase_		)






def     UpperCAmelCase__ (       )				->					Optional[int]:
      '''simple docstring'''


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






@require_torch
class 				_A		( unittest.TestCase			):

      @cached_property
      def        A__ (		self		):







            """simple docstring"""



            return LayoutLMvaImageProcessor(apply_ocr=lowerCamelCase_		) if is_vision_available() else None







      @slow
      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			LayoutLMvaModel.from_pretrained("""microsoft/layoutlmv3-base"""		).to(lowerCamelCase_		)

            lowercase														=			self.default_image_processor
            lowercase														=			prepare_img()
            lowercase														=			image_processor(images=lowerCamelCase_	, return_tensors="""pt"""		).pixel_values.to(lowerCamelCase_		)

            lowercase														=			torch.tensor([[1, 2]]		)
            lowercase														=			torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]]		).unsqueeze(0		)

            # forward pass
            lowercase														=			model(
                input_ids=input_ids.to(lowerCamelCase_		)	, bbox=bbox.to(lowerCamelCase_		)	, pixel_values=pixel_values.to(lowerCamelCase_		)	, )

            # verify the logits
            lowercase														=			torch.Size((1, 199, 768)		)
            self.assertEqual(outputs.last_hidden_state.shape	, lowerCamelCase_		)

            lowercase														=			torch.tensor(
                [[-0.0_5_2_9, 0.3_6_1_8, 0.1_6_3_2], [-0.1_5_8_7, -0.1_6_6_7, -0.0_4_0_0], [-0.1_5_5_7, -0.1_6_7_1, -0.0_5_0_5]]		).to(lowerCamelCase_		)

            self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3]	, lowerCamelCase_	, atol=1E-4		)		)






"""simple docstring"""






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

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


__lowerCAmelCase					: Tuple        =logging.get_logger(__name__)

__lowerCAmelCase					: Tuple        ={
    """facebook/blenderbot_small-90M""": """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/config.json""",
    # See all BlenderbotSmall models at https://huggingface.co/models?filter=blenderbot_small
}





class 				_A		( lowerCAmelCase			):
      snake_case__    :     int													=			'blenderbot-small'
      snake_case__    :     Optional[Any]													=			['past_key_values']
      snake_case__    :     Optional[Any]													=			{'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}

      def __init__(		self	, __lowerCAmelCase=5_0265	, __lowerCAmelCase=512	, __lowerCAmelCase=8	, __lowerCAmelCase=2048	, __lowerCAmelCase=16	, __lowerCAmelCase=8	, __lowerCAmelCase=2048	, __lowerCAmelCase=16	, __lowerCAmelCase=0.0	, __lowerCAmelCase=0.0	, __lowerCAmelCase=True	, __lowerCAmelCase=True	, __lowerCAmelCase="gelu"	, __lowerCAmelCase=512	, __lowerCAmelCase=0.1	, __lowerCAmelCase=0.0	, __lowerCAmelCase=0.0	, __lowerCAmelCase=0.0_2	, __lowerCAmelCase=1	, __lowerCAmelCase=False	, __lowerCAmelCase=0	, __lowerCAmelCase=1	, __lowerCAmelCase=2	, __lowerCAmelCase=2	, **__lowerCAmelCase	, ):







            """simple docstring"""



            lowercase														=			vocab_size
            lowercase														=			max_position_embeddings
            lowercase														=			d_model
            lowercase														=			encoder_ffn_dim
            lowercase														=			encoder_layers
            lowercase														=			encoder_attention_heads
            lowercase														=			decoder_ffn_dim
            lowercase														=			decoder_layers
            lowercase														=			decoder_attention_heads
            lowercase														=			dropout
            lowercase														=			attention_dropout
            lowercase														=			activation_dropout
            lowercase														=			activation_function
            lowercase														=			init_std
            lowercase														=			encoder_layerdrop
            lowercase														=			decoder_layerdrop
            lowercase														=			use_cache
            lowercase														=			encoder_layers
            lowercase														=			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	, forced_eos_token_id=__lowerCAmelCase	, **__lowerCAmelCase	, )






class 				_A		( lowerCAmelCase			):

      @property
      def        A__ (		self		):







            """simple docstring"""



            if self.task in ["default", "seq2seq-lm"]:
                  lowercase														=			OrderedDict(
                      [
                          ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
                          ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
                      ]		)

                  if self.use_past:
                        lowercase														=			{0: """batch"""}
                        lowercase														=			{0: """batch""", 1: """past_decoder_sequence + sequence"""}
                  else:
                        lowercase														=			{0: """batch""", 1: """decoder_sequence"""}
                        lowercase														=			{0: """batch""", 1: """decoder_sequence"""}

                  if self.use_past:
                        self.fill_with_past_key_values_(__lowerCAmelCase	, direction="""inputs"""		)
            elif self.task == "causal-lm":
                  # TODO: figure this case out.
                  lowercase														=			OrderedDict(
                      [
                          ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
                          ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
                      ]		)
                  if self.use_past:
                        lowercase							,							lowercase														=			self.num_layers
                        for i in range(__lowerCAmelCase		):
                              lowercase														=			{0: """batch""", 2: """past_sequence + sequence"""}
                              lowercase														=			{0: """batch""", 2: """past_sequence + sequence"""}
            else:
                  lowercase														=			OrderedDict(
                      [
                          ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
                          ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
                          ("""decoder_input_ids""", {0: """batch""", 1: """decoder_sequence"""}),
                          ("""decoder_attention_mask""", {0: """batch""", 1: """decoder_sequence"""}),
                      ]		)

            return common_inputs

      @property
      def        A__ (		self		):







            """simple docstring"""



            if self.task in ["default", "seq2seq-lm"]:
                  lowercase														=			super().outputs
            else:
                  lowercase														=			super(__lowerCAmelCase	, self		).outputs
                  if self.use_past:
                        lowercase							,							lowercase														=			self.num_layers
                        for i in range(__lowerCAmelCase		):
                              lowercase														=			{0: """batch""", 2: """past_sequence + sequence"""}
                              lowercase														=			{0: """batch""", 2: """past_sequence + sequence"""}
            return common_outputs

      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase = -1	, __lowerCAmelCase = -1	, __lowerCAmelCase = False	, __lowerCAmelCase = None	, ):







            """simple docstring"""



            lowercase														=			self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase		)

            # Generate decoder inputs
            lowercase														=			seq_length if not self.use_past else 1
            lowercase														=			self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase		)
            lowercase														=			{f'decoder_{name}': tensor for name, tensor in decoder_inputs.items()}
            lowercase														=			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
                  lowercase							,							lowercase														=			common_inputs["""input_ids"""].shape
                  lowercase														=			common_inputs["""decoder_input_ids"""].shape[1]
                  lowercase							,							lowercase														=			self.num_attention_heads
                  lowercase														=			(
                      batch,
                      num_encoder_attention_heads,
                      encoder_seq_length,
                      self._config.hidden_size // num_encoder_attention_heads,
                  )
                  lowercase														=			decoder_seq_length + 3
                  lowercase														=			(
                      batch,
                      num_decoder_attention_heads,
                      decoder_past_length,
                      self._config.hidden_size // num_decoder_attention_heads,
                  )

                  lowercase														=			torch.cat(
                      [common_inputs["""decoder_attention_mask"""], torch.ones(__lowerCAmelCase	, __lowerCAmelCase		)]	, dim=1		)

                  lowercase														=			[]
                  # If the number of encoder and decoder layers are present in the model configuration, both are considered
                  lowercase							,							lowercase														=			self.num_layers
                  lowercase														=			min(__lowerCAmelCase	, __lowerCAmelCase		)
                  lowercase														=			max(__lowerCAmelCase	, __lowerCAmelCase		) - min_num_layers
                  lowercase														=			"""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.
                  lowercase														=			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

      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase = -1	, __lowerCAmelCase = -1	, __lowerCAmelCase = False	, __lowerCAmelCase = None	, ):







            """simple docstring"""



            lowercase														=			self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __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
                  lowercase							,							lowercase														=			common_inputs["""input_ids"""].shape
                  # Not using the same length for past_key_values
                  lowercase														=			seqlen + 2
                  lowercase							,							lowercase														=			self.num_layers
                  lowercase							,							lowercase														=			self.num_attention_heads
                  lowercase														=			(
                      batch,
                      num_encoder_attention_heads,
                      past_key_values_length,
                      self._config.hidden_size // num_encoder_attention_heads,
                  )

                  lowercase														=			common_inputs["""attention_mask"""].dtype
                  lowercase														=			torch.cat(
                      [common_inputs["""attention_mask"""], torch.ones(__lowerCAmelCase	, __lowerCAmelCase	, dtype=__lowerCAmelCase		)]	, dim=1		)
                  lowercase														=			[
                      (torch.zeros(__lowerCAmelCase		), torch.zeros(__lowerCAmelCase		)) for _ in range(__lowerCAmelCase		)
                  ]
            return common_inputs

      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase = -1	, __lowerCAmelCase = -1	, __lowerCAmelCase = False	, __lowerCAmelCase = None	, ):







            """simple docstring"""



            lowercase														=			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
            lowercase														=			tokenizer.num_special_tokens_to_add(__lowerCAmelCase		)
            lowercase														=			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
            lowercase														=			[""" """.join([tokenizer.unk_token]		) * seq_length] * batch_size
            lowercase														=			dict(tokenizer(__lowerCAmelCase	, return_tensors=__lowerCAmelCase		)		)
            return common_inputs

      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase = -1	, __lowerCAmelCase = -1	, __lowerCAmelCase = False	, __lowerCAmelCase = None	, ):







            """simple docstring"""



            if self.task in ["default", "seq2seq-lm"]:
                  lowercase														=			self._generate_dummy_inputs_for_default_and_seqaseq_lm(
                      __lowerCAmelCase	, batch_size=__lowerCAmelCase	, seq_length=__lowerCAmelCase	, is_pair=__lowerCAmelCase	, framework=__lowerCAmelCase		)

            elif self.task == "causal-lm":
                  lowercase														=			self._generate_dummy_inputs_for_causal_lm(
                      __lowerCAmelCase	, batch_size=__lowerCAmelCase	, seq_length=__lowerCAmelCase	, is_pair=__lowerCAmelCase	, framework=__lowerCAmelCase		)
            else:
                  lowercase														=			self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                      __lowerCAmelCase	, batch_size=__lowerCAmelCase	, seq_length=__lowerCAmelCase	, is_pair=__lowerCAmelCase	, framework=__lowerCAmelCase		)

            return common_inputs







      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase		):







            """simple docstring"""



            if self.task in ["default", "seq2seq-lm"]:
                  lowercase														=			super()._flatten_past_key_values_(__lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase		)
            else:
                  lowercase														=			super(__lowerCAmelCase	, self		)._flatten_past_key_values_(
                      __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase		)



