


"""simple docstring"""





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


_a														=		logging.get_logger(__name__)






class    _UpperCAmelCase(   lowerCamelCase     ):
		lowercase__      =				['input_values', 'attention_mask']



		def __init__( self    ,		__a = 1    ,		__a = 1_60_00    ,		__a = 0.0    ,		__a = False    ,		__a = 80    ,		__a = 16    ,		__a = 64    ,		__a = "hann_window"    ,		__a = 1.0    ,		__a = 80    ,		__a = 76_00    ,		__a = 1e-10    ,		__a = 2    ,		__a = True    ,		**__a    ,		)	->	Optional[Any]:






							'''simple docstring'''
							super().__init__(feature_size=__a    ,		sampling_rate=__a    ,		padding_value=__a    ,		**__a)
							_UpperCamelCase										=  do_normalize
							_UpperCamelCase										=  return_attention_mask

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

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

							_UpperCamelCase										=  window_function(window_length=self.sample_size    ,		name=self.win_function    ,		periodic=__a)

							_UpperCamelCase										=  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'''    ,		__a    ,		)
							if reduction_factor != 2.0:
												warnings.warn(
												    '''The argument `reduction_factor` is deprecated and will be removed in version 4.30.0 of Transformers'''    ,		__a    ,		)



		@staticmethod
		# Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm
		def 						UpperCAmelCase							( __a    ,		__a    ,		__a = 0.0)	->	List[np.ndarray]:






							'''simple docstring'''
							if attention_mask is not None:
												_UpperCamelCase										=  np.array(__a    ,		np.intaa)
												_UpperCamelCase										=  []

												for vector, length in zip(__a    ,		attention_mask.sum(-1)):
																	_UpperCamelCase										=  (vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1e-7)
																	if length < normed_slice.shape[0]:
																						_UpperCamelCase										=  padding_value

																	normed_input_values.append(__a)
							else:
												_UpperCamelCase										=  [(x - x.mean()) / np.sqrt(x.var() + 1e-7) for x in input_values]

							return normed_input_values



		def 						UpperCAmelCase							( self    ,		__a    ,		)	->	np.ndarray:






							'''simple docstring'''
							_UpperCamelCase										=  spectrogram(
							    __a    ,		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    ,		__a = None    ,		__a = None    ,		__a = False    ,		__a = None    ,		__a = False    ,		__a = None    ,		__a = None    ,		__a = None    ,		__a = None    ,		**__a    ,		)	->	BatchFeature:






							'''simple docstring'''
							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:
												_UpperCamelCase										=  self._process_audio(
												    __a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		**__a    ,		)
							else:
												_UpperCamelCase										=  None

							if audio_target is not None:
												_UpperCamelCase										=  self._process_audio(
												    __a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		**__a    ,		)

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

							return inputs



		def 						UpperCAmelCase							( self    ,		__a    ,		__a = False    ,		__a = False    ,		__a = None    ,		__a = False    ,		__a = None    ,		__a = None    ,		__a = None    ,		**__a    ,		)	->	BatchFeature:






							'''simple docstring'''
							_UpperCamelCase										=  isinstance(__a    ,		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}''')
							_UpperCamelCase										=  is_batched_numpy or (
							    isinstance(__a    ,		(list, tuple)) and (isinstance(speech[0]    ,		(np.ndarray, tuple, list)))
							)

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

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

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

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

							_UpperCamelCase										=  self.pad(
							    __a    ,		padding=__a    ,		max_length=__a    ,		truncation=__a    ,		pad_to_multiple_of=__a    ,		return_attention_mask=__a    ,		**__a    ,		)

							_UpperCamelCase										=  feature_size_hack

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

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

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

							if return_tensors is not None:
												_UpperCamelCase										=  padded_inputs.convert_to_tensors(__a)

							return padded_inputs




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






							'''simple docstring'''
							_UpperCamelCase										=  super().to_dict()

							# Don't serialize these as they are derived from the other properties.
							_UpperCamelCase										=  ['''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"""





from typing import Dict, List, Optional, Tuple, Union

import torch

from ...models import AutoencoderKL, TransformeraDModel
from ...schedulers import KarrasDiffusionSchedulers
from ...utils import randn_tensor
from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput






class    _UpperCAmelCase(   lowerCamelCase     ):



		def __init__( self    ,		__a    ,		__a    ,		__a    ,		__a = None    ,		)	->	List[str]:






							'''simple docstring'''
							super().__init__()
							self.register_modules(transformer=__a    ,		vae=__a    ,		scheduler=__a)

							# create a imagenet -> id dictionary for easier use
							_UpperCamelCase										=  {}
							if idalabel is not None:
												for key, value in idalabel.items():
																	for label in value.split(''','''):
																						_UpperCamelCase										=  int(__a)
												_UpperCamelCase										=  dict(sorted(self.labels.items()))



		def 						UpperCAmelCase							( self    ,		__a)	->	List[int]:






							'''simple docstring'''

							if not isinstance(__a    ,		__a):
												_UpperCamelCase										=  list(__a)

							for l in label:
												if l not in self.labels:
																	raise ValueError(
																	    F'''{l} does not exist. Please make sure to select one of the following labels: \n {self.labels}.''')

							return [self.labels[l] for l in label]




		@torch.no_grad()
		def __call__( self    ,		__a    ,		__a = 4.0    ,		__a = None    ,		__a = 50    ,		__a = "pil"    ,		__a = True    ,		)	->	Union[ImagePipelineOutput, Tuple]:






							'''simple docstring'''
							_UpperCamelCase										=  len(__a)
							_UpperCamelCase										=  self.transformer.config.sample_size
							_UpperCamelCase										=  self.transformer.config.in_channels

							_UpperCamelCase										=  randn_tensor(
							    shape=(batch_size, latent_channels, latent_size, latent_size)    ,		generator=__a    ,		device=self.device    ,		dtype=self.transformer.dtype    ,		)
							_UpperCamelCase										=  torch.cat([latents] * 2) if guidance_scale > 1 else latents

							_UpperCamelCase										=  torch.tensor(__a    ,		device=self.device).reshape(-1)
							_UpperCamelCase										=  torch.tensor([10_00] * batch_size    ,		device=self.device)
							_UpperCamelCase										=  torch.cat([class_labels, class_null]    ,		0) if guidance_scale > 1 else class_labels

							# set step values
							self.scheduler.set_timesteps(__a)

							for t in self.progress_bar(self.scheduler.timesteps):
												if guidance_scale > 1:
																	_UpperCamelCase										=  latent_model_input[: len(__a) // 2]
																	_UpperCamelCase										=  torch.cat([half, half]    ,		dim=0)
												_UpperCamelCase										=  self.scheduler.scale_model_input(__a    ,		__a)

												_UpperCamelCase										=  t
												if not torch.is_tensor(__a):
																	# TODO: this requires sync between CPU and GPU. So try to pass timesteps as tensors if you can
																	# This would be a good case for the `match` statement (Python 3.10+)
																	_UpperCamelCase										=  latent_model_input.device.type == '''mps'''
																	if isinstance(__a    ,		__a):
																						_UpperCamelCase										=  torch.floataa if is_mps else torch.floataa
																	else:
																						_UpperCamelCase										=  torch.intaa if is_mps else torch.intaa
																	_UpperCamelCase										=  torch.tensor([timesteps]    ,		dtype=__a    ,		device=latent_model_input.device)
												elif len(timesteps.shape) == 0:
																	_UpperCamelCase										=  timesteps[None].to(latent_model_input.device)
												# broadcast to batch dimension in a way that's compatible with ONNX/Core ML
												_UpperCamelCase										=  timesteps.expand(latent_model_input.shape[0])
												# predict noise model_output
												_UpperCamelCase										=  self.transformer(
												    __a    ,		timestep=__a    ,		class_labels=__a).sample

												# perform guidance
												if guidance_scale > 1:
																	_UpperCamelCase			,  _UpperCamelCase										=  noise_pred[:, :latent_channels], noise_pred[:, latent_channels:]
																	_UpperCamelCase			,  _UpperCamelCase										=  torch.split(__a    ,		len(__a) // 2    ,		dim=0)

																	_UpperCamelCase										=  uncond_eps + guidance_scale * (cond_eps - uncond_eps)
																	_UpperCamelCase										=  torch.cat([half_eps, half_eps]    ,		dim=0)

																	_UpperCamelCase										=  torch.cat([eps, rest]    ,		dim=1)

												# learned sigma
												if self.transformer.config.out_channels // 2 == latent_channels:
																	_UpperCamelCase			,  _UpperCamelCase										=  torch.split(__a    ,		__a    ,		dim=1)
												else:
																	_UpperCamelCase										=  noise_pred

												# compute previous image: x_t -> x_t-1
												_UpperCamelCase										=  self.scheduler.step(__a    ,		__a    ,		__a).prev_sample

							if guidance_scale > 1:
												_UpperCamelCase			,  _UpperCamelCase										=  latent_model_input.chunk(2    ,		dim=0)
							else:
												_UpperCamelCase										=  latent_model_input

							_UpperCamelCase										=  1 / self.vae.config.scaling_factor * latents
							_UpperCamelCase										=  self.vae.decode(__a).sample

							_UpperCamelCase										=  (samples / 2 + 0.5).clamp(0    ,		1)

							# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
							_UpperCamelCase										=  samples.cpu().permute(0    ,		2    ,		3    ,		1).float().numpy()

							if output_type == "pil":
												_UpperCamelCase										=  self.numpy_to_pil(__a)

							if not return_dict:
												return (samples,)

							return ImagePipelineOutput(images=__a)






