'''simple docstring'''




from typing import Callable, List, Optional, Union

import torch

from ...models import UNetaDModel
from ...schedulers import CMStochasticIterativeScheduler
from ...utils import (
    is_accelerate_available,
    is_accelerate_version,
    logging,
    randn_tensor,
    replace_example_docstring,
)
from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput


__snake_case   :      List[str]						    =  logging.get_logger(__name__)  # pylint: disable=invalid-name


__snake_case   :      Tuple						    =  """
    Examples:
        ```py
        >>> import torch

        >>> from diffusers import ConsistencyModelPipeline

        >>> device = \"cuda\"
        >>> # Load the cd_imagenet64_l2 checkpoint.
        >>> model_id_or_path = \"openai/diffusers-cd_imagenet64_l2\"
        >>> pipe = ConsistencyModelPipeline.from_pretrained(model_id_or_path, torch_dtype=torch.float16)
        >>> pipe.to(device)

        >>> # Onestep Sampling
        >>> image = pipe(num_inference_steps=1).images[0]
        >>> image.save(\"cd_imagenet64_l2_onestep_sample.png\")

        >>> # Onestep sampling, class-conditional image generation
        >>> # ImageNet-64 class label 145 corresponds to king penguins
        >>> image = pipe(num_inference_steps=1, class_labels=145).images[0]
        >>> image.save(\"cd_imagenet64_l2_onestep_sample_penguin.png\")

        >>> # Multistep sampling, class-conditional image generation
        >>> # Timesteps can be explicitly specified; the particular timesteps below are from the original Github repo:
        >>> # https://github.com/openai/consistency_models/blob/main/scripts/launch.sh#L77
        >>> image = pipe(num_inference_steps=None, timesteps=[22, 0], class_labels=145).images[0]
        >>> image.save(\"cd_imagenet64_l2_multistep_sample_penguin.png\")
        ```
"""




