






import time
import warnings
from abc import ABC
from copy import deepcopy
from typing import Optional

import torch

from ..utils import add_start_docstrings, logging


__magic_name__:					Optional[int]							=     logging.get_logger(__name__)


__magic_name__:					Union[str, Any]							=     r"\n    Args:\n        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):\n            Indices of input sequence tokens in the vocabulary.\n\n            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and\n            [`PreTrainedTokenizer.__call__`] for details.\n\n            [What are input IDs?](../glossary#input-ids)\n        scores (`torch.FloatTensor` of shape `(batch_size, config.vocab_size)`):\n            Prediction scores of a language modeling head. These can be scores for each vocabulary token before SoftMax\n            or scores for each vocabulary token after SoftMax.\n        kwargs (`Dict[str, Any]`, *optional*):\n            Additional stopping criteria specific kwargs.\n\n    Return:\n        `bool`. `False` indicates we should continue, `True` indicates we should stop.\n\n"





class 	snake_case__ (							_lowerCAmelCase				):
	@add_start_docstrings(lowerCAmelCase__			)
	def __call__(    self      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       **lowerCAmelCase__			)							->				bool:
							raise NotImplementedError("""StoppingCriteria needs to be subclassed"""			)





class 	snake_case__ (							_lowerCAmelCase				):
	def __init__(    self      ,       lowerCAmelCase__      ,       lowerCAmelCase__ = None			)							->				List[str]:
							__magic_name__      :       int       						= max_length
							__magic_name__      :       Tuple       						= max_position_embeddings







	@add_start_docstrings(lowerCAmelCase__			)
	def __call__(    self      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       **lowerCAmelCase__			)							->				bool:
							__magic_name__      :       Dict       						= input_ids.shape[-1]
							__magic_name__      :       Dict       						= cur_len >= self.max_length
							if self.max_position_embeddings is not None and not is_done and cur_len >= self.max_position_embeddings:
													logger.warning_once(
													    """This is a friendly reminder - the current text generation call will exceed the model's predefined """
													    F'maximum length ({self.max_position_embeddings}). Depending on the model, you may observe '
													    """exceptions, performance degradation, or nothing at all."""			)
							return is_done





class 	snake_case__ (							_lowerCAmelCase				):
	def __init__(    self      ,       lowerCAmelCase__      ,       lowerCAmelCase__			)							->				Optional[int]:
							warnings.warn(
							    """The class `MaxNewTokensCriteria` is deprecated. """
							    F'Please use `MaxLengthCriteria(max_length={start_length + max_new_tokens})` '
							    """with `max_length = start_length + max_new_tokens` instead."""      ,       lowerCAmelCase__      ,       )
							__magic_name__      :       Optional[Any]       						= start_length
							__magic_name__      :       List[str]       						= max_new_tokens
							__magic_name__      :       Dict       						= start_length + max_new_tokens







	@add_start_docstrings(lowerCAmelCase__			)
	def __call__(    self      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       **lowerCAmelCase__			)							->				bool:
							return input_ids.shape[-1] >= self.max_length





class 	snake_case__ (							_lowerCAmelCase				):
	def __init__(    self      ,       lowerCAmelCase__      ,       lowerCAmelCase__ = None			)							->				str:
							__magic_name__      :       Optional[Any]       						= max_time
							__magic_name__      :       Tuple       						= time.time() if initial_timestamp is None else initial_timestamp







	@add_start_docstrings(lowerCAmelCase__			)
	def __call__(    self      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       **lowerCAmelCase__			)							->				bool:
							return time.time() - self.initial_timestamp > self.max_time





class 	snake_case__ (							_lowerCAmelCase				):
	@add_start_docstrings(lowerCAmelCase__			)
	def __call__(    self      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       **lowerCAmelCase__			)							->				bool:
							return any(criteria(lowerCAmelCase__      ,       lowerCAmelCase__			) for criteria in self			)







	@property
	def       __magic_name__    (    self			)							->				Optional[int]:
							for stopping_criterium in self:
													if isinstance(lowerCAmelCase__      ,       lowerCAmelCase__			):
																			return stopping_criterium.max_length
													elif isinstance(lowerCAmelCase__      ,       lowerCAmelCase__			):
																			return stopping_criterium.max_length
							return None



