



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


snake_case    :						Any  =						logging.get_logger(__name__)

snake_case    :						Union[str, Any]  =						{
    '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 							lowerCAmelCase__  (	UpperCamelCase						):
					__A     :    List[str]           =						'blenderbot-small'
					__A     :    List[str]           =						['past_key_values']
					__A     :    str           =						{'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}







					def __init__(       self      :     int			,		_A      :     Optional[int]=5_0265			,		_A      :     Dict=512			,		_A      :     str=8			,		_A      :     Optional[Any]=2048			,		_A      :     Union[str, Any]=16			,		_A      :     Dict=8			,		_A      :     List[Any]=2048			,		_A      :     Union[str, Any]=16			,		_A      :     Union[str, Any]=0.0			,		_A      :     List[Any]=0.0			,		_A      :     Optional[Any]=True			,		_A      :     List[str]=True			,		_A      :     Union[str, Any]="gelu"			,		_A      :     Union[str, Any]=512			,		_A      :     List[str]=0.1			,		_A      :     Union[str, Any]=0.0			,		_A      :     str=0.0			,		_A      :     str=0.02			,		_A      :     Optional[Any]=1			,		_A      :     str=False			,		_A      :     Union[str, Any]=0			,		_A      :     List[str]=1			,		_A      :     Tuple=2			,		_A      :     Optional[Any]=2			,		**_A      :     Any			,		):
											A__	:							Union[str, Any]						    = vocab_size
											A__	:							List[str]						    = max_position_embeddings
											A__	:							str						    = d_model
											A__	:							str						    = encoder_ffn_dim
											A__	:							Any						    = encoder_layers
											A__	:							Dict						    = encoder_attention_heads
											A__	:							List[str]						    = decoder_ffn_dim
											A__	:							List[str]						    = decoder_layers
											A__	:							int						    = decoder_attention_heads
											A__	:							Tuple						    = dropout
											A__	:							int						    = attention_dropout
											A__	:							int						    = activation_dropout
											A__	:							Dict						    = activation_function
											A__	:							List[Any]						    = init_std
											A__	:							str						    = encoder_layerdrop
											A__	:							Tuple						    = decoder_layerdrop
											A__	:							Tuple						    = use_cache
											A__	:							Union[str, Any]						    = encoder_layers
											A__	:							List[str]						    = 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			,		forced_eos_token_id=_A			,		**_A			,		)





class 							lowerCAmelCase__  (	UpperCamelCase						):







					@property
					def  _lowercase			(       self      :     Union[str, Any]):
											if self.task in ["default", "seq2seq-lm"]:
																	A__	:							int						    = OrderedDict(
																	    [
																	        ("input_ids", {0: "batch", 1: "encoder_sequence"}),
																	        ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
																	    ])

																	if self.use_past:
																							A__	:							Union[str, Any]						    = {0: "batch"}
																							A__	:							str						    = {0: "batch", 1: "past_decoder_sequence + sequence"}
																	else:
																							A__	:							Dict						    = {0: "batch", 1: "decoder_sequence"}
																							A__	:							Dict						    = {0: "batch", 1: "decoder_sequence"}

																	if self.use_past:
																							self.fill_with_past_key_values_(_A			,		direction="inputs")
											elif self.task == "causal-lm":
																	# TODO: figure this case out.
																	A__	:							Tuple						    = OrderedDict(
																	    [
																	        ("input_ids", {0: "batch", 1: "encoder_sequence"}),
																	        ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
																	    ])
																	if self.use_past:
																							A__	:							Any						    = self.num_layers
																							for i in range(_A):
																													A__	:							Union[str, Any]						    = {0: "batch", 2: "past_sequence + sequence"}
																													A__	:							Tuple						    = {0: "batch", 2: "past_sequence + sequence"}
											else:
																	A__	:							Union[str, 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
					def  _lowercase			(       self      :     List[str]):
											if self.task in ["default", "seq2seq-lm"]:
																	A__	:							Union[str, Any]						    = super().outputs
											else:
																	A__	:							List[Any]						    = super(_A			,		self).outputs
																	if self.use_past:
																							A__	:							Dict						    = self.num_layers
																							for i in range(_A):
																													A__	:							Any						    = {0: "batch", 2: "past_sequence + sequence"}
																													A__	:							Union[str, Any]						    = {0: "batch", 2: "past_sequence + sequence"}
											return common_outputs







					def  _lowercase			(       self      :     List[Any]			,		_A      :     PreTrainedTokenizer			,		_A      :     int = -1			,		_A      :     int = -1			,		_A      :     bool = False			,		_A      :     Optional[TensorType] = None			,		):
											A__	:							List[str]						    = self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
											    _A			,		_A			,		_A			,		_A			,		_A)

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

																	A__	:							Dict						    = torch.cat(
																	    [common_inputs["decoder_attention_mask"], torch.ones(_A			,		_A)]			,		dim=1)

																	A__	:							List[str]						    = []
																	# If the number of encoder and decoder layers are present in the model configuration, both are considered
																	A__	:							int						    = self.num_layers
																	A__	:							Dict						    = min(_A			,		_A)
																	A__	:							Optional[Any]						    = max(_A			,		_A) - min_num_layers
																	A__	:							Optional[Any]						    = "encoder" if num_encoder_layers > num_decoder_layers else "decoder"

																	for _ in range(_A):
																							common_inputs["past_key_values"].append(
																							    (
																							        torch.zeros(_A),
																							        torch.zeros(_A),
																							        torch.zeros(_A),
																							        torch.zeros(_A),
																							    ))
																	# TODO: test this.
																	A__	:							List[str]						    = encoder_shape if remaining_side_name == "encoder" else decoder_shape
																	for _ in range(_A			,		_A):
																							common_inputs["past_key_values"].append((torch.zeros(_A), torch.zeros(_A)))
											return common_inputs







					def  _lowercase			(       self      :     int			,		_A      :     PreTrainedTokenizer			,		_A      :     int = -1			,		_A      :     int = -1			,		_A      :     bool = False			,		_A      :     Optional[TensorType] = None			,		):
											A__	:							List[str]						    = 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
																	A__	:							Tuple						    = common_inputs["input_ids"].shape
																	# Not using the same length for past_key_values
																	A__	:							Any						    = seqlen + 2
																	A__	:							str						    = self.num_layers
																	A__	:							Union[str, Any]						    = self.num_attention_heads
																	A__	:							Dict						    = (
																	    batch,
																	    num_encoder_attention_heads,
																	    past_key_values_length,
																	    self._config.hidden_size // num_encoder_attention_heads,
																	)

																	A__	:							List[Any]						    = common_inputs["attention_mask"].dtype
																	A__	:							Optional[Any]						    = torch.cat(
																	    [common_inputs["attention_mask"], torch.ones(_A			,		_A			,		dtype=_A)]			,		dim=1)
																	A__	:							Any						    = [
																	    (torch.zeros(_A), torch.zeros(_A)) for _ in range(_A)
																	]
											return common_inputs







					def  _lowercase			(       self      :     Union[str, Any]			,		_A      :     PreTrainedTokenizer			,		_A      :     int = -1			,		_A      :     int = -1			,		_A      :     bool = False			,		_A      :     Optional[TensorType] = None			,		):
											# Copied from OnnxConfig.generate_dummy_inputs
											# Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity.
											# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
											A__	:							List[Any]						    = 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
											A__	:							Any						    = tokenizer.num_special_tokens_to_add(_A)
											A__	:							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
											A__	:							Optional[int]						    = [" ".join([tokenizer.unk_token]) * seq_length] * batch_size
											A__	:							List[Any]						    = dict(tokenizer(_A			,		return_tensors=_A))
											return common_inputs







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




# Lint as: python3
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING, Dict, Optional

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.logging import get_logger
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


if TYPE_CHECKING:
		import jax
		import jaxlib

snake_case    :						Optional[int]  =						get_logger()

snake_case    :						Optional[dict]  =						None




class 							lowerCAmelCase__  (	TensorFormatter[Mapping, 'jax.Array', Mapping]						):







					def __init__(       self      :     Optional[Any]			,		_A      :     Optional[int]=None			,		_A      :     str=None			,		**_A      :     Tuple):
											super().__init__(features=_A)
											import jax
											from jaxlib.xla_client import Device

											if isinstance(_A			,		_A):
																	raise ValueError(
																	    F'Expected {device} to be a `str` not {type(_A)}, as `jaxlib.xla_extension.Device` '
																	    "is not serializable neither with `pickle` nor with `dill`. Instead you can surround "
																	    "the device with `str()` to get its string identifier that will be internally mapped "
																	    "to the actual `jaxlib.xla_extension.Device`.")
											A__	:							Union[str, Any]						    = device if isinstance(_A			,		_A) else str(jax.devices()[0])
											# using global variable since `jaxlib.xla_extension.Device` is not serializable neither
											# with `pickle` nor with `dill`, so we need to use a global variable instead
											global DEVICE_MAPPING
											if DEVICE_MAPPING is None:
																	A__	:							Any						    = self._map_devices_to_str()
											if self.device not in list(DEVICE_MAPPING.keys()):
																	logger.warning(
																	    F'Device with string identifier {self.device} not listed among the available '
																	    F'devices: {list(DEVICE_MAPPING.keys())}, so falling back to the default '
																	    F'device: {str(jax.devices()[0])}.')
																	A__	:							str						    = str(jax.devices()[0])
											A__	:							List[Any]						    = jnp_array_kwargs







					@staticmethod
					def  _lowercase			(       ):
											import jax

											return {str(_A): device for device in jax.devices()}







					def  _lowercase			(       self      :     Tuple			,		_A      :     int):
											import jax
											import jax.numpy as jnp

											if isinstance(_A			,		_A) and column:
																	if all(
																	    isinstance(_A			,		jax.Array) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column):
																							return jnp.stack(_A			,		axis=0)
											return column







					def  _lowercase			(       self      :     List[str]			,		_A      :     Union[str, Any]):
											import jax
											import jax.numpy as jnp

											if isinstance(_A			,		(str, bytes, type(_A))):
																	return value
											elif isinstance(_A			,		(np.character, np.ndarray)) and np.issubdtype(value.dtype			,		np.character):
																	return value.tolist()

											A__	:							Dict						    = {}

											if isinstance(_A			,		(np.number, np.ndarray)) and np.issubdtype(value.dtype			,		np.integer):
																	# the default int precision depends on the jax config
																	# see https://jax.readthedocs.io/en/latest/notebooks/Common_Gotchas_in_JAX.html#double-64bit-precision
																	if jax.config.jax_enable_xaa:
																							A__	:							Union[str, Any]						    = {"dtype": jnp.intaa}
																	else:
																							A__	:							int						    = {"dtype": jnp.intaa}
											elif isinstance(_A			,		(np.number, np.ndarray)) and np.issubdtype(value.dtype			,		np.floating):
																	A__	:							str						    = {"dtype": jnp.floataa}
											elif config.PIL_AVAILABLE and "PIL" in sys.modules:
																	import PIL.Image

																	if isinstance(_A			,		PIL.Image.Image):
																							A__	:							Any						    = np.asarray(_A)

        # using global variable since `jaxlib.xla_extension.Device` is not serializable neither
        # with `pickle` nor with `dill`, so we need to use a global variable instead
											global DEVICE_MAPPING
											if DEVICE_MAPPING is None:
																	A__	:							Optional[Any]						    = self._map_devices_to_str()

											with jax.default_device(DEVICE_MAPPING[self.device]):
																	# calling jnp.array on a np.ndarray does copy the data
																	# see https://github.com/google/jax/issues/4486
																	return jnp.array(_A			,		**{**default_dtype, **self.jnp_array_kwargs})







					def  _lowercase			(       self      :     Dict			,		_A      :     Any):
											import jax

											# support for torch, tf, jax etc.
											if config.TORCH_AVAILABLE and "torch" in sys.modules:
																	import torch

																	if isinstance(_A			,		torch.Tensor):
																							return self._tensorize(data_struct.detach().cpu().numpy()[()])
											if hasattr(_A			,		"__array__") and not isinstance(_A			,		jax.Array):
																	A__	:							Any						    = data_struct.__array__()
											# support for nested types like struct of list of struct
											if isinstance(_A			,		np.ndarray):
																	if data_struct.dtype == object:  # jax arrays cannot be instantied from an array of objects
																							return self._consolidate([self.recursive_tensorize(_A) for substruct in data_struct])
											elif isinstance(_A			,		(list, tuple)):
																	return self._consolidate([self.recursive_tensorize(_A) for substruct in data_struct])
											return self._tensorize(_A)







					def  _lowercase			(       self      :     Optional[Any]			,		_A      :     dict):
											return map_nested(self._recursive_tensorize			,		_A			,		map_list=_A)







					def  _lowercase			(       self      :     List[str]			,		_A      :     pa.Table):
											A__	:							Dict						    = self.numpy_arrow_extractor().extract_row(_A)
											A__	:							int						    = self.python_features_decoder.decode_row(_A)
											return self.recursive_tensorize(_A)







					def  _lowercase			(       self      :     int			,		_A      :     pa.Table):
											A__	:							int						    = self.numpy_arrow_extractor().extract_column(_A)
											A__	:							Optional[int]						    = self.python_features_decoder.decode_column(_A			,		pa_table.column_names[0])
											A__	:							Dict						    = self.recursive_tensorize(_A)
											A__	:							List[Any]						    = self._consolidate(_A)
											return column







					def  _lowercase			(       self      :     Tuple			,		_A      :     pa.Table):
											A__	:							Any						    = self.numpy_arrow_extractor().extract_batch(_A)
											A__	:							str						    = self.python_features_decoder.decode_batch(_A)
											A__	:							Optional[Any]						    = self.recursive_tensorize(_A)
											for column_name in batch:
																	A__	:							Optional[int]						    = self._consolidate(batch[column_name])
											return batch