class 	__UpperCAmelCase				(				_lowerCAmelCase     ):





       '''simple docstring'''



       def __init__(      self    ,     _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE )		->     None:
           super().__init__()

           self.register_modules(
               unet=A__    ,     scheduler=A__    ,     )

           A_     =   None

       def 					__A       (      self    ,     _SCREAMING_SNAKE_CASE=0 )		->     Any:
           if is_accelerate_available() and is_accelerate_version('''>='''    ,     '''0.14.0''' ):
               from accelerate import cpu_offload
           else:
               raise ImportError('''`enable_sequential_cpu_offload` requires `accelerate v0.14.0` or higher''' )

           A_     =   torch.device(F'''cuda:{gpu_id}''' )

           if self.device.type != "cpu":
               self.to('''cpu'''    ,     silence_dtype_warnings=A__ )
               torch.cuda.empty_cache()  # otherwise we don't see the memory savings (but they probably exist)

           for cpu_offloaded_model in [self.unet]:
               cpu_offload(A__    ,     A__ )

           if self.safety_checker is not None:
               cpu_offload(self.safety_checker    ,     execution_device=A__    ,     offload_buffers=A__ )

       def 					__A       (      self    ,     _SCREAMING_SNAKE_CASE=0 )		->     List[Any]:
           if is_accelerate_available() and is_accelerate_version('''>='''    ,     '''0.17.0.dev0''' ):
               from accelerate import cpu_offload_with_hook
           else:
               raise ImportError('''`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.''' )

           A_     =   torch.device(F'''cuda:{gpu_id}''' )

           if self.device.type != "cpu":
               self.to('''cpu'''    ,     silence_dtype_warnings=A__ )
               torch.cuda.empty_cache()  # otherwise we don't see the memory savings (but they probably exist)

           A_     =   None
           for cpu_offloaded_model in [self.unet]:
               A_   ,A_     =   cpu_offload_with_hook(A__    ,     A__    ,     prev_module_hook=A__ )

           if self.safety_checker is not None:
               A_   ,A_     =   cpu_offload_with_hook(self.safety_checker    ,     A__    ,     prev_module_hook=A__ )

           # We'll offload the last model manually.
           A_     =   hook

       @property
       # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
       def 					__A       (      self )		->     int:
           if not hasattr(self.unet    ,     '''_hf_hook''' ):
               return self.device
           for module in self.unet.modules():
               if (
                   hasattr(A__    ,     '''_hf_hook''' )
                   and hasattr(module._hf_hook    ,     '''execution_device''' )
                   and module._hf_hook.execution_device is not None
               ):
                   return torch.device(module._hf_hook.execution_device )
           return self.device

       def 					__A       (      self    ,     _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE=None )		->     Tuple:
           A_     =   (batch_size, num_channels, height, width)
           if isinstance(A__    ,     A__ ) and len(A__ ) != batch_size:
               raise ValueError(
                   F'''You have passed a list of generators of length {len(A__ )}, but requested an effective batch'''
                   F''' size of {batch_size}. Make sure the batch size matches the length of the generators.''' )

           if latents is None:
               A_     =   randn_tensor(A__    ,     generator=A__    ,     device=A__    ,     dtype=A__ )
           else:
               A_     =   latents.to(device=A__    ,     dtype=A__ )

           # scale the initial noise by the standard deviation required by the scheduler
           A_     =   latents * self.scheduler.init_noise_sigma
           return latents

       def 					__A       (      self    ,     _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE = "pil" )		->     Tuple:
           if output_type not in ["pt", "np", "pil"]:
               raise ValueError(
                   F'''output_type={output_type} is not supported. Make sure to choose one of [\'pt\', \'np\', or \'pil\']''' )

           # Equivalent to diffusers.VaeImageProcessor.denormalize
           A_     =   (sample / 2 + 0.5).clamp(0    ,     1 )
           if output_type == "pt":
               return sample

           # Equivalent to diffusers.VaeImageProcessor.pt_to_numpy
           A_     =   sample.cpu().permute(0    ,     2    ,     3    ,     1 ).numpy()
           if output_type == "np":
               return sample

           # Output_type must be 'pil'
           A_     =   self.numpy_to_pil(A__ )
           return sample

       def 					__A       (      self    ,     _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE=None )		->     int:
           if self.unet.config.num_class_embeds is not None:
               if isinstance(A__    ,     A__ ):
                   A_     =   torch.tensor(A__    ,     dtype=torch.int )
               elif isinstance(A__    ,     A__ ):
                   assert batch_size == 1, "Batch size must be 1 if classes is an int"
                   A_     =   torch.tensor([class_labels]    ,     dtype=torch.int )
               elif class_labels is None:
                   # Randomly generate batch_size class labels
                   # TODO: should use generator here? int analogue of randn_tensor is not exposed in ...utils
                   A_     =   torch.randint(0    ,     self.unet.config.num_class_embeds    ,     size=(batch_size,) )
               A_     =   class_labels.to(A__ )
           else:
               A_     =   None
           return class_labels

       def 					__A       (      self    ,     _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE )		->     List[Any]:
           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:
               logger.warning(
                   F'''Both `num_inference_steps`: {num_inference_steps} and `timesteps`: {timesteps} are supplied;'''
                   ''' `timesteps` will be used over `num_inference_steps`.''' )

           if latents is not None:
               A_     =   (batch_size, 3, img_size, img_size)
               if latents.shape != expected_shape:
                   raise ValueError(F'''The shape of latents is {latents.shape} but is expected to be {expected_shape}.''' )

           if (callback_steps is None) or (
               callback_steps is not None and (not isinstance(A__    ,     A__ ) or callback_steps <= 0)
           ):
               raise ValueError(
                   F'''`callback_steps` has to be a positive integer but is {callback_steps} of type'''
                   F''' {type(A__ )}.''' )

       @torch.no_grad()
       @replace_example_docstring(A__ )
       def __call__(      self    ,     _SCREAMING_SNAKE_CASE = 1    ,     _SCREAMING_SNAKE_CASE = None    ,     _SCREAMING_SNAKE_CASE = 1    ,     _SCREAMING_SNAKE_CASE = None    ,     _SCREAMING_SNAKE_CASE = None    ,     _SCREAMING_SNAKE_CASE = None    ,     _SCREAMING_SNAKE_CASE = "pil"    ,     _SCREAMING_SNAKE_CASE = True    ,     _SCREAMING_SNAKE_CASE = None    ,     _SCREAMING_SNAKE_CASE = 1    ,     )		->     Optional[Any]:
           A_     =   self.unet.config.sample_size
           A_     =   self._execution_device

           # 1. Check inputs
           self.check_inputs(A__    ,     A__    ,     A__    ,     A__    ,     A__    ,     A__ )

           # 2. Prepare image latents
           # Sample image latents x_0 ~ N(0, sigma_0^2 * I)
           A_     =   self.prepare_latents(
               batch_size=A__    ,     num_channels=self.unet.config.in_channels    ,     height=A__    ,     width=A__    ,     dtype=self.unet.dtype    ,     device=A__    ,     generator=A__    ,     latents=A__    ,     )

           # 3. Handle class_labels for class-conditional models
           A_     =   self.prepare_class_labels(A__    ,     A__    ,     class_labels=A__ )

           # 4. Prepare timesteps
           if timesteps is not None:
               self.scheduler.set_timesteps(timesteps=A__    ,     device=A__ )
               A_     =   self.scheduler.timesteps
               A_     =   len(A__ )
           else:
               self.scheduler.set_timesteps(A__ )
               A_     =   self.scheduler.timesteps

           # 5. Denoising loop
           # Multistep sampling: implements Algorithm 1 in the paper
           with self.progress_bar(total=A__ ) as progress_bar:
               for i, t in enumerate(A__ ):
                   A_     =   self.scheduler.scale_model_input(A__    ,     A__ )
                   A_     =   self.unet(A__    ,     A__    ,     class_labels=A__    ,     return_dict=A__ )[0]

                   A_     =   self.scheduler.step(A__    ,     A__    ,     A__    ,     generator=A__ )[0]

                   # call the callback, if provided
                   progress_bar.update()
                   if callback is not None and i % callback_steps == 0:
                       callback(A__    ,     A__    ,     A__ )

        # 6. Post-process image sample
           A_     =   self.postprocess_image(A__    ,     output_type=A__ )

           # Offload last model to CPU
           if hasattr(self    ,     '''final_offload_hook''' ) and self.final_offload_hook is not None:
               self.final_offload_hook.offload()

           if not return_dict:
               return (image,)

           return ImagePipelineOutput(images=A__ )












