






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

lowerCAmelCase_		:			Tuple				   =					{
    '''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				:    Tuple =		'blenderbot'
  _A				:    Dict =		['past_key_values']
  _A				:    int =		{'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}



  def __init__(					self       ,						snake_case__=80_08       ,						snake_case__=1_28       ,						snake_case__=2       ,						snake_case__=1_02_40       ,						snake_case__=32       ,						snake_case__=24       ,						snake_case__=1_02_40       ,						snake_case__=32       ,						snake_case__=0.0       ,						snake_case__=0.0       ,						snake_case__=True       ,						snake_case__=True       ,						snake_case__="gelu"       ,						snake_case__=25_60       ,						snake_case__=0.1       ,						snake_case__=0.0       ,						snake_case__=0.0       ,						snake_case__=0.02       ,						snake_case__=1       ,						snake_case__=False       ,						snake_case__=0       ,						snake_case__=1       ,						snake_case__=2       ,						snake_case__=3       ,						snake_case__=2       ,						**snake_case__       ,						)	->	int:



         """simple docstring"""





         UpperCAmelCase					=	vocab_size
         UpperCAmelCase					=	max_position_embeddings
         UpperCAmelCase					=	d_model
         UpperCAmelCase					=	encoder_ffn_dim
         UpperCAmelCase					=	encoder_layers
         UpperCAmelCase					=	encoder_attention_heads
         UpperCAmelCase					=	decoder_ffn_dim
         UpperCAmelCase					=	decoder_layers
         UpperCAmelCase					=	decoder_attention_heads
         UpperCAmelCase					=	dropout
         UpperCAmelCase					=	attention_dropout
         UpperCAmelCase					=	activation_dropout
         UpperCAmelCase					=	activation_function
         UpperCAmelCase					=	init_std
         UpperCAmelCase					=	encoder_layerdrop
         UpperCAmelCase					=	decoder_layerdrop
         UpperCAmelCase					=	use_cache
         UpperCAmelCase					=	encoder_layers
         UpperCAmelCase					=	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      UpperCamelCase_       (				a_     ):



  @property
  def 						UpperCamelCase_ (					self )	->	Mapping[str, Mapping[int, str]]:



         """simple docstring"""





         if self.task in ["default", "seq2seq-lm"]:
                UpperCAmelCase					=	OrderedDict(
                    [
                        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
                        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
                    ] )
                if self.use_past:
                       UpperCAmelCase					=	{0: """batch"""}
                       UpperCAmelCase					=	{0: """batch""", 1: """past_decoder_sequence + sequence"""}
                else:
                       UpperCAmelCase					=	{0: """batch""", 1: """decoder_sequence"""}
                       UpperCAmelCase					=	{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					=	OrderedDict(
                    [
                        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
                        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
                    ] )
                if self.use_past:
                       UpperCAmelCase			,				UpperCAmelCase					=	self.num_layers
                       for i in range(snake_case__ ):
                              UpperCAmelCase					=	{0: """batch""", 2: """past_sequence + sequence"""}
                              UpperCAmelCase					=	{0: """batch""", 2: """past_sequence + sequence"""}
         else:
                UpperCAmelCase					=	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 						UpperCamelCase_ (					self )	->	Mapping[str, Mapping[int, str]]:



         """simple docstring"""





         if self.task in ["default", "seq2seq-lm"]:
                UpperCAmelCase					=	super().outputs
         else:
                UpperCAmelCase					=	super(snake_case__       ,						self ).outputs
                if self.use_past:
                       UpperCAmelCase			,				UpperCAmelCase					=	self.num_layers
                       for i in range(snake_case__ ):
                              UpperCAmelCase					=	{0: """batch""", 2: """past_sequence + sequence"""}
                              UpperCAmelCase					=	{0: """batch""", 2: """past_sequence + sequence"""}
         return common_outputs



  def 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__ = -1       ,						snake_case__ = -1       ,						snake_case__ = False       ,						snake_case__ = None       ,						)	->	Mapping[str, Any]:



         """simple docstring"""





         UpperCAmelCase					=	self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
             snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__ )
         # Generate decoder inputs
         UpperCAmelCase					=	seq_length if not self.use_past else 1
         UpperCAmelCase					=	self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
             snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__ )
         UpperCAmelCase					=	{f'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
         UpperCAmelCase					=	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					=	common_inputs["""input_ids"""].shape
                UpperCAmelCase					=	common_inputs["""decoder_input_ids"""].shape[1]
                UpperCAmelCase			,				UpperCAmelCase					=	self.num_attention_heads
                UpperCAmelCase					=	(
                    batch,
                    num_encoder_attention_heads,
                    encoder_seq_length,
                    self._config.hidden_size // num_encoder_attention_heads,
                )
                UpperCAmelCase					=	decoder_seq_length
                UpperCAmelCase					=	(
                    batch,
                    num_decoder_attention_heads,
                    decoder_past_length,
                    self._config.hidden_size // num_decoder_attention_heads,
                )
                UpperCAmelCase					=	torch.cat(
                    [common_inputs["""decoder_attention_mask"""], torch.ones(snake_case__       ,						snake_case__ )]       ,						dim=1 )
                UpperCAmelCase					=	[]
                UpperCAmelCase			,				UpperCAmelCase					=	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 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__ = -1       ,						snake_case__ = -1       ,						snake_case__ = False       ,						snake_case__ = None       ,						)	->	Mapping[str, Any]:



         """simple docstring"""





         UpperCAmelCase					=	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					=	common_inputs["""input_ids"""].shape
                UpperCAmelCase					=	seqlen
                UpperCAmelCase			,				UpperCAmelCase					=	self.num_layers
                UpperCAmelCase			,				UpperCAmelCase					=	self.num_attention_heads
                UpperCAmelCase					=	(
                    batch,
                    num_encoder_attention_heads,
                    past_key_values_length,
                    self._config.hidden_size // num_encoder_attention_heads,
                )
                UpperCAmelCase					=	common_inputs["""attention_mask"""].dtype
                UpperCAmelCase					=	torch.cat(
                    [common_inputs["""attention_mask"""], torch.ones(snake_case__       ,						snake_case__       ,						dtype=snake_case__ )]       ,						dim=1 )
                UpperCAmelCase					=	[
                    (torch.zeros(snake_case__ ), torch.zeros(snake_case__ )) for _ in range(snake_case__ )
                ]
         return common_inputs



  def 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__ = -1       ,						snake_case__ = -1       ,						snake_case__ = False       ,						snake_case__ = None       ,						)	->	Mapping[str, Any]:



         """simple docstring"""





         UpperCAmelCase					=	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					=	tokenizer.num_special_tokens_to_add(snake_case__ )
         UpperCAmelCase					=	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					=	[""" """.join([tokenizer.unk_token] ) * seq_length] * batch_size
         UpperCAmelCase					=	dict(tokenizer(snake_case__       ,						return_tensors=snake_case__ ) )
         return common_inputs



  def 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__ = -1       ,						snake_case__ = -1       ,						snake_case__ = False       ,						snake_case__ = None       ,						)	->	Mapping[str, Any]:



         """simple docstring"""





         if self.task in ["default", "seq2seq-lm"]:
                UpperCAmelCase					=	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					=	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					=	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 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__ )	->	Any:



         """simple docstring"""





         if self.task in ["default", "seq2seq-lm"]:
                UpperCAmelCase					=	super()._flatten_past_key_values_(snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__ )
         else:
                UpperCAmelCase					=	super(snake_case__       ,						self )._flatten_past_key_values_(
                    snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__ )



  def 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__ )	->	int:



         """simple docstring"""





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

         UpperCAmelCase					=	"""past_key_values""" if direction == """inputs""" else """present"""
         UpperCAmelCase			,				UpperCAmelCase					=	self.num_layers

         UpperCAmelCase					=	"""past_encoder_sequence"""
         UpperCAmelCase					=	"""past_decoder_sequence""" if direction == """inputs""" else """past_decoder_sequence + sequence"""

         for i in range(snake_case__ ):
                UpperCAmelCase					=	{0: """batch""", 2: decoder_sequence}
                UpperCAmelCase					=	{0: """batch""", 2: decoder_sequence}
                UpperCAmelCase					=	{0: """batch""", 2: encoder_sequence}
                UpperCAmelCase					=	{0: """batch""", 2: encoder_sequence}







'''simple docstring'''







import torch
from torch import autograd





class     _UpperCAmelCase							(						autograd.Function   ):
		@staticmethod
		def   lowerCamelCase__			(  __SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE			):



					'''simple docstring'''

					__lowerCAmelCase											=				inputs.numel()
					__lowerCAmelCase											=				int(0.005 * nb_elems			) + 1
					if sigmoid:
								__lowerCAmelCase											=				(torch.sigmoid(__SCREAMING_SNAKE_CASE			) > threshold).type(inputs.type()			)
					else:
								__lowerCAmelCase											=				(inputs > threshold).type(inputs.type()			)
					if mask.sum() < nb_min:
								# We limit the pruning so that at least 0.5% (half a percent) of the weights are remaining
								__lowerCAmelCase											=				inputs.flatten().kthvalue(max(nb_elems - nb_min,1			)			).values
								__lowerCAmelCase											=				(inputs > k_threshold).type(inputs.type()			)
					return mask







		@staticmethod
		def   lowerCamelCase__			(  __SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE			):



					'''simple docstring'''

					return gradOutput, None, None





class     _UpperCAmelCase							(						autograd.Function   ):
		@staticmethod
		def   lowerCamelCase__			(  __SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE			):



					'''simple docstring'''

					__lowerCAmelCase											=				inputs.clone()
					__lowerCAmelCase						,       __lowerCAmelCase											=				inputs.flatten().sort(descending=__SCREAMING_SNAKE_CASE			)
					__lowerCAmelCase											=				int(threshold * inputs.numel()			)

					# flat_out and mask access the same memory.
					__lowerCAmelCase											=				mask.flatten()
					__lowerCAmelCase											=				0
					__lowerCAmelCase											=				1
					return mask







		@staticmethod
		def   lowerCamelCase__			(  __SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE			):



					'''simple docstring'''

					return gradOutput, None





class     _UpperCAmelCase							(						lowerCAmelCase_   ):
		@staticmethod
		def   lowerCamelCase__			(  __SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE			):



					'''simple docstring'''

					__lowerCAmelCase											=				inputs.clone()
					__lowerCAmelCase						,       __lowerCAmelCase											=				inputs.abs().flatten().sort(descending=__SCREAMING_SNAKE_CASE			)
					__lowerCAmelCase											=				int(threshold * inputs.numel()			)

					# flat_out and mask access the same memory.
					__lowerCAmelCase											=				mask.flatten()
					__lowerCAmelCase											=				0
					__lowerCAmelCase											=				1
					return mask

