






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

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


lowerCamelCase			:							Optional[Any]       =logging.get_logger(__name__)

lowerCamelCase			:							List[Any]       ={
    '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/config.json''',
    # See all M2M100 models at https://huggingface.co/models?filter=m2m_100
}





class     __a						(					A__ ):
 _lowerCAmelCase : List[Any] 				=				'''m2m_100'''
 _lowerCAmelCase : List[str] 				=				['''past_key_values''']
 _lowerCAmelCase : List[str] 				=				{'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}







 def __init__(	self      :     Optional[int]							,     SCREAMING_SNAKE_CASE      :     Any=12_81_12							,     SCREAMING_SNAKE_CASE      :     Optional[Any]=10_24							,     SCREAMING_SNAKE_CASE      :     Optional[Any]=12							,     SCREAMING_SNAKE_CASE      :     Optional[Any]=40_96							,     SCREAMING_SNAKE_CASE      :     str=16							,     SCREAMING_SNAKE_CASE      :     Union[str, Any]=12							,     SCREAMING_SNAKE_CASE      :     Optional[Any]=40_96							,     SCREAMING_SNAKE_CASE      :     Any=16							,     SCREAMING_SNAKE_CASE      :     Optional[Any]=0.0_5							,     SCREAMING_SNAKE_CASE      :     List[str]=0.0_5							,     SCREAMING_SNAKE_CASE      :     Dict=True							,     SCREAMING_SNAKE_CASE      :     Tuple=True							,     SCREAMING_SNAKE_CASE      :     Optional[Any]="relu"							,     SCREAMING_SNAKE_CASE      :     Dict=10_24							,     SCREAMING_SNAKE_CASE      :     int=0.1							,     SCREAMING_SNAKE_CASE      :     Tuple=0.1							,     SCREAMING_SNAKE_CASE      :     Any=0.0							,     SCREAMING_SNAKE_CASE      :     Optional[Any]=0.0_2							,     SCREAMING_SNAKE_CASE      :     List[Any]=2							,     SCREAMING_SNAKE_CASE      :     int=True							,     SCREAMING_SNAKE_CASE      :     str=1							,     SCREAMING_SNAKE_CASE      :     int=0							,     SCREAMING_SNAKE_CASE      :     Any=2							,     **SCREAMING_SNAKE_CASE      :     Tuple							,     ):


      '''simple docstring'''






      UpperCamelCase__    :     int        =						vocab_size
      UpperCamelCase__    :     Dict        =						max_position_embeddings
      UpperCamelCase__    :     Tuple        =						d_model
      UpperCamelCase__    :     Tuple        =						encoder_ffn_dim
      UpperCamelCase__    :     Optional[int]        =						encoder_layers
      UpperCamelCase__    :     List[str]        =						encoder_attention_heads
      UpperCamelCase__    :     Tuple        =						decoder_ffn_dim
      UpperCamelCase__    :     List[str]        =						decoder_layers
      UpperCamelCase__    :     List[Any]        =						decoder_attention_heads
      UpperCamelCase__    :     Dict        =						dropout
      UpperCamelCase__    :     Tuple        =						attention_dropout
      UpperCamelCase__    :     List[str]        =						activation_dropout
      UpperCamelCase__    :     Union[str, Any]        =						activation_function
      UpperCamelCase__    :     str        =						init_std
      UpperCamelCase__    :     Union[str, Any]        =						encoder_layerdrop
      UpperCamelCase__    :     Tuple        =						decoder_layerdrop
      UpperCamelCase__    :     Any        =						use_cache
      UpperCamelCase__    :     List[Any]        =						encoder_layers
      UpperCamelCase__    :     Optional[Any]        =						scale_embedding  # scale factor will be sqrt(d_model) if True

      super().__init__(
          pad_token_id=SCREAMING_SNAKE_CASE							,     bos_token_id=SCREAMING_SNAKE_CASE							,     eos_token_id=SCREAMING_SNAKE_CASE							,     is_encoder_decoder=SCREAMING_SNAKE_CASE							,     decoder_start_token_id=SCREAMING_SNAKE_CASE							,     **SCREAMING_SNAKE_CASE							,     )







class     __a						(					A__ ):







 @property
 def       __lowercase      (	self      :     str		):


      '''simple docstring'''






      UpperCamelCase__    :     List[Any]        =						OrderedDict(
          [
              ("input_ids", {0: "batch", 1: "encoder_sequence"}),
              ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
          ]		)

      if self.use_past:
           UpperCamelCase__    :     List[Any]        =						{0: "batch"}
           UpperCamelCase__    :     List[str]        =						{0: "batch", 1: "past_decoder_sequence + sequence"}
      else:
           UpperCamelCase__    :     List[Any]        =						{0: "batch", 1: "decoder_sequence"}
           UpperCamelCase__    :     Any        =						{0: "batch", 1: "decoder_sequence"}

      if self.use_past:
           self.fill_with_past_key_values_(SCREAMING_SNAKE_CASE							,     direction="inputs"		)
      return common_inputs







 def       __lowercase      (	self      :     List[Any]							,     SCREAMING_SNAKE_CASE      :     PreTrainedTokenizer							,     SCREAMING_SNAKE_CASE      :     int = -1							,     SCREAMING_SNAKE_CASE      :     int = -1							,     SCREAMING_SNAKE_CASE      :     bool = False							,     SCREAMING_SNAKE_CASE      :     Optional[TensorType] = None							,     ):


      '''simple docstring'''






      UpperCamelCase__    :     Tuple        =						compute_effective_axis_dimension(
          SCREAMING_SNAKE_CASE							,     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__    :     Optional[Any]        =						tokenizer.num_special_tokens_to_add(SCREAMING_SNAKE_CASE		)
      UpperCamelCase__    :     str        =						compute_effective_axis_dimension(
          SCREAMING_SNAKE_CASE							,     fixed_dimension=OnnxConfig.default_fixed_sequence							,     num_token_to_add=SCREAMING_SNAKE_CASE		)

      # Generate dummy inputs according to compute batch and sequence
      UpperCamelCase__    :     Optional[Any]        =						[" ".join([tokenizer.unk_token]		) * seq_length] * batch_size
      UpperCamelCase__    :     List[str]        =						dict(tokenizer(SCREAMING_SNAKE_CASE							,     return_tensors=SCREAMING_SNAKE_CASE		)		)
      return common_inputs







 def       __lowercase      (	self      :     Any							,     SCREAMING_SNAKE_CASE      :     PreTrainedTokenizer							,     SCREAMING_SNAKE_CASE      :     int = -1							,     SCREAMING_SNAKE_CASE      :     int = -1							,     SCREAMING_SNAKE_CASE      :     bool = False							,     SCREAMING_SNAKE_CASE      :     Optional[TensorType] = None							,     ):


      '''simple docstring'''






      UpperCamelCase__    :     List[Any]        =						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
          SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		)

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

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

           UpperCamelCase__    :     Union[str, Any]        =						torch.cat(
               [common_inputs["decoder_attention_mask"], torch.ones(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		)]							,     dim=1		)

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

           for _ in range(SCREAMING_SNAKE_CASE		):
                common_inputs["past_key_values"].append(
                    (
                        torch.zeros(SCREAMING_SNAKE_CASE		),
                        torch.zeros(SCREAMING_SNAKE_CASE		),
                        torch.zeros(SCREAMING_SNAKE_CASE		),
                        torch.zeros(SCREAMING_SNAKE_CASE		),
                    )		)
           # TODO: test this.
           UpperCamelCase__    :     Optional[int]        =						encoder_shape if remaining_side_name == "encoder" else decoder_shape
           for _ in range(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		):
                common_inputs["past_key_values"].append((torch.zeros(SCREAMING_SNAKE_CASE		), torch.zeros(SCREAMING_SNAKE_CASE		))		)
      return common_inputs
 _lowerCAmelCase : List[Any] 				=				_generate_dummy_inputs_for_default_and_seqaseq_lm












import numpy as np  # noqa: E402

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


try:
 import librosa  # noqa: E402

 lowerCamelCase			:							List[str]       =True
 lowerCamelCase			:							Dict       =''''''
except Exception as e:
 lowerCamelCase			:							str       =False
 lowerCamelCase			:							List[Any]       =(
     F"""Cannot import librosa because {e}. Make sure to correctly install librosa to be able to install it."""
 )


from PIL import Image  # noqa: E402





class     __a						(					A__ ,     A__ ):
 _lowerCAmelCase : Optional[Any] 				=				'''mel_config.json'''







 @register_to_config
 def __init__(	self      :     Any							,     SCREAMING_SNAKE_CASE      :     int = 2_56							,     SCREAMING_SNAKE_CASE      :     int = 2_56							,     SCREAMING_SNAKE_CASE      :     int = 2_20_50							,     SCREAMING_SNAKE_CASE      :     int = 20_48							,     SCREAMING_SNAKE_CASE      :     int = 5_12							,     SCREAMING_SNAKE_CASE      :     int = 80							,     SCREAMING_SNAKE_CASE      :     int = 32							,     ):


      '''simple docstring'''






      UpperCamelCase__    :     str        =						hop_length
      UpperCamelCase__    :     Optional[Any]        =						sample_rate
      UpperCamelCase__    :     Dict        =						n_fft
      UpperCamelCase__    :     str        =						top_db
      UpperCamelCase__    :     Optional[Any]        =						n_iter
      self.set_resolution(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		)
      UpperCamelCase__    :     str        =						None

      if not _librosa_can_be_imported:
           raise ValueError(_import_error		)







 def       __lowercase      (	self      :     Tuple							,     SCREAMING_SNAKE_CASE      :     int							,     SCREAMING_SNAKE_CASE      :     int		):


      '''simple docstring'''






      UpperCamelCase__    :     Dict        =						x_res
      UpperCamelCase__    :     Dict        =						y_res
      UpperCamelCase__    :     Dict        =						self.y_res
      UpperCamelCase__    :     Any        =						self.x_res * self.hop_length - 1







 def       __lowercase      (	self      :     str							,     SCREAMING_SNAKE_CASE      :     str = None							,     SCREAMING_SNAKE_CASE      :     np.ndarray = None		):


      '''simple docstring'''






      if audio_file is not None:
           UpperCamelCase__       ,  UpperCamelCase__    :     int        =						librosa.load(SCREAMING_SNAKE_CASE							,     mono=SCREAMING_SNAKE_CASE							,     sr=self.sr		)
      else:
           UpperCamelCase__    :     List[Any]        =						raw_audio

      # Pad with silence if necessary.
      if len(self.audio		) < self.x_res * self.hop_length:
           UpperCamelCase__    :     List[Any]        =						np.concatenate([self.audio, np.zeros((self.x_res * self.hop_length - len(self.audio		),)		)]		)







 def       __lowercase      (	self      :     List[Any]		):


      '''simple docstring'''






      return len(self.audio		) // self.slice_size







 def       __lowercase      (	self      :     int							,     SCREAMING_SNAKE_CASE      :     int = 0		):


      '''simple docstring'''






      return self.audio[self.slice_size * slice : self.slice_size * (slice + 1)]







 def       __lowercase      (	self      :     Tuple		):


      '''simple docstring'''






      return self.sr







 def       __lowercase      (	self      :     Union[str, Any]							,     SCREAMING_SNAKE_CASE      :     int		):


      '''simple docstring'''






      UpperCamelCase__    :     Optional[int]        =						librosa.feature.melspectrogram(
          y=self.get_audio_slice(SCREAMING_SNAKE_CASE		)							,     sr=self.sr							,     n_fft=self.n_fft							,     hop_length=self.hop_length							,     n_mels=self.n_mels		)
      UpperCamelCase__    :     Tuple        =						librosa.power_to_db(SCREAMING_SNAKE_CASE							,     ref=np.max							,     top_db=self.top_db		)
      UpperCamelCase__    :     Optional[Any]        =						(((log_S + self.top_db) * 2_55 / self.top_db).clip(0							,     2_55		) + 0.5).astype(np.uinta		)
      UpperCamelCase__    :     Optional[Any]        =						Image.fromarray(SCREAMING_SNAKE_CASE		)
      return image







 def       __lowercase      (	self      :     List[Any]							,     SCREAMING_SNAKE_CASE      :     Image.Image		):


      '''simple docstring'''






      UpperCamelCase__    :     Optional[Any]        =						np.frombuffer(image.tobytes()							,     dtype="uint8"		).reshape((image.height, image.width)		)
      UpperCamelCase__    :     str        =						bytedata.astype("float"		) * self.top_db / 2_55 - self.top_db
      UpperCamelCase__    :     Any        =						librosa.db_to_power(SCREAMING_SNAKE_CASE		)
      UpperCamelCase__    :     Tuple        =						librosa.feature.inverse.mel_to_audio(
          SCREAMING_SNAKE_CASE							,     sr=self.sr							,     n_fft=self.n_fft							,     hop_length=self.hop_length							,     n_iter=self.n_iter		)
      return audio