code: def get_weighted_text_embeddings_sdxl(
      pipe: StableDiffusionXLPipeline,
      prompt: str = "",
      prompt_2: str = None,
      neg_prompt: str = "",
      neg_prompt_2: str = None,
  ):
      """
      This function can process long prompt with weights, no length limitation
      for Stable Diffusion XL
  
      Args:
          pipe (StableDiffusionPipeline)
          prompt (str)
          prompt_2 (str)
          neg_prompt (str)
          neg_prompt_2 (str)
      Returns:
          prompt_embeds (torch.Tensor)
          neg_prompt_embeds (torch.Tensor)
      """
      if prompt_2:
          prompt = f"{prompt} {prompt_2}"
  
      if neg_prompt_2:
          neg_prompt = f"{neg_prompt} {neg_prompt_2}"
  
      eos = pipe.tokenizer.eos_token_id
  
      # tokenizer 1
      prompt_tokens, prompt_weights = get_prompts_tokens_with_weights(
          pipe.tokenizer, prompt
      )
  
      (
          neg_prompt_tokens,
          neg_prompt_weights,
      ) = get_prompts_tokens_with_weights(pipe.tokenizer, neg_prompt)
  
      # tokenizer 2
      (
          prompt_tokens_2,
          prompt_weights_2,
      ) = get_prompts_tokens_with_weights(pipe.tokenizer_2, prompt)
  
      (
          neg_prompt_tokens_2,
          neg_prompt_weights_2,
      ) = get_prompts_tokens_with_weights(pipe.tokenizer_2, neg_prompt)
  
      # padding the shorter one for prompt set 1
      prompt_token_len = len(prompt_tokens)
      neg_prompt_token_len = len(neg_prompt_tokens)
  
      if prompt_token_len > neg_prompt_token_len:
          # padding the neg_prompt with eos token
          neg_prompt_tokens = neg_prompt_tokens + [eos] * abs(
              prompt_token_len - neg_prompt_token_len
          )
          neg_prompt_weights = neg_prompt_weights + [1.0] * abs(
              prompt_token_len - neg_prompt_token_len
          )
      else:
          # padding the prompt
          prompt_tokens = prompt_tokens + [eos] * abs(
              prompt_token_len - neg_prompt_token_len
          )
          prompt_weights = prompt_weights + [1.0] * abs(
              prompt_token_len - neg_prompt_token_len
          )
  
      # padding the shorter one for token set 2
      prompt_token_len_2 = len(prompt_tokens_2)
      neg_prompt_token_len_2 = len(neg_prompt_tokens_2)
  
      if prompt_token_len_2 > neg_prompt_token_len_2:
          # padding the neg_prompt with eos token
          neg_prompt_tokens_2 = neg_prompt_tokens_2 + [eos] * abs(
              prompt_token_len_2 - neg_prompt_token_len_2
          )
          neg_prompt_weights_2 = neg_prompt_weights_2 + [1.0] * abs(
              prompt_token_len_2 - neg_prompt_token_len_2
          )
      else:
          # padding the prompt
          prompt_tokens_2 = prompt_tokens_2 + [eos] * abs(
              prompt_token_len_2 - neg_prompt_token_len_2
          )
          prompt_weights_2 = prompt_weights + [1.0] * abs(
              prompt_token_len_2 - neg_prompt_token_len_2
          )
  
      embeds = []
      neg_embeds = []
  
      (
          prompt_token_groups,
          prompt_weight_groups,
      ) = group_tokens_and_weights(
          prompt_tokens.copy(), prompt_weights.copy()
      )
  
      (
          neg_prompt_token_groups,
          neg_prompt_weight_groups,
      ) = group_tokens_and_weights(
          neg_prompt_tokens.copy(), neg_prompt_weights.copy()
      )
  
      (
          prompt_token_groups_2,
          prompt_weight_groups_2,
      ) = group_tokens_and_weights(
          prompt_tokens_2.copy(), prompt_weights_2.copy()
      )
  
      (
          neg_prompt_token_groups_2,
          neg_prompt_weight_groups_2,
      ) = group_tokens_and_weights(
          neg_prompt_tokens_2.copy(), neg_prompt_weights_2.copy()
      )
  
      # get prompt embeddings one by one is not working.
      for i in range(len(prompt_token_groups)):
          # get positive prompt embeddings with weights
          token_tensor = torch.tensor(
              [prompt_token_groups[i]],
              dtype=torch.long,
              device=pipe.device,
          )
          weight_tensor = torch.tensor(
              prompt_weight_groups[i],
              dtype=torch.float16,
              device=pipe.device,
          )
  
          token_tensor_2 = torch.tensor(
              [prompt_token_groups_2[i]],
              dtype=torch.long,
              device=pipe.device,
          )
  
          # use first text encoder
          prompt_embeds_1 = pipe.text_encoder(
              token_tensor.to(pipe.device), output_hidden_states=True
          )
          prompt_embeds_1_hidden_states = prompt_embeds_1.hidden_states[
              -2
          ]
  
          # use second text encoder
          prompt_embeds_2 = pipe.text_encoder_2(
              token_tensor_2.to(pipe.device), output_hidden_states=True
          )
          prompt_embeds_2_hidden_states = prompt_embeds_2.hidden_states[
              -2
          ]
          pooled_prompt_embeds = prompt_embeds_2[0]
  
          prompt_embeds_list = [
              prompt_embeds_1_hidden_states,
              prompt_embeds_2_hidden_states,
          ]
          token_embedding = torch.concat(
              prompt_embeds_list, dim=-1
          ).squeeze(0)
  
          for j in range(len(weight_tensor)):
              if weight_tensor[j] != 1.0:
                  token_embedding[j] = (
                      token_embedding[-1]
                      + (token_embedding[j] - token_embedding[-1])
                      * weight_tensor[j]
                  )
  
          token_embedding = token_embedding.unsqueeze(0)
          embeds.append(token_embedding)
  
          # get negative prompt embeddings with weights
          neg_token_tensor = torch.tensor(
              [neg_prompt_token_groups[i]],
              dtype=torch.long,
              device=pipe.device,
          )
          neg_token_tensor_2 = torch.tensor(
              [neg_prompt_token_groups_2[i]],
              dtype=torch.long,
              device=pipe.device,
          )
          neg_weight_tensor = torch.tensor(
              neg_prompt_weight_groups[i],
              dtype=torch.float16,
              device=pipe.device,
          )
  
          # use first text encoder
          neg_prompt_embeds_1 = pipe.text_encoder(
              neg_token_tensor.to(pipe.device),
              output_hidden_states=True,
          )
          neg_prompt_embeds_1_hidden_states = (
              neg_prompt_embeds_1.hidden_states[-2]
          )
  
          # use second text encoder
          neg_prompt_embeds_2 = pipe.text_encoder_2(
              neg_token_tensor_2.to(pipe.device),
              output_hidden_states=True,
          )
          neg_prompt_embeds_2_hidden_states = (
              neg_prompt_embeds_2.hidden_states[-2]
          )
          negative_pooled_prompt_embeds = neg_prompt_embeds_2[0]
  
          neg_prompt_embeds_list = [
              neg_prompt_embeds_1_hidden_states,
              neg_prompt_embeds_2_hidden_states,
          ]
          neg_token_embedding = torch.concat(
              neg_prompt_embeds_list, dim=-1
          ).squeeze(0)
  
          for z in range(len(neg_weight_tensor)):
              if neg_weight_tensor[z] != 1.0:
                  neg_token_embedding[z] = (
                      neg_token_embedding[-1]
                      + (
                          neg_token_embedding[z]
                          - neg_token_embedding[-1]
                      )
                      * neg_weight_tensor[z]
                  )
  
          neg_token_embedding = neg_token_embedding.unsqueeze(0)
          neg_embeds.append(neg_token_embedding)
  
      prompt_embeds = torch.cat(embeds, dim=1)
      negative_prompt_embeds = torch.cat(neg_embeds, dim=1)
  
      return (
          prompt_embeds,
          negative_prompt_embeds,
          pooled_prompt_embeds,
          negative_pooled_prompt_embeds,
      )
  
imports: import inspect
  import os
  from typing import Any, Callable, Dict, List, Optional, Tuple, Union
  import torch
  from transformers import CLIPTextModel, CLIPTextModelWithProjection, CLIPTokenizer
  from diffusers import DiffusionPipeline, StableDiffusionXLPipeline
  from diffusers.image_processor import VaeImageProcessor
  from diffusers.loaders import FromSingleFileMixin, LoraLoaderMixin, TextualInversionLoaderMixin
  from diffusers.models import AutoencoderKL, UNet2DConditionModel
  from diffusers.models.attention_processor import AttnProcessor2_0, LoRAAttnProcessor2_0, LoRAXFormersAttnProcessor, XFormersAttnProcessor
  from diffusers.pipelines.stable_diffusion_xl import StableDiffusionXLPipelineOutput
  from diffusers.schedulers import KarrasDiffusionSchedulers
  from diffusers.utils import 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 4 for calling a global function with example usage: get_weighted_text_embeddings_sdxl(pipe, prompt, prompt_2, neg_prompt, neg_prompt_2) and returns: prompt_embeds,  negative_prompt_embeds,  pooled_prompt_embeds,  negative_pooled_prompt_embeds