
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      :					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					):


  '''simple docstring'''







  a__   	=		"""blenderbot"""
  a__   	=		["""past_key_values"""]
  a__   	=		{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}
  def __init__(			self      :			str      ,							UpperCamelCase__      :			Optional[Any]=8008      ,							UpperCamelCase__      :			List[Any]=128      ,							UpperCamelCase__      :			List[Any]=2      ,							UpperCamelCase__      :			Dict=1_0240      ,							UpperCamelCase__      :			str=32      ,							UpperCamelCase__      :			List[Any]=24      ,							UpperCamelCase__      :			Dict=1_0240      ,							UpperCamelCase__      :			Dict=32      ,							UpperCamelCase__      :			Tuple=0.0      ,							UpperCamelCase__      :			Optional[int]=0.0      ,							UpperCamelCase__      :			Any=True      ,							UpperCamelCase__      :			int=True      ,							UpperCamelCase__      :			Optional[int]="gelu"      ,							UpperCamelCase__      :			Any=2560      ,							UpperCamelCase__      :			Tuple=0.1      ,							UpperCamelCase__      :			int=0.0      ,							UpperCamelCase__      :			int=0.0      ,							UpperCamelCase__      :			Optional[int]=0.02      ,							UpperCamelCase__      :			Any=1      ,							UpperCamelCase__      :			str=False      ,							UpperCamelCase__      :			Dict=0      ,							UpperCamelCase__      :			Tuple=1      ,							UpperCamelCase__      :			Any=2      ,							UpperCamelCase__      :			List[str]=3      ,							UpperCamelCase__      :			List[Any]=2      ,							**UpperCamelCase__      :			str      ,							)      ->     int:







    """simple docstring"""






    __magic_name__    					=	vocab_size
    __magic_name__    					=	max_position_embeddings
    __magic_name__    					=	d_model
    __magic_name__    					=	encoder_ffn_dim
    __magic_name__    					=	encoder_layers
    __magic_name__    					=	encoder_attention_heads
    __magic_name__    					=	decoder_ffn_dim
    __magic_name__    					=	decoder_layers
    __magic_name__    					=	decoder_attention_heads
    __magic_name__    					=	dropout
    __magic_name__    					=	attention_dropout
    __magic_name__    					=	activation_dropout
    __magic_name__    					=	activation_function
    __magic_name__    					=	init_std
    __magic_name__    					=	encoder_layerdrop
    __magic_name__    					=	decoder_layerdrop
    __magic_name__    					=	use_cache
    __magic_name__    					=	encoder_layers
    __magic_name__    					=	scale_embedding  # scale factor will be sqrt(d_model) if True

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







