



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

UpperCamelCase__:       Optional[int]							     =      {
    "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     SCREAMING_SNAKE_CASE(							A__      ):







	"""simple docstring"""






	lowerCamelCase__      			=	"""blenderbot"""
	lowerCamelCase__      			=	["""past_key_values"""]
	lowerCamelCase__      			=	{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}







	def __init__(						self      :					int		,      __snake_case      :					Dict=8008		,      __snake_case      :					List[Any]=128		,      __snake_case      :					Union[str, Any]=2		,      __snake_case      :					int=10240		,      __snake_case      :					Optional[int]=32		,      __snake_case      :					Any=24		,      __snake_case      :					Optional[int]=10240		,      __snake_case      :					Union[str, Any]=32		,      __snake_case      :					Tuple=0.0		,      __snake_case      :					Tuple=0.0		,      __snake_case      :					Optional[int]=True		,      __snake_case      :					Tuple=True		,      __snake_case      :					int="gelu"		,      __snake_case      :					Dict=2560		,      __snake_case      :					Union[str, Any]=0.1		,      __snake_case      :					Optional[int]=0.0		,      __snake_case      :					Optional[Any]=0.0		,      __snake_case      :					Optional[Any]=0.02		,      __snake_case      :					Dict=1		,      __snake_case      :					List[str]=False		,      __snake_case      :					Optional[int]=0		,      __snake_case      :					Optional[Any]=1		,      __snake_case      :					Optional[int]=2		,      __snake_case      :					Dict=3		,      __snake_case      :					Tuple=2		,      **__snake_case      :					Tuple		,      )      ->		Any:
								UpperCAmelCase			:	List[str]										=		vocab_size
								UpperCAmelCase			:	Any										=		max_position_embeddings
								UpperCAmelCase			:	Tuple										=		d_model
								UpperCAmelCase			:	Optional[Any]										=		encoder_ffn_dim
								UpperCAmelCase			:	List[str]										=		encoder_layers
								UpperCAmelCase			:	Dict										=		encoder_attention_heads
								UpperCAmelCase			:	List[str]										=		decoder_ffn_dim
								UpperCAmelCase			:	Optional[Any]										=		decoder_layers
								UpperCAmelCase			:	Optional[Any]										=		decoder_attention_heads
								UpperCAmelCase			:	int										=		dropout
								UpperCAmelCase			:	Tuple										=		attention_dropout
								UpperCAmelCase			:	List[str]										=		activation_dropout
								UpperCAmelCase			:	str										=		activation_function
								UpperCAmelCase			:	str										=		init_std
								UpperCAmelCase			:	Dict										=		encoder_layerdrop
								UpperCAmelCase			:	Dict										=		decoder_layerdrop
								UpperCAmelCase			:	Any										=		use_cache
								UpperCAmelCase			:	List[str]										=		encoder_layers
								UpperCAmelCase			:	str										=		scale_embedding  # scale factor will be sqrt(d_model) if True

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

class     SCREAMING_SNAKE_CASE(							A__      ):







	"""simple docstring"""






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







	def 					A     (						self      :					Optional[int]		,      __snake_case      :					PreTrainedTokenizer		,      __snake_case      :					int = -1		,      __snake_case      :					int = -1		,      __snake_case      :					bool = False		,      __snake_case      :					Optional[TensorType] = None		,      )      ->		Mapping[str, Any]:
								UpperCAmelCase			:	Union[str, Any]										=		self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
								    __snake_case		,      __snake_case		,      __snake_case		,      __snake_case		,      __snake_case		)
								# Generate decoder inputs
								UpperCAmelCase			:	List[str]										=		seq_length if not self.use_past else 1
								UpperCAmelCase			:	List[Any]										=		self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
								    __snake_case		,      __snake_case		,      __snake_case		,      __snake_case		,      __snake_case		)
								UpperCAmelCase			:	str										=		{F"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()}
								UpperCAmelCase			:	List[str]										=		dict(**__snake_case		,      **__snake_case		)

								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			:	str										=		common_inputs['''input_ids'''].shape
															UpperCAmelCase			:	Dict										=		common_inputs['''decoder_input_ids'''].shape[1]
															UpperCAmelCase							,		UpperCAmelCase			:	Optional[int]										=		self.num_attention_heads
															UpperCAmelCase			:	str										=		(
															    batch,
															    num_encoder_attention_heads,
															    encoder_seq_length,
															    self._config.hidden_size // num_encoder_attention_heads,
															)
															UpperCAmelCase			:	int										=		decoder_seq_length
															UpperCAmelCase			:	List[str]										=		(
															    batch,
															    num_decoder_attention_heads,
															    decoder_past_length,
															    self._config.hidden_size // num_decoder_attention_heads,
															)
															UpperCAmelCase			:	Any										=		torch.cat(
															    [common_inputs['''decoder_attention_mask'''], torch.ones(__snake_case		,      __snake_case		)]		,      dim=1		)
															UpperCAmelCase			:	Optional[Any]										=		[]
															UpperCAmelCase							,		UpperCAmelCase			:	Any										=		self.num_layers

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







	def 					A     (						self      :					Dict		,      __snake_case      :					PreTrainedTokenizer		,      __snake_case      :					int = -1		,      __snake_case      :					int = -1		,      __snake_case      :					bool = False		,      __snake_case      :					Optional[TensorType] = None		,      )      ->		Mapping[str, Any]:
								UpperCAmelCase			:	List[str]										=		self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
								    __snake_case		,      __snake_case		,      __snake_case		,      __snake_case		,      __snake_case		)

								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			:	int										=		common_inputs['''input_ids'''].shape
															UpperCAmelCase			:	Optional[int]										=		seqlen
															UpperCAmelCase							,		UpperCAmelCase			:	Optional[int]										=		self.num_layers
															UpperCAmelCase							,		UpperCAmelCase			:	Optional[int]										=		self.num_attention_heads
															UpperCAmelCase			:	Union[str, Any]										=		(
															    batch,
															    num_encoder_attention_heads,
															    past_key_values_length,
															    self._config.hidden_size // num_encoder_attention_heads,
															)
															UpperCAmelCase			:	Optional[Any]										=		common_inputs['''attention_mask'''].dtype
															UpperCAmelCase			:	Optional[int]										=		torch.cat(
															    [common_inputs['''attention_mask'''], torch.ones(__snake_case		,      __snake_case		,      dtype=__snake_case		)]		,      dim=1		)
															UpperCAmelCase			:	List[str]										=		[
															    (torch.zeros(__snake_case		), torch.zeros(__snake_case		)) for _ in range(__snake_case		)
															]
								return common_inputs







	def 					A     (						self      :					str		,      __snake_case      :					PreTrainedTokenizer		,      __snake_case      :					int = -1		,      __snake_case      :					int = -1		,      __snake_case      :					bool = False		,      __snake_case      :					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
								UpperCAmelCase			:	str										=		compute_effective_axis_dimension(
								    __snake_case		,      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			:	List[str]										=		tokenizer.num_special_tokens_to_add(__snake_case		)
								UpperCAmelCase			:	Optional[int]										=		compute_effective_axis_dimension(
								    __snake_case		,      fixed_dimension=OnnxConfig.default_fixed_sequence		,      num_token_to_add=__snake_case		)

								# Generate dummy inputs according to compute batch and sequence
								UpperCAmelCase			:	List[Any]										=		[''' '''.join([tokenizer.unk_token]		) * seq_length] * batch_size
								UpperCAmelCase			:	Optional[Any]										=		dict(tokenizer(__snake_case		,      return_tensors=__snake_case		)		)
								return common_inputs







	def 					A     (						self      :					Optional[int]		,      __snake_case      :					PreTrainedTokenizer		,      __snake_case      :					int = -1		,      __snake_case      :					int = -1		,      __snake_case      :					bool = False		,      __snake_case      :					Optional[TensorType] = None		,      )      ->		Mapping[str, Any]:
								if self.task in ["default", "seq2seq-lm"]:
															UpperCAmelCase			:	Any										=		self._generate_dummy_inputs_for_default_and_seqaseq_lm(
															    __snake_case		,      batch_size=__snake_case		,      seq_length=__snake_case		,      is_pair=__snake_case		,      framework=__snake_case		)

								elif self.task == "causal-lm":
															UpperCAmelCase			:	List[Any]										=		self._generate_dummy_inputs_for_causal_lm(
															    __snake_case		,      batch_size=__snake_case		,      seq_length=__snake_case		,      is_pair=__snake_case		,      framework=__snake_case		)
								else:
															UpperCAmelCase			:	Any										=		self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
															    __snake_case		,      batch_size=__snake_case		,      seq_length=__snake_case		,      is_pair=__snake_case		,      framework=__snake_case		)

								return common_inputs







	def 					A     (						self      :					Optional[Any]		,      __snake_case      :					str		,      __snake_case      :					Any		,      __snake_case      :					str		,      __snake_case      :					int		)      ->		Optional[Any]:
								if self.task in ["default", "seq2seq-lm"]:
															UpperCAmelCase			:	Tuple										=		super()._flatten_past_key_values_(__snake_case		,      __snake_case		,      __snake_case		,      __snake_case		)
								else:
															UpperCAmelCase			:	Optional[Any]										=		super(__snake_case		,      self		)._flatten_past_key_values_(
															    __snake_case		,      __snake_case		,      __snake_case		,      __snake_case		)







	def 					A     (						self      :					Union[str, Any]		,      __snake_case      :					Mapping[str, Mapping[int, str]]		,      __snake_case      :					str		)      ->		Dict:
								if direction not in ["inputs", "outputs"]:
															raise ValueError(F"""direction must either be \"inputs\" or \"outputs\", but {direction} was given"""		)

								UpperCAmelCase			:	Dict										=		'''past_key_values''' if direction == '''inputs''' else '''present'''
								UpperCAmelCase							,		UpperCAmelCase			:	Tuple										=		self.num_layers

								UpperCAmelCase			:	List[str]										=		'''past_encoder_sequence'''
								UpperCAmelCase			:	int										=		'''past_decoder_sequence''' if direction == '''inputs''' else '''past_decoder_sequence + sequence'''

								for i in range(__snake_case		):
															UpperCAmelCase			:	int										=		{0: '''batch''', 2: decoder_sequence}
															UpperCAmelCase			:	List[str]										=		{0: '''batch''', 2: decoder_sequence}
															UpperCAmelCase			:	Tuple										=		{0: '''batch''', 2: encoder_sequence}
															UpperCAmelCase			:	List[Any]										=		{0: '''batch''', 2: encoder_sequence}










'''simple docstring'''






import importlib.metadata
import subprocess
import sys


def 			snake_case_					( _lowerCAmelCase  :						bool = False  )				->       str:
							UpperCAmelCase			:	List[str]										=		False
							if "IPython" in sys.modules:
														UpperCAmelCase			:	Optional[Any]										=		'''google.colab''' in str(sys.modules['''IPython'''].get_ipython()  )

							if in_colab:
														if upgrade:
																					UpperCAmelCase			:	List[Any]										=		['''pip''', '''install''', '''-U''', '''torch''']
																					subprocess.run(_lowerCAmelCase       ,				check=_lowerCAmelCase  )
														# get the current version of torch
														UpperCAmelCase			:	Dict										=		importlib.metadata.version('''torch'''  )
														UpperCAmelCase			:	int										=		torch_version[: torch_version.rindex('''.'''  )]
														UpperCAmelCase			:	Any										=		f"""https://storage.googleapis.com/tpu-pytorch/wheels/colab/torch_xla-{torch_version_trunc}-cp37-cp37m-linux_x86_64.whl"""
														UpperCAmelCase			:	List[str]										=		['''pip''', '''install''', xla_wheel]
														subprocess.run(_lowerCAmelCase       ,				check=_lowerCAmelCase  )
							else:
														raise RuntimeError('''`install_xla` utility works only on google colab.'''  )





