




from typing import List, Optional, Union

import numpy as np

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


SCREAMING_SNAKE_CASE:     Optional[int]									= logging.get_logger(__name__)



class    lowercase_		(_snake_case ):
						lowerCAmelCase__			      =["input_features", "attention_mask"]



						def __init__(			self	:   str					,							snake_case__	:   str=80					,							snake_case__	:   Union[str, Any]=1_60_00					,							snake_case__	:   Any=0.0					,							snake_case__	:   List[str]=10					,							snake_case__	:   Optional[int]=25					,							snake_case__	:   Dict="hamming_window"					,							snake_case__	:   Tuple=3_27_68.0					,							snake_case__	:   List[Any]=0.97					,							snake_case__	:   List[str]=1.0					,							snake_case__	:   str=True					,							snake_case__	:   Any=True					,							snake_case__	:   Tuple=False					,							**snake_case__	:   Optional[int]					,							):

								"""simple docstring"""

								super().__init__(feature_size=__lowerCamelCase					,							sampling_rate=__lowerCamelCase					,							padding_value=__lowerCamelCase					,							**__lowerCamelCase  )

								SCREAMING_SNAKE_CASE_					      =							feature_size
								SCREAMING_SNAKE_CASE_					      =							sampling_rate
								SCREAMING_SNAKE_CASE_					      =							padding_value
								SCREAMING_SNAKE_CASE_					      =							hop_length
								SCREAMING_SNAKE_CASE_					      =							win_length
								SCREAMING_SNAKE_CASE_					      =							frame_signal_scale
								SCREAMING_SNAKE_CASE_					      =							preemphasis_coeff
								SCREAMING_SNAKE_CASE_					      =							mel_floor
								SCREAMING_SNAKE_CASE_					      =							normalize_means
								SCREAMING_SNAKE_CASE_					      =							normalize_vars
								SCREAMING_SNAKE_CASE_					      =							win_function
								SCREAMING_SNAKE_CASE_					      =							return_attention_mask

								SCREAMING_SNAKE_CASE_					      =							win_length * sampling_rate // 10_00
								SCREAMING_SNAKE_CASE_					      =							hop_length * sampling_rate // 10_00

								SCREAMING_SNAKE_CASE_					      =							optimal_fft_length(self.sample_size  )
								SCREAMING_SNAKE_CASE_					      =							(self.n_fft // 2) + 1



						def 	__a					(			self	:   List[Any]					,							snake_case__	:   np.array  ):

								"""simple docstring"""

								if self.win_function == "hamming_window":
										SCREAMING_SNAKE_CASE_					      =							window_function(window_length=self.sample_size					,							name=self.win_function					,							periodic=__lowerCamelCase  )
								else:
										SCREAMING_SNAKE_CASE_					      =							window_function(window_length=self.sample_size					,							name=self.win_function  )

								SCREAMING_SNAKE_CASE_					      =							mel_filter_bank(
								    num_frequency_bins=self.n_freqs					,							num_mel_filters=self.feature_size					,							min_frequency=0.0					,							max_frequency=self.sampling_rate / 2.0					,							sampling_rate=self.sampling_rate					,							)

								SCREAMING_SNAKE_CASE_					      =							spectrogram(
								    one_waveform * self.frame_signal_scale					,							window=__lowerCamelCase					,							frame_length=self.sample_size					,							hop_length=self.sample_stride					,							fft_length=self.n_fft					,							center=__lowerCamelCase					,							preemphasis=self.preemphasis_coeff					,							mel_filters=__lowerCamelCase					,							mel_floor=self.mel_floor					,							log_mel='log'					,							)
								return msfc_features.T



						def 	__a					(			self	:   Dict					,							snake_case__	:   Tuple					,							snake_case__	:   Optional[Any]					,							snake_case__	:   List[Any]  ):

								"""simple docstring"""

								if self.normalize_means:
										SCREAMING_SNAKE_CASE_					      =							x[:input_length].mean(axis=0  )
										SCREAMING_SNAKE_CASE_					      =							np.subtract(__lowerCamelCase					,							__lowerCamelCase  )
								if self.normalize_vars:
										SCREAMING_SNAKE_CASE_					      =							x[:input_length].std(axis=0  )
										SCREAMING_SNAKE_CASE_					      =							np.divide(__lowerCamelCase					,							__lowerCamelCase  )

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

								# make sure array is in float32
								SCREAMING_SNAKE_CASE_					      =							x.astype(np.floataa  )

								return x



						def 	__a					(			self	:   Any					,							snake_case__	:   List[np.ndarray]					,							snake_case__	:   Optional[np.ndarray] = None  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							attention_mask.sum(-1  ) if attention_mask is not None else [x.shape[0] for x in input_features]
								return [self._normalize_one(__lowerCamelCase					,							__lowerCamelCase					,							self.padding_value  ) for x, n in zip(__lowerCamelCase					,							__lowerCamelCase  )]






						def __call__(			self	:   Any					,							snake_case__	:   Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]					,							snake_case__	:   Union[bool, str, PaddingStrategy] = False					,							snake_case__	:   Optional[int] = None					,							snake_case__	:   bool = False					,							snake_case__	:   Optional[int] = None					,							snake_case__	:   Optional[bool] = None					,							snake_case__	:   Optional[Union[str, TensorType]] = None					,							snake_case__	:   Optional[int] = None					,							**snake_case__	:   Optional[Any]					,							):

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

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

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

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

								# extract fbank features
								SCREAMING_SNAKE_CASE_					      =							[self._extract_mfsc_features(__lowerCamelCase  ) for one_waveform in raw_speech]

								# convert into correct format for padding
								SCREAMING_SNAKE_CASE_					      =							BatchFeature({'input_features': features}  )

								SCREAMING_SNAKE_CASE_					      =							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
								SCREAMING_SNAKE_CASE_					      =							padded_inputs.get('input_features'  )
								if isinstance(input_features[0]					,							__lowerCamelCase  ):
										SCREAMING_SNAKE_CASE_					      =							[np.asarray(__lowerCamelCase					,							dtype=np.floataa  ) for feature in input_features]

								SCREAMING_SNAKE_CASE_					      =							padded_inputs.get('attention_mask'  )
								if attention_mask is not None:
										SCREAMING_SNAKE_CASE_					      =							[np.asarray(__lowerCamelCase					,							dtype=np.intaa  ) for array in attention_mask]

								if self.normalize_means or self.normalize_vars:
										SCREAMING_SNAKE_CASE_					      =							(
										    np.array(__lowerCamelCase					,							dtype=np.intaa  )
										    if self._get_padding_strategies(__lowerCamelCase					,							max_length=__lowerCamelCase  ) is not PaddingStrategy.DO_NOT_PAD
										    and padding
										    else None
										)
										SCREAMING_SNAKE_CASE_					      =							self.normalize(
										    padded_inputs['input_features']					,							attention_mask=__lowerCamelCase  )

								if return_tensors is not None:
										SCREAMING_SNAKE_CASE_					      =							padded_inputs.convert_to_tensors(__lowerCamelCase  )

								return padded_inputs



'''simple docstring'''





import importlib.util
import json
import os
import warnings
from dataclasses import dataclass, field

import torch

from ..training_args import TrainingArguments
from ..utils import cached_property, is_sagemaker_dp_enabled, logging


__lowerCamelCase												=						logging.get_logger(__name__)


def        a__     (					):
   # Get the sagemaker specific mp parameters from smp_options variable.
   UpperCAmelCase__	:Any      =      os.getenv('''SM_HP_MP_PARAMETERS''',   '''{}'''						)
   try:
      # Parse it and check the field "partitions" is included, it is required for model parallel.
      UpperCAmelCase__	:Any      =      json.loads(UpperCamelCase_						)
      if "partitions" not in smp_options:
         return False
   except json.JSONDecodeError:
      return False

   # Get the sagemaker specific framework parameters from mpi_options variable.
   UpperCAmelCase__	:Optional[Any]      =      os.getenv('''SM_FRAMEWORK_PARAMS''',   '''{}'''						)
   try:
      # Parse it and check the field "sagemaker_distributed_dataparallel_enabled".
      UpperCAmelCase__	:str      =      json.loads(UpperCamelCase_						)
      if not mpi_options.get('''sagemaker_mpi_enabled''',   UpperCamelCase_						):
         return False
   except json.JSONDecodeError:
      return False
   # Lastly, check if the `smdistributed` module is present.
   return importlib.util.find_spec('''smdistributed'''						) is not None


if is_sagemaker_model_parallel_available():
    import smdistributed.modelparallel.torch as smp

    smp.init()




@dataclass
class 				UpperCAmelCase			(			_snake_case						):
 UpperCAmelCase				=					field(
     default=""						, metadata={"help": "Used by the SageMaker launcher to send mp-specific args. Ignored in SageMakerTrainer"}						, )


 def        __SCREAMING_SNAKE_CASE					(      self							:	List[Any] ):
    super().__post_init__()
    warnings.warn(
        '''`SageMakerTrainingArguments` is deprecated and will be removed in v5 of Transformers. You can use '''
        '''`TrainingArguments` instead.'''			,       __lowerCamelCase			,       )


 @cached_property
 def        __SCREAMING_SNAKE_CASE					(      self							:	List[Any] ):
    logger.info('''PyTorch: setting up devices''' )
    if torch.distributed.is_available() and torch.distributed.is_initialized() and self.local_rank == -1:
       logger.warning(
           '''torch.distributed process group is initialized, but local_rank == -1. '''
           '''In order to use Torch DDP, launch your script with `python -m torch.distributed.launch''' )
    if self.no_cuda:
       UpperCAmelCase__	:Union[str, Any]      =      torch.device('''cpu''' )
       UpperCAmelCase__	:Dict      =      0
    elif is_sagemaker_model_parallel_available():
       UpperCAmelCase__	:Dict      =      smp.local_rank()
       UpperCAmelCase__	:List[str]      =      torch.device('''cuda'''			,       __lowerCamelCase )
       UpperCAmelCase__	:Optional[int]      =      1
    elif is_sagemaker_dp_enabled():
       import smdistributed.dataparallel.torch.torch_smddp  # noqa: F401

       torch.distributed.init_process_group(backend='''smddp'''			,       timeout=self.ddp_timeout_delta )
       UpperCAmelCase__	:int      =      int(os.getenv('''SMDATAPARALLEL_LOCAL_RANK''' ) )
       UpperCAmelCase__	:int      =      torch.device('''cuda'''			,       self.local_rank )
       UpperCAmelCase__	:Union[str, Any]      =      1
    elif self.local_rank == -1:
       # if n_gpu is > 1 we'll use nn.DataParallel.
       # If you only want to use a specific subset of GPUs use `CUDA_VISIBLE_DEVICES=0`
       # Explicitly set CUDA to the first (index 0) CUDA device, otherwise `set_device` will
       # trigger an error that a device index is missing. Index 0 takes into account the
       # GPUs available in the environment, so `CUDA_VISIBLE_DEVICES=1,2` with `cuda:0`
       # will use the first GPU in that env, i.e. GPU#1
       UpperCAmelCase__	:str      =      torch.device('''cuda:0''' if torch.cuda.is_available() else '''cpu''' )
       # Sometimes the line in the postinit has not been run before we end up here, so just checking we're not at
       # the default value.
       UpperCAmelCase__	:str      =      torch.cuda.device_count()
    else:
       # Here, we'll use torch.distributed.
       # Initializes the distributed backend which will take care of synchronizing nodes/GPUs
       if not torch.distributed.is_initialized():
          torch.distributed.init_process_group(backend='''nccl'''			,       timeout=self.ddp_timeout_delta )
       UpperCAmelCase__	:str      =      torch.device('''cuda'''			,       self.local_rank )
       UpperCAmelCase__	:Union[str, Any]      =      1

    if device.type == "cuda":
       torch.cuda.set_device(__lowerCamelCase )

    return device


 @property
 def        __SCREAMING_SNAKE_CASE					(      self							:	List[str] ):
    if is_sagemaker_model_parallel_available():
       return smp.dp_size()

    return super().world_size


 @property
 def        __SCREAMING_SNAKE_CASE					(      self							:	Dict ):
    return not is_sagemaker_model_parallel_available()





 @property
 def        __SCREAMING_SNAKE_CASE					(      self							:	int ):
    return False



