






'''simple docstring'''







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


__lowerCAmelCase       		=      logging.get_logger(__name__)





class 	SCREAMING_SNAKE_CASE					(					SCREAMING_SNAKE_CASE_       ):
			snake_case__  =				["input_features", "attention_mask"]






			def __init__( self      :			Union[str, Any]  ,						__SCREAMING_SNAKE_CASE      :			Any=80  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]=1_6000  ,						__SCREAMING_SNAKE_CASE      :			Dict=80  ,						__SCREAMING_SNAKE_CASE      :			Dict=0.0  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=True  ,						__SCREAMING_SNAKE_CASE      :			Tuple=True  ,						__SCREAMING_SNAKE_CASE      :			List[Any]=True  ,						**__SCREAMING_SNAKE_CASE      :			Union[str, Any]  ,						)			->			Tuple:
							super().__init__(feature_size=__SCREAMING_SNAKE_CASE  ,						sampling_rate=__SCREAMING_SNAKE_CASE  ,						padding_value=__SCREAMING_SNAKE_CASE  ,						**__SCREAMING_SNAKE_CASE					)
							a_		:  Union[str, Any]											=      num_mel_bins
							a_		:  str											=      do_ceptral_normalize
							a_		:  int											=      normalize_means
							a_		:  Optional[int]											=      normalize_vars
							a_		:  Dict											=      True






			def 			SCREAMING_SNAKE_CASE      ( self      :			str  ,						__SCREAMING_SNAKE_CASE      :			np.ndarray  ,						)			->			np.ndarray:
							a_		:  str											=      waveform * (2**15)  # Kaldi compliance: 16-bit signed integers
							a_		:  Union[str, Any]											=      torch.from_numpy(__SCREAMING_SNAKE_CASE					).unsqueeze(0					)
							a_		:  List[Any]											=      ta_kaldi.fbank(__SCREAMING_SNAKE_CASE  ,						num_mel_bins=self.num_mel_bins  ,						sample_frequency=self.sampling_rate					)
							return features.numpy()






			@staticmethod
			def 			SCREAMING_SNAKE_CASE      ( __SCREAMING_SNAKE_CASE      :			np.ndarray  ,						__SCREAMING_SNAKE_CASE      :			int  ,						__SCREAMING_SNAKE_CASE      :			Optional[bool] = True  ,						__SCREAMING_SNAKE_CASE      :			Optional[bool] = True  ,						__SCREAMING_SNAKE_CASE      :			float = 0.0  ,						)			->			np.ndarray:
							# make sure we normalize float32 arrays
							if normalize_means:
											a_		:  Any											=      x[:input_length].mean(axis=0					)
											a_		:  Any											=      np.subtract(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)
							if normalize_vars:
											a_		:  Dict											=      x[:input_length].std(axis=0					)
											a_		:  List[Any]											=      np.divide(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)

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

							# make sure array is in float32
							a_		:  Union[str, Any]											=      x.astype(np.floataa					)

							return x






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			List[np.ndarray]  ,						__SCREAMING_SNAKE_CASE      :			Optional[np.ndarray] = None					)			->			List[np.ndarray]:
							a_		:  Optional[int]											=      attention_mask.sum(-1					) if attention_mask is not None else [x.shape[0] for x in input_features]
							return [
							    self.utterance_cmvn(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						self.normalize_means  ,						self.normalize_vars  ,						self.padding_value					)
							    for x, n in zip(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)
							]







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

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

							a_		:  Dict											=      isinstance(__SCREAMING_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}'					)
							a_		:  int											=      is_batched_numpy or (
							    isinstance(__SCREAMING_SNAKE_CASE  ,						(list, tuple)					) and (isinstance(raw_speech[0]  ,						(np.ndarray, tuple, list)					))
							)

							if is_batched:
											a_		:  Dict											=      [np.asarray(__SCREAMING_SNAKE_CASE  ,						dtype=np.floataa					) for speech in raw_speech]
							elif not is_batched and not isinstance(__SCREAMING_SNAKE_CASE  ,						np.ndarray					):
											a_		:  Optional[int]											=      np.asarray(__SCREAMING_SNAKE_CASE  ,						dtype=np.floataa					)
							elif isinstance(__SCREAMING_SNAKE_CASE  ,						np.ndarray					) and raw_speech.dtype is np.dtype(np.floataa					):
											a_		:  Dict											=      raw_speech.astype(np.floataa					)

							# always return batch
							if not is_batched:
											a_		:  List[Any]											=      [raw_speech]

							# extract fbank features
							a_		:  int											=      [self._extract_fbank_features(__SCREAMING_SNAKE_CASE					) for waveform in raw_speech]

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

							a_		:  int											=      self.pad(
							    __SCREAMING_SNAKE_CASE  ,						padding=__SCREAMING_SNAKE_CASE  ,						max_length=__SCREAMING_SNAKE_CASE  ,						truncation=__SCREAMING_SNAKE_CASE  ,						pad_to_multiple_of=__SCREAMING_SNAKE_CASE  ,						return_attention_mask=__SCREAMING_SNAKE_CASE  ,						**__SCREAMING_SNAKE_CASE  ,						)

							# make sure list is in array format
							a_		:  Optional[Any]											=      padded_inputs.get('''input_features'''					)
							if isinstance(input_features[0]  ,						__SCREAMING_SNAKE_CASE					):
											a_		:  str											=      [np.asarray(__SCREAMING_SNAKE_CASE  ,						dtype=np.floataa					) for feature in input_features]

							a_		:  Union[str, Any]											=      padded_inputs.get('''attention_mask'''					)
							if attention_mask is not None:
											a_		:  str											=      [np.asarray(__SCREAMING_SNAKE_CASE  ,						dtype=np.intaa					) for array in attention_mask]

							# Utterance-level cepstral mean and variance normalization
							if self.do_ceptral_normalize:
											a_		:  List[str]											=      (
											    np.array(__SCREAMING_SNAKE_CASE  ,						dtype=np.intaa					)
											    if self._get_padding_strategies(__SCREAMING_SNAKE_CASE  ,						max_length=__SCREAMING_SNAKE_CASE					) is not PaddingStrategy.DO_NOT_PAD
											    else None
											)
											a_		:  List[Any]											=      self.normalize(
											    padded_inputs['''input_features''']  ,						attention_mask=__SCREAMING_SNAKE_CASE					)

							if return_tensors is not None:
											a_		:  Optional[int]											=      padded_inputs.convert_to_tensors(__SCREAMING_SNAKE_CASE					)

							return padded_inputs