# DISCLAIMER: This code is strongly influenced by https://github.com/pesser/pytorch_diffusion
# and https://github.com/hojonathanho/diffusion

from dataclasses import dataclass
from typing import Optional, Tuple, Union

import flax
import jax.numpy as jnp

from ..configuration_utils import ConfigMixin, register_to_config
from .scheduling_utils_flax import (
    CommonSchedulerState,
    FlaxKarrasDiffusionSchedulers,
    FlaxSchedulerMixin,
    FlaxSchedulerOutput,
    add_noise_common,
    get_velocity_common,
)






@flax.struct.dataclass
class 	A	:
     """simple docstring"""

     _snake_case:		CommonSchedulerState
     _snake_case:		jnp.ndarray

     # setable values
     _snake_case:		jnp.ndarray
     _snake_case:		jnp.ndarray
     _snake_case:		Optional[int]   =None




     @classmethod
     def        __snake_case      (			cls     :      Optional[int]		,     A__     :      CommonSchedulerState		,     A__     :      jnp.ndarray		,     A__     :      jnp.ndarray		,     A__     :      jnp.ndarray		,     ) ->				Any:
           return cls(
               common=A__		,     final_alpha_cumprod=A__		,     init_noise_sigma=A__		,     timesteps=A__		,     )






@dataclass
class 	A	(						_lowerCAmelCase						):
     """simple docstring"""

     _snake_case:		DDIMSchedulerState






