


import copy
from typing import Any, Dict, List, Optional, Union

import numpy as np

from ...audio_utils import mel_filter_bank, spectrogram, window_function
from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import TensorType, logging


SCREAMING_SNAKE_CASE						:Union[str, Any]			=  logging.get_logger(__name__)


class      __lowerCAmelCase  (			a       ):






  """simple docstring"""





  _SCREAMING_SNAKE_CASE		     =							['input_features']



  def __init__(  self							:							Optional[Any]     ,					_lowerCAmelCase							:							Optional[int]=8_0     ,					_lowerCAmelCase							:							Dict=1_6_0_0_0     ,					_lowerCAmelCase							:							Any=1_6_0     ,					_lowerCAmelCase							:							Optional[int]=3_0     ,					_lowerCAmelCase							:							Optional[int]=4_0_0     ,					_lowerCAmelCase							:							List[str]=0.0     ,					_lowerCAmelCase							:							Optional[int]=False     ,					**_lowerCAmelCase							:							Optional[Any]     ,					) ->			Union[str, Any]:




     """simple docstring"""
     super().__init__(
         feature_size=_lowerCAmelCase     ,					sampling_rate=_lowerCAmelCase     ,					padding_value=_lowerCAmelCase     ,					return_attention_mask=_lowerCAmelCase     ,					**_lowerCAmelCase     ,					)
     snake_case_            =       n_fft
     snake_case_            =       hop_length
     snake_case_            =       chunk_length
     snake_case_            =       chunk_length * sampling_rate
     snake_case_            =       self.n_samples // hop_length
     snake_case_            =       sampling_rate
     snake_case_            =       mel_filter_bank(
         num_frequency_bins=1 + n_fft // 2     ,					num_mel_filters=_lowerCAmelCase     ,					min_frequency=0.0     ,					max_frequency=8_000.0     ,					sampling_rate=_lowerCAmelCase     ,					norm="slaney"     ,					mel_scale="slaney"     ,					)



  def        lowerCAmelCase__	(  self							:							Dict     ,					_lowerCAmelCase							:							np.array	) ->			np.ndarray:




     """simple docstring"""
     snake_case_            =       spectrogram(
         _lowerCAmelCase     ,					window_function(self.n_fft     ,					"hann"	)     ,					frame_length=self.n_fft     ,					hop_length=self.hop_length     ,					power=2.0     ,					mel_filters=self.mel_filters     ,					log_mel="log10"     ,					)
     snake_case_            =       log_spec[:, :-1]
     snake_case_            =       np.maximum(_lowerCAmelCase     ,					log_spec.max() - 8.0	)
     snake_case_            =       (log_spec + 4.0) / 4.0
     return log_spec



  @staticmethod
  # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm
  def        lowerCAmelCase__	(  _lowerCAmelCase							:							List[np.ndarray]     ,					_lowerCAmelCase							:							List[np.ndarray]     ,					_lowerCAmelCase							:							float = 0.0	) ->			List[np.ndarray]:




     """simple docstring"""
     if attention_mask is not None:
        snake_case_            =       np.array(_lowerCAmelCase     ,					np.intaa	)
        snake_case_            =       []

        for vector, length in zip(_lowerCAmelCase     ,					attention_mask.sum(-1	)	):
           snake_case_            =       (vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1e-7	)
           if length < normed_slice.shape[0]:
              snake_case_            =       padding_value

           normed_input_values.append(_lowerCAmelCase	)
     else:
        snake_case_            =       [(x - x.mean()) / np.sqrt(x.var() + 1e-7	) for x in input_values]

     return normed_input_values



  def __call__(  self							:							List[str]     ,					_lowerCAmelCase							:							Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]     ,					_lowerCAmelCase							:							bool = True     ,					_lowerCAmelCase							:							Optional[int] = None     ,					_lowerCAmelCase							:							Optional[Union[str, TensorType]] = None     ,					_lowerCAmelCase							:							Optional[bool] = None     ,					_lowerCAmelCase							:							Optional[str] = "max_length"     ,					_lowerCAmelCase							:							Optional[int] = None     ,					_lowerCAmelCase							:							Optional[int] = None     ,					_lowerCAmelCase							:							Optional[bool] = None     ,					**_lowerCAmelCase							:							int     ,					) ->			BatchFeature:




     """simple docstring"""

     if sampling_rate is not None:
        if sampling_rate != self.sampling_rate:
           raise ValueError(
               F'''The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a'''
               F''' sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input'''
               F''' was sampled with {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."	)

     snake_case_            =       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}'''	)
     snake_case_            =       is_batched_numpy or (
         isinstance(_lowerCAmelCase     ,					(list, tuple)	) and (isinstance(raw_speech[0]     ,					(np.ndarray, tuple, list)	))
     )

     if is_batched:
        snake_case_            =       [np.asarray([speech]     ,					dtype=np.floataa	).T for speech in raw_speech]
     elif not is_batched and not isinstance(_lowerCAmelCase     ,					np.ndarray	):
        snake_case_            =       np.asarray(_lowerCAmelCase     ,					dtype=np.floataa	)
     elif isinstance(_lowerCAmelCase     ,					np.ndarray	) and raw_speech.dtype is np.dtype(np.floataa	):
        snake_case_            =       raw_speech.astype(np.floataa	)

     # always return batch
     if not is_batched:
        snake_case_            =       [np.asarray([raw_speech]	).T]

     snake_case_            =       BatchFeature({"input_features": raw_speech}	)

     # convert into correct format for padding

     snake_case_            =       self.pad(
         _lowerCAmelCase     ,					padding=_lowerCAmelCase     ,					max_length=max_length if max_length else self.n_samples     ,					truncation=_lowerCAmelCase     ,					pad_to_multiple_of=_lowerCAmelCase     ,					return_attention_mask=return_attention_mask or do_normalize     ,					)

     # zero-mean and unit-variance normalization
     if do_normalize:
        snake_case_            =       self.zero_mean_unit_var_norm(
            padded_inputs["input_features"]     ,					attention_mask=padded_inputs["attention_mask"]     ,					padding_value=self.padding_value     ,					)
        snake_case_            =       np.stack(padded_inputs["input_features"]     ,					axis=0	)

     # make sure list is in array format
     snake_case_            =       padded_inputs.get("input_features"	).transpose(2     ,					0     ,					1	)

     snake_case_            =       [self._np_extract_fbank_features(_lowerCAmelCase	) for waveform in input_features[0]]

     if isinstance(input_features[0]     ,					_lowerCAmelCase	):
        snake_case_            =       [np.asarray(_lowerCAmelCase     ,					dtype=np.floataa	) for feature in input_features]
     else:
        snake_case_            =       input_features

     if return_attention_mask:
        # rescale from sample (48000) to feature (3000)
        snake_case_            =       padded_inputs["attention_mask"][:, :: self.hop_length]

     if return_tensors is not None:
        snake_case_            =       padded_inputs.convert_to_tensors(_lowerCAmelCase	)

     return padded_inputs



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




     """simple docstring"""
     snake_case_            =       copy.deepcopy(self.__dict__	)
     snake_case_            =       self.__class__.__name__
     if "mel_filters" in output:
        del output["mel_filters"]
     return output