'''simple docstring'''







import gc
import unittest

import numpy as np
import torch

from diffusers import (
    AudioDiffusionPipeline,
    AutoencoderKL,
    DDIMScheduler,
    DDPMScheduler,
    DiffusionPipeline,
    Mel,
    UNetaDConditionModel,
    UNetaDModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()





class 	SCREAMING_SNAKE_CASE					(					unittest.TestCase       ):






			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict					)			->			List[Any]:
							# clean up the VRAM after each test
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()






			@property
			def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple					)			->			List[Any]:
							torch.manual_seed(0					)
							a_		:  Tuple											=      UNetaDModel(
							    sample_size=(32, 64)  ,						in_channels=1  ,						out_channels=1  ,						layers_per_block=2  ,						block_out_channels=(128, 128)  ,						down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''')  ,						up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''')  ,						)
							return model






			@property
			def 			SCREAMING_SNAKE_CASE      ( self      :			int					)			->			List[Any]:
							torch.manual_seed(0					)
							a_		:  Any											=      UNetaDConditionModel(
							    sample_size=(64, 32)  ,						in_channels=1  ,						out_channels=1  ,						layers_per_block=2  ,						block_out_channels=(128, 128)  ,						down_block_types=('''CrossAttnDownBlock2D''', '''DownBlock2D''')  ,						up_block_types=('''UpBlock2D''', '''CrossAttnUpBlock2D''')  ,						cross_attention_dim=10  ,						)
							return model






			@property
			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			List[str]:
							torch.manual_seed(0					)
							a_		:  List[Any]											=      AutoencoderKL(
							    sample_size=(128, 64)  ,						in_channels=1  ,						out_channels=1  ,						latent_channels=1  ,						layers_per_block=2  ,						block_out_channels=(128, 128)  ,						down_block_types=('''DownEncoderBlock2D''', '''DownEncoderBlock2D''')  ,						up_block_types=('''UpDecoderBlock2D''', '''UpDecoderBlock2D''')  ,						)
							a_		:  List[Any]											=      UNetaDModel(
							    sample_size=(64, 32)  ,						in_channels=1  ,						out_channels=1  ,						layers_per_block=2  ,						block_out_channels=(128, 128)  ,						down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''')  ,						up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''')  ,						)
							return vqvae, unet







			@slow
			def 			SCREAMING_SNAKE_CASE      ( self      :			Union[str, Any]					)			->			Union[str, Any]:
							a_		:  Dict											=      '''cpu'''  # ensure determinism for the device-dependent torch.Generator
							a_		:  Union[str, Any]											=      Mel(
							    x_res=self.dummy_unet.config.sample_size[1]  ,						y_res=self.dummy_unet.config.sample_size[0]  ,						)

							a_		:  Any											=      DDPMScheduler()
							a_		:  str											=      AudioDiffusionPipeline(vqvae=__SCREAMING_SNAKE_CASE  ,						unet=self.dummy_unet  ,						mel=__SCREAMING_SNAKE_CASE  ,						scheduler=__SCREAMING_SNAKE_CASE					)
							a_		:  List[Any]											=      pipe.to(__SCREAMING_SNAKE_CASE					)
							pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)

							a_		:  Optional[int]											=      torch.Generator(device=__SCREAMING_SNAKE_CASE					).manual_seed(42					)
							a_		:  List[str]											=      pipe(generator=__SCREAMING_SNAKE_CASE  ,						steps=4					)
							a_		:  List[Any]											=      output.audios[0]
							a_		:  Dict											=      output.images[0]

							a_		:  Dict											=      torch.Generator(device=__SCREAMING_SNAKE_CASE					).manual_seed(42					)
							a_		:  Optional[Any]											=      pipe(generator=__SCREAMING_SNAKE_CASE  ,						steps=4  ,						return_dict=__SCREAMING_SNAKE_CASE					)
							a_		:  Union[str, Any]											=      output[0][0]

							assert audio.shape == (1, (self.dummy_unet.config.sample_size[1] - 1) * mel.hop_length)
							assert (
							    image.height == self.dummy_unet.config.sample_size[0]
							    and image.width == self.dummy_unet.config.sample_size[1]
							)
							a_		:  Dict											=      np.frombuffer(image.tobytes()  ,						dtype='''uint8'''					)[:10]
							a_		:  str											=      np.frombuffer(image_from_tuple.tobytes()  ,						dtype='''uint8'''					)[:10]
							a_		:  List[str]											=      np.array([69, 255, 255, 255, 0, 0, 77, 181, 12, 127]					)

							assert np.abs(image_slice.flatten() - expected_slice					).max() == 0
							assert np.abs(image_from_tuple_slice.flatten() - expected_slice					).max() == 0

							a_		:  str											=      Mel(
							    x_res=self.dummy_vqvae_and_unet[0].config.sample_size[1]  ,						y_res=self.dummy_vqvae_and_unet[0].config.sample_size[0]  ,						)

							a_		:  int											=      DDIMScheduler()
							a_		:  Dict											=      self.dummy_vqvae_and_unet
							a_		:  List[str]											=      AudioDiffusionPipeline(
							    vqvae=self.dummy_vqvae_and_unet[0]  ,						unet=dummy_vqvae_and_unet[1]  ,						mel=__SCREAMING_SNAKE_CASE  ,						scheduler=__SCREAMING_SNAKE_CASE					)
							a_		:  Any											=      pipe.to(__SCREAMING_SNAKE_CASE					)
							pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)

							np.random.seed(0					)
							a_		:  List[str]											=      np.random.uniform(-1  ,						1  ,						((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,)					)
							a_		:  int											=      torch.Generator(device=__SCREAMING_SNAKE_CASE					).manual_seed(42					)
							a_		:  int											=      pipe(raw_audio=__SCREAMING_SNAKE_CASE  ,						generator=__SCREAMING_SNAKE_CASE  ,						start_step=5  ,						steps=10					)
							a_		:  List[str]											=      output.images[0]

							assert (
							    image.height == self.dummy_vqvae_and_unet[0].config.sample_size[0]
							    and image.width == self.dummy_vqvae_and_unet[0].config.sample_size[1]
							)
							a_		:  Optional[Any]											=      np.frombuffer(image.tobytes()  ,						dtype='''uint8'''					)[:10]
							a_		:  List[str]											=      np.array([120, 117, 110, 109, 138, 167, 138, 148, 132, 121]					)

							assert np.abs(image_slice.flatten() - expected_slice					).max() == 0

							a_		:  List[str]											=      self.dummy_unet_condition
							a_		:  Dict											=      AudioDiffusionPipeline(
							    vqvae=self.dummy_vqvae_and_unet[0]  ,						unet=__SCREAMING_SNAKE_CASE  ,						mel=__SCREAMING_SNAKE_CASE  ,						scheduler=__SCREAMING_SNAKE_CASE					)
							a_		:  int											=      pipe.to(__SCREAMING_SNAKE_CASE					)
							pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)

							np.random.seed(0					)
							a_		:  Any											=      torch.rand((1, 1, 10)					)
							a_		:  Tuple											=      pipe(generator=__SCREAMING_SNAKE_CASE  ,						encoding=__SCREAMING_SNAKE_CASE					)
							a_		:  Union[str, Any]											=      output.images[0]
							a_		:  Dict											=      np.frombuffer(image.tobytes()  ,						dtype='''uint8'''					)[:10]
							a_		:  List[str]											=      np.array([107, 103, 120, 127, 142, 122, 113, 122, 97, 111]					)

							assert np.abs(image_slice.flatten() - expected_slice					).max() == 0





