






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


UpperCamelCase      					=logging.get_logger(__name__)

UpperCamelCase      					={
    "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      A     (   SCREAMING_SNAKE_CASE__    ):






 """simple docstring"""





 __a      :    int			  =    '''marian'''
 __a      :    List[str]			  =    ['''past_key_values''']
 __a      :    Dict			  =    {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}
 def __init__(				self      ,  __lowerCAmelCase=5_81_01      ,  __lowerCAmelCase=None      ,  __lowerCAmelCase=10_24      ,  __lowerCAmelCase=12      ,  __lowerCAmelCase=40_96      ,  __lowerCAmelCase=16      ,  __lowerCAmelCase=12      ,  __lowerCAmelCase=40_96      ,  __lowerCAmelCase=16      ,  __lowerCAmelCase=0.0      ,  __lowerCAmelCase=0.0      ,  __lowerCAmelCase=True      ,  __lowerCAmelCase=True      ,  __lowerCAmelCase="gelu"      ,  __lowerCAmelCase=10_24      ,  __lowerCAmelCase=0.1      ,  __lowerCAmelCase=0.0      ,  __lowerCAmelCase=0.0      ,  __lowerCAmelCase=0.02      ,  __lowerCAmelCase=5_81_00      ,  __lowerCAmelCase=False      ,  __lowerCAmelCase=5_81_00      ,  __lowerCAmelCase=0      ,  __lowerCAmelCase=0      ,  __lowerCAmelCase=True      ,  **__lowerCAmelCase      ,  ):
       UpperCamelCase_					: Tuple            =				vocab_size
       UpperCamelCase_					: Any            =				decoder_vocab_size or vocab_size
       UpperCamelCase_					: int            =				max_position_embeddings
       UpperCamelCase_					: Optional[int]            =				d_model
       UpperCamelCase_					: Optional[Any]            =				encoder_ffn_dim
       UpperCamelCase_					: List[Any]            =				encoder_layers
       UpperCamelCase_					: List[Any]            =				encoder_attention_heads
       UpperCamelCase_					: str            =				decoder_ffn_dim
       UpperCamelCase_					: Union[str, Any]            =				decoder_layers
       UpperCamelCase_					: Dict            =				decoder_attention_heads
       UpperCamelCase_					: List[str]            =				dropout
       UpperCamelCase_					: int            =				attention_dropout
       UpperCamelCase_					: List[str]            =				activation_dropout
       UpperCamelCase_					: Dict            =				activation_function
       UpperCamelCase_					: Union[str, Any]            =				init_std
       UpperCamelCase_					: int            =				encoder_layerdrop
       UpperCamelCase_					: Union[str, Any]            =				decoder_layerdrop
       UpperCamelCase_					: Any            =				use_cache
       UpperCamelCase_					: Dict            =				encoder_layers
       UpperCamelCase_					: Any            =				scale_embedding  # scale factor will be sqrt(d_model) if True
       UpperCamelCase_					: Union[str, Any]            =				share_encoder_decoder_embeddings
       super().__init__(
           pad_token_id=__lowerCAmelCase      ,  eos_token_id=__lowerCAmelCase      ,  is_encoder_decoder=__lowerCAmelCase      ,  decoder_start_token_id=__lowerCAmelCase      ,  forced_eos_token_id=__lowerCAmelCase      ,  **__lowerCAmelCase      ,  )






class      A     (   SCREAMING_SNAKE_CASE__    ):






 """simple docstring"""





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

             if self.use_past:
                   UpperCamelCase_					: int            =				{0: """batch"""}
                   UpperCamelCase_					: str            =				{0: """batch""", 1: """past_decoder_sequence + sequence"""}
             else:
                   UpperCamelCase_					: Union[str, Any]            =				{0: """batch""", 1: """decoder_sequence"""}
                   UpperCamelCase_					: Optional[int]            =				{0: """batch""", 1: """decoder_sequence"""}

             if self.use_past:
                   self.fill_with_past_key_values_(__lowerCAmelCase      ,  direction="""inputs"""			)
       elif self.task == "causal-lm":
             # TODO: figure this case out.
             UpperCamelCase_					: Optional[int]            =				OrderedDict(
                 [
                     ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
                     ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
                 ]			)
             if self.use_past:
                   UpperCamelCase_     ,      UpperCamelCase_					: int            =				self.num_layers
                   for i in range(__lowerCAmelCase			):
                         UpperCamelCase_					: int            =				{0: """batch""", 2: """past_sequence + sequence"""}
                         UpperCamelCase_					: Optional[Any]            =				{0: """batch""", 2: """past_sequence + sequence"""}
       else:
             UpperCamelCase_					: Tuple            =				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			):
       if self.task in ["default", "seq2seq-lm"]:
             UpperCamelCase_					: List[Any]            =				super().outputs
       else:
             UpperCamelCase_					: str            =				super(__lowerCAmelCase      ,  self			).outputs
             if self.use_past:
                   UpperCamelCase_     ,      UpperCamelCase_					: Union[str, Any]            =				self.num_layers
                   for i in range(__lowerCAmelCase			):
                         UpperCamelCase_					: Dict            =				{0: """batch""", 2: """past_sequence + sequence"""}
                         UpperCamelCase_					: int            =				{0: """batch""", 2: """past_sequence + sequence"""}
       return common_outputs
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase = -1      ,  __lowerCAmelCase = -1      ,  __lowerCAmelCase = False      ,  __lowerCAmelCase = None      ,  ):
       UpperCamelCase_					: Tuple            =				self._generate_dummy_inputs_for_encoder_and_decoder(
           __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase			)

       # Generate decoder inputs
       UpperCamelCase_					: int            =				seq_length if not self.use_past else 1
       UpperCamelCase_					: Any            =				self._generate_dummy_inputs_for_encoder_and_decoder(
           __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase			)
       UpperCamelCase_					: List[Any]            =				{F"decoder_{name}": tensor for name, tensor in decoder_inputs.items()}
       UpperCamelCase_					: Any            =				dict(**__lowerCAmelCase      ,  **__lowerCAmelCase			)

       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_					: int            =				common_inputs["""input_ids"""].shape
             UpperCamelCase_					: int            =				common_inputs["""decoder_input_ids"""].shape[1]
             UpperCamelCase_     ,      UpperCamelCase_					: Any            =				self.num_attention_heads
             UpperCamelCase_					: List[Any]            =				(
                 batch,
                 num_encoder_attention_heads,
                 encoder_seq_length,
                 self._config.hidden_size // num_encoder_attention_heads,
             )
             UpperCamelCase_					: List[str]            =				decoder_seq_length + 3
             UpperCamelCase_					: Optional[int]            =				(
                 batch,
                 num_decoder_attention_heads,
                 decoder_past_length,
                 self._config.hidden_size // num_decoder_attention_heads,
             )

             UpperCamelCase_					: Dict            =				torch.cat(
                 [common_inputs["""decoder_attention_mask"""], torch.ones(__lowerCAmelCase      ,  __lowerCAmelCase			)]      ,  dim=1			)

             UpperCamelCase_					: List[str]            =				[]
             # If the number of encoder and decoder layers are present in the model configuration, both are considered
             UpperCamelCase_     ,      UpperCamelCase_					: List[str]            =				self.num_layers
             UpperCamelCase_					: Dict            =				min(__lowerCAmelCase      ,  __lowerCAmelCase			)
             UpperCamelCase_					: List[str]            =				max(__lowerCAmelCase      ,  __lowerCAmelCase			) - min_num_layers
             UpperCamelCase_					: Optional[int]            =				"""encoder""" if num_encoder_layers > num_decoder_layers else """decoder"""

             for _ in range(__lowerCAmelCase			):
                   common_inputs["past_key_values"].append(
                       (
                           torch.zeros(__lowerCAmelCase			),
                           torch.zeros(__lowerCAmelCase			),
                           torch.zeros(__lowerCAmelCase			),
                           torch.zeros(__lowerCAmelCase			),
                       )			)
             # TODO: test this.
             UpperCamelCase_					: Any            =				encoder_shape if remaining_side_name == """encoder""" else decoder_shape
             for _ in range(__lowerCAmelCase      ,  __lowerCAmelCase			):
                   common_inputs["past_key_values"].append((torch.zeros(__lowerCAmelCase			), torch.zeros(__lowerCAmelCase			))			)
       return common_inputs
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase = -1      ,  __lowerCAmelCase = -1      ,  __lowerCAmelCase = False      ,  __lowerCAmelCase = None      ,  ):
       UpperCamelCase_					: Any            =				self._generate_dummy_inputs_for_encoder_and_decoder(
           __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase			)

       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_					: int            =				common_inputs["""input_ids"""].shape
             # Not using the same length for past_key_values
             UpperCamelCase_					: Union[str, Any]            =				seqlen + 2
             UpperCamelCase_     ,      UpperCamelCase_					: str            =				self.num_layers
             UpperCamelCase_     ,      UpperCamelCase_					: Union[str, Any]            =				self.num_attention_heads
             UpperCamelCase_					: Union[str, Any]            =				(
                 batch,
                 num_encoder_attention_heads,
                 past_key_values_length,
                 self._config.hidden_size // num_encoder_attention_heads,
             )

             UpperCamelCase_					: Dict            =				common_inputs["""attention_mask"""].dtype
             UpperCamelCase_					: int            =				torch.cat(
                 [common_inputs["""attention_mask"""], torch.ones(__lowerCAmelCase      ,  __lowerCAmelCase      ,  dtype=__lowerCAmelCase			)]      ,  dim=1			)
             UpperCamelCase_					: Any            =				[
                 (torch.zeros(__lowerCAmelCase			), torch.zeros(__lowerCAmelCase			)) for _ in range(__lowerCAmelCase			)
             ]
       return common_inputs
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase = -1      ,  __lowerCAmelCase = -1      ,  __lowerCAmelCase = False      ,  __lowerCAmelCase = None      ,  ):
       # 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
       UpperCamelCase_					: Tuple            =				compute_effective_axis_dimension(
           __lowerCAmelCase      ,  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_					: Tuple            =				tokenizer.num_special_tokens_to_add(__lowerCAmelCase			)
       UpperCamelCase_					: Any            =				compute_effective_axis_dimension(
           __lowerCAmelCase      ,  fixed_dimension=OnnxConfig.default_fixed_sequence      ,  num_token_to_add=__lowerCAmelCase			)

       # Generate dummy inputs according to compute batch and sequence
       UpperCamelCase_					: Union[str, Any]            =				[""" """.join([tokenizer.unk_token]			) * seq_length] * batch_size
       UpperCamelCase_					: Dict            =				dict(tokenizer(__lowerCAmelCase      ,  return_tensors=__lowerCAmelCase			)			)
       return common_inputs
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase = -1      ,  __lowerCAmelCase = -1      ,  __lowerCAmelCase = False      ,  __lowerCAmelCase = None      ,  ):
       if self.task in ["default", "seq2seq-lm"]:
             UpperCamelCase_					: Optional[int]            =				self._generate_dummy_inputs_for_default_and_seqaseq_lm(
                 __lowerCAmelCase      ,  batch_size=__lowerCAmelCase      ,  seq_length=__lowerCAmelCase      ,  is_pair=__lowerCAmelCase      ,  framework=__lowerCAmelCase			)

       else:
             UpperCamelCase_					: Union[str, Any]            =				self._generate_dummy_inputs_for_causal_lm(
                 __lowerCAmelCase      ,  batch_size=__lowerCAmelCase      ,  seq_length=__lowerCAmelCase      ,  is_pair=__lowerCAmelCase      ,  framework=__lowerCAmelCase			)

       return common_inputs
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase			):
       if self.task in ["default", "seq2seq-lm"]:
             UpperCamelCase_					: Any            =				super()._flatten_past_key_values_(__lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase			)
       else:
             UpperCamelCase_					: int            =				super(__lowerCAmelCase      ,  self			)._flatten_past_key_values_(
                 __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase			)





 @property
 def 	_UpperCAmelCase							(				self			):
       return 1E-4














