class_docstr: Pipeline based on the [`StableDiffusionPipeline`] for text-to-image generation using Safe Latent Diffusion.
  
  This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods
  implemented for all pipelines (downloading, saving, running on a particular device, etc.).
  
  Args:
      vae ([`AutoencoderKL`]):
          Variational Auto-Encoder (VAE) model to encode and decode images to and from latent representations.
      text_encoder ([`~transformers.CLIPTextModel`]):
          Frozen text-encoder ([clip-vit-large-patch14](https://huggingface.co/openai/clip-vit-large-patch14)).
      tokenizer ([`~transformers.CLIPTokenizer`]):
          A `CLIPTokenizer` to tokenize text.
      unet ([`UNet2DConditionModel`]):
          A `UNet2DConditionModel` to denoise the encoded image latents.
      scheduler ([`SchedulerMixin`]):
          A scheduler to be used in combination with `unet` to denoise the encoded image latents. Can be one of
          [`DDIMScheduler`], [`LMSDiscreteScheduler`], or [`PNDMScheduler`].
      safety_checker ([`StableDiffusionSafetyChecker`]):
          Classification module that estimates whether generated images could be considered offensive or harmful.
          Please refer to the [model card](https://huggingface.co/runwayml/stable-diffusion-v1-5) for more details
          about a model's potential harms.
      feature_extractor ([`~transformers.CLIPImageProcessor`]):
          A `CLIPImageProcessor` to extract features from generated images; used as inputs to the `safety_checker`.
class_docstr_tok: ["Pipeline","based","on","the","[","`","StableDiffusionPipeline","`","]","for","text-to-image","generation","using","Safe","Latent","Diffusion",".","This","model","inherits","from","[","`","DiffusionPipeline","`","]",".","Check","the","superclass","documentation","for","the","generic","methods","implemented","for","all","pipelines","(","downloading",",","saving",",","running","on","a","particular","device",",","etc.",")",".","Args",":","vae","(","[","`","AutoencoderKL","`","]",")",":","Variational","Auto-Encoder","(","VAE",")","model","to","encode","and","decode","images","to","and","from","latent","representations",".","text_encoder","(","[","`","~transformers.CLIPTextModel","`","]",")",":","Frozen","text-encoder","(","[","clip-vit-large-patch14","]","(","https",":","\/\/huggingface.co\/openai\/clip-vit-large-patch14",")",")",".","tokenizer","(","[","`","~transformers.CLIPTokenizer","`","]",")",":","A","`","CLIPTokenizer","`","to","tokenize","text",".","unet","(","[","`","UNet2DConditionModel","`","]",")",":","A","`","UNet2DConditionModel","`","to","denoise","the","encoded","image","latents",".","scheduler","(","[","`","SchedulerMixin","`","]",")",":","A","scheduler","to","be","used","in","combination","with","`","unet","`","to","denoise","the","encoded","image","latents",".","Can","be","one","of","[","`","DDIMScheduler","`","]",",","[","`","LMSDiscreteScheduler","`","]",",","or","[","`","PNDMScheduler","`","]",".","safety_checker","(","[","`","StableDiffusionSafetyChecker","`","]",")",":","Classification","module","that","estimates","whether","generated","images","could","be","considered","offensive","or","harmful",".","Please","refer","to","the","[","model","card","]","(","https",":","\/\/huggingface.co\/runwayml\/stable-diffusion-v1-5",")","for","more","details","about","a","model","'s","potential","harms",".","feature_extractor","(","[","`","~transformers.CLIPImageProcessor","`","]",")",":","A","`","CLIPImageProcessor","`","to","extract","features","from","generated","images",";","used","as","inputs","to","the","`","safety_checker","`","."]
code: def perform_safety_guidance(
      self,
      enable_safety_guidance,
      safety_momentum,
      noise_guidance,
      noise_pred_out,
      i,
      sld_guidance_scale,
      sld_warmup_steps,
      sld_threshold,
      sld_momentum_scale,
      sld_mom_beta,
  ):
      # Perform SLD guidance
      if enable_safety_guidance:
          if safety_momentum is None:
              safety_momentum = torch.zeros_like(noise_guidance)
          noise_pred_text, noise_pred_uncond = (
              noise_pred_out[0],
              noise_pred_out[1],
          )
          noise_pred_safety_concept = noise_pred_out[2]
  
          # Equation 6
          scale = torch.clamp(
              torch.abs((noise_pred_text - noise_pred_safety_concept))
              * sld_guidance_scale,
              max=1.0,
          )
  
          # Equation 6
          safety_concept_scale = torch.where(
              (noise_pred_text - noise_pred_safety_concept)
              >= sld_threshold,
              torch.zeros_like(scale),
              scale,
          )
  
          # Equation 4
          noise_guidance_safety = torch.mul(
              (noise_pred_safety_concept - noise_pred_uncond),
              safety_concept_scale,
          )
  
          # Equation 7
          noise_guidance_safety = (
              noise_guidance_safety
              + sld_momentum_scale * safety_momentum
          )
  
          # Equation 8
          safety_momentum = (
              sld_mom_beta * safety_momentum
              + (1 - sld_mom_beta) * noise_guidance_safety
          )
  
          if i >= sld_warmup_steps:  # Warmup
              # Equation 3
              noise_guidance = noise_guidance - noise_guidance_safety
      return noise_guidance, safety_momentum
  
imports: import inspect
  import warnings
  from typing import Callable, List, Optional, Union
  import numpy
  import torch
  from packaging import version
  from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer
  from ...configuration_utils import FrozenDict
  from ...models import AutoencoderKL, UNet2DConditionModel
  from ...schedulers import KarrasDiffusionSchedulers
  from ...utils import deprecate, logging
  from ...utils.torch_utils import randn_tensor
  from ..pipeline_utils import DiffusionPipeline
  from .None import StableDiffusionSafePipelineOutput
  from .safety_checker import SafeStableDiffusionSafetyChecker
label_desc: Use image node_id 10 for calling the StableDiffusionPipelineSafe obj's underlying member method code with example usage: obj.perform_safety_guidance(enable_safety_guidance, safety_momentum, noise_guidance, noise_pred_out, i, sld_guidance_scale, sld_warmup_steps, sld_threshold, sld_momentum_scale, sld_mom_beta) and returns: noise_guidance,  safety_momentum