



"""simple docstring"""





from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import TensorType, is_torch_available, logging


lowerCamelCase       	=						logging.get_logger(__name__)

lowerCamelCase       	=						{
    """Helsinki-NLP/opus-mt-en-de""": """https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/config.json""",
    # See all Marian models at https://huggingface.co/models?filter=marian
}
class 				lowercase__       (   SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					'''marian'''
       UpperCamelCase						=					['''past_key_values''']
       UpperCamelCase						=					{'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}



       def __init__(							self      :  int		,    _UpperCAmelCase      :  int=58101		,    _UpperCAmelCase      :  Union[str, Any]=None		,    _UpperCAmelCase      :  Dict=1024		,    _UpperCAmelCase      :  int=12		,    _UpperCAmelCase      :  Any=4096		,    _UpperCAmelCase      :  Any=16		,    _UpperCAmelCase      :  Optional[int]=12		,    _UpperCAmelCase      :  Optional[int]=4096		,    _UpperCAmelCase      :  Union[str, Any]=16		,    _UpperCAmelCase      :  Any=0.0		,    _UpperCAmelCase      :  str=0.0		,    _UpperCAmelCase      :  Tuple=True		,    _UpperCAmelCase      :  List[Any]=True		,    _UpperCAmelCase      :  Any="gelu"		,    _UpperCAmelCase      :  Union[str, Any]=1024		,    _UpperCAmelCase      :  str=0.1		,    _UpperCAmelCase      :  str=0.0		,    _UpperCAmelCase      :  List[str]=0.0		,    _UpperCAmelCase      :  Tuple=0.02		,    _UpperCAmelCase      :  Union[str, Any]=58100		,    _UpperCAmelCase      :  List[str]=False		,    _UpperCAmelCase      :  List[str]=58100		,    _UpperCAmelCase      :  List[Any]=0		,    _UpperCAmelCase      :  Union[str, Any]=0		,    _UpperCAmelCase      :  Any=True		,    **_UpperCAmelCase      :  Tuple		,    )			->						Optional[Any]:


             '''simple docstring'''




             UpperCAmelCase_      			=							vocab_size
             UpperCAmelCase_      			=							decoder_vocab_size or 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
             UpperCAmelCase_      			=							share_encoder_decoder_embeddings
             super().__init__(
                 pad_token_id=_UpperCAmelCase		,    eos_token_id=_UpperCAmelCase		,    is_encoder_decoder=_UpperCAmelCase		,    decoder_start_token_id=_UpperCAmelCase		,    forced_eos_token_id=_UpperCAmelCase		,    **_UpperCAmelCase		,    )



class 				lowercase__       (   SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       @property
       # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs
       def 					lowercase__    (							self      :  Any   )			->						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_(_UpperCAmelCase		,    direction="inputs"   )
             elif self.task == "causal-lm":
                   # TODO: figure this case out.
                   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(_UpperCAmelCase   ):
                               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 					lowercase__    (							self      :  Union[str, Any]   )			->						Mapping[str, Mapping[int, str]]:


             '''simple docstring'''




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



       def 					lowercase__    (							self      :  Optional[int]		,    _UpperCAmelCase      :  PreTrainedTokenizer		,    _UpperCAmelCase      :  int = -1		,    _UpperCAmelCase      :  int = -1		,    _UpperCAmelCase      :  bool = False		,    _UpperCAmelCase      :  Optional[TensorType] = None		,    )			->						Mapping[str, Any]:


             '''simple docstring'''




             UpperCAmelCase_      			=							self._generate_dummy_inputs_for_encoder_and_decoder(
                 _UpperCAmelCase		,    _UpperCAmelCase		,    _UpperCAmelCase		,    _UpperCAmelCase		,    _UpperCAmelCase   )

             # Generate decoder inputs
             UpperCAmelCase_      			=							seq_length if not self.use_past else 1
             UpperCAmelCase_      			=							self._generate_dummy_inputs_for_encoder_and_decoder(
                 _UpperCAmelCase		,    _UpperCAmelCase		,    _UpperCAmelCase		,    _UpperCAmelCase		,    _UpperCAmelCase   )
             UpperCAmelCase_      			=							{F"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()}
             UpperCAmelCase_      			=							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
                   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 + 3
                   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(_UpperCAmelCase		,    _UpperCAmelCase   )]		,    dim=1   )

                   UpperCAmelCase_      			=							[]
                   # If the number of encoder and decoder layers are present in the model configuration, both are considered
                   UpperCAmelCase_			,   UpperCAmelCase_      			=							self.num_layers
                   UpperCAmelCase_      			=							min(_UpperCAmelCase		,    _UpperCAmelCase   )
                   UpperCAmelCase_      			=							max(_UpperCAmelCase		,    _UpperCAmelCase   ) - min_num_layers
                   UpperCAmelCase_      			=							"encoder" if num_encoder_layers > num_decoder_layers else "decoder"

                   for _ in range(_UpperCAmelCase   ):
                         common_inputs["past_key_values"].append(
                             (
                                 torch.zeros(_UpperCAmelCase   ),
                                 torch.zeros(_UpperCAmelCase   ),
                                 torch.zeros(_UpperCAmelCase   ),
                                 torch.zeros(_UpperCAmelCase   ),
                             )   )
                   # TODO: test this.
                   UpperCAmelCase_      			=							encoder_shape if remaining_side_name == "encoder" else decoder_shape
                   for _ in range(_UpperCAmelCase		,    _UpperCAmelCase   ):
                         common_inputs["past_key_values"].append((torch.zeros(_UpperCAmelCase   ), torch.zeros(_UpperCAmelCase   ))   )
             return common_inputs



       def 					lowercase__    (							self      :  Optional[int]		,    _UpperCAmelCase      :  PreTrainedTokenizer		,    _UpperCAmelCase      :  int = -1		,    _UpperCAmelCase      :  int = -1		,    _UpperCAmelCase      :  bool = False		,    _UpperCAmelCase      :  Optional[TensorType] = None		,    )			->						Mapping[str, Any]:


             '''simple docstring'''




             UpperCAmelCase_      			=							self._generate_dummy_inputs_for_encoder_and_decoder(
                 _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
                   UpperCAmelCase_			,   UpperCAmelCase_      			=							common_inputs["input_ids"].shape
                   # Not using the same length for past_key_values
                   UpperCAmelCase_      			=							seqlen + 2
                   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(_UpperCAmelCase		,    _UpperCAmelCase		,    dtype=_UpperCAmelCase   )]		,    dim=1   )
                   UpperCAmelCase_      			=							[
                       (torch.zeros(_UpperCAmelCase   ), torch.zeros(_UpperCAmelCase   )) for _ in range(_UpperCAmelCase   )
                   ]
             return common_inputs



       def 					lowercase__    (							self      :  int		,    _UpperCAmelCase      :  PreTrainedTokenizer		,    _UpperCAmelCase      :  int = -1		,    _UpperCAmelCase      :  int = -1		,    _UpperCAmelCase      :  bool = False		,    _UpperCAmelCase      :  Optional[TensorType] = None		,    )			->						Mapping[str, Any]:


             '''simple docstring'''




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



       def 					lowercase__    (							self      :  List[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"]:
                   UpperCAmelCase_      			=							self._generate_dummy_inputs_for_default_and_seqaseq_lm(
                       _UpperCAmelCase		,    batch_size=_UpperCAmelCase		,    seq_length=_UpperCAmelCase		,    is_pair=_UpperCAmelCase		,    framework=_UpperCAmelCase   )

             else:
                   UpperCAmelCase_      			=							self._generate_dummy_inputs_for_causal_lm(
                       _UpperCAmelCase		,    batch_size=_UpperCAmelCase		,    seq_length=_UpperCAmelCase		,    is_pair=_UpperCAmelCase		,    framework=_UpperCAmelCase   )

             return common_inputs



       def 					lowercase__    (							self      :  Dict		,    _UpperCAmelCase      :  Any		,    _UpperCAmelCase      :  str		,    _UpperCAmelCase      :  str		,    _UpperCAmelCase      :  List[Any]   )			->						List[Any]:


             '''simple docstring'''




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







       @property
       def 					lowercase__    (							self      :  Dict   )			->						float:


             '''simple docstring'''




             return 1e-4









"""simple docstring"""





def        a__     (				lowerCAmelCase__     ,      lowerCAmelCase__				):
      return [sentence[i : i + ngram_size] for i in range(len(lowerCAmelCase__				) - ngram_size + 1				)]


if __name__ == "__main__":
  from doctest import testmod

  testmod()