import math
import time
from typing import Dict, List, Optional

from torch.utils.data import Dataset

from transformers import SeqaSeqTrainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput, speed_metrics


if is_torch_tpu_available(check_device=False):
   import torch_xla.core.xla_model as xm
   import torch_xla.debug.metrics as met


class      __lowerCAmelCase  (			a       ):






  """simple docstring"""





  def __init__(  self							:							Union[str, Any]     ,					*_lowerCAmelCase							:							Dict     ,					_lowerCAmelCase							:							List[Any]=None     ,					_lowerCAmelCase							:							str=None     ,					**_lowerCAmelCase							:							str	) ->			Dict:




     """simple docstring"""
     super().__init__(*_lowerCAmelCase     ,					**_lowerCAmelCase	)
     snake_case_            =       eval_examples
     snake_case_            =       post_process_function



  def        lowerCAmelCase__	(  self							:							Union[str, Any]     ,					_lowerCAmelCase							:							Optional[Dataset] = None     ,					_lowerCAmelCase							:							Any=None     ,					_lowerCAmelCase							:							Optional[List[str]] = None     ,					_lowerCAmelCase							:							str = "eval"     ,					**_lowerCAmelCase							:							Tuple     ,					) ->			Dict[str, float]:




     """simple docstring"""
     snake_case_            =       gen_kwargs.copy()
     snake_case_            =       (
         gen_kwargs["max_length"] if gen_kwargs.get("max_length"	) is not None else self.args.generation_max_length
     )
     snake_case_            =       (
         gen_kwargs["num_beams"] if gen_kwargs.get("num_beams"	) is not None else self.args.generation_num_beams
     )
     snake_case_            =       gen_kwargs

     snake_case_            =       self.eval_dataset if eval_dataset is None else eval_dataset
     snake_case_            =       self.get_eval_dataloader(_lowerCAmelCase	)
     snake_case_            =       self.eval_examples if eval_examples is None else eval_examples

     # Temporarily disable metric computation, we will do it in the loop here.
     snake_case_            =       self.compute_metrics
     snake_case_            =       None
     snake_case_            =       time.time()
     snake_case_            =       self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
     try:
        snake_case_            =       eval_loop(
            _lowerCAmelCase     ,					description="Evaluation"     ,					prediction_loss_only=True if compute_metrics is None else None     ,					ignore_keys=_lowerCAmelCase     ,					metric_key_prefix=_lowerCAmelCase     ,					)
     finally:
        snake_case_            =       compute_metrics
     snake_case_            =       self.args.eval_batch_size * self.args.world_size
     if F'''{metric_key_prefix}_jit_compilation_time''' in output.metrics:
        start_time += output.metrics[F'''{metric_key_prefix}_jit_compilation_time''']
     output.metrics.update(
         speed_metrics(
             _lowerCAmelCase     ,					_lowerCAmelCase     ,					num_samples=output.num_samples     ,					num_steps=math.ceil(output.num_samples / total_batch_size	)     ,					)	)

     if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save:
        # Only the main node write the results by default
        snake_case_            =       self.post_process_function(_lowerCAmelCase     ,					_lowerCAmelCase     ,					_lowerCAmelCase	)
        snake_case_            =       self.compute_metrics(_lowerCAmelCase	)

        # Prefix all keys with metric_key_prefix + '_'
        for key in list(metrics.keys()	):
           if not key.startswith(F'''{metric_key_prefix}_'''	):
              snake_case_            =       metrics.pop(_lowerCAmelCase	)

        metrics.update(output.metrics	)
     else:
        snake_case_            =       output.metrics

     if self.args.should_log:
        # Only the main node log the results by default
        self.log(_lowerCAmelCase	)

     if self.args.tpu_metrics_debug or self.args.debug:
        # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
        xm.master_print(met.metrics_report()	)

     snake_case_            =       self.callback_handler.on_evaluate(self.args     ,					self.state     ,					self.control     ,					_lowerCAmelCase	)
     return metrics



  def        lowerCAmelCase__	(  self							:							Optional[Any]     ,					_lowerCAmelCase							:							Optional[Any]     ,					_lowerCAmelCase							:							Union[str, Any]     ,					_lowerCAmelCase							:							List[str]=None     ,					_lowerCAmelCase							:							str = "test"     ,					**_lowerCAmelCase							:							Optional[Any]	) ->			Union[str, Any]:




     """simple docstring"""
     snake_case_            =       gen_kwargs.copy()

     snake_case_            =       self.get_test_dataloader(_lowerCAmelCase	)

     # Temporarily disable metric computation, we will do it in the loop here.
     snake_case_            =       self.compute_metrics
     snake_case_            =       None
     snake_case_            =       time.time()
     snake_case_            =       self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
     try:
        snake_case_            =       eval_loop(
            _lowerCAmelCase     ,					description="Prediction"     ,					prediction_loss_only=True if compute_metrics is None else None     ,					ignore_keys=_lowerCAmelCase     ,					metric_key_prefix=_lowerCAmelCase     ,					)
     finally:
        snake_case_            =       compute_metrics

     snake_case_            =       self.args.eval_batch_size * self.args.world_size
     if F'''{metric_key_prefix}_jit_compilation_time''' in output.metrics:
        start_time += output.metrics[F'''{metric_key_prefix}_jit_compilation_time''']
     output.metrics.update(
         speed_metrics(
             _lowerCAmelCase     ,					_lowerCAmelCase     ,					num_samples=output.num_samples     ,					num_steps=math.ceil(output.num_samples / total_batch_size	)     ,					)	)
     if self.post_process_function is None or self.compute_metrics is None:
        return output

     snake_case_            =       self.post_process_function(_lowerCAmelCase     ,					_lowerCAmelCase     ,					_lowerCAmelCase     ,					"predict"	)
     snake_case_            =       self.compute_metrics(_lowerCAmelCase	)

     # Prefix all keys with metric_key_prefix + '_'
     for key in list(metrics.keys()	):
        if not key.startswith(F'''{metric_key_prefix}_'''	):
           snake_case_            =       metrics.pop(_lowerCAmelCase	)
     metrics.update(output.metrics	)
     return PredictionOutput(predictions=predictions.predictions     ,					label_ids=predictions.label_ids     ,					metrics=_lowerCAmelCase	)