'''simple docstring'''

from typing import List, Optional, Union

import numpy as np
import torch
import torchaudio.compliance.kaldi as ta_kaldi

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


UpperCamelCase      					=logging.get_logger(__name__)




class      A     (   SCREAMING_SNAKE_CASE__    ):






 """simple docstring"""





 __a      :    List[Any]			  =    ['''input_features''', '''attention_mask''']
 def __init__(				self      ,  __lowerCAmelCase=80      ,  __lowerCAmelCase=1_60_00      ,  __lowerCAmelCase=80      ,  __lowerCAmelCase=0.0      ,  __lowerCAmelCase=True      ,  __lowerCAmelCase=True      ,  __lowerCAmelCase=True      ,  **__lowerCAmelCase      ,  ):
       super().__init__(feature_size=__lowerCAmelCase      ,  sampling_rate=__lowerCAmelCase      ,  padding_value=__lowerCAmelCase      ,  **__lowerCAmelCase			)
       UpperCamelCase_					: int            =				num_mel_bins
       UpperCamelCase_					: Union[str, Any]            =				do_ceptral_normalize
       UpperCamelCase_					: Any            =				normalize_means
       UpperCamelCase_					: int            =				normalize_vars
       UpperCamelCase_					: int            =				True
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  ):
       UpperCamelCase_					: Optional[Any]            =				waveform * (2**15)  # Kaldi compliance: 16-bit signed integers
       UpperCamelCase_					: Optional[int]            =				torch.from_numpy(__lowerCAmelCase			).unsqueeze(0			)
       UpperCamelCase_					: Tuple            =				ta_kaldi.fbank(__lowerCAmelCase      ,  num_mel_bins=self.num_mel_bins      ,  sample_frequency=self.sampling_rate			)
       return features.numpy()
 @staticmethod
 def 	_UpperCAmelCase							(				__lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase = True      ,  __lowerCAmelCase = True      ,  __lowerCAmelCase = 0.0      ,  ):
       # make sure we normalize float32 arrays
       if normalize_means:
             UpperCamelCase_					: int            =				x[:input_length].mean(axis=0			)
             UpperCamelCase_					: str            =				np.subtract(__lowerCAmelCase      ,  __lowerCAmelCase			)
       if normalize_vars:
             UpperCamelCase_					: Optional[int]            =				x[:input_length].std(axis=0			)
             UpperCamelCase_					: Optional[Any]            =				np.divide(__lowerCAmelCase      ,  __lowerCAmelCase			)

       if input_length < x.shape[0]:
             UpperCamelCase_					: Any            =				padding_value

       # make sure array is in float32
       UpperCamelCase_					: str            =				x.astype(np.floataa			)

       return x
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase = None			):
       UpperCamelCase_					: List[str]            =				attention_mask.sum(-1			) if attention_mask is not None else [x.shape[0] for x in input_features]
       return [
           self.utterance_cmvn(__lowerCAmelCase      ,  __lowerCAmelCase      ,  self.normalize_means      ,  self.normalize_vars      ,  self.padding_value			)
           for x, n in zip(__lowerCAmelCase      ,  __lowerCAmelCase			)
       ]





 def __call__(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase = False      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = False      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = None      ,  **__lowerCAmelCase      ,  ):

       if sampling_rate is not None:
             if sampling_rate != self.sampling_rate:
                   raise ValueError(
                       F"The model corresponding to this feature extractor: {self} was trained using a sampling rate of"
                       F" {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with"
                       F" {self.sampling_rate} and not {sampling_rate}."			)
       else:
             logger.warning(
                 """It is strongly recommended to pass the `sampling_rate` argument to this function. """
                 """Failing to do so can result in silent errors that might be hard to debug."""			)

       UpperCamelCase_					: Optional[int]            =				isinstance(__lowerCAmelCase      ,  np.ndarray			) and len(raw_speech.shape			) > 1
       if is_batched_numpy and len(raw_speech.shape			) > 2:
             raise ValueError(F"Only mono-channel audio is supported for input to {self}"			)
       UpperCamelCase_					: List[str]            =				is_batched_numpy or (
           isinstance(__lowerCAmelCase      ,  (list, tuple)			) and (isinstance(raw_speech[0]      ,  (np.ndarray, tuple, list)			))
       )

       if is_batched:
             UpperCamelCase_					: str            =				[np.asarray(__lowerCAmelCase      ,  dtype=np.floataa			) for speech in raw_speech]
       elif not is_batched and not isinstance(__lowerCAmelCase      ,  np.ndarray			):
             UpperCamelCase_					: Optional[Any]            =				np.asarray(__lowerCAmelCase      ,  dtype=np.floataa			)
       elif isinstance(__lowerCAmelCase      ,  np.ndarray			) and raw_speech.dtype is np.dtype(np.floataa			):
             UpperCamelCase_					: int            =				raw_speech.astype(np.floataa			)

       # always return batch
       if not is_batched:
             UpperCamelCase_					: Tuple            =				[raw_speech]

       # extract fbank features
       UpperCamelCase_					: Any            =				[self._extract_fbank_features(__lowerCAmelCase			) for waveform in raw_speech]

       # convert into correct format for padding
       UpperCamelCase_					: Tuple            =				BatchFeature({"""input_features""": features}			)

       UpperCamelCase_					: Tuple            =				self.pad(
           __lowerCAmelCase      ,  padding=__lowerCAmelCase      ,  max_length=__lowerCAmelCase      ,  truncation=__lowerCAmelCase      ,  pad_to_multiple_of=__lowerCAmelCase      ,  return_attention_mask=__lowerCAmelCase      ,  **__lowerCAmelCase      ,  )

       # make sure list is in array format
       UpperCamelCase_					: str            =				padded_inputs.get("""input_features"""			)
       if isinstance(input_features[0]      ,  __lowerCAmelCase			):
             UpperCamelCase_					: Any            =				[np.asarray(__lowerCAmelCase      ,  dtype=np.floataa			) for feature in input_features]

       UpperCamelCase_					: Union[str, Any]            =				padded_inputs.get("""attention_mask"""			)
       if attention_mask is not None:
             UpperCamelCase_					: Union[str, Any]            =				[np.asarray(__lowerCAmelCase      ,  dtype=np.intaa			) for array in attention_mask]

       # Utterance-level cepstral mean and variance normalization
       if self.do_ceptral_normalize:
             UpperCamelCase_					: List[str]            =				(
                 np.array(__lowerCAmelCase      ,  dtype=np.intaa			)
                 if self._get_padding_strategies(__lowerCAmelCase      ,  max_length=__lowerCAmelCase			) is not PaddingStrategy.DO_NOT_PAD
                 else None
             )
             UpperCamelCase_					: Optional[int]            =				self.normalize(
                 padded_inputs["""input_features"""]      ,  attention_mask=__lowerCAmelCase			)

       if return_tensors is not None:
             UpperCamelCase_					: Optional[Any]            =				padded_inputs.convert_to_tensors(__lowerCAmelCase			)

       return padded_inputs






