import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionSAGPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()
class     a_			(						SCREAMING_SNAKE_CASE	,					SCREAMING_SNAKE_CASE	,					unittest.TestCase						):






 '''simple docstring'''






 UpperCAmelCase_     			= StableDiffusionSAGPipeline
 UpperCAmelCase_     			= TEXT_TO_IMAGE_PARAMS
 UpperCAmelCase_     			= TEXT_TO_IMAGE_BATCH_PARAMS
 UpperCAmelCase_     			= TEXT_TO_IMAGE_IMAGE_PARAMS
 UpperCAmelCase_     			= TEXT_TO_IMAGE_IMAGE_PARAMS
 UpperCAmelCase_     			= False




 def 		__snake_case						(      self :  int):







        '''simple docstring'''



        torch.manual_seed(0)
        lowerCAmelCase__					     = UNetaDConditionModel(
            block_out_channels=(32, 64)      ,   layers_per_block=2      ,   sample_size=32      ,   in_channels=4      ,   out_channels=4      ,   down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')      ,   up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')      ,   cross_attention_dim=32      ,   )
        lowerCAmelCase__					     = DDIMScheduler(
            beta_start=0.00_085      ,   beta_end=0.012      ,   beta_schedule='scaled_linear'      ,   clip_sample=lowercase__      ,   set_alpha_to_one=lowercase__      ,   )
        torch.manual_seed(0)
        lowerCAmelCase__					     = AutoencoderKL(
            block_out_channels=[32, 64]      ,   in_channels=3      ,   out_channels=3      ,   down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']      ,   up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']      ,   latent_channels=4      ,   )
        torch.manual_seed(0)
        lowerCAmelCase__					     = CLIPTextConfig(
            bos_token_id=0      ,   eos_token_id=2      ,   hidden_size=32      ,   intermediate_size=37      ,   layer_norm_eps=1e-05      ,   num_attention_heads=4      ,   num_hidden_layers=5      ,   pad_token_id=1      ,   vocab_size=1_000      ,   )
        lowerCAmelCase__					     = CLIPTextModel(lowercase__)
        lowerCAmelCase__					     = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip')

        lowerCAmelCase__					     = {
            'unet': unet,
            'scheduler': scheduler,
            'vae': vae,
            'text_encoder': text_encoder,
            'tokenizer': tokenizer,
            'safety_checker': None,
            'feature_extractor': None,
        }
        return components




 def 		__snake_case						(      self :  Dict      ,   lowercase__ :  List[Any]      ,   lowercase__ :  int=0):







        '''simple docstring'''



        if str(lowercase__).startswith('mps'):
               lowerCAmelCase__					     = torch.manual_seed(lowercase__)
        else:
               lowerCAmelCase__					     = torch.Generator(device=lowercase__).manual_seed(lowercase__)
        lowerCAmelCase__					     = {
            'prompt': '.',
            'generator': generator,
            'num_inference_steps': 2,
            'guidance_scale': 1.0,
            'sag_scale': 1.0,
            'output_type': 'numpy',
        }
        return inputs




 def 		__snake_case						(      self :  Optional[Any]):







        '''simple docstring'''



        super().test_inference_batch_single_identical(expected_max_diff=3e-3)






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






 '''simple docstring'''






 def 		__snake_case						(      self :  int):







        '''simple docstring'''



        super().tearDown()
        gc.collect()
        torch.cuda.empty_cache()




 def 		__snake_case						(      self :  List[Any]):







        '''simple docstring'''



        lowerCAmelCase__					     = StableDiffusionSAGPipeline.from_pretrained('CompVis/stable-diffusion-v1-4')
        lowerCAmelCase__					     = sag_pipe.to(lowercase__)
        sag_pipe.set_progress_bar_config(disable=lowercase__)

        lowerCAmelCase__					     = '.'
        lowerCAmelCase__					     = torch.manual_seed(0)
        lowerCAmelCase__					     = sag_pipe(
            [prompt]      ,   generator=lowercase__      ,   guidance_scale=7.5      ,   sag_scale=1.0      ,   num_inference_steps=20      ,   output_type='np')

        lowerCAmelCase__					     = output.images

        lowerCAmelCase__					     = image[0, -3:, -3:, -1]

        assert image.shape == (1, 512, 512, 3)
        lowerCAmelCase__					     = np.array([0.1_568, 0.1_738, 0.1_695, 0.1_693, 0.1_507, 0.1_705, 0.1_547, 0.1_751, 0.1_949])

        assert np.abs(image_slice.flatten() - expected_slice).max() < 5e-2




 def 		__snake_case						(      self :  Tuple):







        '''simple docstring'''



        lowerCAmelCase__					     = StableDiffusionSAGPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base')
        lowerCAmelCase__					     = sag_pipe.to(lowercase__)
        sag_pipe.set_progress_bar_config(disable=lowercase__)

        lowerCAmelCase__					     = '.'
        lowerCAmelCase__					     = torch.manual_seed(0)
        lowerCAmelCase__					     = sag_pipe(
            [prompt]      ,   generator=lowercase__      ,   guidance_scale=7.5      ,   sag_scale=1.0      ,   num_inference_steps=20      ,   output_type='np')

        lowerCAmelCase__					     = output.images

        lowerCAmelCase__					     = image[0, -3:, -3:, -1]

        assert image.shape == (1, 512, 512, 3)
        lowerCAmelCase__					     = np.array([0.3_459, 0.2_876, 0.2_537, 0.3_002, 0.2_671, 0.2_160, 0.3_026, 0.2_262, 0.2_371])

        assert np.abs(image_slice.flatten() - expected_slice).max() < 5e-2




 def 		__snake_case						(      self :  Dict):







        '''simple docstring'''



        lowerCAmelCase__					     = StableDiffusionSAGPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base')
        lowerCAmelCase__					     = sag_pipe.to(lowercase__)
        sag_pipe.set_progress_bar_config(disable=lowercase__)

        lowerCAmelCase__					     = '.'
        lowerCAmelCase__					     = torch.manual_seed(0)
        lowerCAmelCase__					     = sag_pipe(
            [prompt]      ,   width=768      ,   height=512      ,   generator=lowercase__      ,   guidance_scale=7.5      ,   sag_scale=1.0      ,   num_inference_steps=20      ,   output_type='np'      ,   )

        lowerCAmelCase__					     = output.images

        assert image.shape == (1, 512, 768, 3)

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__     						=			logging.get_logger(__name__)
class     a_			(						SCREAMING_SNAKE_CASE						):






 '''simple docstring'''






 UpperCAmelCase_     			= ['input_values', 'attention_mask']




 def __init__(      self :  Union[str, Any]      ,   lowercase__ :  int = 1      ,   lowercase__ :  int = 16_000      ,   lowercase__ :  float = 0.0      ,   lowercase__ :  bool = False      ,   lowercase__ :  int = 80      ,   lowercase__ :  int = 16      ,   lowercase__ :  int = 64      ,   lowercase__ :  str = "hann_window"      ,   lowercase__ :  float = 1.0      ,   lowercase__ :  float = 80      ,   lowercase__ :  float = 7_600      ,   lowercase__ :  float = 1e-10      ,   lowercase__ :  int = 2      ,   lowercase__ :  bool = True      ,   **lowercase__ :  Optional[int]      ,   ):







        '''simple docstring'''



        super().__init__(feature_size=lowercase__      ,   sampling_rate=lowercase__      ,   padding_value=lowercase__      ,   **lowercase__)
        lowerCAmelCase__					     = do_normalize
        lowerCAmelCase__					     = return_attention_mask

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

        lowerCAmelCase__					     = win_length * sampling_rate // 1_000
        lowerCAmelCase__					     = hop_length * sampling_rate // 1_000
        lowerCAmelCase__					     = optimal_fft_length(self.sample_size)
        lowerCAmelCase__					     = (self.n_fft // 2) + 1

        lowerCAmelCase__					     = window_function(window_length=self.sample_size      ,   name=self.win_function      ,   periodic=lowercase__)

        lowerCAmelCase__					     = 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 		__snake_case						(      lowercase__ :  List[np.ndarray]      ,   lowercase__ :  List[np.ndarray]      ,   lowercase__ :  float = 0.0):







        '''simple docstring'''



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

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

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

        return normed_input_values




 def 		__snake_case						(      self :  List[Any]      ,   lowercase__ :  np.ndarray      ,   ):







        '''simple docstring'''



        lowerCAmelCase__					     = 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 :  str      ,   lowercase__ :  Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None      ,   lowercase__ :  Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None      ,   lowercase__ :  Union[bool, str, PaddingStrategy] = False      ,   lowercase__ :  Optional[int] = None      ,   lowercase__ :  bool = False      ,   lowercase__ :  Optional[int] = None      ,   lowercase__ :  Optional[bool] = None      ,   lowercase__ :  Optional[Union[str, TensorType]] = None      ,   lowercase__ :  Optional[int] = None      ,   **lowercase__ :  Union[str, Any]      ,   ):







        '''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:
               lowerCAmelCase__					     = self._process_audio(
                   lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   **lowercase__      ,   )
        else:
               lowerCAmelCase__					     = None

        if audio_target is not None:
               lowerCAmelCase__					     = self._process_audio(
                   lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   **lowercase__      ,   )

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

        return inputs




 def 		__snake_case						(      self :  Tuple      ,   lowercase__ :  Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]      ,   lowercase__ :  bool = False      ,   lowercase__ :  Union[bool, str, PaddingStrategy] = False      ,   lowercase__ :  Optional[int] = None      ,   lowercase__ :  bool = False      ,   lowercase__ :  Optional[int] = None      ,   lowercase__ :  Optional[bool] = None      ,   lowercase__ :  Optional[Union[str, TensorType]] = None      ,   **lowercase__ :  Optional[Any]      ,   ):







        '''simple docstring'''



        lowerCAmelCase__					     = 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}""")
        lowerCAmelCase__					     = is_batched_numpy or (
            isinstance(lowercase__      ,   (list, tuple)) and (isinstance(speech[0]      ,   (np.ndarray, tuple, list)))
        )

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

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

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

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

        lowerCAmelCase__					     = self.pad(
            lowercase__      ,   padding=lowercase__      ,   max_length=lowercase__      ,   truncation=lowercase__      ,   pad_to_multiple_of=lowercase__      ,   return_attention_mask=lowercase__      ,   **lowercase__      ,   )

        lowerCAmelCase__					     = feature_size_hack

        # convert input values to correct format
        lowerCAmelCase__					     = padded_inputs['input_values']
        if not isinstance(input_values[0]      ,   np.ndarray):
               lowerCAmelCase__					     = [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)
        ):
               lowerCAmelCase__					     = [array.astype(np.floataa) for array in input_values]
        elif isinstance(lowercase__      ,   np.ndarray) and input_values.dtype is np.dtype(np.floataa):
               lowerCAmelCase__					     = input_values.astype(np.floataa)

        # convert attention_mask to correct format
        lowerCAmelCase__					     = padded_inputs.get('attention_mask')
        if attention_mask is not None:
               lowerCAmelCase__					     = [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:
               lowerCAmelCase__					     = (
                   attention_mask
                   if self._get_padding_strategies(lowercase__      ,   max_length=lowercase__) is not PaddingStrategy.DO_NOT_PAD
                   else None
               )
               lowerCAmelCase__					     = self.zero_mean_unit_var_norm(
                   padded_inputs['input_values']      ,   attention_mask=lowercase__      ,   padding_value=self.padding_value)

        if return_tensors is not None:
               lowerCAmelCase__					     = padded_inputs.convert_to_tensors(lowercase__)

        return padded_inputs




 def 		__snake_case						(      self :  Any):







        '''simple docstring'''



        lowerCAmelCase__					     = super().to_dict()

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

        return output