class 	UpperCAmelCase_      (			_A					):


  '''simple docstring'''







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







    """simple docstring"""






    if self.task in ["default", "seq2seq-lm"]:
      __magic_name__    					=	OrderedDict(
          [
              ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
              ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
          ]   )
      if self.use_past:
        __magic_name__    					=	{0: """batch"""}
        __magic_name__    					=	{0: """batch""", 1: """past_decoder_sequence + sequence"""}
      else:
        __magic_name__    					=	{0: """batch""", 1: """decoder_sequence"""}
        __magic_name__    					=	{0: """batch""", 1: """decoder_sequence"""}
      if self.use_past:
        self.fill_with_past_key_values_(UpperCamelCase__      ,							direction="""inputs"""   )
    elif self.task == "causal-lm":
      __magic_name__    					=	OrderedDict(
          [
              ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
              ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
          ]   )
      if self.use_past:
        __magic_name__					,   __magic_name__    					=	self.num_layers
        for i in range(UpperCamelCase__   ):
          __magic_name__    					=	{0: """batch""", 2: """past_sequence + sequence"""}
          __magic_name__    					=	{0: """batch""", 2: """past_sequence + sequence"""}
    else:
      __magic_name__    					=	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 							_lowercase							(			self      :			Optional[Any]   )      ->     Mapping[str, Mapping[int, str]]:







    """simple docstring"""






    if self.task in ["default", "seq2seq-lm"]:
      __magic_name__    					=	super().outputs
    else:
      __magic_name__    					=	super(UpperCamelCase__      ,							self   ).outputs
      if self.use_past:
        __magic_name__					,   __magic_name__    					=	self.num_layers
        for i in range(UpperCamelCase__   ):
          __magic_name__    					=	{0: """batch""", 2: """past_sequence + sequence"""}
          __magic_name__    					=	{0: """batch""", 2: """past_sequence + sequence"""}
    return common_outputs
  def 							_lowercase							(			self      :			Tuple      ,							UpperCamelCase__      :			PreTrainedTokenizer      ,							UpperCamelCase__      :			int = -1      ,							UpperCamelCase__      :			int = -1      ,							UpperCamelCase__      :			bool = False      ,							UpperCamelCase__      :			Optional[TensorType] = None      ,							)      ->     Mapping[str, Any]:







    """simple docstring"""






    __magic_name__    					=	self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
        UpperCamelCase__      ,							UpperCamelCase__      ,							UpperCamelCase__      ,							UpperCamelCase__      ,							UpperCamelCase__   )
    # Generate decoder inputs
    __magic_name__    					=	seq_length if not self.use_past else 1
    __magic_name__    					=	self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
        UpperCamelCase__      ,							UpperCamelCase__      ,							UpperCamelCase__      ,							UpperCamelCase__      ,							UpperCamelCase__   )
    __magic_name__    					=	{F'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
    __magic_name__    					=	dict(**UpperCamelCase__      ,							**UpperCamelCase__   )

    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__    					=	common_inputs["""input_ids"""].shape
      __magic_name__    					=	common_inputs["""decoder_input_ids"""].shape[1]
      __magic_name__					,   __magic_name__    					=	self.num_attention_heads
      __magic_name__    					=	(
          batch,
          num_encoder_attention_heads,
          encoder_seq_length,
          self._config.hidden_size // num_encoder_attention_heads,
      )
      __magic_name__    					=	decoder_seq_length
      __magic_name__    					=	(
          batch,
          num_decoder_attention_heads,
          decoder_past_length,
          self._config.hidden_size // num_decoder_attention_heads,
      )
      __magic_name__    					=	torch.cat(
          [common_inputs["""decoder_attention_mask"""], torch.ones(UpperCamelCase__      ,							UpperCamelCase__   )]      ,							dim=1   )
      __magic_name__    					=	[]
      __magic_name__					,   __magic_name__    					=	self.num_layers

      for _ in range(UpperCamelCase__   ):
        common_inputs["past_key_values"].append(
            (
                torch.zeros(UpperCamelCase__   ),
                torch.zeros(UpperCamelCase__   ),
                torch.zeros(UpperCamelCase__   ),
                torch.zeros(UpperCamelCase__   ),
            )   )
    return common_inputs
  def 							_lowercase							(			self      :			Dict      ,							UpperCamelCase__      :			PreTrainedTokenizer      ,							UpperCamelCase__      :			int = -1      ,							UpperCamelCase__      :			int = -1      ,							UpperCamelCase__      :			bool = False      ,							UpperCamelCase__      :			Optional[TensorType] = None      ,							)      ->     Mapping[str, Any]:







    """simple docstring"""






    __magic_name__    					=	self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
        UpperCamelCase__      ,							UpperCamelCase__      ,							UpperCamelCase__      ,							UpperCamelCase__      ,							UpperCamelCase__   )

    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__    					=	common_inputs["""input_ids"""].shape
      __magic_name__    					=	seqlen
      __magic_name__					,   __magic_name__    					=	self.num_layers
      __magic_name__					,   __magic_name__    					=	self.num_attention_heads
      __magic_name__    					=	(
          batch,
          num_encoder_attention_heads,
          past_key_values_length,
          self._config.hidden_size // num_encoder_attention_heads,
      )
      __magic_name__    					=	common_inputs["""attention_mask"""].dtype
      __magic_name__    					=	torch.cat(
          [common_inputs["""attention_mask"""], torch.ones(UpperCamelCase__      ,							UpperCamelCase__      ,							dtype=UpperCamelCase__   )]      ,							dim=1   )
      __magic_name__    					=	[
          (torch.zeros(UpperCamelCase__   ), torch.zeros(UpperCamelCase__   )) for _ in range(UpperCamelCase__   )
      ]
    return common_inputs
  def 							_lowercase							(			self      :			List[str]      ,							UpperCamelCase__      :			PreTrainedTokenizer      ,							UpperCamelCase__      :			int = -1      ,							UpperCamelCase__      :			int = -1      ,							UpperCamelCase__      :			bool = False      ,							UpperCamelCase__      :			Optional[TensorType] = None      ,							)      ->     Mapping[str, Any]:







    """simple docstring"""






    __magic_name__    					=	compute_effective_axis_dimension(
        UpperCamelCase__      ,							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__    					=	tokenizer.num_special_tokens_to_add(UpperCamelCase__   )
    __magic_name__    					=	compute_effective_axis_dimension(
        UpperCamelCase__      ,							fixed_dimension=OnnxConfig.default_fixed_sequence      ,							num_token_to_add=UpperCamelCase__   )

    # Generate dummy inputs according to compute batch and sequence
    __magic_name__    					=	[""" """.join([tokenizer.unk_token]   ) * seq_length] * batch_size
    __magic_name__    					=	dict(tokenizer(UpperCamelCase__      ,							return_tensors=UpperCamelCase__   )   )
    return common_inputs
  def 							_lowercase							(			self      :			Any      ,							UpperCamelCase__      :			PreTrainedTokenizer      ,							UpperCamelCase__      :			int = -1      ,							UpperCamelCase__      :			int = -1      ,							UpperCamelCase__      :			bool = False      ,							UpperCamelCase__      :			Optional[TensorType] = None      ,							)      ->     Mapping[str, Any]:







    """simple docstring"""






    if self.task in ["default", "seq2seq-lm"]:
      __magic_name__    					=	self._generate_dummy_inputs_for_default_and_seqaseq_lm(
          UpperCamelCase__      ,							batch_size=UpperCamelCase__      ,							seq_length=UpperCamelCase__      ,							is_pair=UpperCamelCase__      ,							framework=UpperCamelCase__   )

    elif self.task == "causal-lm":
      __magic_name__    					=	self._generate_dummy_inputs_for_causal_lm(
          UpperCamelCase__      ,							batch_size=UpperCamelCase__      ,							seq_length=UpperCamelCase__      ,							is_pair=UpperCamelCase__      ,							framework=UpperCamelCase__   )
    else:
      __magic_name__    					=	self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
          UpperCamelCase__      ,							batch_size=UpperCamelCase__      ,							seq_length=UpperCamelCase__      ,							is_pair=UpperCamelCase__      ,							framework=UpperCamelCase__   )

    return common_inputs
  def 							_lowercase							(			self      :			Optional[int]      ,							UpperCamelCase__      :			List[Any]      ,							UpperCamelCase__      :			Optional[Any]      ,							UpperCamelCase__      :			Union[str, Any]      ,							UpperCamelCase__      :			Dict   )      ->     List[Any]:







    """simple docstring"""






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





  def 							_lowercase							(			self      :			str      ,							UpperCamelCase__      :			Mapping[str, Mapping[int, str]]      ,							UpperCamelCase__      :			str   )      ->     Tuple:







    """simple docstring"""






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

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

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

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



