

"""simple docstring"""

import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    EulerAncestralDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionModelEditingPipeline,
    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 PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()






@skip_mps
class  __a				(	__magic_name__					,  __magic_name__					,  __magic_name__					,  unittest.TestCase					):





       """simple docstring"""


       __UpperCamelCase				:			Any          =	StableDiffusionModelEditingPipeline
       __UpperCamelCase				:			Dict          =	TEXT_TO_IMAGE_PARAMS
       __UpperCamelCase				:			List[Any]          =	TEXT_TO_IMAGE_BATCH_PARAMS
       __UpperCamelCase				:			Dict          =	TEXT_TO_IMAGE_IMAGE_PARAMS
       __UpperCamelCase				:			Tuple          =	TEXT_TO_IMAGE_IMAGE_PARAMS






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        torch.manual_seed(0      )
        lowerCAmelCase__      :							Optional[Any]    =							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__      :							Union[str, Any]    =							DDIMScheduler()
        torch.manual_seed(0      )
        lowerCAmelCase__      :							List[Any]    =							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__      :							List[str]    =							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__      :							Tuple    =							CLIPTextModel(snake_case      )
        lowerCAmelCase__      :							List[str]    =							CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"      )

        lowerCAmelCase__      :							str    =							{
            "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 ,				snake_case ,				snake_case=0      ):





        """simple docstring"""

        lowerCAmelCase__      :							Tuple    =							torch.manual_seed(snake_case      )
        lowerCAmelCase__      :							Dict    =							{
            "prompt": "A field of roses",
            "generator": generator,
            # Setting height and width to None to prevent OOMs on CPU.
            "height": None,
            "width": None,
            "num_inference_steps": 2,
            "guidance_scale": 6.0,
            "output_type": "numpy",
        }
        return inputs






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Union[str, Any]    =							"cpu"  # ensure determinism for the device-dependent torch.Generator
        lowerCAmelCase__      :							Union[str, Any]    =							self.get_dummy_components()
        lowerCAmelCase__      :							Optional[int]    =							StableDiffusionModelEditingPipeline(**snake_case      )
        lowerCAmelCase__      :							Union[str, Any]    =							sd_pipe.to(snake_case      )
        sd_pipe.set_progress_bar_config(disable=snake_case      )

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

        lowerCAmelCase__      :							str    =							np.array([0.4_755, 0.5_132, 0.4_976, 0.3_904, 0.3_554, 0.4_765, 0.5_139, 0.5_158, 0.4_889]      )

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






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Optional[Any]    =							"cpu"  # ensure determinism for the device-dependent torch.Generator
        lowerCAmelCase__      :							List[Any]    =							self.get_dummy_components()
        lowerCAmelCase__      :							Optional[Any]    =							StableDiffusionModelEditingPipeline(**snake_case      )
        lowerCAmelCase__      :							Optional[Any]    =							sd_pipe.to(snake_case      )
        sd_pipe.set_progress_bar_config(disable=snake_case      )

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

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

        lowerCAmelCase__      :							str    =							np.array([0.4_992, 0.5_101, 0.5_004, 0.3_949, 0.3_604, 0.4_735, 0.5_216, 0.5_204, 0.4_913]      )

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






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							List[str]    =							"cpu"  # ensure determinism for the device-dependent torch.Generator
        lowerCAmelCase__      :							Tuple    =							self.get_dummy_components()
        lowerCAmelCase__      :							int    =							EulerAncestralDiscreteScheduler(
            beta_start=0.00_085 ,				beta_end=0.012 ,				beta_schedule="scaled_linear"      )
        lowerCAmelCase__      :							int    =							StableDiffusionModelEditingPipeline(**snake_case      )
        lowerCAmelCase__      :							Any    =							sd_pipe.to(snake_case      )
        sd_pipe.set_progress_bar_config(disable=snake_case      )

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

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

        lowerCAmelCase__      :							Tuple    =							np.array([0.4_747, 0.5_372, 0.4_779, 0.4_982, 0.5_543, 0.4_816, 0.5_238, 0.4_904, 0.5_027]      )

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






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							List[str]    =							"cpu"  # ensure determinism for the device-dependent torch.Generator
        lowerCAmelCase__      :							List[Any]    =							self.get_dummy_components()
        lowerCAmelCase__      :							Any    =							PNDMScheduler()
        lowerCAmelCase__      :							List[Any]    =							StableDiffusionModelEditingPipeline(**snake_case      )
        lowerCAmelCase__      :							str    =							sd_pipe.to(snake_case      )
        sd_pipe.set_progress_bar_config(disable=snake_case      )

        lowerCAmelCase__      :							Optional[Any]    =							self.get_dummy_inputs(snake_case      )
        # the pipeline does not expect pndm so test if it raises error.
        with self.assertRaises(snake_case      ):
         lowerCAmelCase__      :							Optional[int]    =							sd_pipe(**snake_case      ).images






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

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






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        super().test_attention_slicing_forward_pass(expected_max_diff=5e-3      )






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





       """simple docstring"""


       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

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






       def  SCREAMING_SNAKE_CASE_  (      self ,				snake_case=0      ):





        """simple docstring"""

        lowerCAmelCase__      :							Optional[Any]    =							torch.manual_seed(snake_case      )
        lowerCAmelCase__      :							int    =							{
            "prompt": "A field of roses",
            "generator": generator,
            "num_inference_steps": 3,
            "guidance_scale": 7.5,
            "output_type": "numpy",
        }
        return inputs






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Any    =							"CompVis/stable-diffusion-v1-4"
        lowerCAmelCase__      :							Any    =							StableDiffusionModelEditingPipeline.from_pretrained(snake_case ,				safety_checker=snake_case      )
        pipe.to(snake_case      )
        pipe.set_progress_bar_config(disable=snake_case      )
        pipe.enable_attention_slicing()

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

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

        lowerCAmelCase__      :							int    =							np.array(
            [0.6_749_496, 0.6_386_453, 0.51_443_267, 0.66_094_905, 0.61_921_215, 0.5_491_332, 0.5_744_417, 0.58_075_106, 0.5_174_658]      )

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

        # make sure image changes after editing
        pipe.edit_model("A pack of roses" ,				"A pack of blue roses"      )

        lowerCAmelCase__      :							Union[str, Any]    =							pipe(**snake_case      ).images
        lowerCAmelCase__      :							Any    =							image[0, -3:, -3:, -1].flatten()

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

        assert np.abs(expected_slice - image_slice      ).max() > 1e-1






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        torch.cuda.empty_cache()
        torch.cuda.reset_max_memory_allocated()
        torch.cuda.reset_peak_memory_stats()

        lowerCAmelCase__      :							str    =							"CompVis/stable-diffusion-v1-4"
        lowerCAmelCase__      :							Union[str, Any]    =							DDIMScheduler.from_pretrained(snake_case ,				subfolder="scheduler"      )
        lowerCAmelCase__      :							Union[str, Any]    =							StableDiffusionModelEditingPipeline.from_pretrained(
            snake_case ,				scheduler=snake_case ,				safety_checker=snake_case      )
        lowerCAmelCase__      :							Union[str, Any]    =							pipe.to(snake_case      )
        pipe.set_progress_bar_config(disable=snake_case      )
        pipe.enable_attention_slicing(1      )
        pipe.enable_sequential_cpu_offload()

        lowerCAmelCase__      :							Any    =							self.get_inputs()
        lowerCAmelCase__      :							Union[str, Any]    =							pipe(**snake_case      )

        lowerCAmelCase__      :							Optional[Any]    =							torch.cuda.max_memory_allocated()
        # make sure that less than 4.4 GB is allocated
        assert mem_bytes < 4.4 * 10**9









"""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


_UpperCamelCase		    =						logging.get_logger(__name__)






class  __a				(	__magic_name__					):





       """simple docstring"""


       __UpperCamelCase				:			Tuple          =	['input_values', 'attention_mask']






       def __init__(      self ,				snake_case=1 ,				snake_case=16_000 ,				snake_case=0.0 ,				snake_case=False ,				snake_case=True ,				**snake_case ,				):





        """simple docstring"""

        super().__init__(feature_size=snake_case ,				sampling_rate=snake_case ,				padding_value=snake_case ,				**snake_case      )
        lowerCAmelCase__      :							Dict    =							return_attention_mask
        lowerCAmelCase__      :							List[str]    =							do_normalize






       @staticmethod
       def  SCREAMING_SNAKE_CASE_  (      snake_case ,				snake_case ,				snake_case = 0.0      ):





        """simple docstring"""

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

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

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

        return normed_input_values






       def __call__(      self ,				snake_case ,				snake_case = False ,				snake_case = None ,				snake_case = False ,				snake_case = None ,				snake_case = None ,				snake_case = None ,				snake_case = None ,				**snake_case ,				):





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

        lowerCAmelCase__      :							List[str]    =							isinstance(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}"""      )
        lowerCAmelCase__      :							Tuple    =							is_batched_numpy or (
            isinstance(snake_case ,				(list, tuple)      ) and (isinstance(raw_speech[0] ,				(np.ndarray, tuple, list)      ))
        )

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

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

        lowerCAmelCase__      :							Any    =							self.pad(
            snake_case ,				padding=snake_case ,				max_length=snake_case ,				truncation=snake_case ,				pad_to_multiple_of=snake_case ,				return_attention_mask=snake_case ,				)

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

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

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

        if return_tensors is not None:
         lowerCAmelCase__      :							Optional[Any]    =							padded_inputs.convert_to_tensors(snake_case      )

        return padded_inputs






