



'''simple docstring'''

import inspect
import os

import torch
from transformers import AutoModel
from transformers.testing_utils import mockenv_context
from transformers.trainer_utils import set_seed

import accelerate
from accelerate.accelerator import Accelerator
from accelerate.state import AcceleratorState
from accelerate.test_utils.testing import (
    AccelerateTestCase,
    TempDirTestCase,
    execute_subprocess_async,
    require_cuda,
    require_fsdp,
    require_multi_gpu,
    slow,
)
from accelerate.utils.constants import (
    FSDP_AUTO_WRAP_POLICY,
    FSDP_BACKWARD_PREFETCH,
    FSDP_SHARDING_STRATEGY,
    FSDP_STATE_DICT_TYPE,
)
from accelerate.utils.dataclasses import FullyShardedDataParallelPlugin
from accelerate.utils.other import patch_environment


set_seed(4_2)

_A				:    Tuple     						='''bert-base-cased'''
_A				:    Optional[Any]     						='''fp16'''
_A				:    Optional[Any]     						='''bf16'''
_A				:    Tuple     						=[FPaa, BFaa]



@require_fsdp
@require_cuda
class 		lowerCamelCase__      (						A     ):




					'''simple docstring'''



					def 						__UpperCAmelCase     (  self				:	Union[str, Any]					)  ->       int:



						'''simple docstring'''





						super().setUp()

						_lowercase					:       Dict       =		dict(
						    ACCELERATE_USE_FSDP='true'     ,  MASTER_ADDR='localhost'     ,  MASTER_PORT='10999'     ,  RANK='0'     ,  LOCAL_RANK='0'     ,  WORLD_SIZE='1'     ,  )




					def 						__UpperCAmelCase     (  self				:	Dict					)  ->       List[Any]:



						'''simple docstring'''





						from torch.distributed.fsdp.fully_sharded_data_parallel import ShardingStrategy

						for i, strategy in enumerate(UpperCamelCase_					):
							_lowercase					:       Optional[int]       =		self.dist_env.copy()
							_lowercase					:       str       =		F'''{i + 1}'''
							_lowercase					:       List[str]       =		strategy
							with mockenv_context(**UpperCamelCase_					):
								_lowercase					:       Any       =		FullyShardedDataParallelPlugin()
								self.assertEqual(fsdp_plugin.sharding_strategy     ,  ShardingStrategy(i + 1					)					)




					def 						__UpperCAmelCase     (  self				:	str					)  ->       Optional[int]:



						'''simple docstring'''





						from torch.distributed.fsdp.fully_sharded_data_parallel import BackwardPrefetch

						for i, prefetch_policy in enumerate(UpperCamelCase_					):
							_lowercase					:       Tuple       =		self.dist_env.copy()
							_lowercase					:       Optional[int]       =		prefetch_policy
							with mockenv_context(**UpperCamelCase_					):
								_lowercase					:       Optional[int]       =		FullyShardedDataParallelPlugin()
								if prefetch_policy == "NO_PREFETCH":
									self.assertIsNone(fsdp_plugin.backward_prefetch					)
								else:
									self.assertEqual(fsdp_plugin.backward_prefetch     ,  BackwardPrefetch(i + 1					)					)




					def 						__UpperCAmelCase     (  self				:	Dict					)  ->       List[Any]:



						'''simple docstring'''





						from torch.distributed.fsdp.fully_sharded_data_parallel import StateDictType

						for i, state_dict_type in enumerate(UpperCamelCase_					):
							_lowercase					:       List[Any]       =		self.dist_env.copy()
							_lowercase					:       Tuple       =		state_dict_type
							with mockenv_context(**UpperCamelCase_					):
								_lowercase					:       Tuple       =		FullyShardedDataParallelPlugin()
								self.assertEqual(fsdp_plugin.state_dict_type     ,  StateDictType(i + 1					)					)
								if state_dict_type == "FULL_STATE_DICT":
									self.assertTrue(fsdp_plugin.state_dict_config.offload_to_cpu					)
									self.assertTrue(fsdp_plugin.state_dict_config.ranka_only					)




					def 						__UpperCAmelCase     (  self				:	Optional[int]					)  ->       str:



						'''simple docstring'''





						_lowercase					:       Any       =		AutoModel.from_pretrained(UpperCamelCase_					)
						for policy in FSDP_AUTO_WRAP_POLICY:
							_lowercase					:       Dict       =		self.dist_env.copy()
							_lowercase					:       int       =		policy
							if policy == "TRANSFORMER_BASED_WRAP":
								_lowercase					:       List[str]       =		'BertLayer'
							elif policy == "SIZE_BASED_WRAP":
								_lowercase					:       List[str]       =		'2000'
							with mockenv_context(**UpperCamelCase_					):
								_lowercase					:       List[Any]       =		FullyShardedDataParallelPlugin()
								fsdp_plugin.set_auto_wrap_policy(UpperCamelCase_					)
								if policy == "NO_WRAP":
									self.assertIsNone(fsdp_plugin.auto_wrap_policy					)
								else:
									self.assertIsNotNone(fsdp_plugin.auto_wrap_policy					)

						_lowercase					:       Optional[Any]       =		self.dist_env.copy()
						_lowercase					:       Optional[int]       =		'TRANSFORMER_BASED_WRAP'
						_lowercase					:       int       =		'T5Layer'
						with mockenv_context(**UpperCamelCase_					):
							_lowercase					:       Tuple       =		FullyShardedDataParallelPlugin()
							with self.assertRaises(UpperCamelCase_					) as cm:
								fsdp_plugin.set_auto_wrap_policy(UpperCamelCase_					)
							self.assertTrue('Could not find the transformer layer class to wrap in the model.' in str(cm.exception					)					)

						_lowercase					:       Optional[Any]       =		self.dist_env.copy()
						_lowercase					:       Optional[Any]       =		'SIZE_BASED_WRAP'
						_lowercase					:       Any       =		'0'
						with mockenv_context(**UpperCamelCase_					):
							_lowercase					:       Optional[int]       =		FullyShardedDataParallelPlugin()
							fsdp_plugin.set_auto_wrap_policy(UpperCamelCase_					)
							self.assertIsNone(fsdp_plugin.auto_wrap_policy					)




					def 						__UpperCAmelCase     (  self				:	Optional[int]					)  ->       Any:



						'''simple docstring'''





						from torch.distributed.fsdp.fully_sharded_data_parallel import MixedPrecision
						from torch.distributed.fsdp.sharded_grad_scaler import ShardedGradScaler

						for mp_dtype in dtypes:
							_lowercase					:       Union[str, Any]       =		self.dist_env.copy()
							_lowercase					:       Tuple       =		mp_dtype
							with mockenv_context(**UpperCamelCase_					):
								_lowercase					:       Union[str, Any]       =		Accelerator()
								if mp_dtype == "fp16":
									_lowercase					:       int       =		torch.floataa
								elif mp_dtype == "bf16":
									_lowercase					:       int       =		torch.bfloataa
								_lowercase					:       Tuple       =		MixedPrecision(param_dtype=UpperCamelCase_     ,  reduce_dtype=UpperCamelCase_     ,  buffer_dtype=UpperCamelCase_					)
								self.assertEqual(accelerator.state.fsdp_plugin.mixed_precision_policy     ,  UpperCamelCase_					)
								if mp_dtype == FPaa:
									self.assertTrue(isinstance(accelerator.scaler     ,  UpperCamelCase_					)					)
								elif mp_dtype == BFaa:
									self.assertIsNone(accelerator.scaler					)
								AcceleratorState._reset_state(UpperCamelCase_					)




					def 						__UpperCAmelCase     (  self				:	Optional[Any]					)  ->       str:



						'''simple docstring'''





						from torch.distributed.fsdp.fully_sharded_data_parallel import CPUOffload

						for flag in [True, False]:
							_lowercase					:       List[Any]       =		self.dist_env.copy()
							_lowercase					:       Tuple       =		str(UpperCamelCase_					).lower()
							with mockenv_context(**UpperCamelCase_					):
								_lowercase					:       Optional[int]       =		FullyShardedDataParallelPlugin()
								self.assertEqual(fsdp_plugin.cpu_offload     ,  CPUOffload(offload_params=UpperCamelCase_					)					)






