import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    EulerAncestralDiscreteScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionPanoramaPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps

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()





@skip_mps
class   __a      (   __UpperCamelCase	,	__UpperCamelCase	,	unittest.TestCase				):
 __lowercase			:							Tuple       					=			StableDiffusionPanoramaPipeline
 __lowercase			:							Any       					=			TEXT_TO_IMAGE_PARAMS
 __lowercase			:							List[Any]       					=			TEXT_TO_IMAGE_BATCH_PARAMS
 __lowercase			:							Dict       					=			TEXT_TO_IMAGE_IMAGE_PARAMS
 __lowercase			:							int       					=			TEXT_TO_IMAGE_IMAGE_PARAMS
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Dict:




        '''simple docstring'''
        torch.manual_seed(0			)
        lowercase__:       Optional[Any]        =       UNetaDConditionModel(
            block_out_channels=(32, 64)    ,			layers_per_block=1    ,			sample_size=32    ,			in_channels=4    ,			out_channels=4    ,			down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')    ,			up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')    ,			cross_attention_dim=32    ,			)
        lowercase__:       Union[str, Any]        =       DDIMScheduler()
        torch.manual_seed(0			)
        lowercase__:       Optional[int]        =       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			)
        lowercase__:       List[Any]        =       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    ,			)
        lowercase__:       List[Any]        =       CLIPTextModel(lowerCAmelCase__			)
        lowercase__:       Optional[int]        =       CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'			)

        lowercase__:       Tuple        =       {
            'unet': unet,
            'scheduler': scheduler,
            'vae': vae,
            'text_encoder': text_encoder,
            'tokenizer': tokenizer,
            'safety_checker': None,
            'feature_extractor': None,
        }
        return components
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__=0			) ->    Optional[int]:




        '''simple docstring'''
        lowercase__:       List[Any]        =       torch.manual_seed(lowerCAmelCase__			)
        lowercase__:       Any        =       {
            'prompt': 'a photo of the dolomites',
            'generator': generator,
            # Setting height and width to None to prevent OOMs on CPU.
            'height': None,
            'width': None,
            'num_inference_steps': 1,
            'guidance_scale': 6.0,
            'output_type': 'numpy',
        }
        return inputs
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    List[str]:




        '''simple docstring'''
        lowercase__:       Tuple        =       'cpu'  # ensure determinism for the device-dependent torch.Generator
        lowercase__:       Optional[Any]        =       self.get_dummy_components()
        lowercase__:       Tuple        =       StableDiffusionPanoramaPipeline(**lowerCAmelCase__			)
        lowercase__:       Tuple        =       sd_pipe.to(lowerCAmelCase__			)
        sd_pipe.set_progress_bar_config(disable=lowerCAmelCase__			)

        lowercase__:       Any        =       self.get_dummy_inputs(lowerCAmelCase__			)
        lowercase__:       Tuple        =       sd_pipe(**lowerCAmelCase__			).images
        lowercase__:       List[Any]        =       image[0, -3:, -3:, -1]
        assert image.shape == (1, 64, 64, 3)

        lowercase__:       List[str]        =       np.array([0.6_1_8_6, 0.5_3_7_4, 0.4_9_1_5, 0.4_1_3_5, 0.4_1_1_4, 0.4_5_6_3, 0.5_1_2_8, 0.4_9_7_7, 0.4_7_5_7]			)

        assert np.abs(image_slice.flatten() - expected_slice			).max() < 1E-2
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Any:




        '''simple docstring'''
        super().test_inference_batch_consistent(batch_sizes=[1, 2]			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    int:




        '''simple docstring'''
        super().test_inference_batch_single_identical(batch_size=2    ,			expected_max_diff=3.25E-3			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Union[str, Any]:




        '''simple docstring'''
        lowercase__:       Union[str, Any]        =       'cpu'  # ensure determinism for the device-dependent torch.Generator
        lowercase__:       List[Any]        =       self.get_dummy_components()
        lowercase__:       int        =       StableDiffusionPanoramaPipeline(**lowerCAmelCase__			)
        lowercase__:       Optional[int]        =       sd_pipe.to(lowerCAmelCase__			)
        sd_pipe.set_progress_bar_config(disable=lowerCAmelCase__			)

        lowercase__:       Any        =       self.get_dummy_inputs(lowerCAmelCase__			)
        lowercase__:       Union[str, Any]        =       'french fries'
        lowercase__:       Tuple        =       sd_pipe(**lowerCAmelCase__    ,			negative_prompt=lowerCAmelCase__			)
        lowercase__:       Any        =       output.images
        lowercase__:       Any        =       image[0, -3:, -3:, -1]

        assert image.shape == (1, 64, 64, 3)

        lowercase__:       Dict        =       np.array([0.6_1_8_7, 0.5_3_7_5, 0.4_9_1_5, 0.4_1_3_6, 0.4_1_1_4, 0.4_5_6_3, 0.5_1_2_8, 0.4_9_7_6, 0.4_7_5_7]			)

        assert np.abs(image_slice.flatten() - expected_slice			).max() < 1E-2
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    List[Any]:




        '''simple docstring'''
        lowercase__:       Tuple        =       'cpu'  # ensure determinism for the device-dependent torch.Generator
        lowercase__:       Any        =       self.get_dummy_components()
        lowercase__:       int        =       StableDiffusionPanoramaPipeline(**lowerCAmelCase__			)
        lowercase__:       Tuple        =       sd_pipe.to(lowerCAmelCase__			)
        sd_pipe.set_progress_bar_config(disable=lowerCAmelCase__			)

        lowercase__:       List[str]        =       self.get_dummy_inputs(lowerCAmelCase__			)
        lowercase__:       Optional[Any]        =       sd_pipe(**lowerCAmelCase__    ,			view_batch_size=2			)
        lowercase__:       Optional[int]        =       output.images
        lowercase__:       int        =       image[0, -3:, -3:, -1]

        assert image.shape == (1, 64, 64, 3)

        lowercase__:       List[Any]        =       np.array([0.6_1_8_7, 0.5_3_7_5, 0.4_9_1_5, 0.4_1_3_6, 0.4_1_1_4, 0.4_5_6_3, 0.5_1_2_8, 0.4_9_7_6, 0.4_7_5_7]			)

        assert np.abs(image_slice.flatten() - expected_slice			).max() < 1E-2
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Any:




        '''simple docstring'''
        lowercase__:       List[Any]        =       'cpu'  # ensure determinism for the device-dependent torch.Generator
        lowercase__:       Union[str, Any]        =       self.get_dummy_components()
        lowercase__:       Tuple        =       EulerAncestralDiscreteScheduler(
            beta_start=0.0_0_0_8_5    ,			beta_end=0.0_1_2    ,			beta_schedule='scaled_linear'			)
        lowercase__:       List[str]        =       StableDiffusionPanoramaPipeline(**lowerCAmelCase__			)
        lowercase__:       int        =       sd_pipe.to(lowerCAmelCase__			)
        sd_pipe.set_progress_bar_config(disable=lowerCAmelCase__			)

        lowercase__:       List[str]        =       self.get_dummy_inputs(lowerCAmelCase__			)
        lowercase__:       Dict        =       sd_pipe(**lowerCAmelCase__			).images
        lowercase__:       Tuple        =       image[0, -3:, -3:, -1]

        assert image.shape == (1, 64, 64, 3)

        lowercase__:       List[str]        =       np.array([0.4_0_2_4, 0.6_5_1_0, 0.4_9_0_1, 0.5_3_7_8, 0.5_8_1_3, 0.5_6_2_2, 0.4_7_9_5, 0.4_4_6_7, 0.4_9_5_2]			)

        assert np.abs(image_slice.flatten() - expected_slice			).max() < 1E-2
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    str:




        '''simple docstring'''
        lowercase__:       Optional[int]        =       'cpu'  # ensure determinism for the device-dependent torch.Generator
        lowercase__:       List[str]        =       self.get_dummy_components()
        lowercase__:       List[str]        =       PNDMScheduler(
            beta_start=0.0_0_0_8_5    ,			beta_end=0.0_1_2    ,			beta_schedule='scaled_linear'    ,			skip_prk_steps=lowerCAmelCase__			)
        lowercase__:       Optional[int]        =       StableDiffusionPanoramaPipeline(**lowerCAmelCase__			)
        lowercase__:       Optional[Any]        =       sd_pipe.to(lowerCAmelCase__			)
        sd_pipe.set_progress_bar_config(disable=lowerCAmelCase__			)

        lowercase__:       Optional[Any]        =       self.get_dummy_inputs(lowerCAmelCase__			)
        lowercase__:       Tuple        =       sd_pipe(**lowerCAmelCase__			).images
        lowercase__:       Optional[int]        =       image[0, -3:, -3:, -1]

        assert image.shape == (1, 64, 64, 3)

        lowercase__:       Optional[Any]        =       np.array([0.6_3_9_1, 0.6_2_9_1, 0.4_8_6_1, 0.5_1_3_4, 0.5_5_5_2, 0.4_5_7_8, 0.5_0_3_2, 0.5_0_2_3, 0.4_5_3_9]			)

        assert np.abs(image_slice.flatten() - expected_slice			).max() < 1E-2





@slow
@require_torch_gpu
class   __a      (   unittest.TestCase				):
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    str:




        '''simple docstring'''
        super().tearDown()
        gc.collect()
        torch.cuda.empty_cache()
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__=0			) ->    Dict:




        '''simple docstring'''
        lowercase__:       Optional[int]        =       torch.manual_seed(lowerCAmelCase__			)
        lowercase__:       Dict        =       {
            'prompt': 'a photo of the dolomites',
            'generator': generator,
            'num_inference_steps': 3,
            'guidance_scale': 7.5,
            'output_type': 'numpy',
        }
        return inputs
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    str:




        '''simple docstring'''
        lowercase__:       List[Any]        =       'stabilityai/stable-diffusion-2-base'
        lowercase__:       Any        =       DDIMScheduler.from_pretrained(lowerCAmelCase__    ,			subfolder='scheduler'			)
        lowercase__:       Optional[int]        =       StableDiffusionPanoramaPipeline.from_pretrained(lowerCAmelCase__    ,			scheduler=lowerCAmelCase__    ,			safety_checker=lowerCAmelCase__			)
        pipe.to(lowerCAmelCase__			)
        pipe.set_progress_bar_config(disable=lowerCAmelCase__			)
        pipe.enable_attention_slicing()

        lowercase__:       Optional[int]        =       self.get_inputs()
        lowercase__:       Union[str, Any]        =       pipe(**lowerCAmelCase__			).images
        lowercase__:       Tuple        =       image[0, -3:, -3:, -1].flatten()

        assert image.shape == (1, 512, 2_048, 3)

        lowercase__:       int        =       np.array(
            [
                0.3_6_9_6_8_3_9_2,
                0.2_7_0_2_5_3_7_2,
                0.3_2_4_4_6_7_6_6,
                0.2_8_3_7_9_3_8_7,
                0.3_6_3_6_3_2_7_4,
                0.3_0_7_3_3_3_4_7,
                0.2_7_1_0_0_0_2_7,
                0.2_7_0_5_4_1_2_5,
                0.2_5_5_3_6_0_9_6,
            ]			)

        assert np.abs(expected_slice - image_slice			).max() < 1E-2
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Optional[Any]:




        '''simple docstring'''
        lowercase__:       List[str]        =       StableDiffusionPanoramaPipeline.from_pretrained(
            'stabilityai/stable-diffusion-2-base'    ,			safety_checker=lowerCAmelCase__			)
        lowercase__:       Optional[int]        =       LMSDiscreteScheduler.from_config(pipe.scheduler.config			)
        pipe.to(lowerCAmelCase__			)
        pipe.set_progress_bar_config(disable=lowerCAmelCase__			)
        pipe.enable_attention_slicing()

        lowercase__:       Dict        =       self.get_inputs()
        lowercase__:       Tuple        =       pipe(**lowerCAmelCase__			).images
        lowercase__:       List[str]        =       image[0, -3:, -3:, -1].flatten()

        assert image.shape == (1, 512, 2_048, 3)

        lowercase__:       Any        =       np.array(
            [
                [
                    0.0,
                    0.0,
                    0.0,
                    0.0,
                    0.0,
                    0.0,
                    0.0,
                    0.0,
                    0.0,
                ]
            ]			)

        assert np.abs(expected_slice - image_slice			).max() < 1E-3
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Any:




        '''simple docstring'''
        lowercase__:       Optional[Any]        =       0

        def callback_fn(lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__			) -> None:
               lowercase__:       List[Any]        =       True
               nonlocal number_of_steps
               number_of_steps += 1
               if step == 1:
                      lowercase__:       Any        =       latents.detach().cpu().numpy()
                      assert latents.shape == (1, 4, 64, 256)
                      lowercase__:       int        =       latents[0, -3:, -3:, -1]

                      lowercase__:       Tuple        =       np.array(
                          [
                              0.1_8_6_8_1_8_6_9,
                              0.3_3_9_0_7_8_1_6,
                              0.5_3_6_1_2_7_6,
                              0.1_4_4_3_2_8_6_5,
                              -0.0_2_8_5_6_6_1_1,
                              -0.7_3_9_4_1_1_2_3,
                              0.2_3_3_9_7_9_8_7,
                              0.4_7_3_2_2_6_8_2,
                              -0.3_7_8_2_3_1_6_4,
                          ]			)
                      assert np.abs(latents_slice.flatten() - expected_slice			).max() < 5E-2
               elif step == 2:
                      lowercase__:       Tuple        =       latents.detach().cpu().numpy()
                      assert latents.shape == (1, 4, 64, 256)
                      lowercase__:       str        =       latents[0, -3:, -3:, -1]

                      lowercase__:       int        =       np.array(
                          [
                              0.1_8_5_3_9_6_4_5,
                              0.3_3_9_8_7_2_4_8,
                              0.5_3_7_8_5_5_9,
                              0.1_4_4_3_7_1_4_2,
                              -0.0_2_4_5_5_2_6_1,
                              -0.7_3_3_8_3_1_7,
                              0.2_3_9_9_0_7_5_5,
                              0.4_7_3_5_6_2_7_2,
                              -0.3_7_8_6_5_0_5,
                          ]			)

                      assert np.abs(latents_slice.flatten() - expected_slice			).max() < 5E-2

        lowercase__:       int        =       False

        lowercase__:       Union[str, Any]        =       'stabilityai/stable-diffusion-2-base'
        lowercase__:       Optional[Any]        =       DDIMScheduler.from_pretrained(lowerCAmelCase__    ,			subfolder='scheduler'			)
        lowercase__:       Optional[Any]        =       StableDiffusionPanoramaPipeline.from_pretrained(lowerCAmelCase__    ,			scheduler=lowerCAmelCase__    ,			safety_checker=lowerCAmelCase__			)
        lowercase__:       int        =       pipe.to(lowerCAmelCase__			)
        pipe.set_progress_bar_config(disable=lowerCAmelCase__			)
        pipe.enable_attention_slicing()

        lowercase__:       Any        =       self.get_inputs()
        pipe(**lowerCAmelCase__    ,			callback=lowerCAmelCase__    ,			callback_steps=1			)
        assert callback_fn.has_been_called
        assert number_of_steps == 3
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Optional[int]:




        '''simple docstring'''
        torch.cuda.empty_cache()
        torch.cuda.reset_max_memory_allocated()
        torch.cuda.reset_peak_memory_stats()

        lowercase__:       Any        =       'stabilityai/stable-diffusion-2-base'
        lowercase__:       List[str]        =       DDIMScheduler.from_pretrained(lowerCAmelCase__    ,			subfolder='scheduler'			)
        lowercase__:       Any        =       StableDiffusionPanoramaPipeline.from_pretrained(lowerCAmelCase__    ,			scheduler=lowerCAmelCase__    ,			safety_checker=lowerCAmelCase__			)
        lowercase__:       List[str]        =       pipe.to(lowerCAmelCase__			)
        pipe.set_progress_bar_config(disable=lowerCAmelCase__			)
        pipe.enable_attention_slicing(1			)
        pipe.enable_sequential_cpu_offload()

        lowercase__:       List[str]        =       self.get_inputs()
        lowercase__:       Dict        =       pipe(**lowerCAmelCase__			)

        lowercase__:       Optional[int]        =       torch.cuda.max_memory_allocated()
        # make sure that less than 5.2 GB is allocated
        assert mem_bytes < 5.5 * 10**9






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      (   __UpperCamelCase				):
 __lowercase			:							Tuple       					=			['input_values', 'attention_mask']
 def __init__(       self    ,			lowerCAmelCase__ = 1    ,			lowerCAmelCase__ = 16_000    ,			lowerCAmelCase__ = 0.0    ,			lowerCAmelCase__ = False    ,			lowerCAmelCase__ = 80    ,			lowerCAmelCase__ = 16    ,			lowerCAmelCase__ = 64    ,			lowerCAmelCase__ = "hann_window"    ,			lowerCAmelCase__ = 1.0    ,			lowerCAmelCase__ = 80    ,			lowerCAmelCase__ = 7_600    ,			lowerCAmelCase__ = 1E-10    ,			lowerCAmelCase__ = 2    ,			lowerCAmelCase__ = True    ,			**lowerCAmelCase__    ,			) ->    List[Any]:




        '''simple docstring'''
        super().__init__(feature_size=lowerCAmelCase__    ,			sampling_rate=lowerCAmelCase__    ,			padding_value=lowerCAmelCase__    ,			**lowerCAmelCase__			)
        lowercase__:       Dict        =       do_normalize
        lowercase__:       Optional[Any]        =       return_attention_mask

        lowercase__:       str        =       num_mel_bins
        lowercase__:       Dict        =       hop_length
        lowercase__:       Dict        =       win_length
        lowercase__:       Optional[int]        =       win_function
        lowercase__:       Any        =       frame_signal_scale
        lowercase__:       Tuple        =       fmin
        lowercase__:       Tuple        =       fmax
        lowercase__:       Dict        =       mel_floor
        lowercase__:       int        =       reduction_factor

        lowercase__:       List[Any]        =       win_length * sampling_rate // 1_000
        lowercase__:       Optional[Any]        =       hop_length * sampling_rate // 1_000
        lowercase__:       Optional[int]        =       optimal_fft_length(self.sample_size			)
        lowercase__:       Optional[Any]        =       (self.n_fft // 2) + 1

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

        lowercase__:       Optional[Any]        =       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'    ,			lowerCAmelCase__    ,			)
        if reduction_factor != 2.0:
               warnings.warn(
                   'The argument `reduction_factor` is deprecated and will be removed in version 4.30.0 of Transformers'    ,			lowerCAmelCase__    ,			)
 @staticmethod
 # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm
 def 		SCREAMING_SNAKE_CASE__       (       lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__ = 0.0			) ->    List[np.ndarray]:




        '''simple docstring'''
        if attention_mask is not None:
               lowercase__:       List[str]        =       np.array(lowerCAmelCase__    ,			np.intaa			)
               lowercase__:       Tuple        =       []

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

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

        return normed_input_values
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			) ->    np.ndarray:




        '''simple docstring'''
        lowercase__:       List[str]        =       spectrogram(
            lowerCAmelCase__    ,			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    ,			lowerCAmelCase__ = None    ,			lowerCAmelCase__ = None    ,			lowerCAmelCase__ = False    ,			lowerCAmelCase__ = None    ,			lowerCAmelCase__ = False    ,			lowerCAmelCase__ = None    ,			lowerCAmelCase__ = None    ,			lowerCAmelCase__ = None    ,			lowerCAmelCase__ = None    ,			**lowerCAmelCase__    ,			) ->    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:
               lowercase__:       Dict        =       self._process_audio(
                   lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			**lowerCAmelCase__    ,			)
        else:
               lowercase__:       str        =       None

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

               if inputs is None:
                      return inputs_target
               else:
                      lowercase__:       int        =       inputs_target['input_values']
                      lowercase__:       List[str]        =       inputs_target.get('attention_mask'			)
                      if decoder_attention_mask is not None:
                             lowercase__:       Optional[int]        =       decoder_attention_mask

        return inputs
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__ = False    ,			lowerCAmelCase__ = False    ,			lowerCAmelCase__ = None    ,			lowerCAmelCase__ = False    ,			lowerCAmelCase__ = None    ,			lowerCAmelCase__ = None    ,			lowerCAmelCase__ = None    ,			**lowerCAmelCase__    ,			) ->    BatchFeature:




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

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

        # always return batch
        if not is_batched:
               lowercase__:       Optional[int]        =       [speech]

        # needed to make pad() work on spectrogram inputs
        lowercase__:       str        =       self.feature_size

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

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

        lowercase__:       List[str]        =       feature_size_hack

        # convert input values to correct format
        lowercase__:       Union[str, Any]        =       padded_inputs['input_values']
        if not isinstance(input_values[0]    ,			np.ndarray			):
               lowercase__:       List[Any]        =       [np.asarray(lowerCAmelCase__    ,			dtype=np.floataa			) for array in input_values]
        elif (
            not isinstance(lowerCAmelCase__    ,			np.ndarray			)
            and isinstance(input_values[0]    ,			np.ndarray			)
            and input_values[0].dtype is np.dtype(np.floataa			)
        ):
               lowercase__:       Dict        =       [array.astype(np.floataa			) for array in input_values]
        elif isinstance(lowerCAmelCase__    ,			np.ndarray			) and input_values.dtype is np.dtype(np.floataa			):
               lowercase__:       Tuple        =       input_values.astype(np.floataa			)

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

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

        if return_tensors is not None:
               lowercase__:       Union[str, Any]        =       padded_inputs.convert_to_tensors(lowerCAmelCase__			)

        return padded_inputs
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Dict[str, Any]:




        '''simple docstring'''
        lowercase__:       int        =       super().to_dict()

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

        return output


