





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


_snake_case           =			logging.get_logger(__name__)

_snake_case           =			{
    """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  lowerCAmelCase							(       lowercase_ ):
  __lowerCamelCase		=      'marian'
  __lowerCamelCase		=      ['past_key_values']
  __lowerCamelCase		=      {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}







  def __init__(      self  :int   ,							_lowercase  :Dict=5_81_01   ,							_lowercase  :int=None   ,							_lowercase  :str=10_24   ,							_lowercase  :Optional[int]=12   ,							_lowercase  :int=40_96   ,							_lowercase  :List[Any]=16   ,							_lowercase  :int=12   ,							_lowercase  :List[Any]=40_96   ,							_lowercase  :List[str]=16   ,							_lowercase  :int=0.0   ,							_lowercase  :str=0.0   ,							_lowercase  :List[Any]=True   ,							_lowercase  :str=True   ,							_lowercase  :Optional[int]="gelu"   ,							_lowercase  :Tuple=10_24   ,							_lowercase  :Union[str, Any]=0.1   ,							_lowercase  :Optional[Any]=0.0   ,							_lowercase  :Dict=0.0   ,							_lowercase  :str=0.02   ,							_lowercase  :Optional[Any]=5_81_00   ,							_lowercase  :Union[str, Any]=False   ,							_lowercase  :Union[str, Any]=5_81_00   ,							_lowercase  :Union[str, Any]=0   ,							_lowercase  :Any=0   ,							_lowercase  :Optional[int]=True   ,							**_lowercase  :int   ,							):






     '''simple docstring'''





     lowercase__			       =  vocab_size
     lowercase__			       =  decoder_vocab_size or vocab_size
     lowercase__			       =  max_position_embeddings
     lowercase__			       =  d_model
     lowercase__			       =  encoder_ffn_dim
     lowercase__			       =  encoder_layers
     lowercase__			       =  encoder_attention_heads
     lowercase__			       =  decoder_ffn_dim
     lowercase__			       =  decoder_layers
     lowercase__			       =  decoder_attention_heads
     lowercase__			       =  dropout
     lowercase__			       =  attention_dropout
     lowercase__			       =  activation_dropout
     lowercase__			       =  activation_function
     lowercase__			       =  init_std
     lowercase__			       =  encoder_layerdrop
     lowercase__			       =  decoder_layerdrop
     lowercase__			       =  use_cache
     lowercase__			       =  encoder_layers
     lowercase__			       =  scale_embedding  # scale factor will be sqrt(d_model) if True
     lowercase__			       =  share_encoder_decoder_embeddings
     super().__init__(
         pad_token_id=_lowercase   ,							eos_token_id=_lowercase   ,							is_encoder_decoder=_lowercase   ,							decoder_start_token_id=_lowercase   ,							forced_eos_token_id=_lowercase   ,							**_lowercase   ,							)





class  lowerCAmelCase							(       lowercase_ ):







  @property
  # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs
  def   UpperCAmelCase					(      self  :List[Any] ):






     '''simple docstring'''





     if self.task in ["default", "seq2seq-lm"]:
        lowercase__			       =  OrderedDict(
            [
                ("input_ids", {0: "batch", 1: "encoder_sequence"}),
                ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
            ] )

        if self.use_past:
           lowercase__			       =  {0: "batch"}
           lowercase__			       =  {0: "batch", 1: "past_decoder_sequence + sequence"}
        else:
           lowercase__			       =  {0: "batch", 1: "decoder_sequence"}
           lowercase__			       =  {0: "batch", 1: "decoder_sequence"}

        if self.use_past:
           self.fill_with_past_key_values_(_lowercase   ,							direction="inputs" )
     elif self.task == "causal-lm":
        # TODO: figure this case out.
        lowercase__			       =  OrderedDict(
            [
                ("input_ids", {0: "batch", 1: "encoder_sequence"}),
                ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
            ] )
        if self.use_past:
           lowercase__       ,							lowercase__			       =  self.num_layers
           for i in range(_lowercase ):
              lowercase__			       =  {0: "batch", 2: "past_sequence + sequence"}
              lowercase__			       =  {0: "batch", 2: "past_sequence + sequence"}
     else:
        lowercase__			       =  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  :int ):






     '''simple docstring'''





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







  def   UpperCAmelCase					(      self  :Optional[int]   ,							_lowercase  :PreTrainedTokenizer   ,							_lowercase  :int = -1   ,							_lowercase  :int = -1   ,							_lowercase  :bool = False   ,							_lowercase  :Optional[TensorType] = None   ,							):






     '''simple docstring'''





     lowercase__			       =  self._generate_dummy_inputs_for_encoder_and_decoder(
         _lowercase   ,							_lowercase   ,							_lowercase   ,							_lowercase   ,							_lowercase )

     # Generate decoder inputs
     lowercase__			       =  seq_length if not self.use_past else 1
     lowercase__			       =  self._generate_dummy_inputs_for_encoder_and_decoder(
         _lowercase   ,							_lowercase   ,							_lowercase   ,							_lowercase   ,							_lowercase )
     lowercase__			       =  {f'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
     lowercase__			       =  dict(**_lowercase   ,							**_lowercase )

     if self.use_past:
        if not is_torch_available():
           raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." )
        else:
           import torch
        lowercase__       ,							lowercase__			       =  common_inputs["input_ids"].shape
        lowercase__			       =  common_inputs["decoder_input_ids"].shape[1]
        lowercase__       ,							lowercase__			       =  self.num_attention_heads
        lowercase__			       =  (
            batch,
            num_encoder_attention_heads,
            encoder_seq_length,
            self._config.hidden_size // num_encoder_attention_heads,
        )
        lowercase__			       =  decoder_seq_length + 3
        lowercase__			       =  (
            batch,
            num_decoder_attention_heads,
            decoder_past_length,
            self._config.hidden_size // num_decoder_attention_heads,
        )

        lowercase__			       =  torch.cat(
            [common_inputs["decoder_attention_mask"], torch.ones(_lowercase   ,							_lowercase )]   ,							dim=1 )

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

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







  def   UpperCAmelCase					(      self  :Tuple   ,							_lowercase  :PreTrainedTokenizer   ,							_lowercase  :int = -1   ,							_lowercase  :int = -1   ,							_lowercase  :bool = False   ,							_lowercase  :Optional[TensorType] = None   ,							):






     '''simple docstring'''





     lowercase__			       =  self._generate_dummy_inputs_for_encoder_and_decoder(
         _lowercase   ,							_lowercase   ,							_lowercase   ,							_lowercase   ,							_lowercase )

     if self.use_past:
        if not is_torch_available():
           raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." )
        else:
           import torch
        lowercase__       ,							lowercase__			       =  common_inputs["input_ids"].shape
        # Not using the same length for past_key_values
        lowercase__			       =  seqlen + 2
        lowercase__       ,							lowercase__			       =  self.num_layers
        lowercase__       ,							lowercase__			       =  self.num_attention_heads
        lowercase__			       =  (
            batch,
            num_encoder_attention_heads,
            past_key_values_length,
            self._config.hidden_size // num_encoder_attention_heads,
        )

        lowercase__			       =  common_inputs["attention_mask"].dtype
        lowercase__			       =  torch.cat(
            [common_inputs["attention_mask"], torch.ones(_lowercase   ,							_lowercase   ,							dtype=_lowercase )]   ,							dim=1 )
        lowercase__			       =  [
            (torch.zeros(_lowercase ), torch.zeros(_lowercase )) for _ in range(_lowercase )
        ]
     return common_inputs







  def   UpperCAmelCase					(      self  :Optional[int]   ,							_lowercase  :PreTrainedTokenizer   ,							_lowercase  :int = -1   ,							_lowercase  :int = -1   ,							_lowercase  :bool = False   ,							_lowercase  :Optional[TensorType] = None   ,							):






     '''simple docstring'''





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

     # Generate dummy inputs according to compute batch and sequence
     lowercase__			       =  [" ".join([tokenizer.unk_token] ) * seq_length] * batch_size
     lowercase__			       =  dict(tokenizer(_lowercase   ,							return_tensors=_lowercase ) )
     return common_inputs







  def   UpperCAmelCase					(      self  :Dict   ,							_lowercase  :PreTrainedTokenizer   ,							_lowercase  :int = -1   ,							_lowercase  :int = -1   ,							_lowercase  :bool = False   ,							_lowercase  :Optional[TensorType] = None   ,							):






     '''simple docstring'''





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

     else:
        lowercase__			       =  self._generate_dummy_inputs_for_causal_lm(
            _lowercase   ,							batch_size=_lowercase   ,							seq_length=_lowercase   ,							is_pair=_lowercase   ,							framework=_lowercase )

     return common_inputs







  def   UpperCAmelCase					(      self  :Any   ,							_lowercase  :Union[str, Any]   ,							_lowercase  :Dict   ,							_lowercase  :str   ,							_lowercase  :Tuple ):






     '''simple docstring'''





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







  @property
  def   UpperCAmelCase					(      self  :int ):






     '''simple docstring'''





     return 1e-4












import math
from typing import List, Optional, Tuple, Union

import numpy as np
import torch

from ..configuration_utils import ConfigMixin, register_to_config
from .scheduling_utils import SchedulerMixin, SchedulerOutput





class  lowerCAmelCase							(       lowercase_							,							lowercase_ ):
  __lowerCamelCase		=      1







  @register_to_config
  def __init__(      self  :Dict   ,							_lowercase  :int = 10_00   ,							_lowercase  :Optional[Union[np.ndarray, List[float]]] = None ):






     '''simple docstring'''





     self.set_timesteps(_lowercase )

     # standard deviation of the initial noise distribution
     lowercase__			       =  1.0

     # For now we only support F-PNDM, i.e. the runge-kutta method
     # For more information on the algorithm please take a look at the paper: https://arxiv.org/pdf/2202.09778.pdf
     # mainly at formula (9), (12), (13) and the Algorithm 2.
     lowercase__			       =  4

     # running values
     lowercase__			       =  []







  def   UpperCAmelCase					(      self  :str   ,							_lowercase  :int   ,							_lowercase  :Union[str, torch.device] = None ):






     '''simple docstring'''





     lowercase__			       =  num_inference_steps
     lowercase__			       =  torch.linspace(1   ,							0   ,							num_inference_steps + 1 )[:-1]
     lowercase__			       =  torch.cat([steps, torch.tensor([0.0] )] )

     if self.config.trained_betas is not None:
        lowercase__			       =  torch.tensor(self.config.trained_betas   ,							dtype=torch.floataa )
     else:
        lowercase__			       =  torch.sin(steps * math.pi / 2 ) ** 2

     lowercase__			       =  (1.0 - self.betas**2) ** 0.5

     lowercase__			       =  (torch.atana(self.betas   ,							self.alphas ) / math.pi * 2)[:-1]
     lowercase__			       =  timesteps.to(_lowercase )

     lowercase__			       =  []







  def   UpperCAmelCase					(      self  :Optional[int]   ,							_lowercase  :torch.FloatTensor   ,							_lowercase  :int   ,							_lowercase  :torch.FloatTensor   ,							_lowercase  :bool = True   ,							):






     '''simple docstring'''





     if self.num_inference_steps is None:
        raise ValueError(
            "Number of inference steps is 'None', you need to run 'set_timesteps' after creating the scheduler" )

     lowercase__			       =  (self.timesteps == timestep).nonzero().item()
     lowercase__			       =  timestep_index + 1

     lowercase__			       =  sample * self.betas[timestep_index] + model_output * self.alphas[timestep_index]
     self.ets.append(_lowercase )

     if len(self.ets ) == 1:
        lowercase__			       =  self.ets[-1]
     elif len(self.ets ) == 2:
        lowercase__			       =  (3 * self.ets[-1] - self.ets[-2]) / 2
     elif len(self.ets ) == 3:
        lowercase__			       =  (23 * self.ets[-1] - 16 * self.ets[-2] + 5 * self.ets[-3]) / 12
     else:
        lowercase__			       =  (1 / 24) * (55 * self.ets[-1] - 59 * self.ets[-2] + 37 * self.ets[-3] - 9 * self.ets[-4])

     lowercase__			       =  self._get_prev_sample(_lowercase   ,							_lowercase   ,							_lowercase   ,							_lowercase )

     if not return_dict:
        return (prev_sample,)

     return SchedulerOutput(prev_sample=_lowercase )







  def   UpperCAmelCase					(      self  :Any   ,							_lowercase  :torch.FloatTensor   ,							*_lowercase  :int   ,							**_lowercase  :int ):






     '''simple docstring'''





     return sample







  def   UpperCAmelCase					(      self  :str   ,							_lowercase  :Tuple   ,							_lowercase  :int   ,							_lowercase  :Optional[Any]   ,							_lowercase  :List[str] ):






     '''simple docstring'''





     lowercase__			       =  self.alphas[timestep_index]
     lowercase__			       =  self.betas[timestep_index]

     lowercase__			       =  self.alphas[prev_timestep_index]
     lowercase__			       =  self.betas[prev_timestep_index]

     lowercase__			       =  (sample - sigma * ets) / max(_lowercase   ,							1e-8 )
     lowercase__			       =  next_alpha * pred + ets * next_sigma

     return prev_sample







  def __len__(      self  :Union[str, Any] ):






     '''simple docstring'''





     return self.config.num_train_timesteps