@require_fsdp
@require_multi_gpu
@slow
class 		lowerCamelCase__      (						A     ):




					'''simple docstring'''



					def 						__UpperCAmelCase     (  self				:	int					)  ->       Union[str, Any]:



						'''simple docstring'''





						super().setUp()
						_lowercase					:       Any       =		0.82
						_lowercase					:       str       =		[
						    'fsdp_shard_grad_op_transformer_based_wrap',
						    'fsdp_full_shard_transformer_based_wrap',
						]
						_lowercase					:       str       =		{
						    'multi_gpu_fp16': 3200,
						    'fsdp_shard_grad_op_transformer_based_wrap_fp16': 2000,
						    'fsdp_full_shard_transformer_based_wrap_fp16': 1900,
						    # Disabling below test as it overwhelms the RAM memory usage
						    # on CI self-hosted runner leading to tests getting killed.
						    # "fsdp_full_shard_cpu_offload_transformer_based_wrap_fp32": 1500,  # fp16 was leading to indefinite hang
						}
						_lowercase					:       int       =		160
						_lowercase					:       Dict       =		160

						_lowercase					:       Union[str, Any]       =		inspect.getfile(accelerate.test_utils					)
						_lowercase					:       List[str]       =		os.path.sep.join(mod_file.split(os.path.sep					)[:-1] + ['scripts', 'external_deps']					)




					def 						__UpperCAmelCase     (  self				:	Optional[int]					)  ->       Union[str, Any]:



						'''simple docstring'''





						_lowercase					:       Union[str, Any]       =		os.path.join(self.test_scripts_folder     ,  'test_performance.py'					)
						_lowercase					:       Any       =		['accelerate', 'launch', '--num_processes=2', '--num_machines=1', '--machine_rank=0', '--use_fsdp']
						for config in self.performance_configs:
							_lowercase					:       Union[str, Any]       =		cmd.copy()
							for i, strategy in enumerate(UpperCamelCase_					):
								if strategy.lower() in config:
									cmd_config.append(F'''--fsdp_sharding_strategy={i+1}'''					)
									break

							if "fp32" in config:
								cmd_config.append('--mixed_precision=no'					)
							else:
								cmd_config.append('--mixed_precision=fp16'					)

							if "cpu_offload" in config:
								cmd_config.append('--fsdp_offload_params=True'					)

							for policy in FSDP_AUTO_WRAP_POLICY:
								if policy.lower() in config:
									cmd_config.append(F'''--fsdp_auto_wrap_policy={policy}'''					)
									break

							if policy == "TRANSFORMER_BASED_WRAP":
								cmd_config.append('--fsdp_transformer_layer_cls_to_wrap=BertLayer'					)
							elif policy == "SIZE_BASED_WRAP":
								cmd_config.append('--fsdp_min_num_params=2000'					)

							cmd_config.extend(
							    [
							        self.test_file_path,
							        F'''--output_dir={self.tmpdir}''',
							        F'''--performance_lower_bound={self.performance_lower_bound}''',
							    ]					)
							with patch_environment(omp_num_threads=1					):
								execute_subprocess_async(UpperCamelCase_     ,  env=os.environ.copy()					)




					def 						__UpperCAmelCase     (  self				:	str					)  ->       Optional[Any]:



						'''simple docstring'''





						_lowercase					:       Optional[Any]       =		os.path.join(self.test_scripts_folder     ,  'test_checkpointing.py'					)
						_lowercase					:       Dict       =		[
						    'accelerate',
						    'launch',
						    '--num_processes=2',
						    '--num_machines=1',
						    '--machine_rank=0',
						    '--use_fsdp',
						    '--mixed_precision=fp16',
						    '--fsdp_transformer_layer_cls_to_wrap=BertLayer',
						]

						for i, strategy in enumerate(UpperCamelCase_					):
							_lowercase					:       Any       =		cmd.copy()
							cmd_config.append(F'''--fsdp_sharding_strategy={i+1}'''					)
							if strategy != "FULL_SHARD":
								continue
							_lowercase					:       Any       =		len(UpperCamelCase_					)
							for state_dict_type in FSDP_STATE_DICT_TYPE:
								_lowercase					:       str       =		cmd_config[:state_dict_config_index]
								cmd_config.append(F'''--fsdp_state_dict_type={state_dict_type}'''					)
								cmd_config.extend(
								    [
								        self.test_file_path,
								        F'''--output_dir={self.tmpdir}''',
								        '--partial_train_epoch=1',
								    ]					)
								with patch_environment(omp_num_threads=1					):
									execute_subprocess_async(UpperCamelCase_     ,  env=os.environ.copy()					)

								_lowercase					:       List[str]       =		cmd_config[:-1]
								_lowercase					:       Dict       =		os.path.join(self.tmpdir     ,  'epoch_0'					)
								cmd_config.extend(
								    [
								        F'''--resume_from_checkpoint={resume_from_checkpoint}''',
								    ]					)
								with patch_environment(omp_num_threads=1					):
									execute_subprocess_async(UpperCamelCase_     ,  env=os.environ.copy()					)




					def 						__UpperCAmelCase     (  self				:	Dict					)  ->       Dict:



						'''simple docstring'''





						_lowercase					:       Optional[int]       =		os.path.join(self.test_scripts_folder     ,  'test_peak_memory_usage.py'					)
						_lowercase					:       str       =		[
						    'accelerate',
						    'launch',
						    '--num_processes=2',
						    '--num_machines=1',
						    '--machine_rank=0',
						]
						for spec, peak_mem_upper_bound in self.peak_memory_usage_upper_bound.items():
							_lowercase					:       Any       =		cmd.copy()
							if "fp16" in spec:
								cmd_config.extend(['--mixed_precision=fp16']					)
							else:
								cmd_config.extend(['--mixed_precision=no']					)

							if "multi_gpu" in spec:
								continue
							else:
								cmd_config.extend(['--use_fsdp']					)
								for i, strategy in enumerate(UpperCamelCase_					):
									if strategy.lower() in spec:
										cmd_config.append(F'''--fsdp_sharding_strategy={i+1}'''					)
										break

								if "cpu_offload" in spec:
									cmd_config.append('--fsdp_offload_params=True'					)

								for policy in FSDP_AUTO_WRAP_POLICY:
									if policy.lower() in spec:
										cmd_config.append(F'''--fsdp_auto_wrap_policy={policy}'''					)
										break

								if policy == "TRANSFORMER_BASED_WRAP":
									cmd_config.append('--fsdp_transformer_layer_cls_to_wrap=BertLayer'					)
								elif policy == "SIZE_BASED_WRAP":
									cmd_config.append('--fsdp_min_num_params=2000'					)

							cmd_config.extend(
							    [
							        self.test_file_path,
							        F'''--output_dir={self.tmpdir}''',
							        F'''--peak_memory_upper_bound={peak_mem_upper_bound}''',
							        F'''--n_train={self.n_train}''',
							        F'''--n_val={self.n_val}''',
							    ]					)
							with patch_environment(omp_num_threads=1					):
								execute_subprocess_async(UpperCamelCase_     ,  env=os.environ.copy()					)





