docstr: Encodes the prompt into text encoder hidden states.
  
  Args:
       prompt (`str` or `List[str]`, *optional*):
          prompt to be encoded
      device: (`torch.device`, *optional*):
          torch device to place the resulting embeddings on
      num_images_per_prompt (`int`, *optional*, defaults to 1):
          number of images that should be generated per prompt
      do_classifier_free_guidance (`bool`, *optional*, defaults to `True`):
          whether to use classifier free guidance or not
      negative_prompt (`str` or `List[str]`, *optional*):
          The prompt or prompts not to guide the image generation. If not defined, one has to pass
          `negative_prompt_embeds`. instead. If not defined, one has to pass `negative_prompt_embeds`. instead.
          Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`).
      prompt_embeds (`torch.FloatTensor`, *optional*):
          Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
          provided, text embeddings will be generated from `prompt` input argument.
      negative_prompt_embeds (`torch.FloatTensor`, *optional*):
          Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt
          weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input
          argument.
docstr_tok: ["Encodes","the","prompt","into","text","encoder","hidden","states",".","Args",":","prompt","(","`","str","`","or","`","List","[","str","]","`",",","*","optional","*",")",":","prompt","to","be","encoded","device",":","(","`","torch.device","`",",","*","optional","*",")",":","torch","device","to","place","the","resulting","embeddings","on","num_images_per_prompt","(","`","int","`",",","*","optional","*",",","defaults","to","1",")",":","number","of","images","that","should","be","generated","per","prompt","do_classifier_free_guidance","(","`","bool","`",",","*","optional","*",",","defaults","to","`","True","`",")",":","whether","to","use","classifier","free","guidance","or","not","negative_prompt","(","`","str","`","or","`","List","[","str","]","`",",","*","optional","*",")",":","The","prompt","or","prompts","not","to","guide","the","image","generation",".","If","not","defined",",","one","has","to","pass","`","negative_prompt_embeds","`",".","instead",".","If","not","defined",",","one","has","to","pass","`","negative_prompt_embeds","`",".","instead",".","Ignored","when","not","using","guidance","(","i.e.",",","ignored","if","`","guidance_scale","`","is","less","than","`","1","`",")",".","prompt_embeds","(","`","torch.FloatTensor","`",",","*","optional","*",")",":","Pre-generated","text","embeddings",".","Can","be","used","to","easily","tweak","text","inputs",",","*","e.g",".","*","prompt","weighting",".","If","not","provided",",","text","embeddings","will","be","generated","from","`","prompt","`","input","argument",".","negative_prompt_embeds","(","`","torch.FloatTensor","`",",","*","optional","*",")",":","Pre-generated","negative","text","embeddings",".","Can","be","used","to","easily","tweak","text","inputs",",","*","e.g",".","*","prompt","weighting",".","If","not","provided",",","negative_prompt_embeds","will","be","generated","from","`","negative_prompt","`","input","argument","."]
code: def encode_prompt(
      self,
      prompt,
      do_classifier_free_guidance=True,
      num_images_per_prompt=1,
      device=None,
      negative_prompt=None,
      prompt_embeds: Optional[torch.FloatTensor] = None,
      negative_prompt_embeds: Optional[torch.FloatTensor] = None,
      clean_caption: bool = False,
  ):
      r"""
      Encodes the prompt into text encoder hidden states.
  
      Args:
           prompt (`str` or `List[str]`, *optional*):
              prompt to be encoded
          device: (`torch.device`, *optional*):
              torch device to place the resulting embeddings on
          num_images_per_prompt (`int`, *optional*, defaults to 1):
              number of images that should be generated per prompt
          do_classifier_free_guidance (`bool`, *optional*, defaults to `True`):
              whether to use classifier free guidance or not
          negative_prompt (`str` or `List[str]`, *optional*):
              The prompt or prompts not to guide the image generation. If not defined, one has to pass
              `negative_prompt_embeds`. instead. If not defined, one has to pass `negative_prompt_embeds`. instead.
              Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`).
          prompt_embeds (`torch.FloatTensor`, *optional*):
              Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
              provided, text embeddings will be generated from `prompt` input argument.
          negative_prompt_embeds (`torch.FloatTensor`, *optional*):
              Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt
              weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input
              argument.
      """
      if prompt is not None and negative_prompt is not None:
          if type(prompt) is not type(negative_prompt):
              raise TypeError(
                  f"`negative_prompt` should be the same type to `prompt`, but got {type(negative_prompt)} !="
                  f" {type(prompt)}."
              )
  
      if device is None:
          device = self._execution_device
  
      if prompt is not None and isinstance(prompt, str):
          batch_size = 1
      elif prompt is not None and isinstance(prompt, list):
          batch_size = len(prompt)
      else:
          batch_size = prompt_embeds.shape[0]
  
      # while T5 can handle much longer input sequences than 77, the text encoder was trained with a max length of 77 for IF
      max_length = 77
  
      if prompt_embeds is None:
          prompt = self._text_preprocessing(
              prompt, clean_caption=clean_caption
          )
          text_inputs = self.tokenizer(
              prompt,
              padding="max_length",
              max_length=max_length,
              truncation=True,
              add_special_tokens=True,
              return_tensors="pt",
          )
          text_input_ids = text_inputs.input_ids
          untruncated_ids = self.tokenizer(
              prompt, padding="longest", return_tensors="pt"
          ).input_ids
  
          if untruncated_ids.shape[-1] >= text_input_ids.shape[
              -1
          ] and not torch.equal(text_input_ids, untruncated_ids):
              removed_text = self.tokenizer.batch_decode(
                  untruncated_ids[:, max_length - 1 : -1]
              )
              logger.warning(
                  "The following part of your input was truncated because CLIP can only handle sequences up to"
                  f" {max_length} tokens: {removed_text}"
              )
  
          attention_mask = text_inputs.attention_mask.to(device)
  
          prompt_embeds = self.text_encoder(
              text_input_ids.to(device),
              attention_mask=attention_mask,
          )
          prompt_embeds = prompt_embeds[0]
  
      if self.text_encoder is not None:
          dtype = self.text_encoder.dtype
      elif self.unet is not None:
          dtype = self.unet.dtype
      else:
          dtype = None
  
      prompt_embeds = prompt_embeds.to(dtype=dtype, device=device)
  
      bs_embed, seq_len, _ = prompt_embeds.shape
      # duplicate text embeddings for each generation per prompt, using mps friendly method
      prompt_embeds = prompt_embeds.repeat(1, num_images_per_prompt, 1)
      prompt_embeds = prompt_embeds.view(
          bs_embed * num_images_per_prompt, seq_len, -1
      )
  
      # get unconditional embeddings for classifier free guidance
      if do_classifier_free_guidance and negative_prompt_embeds is None:
          uncond_tokens: List[str]
          if negative_prompt is None:
              uncond_tokens = [""] * batch_size
          elif isinstance(negative_prompt, str):
              uncond_tokens = [negative_prompt]
          elif batch_size != len(negative_prompt):
              raise ValueError(
                  f"`negative_prompt`: {negative_prompt} has batch size {len(negative_prompt)}, but `prompt`:"
                  f" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches"
                  " the batch size of `prompt`."
              )
          else:
              uncond_tokens = negative_prompt
  
          uncond_tokens = self._text_preprocessing(
              uncond_tokens, clean_caption=clean_caption
          )
          max_length = prompt_embeds.shape[1]
          uncond_input = self.tokenizer(
              uncond_tokens,
              padding="max_length",
              max_length=max_length,
              truncation=True,
              return_attention_mask=True,
              add_special_tokens=True,
              return_tensors="pt",
          )
          attention_mask = uncond_input.attention_mask.to(device)
  
          negative_prompt_embeds = self.text_encoder(
              uncond_input.input_ids.to(device),
              attention_mask=attention_mask,
          )
          negative_prompt_embeds = negative_prompt_embeds[0]
  
      if do_classifier_free_guidance:
          # duplicate unconditional embeddings for each generation per prompt, using mps friendly method
          seq_len = negative_prompt_embeds.shape[1]
  
          negative_prompt_embeds = negative_prompt_embeds.to(
              dtype=dtype, device=device
          )
  
          negative_prompt_embeds = negative_prompt_embeds.repeat(
              1, num_images_per_prompt, 1
          )
          negative_prompt_embeds = negative_prompt_embeds.view(
              batch_size * num_images_per_prompt, seq_len, -1
          )
  
          # For classifier free guidance, we need to do two forward passes.
          # Here we concatenate the unconditional and text embeddings into a single batch
          # to avoid doing two forward passes
      else:
          negative_prompt_embeds = None
  
      return prompt_embeds, negative_prompt_embeds
  
imports: import html
  import inspect
  import re
  import urllib.parse
  from typing import Any, Callable, Dict, List, Optional, Union
  import numpy
  import PIL.Image
  import torch
  import torch.nn.functional
  from transformers import CLIPImageProcessor, T5EncoderModel, T5Tokenizer
  from ...loaders import LoraLoaderMixin
  from ...models import UNet2DConditionModel
  from ...schedulers import DDPMScheduler
  from ...utils import BACKENDS_MAPPING, is_accelerate_available, is_bs4_available, is_ftfy_available, logging, replace_example_docstring
  from ...utils.torch_utils import randn_tensor
  from ..pipeline_utils import DiffusionPipeline
  from .pipeline_output import IFPipelineOutput
  from .safety_checker import IFSafetyChecker
  from .watermark import IFWatermarker
label_desc: Use image node_id 5 for calling the IFSuperResolutionPipeline obj's underlying member method code with example usage: obj.encode_prompt(prompt, do_classifier_free_guidance, num_images_per_prompt, device, negative_prompt, prompt_embeds, negative_prompt_embeds, clean_caption) and returns: prompt_embeds,  negative_prompt_embeds