class_docstr: Pipeline for text-to-image generation using Stable Diffusion XL.
  
  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.FromSingleFileMixin.from_single_file`] for loading `.ckpt` files
      - [`~loaders.IPAdapterMixin.load_ip_adapter`] for loading IP Adapters
      - [`~loaders.LoraLoaderMixin.load_lora_weights`] for loading LoRA weights
      - [`~loaders.LoraLoaderMixin.save_lora_weights`] for saving LoRA weights
      - [`~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 ([`CLIPTextModel`]):
          Frozen text-encoder. Stable Diffusion XL uses the text portion of
          [CLIP](https://huggingface.co/docs/transformers/model_doc/clip#transformers.CLIPTextModel), specifically
          the [clip-vit-large-patch14](https://huggingface.co/openai/clip-vit-large-patch14) variant.
      text_encoder_2 ([` CLIPTextModelWithProjection`]):
          Second frozen text-encoder. Stable Diffusion XL uses the text and pool portion of
          [CLIP](https://huggingface.co/docs/transformers/model_doc/clip#transformers.CLIPTextModelWithProjection),
          specifically the
          [laion/CLIP-ViT-bigG-14-laion2B-39B-b160k](https://huggingface.co/laion/CLIP-ViT-bigG-14-laion2B-39B-b160k)
          variant.
      tokenizer (`CLIPTokenizer`):
          Tokenizer of class
          [CLIPTokenizer](https://huggingface.co/docs/transformers/v4.21.0/en/model_doc/clip#transformers.CLIPTokenizer).
      tokenizer_2 (`CLIPTokenizer`):
          Second Tokenizer of class
          [CLIPTokenizer](https://huggingface.co/docs/transformers/v4.21.0/en/model_doc/clip#transformers.CLIPTokenizer).
      unet ([`UNet2DConditionModel`]):
          Conditional U-Net architecture 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`].
      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","XL",".","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.FromSingleFileMixin.from_single_file","`","]","for","loading","`",".ckpt","`","files","-","[","`","~loaders.IPAdapterMixin.load_ip_adapter","`","]","for","loading","IP","Adapters","-","[","`","~loaders.LoraLoaderMixin.load_lora_weights","`","]","for","loading","LoRA","weights","-","[","`","~loaders.LoraLoaderMixin.save_lora_weights","`","]","for","saving","LoRA","weights","-","[","`","~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","(","[","`","CLIPTextModel","`","]",")",":","Frozen","text-encoder",".","Stable","Diffusion","XL","uses","the","text","portion","of","[","CLIP","]","(","https",":","\/\/huggingface.co\/docs\/transformers\/model_doc\/clip","#","transformers.CLIPTextModel",")",",","specifically","the","[","clip-vit-large-patch14","]","(","https",":","\/\/huggingface.co\/openai\/clip-vit-large-patch14",")","variant",".","text_encoder_2","(","[","`","CLIPTextModelWithProjection","`","]",")",":","Second","frozen","text-encoder",".","Stable","Diffusion","XL","uses","the","text","and","pool","portion","of","[","CLIP","]","(","https",":","\/\/huggingface.co\/docs\/transformers\/model_doc\/clip","#","transformers.CLIPTextModelWithProjection",")",",","specifically","the","[","laion\/CLIP-ViT-bigG-14-laion2B-39B-b160k","]","(","https",":","\/\/huggingface.co\/laion\/CLIP-ViT-bigG-14-laion2B-39B-b160k",")","variant",".","tokenizer","(","`","CLIPTokenizer","`",")",":","Tokenizer","of","class","[","CLIPTokenizer","]","(","https",":","\/\/huggingface.co\/docs\/transformers\/v4.21.0\/en\/model_doc\/clip","#","transformers.CLIPTokenizer",")",".","tokenizer_2","(","`","CLIPTokenizer","`",")",":","Second","Tokenizer","of","class","[","CLIPTokenizer","]","(","https",":","\/\/huggingface.co\/docs\/transformers\/v4.21.0\/en\/model_doc\/clip","#","transformers.CLIPTokenizer",")",".","unet","(","[","`","UNet2DConditionModel","`","]",")",":","Conditional","U-Net","architecture","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","`","]",".","feature_extractor","(","[","`","~transformers.CLIPImageProcessor","`","]",")",":","A","`","CLIPImageProcessor","`","to","extract","features","from","generated","images",";","used","as","inputs","to","the","`","safety_checker","`","."]
code: def prepare_latents(
      self,
      image,
      mask,
      width,
      height,
      num_channels_latents,
      timestep,
      batch_size,
      num_images_per_prompt,
      dtype,
      device,
      generator=None,
      add_noise=True,
      latents=None,
      is_strength_max=True,
      return_noise=False,
      return_image_latents=False,
  ):
      batch_size *= num_images_per_prompt
  
      if image is None:
          shape = (
              batch_size,
              num_channels_latents,
              height // self.vae_scale_factor,
              width // self.vae_scale_factor,
          )
          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 latents is None:
              latents = randn_tensor(
                  shape, generator=generator, device=device, dtype=dtype
              )
          else:
              latents = latents.to(device)
  
          # scale the initial noise by the standard deviation required by the scheduler
          latents = latents * self.scheduler.init_noise_sigma
          return latents
  
      elif mask is None:
          if not isinstance(image, (torch.Tensor, Image.Image, list)):
              raise ValueError(
                  f"`image` has to be of type `torch.Tensor`, `PIL.Image.Image` or list but is {type(image)}"
              )
  
          # Offload text encoder if `enable_model_cpu_offload` was enabled
          if (
              hasattr(self, "final_offload_hook")
              and self.final_offload_hook is not None
          ):
              self.text_encoder_2.to("cpu")
              torch.cuda.empty_cache()
  
          image = image.to(device=device, dtype=dtype)
  
          if image.shape[1] == 4:
              init_latents = image
  
          else:
              # make sure the VAE is in float32 mode, as it overflows in float16
              if self.vae.config.force_upcast:
                  image = image.float()
                  self.vae.to(dtype=torch.float32)
  
              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."
                  )
  
              elif isinstance(generator, list):
                  init_latents = [
                      retrieve_latents(
                          self.vae.encode(image[i : i + 1]),
                          generator=generator[i],
                      )
                      for i in range(batch_size)
                  ]
                  init_latents = torch.cat(init_latents, dim=0)
              else:
                  init_latents = retrieve_latents(
                      self.vae.encode(image), generator=generator
                  )
  
              if self.vae.config.force_upcast:
                  self.vae.to(dtype)
  
              init_latents = init_latents.to(dtype)
              init_latents = (
                  self.vae.config.scaling_factor * init_latents
              )
  
          if (
              batch_size > init_latents.shape[0]
              and batch_size % init_latents.shape[0] == 0
          ):
              # expand init_latents for batch_size
              additional_image_per_prompt = (
                  batch_size // init_latents.shape[0]
              )
              init_latents = torch.cat(
                  [init_latents] * additional_image_per_prompt, dim=0
              )
          elif (
              batch_size > init_latents.shape[0]
              and batch_size % init_latents.shape[0] != 0
          ):
              raise ValueError(
                  f"Cannot duplicate `image` of batch size {init_latents.shape[0]} to {batch_size} text prompts."
              )
          else:
              init_latents = torch.cat([init_latents], dim=0)
  
          if add_noise:
              shape = init_latents.shape
              noise = randn_tensor(
                  shape, generator=generator, device=device, dtype=dtype
              )
              # get latents
              init_latents = self.scheduler.add_noise(
                  init_latents, noise, timestep
              )
  
          latents = init_latents
          return latents
  
      else:
          shape = (
              batch_size,
              num_channels_latents,
              height // self.vae_scale_factor,
              width // self.vae_scale_factor,
          )
          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 (
              image is None or timestep is None
          ) and not is_strength_max:
              raise ValueError(
                  "Since strength < 1. initial latents are to be initialised as a combination of Image + Noise."
                  "However, either the image or the noise timestep has not been provided."
              )
  
          if image.shape[1] == 4:
              image_latents = image.to(device=device, dtype=dtype)
              image_latents = image_latents.repeat(
                  batch_size // image_latents.shape[0], 1, 1, 1
              )
          elif return_image_latents or (
              latents is None and not is_strength_max
          ):
              image = image.to(device=device, dtype=dtype)
              image_latents = self._encode_vae_image(
                  image=image, generator=generator
              )
              image_latents = image_latents.repeat(
                  batch_size // image_latents.shape[0], 1, 1, 1
              )
  
          if latents is None and add_noise:
              noise = randn_tensor(
                  shape, generator=generator, device=device, dtype=dtype
              )
              # if strength is 1. then initialise the latents to noise, else initial to image + noise
              latents = (
                  noise
                  if is_strength_max
                  else self.scheduler.add_noise(
                      image_latents, noise, timestep
                  )
              )
              # if pure noise then scale the initial latents by the  Scheduler's init sigma
              latents = (
                  latents * self.scheduler.init_noise_sigma
                  if is_strength_max
                  else latents
              )
          elif add_noise:
              noise = latents.to(device)
              latents = noise * self.scheduler.init_noise_sigma
          else:
              noise = randn_tensor(
                  shape, generator=generator, device=device, dtype=dtype
              )
              latents = image_latents.to(device)
  
          outputs = (latents,)
  
          if return_noise:
              outputs += (noise,)
  
          if return_image_latents:
              outputs += (image_latents,)
  
          return outputs
  
imports: import inspect
  import os
  from typing import Any, Callable, Dict, List, Optional, Tuple, Union
  import torch
  from PIL import Image
  from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTextModelWithProjection, CLIPTokenizer, CLIPVisionModelWithProjection
  from diffusers import DiffusionPipeline, StableDiffusionXLPipeline
  from diffusers.image_processor import PipelineImageInput, VaeImageProcessor
  from diffusers.loaders import FromSingleFileMixin, IPAdapterMixin, LoraLoaderMixin, TextualInversionLoaderMixin
  from diffusers.models import AutoencoderKL, ImageProjection, UNet2DConditionModel
  from diffusers.models.attention_processor import AttnProcessor2_0, FusedAttnProcessor2_0, LoRAAttnProcessor2_0, LoRAXFormersAttnProcessor, XFormersAttnProcessor
  from diffusers.pipelines.stable_diffusion_xl.pipeline_output import StableDiffusionXLPipelineOutput
  from diffusers.schedulers import KarrasDiffusionSchedulers
  from diffusers.utils import deprecate, is_accelerate_available, is_accelerate_version, is_invisible_watermark_available, logging, replace_example_docstring
  from diffusers.utils.torch_utils import randn_tensor
label_desc: Use image node_id 16 for calling the SDXLLongPromptWeightingPipeline obj's underlying member method code with example usage: obj.prepare_latents(image, mask, width, height, num_channels_latents, timestep, batch_size, num_images_per_prompt, dtype, device, generator, add_noise, latents, is_strength_max, return_noise, return_image_latents) and returns: latents, latents, outputs