class_docstr: Pipeline for text-to-image generation using Stable Diffusion and Attend-and-Excite.
  
  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.).
  
  The pipeline also inherits the following loading methods:
      - [`~loaders.TextualInversionLoaderMixin.load_textual_inversion`] for loading textual inversion embeddings
  
  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","for","text-to-image","generation","using","Stable","Diffusion","and","Attend-and-Excite",".","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.",")",".","The","pipeline","also","inherits","the","following","loading","methods",":","-","[","`","~loaders.TextualInversionLoaderMixin.load_textual_inversion","`","]","for","loading","textual","inversion","embeddings","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","`","."]
docstr: Performs the iterative latent refinement introduced in the paper. Here, we continuously update the latent code
  according to our loss objective until the given threshold is reached for all tokens.
code: def _perform_iterative_refinement_step(
      self,
      latents: torch.Tensor,
      indices: List[int],
      loss: torch.Tensor,
      threshold: float,
      text_embeddings: torch.Tensor,
      step_size: float,
      t: int,
      max_refinement_steps: int = 20,
  ):
      """
      Performs the iterative latent refinement introduced in the paper. Here, we continuously update the latent code
      according to our loss objective until the given threshold is reached for all tokens.
      """
      iteration = 0
      target_loss = max(0, 1.0 - threshold)
      while loss > target_loss:
          iteration += 1
  
          latents = latents.clone().detach().requires_grad_(True)
          self.unet(
              latents, t, encoder_hidden_states=text_embeddings
          ).sample
          self.unet.zero_grad()
  
          # Get max activation value for each subject token
          max_attention_per_index = (
              self._aggregate_and_get_max_attention_per_token(
                  indices=indices,
              )
          )
  
          loss = self._compute_loss(max_attention_per_index)
  
          if loss != 0:
              latents = self._update_latent(latents, loss, step_size)
  
          logger.info(f"\t Try {iteration}. loss: {loss}")
  
          if iteration >= max_refinement_steps:
              logger.info(
                  f"\t Exceeded max number of iterations ({max_refinement_steps})! "
              )
              break
  
      # Run one more time but don't compute gradients and update the latents.
      # We just need to compute the new loss - the grad update will occur below
      latents = latents.clone().detach().requires_grad_(True)
      _ = self.unet(
          latents, t, encoder_hidden_states=text_embeddings
      ).sample
      self.unet.zero_grad()
  
      # Get max activation value for each subject token
      max_attention_per_index = (
          self._aggregate_and_get_max_attention_per_token(
              indices=indices,
          )
      )
      loss = self._compute_loss(max_attention_per_index)
      logger.info(f"\t Finished with loss of: {loss}")
      return loss, latents, max_attention_per_index
  
imports: import inspect
  import math
  from typing import Any, Callable, Dict, List, Optional, Tuple, Union
  import numpy
  import torch
  from torch.nn import functional
  from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer
  from ...image_processor import VaeImageProcessor
  from ...loaders import LoraLoaderMixin, TextualInversionLoaderMixin
  from ...models import AutoencoderKL, UNet2DConditionModel
  from ...models.attention_processor import Attention
  from ...models.lora import adjust_lora_scale_text_encoder
  from ...schedulers import KarrasDiffusionSchedulers
  from ...utils import USE_PEFT_BACKEND, deprecate, logging, replace_example_docstring, scale_lora_layers, unscale_lora_layers
  from ...utils.torch_utils import randn_tensor
  from ..pipeline_utils import DiffusionPipeline
  from ..stable_diffusion import StableDiffusionPipelineOutput
  from ..stable_diffusion.safety_checker import StableDiffusionSafetyChecker
label_desc: Use image node_id 15 for calling the StableDiffusionAttendAndExcitePipeline obj's underlying member method code with example usage: obj._perform_iterative_refinement_step(latents, indices, loss, threshold, text_embeddings, step_size, t, max_refinement_steps) and returns: loss,  latents,  max_attention_per_index