






'''simple docstring'''




import math
from dataclasses import dataclass
from typing import List, Optional, Tuple, Union

import numpy as np
import torch

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, logging, randn_tensor
from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin


lowerCamelCase_							      =      logging.get_logger(__name__)  # pylint: disable=invalid-name

@dataclass
# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->EulerAncestralDiscrete
class      _UpperCAmelCase    (  snake_case_		):
   snake_case					       =		42
   snake_case					       =		None





def       __lowercase		(    __lowercase     ,						__lowercase=0.999     ,						__lowercase="cosine"     ,						) ->		List[str]:

      '''simple docstring'''


      if alpha_transform_type == "cosine":

            def alpha_bar_fn(__lowercase ):
                  return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2

      elif alpha_transform_type == "exp":

            def alpha_bar_fn(__lowercase ):
                  return math.exp(t * -12.0 )

      else:
            raise ValueError(F'''Unsupported alpha_tranform_type: {alpha_transform_type}''' )

      _A      =			[]
      for i in range(__lowerCamelCase ):
            _A      =			i / num_diffusion_timesteps
            _A      =			(i + 1) / num_diffusion_timesteps
            betas.append(min(1 - alpha_bar_fn(__lowerCamelCase ) / alpha_bar_fn(__lowerCamelCase )     ,						__lowerCamelCase ) )
      return torch.tensor(__lowerCamelCase     ,						dtype=torch.floataa )


