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


UpperCamelCase_						   =   logging.get_logger(__name__)

UpperCamelCase_						   =   {
    'facebook/blenderbot-3B': 'https://huggingface.co/facebook/blenderbot-3B/resolve/main/config.json',
    # See all Blenderbot models at https://huggingface.co/models?filter=blenderbot
}





class 							snake_case       (     SCREAMING_SNAKE_CASE_		):
   a_					:		Optional[Any]     					=     """blenderbot"""
   a_					:		List[Any]     					=     ["""past_key_values"""]
   a_					:		Dict     					=     {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}







   def __init__( self       ,							__UpperCAmelCase=80_08       ,							__UpperCAmelCase=1_28       ,							__UpperCAmelCase=2       ,							__UpperCAmelCase=1_02_40       ,							__UpperCAmelCase=32       ,							__UpperCAmelCase=24       ,							__UpperCAmelCase=1_02_40       ,							__UpperCAmelCase=32       ,							__UpperCAmelCase=0.0       ,							__UpperCAmelCase=0.0       ,							__UpperCAmelCase=True       ,							__UpperCAmelCase=True       ,							__UpperCAmelCase="gelu"       ,							__UpperCAmelCase=25_60       ,							__UpperCAmelCase=0.1       ,							__UpperCAmelCase=0.0       ,							__UpperCAmelCase=0.0       ,							__UpperCAmelCase=0.02       ,							__UpperCAmelCase=1       ,							__UpperCAmelCase=False       ,							__UpperCAmelCase=0       ,							__UpperCAmelCase=1       ,							__UpperCAmelCase=2       ,							__UpperCAmelCase=3       ,							__UpperCAmelCase=2       ,							**__UpperCAmelCase       ,							)   ->Optional[int]:
      a_						   =     vocab_size
      a_						   =     max_position_embeddings
      a_						   =     d_model
      a_						   =     encoder_ffn_dim
      a_						   =     encoder_layers
      a_						   =     encoder_attention_heads
      a_						   =     decoder_ffn_dim
      a_						   =     decoder_layers
      a_						   =     decoder_attention_heads
      a_						   =     dropout
      a_						   =     attention_dropout
      a_						   =     activation_dropout
      a_						   =     activation_function
      a_						   =     init_std
      a_						   =     encoder_layerdrop
      a_						   =     decoder_layerdrop
      a_						   =     use_cache
      a_						   =     encoder_layers
      a_						   =     scale_embedding  # scale factor will be sqrt(d_model) if True

      super().__init__(
          pad_token_id=__UpperCAmelCase       ,							bos_token_id=__UpperCAmelCase       ,							eos_token_id=__UpperCAmelCase       ,							is_encoder_decoder=__UpperCAmelCase       ,							decoder_start_token_id=__UpperCAmelCase       ,							encoder_no_repeat_ngram_size=__UpperCAmelCase       ,							forced_eos_token_id=__UpperCAmelCase       ,							**__UpperCAmelCase       ,							)






