





import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMParallelScheduler,
    DDPMParallelScheduler,
    StableDiffusionParadigmsPipeline,
    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(    __UpperCAmelCase    ,      __UpperCAmelCase    ,      unittest.TestCase			):
     __A							=							StableDiffusionParadigmsPipeline
     __A							=							TEXT_TO_IMAGE_PARAMS
     __A							=							TEXT_TO_IMAGE_BATCH_PARAMS
     __A							=							TEXT_TO_IMAGE_IMAGE_PARAMS
     __A							=							TEXT_TO_IMAGE_IMAGE_PARAMS

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            torch.manual_seed(0   )
            _UpperCamelCase														=    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,							attention_head_dim=(2, 4),							use_linear_projection=A,							)
            _UpperCamelCase														=    DDIMParallelScheduler(
                beta_start=0.00_085,							beta_end=0.012,							beta_schedule='''scaled_linear''',							clip_sample=A,							set_alpha_to_one=A,							)
            torch.manual_seed(0   )
            _UpperCamelCase														=    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,							sample_size=128,							)
            torch.manual_seed(0   )
            _UpperCamelCase														=    CLIPTextConfig(
                bos_token_id=0,							eos_token_id=2,							hidden_size=32,							intermediate_size=37,							layer_norm_eps=1E-0_5,							num_attention_heads=4,							num_hidden_layers=5,							pad_token_id=1,							vocab_size=1000,							hidden_act='''gelu''',							projection_dim=512,							)
            _UpperCamelCase														=    CLIPTextModel(A   )
            _UpperCamelCase														=    CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''   )

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

     def 						_UpperCamelCase  (   self,							A,							A=0   ):





            """simple docstring"""

            if str(A   ).startswith('''mps'''   ):
                   _UpperCamelCase														=    torch.manual_seed(A   )
            else:
                   _UpperCamelCase														=    torch.Generator(device=A   ).manual_seed(A   )
            _UpperCamelCase														=    {
                '''prompt''': '''a photograph of an astronaut riding a horse''',
                '''generator''': generator,
                '''num_inference_steps''': 10,
                '''guidance_scale''': 6.0,
                '''output_type''': '''numpy''',
                '''parallel''': 3,
                '''debug''': True,
            }
            return inputs

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            _UpperCamelCase														=    '''cpu'''  # ensure determinism for the device-dependent torch.Generator
            _UpperCamelCase														=    self.get_dummy_components()
            _UpperCamelCase														=    StableDiffusionParadigmsPipeline(**A   )
            _UpperCamelCase														=    sd_pipe.to(A   )
            sd_pipe.set_progress_bar_config(disable=A   )

            _UpperCamelCase														=    self.get_dummy_inputs(A   )
            _UpperCamelCase														=    sd_pipe(**A   ).images
            _UpperCamelCase														=    image[0, -3:, -3:, -1]
            assert image.shape == (1, 64, 64, 3)

            _UpperCamelCase														=    np.array([0.4_773, 0.5_417, 0.4_723, 0.4_925, 0.5_631, 0.4_752, 0.5_240, 0.4_935, 0.5_023]   )

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

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            _UpperCamelCase														=    '''cpu'''  # ensure determinism for the device-dependent torch.Generator
            _UpperCamelCase														=    self.get_dummy_components()
            torch.manual_seed(0   )
            _UpperCamelCase														=    DDPMParallelScheduler()
            torch.manual_seed(0   )
            _UpperCamelCase														=    StableDiffusionParadigmsPipeline(**A   )
            _UpperCamelCase														=    sd_pipe.to(A   )
            sd_pipe.set_progress_bar_config(disable=A   )

            _UpperCamelCase														=    self.get_dummy_inputs(A   )
            _UpperCamelCase														=    sd_pipe(**A   ).images
            _UpperCamelCase														=    image[0, -3:, -3:, -1]
            assert image.shape == (1, 64, 64, 3)

            _UpperCamelCase														=    np.array([0.3_573, 0.4_420, 0.4_960, 0.4_799, 0.3_796, 0.3_879, 0.4_819, 0.4_365, 0.4_468]   )

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

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            super().test_inference_batch_consistent(batch_sizes=[1, 2]   )

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            super().test_inference_batch_single_identical(batch_size=2,							expected_max_diff=3E-3   )



     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            _UpperCamelCase														=    '''cpu'''  # ensure determinism for the device-dependent torch.Generator
            _UpperCamelCase														=    self.get_dummy_components()
            _UpperCamelCase														=    StableDiffusionParadigmsPipeline(**A   )
            _UpperCamelCase														=    sd_pipe.to(A   )
            sd_pipe.set_progress_bar_config(disable=A   )

            _UpperCamelCase														=    self.get_dummy_inputs(A   )
            _UpperCamelCase														=    '''french fries'''
            _UpperCamelCase														=    sd_pipe(**A,							negative_prompt=A   )
            _UpperCamelCase														=    output.images
            _UpperCamelCase														=    image[0, -3:, -3:, -1]

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

            _UpperCamelCase														=    np.array([0.4_771, 0.5_420, 0.4_683, 0.4_918, 0.5_636, 0.4_725, 0.5_230, 0.4_923, 0.5_015]   )

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






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

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

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

     def 						_UpperCamelCase  (   self,							A=0   ):





            """simple docstring"""

            _UpperCamelCase														=    torch.Generator(device=A   ).manual_seed(A   )
            _UpperCamelCase														=    {
                '''prompt''': '''a photograph of an astronaut riding a horse''',
                '''generator''': generator,
                '''num_inference_steps''': 10,
                '''guidance_scale''': 7.5,
                '''output_type''': '''numpy''',
                '''parallel''': 3,
                '''debug''': True,
            }
            return inputs



     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            _UpperCamelCase														=    '''stabilityai/stable-diffusion-2-base'''
            _UpperCamelCase														=    DDIMParallelScheduler.from_pretrained(A,							subfolder='''scheduler'''   )
            _UpperCamelCase														=    StableDiffusionParadigmsPipeline.from_pretrained(A,							scheduler=A,							safety_checker=A   )
            pipe.to(A   )
            pipe.set_progress_bar_config(disable=A   )
            pipe.enable_attention_slicing()

            _UpperCamelCase														=    self.get_inputs()
            _UpperCamelCase														=    pipe(**A   ).images
            _UpperCamelCase														=    image[0, -3:, -3:, -1].flatten()

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

            _UpperCamelCase														=    np.array([0.9_622, 0.9_602, 0.9_748, 0.9_591, 0.9_630, 0.9_691, 0.9_661, 0.9_631, 0.9_741]   )

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













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 SchedulerMixin