class      _UpperCAmelCase    (  snake_case_							,					snake_case_		):
   snake_case					       =		[e.name for e in KarrasDiffusionSchedulers]
   snake_case					       =		1

   @register_to_config
   def __init__(	self				:			Tuple   ,   __UpperCAmelCase				:			str = 1000   ,   __UpperCAmelCase				:			Optional[Any] = 0.0001   ,   __UpperCAmelCase				:			Optional[Any] = 0.02   ,   __UpperCAmelCase				:			List[str] = "linear"   ,   __UpperCAmelCase				:			Optional[int] = None   ,   __UpperCAmelCase				:			Any = "epsilon"   ,   __UpperCAmelCase				:			Union[str, Any] = "linspace"   ,   __UpperCAmelCase				:			Optional[int] = 0   ,   ):





         '''simple docstring'''





         if trained_betas is not None:
               _A      =			torch.tensor(__UpperCAmelCase   ,   dtype=torch.floataa	)
         elif beta_schedule == "linear":
               _A      =			torch.linspace(__UpperCAmelCase   ,   __UpperCAmelCase   ,   __UpperCAmelCase   ,   dtype=torch.floataa	)
         elif beta_schedule == "scaled_linear":
               # this schedule is very specific to the latent diffusion model.
               _A      =			(
                   torch.linspace(beta_start**0.5   ,   beta_end**0.5   ,   __UpperCAmelCase   ,   dtype=torch.floataa	) ** 2
               )
         elif beta_schedule == "squaredcos_cap_v2":
               # Glide cosine schedule
               _A      =			betas_for_alpha_bar(__UpperCAmelCase	)
         else:
               raise NotImplementedError(f'''{beta_schedule} does is not implemented for {self.__class__}'''	)

         _A      =			1.0 - self.betas
         _A      =			torch.cumprod(self.alphas   ,   dim=0	)

         _A      =			np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5	)
         _A      =			np.concatenate([sigmas[::-1], [0.0]]	).astype(np.floataa	)
         _A      =			torch.from_numpy(__UpperCAmelCase	)

         # setable values
         _A      =			None
         _A      =			np.linspace(0   ,   num_train_timesteps - 1   ,   __UpperCAmelCase   ,   dtype=__UpperCAmelCase	)[::-1].copy()
         _A      =			torch.from_numpy(__UpperCAmelCase	)
         _A      =			False

   @property
   def 		lowerCAmelCase				(	self				:			str	):





         '''simple docstring'''





         if self.config.timestep_spacing in ["linspace", "trailing"]:
               return self.sigmas.max()

         return (self.sigmas.max() ** 2 + 1) ** 0.5

   def 		lowerCAmelCase				(	self				:			List[str]   ,   __UpperCAmelCase				:			Union[str, Any]   ,   __UpperCAmelCase				:			Optional[Any]	):





         '''simple docstring'''





         if isinstance(__UpperCAmelCase   ,   torch.Tensor	):
               _A      =			timestep.to(self.timesteps.device	)
         _A      =			(self.timesteps == timestep).nonzero().item()
         _A      =			self.sigmas[step_index]
         _A      =			sample / ((sigma**2 + 1) ** 0.5)
         _A      =			True
         return sample

   def 		lowerCAmelCase				(	self				:			List[Any]   ,   __UpperCAmelCase				:			str   ,   __UpperCAmelCase				:			List[str] = None	):





         '''simple docstring'''





         _A      =			num_inference_steps

         # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891
         if self.config.timestep_spacing == "linspace":
               _A      =			np.linspace(0   ,   self.config.num_train_timesteps - 1   ,   __UpperCAmelCase   ,   dtype=__UpperCAmelCase	)[
                   ::-1
               ].copy()
         elif self.config.timestep_spacing == "leading":
               _A      =			self.config.num_train_timesteps // self.num_inference_steps
               # creates integer timesteps by multiplying by ratio
               # casting to int to avoid issues when num_inference_step is power of 3
               _A      =			(np.arange(0   ,   __UpperCAmelCase	) * step_ratio).round()[::-1].copy().astype(__UpperCAmelCase	)
               timesteps += self.config.steps_offset
         elif self.config.timestep_spacing == "trailing":
               _A      =			self.config.num_train_timesteps / self.num_inference_steps
               # creates integer timesteps by multiplying by ratio
               # casting to int to avoid issues when num_inference_step is power of 3
               _A      =			(np.arange(self.config.num_train_timesteps   ,   0   ,   -step_ratio	)).round().copy().astype(__UpperCAmelCase	)
               timesteps -= 1
         else:
               raise ValueError(
                   f'''{self.config.timestep_spacing} is not supported. Please make sure to choose one of \'linspace\', \'leading\' or \'trailing\'.'''	)

         _A      =			np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5	)
         _A      =			np.interp(__UpperCAmelCase   ,   np.arange(0   ,   len(__UpperCAmelCase	)	)   ,   __UpperCAmelCase	)
         _A      =			np.concatenate([sigmas, [0.0]]	).astype(np.floataa	)
         _A      =			torch.from_numpy(__UpperCAmelCase	).to(device=__UpperCAmelCase	)
         if str(__UpperCAmelCase	).startswith("mps"	):
               # mps does not support float64
               _A      =			torch.from_numpy(__UpperCAmelCase	).to(__UpperCAmelCase   ,   dtype=torch.floataa	)
         else:
               _A      =			torch.from_numpy(__UpperCAmelCase	).to(device=__UpperCAmelCase	)

   def 		lowerCAmelCase				(	self				:			List[Any]   ,   __UpperCAmelCase				:			List[str]   ,   __UpperCAmelCase				:			Union[str, Any]   ,   __UpperCAmelCase				:			Any   ,   __UpperCAmelCase				:			List[Any] = None   ,   __UpperCAmelCase				:			Dict = True   ,   ):





         '''simple docstring'''





         if (
             isinstance(__UpperCAmelCase   ,   __UpperCAmelCase	)
             or isinstance(__UpperCAmelCase   ,   torch.IntTensor	)
             or isinstance(__UpperCAmelCase   ,   torch.LongTensor	)
         ):
               raise ValueError(
                   (
                       "Passing integer indices (e.g. from `enumerate(timesteps)`) as timesteps to"
                       " `EulerDiscreteScheduler.step()` is not supported. Make sure to pass"
                       " one of the `scheduler.timesteps` as a timestep."
                   )   ,   )

         if not self.is_scale_input_called:
               logger.warning(
                   "The `scale_model_input` function should be called before `step` to ensure correct denoising. "
                   "See `StableDiffusionPipeline` for a usage example."	)

         if isinstance(__UpperCAmelCase   ,   torch.Tensor	):
               _A      =			timestep.to(self.timesteps.device	)

         _A      =			(self.timesteps == timestep).nonzero().item()
         _A      =			self.sigmas[step_index]

         # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise
         if self.config.prediction_type == "epsilon":
               _A      =			sample - sigma * model_output
         elif self.config.prediction_type == "v_prediction":
               # * c_out + input * c_skip
               _A      =			model_output * (-sigma / (sigma**2 + 1) ** 0.5) + (sample / (sigma**2 + 1))
         elif self.config.prediction_type == "sample":
               raise NotImplementedError("prediction_type not implemented yet: sample"	)
         else:
               raise ValueError(
                   f'''prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`'''	)

         _A      =			self.sigmas[step_index]
         _A      =			self.sigmas[step_index + 1]
         _A      =			(sigma_to**2 * (sigma_from**2 - sigma_to**2) / sigma_from**2) ** 0.5
         _A      =			(sigma_to**2 - sigma_up**2) ** 0.5

         # 2. Convert to an ODE derivative
         _A      =			(sample - pred_original_sample) / sigma

         _A      =			sigma_down - sigma

         _A      =			sample + derivative * dt

         _A      =			model_output.device
         _A      =			randn_tensor(model_output.shape   ,   dtype=model_output.dtype   ,   device=__UpperCAmelCase   ,   generator=__UpperCAmelCase	)

         _A      =			prev_sample + noise * sigma_up

         if not return_dict:
               return (prev_sample,)

         return EulerAncestralDiscreteSchedulerOutput(
             prev_sample=__UpperCAmelCase   ,   pred_original_sample=__UpperCAmelCase	)

   def 		lowerCAmelCase				(	self				:			Optional[Any]   ,   __UpperCAmelCase				:			Optional[Any]   ,   __UpperCAmelCase				:			int   ,   __UpperCAmelCase				:			Optional[Any]   ,   ):





         '''simple docstring'''





         _A      =			self.sigmas.to(device=original_samples.device   ,   dtype=original_samples.dtype	)
         if original_samples.device.type == "mps" and torch.is_floating_point(__UpperCAmelCase	):
               # mps does not support float64
               _A      =			self.timesteps.to(original_samples.device   ,   dtype=torch.floataa	)
               _A      =			timesteps.to(original_samples.device   ,   dtype=torch.floataa	)
         else:
               _A      =			self.timesteps.to(original_samples.device	)
               _A      =			timesteps.to(original_samples.device	)

         _A      =			[(schedule_timesteps == t).nonzero().item() for t in timesteps]

         _A      =			sigmas[step_indices].flatten()
         while len(sigma.shape	) < len(original_samples.shape	):
               _A      =			sigma.unsqueeze(-1	)

         _A      =			original_samples + noise * sigma
         return noisy_samples

   def __len__(	self				:			int	):





         '''simple docstring'''





         return self.config.num_train_timesteps













