





import re
from typing import Callable, List, Optional, Union

import tensorflow as tf


try:
  from tensorflow.keras.optimizers.legacy import Adam
except ImportError:
  from tensorflow.keras.optimizers import Adam







class 	__lowerCAmelCase      (       tf.keras.optimizers.schedules.LearningRateSchedule	):





   def __init__(			self       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case = 1.0       ,      snake_case = None       ,      )					->	Tuple:
      """simple docstring"""





      super().__init__()
      a__						:    Tuple   		=	initial_learning_rate
      a__						:    Tuple   		=	warmup_steps
      a__						:    Any   		=	power
      a__						:    List[Any]   		=	decay_schedule_fn
      a__						:    str   		=	name





   def __call__(			self       ,      snake_case   )					->	Dict:
      """simple docstring"""





      with tf.name_scope(self.name or "WarmUp"   ) as name:
         # Implements polynomial warmup. i.e., if global_step < warmup_steps, the
         # learning rate will be `global_step/num_warmup_steps * init_lr`.
         a__						:    Tuple   		=	tf.cast(snake_case       ,      tf.floataa   )
         a__						:    Optional[Any]   		=	tf.cast(self.warmup_steps       ,      tf.floataa   )
         a__						:    Optional[int]   		=	global_step_float / warmup_steps_float
         a__						:    List[Any]   		=	self.initial_learning_rate * tf.math.pow(snake_case       ,      self.power   )
         return tf.cond(
             global_step_float < warmup_steps_float       ,      lambda: warmup_learning_rate       ,      lambda: self.decay_schedule_fn(step - self.warmup_steps   )       ,      name=snake_case       ,      )





   def 						_snake_case			(			self   )					->	Dict:
      """simple docstring"""





      return {
          "initial_learning_rate": self.initial_learning_rate,
          "decay_schedule_fn": self.decay_schedule_fn,
          "warmup_steps": self.warmup_steps,
          "power": self.power,
          "name": self.name,
      }







def    _A	(   lowerCamelCase				,			lowerCamelCase				,			lowerCamelCase				,			lowerCamelCase = 0.0				,			lowerCamelCase = 0.9				,			lowerCamelCase = 0.999				,			lowerCamelCase = 1E-8				,			lowerCamelCase = None				,			lowerCamelCase = None				,			lowerCamelCase = 0.0				,			lowerCamelCase = 1.0				,			lowerCamelCase = None				,			):
   a__						:    Union[str, Any]   		=	tf.keras.optimizers.schedules.PolynomialDecay(
       initial_learning_rate=lowerCamelCase				,			decay_steps=num_train_steps - num_warmup_steps				,			end_learning_rate=init_lr * min_lr_ratio				,			power=lowerCamelCase				,			)
   if num_warmup_steps:
      a__						:    Any   		=	WarmUp(
          initial_learning_rate=lowerCamelCase				,			decay_schedule_fn=lowerCamelCase				,			warmup_steps=lowerCamelCase				,			)
   if weight_decay_rate > 0.0:
      a__						:    Any   		=	AdamWeightDecay(
          learning_rate=lowerCamelCase				,			weight_decay_rate=lowerCamelCase				,			beta_a=lowerCamelCase				,			beta_a=lowerCamelCase				,			epsilon=lowerCamelCase				,			clipnorm=lowerCamelCase				,			global_clipnorm=lowerCamelCase				,			exclude_from_weight_decay=["LayerNorm", "layer_norm", "bias"]				,			include_in_weight_decay=lowerCamelCase				,			)
   else:
      a__						:    str   		=	tf.keras.optimizers.Adam(
          learning_rate=lowerCamelCase				,			beta_a=lowerCamelCase				,			beta_a=lowerCamelCase				,			epsilon=lowerCamelCase				,			clipnorm=lowerCamelCase				,			global_clipnorm=lowerCamelCase				,			)
   # We return the optimizer and the LR scheduler in order to better track the
   # evolution of the LR independently of the optimizer.
   return optimizer, lr_schedule







