class_docstr: The Mixed Template class defined by a string of `text`. See more examples in the `tutorial <https://github.com/thunlp/OpenPrompt/blob/ca27491101df0108a8dd753e5b1e79bf591f65d3/tutorial/1.1_mixed_template.py>`_.
  
  Args:
      model (:obj:`PreTrainedModel`): The pre-trained language model for the current prompt-learning task.
      tokenizer (:obj:`PreTrainedTokenizer`): A tokenizer to appoint the vocabulary and the tokenization strategy.
      text (:obj:`Optional[List[str]]`, optional): manual template format. Defaults to None.
class_docstr_tok: ["The","Mixed","Template","class","defined","by","a","string","of","`","text","`",".","See","more","examples","in","the","`","tutorial","<","https",":","\/\/github.com\/thunlp\/OpenPrompt\/blob\/ca27491101df0108a8dd753e5b1e79bf591f65d3\/tutorial\/1.1_mixed_template.py",">","`","_",".","Args",":","model","(",":","obj",":","`","PreTrainedModel","`",")",":","The","pre-trained","language","model","for","the","current","prompt-learning","task",".","tokenizer","(",":","obj",":","`","PreTrainedTokenizer","`",")",":","A","tokenizer","to","appoint","the","vocabulary","and","the","tokenization","strategy",".","text","(",":","obj",":","`","Optional","[","List","[","str","]","]","`",",","optional",")",":","manual","template","format",".","Defaults","to","None","."]
docstr: get the soft token indices ( soft_token_ids ) for the template
  
  ``"soft_id"`` can be used to reference the previous soft token, which means these tokens use the same embeddings.
  **Note that ``"soft_id"`` should have index start from 1 but not 0**
  
  e.g. when self.text is ``'{"soft": None} {"soft": "the", "soft_id": 1} {"soft": None} {"soft": "it", "soft_id": 3} {"soft_id": 1} {"soft": "was"} {"mask"}'``,
  output is [1, 2, 3, 4, 2, 5, 0]
docstr_tok: ["get","the","soft","token","indices","(","soft_token_ids",")","for","the","template","``","``","soft_id","''","``","can","be","used","to","reference","the","previous","soft","token",",","which","means","these","tokens","use","the","same","embeddings",".","*","*","Note","that","``","``","soft_id","''","``","should","have","index","start","from","1","but","not","0","*","*","e.g",".","when","self.text","is","``","'","{","``","soft","''",":","None","}","{","``","soft","''",":","``","the","''",",","``","soft_id","''",":","1","}","{","``","soft","''",":","None","}","{","``","soft","''",":","``","it","''",",","``","soft_id","''",":","3","}","{","``","soft_id","''",":","1","}","{","``","soft","''",":","``","was","''","}","{","``","mask","''","}","'","``",",","output","is","[","1",",","2",",","3",",","4",",","2",",","5",",","0","]"]
code: def prepare(self):
      r"""get the soft token indices ( soft_token_ids ) for the template
  
      ``"soft_id"`` can be used to reference the previous soft token, which means these tokens use the same embeddings.
      **Note that ``"soft_id"`` should have index start from 1 but not 0**
  
      e.g. when self.text is ``'{"soft": None} {"soft": "the", "soft_id": 1} {"soft": None} {"soft": "it", "soft_id": 3} {"soft_id": 1} {"soft": "was"} {"mask"}'``,
      output is [1, 2, 3, 4, 2, 5, 0]
      """
      num_soft_token = 0
      text = []
      soft_token_ids = []
      idx_mp = {}
      emb_mp = {}
      for d in self.text:
          if "soft" not in d and "soft_id" not in d:
              text.append(d)
              soft_token_ids.append(0)
              continue
  
          old_num = num_soft_token
  
          if "soft_id" in d:
              if not isinstance(d["soft_id"], int) or d["soft_id"] <= 0:
                  raise ValueError(
                      f'soft_id should be integer greater than zero, but get {d["soft_id"]}'
                  )
              if d["soft_id"] in idx_mp:
                  id_list = idx_mp[d["soft_id"]]
                  text.extend(
                      [{"soft": None} for _ in range(len(id_list))]
                  )
                  soft_token_ids.extend(id_list)
                  continue
              else:
                  if "soft" not in d:
                      d["soft"] = None
  
          if d["soft"] is None:
              if "duplicate" in d:
                  if "same" in d and d["same"]:
                      num_soft_token += 1
                      id_list = [
                          num_soft_token for _ in range(d["duplicate"])
                      ]
                  else:
                      num_soft_token += d["duplicate"]
                      id_list = list(
                          range(old_num + 1, num_soft_token + 1)
                      )
              else:
                  num_soft_token += 1
                  id_list = [num_soft_token]
              text.extend([{"soft": ""} for _ in range(len(id_list))])
          else:
              token_ids = self.tokenizer(
                  d["add_prefix_space"] + d["soft"],
                  add_special_tokens=False,
              )["input_ids"]
              surface_forms = self.tokenizer.convert_ids_to_tokens(
                  token_ids
              )
              assert len(token_ids) == len(surface_forms)
              num_soft_token += len(token_ids)
              id_list = list(range(old_num + 1, num_soft_token + 1))
              for idx, soft_id in enumerate(id_list):
                  emb_mp[soft_id] = token_ids[idx]
  
              text.extend(
                  [
                      {"soft": surface_form}
                      for surface_form in surface_forms
                  ]
              )
          soft_token_ids.extend(id_list)
  
          if "soft_id" in d:
              idx_mp[d["soft_id"]] = id_list
  
      self.num_soft_token = num_soft_token
      self.text = text
      self.soft_token_ids = soft_token_ids
  
      # Generate the embedding needed for soft tokens
  
      self.soft_embedding = nn.Embedding(
          1 + self.num_soft_token, self.embedding_size
      )
      for soft_id, token_id in emb_mp.items():
          self.soft_embedding.weight.data[soft_id, :] = (
              self.raw_embedding.weight.data[token_id, :]
              .clone()
              .detach()
              .requires_grad_(True)
          )
  
file_path: OpenPrompt/openprompt/prompts/mixed_template.py
imports: import os
  import string
  from openprompt.utils.logging import logger
  from openprompt.data_utils.utils import InputExample, InputFeatures
  from typing import *
  from transformers import PreTrainedModel
  from transformers.tokenization_utils import PreTrainedTokenizer
  from openprompt import Template
  import torch
  from torch import nn
  from sys import exit
label_desc: Use image node_id 3 for calling the MixedTemplate obj's underlying member method code with example usage: obj.prepare() without return types