



"""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:					Dict     				=	logging.get_logger(__name__)

_UpperCamelCase:					Dict     				=	{
    '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 	a__		(    SCREAMING_SNAKE_CASE__  ):
   _lowerCamelCase       				=					'blenderbot'
   _lowerCamelCase       				=					['past_key_values']
   _lowerCamelCase       				=					{'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}


   def __init__(				self   :				str,  lowerCAmelCase   :				int=8008,  lowerCAmelCase   :				Union[str, Any]=128,  lowerCAmelCase   :				Dict=2,  lowerCAmelCase   :				Optional[int]=10240,  lowerCAmelCase   :				Tuple=32,  lowerCAmelCase   :				Any=24,  lowerCAmelCase   :				str=10240,  lowerCAmelCase   :				List[str]=32,  lowerCAmelCase   :				Any=0.0,  lowerCAmelCase   :				Union[str, Any]=0.0,  lowerCAmelCase   :				Tuple=True,  lowerCAmelCase   :				str=True,  lowerCAmelCase   :				str="gelu",  lowerCAmelCase   :				int=2560,  lowerCAmelCase   :				Optional[Any]=0.1,  lowerCAmelCase   :				Optional[int]=0.0,  lowerCAmelCase   :				List[Any]=0.0,  lowerCAmelCase   :				List[Any]=0.02,  lowerCAmelCase   :				Dict=1,  lowerCAmelCase   :				int=False,  lowerCAmelCase   :				Optional[int]=0,  lowerCAmelCase   :				Union[str, Any]=1,  lowerCAmelCase   :				Dict=2,  lowerCAmelCase   :				str=3,  lowerCAmelCase   :				Tuple=2,  **lowerCAmelCase   :				List[Any],  )     ->							List[str]:
         lowercase	:   Dict											= vocab_size
         lowercase	:   List[Any]											= max_position_embeddings
         lowercase	:   Tuple											= d_model
         lowercase	:   Any											= encoder_ffn_dim
         lowercase	:   Dict											= encoder_layers
         lowercase	:   Union[str, Any]											= encoder_attention_heads
         lowercase	:   Optional[int]											= decoder_ffn_dim
         lowercase	:   Union[str, Any]											= decoder_layers
         lowercase	:   List[str]											= decoder_attention_heads
         lowercase	:   Tuple											= dropout
         lowercase	:   Optional[int]											= attention_dropout
         lowercase	:   Union[str, Any]											= activation_dropout
         lowercase	:   Optional[int]											= activation_function
         lowercase	:   int											= init_std
         lowercase	:   List[str]											= encoder_layerdrop
         lowercase	:   Tuple											= decoder_layerdrop
         lowercase	:   List[Any]											= use_cache
         lowercase	:   Dict											= encoder_layers
         lowercase	:   Union[str, Any]											= 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,  encoder_no_repeat_ngram_size=lowerCAmelCase,  forced_eos_token_id=lowerCAmelCase,  **lowerCAmelCase,  )




class 	a__		(    SCREAMING_SNAKE_CASE__  ):


   @property
   def 							lowercase					(				self   :				Any      )     ->							Mapping[str, Mapping[int, str]]:
         if self.task in ["default", "seq2seq-lm"]:
               lowercase	:   List[str]											= OrderedDict(
                   [
                       ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
                       ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
                   ]      )
               if self.use_past:
                     lowercase	:   Union[str, Any]											= {0: 'batch'}
                     lowercase	:   List[str]											= {0: 'batch', 1: 'past_decoder_sequence + sequence'}
               else:
                     lowercase	:   Optional[Any]											= {0: 'batch', 1: 'decoder_sequence'}
                     lowercase	:   Union[str, Any]											= {0: 'batch', 1: 'decoder_sequence'}
               if self.use_past:
                     self.fill_with_past_key_values_(lowerCAmelCase,  direction='inputs'      )
         elif self.task == "causal-lm":
               lowercase	:   Any											= OrderedDict(
                   [
                       ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
                       ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
                   ]      )
               if self.use_past:
                     lowercase						,     lowercase	:   str											= self.num_layers
                     for i in range(lowerCAmelCase      ):
                           lowercase	:   Tuple											= {0: 'batch', 2: 'past_sequence + sequence'}
                           lowercase	:   int											= {0: 'batch', 2: 'past_sequence + sequence'}
         else:
               lowercase	:   List[Any]											= 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 							lowercase					(				self   :				Tuple      )     ->							Mapping[str, Mapping[int, str]]:
         if self.task in ["default", "seq2seq-lm"]:
               lowercase	:   Union[str, Any]											= super().outputs
         else:
               lowercase	:   List[str]											= super(lowerCAmelCase,  self      ).outputs
               if self.use_past:
                     lowercase						,     lowercase	:   Optional[int]											= self.num_layers
                     for i in range(lowerCAmelCase      ):
                           lowercase	:   Dict											= {0: 'batch', 2: 'past_sequence + sequence'}
                           lowercase	:   Any											= {0: 'batch', 2: 'past_sequence + sequence'}
         return common_outputs


   def 							lowercase					(				self   :				Tuple,  lowerCAmelCase   :				PreTrainedTokenizer,  lowerCAmelCase   :				int = -1,  lowerCAmelCase   :				int = -1,  lowerCAmelCase   :				bool = False,  lowerCAmelCase   :				Optional[TensorType] = None,  )     ->							Mapping[str, Any]:
         lowercase	:   Optional[int]											= self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
             lowerCAmelCase,  lowerCAmelCase,  lowerCAmelCase,  lowerCAmelCase,  lowerCAmelCase      )
         # Generate decoder inputs
         lowercase	:   Tuple											= seq_length if not self.use_past else 1
         lowercase	:   Optional[int]											= self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
             lowerCAmelCase,  lowerCAmelCase,  lowerCAmelCase,  lowerCAmelCase,  lowerCAmelCase      )
         lowercase	:   List[str]											= {f'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
         lowercase	:   Tuple											= 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	:   Union[str, Any]											= common_inputs['input_ids'].shape
               lowercase	:   str											= common_inputs['decoder_input_ids'].shape[1]
               lowercase						,     lowercase	:   List[str]											= self.num_attention_heads
               lowercase	:   List[str]											= (
                   batch,
                   num_encoder_attention_heads,
                   encoder_seq_length,
                   self._config.hidden_size // num_encoder_attention_heads,
               )
               lowercase	:   Tuple											= decoder_seq_length
               lowercase	:   Union[str, Any]											= (
                   batch,
                   num_decoder_attention_heads,
                   decoder_past_length,
                   self._config.hidden_size // num_decoder_attention_heads,
               )
               lowercase	:   int											= torch.cat(
                   [common_inputs['decoder_attention_mask'], torch.ones(lowerCAmelCase,  lowerCAmelCase      )],  dim=1      )
               lowercase	:   List[str]											= []
               lowercase						,     lowercase	:   Any											= self.num_layers

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


   def 							lowercase					(				self   :				Dict,  lowerCAmelCase   :				PreTrainedTokenizer,  lowerCAmelCase   :				int = -1,  lowerCAmelCase   :				int = -1,  lowerCAmelCase   :				bool = False,  lowerCAmelCase   :				Optional[TensorType] = None,  )     ->							Mapping[str, Any]:
         lowercase	:   Optional[int]											= 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	:   str											= common_inputs['input_ids'].shape
               lowercase	:   Tuple											= seqlen
               lowercase						,     lowercase	:   Union[str, Any]											= self.num_layers
               lowercase						,     lowercase	:   List[Any]											= self.num_attention_heads
               lowercase	:   Dict											= (
                   batch,
                   num_encoder_attention_heads,
                   past_key_values_length,
                   self._config.hidden_size // num_encoder_attention_heads,
               )
               lowercase	:   Optional[int]											= common_inputs['attention_mask'].dtype
               lowercase	:   Optional[Any]											= torch.cat(
                   [common_inputs['attention_mask'], torch.ones(lowerCAmelCase,  lowerCAmelCase,  dtype=lowerCAmelCase      )],  dim=1      )
               lowercase	:   Union[str, Any]											= [
                   (torch.zeros(lowerCAmelCase      ), torch.zeros(lowerCAmelCase      )) for _ in range(lowerCAmelCase      )
               ]
         return common_inputs


   def 							lowercase					(				self   :				Optional[int],  lowerCAmelCase   :				PreTrainedTokenizer,  lowerCAmelCase   :				int = -1,  lowerCAmelCase   :				int = -1,  lowerCAmelCase   :				bool = False,  lowerCAmelCase   :				Optional[TensorType] = 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
         lowercase	:   str											= 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	:   Optional[Any]											= tokenizer.num_special_tokens_to_add(lowerCAmelCase      )
         lowercase	:   Any											= 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	:   Optional[int]											= [' '.join([tokenizer.unk_token]      ) * seq_length] * batch_size
         lowercase	:   Dict											= dict(tokenizer(lowerCAmelCase,  return_tensors=lowerCAmelCase      )      )
         return common_inputs


   def 							lowercase					(				self   :				Union[str, Any],  lowerCAmelCase   :				PreTrainedTokenizer,  lowerCAmelCase   :				int = -1,  lowerCAmelCase   :				int = -1,  lowerCAmelCase   :				bool = False,  lowerCAmelCase   :				Optional[TensorType] = None,  )     ->							Mapping[str, Any]:
         if self.task in ["default", "seq2seq-lm"]:
               lowercase	:   str											= 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	:   int											= self._generate_dummy_inputs_for_causal_lm(
                   lowerCAmelCase,  batch_size=lowerCAmelCase,  seq_length=lowerCAmelCase,  is_pair=lowerCAmelCase,  framework=lowerCAmelCase      )
         else:
               lowercase	:   List[Any]											= 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 							lowercase					(				self   :				str,  lowerCAmelCase   :				List[Any],  lowerCAmelCase   :				int,  lowerCAmelCase   :				Optional[Any],  lowerCAmelCase   :				Optional[Any]      )     ->							Union[str, Any]:
         if self.task in ["default", "seq2seq-lm"]:
               lowercase	:   List[str]											= super()._flatten_past_key_values_(lowerCAmelCase,  lowerCAmelCase,  lowerCAmelCase,  lowerCAmelCase      )
         else:
               lowercase	:   List[Any]											= super(lowerCAmelCase,  self      )._flatten_past_key_values_(
                   lowerCAmelCase,  lowerCAmelCase,  lowerCAmelCase,  lowerCAmelCase      )






   def 							lowercase					(				self   :				Optional[Any],  lowerCAmelCase   :				Mapping[str, Mapping[int, str]],  lowerCAmelCase   :				str      )     ->							List[Any]:
         if direction not in ["inputs", "outputs"]:
               raise ValueError(f'''direction must either be "inputs" or "outputs", but {direction} was given'''      )

         lowercase	:   Any											= 'past_key_values' if direction == 'inputs' else 'present'
         lowercase						,     lowercase	:   Dict											= self.num_layers

         lowercase	:   int											= 'past_encoder_sequence'
         lowercase	:   int											= 'past_decoder_sequence' if direction == 'inputs' else 'past_decoder_sequence + sequence'

         for i in range(lowerCAmelCase      ):
               lowercase	:   Union[str, Any]											= {0: 'batch', 2: decoder_sequence}
               lowercase	:   Optional[int]											= {0: 'batch', 2: decoder_sequence}
               lowercase	:   Dict											= {0: 'batch', 2: encoder_sequence}
               lowercase	:   Dict											= {0: 'batch', 2: encoder_sequence}





"""simple docstring"""




import unittest
from pathlib import Path

from transformers import is_vision_available, load_tool
from transformers.testing_utils import get_tests_dir

from .test_tools_common import ToolTesterMixin


if is_vision_available():
     from PIL import Image


class 	a__		(    unittest.TestCase,	SCREAMING_SNAKE_CASE__  ):


   def 							lowercase					(				self   :				Dict      )     ->							List[str]:
         lowercase	:   str											= load_tool('image-captioning'      )
         self.tool.setup()
         lowercase	:   List[str]											= load_tool('image-captioning',  remote=lowerCAmelCase      )


   def 							lowercase					(				self   :				str      )     ->							int:
         lowercase	:   Union[str, Any]											= Image.open(Path(get_tests_dir('fixtures/tests_samples/COCO'      )      ) / '000000039769.png'      )
         lowercase	:   List[str]											= self.tool(lowerCAmelCase      )
         self.assertEqual(lowerCAmelCase,  'two cats sleeping on a couch'      )


   def 							lowercase					(				self   :				str      )     ->							List[str]:
         lowercase	:   str											= Image.open(Path(get_tests_dir('fixtures/tests_samples/COCO'      )      ) / '000000039769.png'      )
         lowercase	:   Tuple											= self.remote_tool(lowerCAmelCase      )
         self.assertEqual(lowerCAmelCase,  'two cats sleeping on a couch'      )


   def 							lowercase					(				self   :				Tuple      )     ->							Union[str, Any]:
         lowercase	:   Tuple											= Image.open(Path(get_tests_dir('fixtures/tests_samples/COCO'      )      ) / '000000039769.png'      )
         lowercase	:   Union[str, Any]											= self.tool(image=lowerCAmelCase      )
         self.assertEqual(lowerCAmelCase,  'two cats sleeping on a couch'      )






   def 							lowercase					(				self   :				str      )     ->							Optional[int]:
         lowercase	:   List[str]											= Image.open(Path(get_tests_dir('fixtures/tests_samples/COCO'      )      ) / '000000039769.png'      )
         lowercase	:   str											= self.remote_tool(image=lowerCAmelCase      )
         self.assertEqual(lowerCAmelCase,  'two cats sleeping on a couch'      )