'''simple docstring'''

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

import numpy as np
import torch

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


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



class 		lowerCamelCase__      (						A     ):




					'''simple docstring'''



					A_						    =						["""input_features""", """is_longer"""]




					def __init__(  self				:	List[Any]     ,  UpperCamelCase_				:	List[Any]=64     ,  UpperCamelCase_				:	int=4_8000     ,  UpperCamelCase_				:	Union[str, Any]=480     ,  UpperCamelCase_				:	Any=10     ,  UpperCamelCase_				:	Optional[int]=1024     ,  UpperCamelCase_				:	Optional[int]=0.0     ,  UpperCamelCase_				:	Tuple=False     ,  UpperCamelCase_				:	float = 0     ,  UpperCamelCase_				:	float = 1_4000     ,  UpperCamelCase_				:	int = None     ,  UpperCamelCase_				:	str = "fusion"     ,  UpperCamelCase_				:	str = "repeatpad"     ,  **UpperCamelCase_				:	Optional[Any]     ,  )  ->       Dict:



						'''simple docstring'''





						super().__init__(
						    feature_size=UpperCamelCase_     ,  sampling_rate=UpperCamelCase_     ,  padding_value=UpperCamelCase_     ,  return_attention_mask=UpperCamelCase_     ,  **UpperCamelCase_     ,  )
						_lowercase					:       Tuple       =		top_db
						_lowercase					:       Any       =		truncation
						_lowercase					:       str       =		padding
						_lowercase					:       int       =		fft_window_size
						_lowercase					:       Any       =		(fft_window_size >> 1) + 1
						_lowercase					:       int       =		hop_length
						_lowercase					:       Any       =		max_length_s
						_lowercase					:       str       =		max_length_s * sampling_rate
						_lowercase					:       Any       =		sampling_rate
						_lowercase					:       List[Any]       =		frequency_min
						_lowercase					:       Tuple       =		frequency_max
						_lowercase					:       Tuple       =		mel_filter_bank(
						    num_frequency_bins=self.nb_frequency_bins     ,  num_mel_filters=UpperCamelCase_     ,  min_frequency=UpperCamelCase_     ,  max_frequency=UpperCamelCase_     ,  sampling_rate=UpperCamelCase_     ,  norm=UpperCamelCase_     ,  mel_scale='htk'     ,  )
						_lowercase					:       Any       =		mel_filter_bank(
						    num_frequency_bins=self.nb_frequency_bins     ,  num_mel_filters=UpperCamelCase_     ,  min_frequency=UpperCamelCase_     ,  max_frequency=UpperCamelCase_     ,  sampling_rate=UpperCamelCase_     ,  norm='slaney'     ,  mel_scale='slaney'     ,  )




					def 						__UpperCAmelCase     (  self				:	Tuple					)  ->       Dict[str, Any]:



						'''simple docstring'''





						_lowercase					:       Tuple       =		copy.deepcopy(self.__dict__					)
						_lowercase					:       int       =		self.__class__.__name__
						if "mel_filters" in output:
							del output["mel_filters"]
						if "mel_filters_slaney" in output:
							del output["mel_filters_slaney"]
						return output




					def 						__UpperCAmelCase     (  self				:	Dict     ,  UpperCamelCase_				:	np.array     ,  UpperCamelCase_				:	Optional[np.array] = None					)  ->       np.ndarray:



						'''simple docstring'''





						_lowercase					:       List[str]       =		spectrogram(
						    UpperCamelCase_     ,  window_function(self.fft_window_size     ,  'hann'					)     ,  frame_length=self.fft_window_size     ,  hop_length=self.hop_length     ,  power=2.0     ,  mel_filters=UpperCamelCase_     ,  log_mel='dB'     ,  )
						return log_mel_spectrogram.T




					def 						__UpperCAmelCase     (  self				:	List[Any]     ,  UpperCamelCase_				:	Any     ,  UpperCamelCase_				:	List[Any]     ,  UpperCamelCase_				:	Union[str, Any]					)  ->       Optional[int]:



						'''simple docstring'''





						_lowercase					:       Tuple       =		np.array_split(list(range(0     ,  total_frames - chunk_frames + 1					)					)     ,  3					)
						if len(ranges[1]					) == 0:
							# if the audio is too short, we just use the first chunk
							_lowercase					:       int       =		[0]
						if len(ranges[2]					) == 0:
							# if the audio is too short, we just use the first chunk
							_lowercase					:       Union[str, Any]       =		[0]
						# randomly choose index for each part
						_lowercase					:       Tuple       =		np.random.choice(ranges[0]					)
						_lowercase					:       int       =		np.random.choice(ranges[1]					)
						_lowercase					:       Any       =		np.random.choice(ranges[2]					)

						_lowercase					:       int       =		mel[idx_front : idx_front + chunk_frames, :]
						_lowercase					:       int       =		mel[idx_middle : idx_middle + chunk_frames, :]
						_lowercase					:       Tuple       =		mel[idx_back : idx_back + chunk_frames, :]

						_lowercase					:       List[Any]       =		torch.tensor(mel[None, None, :]					)
						_lowercase					:       Optional[int]       =		torch.nn.functional.interpolate(
						    UpperCamelCase_     ,  size=[chunk_frames, 64]     ,  mode='bilinear'     ,  align_corners=UpperCamelCase_					)
						_lowercase					:       str       =		mel_shrink[0][0].numpy()
						_lowercase					:       int       =		np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back]     ,  axis=0					)
						return mel_fusion




					def 						__UpperCAmelCase     (  self				:	List[str]     ,  UpperCamelCase_				:	np.array     ,  UpperCamelCase_				:	List[Any]     ,  UpperCamelCase_				:	int     ,  UpperCamelCase_				:	Optional[int]					)  ->       np.array:



						'''simple docstring'''





						if waveform.shape[0] > max_length:
							if truncation == "rand_trunc":
								_lowercase					:       Tuple       =		True
								# random crop to max_length (for compatibility) -> this should be handled by self.pad
								_lowercase					:       Any       =		len(UpperCamelCase_					) - max_length
								_lowercase					:       Dict       =		np.random.randint(0     ,  overflow + 1					)
								_lowercase					:       Optional[int]       =		waveform[idx : idx + max_length]
								_lowercase					:       Dict       =		self._np_extract_fbank_features(UpperCamelCase_     ,  self.mel_filters_slaney					)[None, :]
							elif truncation == "fusion":
								_lowercase					:       List[Any]       =		self._np_extract_fbank_features(UpperCamelCase_     ,  self.mel_filters					)
								_lowercase					:       List[Any]       =		max_length // self.hop_length + 1  # the +1 related to how the spectrogram is computed
								_lowercase					:       Optional[int]       =		mel.shape[0]
								if chunk_frames == total_frames:
									# there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length.
									# In this case, we just use the whole audio.
									_lowercase					:       Optional[Any]       =		np.stack([mel, mel, mel, mel]     ,  axis=0					)
									_lowercase					:       List[Any]       =		False
								else:
									_lowercase					:       Union[str, Any]       =		self._random_mel_fusion(UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_					)
									_lowercase					:       int       =		True
							else:
								raise NotImplementedError(F'''data_truncating {truncation} not implemented'''					)

						else:
							_lowercase					:       Any       =		False
							# only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding
							if waveform.shape[0] < max_length:
								if padding == "repeat":
									_lowercase					:       List[Any]       =		int(max_length / len(UpperCamelCase_					)					)
									_lowercase					:       List[str]       =		np.stack(np.tile(UpperCamelCase_     ,  n_repeat + 1					)					)[:max_length]
								if padding == "repeatpad":
									_lowercase					:       Union[str, Any]       =		int(max_length / len(UpperCamelCase_					)					)
									_lowercase					:       Union[str, Any]       =		np.stack(np.tile(UpperCamelCase_     ,  UpperCamelCase_					)					)
								_lowercase					:       Dict       =		np.pad(UpperCamelCase_     ,  (0, max_length - waveform.shape[0])     ,  mode='constant'     ,  constant_values=0					)

							if truncation == "fusion":
								_lowercase					:       str       =		self._np_extract_fbank_features(UpperCamelCase_     ,  self.mel_filters					)
								_lowercase					:       Dict       =		np.stack([input_mel, input_mel, input_mel, input_mel]     ,  axis=0					)
							else:
								_lowercase					:       List[Any]       =		self._np_extract_fbank_features(UpperCamelCase_     ,  self.mel_filters_slaney					)[None, :]

						return input_mel, longer




					def __call__(  self				:	Union[str, Any]     ,  UpperCamelCase_				:	Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]     ,  UpperCamelCase_				:	str = None     ,  UpperCamelCase_				:	Optional[str] = None     ,  UpperCamelCase_				:	Optional[int] = None     ,  UpperCamelCase_				:	Optional[int] = None     ,  UpperCamelCase_				:	Optional[Union[str, TensorType]] = None     ,  **UpperCamelCase_				:	Dict     ,  )  ->       BatchFeature:



						'''simple docstring'''





						_lowercase					:       Dict       =		truncation if truncation is not None else self.truncation
						_lowercase					:       int       =		padding if padding else self.padding

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

						_lowercase					:       Optional[Any]       =		isinstance(UpperCamelCase_     ,  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}'''					)
						_lowercase					:       List[str]       =		is_batched_numpy or (
						    isinstance(UpperCamelCase_     ,  (list, tuple)					) and (isinstance(raw_speech[0]     ,  (np.ndarray, tuple, list)					))
						)

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

						# always return batch
						if not is_batched:
							_lowercase					:       int       =		[np.asarray(UpperCamelCase_					)]

						# convert to mel spectrogram, truncate and pad if needed.
						_lowercase					:       Optional[Any]       =		[
						    self._get_input_mel(UpperCamelCase_     ,  max_length if max_length else self.nb_max_samples     ,  UpperCamelCase_     ,  UpperCamelCase_					)
						    for waveform in raw_speech
						]

						_lowercase					:       List[Any]       =		[]
						_lowercase					:       Dict       =		[]
						for mel, longer in padded_inputs:
							input_mel.append(UpperCamelCase_					)
							is_longer.append(UpperCamelCase_					)

						if truncation == "fusion" and sum(UpperCamelCase_					) == 0:
							# if no audio is longer than 10s, then randomly select one audio to be longer
							_lowercase					:       Optional[Any]       =		np.random.randint(0     ,  len(UpperCamelCase_					)					)
							_lowercase					:       str       =		True

						if isinstance(input_mel[0]     ,  UpperCamelCase_					):
							_lowercase					:       str       =		[np.asarray(UpperCamelCase_     ,  dtype=np.floataa					) for feature in input_mel]

						# is_longer is a list of bool
						_lowercase					:       Tuple       =		[[longer] for longer in is_longer]

						_lowercase					:       Optional[Any]       =		{'input_features': input_mel, 'is_longer': is_longer}
						_lowercase					:       Optional[int]       =		BatchFeature(UpperCamelCase_					)

						if return_tensors is not None:
							_lowercase					:       List[Any]       =		input_features.convert_to_tensors(UpperCamelCase_					)

						return input_features
