code: 
  
  
  
  
  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
  
  
  _SCREAMING_SNAKE_CASE    :				Tuple       =					logging.get_logger(__name__)  # pylint: disable=invalid-name
  
  @dataclass
  # Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->EulerAncestralDiscrete
  class 						A__      (					lowerCAmelCase__			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      __magic_name__    						=	42
      __magic_name__    						=	None
  
  
  
  
  
  
  def     UpperCAmelCase__		(UpperCamelCase_							,UpperCamelCase_=0.999							,UpperCamelCase_="cosine"							,):
  
  
  
       """simple docstring"""
  
  
  
  
  
  
       if alpha_transform_type == "cosine":
  
            def alpha_bar_fn(UpperCamelCase_       ):
                 return math.cos((t + 0.008) / 1.008 * math.pi / 2       ) ** 2
  
       elif alpha_transform_type == "exp":
  
            def alpha_bar_fn(UpperCamelCase_       ):
                 return math.exp(t * -12.0       )
  
       else:
            raise ValueError(F'''Unsupported alpha_tranform_type: {alpha_transform_type}'''       )
  
       snake_case					=       []
       for i in range(UpperCamelCase_       ):
            snake_case					=       i / num_diffusion_timesteps
            snake_case					=       (i + 1) / num_diffusion_timesteps
            betas.append(min(1 - alpha_bar_fn(UpperCamelCase_       ) / alpha_bar_fn(UpperCamelCase_       )							,UpperCamelCase_       )       )
       return torch.tensor(UpperCamelCase_							,dtype=torch.floataa       )
  
  class 						A__      (					lowerCAmelCase__ ,      lowerCAmelCase__			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      __magic_name__    						=	[e.name for e in KarrasDiffusionSchedulers]
      __magic_name__    						=	1
      @register_to_config
      def __init__( self    ,      __snake_case = 1_0_0_0    ,      __snake_case = 0.0001    ,      __snake_case = 0.02    ,      __snake_case = "linear"    ,      __snake_case = None    ,      __snake_case = "epsilon"    ,      __snake_case = "linspace"    ,      __snake_case = 0    ,      ):
  
           if trained_betas is not None:
                snake_case					=       torch.tensor(_lowerCamelCase    ,      dtype=torch.floataa					)
           elif beta_schedule == "linear":
                snake_case					=       torch.linspace(_lowerCamelCase    ,      _lowerCamelCase    ,      _lowerCamelCase    ,      dtype=torch.floataa					)
           elif beta_schedule == "scaled_linear":
                # this schedule is very specific to the latent diffusion model.
                snake_case					=       (
                    torch.linspace(beta_start**0.5    ,      beta_end**0.5    ,      _lowerCamelCase    ,      dtype=torch.floataa					) ** 2
                )
           elif beta_schedule == "squaredcos_cap_v2":
                # Glide cosine schedule
                snake_case					=       betas_for_alpha_bar(_lowerCamelCase					)
           else:
                raise NotImplementedError(F'''{beta_schedule} does is not implemented for {self.__class__}'''					)
  
           snake_case					=       1.0 - self.betas
           snake_case					=       torch.cumprod(self.alphas    ,      dim=0					)
  
           snake_case					=       np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5					)
           snake_case					=       np.concatenate([sigmas[::-1], [0.0]]					).astype(np.floataa					)
           snake_case					=       torch.from_numpy(_lowerCamelCase					)
  
           # setable values
           snake_case					=       None
           snake_case					=       np.linspace(0    ,      num_train_timesteps - 1    ,      _lowerCamelCase    ,      dtype=_lowerCamelCase					)[::-1].copy()
           snake_case					=       torch.from_numpy(_lowerCamelCase					)
           snake_case					=       False
      @property
      def 	a_    ( self					):
  
           if self.config.timestep_spacing in ["linspace", "trailing"]:
                return self.sigmas.max()
  
           return (self.sigmas.max() ** 2 + 1) ** 0.5
      def 	a_    ( self    ,      __snake_case    ,      __snake_case					):
  
           if isinstance(_lowerCamelCase    ,      torch.Tensor					):
                snake_case					=       timestep.to(self.timesteps.device					)
           snake_case					=       (self.timesteps == timestep).nonzero().item()
           snake_case					=       self.sigmas[step_index]
           snake_case					=       sample / ((sigma**2 + 1) ** 0.5)
           snake_case					=       True
           return sample
      def 	a_    ( self    ,      __snake_case    ,      __snake_case = None					):
           snake_case					=       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":
                snake_case					=       np.linspace(0    ,      self.config.num_train_timesteps - 1    ,      _lowerCamelCase    ,      dtype=_lowerCamelCase					)[
                    ::-1
                ].copy()
           elif self.config.timestep_spacing == "leading":
                snake_case					=       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
                snake_case					=       (np.arange(0    ,      _lowerCamelCase					) * step_ratio).round()[::-1].copy().astype(_lowerCamelCase					)
                timesteps += self.config.steps_offset
           elif self.config.timestep_spacing == "trailing":
                snake_case					=       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
                snake_case					=       (np.arange(self.config.num_train_timesteps    ,      0    ,      -step_ratio					)).round().copy().astype(_lowerCamelCase					)
                timesteps -= 1
           else:
                raise ValueError(
                    F'''{self.config.timestep_spacing} is not supported. Please make sure to choose one of \'linspace\', \'leading\' or \'trailing\'.'''					)
  
           snake_case					=       np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5					)
           snake_case					=       np.interp(_lowerCamelCase    ,      np.arange(0    ,      len(_lowerCamelCase					)					)    ,      _lowerCamelCase					)
           snake_case					=       np.concatenate([sigmas, [0.0]]					).astype(np.floataa					)
           snake_case					=       torch.from_numpy(_lowerCamelCase					).to(device=_lowerCamelCase					)
           if str(_lowerCamelCase					).startswith('''mps'''					):
                # mps does not support float64
                snake_case					=       torch.from_numpy(_lowerCamelCase					).to(_lowerCamelCase    ,      dtype=torch.floataa					)
           else:
                snake_case					=       torch.from_numpy(_lowerCamelCase					).to(device=_lowerCamelCase					)
      def 	a_    ( self    ,      __snake_case    ,      __snake_case    ,      __snake_case    ,      __snake_case = None    ,      __snake_case = True    ,      ):
  
           if (
               isinstance(_lowerCamelCase    ,      _lowerCamelCase					)
               or isinstance(_lowerCamelCase    ,      torch.IntTensor					)
               or isinstance(_lowerCamelCase    ,      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(_lowerCamelCase    ,      torch.Tensor					):
                snake_case					=       timestep.to(self.timesteps.device					)
  
           snake_case					=       (self.timesteps == timestep).nonzero().item()
           snake_case					=       self.sigmas[step_index]
  
           # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise
           if self.config.prediction_type == "epsilon":
                snake_case					=       sample - sigma * model_output
           elif self.config.prediction_type == "v_prediction":
                # * c_out + input * c_skip
                snake_case					=       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`'''					)
  
           snake_case					=       self.sigmas[step_index]
           snake_case					=       self.sigmas[step_index + 1]
           snake_case					=       (sigma_to**2 * (sigma_from**2 - sigma_to**2) / sigma_from**2) ** 0.5
           snake_case					=       (sigma_to**2 - sigma_up**2) ** 0.5
  
           # 2. Convert to an ODE derivative
           snake_case					=       (sample - pred_original_sample) / sigma
  
           snake_case					=       sigma_down - sigma
  
           snake_case					=       sample + derivative * dt
  
           snake_case					=       model_output.device
           snake_case					=       randn_tensor(model_output.shape    ,      dtype=model_output.dtype    ,      device=_lowerCamelCase    ,      generator=_lowerCamelCase					)
  
           snake_case					=       prev_sample + noise * sigma_up
  
           if not return_dict:
                return (prev_sample,)
  
           return EulerAncestralDiscreteSchedulerOutput(
               prev_sample=_lowerCamelCase    ,      pred_original_sample=_lowerCamelCase					)
      def 	a_    ( self    ,      __snake_case    ,      __snake_case    ,      __snake_case    ,      ):
           snake_case					=       self.sigmas.to(device=original_samples.device    ,      dtype=original_samples.dtype					)
           if original_samples.device.type == "mps" and torch.is_floating_point(_lowerCamelCase					):
                # mps does not support float64
                snake_case					=       self.timesteps.to(original_samples.device    ,      dtype=torch.floataa					)
                snake_case					=       timesteps.to(original_samples.device    ,      dtype=torch.floataa					)
           else:
                snake_case					=       self.timesteps.to(original_samples.device					)
                snake_case					=       timesteps.to(original_samples.device					)
  
           snake_case					=       [(schedule_timesteps == t).nonzero().item() for t in timesteps]
  
           snake_case					=       sigmas[step_indices].flatten()
           while len(sigma.shape					) < len(original_samples.shape					):
                snake_case					=       sigma.unsqueeze(-1					)
  
           snake_case					=       original_samples + noise * sigma
           return noisy_samples
  
  
  
  
      def __len__( self					):
           return self.config.num_train_timesteps
  
  
  
  
  
style_context: 
  
  
  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__      (					snake_case__ ,      snake_case__ ,      unittest.TestCase			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      __magic_name__    						=	StableDiffusionParadigmsPipeline
      __magic_name__    						=	TEXT_TO_IMAGE_PARAMS
      __magic_name__    						=	TEXT_TO_IMAGE_BATCH_PARAMS
      __magic_name__    						=	TEXT_TO_IMAGE_IMAGE_PARAMS
      __magic_name__    						=	TEXT_TO_IMAGE_IMAGE_PARAMS
      def 	a_    ( self					):
           torch.manual_seed(0					)
           snake_case					=       UNetaDConditionModel(
               block_out_channels=(3_2, 6_4)    ,      layers_per_block=2    ,      sample_size=3_2    ,      in_channels=4    ,      out_channels=4    ,      down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')    ,      up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')    ,      cross_attention_dim=3_2    ,      attention_head_dim=(2, 4)    ,      use_linear_projection=__snake_case    ,      )
           snake_case					=       DDIMParallelScheduler(
               beta_start=0.0_0085    ,      beta_end=0.012    ,      beta_schedule='''scaled_linear'''    ,      clip_sample=__snake_case    ,      set_alpha_to_one=__snake_case    ,      )
           torch.manual_seed(0					)
           snake_case					=       AutoencoderKL(
               block_out_channels=[3_2, 6_4]    ,      in_channels=3    ,      out_channels=3    ,      down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']    ,      up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']    ,      latent_channels=4    ,      sample_size=1_2_8    ,      )
           torch.manual_seed(0					)
           snake_case					=       CLIPTextConfig(
               bos_token_id=0    ,      eos_token_id=2    ,      hidden_size=3_2    ,      intermediate_size=3_7    ,      layer_norm_eps=1E-05    ,      num_attention_heads=4    ,      num_hidden_layers=5    ,      pad_token_id=1    ,      vocab_size=1_0_0_0    ,      hidden_act='''gelu'''    ,      projection_dim=5_1_2    ,      )
           snake_case					=       CLIPTextModel(__snake_case					)
           snake_case					=       CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''					)
  
           snake_case					=       {
               '''unet''': unet,
               '''scheduler''': scheduler,
               '''vae''': vae,
               '''text_encoder''': text_encoder,
               '''tokenizer''': tokenizer,
               '''safety_checker''': None,
               '''feature_extractor''': None,
           }
           return components
      def 	a_    ( self    ,      __snake_case    ,      __snake_case=0					):
           if str(__snake_case					).startswith('''mps'''					):
                snake_case					=       torch.manual_seed(__snake_case					)
           else:
                snake_case					=       torch.Generator(device=__snake_case					).manual_seed(__snake_case					)
           snake_case					=       {
               '''prompt''': '''a photograph of an astronaut riding a horse''',
               '''generator''': generator,
               '''num_inference_steps''': 1_0,
               '''guidance_scale''': 6.0,
               '''output_type''': '''numpy''',
               '''parallel''': 3,
               '''debug''': True,
           }
           return inputs
      def 	a_    ( self					):
           snake_case					=       '''cpu'''  # ensure determinism for the device-dependent torch.Generator
           snake_case					=       self.get_dummy_components()
           snake_case					=       StableDiffusionParadigmsPipeline(**__snake_case					)
           snake_case					=       sd_pipe.to(__snake_case					)
           sd_pipe.set_progress_bar_config(disable=__snake_case					)
  
           snake_case					=       self.get_dummy_inputs(__snake_case					)
           snake_case					=       sd_pipe(**__snake_case					).images
           snake_case					=       image[0, -3:, -3:, -1]
           assert image.shape == (1, 6_4, 6_4, 3)
  
           snake_case					=       np.array([0.4773, 0.5417, 0.4723, 0.4925, 0.5631, 0.4752, 0.5240, 0.4935, 0.5023]					)
  
           assert np.abs(image_slice.flatten() - expected_slice					).max() < 1E-2
      def 	a_    ( self					):
           snake_case					=       '''cpu'''  # ensure determinism for the device-dependent torch.Generator
           snake_case					=       self.get_dummy_components()
           torch.manual_seed(0					)
           snake_case					=       DDPMParallelScheduler()
           torch.manual_seed(0					)
           snake_case					=       StableDiffusionParadigmsPipeline(**__snake_case					)
           snake_case					=       sd_pipe.to(__snake_case					)
           sd_pipe.set_progress_bar_config(disable=__snake_case					)
  
           snake_case					=       self.get_dummy_inputs(__snake_case					)
           snake_case					=       sd_pipe(**__snake_case					).images
           snake_case					=       image[0, -3:, -3:, -1]
           assert image.shape == (1, 6_4, 6_4, 3)
  
           snake_case					=       np.array([0.3573, 0.4420, 0.4960, 0.4799, 0.3796, 0.3879, 0.4819, 0.4365, 0.4468]					)
  
           assert np.abs(image_slice.flatten() - expected_slice					).max() < 1E-2
      def 	a_    ( self					):
           super().test_inference_batch_consistent(batch_sizes=[1, 2]					)
      def 	a_    ( self					):
           super().test_inference_batch_single_identical(batch_size=2    ,      expected_max_diff=3E-3					)
  
  
  
  
      def 	a_    ( self					):
           snake_case					=       '''cpu'''  # ensure determinism for the device-dependent torch.Generator
           snake_case					=       self.get_dummy_components()
           snake_case					=       StableDiffusionParadigmsPipeline(**__snake_case					)
           snake_case					=       sd_pipe.to(__snake_case					)
           sd_pipe.set_progress_bar_config(disable=__snake_case					)
  
           snake_case					=       self.get_dummy_inputs(__snake_case					)
           snake_case					=       '''french fries'''
           snake_case					=       sd_pipe(**__snake_case    ,      negative_prompt=__snake_case					)
           snake_case					=       output.images
           snake_case					=       image[0, -3:, -3:, -1]
  
           assert image.shape == (1, 6_4, 6_4, 3)
  
           snake_case					=       np.array([0.4771, 0.5420, 0.4683, 0.4918, 0.5636, 0.4725, 0.5230, 0.4923, 0.5015]					)
  
           assert np.abs(image_slice.flatten() - expected_slice					).max() < 1E-2
  
  
  
  
  
  
  @slow
  @require_torch_gpu
  class 						A__      (					unittest.TestCase			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      def 	a_    ( self					):
           super().tearDown()
           gc.collect()
           torch.cuda.empty_cache()
      def 	a_    ( self    ,      __snake_case=0					):
           snake_case					=       torch.Generator(device=__snake_case					).manual_seed(__snake_case					)
           snake_case					=       {
               '''prompt''': '''a photograph of an astronaut riding a horse''',
               '''generator''': generator,
               '''num_inference_steps''': 1_0,
               '''guidance_scale''': 7.5,
               '''output_type''': '''numpy''',
               '''parallel''': 3,
               '''debug''': True,
           }
           return inputs
  
  
  
  
      def 	a_    ( self					):
           snake_case					=       '''stabilityai/stable-diffusion-2-base'''
           snake_case					=       DDIMParallelScheduler.from_pretrained(__snake_case    ,      subfolder='''scheduler'''					)
           snake_case					=       StableDiffusionParadigmsPipeline.from_pretrained(__snake_case    ,      scheduler=__snake_case    ,      safety_checker=__snake_case					)
           pipe.to(__snake_case					)
           pipe.set_progress_bar_config(disable=__snake_case					)
           pipe.enable_attention_slicing()
  
           snake_case					=       self.get_inputs()
           snake_case					=       pipe(**__snake_case					).images
           snake_case					=       image[0, -3:, -3:, -1].flatten()
  
           assert image.shape == (1, 5_1_2, 5_1_2, 3)
  
           snake_case					=       np.array([0.9622, 0.9602, 0.9748, 0.9591, 0.9630, 0.9691, 0.9661, 0.9631, 0.9741]					)
  
           assert np.abs(expected_slice - image_slice					).max() < 1E-2
  
  
  
  
  