import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
    import transformer_engine.pytorch as te

def a__   (	A_,					A_=True,					A_=True,					A_=True ):






  '''simple docstring'''





  if not is_fpa_available():
    raise ImportError("""Using `convert_model` requires transformer_engine to be installed.""" )
  for name, module in model.named_children():
    if isinstance(A_,					nn.Linear ) and to_transformer_engine and _convert_linear:
      # Return early if the linear layer weights are not multiples of 16
      if any(p % 16 != 0 for p in module.weight.shape ):
        return
      __magic_name__    					=	module.bias is not None
      __magic_name__    					=	te.Linear(
          module.in_features,					module.out_features,					bias=A_,					params_dtype=module.weight.dtype )
      __magic_name__    					=	module.weight.data.clone()
      if has_bias:
        __magic_name__    					=	module.bias.data.clone()

      setattr(A_,					A_,					A_ )
    elif isinstance(A_,					nn.LayerNorm ) and to_transformer_engine and _convert_ln:
      __magic_name__    					=	te.LayerNorm(module.normalized_shape[0],					eps=module.eps,					params_dtype=module.weight.dtype )
      __magic_name__    					=	module.weight.data.clone()
      __magic_name__    					=	module.bias.data.clone()

      setattr(A_,					A_,					A_ )
    elif isinstance(A_,					te.Linear ) and not to_transformer_engine and _convert_linear:
      __magic_name__    					=	module.bias is not None
      __magic_name__    					=	nn.Linear(
          module.in_features,					module.out_features,					bias=A_,					params_dtype=module.weight.dtype )
      __magic_name__    					=	module.weight.data.clone()
      if has_bias:
        __magic_name__    					=	module.bias.data.clone()

      setattr(A_,					A_,					A_ )
    elif isinstance(A_,					te.LayerNorm ) and not to_transformer_engine and _convert_ln:
      __magic_name__    					=	nn.LayerNorm(module.normalized_shape[0],					eps=module.eps,					params_dtype=module.weight.dtype )
      __magic_name__    					=	module.weight.data.clone()
      __magic_name__    					=	module.bias.data.clone()

      setattr(A_,					A_,					A_ )
    else:
      convert_model(
          A_,					to_transformer_engine=A_,					_convert_linear=_convert_linear,					_convert_ln=_convert_ln,					)





def a__   (	A_ ):






  '''simple docstring'''





  if not is_fpa_available():
    raise ImportError("""Using `has_transformer_engine_layers` requires transformer_engine to be installed.""" )
  for m in model.modules():
    if isinstance(A_,					(te.LayerNorm, te.Linear) ):
      return True
  return False