@slow
@require_torch_gpu
class 	SCREAMING_SNAKE_CASE					(					unittest.TestCase       ):






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]					)			->			Any:
							# clean up the VRAM after each test
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()







			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			Any:
							a_		:  Any											=      torch_device

							a_		:  Optional[int]											=      DiffusionPipeline.from_pretrained('''teticio/audio-diffusion-ddim-256'''					)
							a_		:  Dict											=      pipe.to(__SCREAMING_SNAKE_CASE					)
							pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)

							a_		:  str											=      torch.Generator(device=__SCREAMING_SNAKE_CASE					).manual_seed(42					)
							a_		:  List[str]											=      pipe(generator=__SCREAMING_SNAKE_CASE					)
							a_		:  Union[str, Any]											=      output.audios[0]
							a_		:  Tuple											=      output.images[0]

							assert audio.shape == (1, (pipe.unet.config.sample_size[1] - 1) * pipe.mel.hop_length)
							assert image.height == pipe.unet.config.sample_size[0] and image.width == pipe.unet.config.sample_size[1]
							a_		:  str											=      np.frombuffer(image.tobytes()  ,						dtype='''uint8'''					)[:10]
							a_		:  Tuple											=      np.array([151, 167, 154, 144, 122, 134, 121, 105, 70, 26]					)

							assert np.abs(image_slice.flatten() - expected_slice					).max() == 0




