





'''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


_lowercase       						=			logging.get_logger(__name__)

_lowercase       						=			{
    '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						(       __a					):
     _UpperCAmelCase										=					'''marian'''
     _UpperCAmelCase										=					['''past_key_values''']
     _UpperCAmelCase										=					{'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}







     def __init__( self    ,   A__=5_81_01    ,   A__=None    ,   A__=10_24    ,   A__=12    ,   A__=40_96    ,   A__=16    ,   A__=12    ,   A__=40_96    ,   A__=16    ,   A__=0.0    ,   A__=0.0    ,   A__=True    ,   A__=True    ,   A__="gelu"    ,   A__=10_24    ,   A__=0.1    ,   A__=0.0    ,   A__=0.0    ,   A__=0.0_2    ,   A__=5_81_00    ,   A__=False    ,   A__=5_81_00    ,   A__=0    ,   A__=0    ,   A__=True    ,   **A__    ,   ) ->	Tuple:
      snake_case				=			vocab_size
      snake_case				=			decoder_vocab_size or vocab_size
      snake_case				=			max_position_embeddings
      snake_case				=			d_model
      snake_case				=			encoder_ffn_dim
      snake_case				=			encoder_layers
      snake_case				=			encoder_attention_heads
      snake_case				=			decoder_ffn_dim
      snake_case				=			decoder_layers
      snake_case				=			decoder_attention_heads
      snake_case				=			dropout
      snake_case				=			attention_dropout
      snake_case				=			activation_dropout
      snake_case				=			activation_function
      snake_case				=			init_std
      snake_case				=			encoder_layerdrop
      snake_case				=			decoder_layerdrop
      snake_case				=			use_cache
      snake_case				=			encoder_layers
      snake_case				=			scale_embedding  # scale factor will be sqrt(d_model) if True
      snake_case				=			share_encoder_decoder_embeddings
      super().__init__(
          pad_token_id=A__    ,   eos_token_id=A__    ,   is_encoder_decoder=A__    ,   decoder_start_token_id=A__    ,   forced_eos_token_id=A__    ,   **A__    ,   )




class 			_lowercase						(       __a					):







     @property
     # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs
     def     UpperCamelCase			( self    ) ->	Mapping[str, Mapping[int, str]]:
      if self.task in ["default", "seq2seq-lm"]:
       snake_case				=			OrderedDict(
           [
               ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
               ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
           ]    )

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

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







     def     UpperCamelCase			( self    ,   A__    ,   A__ = -1    ,   A__ = -1    ,   A__ = False    ,   A__ = None    ,   ) ->	Mapping[str, Any]:
      snake_case				=			self._generate_dummy_inputs_for_encoder_and_decoder(
          A__    ,   A__    ,   A__    ,   A__    ,   A__    )

      # Generate decoder inputs
      snake_case				=			seq_length if not self.use_past else 1
      snake_case				=			self._generate_dummy_inputs_for_encoder_and_decoder(
          A__    ,   A__    ,   A__    ,   A__    ,   A__    )
      snake_case				=			{F"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()}
      snake_case				=			dict(**A__    ,   **A__    )

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

       snake_case				=			torch.cat(
           [common_inputs['''decoder_attention_mask'''], torch.ones(A__    ,   A__    )]    ,   dim=1    )

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

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







     def     UpperCamelCase			( self    ,   A__    ,   A__ = -1    ,   A__ = -1    ,   A__ = False    ,   A__ = None    ,   ) ->	Mapping[str, Any]:
      snake_case				=			self._generate_dummy_inputs_for_encoder_and_decoder(
          A__    ,   A__    ,   A__    ,   A__    ,   A__    )

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

       snake_case				=			common_inputs['''attention_mask'''].dtype
       snake_case				=			torch.cat(
           [common_inputs['''attention_mask'''], torch.ones(A__    ,   A__    ,   dtype=A__    )]    ,   dim=1    )
       snake_case				=			[
           (torch.zeros(A__    ), torch.zeros(A__    )) for _ in range(A__    )
       ]
      return common_inputs







     def     UpperCamelCase			( self    ,   A__    ,   A__ = -1    ,   A__ = -1    ,   A__ = False    ,   A__ = 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
      snake_case				=			compute_effective_axis_dimension(
          A__    ,   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
      snake_case				=			tokenizer.num_special_tokens_to_add(A__    )
      snake_case				=			compute_effective_axis_dimension(
          A__    ,   fixed_dimension=OnnxConfig.default_fixed_sequence    ,   num_token_to_add=A__    )

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







     def     UpperCamelCase			( self    ,   A__    ,   A__ = -1    ,   A__ = -1    ,   A__ = False    ,   A__ = None    ,   ) ->	Mapping[str, Any]:
      if self.task in ["default", "seq2seq-lm"]:
       snake_case				=			self._generate_dummy_inputs_for_default_and_seqaseq_lm(
           A__    ,   batch_size=A__    ,   seq_length=A__    ,   is_pair=A__    ,   framework=A__    )

      else:
       snake_case				=			self._generate_dummy_inputs_for_causal_lm(
           A__    ,   batch_size=A__    ,   seq_length=A__    ,   is_pair=A__    ,   framework=A__    )

      return common_inputs







     def     UpperCamelCase			( self    ,   A__    ,   A__    ,   A__    ,   A__    ) ->	List[str]:
      if self.task in ["default", "seq2seq-lm"]:
       snake_case				=			super()._flatten_past_key_values_(A__    ,   A__    ,   A__    ,   A__    )
      else:
       snake_case				=			super(A__    ,   self    )._flatten_past_key_values_(
           A__    ,   A__    ,   A__    ,   A__    )







     @property
     def     UpperCamelCase			( self    ) ->	float:
      return 1e-4








'''simple docstring'''







from ...utils import is_note_seq_available, is_transformers_available, is_torch_available
from ...utils import OptionalDependencyNotAvailable


try:
   if not (is_transformers_available() and is_torch_available()):
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   from ...utils.dummy_torch_and_transformers_objects import *  # noqa F403
else:
   from .notes_encoder import SpectrogramNotesEncoder
   from .continous_encoder import SpectrogramContEncoder
   from .pipeline_spectrogram_diffusion import (
       SpectrogramContEncoder,
       SpectrogramDiffusionPipeline,
       TaFilmDecoder,
   )

try:
   if not (is_transformers_available() and is_torch_available() and is_note_seq_available()):
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   from ...utils.dummy_transformers_and_torch_and_note_seq_objects import *  # noqa F403
else:
   from .midi_utils import MidiProcessor

