

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

lowerCamelCase__				:  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     _UpperCAmelCase			(		__a):
			__a :       Union[str, Any]									=							"""blenderbot"""
			__a :       Optional[int]									=							["""past_key_values"""]
			__a :       Any									=							{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}
			def __init__(		self      ,		_A=80_08      ,		_A=1_28      ,		_A=2      ,		_A=1_02_40      ,		_A=32      ,		_A=24      ,		_A=1_02_40      ,		_A=32      ,		_A=0.0      ,		_A=0.0      ,		_A=True      ,		_A=True      ,		_A="gelu"      ,		_A=25_60      ,		_A=0.1      ,		_A=0.0      ,		_A=0.0      ,		_A=0.02      ,		_A=1      ,		_A=False      ,		_A=0      ,		_A=1      ,		_A=2      ,		_A=3      ,		_A=2      ,		**_A      ,		)    ->    List[Any]:






				'''simple docstring'''







				_UpperCAmelCase    :							List[str]									=					vocab_size
				_UpperCAmelCase    :							Any									=					max_position_embeddings
				_UpperCAmelCase    :							Any									=					d_model
				_UpperCAmelCase    :							int									=					encoder_ffn_dim
				_UpperCAmelCase    :							Tuple									=					encoder_layers
				_UpperCAmelCase    :							Union[str, Any]									=					encoder_attention_heads
				_UpperCAmelCase    :							Any									=					decoder_ffn_dim
				_UpperCAmelCase    :							Dict									=					decoder_layers
				_UpperCAmelCase    :							Any									=					decoder_attention_heads
				_UpperCAmelCase    :							List[Any]									=					dropout
				_UpperCAmelCase    :							Tuple									=					attention_dropout
				_UpperCAmelCase    :							Tuple									=					activation_dropout
				_UpperCAmelCase    :							Optional[Any]									=					activation_function
				_UpperCAmelCase    :							Any									=					init_std
				_UpperCAmelCase    :							str									=					encoder_layerdrop
				_UpperCAmelCase    :							Any									=					decoder_layerdrop
				_UpperCAmelCase    :							Union[str, Any]									=					use_cache
				_UpperCAmelCase    :							int									=					encoder_layers
				_UpperCAmelCase    :							Any									=					scale_embedding  # scale factor will be sqrt(d_model) if True

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