'''simple docstring'''




from typing import List, Optional, Union

import numpy as np

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      _UpperCAmelCase    (  snake_case_		):




   """simple docstring"""

   snake_case					       =		['''input_values''', '''attention_mask''']

   def __init__(	self				:			int   ,   __UpperCAmelCase				:			Optional[int]=1   ,   __UpperCAmelCase				:			List[str]=16000   ,   __UpperCAmelCase				:			Optional[Any]=0.0   ,   __UpperCAmelCase				:			Union[str, Any]=False   ,   __UpperCAmelCase				:			Dict=True   ,   **__UpperCAmelCase				:			int   ,   ):





         '''simple docstring'''





         super().__init__(feature_size=__UpperCAmelCase   ,   sampling_rate=__UpperCAmelCase   ,   padding_value=__UpperCAmelCase   ,   **__UpperCAmelCase	)
         _A      =			return_attention_mask
         _A      =			do_normalize

   @staticmethod
   def 		lowerCAmelCase				(	__UpperCAmelCase				:			List[np.ndarray]   ,   __UpperCAmelCase				:			List[np.ndarray]   ,   __UpperCAmelCase				:			float = 0.0	):





         '''simple docstring'''





         if attention_mask is not None:
               _A      =			np.array(__UpperCAmelCase   ,   np.intaa	)
               _A      =			[]

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

                     normed_input_values.append(__UpperCAmelCase	)
         else:
               _A      =			[(x - x.mean()) / np.sqrt(x.var() + 1E-7	) for x in input_values]

         return normed_input_values

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





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

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

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

         # convert into correct format for padding
         _A      =			BatchFeature({"input_values": raw_speech}	)

         _A      =			self.pad(
             __UpperCAmelCase   ,   padding=__UpperCAmelCase   ,   max_length=__UpperCAmelCase   ,   truncation=__UpperCAmelCase   ,   pad_to_multiple_of=__UpperCAmelCase   ,   return_attention_mask=__UpperCAmelCase   ,   )

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

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

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

         if return_tensors is not None:
               _A      =			padded_inputs.convert_to_tensors(__UpperCAmelCase	)

         return padded_inputs