lowercase     :			Optional[int]     						=	logging.get_logger(__name__)  # pylint: disable=invalid-name

@dataclass
class 			__A(    __UpperCAmelCase			):
     __A							=							42






class 			__A(    __UpperCAmelCase    ,      __UpperCAmelCase			):
     __A							=							1

     @register_to_config
     def __init__(   self,							A = 40,							A = 0.002,							A = 80.0,							A = 0.5,							A = 1.0,							A = 7.0,							A = True,							):





            """simple docstring"""

            _UpperCamelCase														=    sigma_max

            _UpperCamelCase														=    np.linspace(0,							1,							A   )
            _UpperCamelCase														=    self._convert_to_karras(A   )
            _UpperCamelCase														=    self.sigma_to_t(A   )

            # setable values
            _UpperCamelCase														=    None
            _UpperCamelCase														=    torch.from_numpy(A   )
            _UpperCamelCase														=    torch.from_numpy(A   )
            _UpperCamelCase														=    False
            _UpperCamelCase														=    False

     def 						_UpperCamelCase  (   self,							A,							A=None   ):





            """simple docstring"""

            if schedule_timesteps is None:
                   _UpperCamelCase														=    self.timesteps

            _UpperCamelCase														=    (schedule_timesteps == timestep).nonzero()
            return indices.item()

     def 						_UpperCamelCase  (   self,							A,							A   ):





            """simple docstring"""

            if isinstance(A,							torch.Tensor   ):
                   _UpperCamelCase														=    timestep.to(self.timesteps.device   )
            _UpperCamelCase														=    self.index_for_timestep(A   )
            _UpperCamelCase														=    self.sigmas[step_idx]

            _UpperCamelCase														=    sample / ((sigma**2 + self.config.sigma_data**2) ** 0.5)

            _UpperCamelCase														=    True
            return sample

     def 						_UpperCamelCase  (   self,							A   ):





            """simple docstring"""

            if not isinstance(A,							np.ndarray   ):
                   _UpperCamelCase														=    np.array(A,							dtype=np.floataa   )

            _UpperCamelCase														=    1000 * 0.25 * np.log(sigmas + 1E-4_4   )

            return timesteps

     def 						_UpperCamelCase  (   self,							A = None,							A = None,							A = None,							):





            """simple docstring"""

            if num_inference_steps is None and timesteps is None:
                   raise ValueError('''Exactly one of `num_inference_steps` or `timesteps` must be supplied.'''   )

            if num_inference_steps is not None and timesteps is not None:
                   raise ValueError('''Can only pass one of `num_inference_steps` or `timesteps`.'''   )

            # Follow DDPMScheduler custom timesteps logic
            if timesteps is not None:
                   for i in range(1,							len(A   )   ):
                          if timesteps[i] >= timesteps[i - 1]:
                                 raise ValueError('''`timesteps` must be in descending order.'''   )

                   if timesteps[0] >= self.config.num_train_timesteps:
                          raise ValueError(
                              F'''`timesteps` must start before `self.config.train_timesteps`:'''
                              F''' {self.config.num_train_timesteps}.'''   )

                   _UpperCamelCase														=    np.array(A,							dtype=np.intaa   )
                   _UpperCamelCase														=    True
            else:
                   if num_inference_steps > self.config.num_train_timesteps:
                          raise ValueError(
                              F'''`num_inference_steps`: {num_inference_steps} cannot be larger than `self.config.train_timesteps`:'''
                              F''' {self.config.num_train_timesteps} as the unet model trained with this scheduler can only handle'''
                              F''' maximal {self.config.num_train_timesteps} timesteps.'''   )

                   _UpperCamelCase														=    num_inference_steps

                   _UpperCamelCase														=    self.config.num_train_timesteps // self.num_inference_steps
                   _UpperCamelCase														=    (np.arange(0,							A   ) * step_ratio).round()[::-1].copy().astype(np.intaa   )
                   _UpperCamelCase														=    False

            # Map timesteps to Karras sigmas directly for multistep sampling
            # See https://github.com/openai/consistency_models/blob/main/cm/karras_diffusion.py#L675
            _UpperCamelCase														=    self.config.num_train_timesteps
            _UpperCamelCase														=    timesteps[::-1].copy()
            _UpperCamelCase														=    ramp / (num_train_timesteps - 1)
            _UpperCamelCase														=    self._convert_to_karras(A   )
            _UpperCamelCase														=    self.sigma_to_t(A   )

            _UpperCamelCase														=    np.concatenate([sigmas, [self.sigma_min]]   ).astype(np.floataa   )
            _UpperCamelCase														=    torch.from_numpy(A   ).to(device=A   )

            if str(A   ).startswith('''mps'''   ):
                   # mps does not support float64
                   _UpperCamelCase														=    torch.from_numpy(A   ).to(A,							dtype=torch.floataa   )
            else:
                   _UpperCamelCase														=    torch.from_numpy(A   ).to(device=A   )

     def 						_UpperCamelCase  (   self,							A   ):





            """simple docstring"""

            _UpperCamelCase														=    self.config.sigma_min
            _UpperCamelCase														=    self.config.sigma_max

            _UpperCamelCase														=    self.config.rho
            _UpperCamelCase														=    sigma_min ** (1 / rho)
            _UpperCamelCase														=    sigma_max ** (1 / rho)
            _UpperCamelCase														=    (max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho
            return sigmas

     def 						_UpperCamelCase  (   self,							A   ):





            """simple docstring"""

            _UpperCamelCase														=    self.config.sigma_data

            _UpperCamelCase														=    sigma_data**2 / (sigma**2 + sigma_data**2)
            _UpperCamelCase														=    sigma * sigma_data / (sigma**2 + sigma_data**2) ** 0.5
            return c_skip, c_out

     def 						_UpperCamelCase  (   self,							A   ):





            """simple docstring"""

            _UpperCamelCase														=    self.config.sigma_min
            _UpperCamelCase														=    self.config.sigma_data

            _UpperCamelCase														=    sigma_data**2 / ((sigma - sigma_min) ** 2 + sigma_data**2)
            _UpperCamelCase														=    (sigma - sigma_min) * sigma_data / (sigma**2 + sigma_data**2) ** 0.5
            return c_skip, c_out

     def 						_UpperCamelCase  (   self,							A,							A,							A,							A = None,							A = True,							):





            """simple docstring"""

            if (
                isinstance(A,							A   )
                or isinstance(A,							torch.IntTensor   )
                or isinstance(A,							torch.LongTensor   )
            ):
                   raise ValueError(
                       (
                           '''Passing integer indices (e.g. from `enumerate(timesteps)`) as timesteps to'''
                           F''' `{self.__class__}.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(A,							torch.Tensor   ):
                   _UpperCamelCase														=    timestep.to(self.timesteps.device   )

            _UpperCamelCase														=    self.config.sigma_min
            _UpperCamelCase														=    self.config.sigma_max

            _UpperCamelCase														=    self.index_for_timestep(A   )

            # sigma_next corresponds to next_t in original implementation
            _UpperCamelCase														=    self.sigmas[step_index]
            if step_index + 1 < self.config.num_train_timesteps:
                   _UpperCamelCase														=    self.sigmas[step_index + 1]
            else:
                   # Set sigma_next to sigma_min
                   _UpperCamelCase														=    self.sigmas[-1]

            # Get scalings for boundary conditions
            _UpperCamelCase							,		_UpperCamelCase														=    self.get_scalings_for_boundary_condition(A   )

            # 1. Denoise model output using boundary conditions
            _UpperCamelCase														=    c_out * model_output + c_skip * sample
            if self.config.clip_denoised:
                   _UpperCamelCase														=    denoised.clamp(-1,							1   )

            # 2. Sample z ~ N(0, s_noise^2 * I)
            # Noise is not used for onestep sampling.
            if len(self.timesteps   ) > 1:
                   _UpperCamelCase														=    randn_tensor(
                       model_output.shape,							dtype=model_output.dtype,							device=model_output.device,							generator=A   )
            else:
                   _UpperCamelCase														=    torch.zeros_like(A   )
            _UpperCamelCase														=    noise * self.config.s_noise

            _UpperCamelCase														=    sigma_next.clamp(min=A,							max=A   )

            # 3. Return noisy sample
            # tau = sigma_hat, eps = sigma_min
            _UpperCamelCase														=    denoised + z * (sigma_hat**2 - sigma_min**2) ** 0.5

            if not return_dict:
                   return (prev_sample,)

            return CMStochasticIterativeSchedulerOutput(prev_sample=A   )

     def 						_UpperCamelCase  (   self,							A,							A,							A,							):





            """simple docstring"""

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

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

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

            _UpperCamelCase														=    original_samples + noise * sigma
            return noisy_samples



     def __len__(   self   ):





            """simple docstring"""

            return self.config.num_train_timesteps