class 	__lowerCAmelCase      (       _UpperCamelCase	):





   def __init__(			self       ,      snake_case = 0.001       ,      snake_case = 0.9       ,      snake_case = 0.999       ,      snake_case = 1E-7       ,      snake_case = False       ,      snake_case = 0.0       ,      snake_case = None       ,      snake_case = None       ,      snake_case = "AdamWeightDecay"       ,      **snake_case       ,      )					->	Dict:
      """simple docstring"""





      super().__init__(snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      **snake_case   )
      a__						:    List[str]   		=	weight_decay_rate
      a__						:    str   		=	include_in_weight_decay
      a__						:    List[Any]   		=	exclude_from_weight_decay





   @classmethod
   def 						_snake_case			(			cls       ,      snake_case   )					->	Dict:
      """simple docstring"""





      a__						:    str   		=	{"WarmUp": WarmUp}
      return super(snake_case       ,      cls   ).from_config(snake_case       ,      custom_objects=snake_case   )





   def 						_snake_case			(			self       ,      snake_case       ,      snake_case       ,      snake_case   )					->	List[str]:
      """simple docstring"""





      super(snake_case       ,      self   )._prepare_local(snake_case       ,      snake_case       ,      snake_case   )
      a__						:    List[Any]   		=	tf.constant(
          self.weight_decay_rate       ,      name="adam_weight_decay_rate"   )





   def 						_snake_case			(			self       ,      snake_case       ,      snake_case       ,      snake_case   )					->	Optional[Any]:
      """simple docstring"""





      a__						:    Tuple   		=	self._do_use_weight_decay(var.name   )
      if do_decay:
         return var.assign_sub(
             learning_rate * var * apply_state[(var.device, var.dtype.base_dtype)]["weight_decay_rate"]       ,      use_locking=self._use_locking       ,      )
      return tf.no_op()





   def 						_snake_case			(			self       ,      snake_case       ,      snake_case=None       ,      **snake_case   )					->	List[str]:
      """simple docstring"""





      a__		,  a__						:    str   		=	list(zip(*snake_case   )   )
      return super(snake_case       ,      self   ).apply_gradients(zip(snake_case       ,      snake_case   )       ,      name=snake_case       ,      **snake_case   )





   def 						_snake_case			(			self       ,      snake_case       ,      snake_case       ,      snake_case   )					->	Dict:
      """simple docstring"""





      if apply_state is None:
         return self._decayed_lr_t[var_dtype], {}

      a__						:    Optional[int]   		=	apply_state or {}
      a__						:    str   		=	apply_state.get((var_device, var_dtype)   )
      if coefficients is None:
         a__						:    Tuple   		=	self._fallback_apply_state(snake_case       ,      snake_case   )
         a__						:    Any   		=	coefficients

      return coefficients["lr_t"], {"apply_state": apply_state}





   def 						_snake_case			(			self       ,      snake_case       ,      snake_case       ,      snake_case=None   )					->	int:
      """simple docstring"""





      a__		,  a__						:    str   		=	self._get_lr(var.device       ,      var.dtype.base_dtype       ,      snake_case   )
      a__						:    Any   		=	self._decay_weights_op(snake_case       ,      snake_case       ,      snake_case   )
      with tf.control_dependencies([decay]   ):
         return super(snake_case       ,      self   )._resource_apply_dense(snake_case       ,      snake_case       ,      **snake_case   )





   def 						_snake_case			(			self       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case=None   )					->	Union[str, Any]:
      """simple docstring"""





      a__		,  a__						:    List[str]   		=	self._get_lr(var.device       ,      var.dtype.base_dtype       ,      snake_case   )
      a__						:    Any   		=	self._decay_weights_op(snake_case       ,      snake_case       ,      snake_case   )
      with tf.control_dependencies([decay]   ):
         return super(snake_case       ,      self   )._resource_apply_sparse(snake_case       ,      snake_case       ,      snake_case       ,      **snake_case   )





   def 						_snake_case			(			self   )					->	Optional[Any]:
      """simple docstring"""





      a__						:    Tuple   		=	super().get_config()
      config.update({"weight_decay_rate": self.weight_decay_rate}   )
      return config





   def 						_snake_case			(			self       ,      snake_case   )					->	int:
      """simple docstring"""





      if self.weight_decay_rate == 0:
         return False

      if self._include_in_weight_decay:
         for r in self._include_in_weight_decay:
            if re.search(snake_case       ,      snake_case   ) is not None:
               return True

      if self._exclude_from_weight_decay:
         for r in self._exclude_from_weight_decay:
            if re.search(snake_case       ,      snake_case   ) is not None:
               return False
      return True