def    UpperCamelCase						(	_A,   _A			):






						"""simple docstring"""







						__magic_name__      :       Tuple       						= stopping_criteria.max_length
						__magic_name__      :       str       						= deepcopy(_A			)
						if stopping_max_length is not None and stopping_max_length != max_length:
												warnings.warn("""You set different `max_length` for stopping criteria and `max_length` parameter""",   _A			)
						elif stopping_max_length is None:
												new_stopping_criteria.append(MaxLengthCriteria(max_length=_A			)			)
						return new_stopping_criteria








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


__magic_name__:					Any							=     logging.get_logger(__name__)

__magic_name__:					Dict							=     {
    "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 	snake_case__ (							_lowerCAmelCase				):
	lowercase__						:					Any											=   '''blenderbot-small'''
	lowercase__						:					Optional[int]											=   ['''past_key_values''']
	lowercase__						:					Dict											=   {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}
	def __init__(    self      ,       lowerCAmelCase__=5_02_65      ,       lowerCAmelCase__=5_12      ,       lowerCAmelCase__=8      ,       lowerCAmelCase__=20_48      ,       lowerCAmelCase__=16      ,       lowerCAmelCase__=8      ,       lowerCAmelCase__=20_48      ,       lowerCAmelCase__=16      ,       lowerCAmelCase__=0.0      ,       lowerCAmelCase__=0.0      ,       lowerCAmelCase__=True      ,       lowerCAmelCase__=True      ,       lowerCAmelCase__="gelu"      ,       lowerCAmelCase__=5_12      ,       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__      ,       )							->				Tuple:
							__magic_name__      :       Tuple       						= vocab_size
							__magic_name__      :       List[str]       						= max_position_embeddings
							__magic_name__      :       Union[str, Any]       						= d_model
							__magic_name__      :       Optional[int]       						= encoder_ffn_dim
							__magic_name__      :       Union[str, Any]       						= encoder_layers
							__magic_name__      :       List[str]       						= encoder_attention_heads
							__magic_name__      :       List[Any]       						= decoder_ffn_dim
							__magic_name__      :       str       						= decoder_layers
							__magic_name__      :       List[str]       						= decoder_attention_heads
							__magic_name__      :       Union[str, Any]       						= dropout
							__magic_name__      :       Tuple       						= attention_dropout
							__magic_name__      :       List[Any]       						= activation_dropout
							__magic_name__      :       List[Any]       						= activation_function
							__magic_name__      :       Optional[int]       						= init_std
							__magic_name__      :       Dict       						= encoder_layerdrop
							__magic_name__      :       Union[str, Any]       						= decoder_layerdrop
							__magic_name__      :       Optional[int]       						= use_cache
							__magic_name__      :       List[Any]       						= encoder_layers
							__magic_name__      :       Dict       						= 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 	snake_case__ (							_lowerCAmelCase				):
	@property
	def       __magic_name__    (    self			)							->				Mapping[str, Mapping[int, str]]:
							if self.task in ["default", "seq2seq-lm"]:
													__magic_name__      :       Optional[int]       						= OrderedDict(
													    [
													        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
													        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
													    ]			)

													if self.use_past:
																			__magic_name__      :       List[Any]       						= {0: """batch"""}
																			__magic_name__      :       Dict       						= {0: """batch""", 1: """past_decoder_sequence + sequence"""}
													else:
																			__magic_name__      :       List[str]       						= {0: """batch""", 1: """decoder_sequence"""}
																			__magic_name__      :       Any       						= {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.
													__magic_name__      :       Union[str, Any]       						= OrderedDict(
													    [
													        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
													        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
													    ]			)
													if self.use_past:
																			__magic_name__						,__magic_name__      :       Dict       						= self.num_layers
																			for i in range(lowerCAmelCase__			):
																									__magic_name__      :       Dict       						= {0: """batch""", 2: """past_sequence + sequence"""}
																									__magic_name__      :       int       						= {0: """batch""", 2: """past_sequence + sequence"""}
							else:
													__magic_name__      :       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       __magic_name__    (    self			)							->				Mapping[str, Mapping[int, str]]:
							if self.task in ["default", "seq2seq-lm"]:
													__magic_name__      :       Any       						= super().outputs
							else:
													__magic_name__      :       int       						= super(lowerCAmelCase__      ,       self			).outputs
													if self.use_past:
																			__magic_name__						,__magic_name__      :       str       						= self.num_layers
																			for i in range(lowerCAmelCase__			):
																									__magic_name__      :       Tuple       						= {0: """batch""", 2: """past_sequence + sequence"""}
																									__magic_name__      :       Dict       						= {0: """batch""", 2: """past_sequence + sequence"""}
							return common_outputs
	def       __magic_name__    (    self      ,       lowerCAmelCase__      ,       lowerCAmelCase__ = -1      ,       lowerCAmelCase__ = -1      ,       lowerCAmelCase__ = False      ,       lowerCAmelCase__ = None      ,       )							->				Mapping[str, Any]:
							__magic_name__      :       Tuple       						= self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
							    lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__			)

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

													__magic_name__      :       List[str]       						= torch.cat(
													    [common_inputs["""decoder_attention_mask"""], torch.ones(lowerCAmelCase__      ,       lowerCAmelCase__			)]      ,       dim=1			)

													__magic_name__      :       Union[str, Any]       						= []
													# If the number of encoder and decoder layers are present in the model configuration, both are considered
													__magic_name__						,__magic_name__      :       List[str]       						= self.num_layers
													__magic_name__      :       Optional[Any]       						= min(lowerCAmelCase__      ,       lowerCAmelCase__			)
													__magic_name__      :       List[str]       						= max(lowerCAmelCase__      ,       lowerCAmelCase__			) - min_num_layers
													__magic_name__      :       Tuple       						= """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.
													__magic_name__      :       Union[str, Any]       						= 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       __magic_name__    (    self      ,       lowerCAmelCase__      ,       lowerCAmelCase__ = -1      ,       lowerCAmelCase__ = -1      ,       lowerCAmelCase__ = False      ,       lowerCAmelCase__ = None      ,       )							->				Mapping[str, Any]:
							__magic_name__      :       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
													__magic_name__						,__magic_name__      :       Tuple       						= common_inputs["""input_ids"""].shape
													# Not using the same length for past_key_values
													__magic_name__      :       List[Any]       						= seqlen + 2
													__magic_name__						,__magic_name__      :       Any       						= self.num_layers
													__magic_name__						,__magic_name__      :       int       						= self.num_attention_heads
													__magic_name__      :       Optional[int]       						= (
													    batch,
													    num_encoder_attention_heads,
													    past_key_values_length,
													    self._config.hidden_size // num_encoder_attention_heads,
													)

													__magic_name__      :       Optional[int]       						= common_inputs["""attention_mask"""].dtype
													__magic_name__      :       Optional[Any]       						= torch.cat(
													    [common_inputs["""attention_mask"""], torch.ones(lowerCAmelCase__      ,       lowerCAmelCase__      ,       dtype=lowerCAmelCase__			)]      ,       dim=1			)
													__magic_name__      :       Tuple       						= [
													    (torch.zeros(lowerCAmelCase__			), torch.zeros(lowerCAmelCase__			)) for _ in range(lowerCAmelCase__			)
													]
							return common_inputs
	def       __magic_name__    (    self      ,       lowerCAmelCase__      ,       lowerCAmelCase__ = -1      ,       lowerCAmelCase__ = -1      ,       lowerCAmelCase__ = False      ,       lowerCAmelCase__ = 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
							__magic_name__      :       Tuple       						= 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
							__magic_name__      :       str       						= tokenizer.num_special_tokens_to_add(lowerCAmelCase__			)
							__magic_name__      :       List[str]       						= 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
							__magic_name__      :       List[Any]       						= [""" """.join([tokenizer.unk_token]			) * seq_length] * batch_size
							__magic_name__      :       List[str]       						= dict(tokenizer(lowerCAmelCase__      ,       return_tensors=lowerCAmelCase__			)			)
							return common_inputs
	def       __magic_name__    (    self      ,       lowerCAmelCase__      ,       lowerCAmelCase__ = -1      ,       lowerCAmelCase__ = -1      ,       lowerCAmelCase__ = False      ,       lowerCAmelCase__ = None      ,       )							->				Mapping[str, Any]:
							if self.task in ["default", "seq2seq-lm"]:
													__magic_name__      :       Tuple       						= 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":
													__magic_name__      :       str       						= self._generate_dummy_inputs_for_causal_lm(
													    lowerCAmelCase__      ,       batch_size=lowerCAmelCase__      ,       seq_length=lowerCAmelCase__      ,       is_pair=lowerCAmelCase__      ,       framework=lowerCAmelCase__			)
							else:
													__magic_name__      :       Dict       						= 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       __magic_name__    (    self      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__			)							->				Dict:
							if self.task in ["default", "seq2seq-lm"]:
													__magic_name__      :       List[Any]       						= super()._flatten_past_key_values_(lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__			)
							else:
													__magic_name__      :       Tuple       						= super(lowerCAmelCase__      ,       self			)._flatten_past_key_values_(
													    lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__			)
