


import warnings
from typing import Any, Dict, 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 ...utils import PaddingStrategy, TensorType, logging


lowerCamelCase__							:						Union[str, Any]							     =   logging.get_logger(__name__)
class 		__magic_name__						(lowercase_				):





     '''simple docstring'''



     __lowercase				:							Any	   =       ['input_values', 'attention_mask']

     def __init__(					self:List[str]     ,		_a:Any = 1     ,		_a:str = 1_60_00     ,		_a:Tuple = 0.0     ,		_a:Optional[Any] = False     ,		_a:Optional[Any] = 80     ,		_a:Union[str, Any] = 16     ,		_a:int = 64     ,		_a:Optional[int] = "hann_window"     ,		_a:Tuple = 1.0     ,		_a:Union[str, Any] = 80     ,		_a:Union[str, Any] = 76_00     ,		_a:Any = 1e-10     ,		_a:Any = 2     ,		_a:int = True     ,		**_a:str     ,		):
       super().__init__(feature_size=_lowercase     ,		sampling_rate=_lowercase     ,		padding_value=_lowercase     ,		**_lowercase   )
       snake_case__						= do_normalize
       snake_case__						= return_attention_mask

       snake_case__						= num_mel_bins
       snake_case__						= hop_length
       snake_case__						= win_length
       snake_case__						= win_function
       snake_case__						= frame_signal_scale
       snake_case__						= fmin
       snake_case__						= fmax
       snake_case__						= mel_floor
       snake_case__						= reduction_factor

       snake_case__						= win_length * sampling_rate // 10_00
       snake_case__						= hop_length * sampling_rate // 10_00
       snake_case__						= optimal_fft_length(self.sample_size   )
       snake_case__						= (self.n_fft // 2) + 1

       snake_case__						= window_function(window_length=self.sample_size     ,		name=self.win_function     ,		periodic=_lowercase   )

       snake_case__						= mel_filter_bank(
           num_frequency_bins=self.n_freqs     ,		num_mel_filters=self.num_mel_bins     ,		min_frequency=self.fmin     ,		max_frequency=self.fmax     ,		sampling_rate=self.sampling_rate     ,		norm='''slaney'''     ,		mel_scale='''slaney'''     ,		)

       if frame_signal_scale != 1.0:
         warnings.warn(
             '''The argument `frame_signal_scale` is deprecated and will be removed in version 4.30.0 of Transformers'''     ,		_lowercase     ,		)
       if reduction_factor != 2.0:
         warnings.warn(
             '''The argument `reduction_factor` is deprecated and will be removed in version 4.30.0 of Transformers'''     ,		_lowercase     ,		)

     @staticmethod
     # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm
     def 	SCREAMING_SNAKE_CASE__						(					_a:Optional[int]     ,		_a:Tuple     ,		_a:Optional[Any] = 0.0   ):
       if attention_mask is not None:
         snake_case__						= np.array(_lowercase     ,		np.intaa   )
         snake_case__						= []

         for vector, length in zip(_lowercase     ,		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(_lowercase   )
       else:
         snake_case__						= [(x - x.mean()) / np.sqrt(x.var() + 1e-7   ) for x in input_values]

       return normed_input_values

     def 	SCREAMING_SNAKE_CASE__						(					self:int     ,		_a:Union[str, Any]     ,		):
       snake_case__						= spectrogram(
           _lowercase     ,		window=self.window     ,		frame_length=self.sample_size     ,		hop_length=self.sample_stride     ,		fft_length=self.n_fft     ,		mel_filters=self.mel_filters     ,		mel_floor=self.mel_floor     ,		log_mel='''log10'''     ,		)
       return log_mel_spec.T

     def __call__(					self:List[str]     ,		_a:Optional[int] = None     ,		_a:List[Any] = None     ,		_a:str = False     ,		_a:Any = None     ,		_a:Union[str, Any] = False     ,		_a:List[str] = None     ,		_a:Optional[Any] = None     ,		_a:int = None     ,		_a:int = None     ,		**_a:Optional[Any]     ,		):
       if audio is None and audio_target is None:
         raise ValueError('''You must provide either `audio` or `audio_target` values.'''   )

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

       if audio is not None:
         snake_case__						= self._process_audio(
             _lowercase     ,		_lowercase     ,		_lowercase     ,		_lowercase     ,		_lowercase     ,		_lowercase     ,		_lowercase     ,		_lowercase     ,		**_lowercase     ,		)
       else:
         snake_case__						= None

       if audio_target is not None:
         snake_case__						= self._process_audio(
             _lowercase     ,		_lowercase     ,		_lowercase     ,		_lowercase     ,		_lowercase     ,		_lowercase     ,		_lowercase     ,		_lowercase     ,		**_lowercase     ,		)

         if inputs is None:
           return inputs_target
         else:
           snake_case__						= inputs_target['''input_values''']
           snake_case__						= inputs_target.get('''attention_mask'''   )
           if decoder_attention_mask is not None:
             snake_case__						= decoder_attention_mask

       return inputs

     def 	SCREAMING_SNAKE_CASE__						(					self:Dict     ,		_a:Tuple     ,		_a:Dict = False     ,		_a:Dict = False     ,		_a:int = None     ,		_a:Dict = False     ,		_a:Union[str, Any] = None     ,		_a:Any = None     ,		_a:List[str] = None     ,		**_a:Tuple     ,		):
       snake_case__						= isinstance(_lowercase     ,		np.ndarray   ) and len(speech.shape   ) > 1
       if is_batched_numpy and len(speech.shape   ) > 2:
         raise ValueError(F"""Only mono-channel audio is supported for input to {self}"""   )
       snake_case__						= is_batched_numpy or (
           isinstance(_lowercase     ,		(list, tuple)   ) and (isinstance(speech[0]     ,		(np.ndarray, tuple, list)   ))
       )

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

       # always return batch
       if not is_batched:
         snake_case__						= [speech]

       # needed to make pad() work on spectrogram inputs
       snake_case__						= self.feature_size

       # convert into correct format for padding
       if is_target:
         snake_case__						= [self._extract_mel_features(_lowercase   ) for waveform in speech]
         snake_case__						= BatchFeature({'''input_values''': features}   )
         snake_case__						= self.num_mel_bins
       else:
         snake_case__						= BatchFeature({'''input_values''': speech}   )

       snake_case__						= self.pad(
           _lowercase     ,		padding=_lowercase     ,		max_length=_lowercase     ,		truncation=_lowercase     ,		pad_to_multiple_of=_lowercase     ,		return_attention_mask=_lowercase     ,		**_lowercase     ,		)

       snake_case__						= feature_size_hack

       # convert input values to correct format
       snake_case__						= padded_inputs['''input_values''']
       if not isinstance(input_values[0]     ,		np.ndarray   ):
         snake_case__						= [np.asarray(_lowercase     ,		dtype=np.floataa   ) for array in input_values]
       elif (
           not isinstance(_lowercase     ,		np.ndarray   )
           and isinstance(input_values[0]     ,		np.ndarray   )
           and input_values[0].dtype is np.dtype(np.floataa   )
       ):
         snake_case__						= [array.astype(np.floataa   ) for array in input_values]
       elif isinstance(_lowercase     ,		np.ndarray   ) and input_values.dtype is np.dtype(np.floataa   ):
         snake_case__						= input_values.astype(np.floataa   )

       # convert attention_mask to correct format
       snake_case__						= padded_inputs.get('''attention_mask'''   )
       if attention_mask is not None:
         snake_case__						= [np.asarray(_lowercase     ,		dtype=np.intaa   ) for array in attention_mask]

       # zero-mean and unit-variance normalization
       if not is_target and self.do_normalize:
         snake_case__						= (
             attention_mask
             if self._get_padding_strategies(_lowercase     ,		max_length=_lowercase   ) is not PaddingStrategy.DO_NOT_PAD
             else None
         )
         snake_case__						= self.zero_mean_unit_var_norm(
             padded_inputs['''input_values''']     ,		attention_mask=_lowercase     ,		padding_value=self.padding_value   )

       if return_tensors is not None:
         snake_case__						= padded_inputs.convert_to_tensors(_lowercase   )

       return padded_inputs







     def 	SCREAMING_SNAKE_CASE__						(					self:Optional[Any]   ):
       snake_case__						= super().to_dict()

       # Don't serialize these as they are derived from the other properties.
       snake_case__						= ['''window''', '''mel_filters''', '''sample_size''', '''sample_stride''', '''n_fft''', '''n_freqs''']
       for name in names:
         if name in output:
           del output[name]

       return output




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


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




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

							# Get the sagemaker specific framework parameters from mpi_options variable.
							SCREAMING_SNAKE_CASE_				=				os.getenv('SM_FRAMEWORK_PARAMS'		,'{}'   )
							try:
														# Parse it and check the field "sagemaker_distributed_dataparallel_enabled".
														SCREAMING_SNAKE_CASE_				=				json.loads(lowerCAmelCase__   )
														if not mpi_options.get('sagemaker_mpi_enabled'		,lowerCAmelCase__   ):
																					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    snake_case     (      lowercase_		):






				"""simple docstring"""
				_a    	=     field(
				    default="""""",						metadata={"""help""": """Used by the SageMaker launcher to send mp-specific args. Ignored in SageMakerTrainer"""},						)



				def   a__					(    self       )     ->							Union[str, Any]:
											super().__post_init__()
											warnings.warn(
											    '`SageMakerTrainingArguments` is deprecated and will be removed in v5 of Transformers. You can use '
											    '`TrainingArguments` instead.',     _lowercase,     )



				@cached_property
				def   a__					(    self       )     ->							"torch.device":
											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:
																		SCREAMING_SNAKE_CASE_				=				torch.device('cpu'       )
																		SCREAMING_SNAKE_CASE_				=				0
											elif is_sagemaker_model_parallel_available():
																		SCREAMING_SNAKE_CASE_				=				smp.local_rank()
																		SCREAMING_SNAKE_CASE_				=				torch.device('cuda',     _lowercase       )
																		SCREAMING_SNAKE_CASE_				=				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       )
																		SCREAMING_SNAKE_CASE_				=				int(os.getenv('SMDATAPARALLEL_LOCAL_RANK'       )       )
																		SCREAMING_SNAKE_CASE_				=				torch.device('cuda',     self.local_rank       )
																		SCREAMING_SNAKE_CASE_				=				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
																		SCREAMING_SNAKE_CASE_				=				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.
																		SCREAMING_SNAKE_CASE_				=				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       )
																		SCREAMING_SNAKE_CASE_				=				torch.device('cuda',     self.local_rank       )
																		SCREAMING_SNAKE_CASE_				=				1

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

											return device



				@property
				def   a__					(    self       )     ->							Optional[Any]:
											if is_sagemaker_model_parallel_available():
																		return smp.dp_size()

											return super().world_size



				@property
				def   a__					(    self       )     ->							Optional[Any]:
											return not is_sagemaker_model_parallel_available()





				@property
				def   a__					(    self       )     ->							Tuple:
											return False





