class_docstr: This is the implementation which support T5 and other Encoder-Decoder model,
  as soon as their blocks allows the ``past_key_values`` to be injected to the model.
  This implementation modifies the huggingface's T5 forward without touching the code-base.
  However, it may fail to work when used in DataParallel model. Please use it using
  single gpu or model-parallel training.
  
  Args:
      model (:obj:`PreTrainedModel`): The pre-trained model.
      plm_config (:obj:`PretrainedConfig`): The configuration of the current pre-trained model.
      tokenizer (:obj:`PreTrainedTokenizer`): The tokenizer of the current pre-trained model.
      mapping_hook (:obj:`nn.Module`, optional):
      text (:obj:`str`, optional):
      num_token (:obj:`int`, optional):
      placeholder_mapping (:obj:`dict`):
      prefix_dropout (:obj:`float`, optional): The dropout rate for the prefix sequence.
class_docstr_tok: ["This","is","the","implementation","which","support","T5","and","other","Encoder-Decoder","model",",","as","soon","as","their","blocks","allows","the","``","past_key_values","``","to","be","injected","to","the","model",".","This","implementation","modifies","the","huggingface","'s","T5","forward","without","touching","the","code-base",".","However",",","it","may","fail","to","work","when","used","in","DataParallel","model",".","Please","use","it","using","single","gpu","or","model-parallel","training",".","Args",":","model","(",":","obj",":","`","PreTrainedModel","`",")",":","The","pre-trained","model",".","plm_config","(",":","obj",":","`","PretrainedConfig","`",")",":","The","configuration","of","the","current","pre-trained","model",".","tokenizer","(",":","obj",":","`","PreTrainedTokenizer","`",")",":","The","tokenizer","of","the","current","pre-trained","model",".","mapping_hook","(",":","obj",":","`","nn.Module","`",",","optional",")",":","text","(",":","obj",":","`","str","`",",","optional",")",":","num_token","(",":","obj",":","`","int","`",",","optional",")",":","placeholder_mapping","(",":","obj",":","`","dict","`",")",":","prefix_dropout","(",":","obj",":","`","float","`",",","optional",")",":","The","dropout","rate","for","the","prefix","sequence","."]
code: def __init__(
      self,
      model: PreTrainedModel,
      tokenizer: PreTrainedTokenizer,
      mapping_hook: Optional[nn.Module] = None,
      text: Optional[str] = None,
      num_token: Optional[int] = 5,
      placeholder_mapping: dict = {
          "<text_a>": "text_a",
          "<text_b>": "text_b",
      },
      prefix_dropout: Optional[float] = 0.0,
      mid_dim: Optional[int] = 512,
      using_encoder_past_key_values: Optional[bool] = True,
      using_decoder_past_key_values: Optional[bool] = True,
  ):
      super().__init__(
          tokenizer=tokenizer, placeholder_mapping=placeholder_mapping
      )
      raw_embedding = model.get_input_embeddings()
      self.config = model.config
      self.mapping_hook = mapping_hook
      self.embedding_size = raw_embedding.weight.shape[-1]
      self.num_token = num_token
  
      self.using_encoder_past_key_values = using_encoder_past_key_values
      self.using_decoder_past_key_values = using_decoder_past_key_values
      assert (
          self.using_encoder_past_key_values
          or self.using_decoder_past_key_values
      ), "Can't be both False."
      if (
          not self.config.is_encoder_decoder
          and not self.using_decoder_past_key_values
      ):
          logger.warning(
              "Ignore using_decoder_past_key_values=False in a decoder-only LM."
          )
  
      if isinstance(self.config, T5Config):
          self.n_layer = self.config.num_layers
          self.n_embd = self.config.d_model
          self.n_head = self.config.num_heads
          self.n_decoder_layer = self.config.num_decoder_layers
          self.match_n_decoder_layer = self.n_decoder_layer
          self.match_n_layer = self.n_layer
      elif isinstance(self.config, GPT2Config):
          self.n_decoder_layer = self.config.n_layer
          self.n_embd = self.config.n_embd
          self.n_head = self.config.n_head
          self.match_n_decoder_layer = self.n_decoder_layer
      self.mid_dim = mid_dim
      self.match_n_head = self.n_head
      self.match_n_embd = self.n_embd // self.n_head
      self.prefix_dropout = prefix_dropout
      self.dropout = nn.Dropout(self.prefix_dropout)
  
      self.default_text1 = '{"placeholder": "text_a"} {"mask"}'
      self.default_text2 = (
          '{"placeholder": "text_a"} {"placeholder": "text_b"} {"mask"}'
      )
  
      self.text = text
  
      self.generate_parameters()  # in prefix tuning the template text has no interact with the parameters.
  
      self.plm_modified = False  # flag to indicate whether the function of plm are replaced for prefix tuning.
      self.modify_plm(model)
  
imports: from functools import partial
  from transformers.configuration_utils import PretrainedConfig
  from transformers.models.gpt2.configuration_gpt2 import GPT2Config
  from transformers.models.t5.configuration_t5 import T5Config
  from transformers.models.t5.modeling_t5 import T5ForConditionalGeneration
  from transformers.models.gpt2.modeling_gpt2 import GPT2LMHeadModel
  from openprompt.data_utils import InputFeatures
  import os
  import torch
  from torch import nn
  from typing import *
  from transformers import PreTrainedModel
  from transformers.tokenization_utils import PreTrainedTokenizer
  from openprompt import Template
  from openprompt.utils.logging import logger
label_desc: Use image node_id 1 to create a new PrefixTuningTemplate object from inherited base classes: Template with example: obj = PrefixTuningTemplate(model, tokenizer, mapping_hook, text, num_token, placeholder_mapping, prefix_dropout, mid_dim, using_encoder_past_key_values, using_decoder_past_key_values)