class 	__lowerCAmelCase      (       _UpperCamelCase	):





   def __init__(			self   )					->	Dict:
      """simple docstring"""





      a__						:    Union[str, Any]   		=	[]
      a__						:    Tuple   		=	None





   @property
   def 						_snake_case			(			self   )					->	Any:
      """simple docstring"""





      if self._accum_steps is None:
         a__						:    Optional[Any]   		=	tf.Variable(
             tf.constant(0       ,      dtype=tf.intaa   )       ,      trainable=snake_case       ,      synchronization=tf.VariableSynchronization.ON_READ       ,      aggregation=tf.VariableAggregation.ONLY_FIRST_REPLICA       ,      )

      return self._accum_steps.value()





   @property
   def 						_snake_case			(			self   )					->	Dict:
      """simple docstring"""





      if not self._gradients:
         raise ValueError("The accumulator should be called first to initialize the gradients"   )
      return [gradient.value() if gradient is not None else gradient for gradient in self._gradients]





   def __call__(			self       ,      snake_case   )					->	Tuple:
      """simple docstring"""





      if not self._gradients:
         a__						:    Union[str, Any]   		=	self.step  # Create the step variable.
         self._gradients.extend(
             [
                 tf.Variable(
                     tf.zeros_like(snake_case   )       ,      trainable=snake_case       ,      synchronization=tf.VariableSynchronization.ON_READ       ,      aggregation=tf.VariableAggregation.ONLY_FIRST_REPLICA       ,      )
                 if gradient is not None
                 else gradient
                 for gradient in gradients
             ]   )
      if len(snake_case   ) != len(self._gradients   ):
         raise ValueError(F"""Expected {len(self._gradients   )} gradients, but got {len(snake_case   )}"""   )

      for accum_gradient, gradient in zip(self._gradients       ,      snake_case   ):
         if accum_gradient is not None and gradient is not None:
            accum_gradient.assign_add(snake_case   )

      self._accum_steps.assign_add(1   )





   def 						_snake_case			(			self   )					->	Optional[Any]:
      """simple docstring"""





      if not self._gradients:
         return
      self._accum_steps.assign(0   )
      for gradient in self._gradients:
         if gradient is not None:
            gradient.assign(tf.zeros_like(snake_case   )   )








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

import numpy as np
import PIL
from PIL import Image

from ...utils import (
    BaseOutput,
    OptionalDependencyNotAvailable,
    is_flax_available,
    is_k_diffusion_available,
    is_k_diffusion_version,
    is_onnx_available,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)







@dataclass
class 	__lowerCAmelCase      (       _UpperCamelCase	):
   _UpperCamelCase			:    Union[List[PIL.Image.Image], np.ndarray]
   _UpperCamelCase			:    Optional[List[bool]]


try:
  if not (is_transformers_available() and is_torch_available()):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from ...utils.dummy_torch_and_transformers_objects import *  # noqa F403