class     _UpperCAmelCase			(		__a):
			@property
			def 							__snake_case (		self     )    ->    Mapping[str, Mapping[int, str]]:






				'''simple docstring'''







				if self.task in ["default", "seq2seq-lm"]:
					_UpperCAmelCase    :							List[str]									=					OrderedDict(
					    [
					        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
					        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
					    ]     )
					if self.use_past:
						_UpperCAmelCase    :							List[Any]									=					{0: """batch"""}
						_UpperCAmelCase    :							str									=					{0: """batch""", 1: """past_decoder_sequence + sequence"""}
					else:
						_UpperCAmelCase    :							str									=					{0: """batch""", 1: """decoder_sequence"""}
						_UpperCAmelCase    :							List[str]									=					{0: """batch""", 1: """decoder_sequence"""}
					if self.use_past:
						self.fill_with_past_key_values_(_A      ,		direction="""inputs"""     )
				elif self.task == "causal-lm":
					_UpperCAmelCase    :							str									=					OrderedDict(
					    [
					        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
					        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
					    ]     )
					if self.use_past:
						_UpperCAmelCase    :							Union[str, Any]									=					self.num_layers
						for i in range(_A     ):
							_UpperCAmelCase    :							Tuple									=					{0: """batch""", 2: """past_sequence + sequence"""}
							_UpperCAmelCase    :							Tuple									=					{0: """batch""", 2: """past_sequence + sequence"""}
				else:
					_UpperCAmelCase    :							str									=					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 							__snake_case (		self     )    ->    Mapping[str, Mapping[int, str]]:






				'''simple docstring'''







				if self.task in ["default", "seq2seq-lm"]:
					_UpperCAmelCase    :							Union[str, Any]									=					super().outputs
				else:
					_UpperCAmelCase    :							Union[str, Any]									=					super(_A      ,		self     ).outputs
					if self.use_past:
						_UpperCAmelCase    :							Any									=					self.num_layers
						for i in range(_A     ):
							_UpperCAmelCase    :							Optional[Any]									=					{0: """batch""", 2: """past_sequence + sequence"""}
							_UpperCAmelCase    :							Union[str, Any]									=					{0: """batch""", 2: """past_sequence + sequence"""}
				return common_outputs
			def 							__snake_case (		self      ,		_A      ,		_A = -1      ,		_A = -1      ,		_A = False      ,		_A = None      ,		)    ->    Mapping[str, Any]:






				'''simple docstring'''







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

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

					for _ in range(_A     ):
						common_inputs["past_key_values"].append(
						    (
						        torch.zeros(_A     ),
						        torch.zeros(_A     ),
						        torch.zeros(_A     ),
						        torch.zeros(_A     ),
						    )     )
				return common_inputs
			def 							__snake_case (		self      ,		_A      ,		_A = -1      ,		_A = -1      ,		_A = False      ,		_A = None      ,		)    ->    Mapping[str, Any]:






				'''simple docstring'''







				_UpperCAmelCase    :							Optional[Any]									=					self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
				    _A      ,		_A      ,		_A      ,		_A      ,		_A     )

				if self.use_past:
					if not is_torch_available():
						raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""     )
					else:
						import torch
					_UpperCAmelCase    :							Any									=					common_inputs["""input_ids"""].shape
					_UpperCAmelCase    :							Optional[Any]									=					seqlen
					_UpperCAmelCase    :							List[str]									=					self.num_layers
					_UpperCAmelCase    :							Optional[int]									=					self.num_attention_heads
					_UpperCAmelCase    :							List[str]									=					(
					    batch,
					    num_encoder_attention_heads,
					    past_key_values_length,
					    self._config.hidden_size // num_encoder_attention_heads,
					)
					_UpperCAmelCase    :							Union[str, Any]									=					common_inputs["""attention_mask"""].dtype
					_UpperCAmelCase    :							int									=					torch.cat(
					    [common_inputs["""attention_mask"""], torch.ones(_A      ,		_A      ,		dtype=_A     )]      ,		dim=1     )
					_UpperCAmelCase    :							Dict									=					[
					    (torch.zeros(_A     ), torch.zeros(_A     )) for _ in range(_A     )
					]
				return common_inputs
			def 							__snake_case (		self      ,		_A      ,		_A = -1      ,		_A = -1      ,		_A = False      ,		_A = None      ,		)    ->    Mapping[str, Any]:






				'''simple docstring'''







				_UpperCAmelCase    :							Dict									=					compute_effective_axis_dimension(
				    _A      ,		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
				_UpperCAmelCase    :							Dict									=					tokenizer.num_special_tokens_to_add(_A     )
				_UpperCAmelCase    :							str									=					compute_effective_axis_dimension(
				    _A      ,		fixed_dimension=OnnxConfig.default_fixed_sequence      ,		num_token_to_add=_A     )

				# Generate dummy inputs according to compute batch and sequence
				_UpperCAmelCase    :							Any									=					[""" """.join([tokenizer.unk_token]     ) * seq_length] * batch_size
				_UpperCAmelCase    :							Any									=					dict(tokenizer(_A      ,		return_tensors=_A     )     )
				return common_inputs
			def 							__snake_case (		self      ,		_A      ,		_A = -1      ,		_A = -1      ,		_A = False      ,		_A = None      ,		)    ->    Mapping[str, Any]:






				'''simple docstring'''







				if self.task in ["default", "seq2seq-lm"]:
					_UpperCAmelCase    :							List[Any]									=					self._generate_dummy_inputs_for_default_and_seqaseq_lm(
					    _A      ,		batch_size=_A      ,		seq_length=_A      ,		is_pair=_A      ,		framework=_A     )

				elif self.task == "causal-lm":
					_UpperCAmelCase    :							Tuple									=					self._generate_dummy_inputs_for_causal_lm(
					    _A      ,		batch_size=_A      ,		seq_length=_A      ,		is_pair=_A      ,		framework=_A     )
				else:
					_UpperCAmelCase    :							Any									=					self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
					    _A      ,		batch_size=_A      ,		seq_length=_A      ,		is_pair=_A      ,		framework=_A     )

				return common_inputs
			def 							__snake_case (		self      ,		_A      ,		_A      ,		_A      ,		_A     )    ->    Any:






				'''simple docstring'''







				if self.task in ["default", "seq2seq-lm"]:
					_UpperCAmelCase    :							Any									=					super()._flatten_past_key_values_(_A      ,		_A      ,		_A      ,		_A     )
				else:
					_UpperCAmelCase    :							str									=					super(_A      ,		self     )._flatten_past_key_values_(
					    _A      ,		_A      ,		_A      ,		_A     )
			def 							__snake_case (		self      ,		_A      ,		_A     )    ->    Dict:






				'''simple docstring'''







				if direction not in ["inputs", "outputs"]:
					raise ValueError(f'''direction must either be "inputs" or "outputs", but {direction} was given'''     )

				_UpperCAmelCase    :							Optional[int]									=					"""past_key_values""" if direction == """inputs""" else """present"""
				_UpperCAmelCase    :							List[str]									=					self.num_layers

				_UpperCAmelCase    :							List[Any]									=					"""past_encoder_sequence"""
				_UpperCAmelCase    :							List[str]									=					"""past_decoder_sequence""" if direction == """inputs""" else """past_decoder_sequence + sequence"""

				for i in range(_A     ):
					_UpperCAmelCase    :							Tuple									=					{0: """batch""", 2: decoder_sequence}
					_UpperCAmelCase    :							Union[str, Any]									=					{0: """batch""", 2: decoder_sequence}
					_UpperCAmelCase    :							Dict									=					{0: """batch""", 2: encoder_sequence}
					_UpperCAmelCase    :							Any									=					{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, prepare_image_inputs


if is_torch_available():
						import torch

if is_vision_available():
						from PIL import Image

						from transformers import ViTImageProcessor






class     _UpperCAmelCase			(		unittest.TestCase):
			def __init__(		self      ,		_A      ,		_A=7      ,		_A=3      ,		_A=18      ,		_A=30      ,		_A=4_00      ,		_A=True      ,		_A=None      ,		_A=True      ,		_A=[0.5, 0.5, 0.5]      ,		_A=[0.5, 0.5, 0.5]      ,		)    ->    Optional[Any]:






				'''simple docstring'''







				_UpperCAmelCase    :							Optional[Any]									=					size if size is not None else {"""height""": 18, """width""": 18}
				_UpperCAmelCase    :							Tuple									=					parent
				_UpperCAmelCase    :							Tuple									=					batch_size
				_UpperCAmelCase    :							Optional[int]									=					num_channels
				_UpperCAmelCase    :							Any									=					image_size
				_UpperCAmelCase    :							str									=					min_resolution
				_UpperCAmelCase    :							Dict									=					max_resolution
				_UpperCAmelCase    :							Optional[Any]									=					do_resize
				_UpperCAmelCase    :							str									=					size
				_UpperCAmelCase    :							Optional[int]									=					do_normalize
				_UpperCAmelCase    :							Optional[Any]									=					image_mean
				_UpperCAmelCase    :							Any									=					image_std
			def 							__snake_case (		self     )    ->    List[Any]:






				'''simple docstring'''







				return {
				    "image_mean": self.image_mean,
				    "image_std": self.image_std,
				    "do_normalize": self.do_normalize,
				    "do_resize": self.do_resize,
				    "size": self.size,
				}






@require_torch
@require_vision
class     _UpperCAmelCase			(		__a    ,     unittest.TestCase):
			__a :       List[str]									=							ViTImageProcessor if is_vision_available() else None
			def 							__snake_case (		self     )    ->    Any:






				'''simple docstring'''







				_UpperCAmelCase    :							Tuple									=					ViTImageProcessingTester(self     )
			@property
			def 							__snake_case (		self     )    ->    List[str]:






				'''simple docstring'''







				return self.image_processor_tester.prepare_image_processor_dict()
			def 							__snake_case (		self     )    ->    str:






				'''simple docstring'''







				_UpperCAmelCase    :							int									=					self.image_processing_class(**self.image_processor_dict     )
				self.assertTrue(hasattr(_A      ,		"""image_mean"""     )     )
				self.assertTrue(hasattr(_A      ,		"""image_std"""     )     )
				self.assertTrue(hasattr(_A      ,		"""do_normalize"""     )     )
				self.assertTrue(hasattr(_A      ,		"""do_resize"""     )     )
				self.assertTrue(hasattr(_A      ,		"""size"""     )     )
			def 							__snake_case (		self     )    ->    Dict:






				'''simple docstring'''







				_UpperCAmelCase    :							Optional[int]									=					self.image_processing_class.from_dict(self.image_processor_dict     )
				self.assertEqual(image_processor.size      ,		{"""height""": 18, """width""": 18}     )

				_UpperCAmelCase    :							Any									=					self.image_processing_class.from_dict(self.image_processor_dict      ,		size=42     )
				self.assertEqual(image_processor.size      ,		{"""height""": 42, """width""": 42}     )
			def 							__snake_case (		self     )    ->    str:






				'''simple docstring'''







				pass
			def 							__snake_case (		self     )    ->    Optional[Any]:






				'''simple docstring'''







				_UpperCAmelCase    :							Dict									=					self.image_processing_class(**self.image_processor_dict     )
				# create random PIL images
				_UpperCAmelCase    :							Dict									=					prepare_image_inputs(self.image_processor_tester      ,		equal_resolution=_A     )
				for image in image_inputs:
					self.assertIsInstance(_A      ,		Image.Image     )

				# Test not batched input
				_UpperCAmelCase    :							Optional[Any]									=					image_processing(image_inputs[0]      ,		return_tensors="""pt"""     ).pixel_values
				self.assertEqual(
				    encoded_images.shape      ,		(
				        1,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.size["""height"""],
				        self.image_processor_tester.size["""width"""],
				    )      ,		)

				# Test batched
				_UpperCAmelCase    :							Union[str, Any]									=					image_processing(_A      ,		return_tensors="""pt"""     ).pixel_values
				self.assertEqual(
				    encoded_images.shape      ,		(
				        self.image_processor_tester.batch_size,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.size["""height"""],
				        self.image_processor_tester.size["""width"""],
				    )      ,		)
			def 							__snake_case (		self     )    ->    str:






				'''simple docstring'''







				_UpperCAmelCase    :							Union[str, Any]									=					self.image_processing_class(**self.image_processor_dict     )
				# create random numpy tensors
				_UpperCAmelCase    :							List[Any]									=					prepare_image_inputs(self.image_processor_tester      ,		equal_resolution=_A      ,		numpify=_A     )
				for image in image_inputs:
					self.assertIsInstance(_A      ,		np.ndarray     )

				# Test not batched input
				_UpperCAmelCase    :							Any									=					image_processing(image_inputs[0]      ,		return_tensors="""pt"""     ).pixel_values
				self.assertEqual(
				    encoded_images.shape      ,		(
				        1,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.size["""height"""],
				        self.image_processor_tester.size["""width"""],
				    )      ,		)

				# Test batched
				_UpperCAmelCase    :							int									=					image_processing(_A      ,		return_tensors="""pt"""     ).pixel_values
				self.assertEqual(
				    encoded_images.shape      ,		(
				        self.image_processor_tester.batch_size,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.size["""height"""],
				        self.image_processor_tester.size["""width"""],
				    )      ,		)
			def 							__snake_case (		self     )    ->    List[str]:






				'''simple docstring'''







				_UpperCAmelCase    :							Union[str, Any]									=					self.image_processing_class(**self.image_processor_dict     )
				# create random PyTorch tensors
				_UpperCAmelCase    :							Tuple									=					prepare_image_inputs(self.image_processor_tester      ,		equal_resolution=_A      ,		torchify=_A     )
				for image in image_inputs:
					self.assertIsInstance(_A      ,		torch.Tensor     )

				# Test not batched input
				_UpperCAmelCase    :							Optional[int]									=					image_processing(image_inputs[0]      ,		return_tensors="""pt"""     ).pixel_values
				self.assertEqual(
				    encoded_images.shape      ,		(
				        1,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.size["""height"""],
				        self.image_processor_tester.size["""width"""],
				    )      ,		)

				# Test batched
				_UpperCAmelCase    :							Tuple									=					image_processing(_A      ,		return_tensors="""pt"""     ).pixel_values
				self.assertEqual(
				    encoded_images.shape      ,		(
				        self.image_processor_tester.batch_size,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.size["""height"""],
				        self.image_processor_tester.size["""width"""],
				    )      ,		)





