


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 TensorType, logging


_SCREAMING_SNAKE_CASE    :				List[Any]       =					logging.get_logger(__name__)

class 						A__      (					snake_case__			):







    """simple docstring"""

    __magic_name__    						=	['input_values', 'attention_mask']
    def __init__( self    ,      __snake_case=1    ,      __snake_case=1_6_0_0_0    ,      __snake_case=1_2_8    ,      __snake_case=1_0_2_4    ,      __snake_case=0.0    ,      __snake_case=True    ,      __snake_case=-4.267_7393    ,      __snake_case=4.568_9974    ,      __snake_case=False    ,      **__snake_case    ,      ):
         super().__init__(feature_size=__snake_case    ,      sampling_rate=__snake_case    ,      padding_value=__snake_case    ,      **__snake_case					)
         snake_case					=       num_mel_bins
         snake_case					=       max_length
         snake_case					=       do_normalize
         snake_case					=       mean
         snake_case					=       std
         snake_case					=       return_attention_mask
    def 	a_    ( self    ,      __snake_case    ,      __snake_case    ,      ):
         snake_case					=       torch.from_numpy(__snake_case					).unsqueeze(0					)
         snake_case					=       ta_kaldi.fbank(
             __snake_case    ,      htk_compat=__snake_case    ,      sample_frequency=self.sampling_rate    ,      use_energy=__snake_case    ,      window_type='''hanning'''    ,      num_mel_bins=self.num_mel_bins    ,      dither=0.0    ,      frame_shift=1_0    ,      )

         snake_case					=       fbank.shape[0]
         snake_case					=       max_length - n_frames

         # pad or truncate, depending on difference
         if difference > 0:
              snake_case					=       torch.nn.ZeroPadad((0, 0, 0, difference)					)
              snake_case					=       pad_module(__snake_case					)
         elif difference < 0:
              snake_case					=       fbank[0:max_length, :]

         snake_case					=       fbank.numpy()

         return fbank
    def 	a_    ( self    ,      __snake_case					):
         return (input_values - (self.mean)) / (self.std * 2)




    def __call__( self    ,      __snake_case    ,      __snake_case = None    ,      __snake_case = None    ,      **__snake_case    ,      ):

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

         snake_case					=       isinstance(__snake_case    ,      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(__snake_case    ,      (list, tuple)					) and (isinstance(raw_speech[0]    ,      (np.ndarray, tuple, list)					))
         )

         if is_batched:
              snake_case					=       [np.asarray(__snake_case    ,      dtype=np.floataa					) for speech in raw_speech]
         elif not is_batched and not isinstance(__snake_case    ,      np.ndarray					):
              snake_case					=       np.asarray(__snake_case    ,      dtype=np.floataa					)
         elif isinstance(__snake_case    ,      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					=       [raw_speech]

         # extract fbank features and pad/truncate to max_length
         snake_case					=       [self._extract_fbank_features(__snake_case    ,      max_length=self.max_length					) for waveform in raw_speech]

         # convert into BatchFeature
         snake_case					=       BatchFeature({'''input_values''': features}					)

         # make sure list is in array format
         snake_case					=       padded_inputs.get('''input_values'''					)
         if isinstance(input_values[0]    ,      __snake_case					):
              snake_case					=       [np.asarray(__snake_case    ,      dtype=np.floataa					) for feature in input_values]

         # normalization
         if self.do_normalize:
              snake_case					=       [self.normalize(__snake_case					) for feature in input_values]

         if return_tensors is not None:
              snake_case					=       padded_inputs.convert_to_tensors(__snake_case					)

         return padded_inputs








import torch

from accelerate import Accelerator, DistributedType

class 						A__      :







    """simple docstring"""

    def __enter__( self					):
         if self.enabled:
              snake_case					=       self.model.no_sync()
              self.model_sync_obj.__enter__()

         return self
    def __exit__( self    ,      __snake_case    ,      __snake_case    ,      __snake_case					):
         if self.enabled:
              # Average all models on exit
              self._sync_and_avg_model_params()
              self.model_sync_obj.__exit__(__snake_case    ,      __snake_case    ,      __snake_case					)
    def __init__( self    ,      __snake_case    ,      __snake_case    ,      __snake_case    ,      __snake_case = True					):
         if accelerator.distributed_type not in [
             DistributedType.NO,
             DistributedType.MULTI_CPU,
             DistributedType.MULTI_GPU,
         ]:
              raise NotImplementedError('''LocalSGD is supported only for CPUs and GPUs (no DeepSpeed or MegatronLM)'''					)
         snake_case					=       enabled and accelerator.distributed_type != DistributedType.NO
         snake_case					=       0
         if self.enabled:
              snake_case					=       accelerator
              snake_case					=       model
              snake_case					=       local_sgd_steps
    def 	a_    ( self					):
         self.num_steps += 1
         if not self.enabled:
              return

         if self.num_steps % self.local_sgd_steps == 0:
              self._sync_and_avg_model_params()




    def 	a_    ( self					):
         self.accelerator.wait_for_everyone()
         with self.accelerator.autocast():
              for param in self.model.parameters():
                   snake_case					=       self.accelerator.reduce(param.data    ,      reduction='''mean'''					)




