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 modify_plm(self, model):
      if self.plm_modified:
          return None
      if isinstance(model, T5ForConditionalGeneration):
          if self.using_encoder_past_key_values:
              backup_encoder_forward_functions = []
              for i, layer_module in enumerate(model.encoder.block):
                  backup_encoder_forward_functions.append(
                      layer_module.layer[0].forward
                  )
  
                  def modified_encoder_forward(*args, **kwargs):
                      layer_id = kwargs.pop("layer_id")
                      batch_size = args[0].shape[0]
                      device = args[0].device
                      if kwargs["past_key_value"] is None:
                          kwargs[
                              "past_key_value"
                          ] = self.expand_to_batchsize_for_layer(
                              self.past_key_values[0],
                              batch_size,
                              layer_id,
                          ).to(
                              device
                          )
                      if kwargs["attention_mask"] is not None:
                          am = kwargs[
                              "attention_mask"
                          ]  # Should check the format of the attention_mask when moving to a new plm.
                          kwargs["attention_mask"] = torch.cat(
                              [
                                  -torch.zeros(
                                      (*am.shape[:-1], self.num_token),
                                      dtype=am.dtype,
                                      device=am.device,
                                  ),
                                  am,
                              ],
                              dim=-1,
                          )
                      return backup_encoder_forward_functions[layer_id](
                          *args, **kwargs
                      )
  
                  layer_module.layer[0].forward = partial(
                      modified_encoder_forward, layer_id=i
                  )
  
          if self.using_decoder_past_key_values:
              backup_decoder_self_attn_forward_functions = []
              for i, layer_module in enumerate(model.decoder.block):
                  backup_decoder_self_attn_forward_functions.append(
                      layer_module.layer[0].forward
                  )
  
                  def modified_decoder_self_attn_forward(
                      *args, **kwargs
                  ):
                      batch_size = args[0].shape[0]
                      layer_id = kwargs.pop("layer_id")
                      device = args[0].device
                      if kwargs["past_key_value"] is None:
                          kwargs[
                              "past_key_value"
                          ] = self.expand_to_batchsize_for_layer(
                              self.past_key_values[1],
                              batch_size,
                              layer_id,
                          ).to(
                              device
                          )
                      if kwargs["past_key_value"][0].size(-2) + args[
                          0
                      ].size(-2) == kwargs["attention_mask"].size(-1):
                          pass
                      elif (
                          kwargs["past_key_value"][0].size(-2)
                          + args[0].size(-2)
                          == kwargs["attention_mask"].size(-1)
                          + self.num_token
                      ):
                          am = kwargs["attention_mask"]
                          kwargs["attention_mask"] = torch.cat(
                              [
                                  torch.zeros(
                                      (*am.shape[:-1], self.num_token),
                                      dtype=am.dtype,
                                      device=am.device,
                                  ),
                                  am,
                              ],
                              dim=-1,
                          )
                      else:
                          raise RuntimeError(
                              "Size not match: past length: {}, inputlength:{},\
                                  attention mask length {}".format(
                                  kwargs["past_key_value"][0].size(-2),
                                  args[0].size(-2),
                                  kwargs["attention_mask"].size(-1),
                              )
                          )
  
                      return backup_decoder_self_attn_forward_functions[
                          layer_id
                      ](*args, **kwargs)
  
                  layer_module.layer[0].forward = partial(
                      modified_decoder_self_attn_forward, layer_id=i
                  )
  
      elif isinstance(model, GPT2LMHeadModel):
          pass
      else:
          raise NotImplementedError
      self.plm_modified = True
  
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 9 for calling the PrefixTuningTemplate obj's underlying member method code with example usage: obj.modify_plm(model) and returns: None, backup_encoder_forward_functions, backup_decoder_self_attn_forward_functions