class 							snake_case       (     SCREAMING_SNAKE_CASE_		):







   @property
   def    UpperCAmelCase__	( self)   ->Mapping[str, Mapping[int, str]]:
      if self.task in ["default", "seq2seq-lm"]:
         a_						   =     OrderedDict(
             [
                 ("input_ids", {0: "batch", 1: "encoder_sequence"}),
                 ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
             ])
         if self.use_past:
            a_						   =     {0: "batch"}
            a_						   =     {0: "batch", 1: "past_decoder_sequence + sequence"}
         else:
            a_						   =     {0: "batch", 1: "decoder_sequence"}
            a_						   =     {0: "batch", 1: "decoder_sequence"}
         if self.use_past:
            self.fill_with_past_key_values_(__UpperCAmelCase       ,							direction="inputs")
      elif self.task == "causal-lm":
         a_						   =     OrderedDict(
             [
                 ("input_ids", {0: "batch", 1: "encoder_sequence"}),
                 ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
             ])
         if self.use_past:
            a_   ,			a_						   =     self.num_layers
            for i in range(__UpperCAmelCase):
               a_						   =     {0: "batch", 2: "past_sequence + sequence"}
               a_						   =     {0: "batch", 2: "past_sequence + sequence"}
      else:
         a_						   =     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
   # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs
   def    UpperCAmelCase__	( self)   ->Mapping[str, Mapping[int, str]]:
      if self.task in ["default", "seq2seq-lm"]:
         a_						   =     super().outputs
      else:
         a_						   =     super(__UpperCAmelCase       ,							self).outputs
         if self.use_past:
            a_   ,			a_						   =     self.num_layers
            for i in range(__UpperCAmelCase):
               a_						   =     {0: "batch", 2: "past_sequence + sequence"}
               a_						   =     {0: "batch", 2: "past_sequence + sequence"}
      return common_outputs







   def    UpperCAmelCase__	( self       ,							__UpperCAmelCase       ,							__UpperCAmelCase = -1       ,							__UpperCAmelCase = -1       ,							__UpperCAmelCase = False       ,							__UpperCAmelCase = None       ,							)   ->Mapping[str, Any]:
      a_						   =     self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
          __UpperCAmelCase       ,							__UpperCAmelCase       ,							__UpperCAmelCase       ,							__UpperCAmelCase       ,							__UpperCAmelCase)
      # Generate decoder inputs
      a_						   =     seq_length if not self.use_past else 1
      a_						   =     self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
          __UpperCAmelCase       ,							__UpperCAmelCase       ,							__UpperCAmelCase       ,							__UpperCAmelCase       ,							__UpperCAmelCase)
      a_						   =     {F'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
      a_						   =     dict(**__UpperCAmelCase       ,							**__UpperCAmelCase)

      if self.use_past:
         if not is_torch_available():
            raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed.")
         else:
            import torch
         a_   ,			a_						   =     common_inputs["input_ids"].shape
         a_						   =     common_inputs["decoder_input_ids"].shape[1]
         a_   ,			a_						   =     self.num_attention_heads
         a_						   =     (
             batch,
             num_encoder_attention_heads,
             encoder_seq_length,
             self._config.hidden_size // num_encoder_attention_heads,
         )
         a_						   =     decoder_seq_length
         a_						   =     (
             batch,
             num_decoder_attention_heads,
             decoder_past_length,
             self._config.hidden_size // num_decoder_attention_heads,
         )
         a_						   =     torch.cat(
             [common_inputs["decoder_attention_mask"], torch.ones(__UpperCAmelCase       ,							__UpperCAmelCase)]       ,							dim=1)
         a_						   =     []
         a_   ,			a_						   =     self.num_layers

         for _ in range(__UpperCAmelCase):
            common_inputs["past_key_values"].append(
                (
                    torch.zeros(__UpperCAmelCase),
                    torch.zeros(__UpperCAmelCase),
                    torch.zeros(__UpperCAmelCase),
                    torch.zeros(__UpperCAmelCase),
                ))
      return common_inputs







   def    UpperCAmelCase__	( self       ,							__UpperCAmelCase       ,							__UpperCAmelCase = -1       ,							__UpperCAmelCase = -1       ,							__UpperCAmelCase = False       ,							__UpperCAmelCase = None       ,							)   ->Mapping[str, Any]:
      a_						   =     self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
          __UpperCAmelCase       ,							__UpperCAmelCase       ,							__UpperCAmelCase       ,							__UpperCAmelCase       ,							__UpperCAmelCase)

      if self.use_past:
         if not is_torch_available():
            raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed.")
         else:
            import torch
         a_   ,			a_						   =     common_inputs["input_ids"].shape
         a_						   =     seqlen
         a_   ,			a_						   =     self.num_layers
         a_   ,			a_						   =     self.num_attention_heads
         a_						   =     (
             batch,
             num_encoder_attention_heads,
             past_key_values_length,
             self._config.hidden_size // num_encoder_attention_heads,
         )
         a_						   =     common_inputs["attention_mask"].dtype
         a_						   =     torch.cat(
             [common_inputs["attention_mask"], torch.ones(__UpperCAmelCase       ,							__UpperCAmelCase       ,							dtype=__UpperCAmelCase)]       ,							dim=1)
         a_						   =     [
             (torch.zeros(__UpperCAmelCase), torch.zeros(__UpperCAmelCase)) for _ in range(__UpperCAmelCase)
         ]
      return common_inputs







   def    UpperCAmelCase__	( self       ,							__UpperCAmelCase       ,							__UpperCAmelCase = -1       ,							__UpperCAmelCase = -1       ,							__UpperCAmelCase = False       ,							__UpperCAmelCase = None       ,							)   ->Mapping[str, Any]:
      # 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
      a_						   =     compute_effective_axis_dimension(
          __UpperCAmelCase       ,							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
      a_						   =     tokenizer.num_special_tokens_to_add(__UpperCAmelCase)
      a_						   =     compute_effective_axis_dimension(
          __UpperCAmelCase       ,							fixed_dimension=OnnxConfig.default_fixed_sequence       ,							num_token_to_add=__UpperCAmelCase)

      # Generate dummy inputs according to compute batch and sequence
      a_						   =     [" ".join([tokenizer.unk_token]) * seq_length] * batch_size
      a_						   =     dict(tokenizer(__UpperCAmelCase       ,							return_tensors=__UpperCAmelCase))
      return common_inputs







   def    UpperCAmelCase__	( self       ,							__UpperCAmelCase       ,							__UpperCAmelCase = -1       ,							__UpperCAmelCase = -1       ,							__UpperCAmelCase = False       ,							__UpperCAmelCase = None       ,							)   ->Mapping[str, Any]:
      if self.task in ["default", "seq2seq-lm"]:
         a_						   =     self._generate_dummy_inputs_for_default_and_seqaseq_lm(
             __UpperCAmelCase       ,							batch_size=__UpperCAmelCase       ,							seq_length=__UpperCAmelCase       ,							is_pair=__UpperCAmelCase       ,							framework=__UpperCAmelCase)

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

      return common_inputs







   def    UpperCAmelCase__	( self       ,							__UpperCAmelCase       ,							__UpperCAmelCase       ,							__UpperCAmelCase       ,							__UpperCAmelCase)   ->List[str]:
      if self.task in ["default", "seq2seq-lm"]:
         a_						   =     super()._flatten_past_key_values_(__UpperCAmelCase       ,							__UpperCAmelCase       ,							__UpperCAmelCase       ,							__UpperCAmelCase)
      else:
         a_						   =     super(__UpperCAmelCase       ,							self)._flatten_past_key_values_(
             __UpperCAmelCase       ,							__UpperCAmelCase       ,							__UpperCAmelCase       ,							__UpperCAmelCase)







   def    UpperCAmelCase__	( self       ,							__UpperCAmelCase       ,							__UpperCAmelCase)   ->List[str]:
      if direction not in ["inputs", "outputs"]:
         raise ValueError(F'''direction must either be "inputs" or "outputs", but {direction} was given''')

      a_						   =     "past_key_values" if direction == "inputs" else "present"
      a_   ,			a_						   =     self.num_layers

      a_						   =     "past_encoder_sequence"
      a_						   =     "past_decoder_sequence" if direction == "inputs" else "past_decoder_sequence + sequence"

      for i in range(__UpperCAmelCase):
         a_						   =     {0: "batch", 2: decoder_sequence}
         a_						   =     {0: "batch", 2: decoder_sequence}
         a_						   =     {0: "batch", 2: encoder_sequence}
         a_						   =     {0: "batch", 2: encoder_sequence}

"""simple docstring"""






import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
      import torch

if is_vision_available():
      from PIL import Image

      from transformers import TvltImageProcessor
def    UpperCamelCase						(					UpperCAmelCase ,   UpperCAmelCase=10 ,   UpperCAmelCase=10 ,   UpperCAmelCase=False ,   UpperCAmelCase=False     )						->Union[str, Any]:

   """simple docstring"""



   a_						   =     []
   for i in range(image_processor_tester.num_frames     ):
      video.append(np.random.randint(255 ,   size=(image_processor_tester.num_channels, width, height) ,   dtype=np.uinta     )     )

   if not numpify and not torchify:
      # PIL expects the channel dimension as last dimension
      a_						   =     [Image.fromarray(np.moveaxis(UpperCAmelCase ,   0 ,   -1     )     ) for frame in video]

   if torchify:
      a_						   =     [torch.from_numpy(UpperCAmelCase     ) for frame in video]

   return video
def    UpperCamelCase						(					UpperCAmelCase ,   UpperCAmelCase=False ,   UpperCAmelCase=False ,   UpperCAmelCase=False     )						->Optional[Any]:

   """simple docstring"""



   assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

   a_						   =     []
   for i in range(image_processor_tester.batch_size     ):
      if equal_resolution:
         a_						   =     a_						   =     image_processor_tester.max_resolution
      else:
         a_   ,			a_						   =     np.random.choice(
             np.arange(image_processor_tester.min_resolution ,   image_processor_tester.max_resolution     ) ,   2     )
         a_						   =     prepare_video(
             image_processor_tester=UpperCAmelCase ,   width=UpperCAmelCase ,   height=UpperCAmelCase ,   numpify=UpperCAmelCase ,   torchify=UpperCAmelCase ,   )
      video_inputs.append(UpperCAmelCase     )

   return video_inputs





class 							snake_case       (     unittest.TestCase		):







   def __init__( self       ,							__UpperCAmelCase       ,							__UpperCAmelCase=7       ,							__UpperCAmelCase=3       ,							__UpperCAmelCase=4       ,							__UpperCAmelCase=18       ,							__UpperCAmelCase=30       ,							__UpperCAmelCase=4_00       ,							__UpperCAmelCase=True       ,							__UpperCAmelCase=None       ,							__UpperCAmelCase=True       ,							__UpperCAmelCase=[0.5, 0.5, 0.5]       ,							__UpperCAmelCase=[0.5, 0.5, 0.5]       ,							__UpperCAmelCase=True       ,							__UpperCAmelCase=None       ,							)   ->str:
      a_						   =     size if size is not None else {"shortest_edge": 18}
      a_						   =     crop_size if crop_size is not None else {"height": 18, "width": 18}

      a_						   =     parent
      a_						   =     batch_size
      a_						   =     num_channels
      a_						   =     num_frames
      a_						   =     image_size
      a_						   =     min_resolution
      a_						   =     max_resolution
      a_						   =     do_resize
      a_						   =     size
      a_						   =     do_normalize
      a_						   =     image_mean
      a_						   =     image_std
      a_						   =     do_center_crop
      a_						   =     crop_size







   def    UpperCAmelCase__	( self)   ->Tuple:
      return {
          "image_mean": self.image_mean,
          "image_std": self.image_std,
          "do_normalize": self.do_normalize,
          "do_resize": self.do_resize,
          "size": self.size,
          "do_center_crop": self.do_center_crop,
          "crop_size": self.crop_size,
      }






@require_torch
@require_vision
class 							snake_case       (     SCREAMING_SNAKE_CASE_							,       unittest.TestCase		):
   a_					:		int     					=     TvltImageProcessor if is_vision_available() else None







   def    UpperCAmelCase__	( self)   ->Optional[Any]:
      a_						   =     TvltImageProcessorTester(self)







   @property
   def    UpperCAmelCase__	( self)   ->int:
      return self.image_processor_tester.prepare_image_processor_dict()







   def    UpperCAmelCase__	( self)   ->int:
      a_						   =     self.image_processing_class(**self.image_processor_dict)
      self.assertTrue(hasattr(__UpperCAmelCase       ,							"image_mean"))
      self.assertTrue(hasattr(__UpperCAmelCase       ,							"image_std"))
      self.assertTrue(hasattr(__UpperCAmelCase       ,							"do_normalize"))
      self.assertTrue(hasattr(__UpperCAmelCase       ,							"do_resize"))
      self.assertTrue(hasattr(__UpperCAmelCase       ,							"do_center_crop"))
      self.assertTrue(hasattr(__UpperCAmelCase       ,							"size"))







   def    UpperCAmelCase__	( self)   ->Any:
      # Initialize image_processor
      a_						   =     self.image_processing_class(**self.image_processor_dict)
      # create random PIL videos
      a_						   =     prepare_video_inputs(self.image_processor_tester       ,							equal_resolution=__UpperCAmelCase)
      for video in video_inputs:
         self.assertIsInstance(__UpperCAmelCase       ,							__UpperCAmelCase)
         self.assertIsInstance(video[0]       ,							Image.Image)

      # Test not batched input
      a_						   =     image_processor(video_inputs[0]       ,							return_tensors="pt").pixel_values
      self.assertEqual(
          encoded_videos.shape       ,							(
              1,
              self.image_processor_tester.num_frames,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size["height"],
              self.image_processor_tester.crop_size["width"],
          )       ,							)

      # Test batched
      a_						   =     image_processor(__UpperCAmelCase       ,							return_tensors="pt").pixel_values
      self.assertEqual(
          encoded_videos.shape       ,							(
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_frames,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size["height"],
              self.image_processor_tester.crop_size["width"],
          )       ,							)







   def    UpperCAmelCase__	( self)   ->int:
      # Initialize image_processor
      a_						   =     self.image_processing_class(**self.image_processor_dict)
      # create random numpy tensors
      a_						   =     prepare_video_inputs(self.image_processor_tester       ,							equal_resolution=__UpperCAmelCase       ,							numpify=__UpperCAmelCase)
      for video in video_inputs:
         self.assertIsInstance(__UpperCAmelCase       ,							__UpperCAmelCase)
         self.assertIsInstance(video[0]       ,							np.ndarray)

      # Test not batched input
      a_						   =     image_processor(video_inputs[0]       ,							return_tensors="pt").pixel_values
      self.assertEqual(
          encoded_videos.shape       ,							(
              1,
              self.image_processor_tester.num_frames,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size["height"],
              self.image_processor_tester.crop_size["width"],
          )       ,							)

      # Test batched
      a_						   =     image_processor(__UpperCAmelCase       ,							return_tensors="pt").pixel_values
      self.assertEqual(
          encoded_videos.shape       ,							(
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_frames,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size["height"],
              self.image_processor_tester.crop_size["width"],
          )       ,							)







   def    UpperCAmelCase__	( self)   ->Tuple:
      # Initialize image_processor
      a_						   =     self.image_processing_class(**self.image_processor_dict)
      # create random PyTorch tensors
      a_						   =     prepare_video_inputs(self.image_processor_tester       ,							equal_resolution=__UpperCAmelCase       ,							torchify=__UpperCAmelCase)
      for video in video_inputs:
         self.assertIsInstance(__UpperCAmelCase       ,							__UpperCAmelCase)
         self.assertIsInstance(video[0]       ,							torch.Tensor)

      # Test not batched input
      a_						   =     image_processor(video_inputs[0]       ,							return_tensors="pt").pixel_values
      self.assertEqual(
          encoded_videos.shape       ,							(
              1,
              self.image_processor_tester.num_frames,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size["height"],
              self.image_processor_tester.crop_size["width"],
          )       ,							)

      # Test batched
      a_						   =     image_processor(__UpperCAmelCase       ,							return_tensors="pt").pixel_values
      self.assertEqual(
          encoded_videos.shape       ,							(
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_frames,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size["height"],
              self.image_processor_tester.crop_size["width"],
          )       ,							)