class 	A	(						_lowerCAmelCase	,  _lowerCAmelCase						):
     """simple docstring"""

     _snake_case:		List[str]   =[e.name for e in FlaxKarrasDiffusionSchedulers]

     _snake_case:		jnp.dtype




     @property
     def        __snake_case      (			self     :      Optional[int]	) ->				List[str]:
           return True




     @register_to_config
     def __init__(			self     :      Optional[Any]		,     A__     :      int = 1_0_0_0		,     A__     :      float = 0.0001		,     A__     :      float = 0.02		,     A__     :      str = "linear"		,     A__     :      Optional[jnp.ndarray] = None		,     A__     :      bool = True		,     A__     :      int = 0		,     A__     :      str = "epsilon"		,     A__     :      jnp.dtype = jnp.floataa		,     ) ->				Optional[int]:
           lowerCamelCase__					     = dtype




     def        __snake_case      (			self     :      Optional[Any]		,     A__     :      Optional[CommonSchedulerState] = None	) ->				DDIMSchedulerState:
           if common is None:
                 lowerCamelCase__					     = CommonSchedulerState.create(self	)

           # At every step in ddim, we are looking into the previous alphas_cumprod
           # For the final step, there is no previous alphas_cumprod because we are already at 0
           # `set_alpha_to_one` decides whether we set this parameter simply to one or
           # whether we use the final alpha of the "non-previous" one.
           lowerCamelCase__					     = (
               jnp.array(1.0		,     dtype=self.dtype	) if self.config.set_alpha_to_one else common.alphas_cumprod[0]
           )

           # standard deviation of the initial noise distribution
           lowerCamelCase__					     = jnp.array(1.0		,     dtype=self.dtype	)

           lowerCamelCase__					     = jnp.arange(0		,     self.config.num_train_timesteps	).round()[::-1]

           return DDIMSchedulerState.create(
               common=A__		,     final_alpha_cumprod=A__		,     init_noise_sigma=A__		,     timesteps=A__		,     )




     def        __snake_case      (			self     :      Optional[Any]		,     A__     :      DDIMSchedulerState		,     A__     :      jnp.ndarray		,     A__     :      Optional[int] = None	) ->				jnp.ndarray:
           return sample




     def        __snake_case      (			self     :      Union[str, Any]		,     A__     :      DDIMSchedulerState		,     A__     :      int		,     A__     :      Tuple = ()	) ->				DDIMSchedulerState:
           lowerCamelCase__					     = self.config.num_train_timesteps // num_inference_steps
           # creates integer timesteps by multiplying by ratio
           # rounding to avoid issues when num_inference_step is power of 3
           lowerCamelCase__					     = (jnp.arange(0		,     A__	) * step_ratio).round()[::-1] + self.config.steps_offset

           return state.replace(
               num_inference_steps=A__		,     timesteps=A__		,     )




     def        __snake_case      (			self     :      Optional[int]		,     A__     :      DDIMSchedulerState		,     A__     :      List[str]		,     A__     :      Optional[Any]	) ->				int:
           lowerCamelCase__					     = state.common.alphas_cumprod[timestep]
           lowerCamelCase__					     = jnp.where(
               prev_timestep >= 0		,     state.common.alphas_cumprod[prev_timestep]		,     state.final_alpha_cumprod	)
           lowerCamelCase__					     = 1 - alpha_prod_t
           lowerCamelCase__					     = 1 - alpha_prod_t_prev

           lowerCamelCase__					     = (beta_prod_t_prev / beta_prod_t) * (1 - alpha_prod_t / alpha_prod_t_prev)

           return variance




     def        __snake_case      (			self     :      List[str]		,     A__     :      DDIMSchedulerState		,     A__     :      jnp.ndarray		,     A__     :      int		,     A__     :      jnp.ndarray		,     A__     :      float = 0.0		,     A__     :      bool = True		,     ) ->				Union[FlaxDDIMSchedulerOutput, Tuple]:
           if state.num_inference_steps is None:
                 raise ValueError(
                     'Number of inference steps is \'None\', you need to run \'set_timesteps\' after creating the scheduler'	)

           # See formulas (12) and (16) of DDIM paper https://arxiv.org/pdf/2010.02502.pdf
           # Ideally, read DDIM paper in-detail understanding

           # Notation (<variable name> -> <name in paper>
           # - pred_noise_t -> e_theta(x_t, t)
           # - pred_original_sample -> f_theta(x_t, t) or x_0
           # - std_dev_t -> sigma_t
           # - eta -> η
           # - pred_sample_direction -> "direction pointing to x_t"
           # - pred_prev_sample -> "x_t-1"

           # 1. get previous step value (=t-1)
           lowerCamelCase__					     = timestep - self.config.num_train_timesteps // state.num_inference_steps

           lowerCamelCase__					     = state.common.alphas_cumprod
           lowerCamelCase__					     = state.final_alpha_cumprod

           # 2. compute alphas, betas
           lowerCamelCase__					     = alphas_cumprod[timestep]
           lowerCamelCase__					     = jnp.where(prev_timestep >= 0		,     alphas_cumprod[prev_timestep]		,     A__	)

           lowerCamelCase__					     = 1 - alpha_prod_t

           # 3. compute predicted original sample from predicted noise also called
           # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
           if self.config.prediction_type == "epsilon":
                 lowerCamelCase__					     = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5
                 lowerCamelCase__					     = model_output
           elif self.config.prediction_type == "sample":
                 lowerCamelCase__					     = model_output
                 lowerCamelCase__					     = (sample - alpha_prod_t ** 0.5 * pred_original_sample) / beta_prod_t ** 0.5
           elif self.config.prediction_type == "v_prediction":
                 lowerCamelCase__					     = (alpha_prod_t**0.5) * sample - (beta_prod_t**0.5) * model_output
                 lowerCamelCase__					     = (alpha_prod_t**0.5) * model_output + (beta_prod_t**0.5) * sample
           else:
                 raise ValueError(
                     F"""prediction_type given as {self.config.prediction_type} must be one of `epsilon`, `sample`, or"""
                     ' `v_prediction`'	)

           # 4. compute variance: "sigma_t(η)" -> see formula (16)
           # σ_t = sqrt((1 − α_t−1)/(1 − α_t)) * sqrt(1 − α_t/α_t−1)
           lowerCamelCase__					     = self._get_variance(A__		,     A__		,     A__	)
           lowerCamelCase__					     = eta * variance ** 0.5

           # 5. compute "direction pointing to x_t" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
           lowerCamelCase__					     = (1 - alpha_prod_t_prev - std_dev_t**2) ** 0.5 * pred_epsilon

           # 6. compute x_t without "random noise" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
           lowerCamelCase__					     = alpha_prod_t_prev ** 0.5 * pred_original_sample + pred_sample_direction

           if not return_dict:
                 return (prev_sample, state)

           return FlaxDDIMSchedulerOutput(prev_sample=A__		,     state=A__	)




     def        __snake_case      (			self     :      Tuple		,     A__     :      DDIMSchedulerState		,     A__     :      jnp.ndarray		,     A__     :      jnp.ndarray		,     A__     :      jnp.ndarray		,     ) ->				jnp.ndarray:
           return add_noise_common(state.common		,     A__		,     A__		,     A__	)




     def        __snake_case      (			self     :      List[Any]		,     A__     :      DDIMSchedulerState		,     A__     :      jnp.ndarray		,     A__     :      jnp.ndarray		,     A__     :      jnp.ndarray		,     ) ->				jnp.ndarray:
           return get_velocity_common(state.common		,     A__		,     A__		,     A__	)




     def __len__(			self     :      Any	) ->				Union[str, Any]:
           return self.config.num_train_timesteps