else:
  from .pipeline_cycle_diffusion import CycleDiffusionPipeline
  from .pipeline_stable_diffusion import StableDiffusionPipeline
  from .pipeline_stable_diffusion_attend_and_excite import StableDiffusionAttendAndExcitePipeline
  from .pipeline_stable_diffusion_imgaimg import StableDiffusionImgaImgPipeline
  from .pipeline_stable_diffusion_inpaint import StableDiffusionInpaintPipeline
  from .pipeline_stable_diffusion_inpaint_legacy import StableDiffusionInpaintPipelineLegacy
  from .pipeline_stable_diffusion_instruct_pixapix import StableDiffusionInstructPixaPixPipeline
  from .pipeline_stable_diffusion_latent_upscale import StableDiffusionLatentUpscalePipeline
  from .pipeline_stable_diffusion_ldmad import StableDiffusionLDMaDPipeline
  from .pipeline_stable_diffusion_model_editing import StableDiffusionModelEditingPipeline
  from .pipeline_stable_diffusion_panorama import StableDiffusionPanoramaPipeline
  from .pipeline_stable_diffusion_paradigms import StableDiffusionParadigmsPipeline
  from .pipeline_stable_diffusion_sag import StableDiffusionSAGPipeline
  from .pipeline_stable_diffusion_upscale import StableDiffusionUpscalePipeline
  from .pipeline_stable_unclip import StableUnCLIPPipeline
  from .pipeline_stable_unclip_imgaimg import StableUnCLIPImgaImgPipeline
  from .safety_checker import StableDiffusionSafetyChecker
  from .stable_unclip_image_normalizer import StableUnCLIPImageNormalizer

try:
  if not (is_transformers_available() and is_torch_available() and is_transformers_version(""">=""", """4.25.0""")):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from ...utils.dummy_torch_and_transformers_objects import StableDiffusionImageVariationPipeline
else:
  from .pipeline_stable_diffusion_image_variation import StableDiffusionImageVariationPipeline


try:
  if not (is_transformers_available() and is_torch_available() and is_transformers_version(""">=""", """4.26.0""")):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from ...utils.dummy_torch_and_transformers_objects import (
      StableDiffusionDepthaImgPipeline,
      StableDiffusionDiffEditPipeline,
      StableDiffusionPixaPixZeroPipeline,
  )
else:
  from .pipeline_stable_diffusion_depthaimg import StableDiffusionDepthaImgPipeline
  from .pipeline_stable_diffusion_diffedit import StableDiffusionDiffEditPipeline
  from .pipeline_stable_diffusion_pixapix_zero import StableDiffusionPixaPixZeroPipeline


try:
  if not (
      is_torch_available()
      and is_transformers_available()
      and is_k_diffusion_available()
      and is_k_diffusion_version(""">=""", """0.0.12""")
  ):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from ...utils.dummy_torch_and_transformers_and_k_diffusion_objects import *  # noqa F403
else:
  from .pipeline_stable_diffusion_k_diffusion import StableDiffusionKDiffusionPipeline

try:
  if not (is_transformers_available() and is_onnx_available()):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from ...utils.dummy_onnx_objects import *  # noqa F403
else:
  from .pipeline_onnx_stable_diffusion import OnnxStableDiffusionPipeline, StableDiffusionOnnxPipeline
  from .pipeline_onnx_stable_diffusion_imgaimg import OnnxStableDiffusionImgaImgPipeline
  from .pipeline_onnx_stable_diffusion_inpaint import OnnxStableDiffusionInpaintPipeline
  from .pipeline_onnx_stable_diffusion_inpaint_legacy import OnnxStableDiffusionInpaintPipelineLegacy
  from .pipeline_onnx_stable_diffusion_upscale import OnnxStableDiffusionUpscalePipeline

if is_transformers_available() and is_flax_available():
  import flax







  @flax.struct.dataclass
  class 	__lowerCAmelCase      (       _UpperCamelCase	):
     _UpperCamelCase			:    np.ndarray
     _UpperCamelCase			:    List[bool]







  from ...schedulers.scheduling_pndm_flax import PNDMSchedulerState
  from .pipeline_flax_stable_diffusion import FlaxStableDiffusionPipeline
  from .pipeline_flax_stable_diffusion_imgaimg import FlaxStableDiffusionImgaImgPipeline
  from .pipeline_flax_stable_diffusion_inpaint import FlaxStableDiffusionInpaintPipeline
  from .safety_checker_flax import FlaxStableDiffusionSafetyChecker

