class_docstr: Pipeline for pixel-level image editing by following text instructions (based on Stable 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.).
  
  The pipeline also inherits the following loading methods:
      - [`~loaders.TextualInversionLoaderMixin.load_textual_inversion`] for loading textual inversion embeddings
      - [`~loaders.LoraLoaderMixin.load_lora_weights`] for loading LoRA weights
      - [`~loaders.LoraLoaderMixin.save_lora_weights`] for saving LoRA weights
  
  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","pixel-level","image","editing","by","following","text","instructions","(","based","on","Stable","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.",")",".","The","pipeline","also","inherits","the","following","loading","methods",":","-","[","`","~loaders.TextualInversionLoaderMixin.load_textual_inversion","`","]","for","loading","textual","inversion","embeddings","-","[","`","~loaders.LoraLoaderMixin.load_lora_weights","`","]","for","loading","LoRA","weights","-","[","`","~loaders.LoraLoaderMixin.save_lora_weights","`","]","for","saving","LoRA","weights","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 prepare_image_latents(
      self,
      image,
      batch_size,
      num_images_per_prompt,
      dtype,
      device,
      do_classifier_free_guidance,
      generator=None,
  ):
      if not isinstance(image, (torch.Tensor, PIL.Image.Image, list)):
          raise ValueError(
              f"`image` has to be of type `torch.Tensor`, `PIL.Image.Image` or list but is {type(image)}"
          )
  
      image = image.to(device=device, dtype=dtype)
  
      batch_size = batch_size * num_images_per_prompt
  
      if image.shape[1] == 4:
          image_latents = image
      else:
          if (
              isinstance(generator, list)
              and len(generator) != batch_size
          ):
              raise ValueError(
                  f"You have passed a list of generators of length {len(generator)}, but requested an effective batch"
                  f" size of {batch_size}. Make sure the batch size matches the length of the generators."
              )
  
          if isinstance(generator, list):
              image_latents = [
                  self.vae.encode(image[i : i + 1]).latent_dist.mode()
                  for i in range(batch_size)
              ]
              image_latents = torch.cat(image_latents, dim=0)
          else:
              image_latents = self.vae.encode(image).latent_dist.mode()
  
      if (
          batch_size > image_latents.shape[0]
          and batch_size % image_latents.shape[0] == 0
      ):
          # expand image_latents for batch_size
          deprecation_message = (
              f"You have passed {batch_size} text prompts (`prompt`), but only {image_latents.shape[0]} initial"
              " images (`image`). Initial images are now duplicating to match the number of text prompts. Note"
              " that this behavior is deprecated and will be removed in a version 1.0.0. Please make sure to update"
              " your script to pass as many initial images as text prompts to suppress this warning."
          )
          deprecate(
              "len(prompt) != len(image)",
              "1.0.0",
              deprecation_message,
              standard_warn=False,
          )
          additional_image_per_prompt = (
              batch_size // image_latents.shape[0]
          )
          image_latents = torch.cat(
              [image_latents] * additional_image_per_prompt, dim=0
          )
      elif (
          batch_size > image_latents.shape[0]
          and batch_size % image_latents.shape[0] != 0
      ):
          raise ValueError(
              f"Cannot duplicate `image` of batch size {image_latents.shape[0]} to {batch_size} text prompts."
          )
      else:
          image_latents = torch.cat([image_latents], dim=0)
  
      if do_classifier_free_guidance:
          uncond_image_latents = torch.zeros_like(image_latents)
          image_latents = torch.cat(
              [image_latents, image_latents, uncond_image_latents],
              dim=0,
          )
  
      return image_latents
  
imports: import inspect
  from typing import Callable, List, Optional, Union
  import numpy
  import PIL.Image
  import torch
  from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer
  from ...image_processor import PipelineImageInput, VaeImageProcessor
  from ...loaders import LoraLoaderMixin, TextualInversionLoaderMixin
  from ...models import AutoencoderKL, UNet2DConditionModel
  from ...schedulers import KarrasDiffusionSchedulers
  from ...utils import PIL_INTERPOLATION, deprecate, logging
  from ...utils.torch_utils import randn_tensor
  from ..pipeline_utils import DiffusionPipeline
  from .None import StableDiffusionPipelineOutput
  from .safety_checker import StableDiffusionSafetyChecker
label_desc: Use image node_id 9 for calling the StableDiffusionInstructPix2PixPipeline obj's underlying member method code with example usage: obj.prepare_image_latents(image, batch_size, num_images_per_prompt, dtype, device, do_classifier_free_guidance, generator) and returns: image_latents