class_docstr: Pipeline for generating image prior for Kandinsky
  
  This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods the
  library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.)
  
  Args:
      prior ([`PriorTransformer`]):
          The canonincal unCLIP prior to approximate the image embedding from the text embedding.
      image_encoder ([`CLIPVisionModelWithProjection`]):
          Frozen image-encoder.
      text_encoder ([`CLIPTextModelWithProjection`]):
          Frozen text-encoder.
      tokenizer (`CLIPTokenizer`):
          Tokenizer of class
          [CLIPTokenizer](https://huggingface.co/docs/transformers/v4.21.0/en/model_doc/clip#transformers.CLIPTokenizer).
      scheduler ([`UnCLIPScheduler`]):
          A scheduler to be used in combination with `prior` to generate image embedding.
class_docstr_tok: ["Pipeline","for","generating","image","prior","for","Kandinsky","This","model","inherits","from","[","`","DiffusionPipeline","`","]",".","Check","the","superclass","documentation","for","the","generic","methods","the","library","implements","for","all","the","pipelines","(","such","as","downloading","or","saving",",","running","on","a","particular","device",",","etc",".",")","Args",":","prior","(","[","`","PriorTransformer","`","]",")",":","The","canonincal","unCLIP","prior","to","approximate","the","image","embedding","from","the","text","embedding",".","image_encoder","(","[","`","CLIPVisionModelWithProjection","`","]",")",":","Frozen","image-encoder",".","text_encoder","(","[","`","CLIPTextModelWithProjection","`","]",")",":","Frozen","text-encoder",".","tokenizer","(","`","CLIPTokenizer","`",")",":","Tokenizer","of","class","[","CLIPTokenizer","]","(","https",":","\/\/huggingface.co\/docs\/transformers\/v4.21.0\/en\/model_doc\/clip","#","transformers.CLIPTokenizer",")",".","scheduler","(","[","`","UnCLIPScheduler","`","]",")",":","A","scheduler","to","be","used","in","combination","with","`","prior","`","to","generate","image","embedding","."]
code: def _encode_prompt(
      self,
      prompt,
      device,
      num_images_per_prompt,
      do_classifier_free_guidance,
      negative_prompt=None,
  ):
      batch_size = len(prompt) if isinstance(prompt, list) else 1
      # get prompt text embeddings
      text_inputs = self.tokenizer(
          prompt,
          padding="max_length",
          max_length=self.tokenizer.model_max_length,
          truncation=True,
          return_tensors="pt",
      )
      text_input_ids = text_inputs.input_ids
      text_mask = text_inputs.attention_mask.bool().to(device)
  
      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[
                  :, self.tokenizer.model_max_length - 1 : -1
              ]
          )
          logger.warning(
              "The following part of your input was truncated because CLIP can only handle sequences up to"
              f" {self.tokenizer.model_max_length} tokens: {removed_text}"
          )
          text_input_ids = text_input_ids[
              :, : self.tokenizer.model_max_length
          ]
  
      text_encoder_output = self.text_encoder(text_input_ids.to(device))
  
      prompt_embeds = text_encoder_output.text_embeds
      text_encoder_hidden_states = text_encoder_output.last_hidden_state
  
      prompt_embeds = prompt_embeds.repeat_interleave(
          num_images_per_prompt, dim=0
      )
      text_encoder_hidden_states = (
          text_encoder_hidden_states.repeat_interleave(
              num_images_per_prompt, dim=0
          )
      )
      text_mask = text_mask.repeat_interleave(
          num_images_per_prompt, dim=0
      )
  
      if do_classifier_free_guidance:
          uncond_tokens: List[str]
          if negative_prompt is None:
              uncond_tokens = [""] * batch_size
          elif 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)}."
              )
          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_input = self.tokenizer(
              uncond_tokens,
              padding="max_length",
              max_length=self.tokenizer.model_max_length,
              truncation=True,
              return_tensors="pt",
          )
          uncond_text_mask = uncond_input.attention_mask.bool().to(
              device
          )
          negative_prompt_embeds_text_encoder_output = (
              self.text_encoder(uncond_input.input_ids.to(device))
          )
  
          negative_prompt_embeds = (
              negative_prompt_embeds_text_encoder_output.text_embeds
          )
          uncond_text_encoder_hidden_states = (
              negative_prompt_embeds_text_encoder_output.last_hidden_state
          )
  
          # 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.repeat(
              1, num_images_per_prompt
          )
          negative_prompt_embeds = negative_prompt_embeds.view(
              batch_size * num_images_per_prompt, seq_len
          )
  
          seq_len = uncond_text_encoder_hidden_states.shape[1]
          uncond_text_encoder_hidden_states = (
              uncond_text_encoder_hidden_states.repeat(
                  1, num_images_per_prompt, 1
              )
          )
          uncond_text_encoder_hidden_states = (
              uncond_text_encoder_hidden_states.view(
                  batch_size * num_images_per_prompt, seq_len, -1
              )
          )
          uncond_text_mask = uncond_text_mask.repeat_interleave(
              num_images_per_prompt, dim=0
          )
  
          # done duplicates
  
          # 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
          prompt_embeds = torch.cat(
              [negative_prompt_embeds, prompt_embeds]
          )
          text_encoder_hidden_states = torch.cat(
              [
                  uncond_text_encoder_hidden_states,
                  text_encoder_hidden_states,
              ]
          )
  
          text_mask = torch.cat([uncond_text_mask, text_mask])
  
      return prompt_embeds, text_encoder_hidden_states, text_mask
  
file_path: llmga/llmga/diffusers/src/diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_prior_emb2emb.py
imports: from typing import List, Optional, Union
  import PIL.Image
  import torch
  from transformers import CLIPImageProcessor, CLIPTextModelWithProjection, CLIPTokenizer, CLIPVisionModelWithProjection
  from ...models import PriorTransformer
  from ...schedulers import UnCLIPScheduler
  from ...utils import logging, replace_example_docstring
  from ...utils.torch_utils import randn_tensor
  from ..kandinsky import KandinskyPriorPipelineOutput
  from ..pipeline_utils import DiffusionPipeline
label_desc: Use image node_id 7 for calling the KandinskyV22PriorEmb2EmbPipeline obj's underlying member method code with example usage: obj._encode_prompt(prompt, device, num_images_per_prompt, do_classifier_free_guidance, negative_prompt) and returns: prompt_embeds,  text_encoder_hidden_states,  text_mask