

"""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						,   _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						,   _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						,   _UpperCAmelCase    :							Optional[Any]									=					common_inputs["""input_ids"""].shape
					_UpperCAmelCase    :							Tuple									=					common_inputs["""decoder_input_ids"""].shape[1]
					_UpperCAmelCase						,   _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						,   _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						,   _UpperCAmelCase    :							Any									=					common_inputs["""input_ids"""].shape
					_UpperCAmelCase    :							Optional[Any]									=					seqlen
					_UpperCAmelCase						,   _UpperCAmelCase    :							List[str]									=					self.num_layers
					_UpperCAmelCase						,   _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						,   _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"""






def       UpperCamelCase					(			_lowerCAmelCase     :						int,  _lowerCAmelCase     :						int = 1000 )				->			str:
	if (not isinstance(_lowerCAmelCase,  _lowerCAmelCase )) or (digit_position <= 0):
		raise ValueError("""Digit position must be a positive integer""" )
	elif (not isinstance(_lowerCAmelCase,  _lowerCAmelCase )) or (precision < 0):
		raise ValueError("""Precision must be a nonnegative integer""" )

	# compute an approximation of (16 ** (n - 1)) * pi whose fractional part is mostly
	# accurate
	_UpperCAmelCase    :							List[Any]									=					(
	    4 * _subsum(_lowerCAmelCase,  1,  _lowerCAmelCase )
	    - 2 * _subsum(_lowerCAmelCase,  4,  _lowerCAmelCase )
	    - _subsum(_lowerCAmelCase,  5,  _lowerCAmelCase )
	    - _subsum(_lowerCAmelCase,  6,  _lowerCAmelCase )
	)

	# return the first hex digit of the fractional part of the result
	return hex(int((sum_result % 1) * 16 ) )[2:]
def       UpperCamelCase					(			_lowerCAmelCase     :						int,  _lowerCAmelCase     :						int,  _lowerCAmelCase     :						int )				->			float:
	_UpperCAmelCase    :							Optional[int]									=					0.0
	for sum_index in range(digit_pos_to_extract + precision ):
		_UpperCAmelCase    :							Optional[Any]									=					8 * sum_index + denominator_addend
		if sum_index < digit_pos_to_extract:
			# if the exponential term is an integer and we mod it by the denominator
			# before dividing, only the integer part of the sum will change;
			# the fractional part will not
			_UpperCAmelCase    :							Optional[Any]									=					pow(
			    16,  digit_pos_to_extract - 1 - sum_index,  _lowerCAmelCase )
		else:
			_UpperCAmelCase    :							int									=					pow(16,  digit_pos_to_extract - 1 - sum_index )
		total += exponential_term / denominator
	return total


if __name__ == "__main__":
						import doctest

						doctest.testmod()





