code: def pad_tokens_and_weights(
      tokens,
      weights,
      max_length,
      bos,
      eos,
      pad,
      no_boseos_middle=True,
      chunk_length=77,
  ):
      r"""
      Pad the tokens (with starting and ending tokens) and weights (with 1.0) to max_length.
      """
      max_embeddings_multiples = (max_length - 2) // (chunk_length - 2)
      weights_length = (
          max_length
          if no_boseos_middle
          else max_embeddings_multiples * chunk_length
      )
      for i in range(len(tokens)):
          tokens[i] = (
              [bos]
              + tokens[i]
              + [pad] * (max_length - 1 - len(tokens[i]) - 1)
              + [eos]
          )
          if no_boseos_middle:
              weights[i] = (
                  [1.0]
                  + weights[i]
                  + [1.0] * (max_length - 1 - len(weights[i]))
              )
          else:
              w = []
              if len(weights[i]) == 0:
                  w = [1.0] * weights_length
              else:
                  for j in range(max_embeddings_multiples):
                      w.append(
                          1.0
                      )  # weight for starting token in this chunk
                      w += weights[i][
                          j
                          * (chunk_length - 2) : min(
                              len(weights[i]),
                              (j + 1) * (chunk_length - 2),
                          )
                      ]
                      w.append(
                          1.0
                      )  # weight for ending token in this chunk
                  w += [1.0] * (weights_length - len(w))
              weights[i] = w[:]
  
      return tokens, weights
  
imports: import inspect
  import re
  from typing import Any, Callable, Dict, List, Optional, Union
  import numpy
  import PIL.Image
  import torch
  from packaging import version
  from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer
  from diffusers import DiffusionPipeline
  from diffusers.configuration_utils import FrozenDict
  from diffusers.image_processor import VaeImageProcessor
  from diffusers.loaders import FromSingleFileMixin, LoraLoaderMixin, TextualInversionLoaderMixin
  from diffusers.models import AutoencoderKL, UNet2DConditionModel
  from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput, StableDiffusionSafetyChecker
  from diffusers.schedulers import KarrasDiffusionSchedulers
  from diffusers.utils import PIL_INTERPOLATION, deprecate, is_accelerate_available, is_accelerate_version, logging
  from diffusers.utils.torch_utils import randn_tensor
label_desc: Use image node_id 3 for calling a global function with example usage: pad_tokens_and_weights(tokens, weights, max_length, bos, eos, pad, no_boseos_middle, chunk_length) and returns: tokens,  weights