class_docstr: ``PromptModel`` with generation loss calculation and generation utils integrated.
  
  
  Args:
      plm (:obj:`PretrainedModel`): A pre-traiend model you decide to use for generation, e.g. GPT.
      template (:obj:`Template`): A ``Template`` object you use to wrap the input text for classification, e.g. ``PrefixTuningTemplate``.
      tokenizer (:obj:`Tokenizer`): A ``Tokenizer`` of the current model.
      gen_config (:obj:`CfgNode`): The generation configs to pass into `GenerationMixin.generate <https://huggingface.co/transformers/_modules/transformers/generation_utils.html#GenerationMixin.generate>`_
      freeze_plm (:obj:`bool`): whether or not to freeze the pretrained language model
      plm_eval_mode (:obj:`bool`): this is a stronger freezing mode than freeze_plm, i.e. the dropout of the model is turned off. No matter whether the other part is set to train.
class_docstr_tok: ["``","PromptModel","``","with","generation","loss","calculation","and","generation","utils","integrated",".","Args",":","plm","(",":","obj",":","`","PretrainedModel","`",")",":","A","pre-traiend","model","you","decide","to","use","for","generation",",","e.g",".","GPT",".","template","(",":","obj",":","`","Template","`",")",":","A","``","Template","``","object","you","use","to","wrap","the","input","text","for","classification",",","e.g",".","``","PrefixTuningTemplate","``",".","tokenizer","(",":","obj",":","`","Tokenizer","`",")",":","A","``","Tokenizer","``","of","the","current","model",".","gen_config","(",":","obj",":","`","CfgNode","`",")",":","The","generation","configs","to","pass","into","`","GenerationMixin.generate","<","https",":","\/\/huggingface.co\/transformers\/_modules\/transformers\/generation_utils.html","#","GenerationMixin.generate",">","`","_","freeze_plm","(",":","obj",":","`","bool","`",")",":","whether","or","not","to","freeze","the","pretrained","language","model","plm_eval_mode","(",":","obj",":","`","bool","`",")",":","this","is","a","stronger","freezing","mode","than","freeze_plm",",","i.e",".","the","dropout","of","the","model","is","turned","off",".","No","matter","whether","the","other","part","is","set","to","train","."]
docstr: This function wraps the generate() methods in parent class ``GenerationMixin``.
  Forward uses the ``PretrainedModel``'s forward method.
  generation_kwargs include all the parameters that are passed in to
  ``transformers.generation_util.GenerationMixin.generate``
  
  Args:
      batch (:obj:`Union[Dict, InputFeatures]`): The input features of batchified data sequences.
      verbose (:obj:`Optional[bool]`): Set to true to verbose the generated sentence.
  
  Returns:
      output_sequences (:obj:`List[torch.Tensor]`): The raw sequences generated by the generation model.
      generated_sentences (:obj:`List[torch.Tensor]`): The generated sentences that have been post-processed.
docstr_tok: ["This","function","wraps","the","generate","(",")","methods","in","parent","class","``","GenerationMixin","``",".","Forward","uses","the","``","PretrainedModel","``","'s","forward","method",".","generation_kwargs","include","all","the","parameters","that","are","passed","in","to","``","transformers.generation_util.GenerationMixin.generate","``","Args",":","batch","(",":","obj",":","`","Union","[","Dict",",","InputFeatures","]","`",")",":","The","input","features","of","batchified","data","sequences",".","verbose","(",":","obj",":","`","Optional","[","bool","]","`",")",":","Set","to","true","to","verbose","the","generated","sentence",".","Returns",":","output_sequences","(",":","obj",":","`","List","[","torch.Tensor","]","`",")",":","The","raw","sequences","generated","by","the","generation","model",".","generated_sentences","(",":","obj",":","`","List","[","torch.Tensor","]","`",")",":","The","generated","sentences","that","have","been","post-processed","."]
code: def generate(
      self,
      batch: Union[Dict, InputFeatures],
      verbose: Optional[bool] = False,
      **generation_kwargs,
  ):
      r"""This function wraps the generate() methods in parent class ``GenerationMixin``.
      Forward uses the ``PretrainedModel``'s forward method.
      generation_kwargs include all the parameters that are passed in to
      ``transformers.generation_util.GenerationMixin.generate``
  
      Args:
          batch (:obj:`Union[Dict, InputFeatures]`): The input features of batchified data sequences.
          verbose (:obj:`Optional[bool]`): Set to true to verbose the generated sentence.
  
      Returns:
          output_sequences (:obj:`List[torch.Tensor]`): The raw sequences generated by the generation model.
          generated_sentences (:obj:`List[torch.Tensor]`): The generated sentences that have been post-processed.
      """
      input_generation_kwargs = generation_kwargs
      if self.config.is_encoder_decoder:
          loss_ids_start = batch["loss_ids"].argmax(dim=-1)
          assert (
              loss_ids_start.min() == loss_ids_start.max()
          ), "The generation start from different position in a batch."
          batch["decoder_input_ids"] = batch["decoder_input_ids"][
              :, : loss_ids_start.min() + 1
          ]
          input_length = batch["decoder_input_ids"].size(1)
          batch_size = batch["decoder_input_ids"].size(0)
  
          self.generate_ith_token = 0
          self.in_generation_function = True
          output_sequences = super().generate(
              **batch,
              **input_generation_kwargs,
              pad_token_id=self.tokenizer.pad_token_id,
              eos_token_id=self.tokenizer.eos_token_id,
          )
          self.in_generation_function = False
          output_sequences = output_sequences.cpu().tolist()
          generated_sentences = self.post_processing(
              output_sequences=output_sequences,
              input_lengths=input_length,
          )
      else:
          input_length = batch["input_ids"].size(1)
          batch_size = batch["input_ids"].size(0)
  
          # Currently huggingface transformers only support single sample generation, or padding to the left (instead of the right).
          # because it will only extract the last position of the output
          # generate one_by_one
          if "input_ids_len" in batch:
              input_real_lens = batch["input_ids_len"]
          else:
              input_real_lens = torch.sum(
                  (
                      batch["input_ids"] != self.tokenizer.pad_token_id
                  ).to(torch.int),
                  dim=-1,
              )
          output_sequences = []
          for instance_id in range(batch_size):
              # remove the pad token
              instance = {
                  key: batch[key][instance_id : instance_id + 1][
                      :, : input_real_lens[instance_id]
                  ]
                  for key in batch
                  if isinstance(batch[key], torch.Tensor)
                  and batch[key].shape[:2]
                  == torch.Size([batch_size, input_length])
              }
              self.generate_ith_token = 0
              self.in_generation_function = True
              output_sequence = super().generate(
                  **instance,
                  **input_generation_kwargs,
                  pad_token_id=self.tokenizer.pad_token_id,
                  eos_token_id=self.tokenizer.eos_token_id,
              )
              self.in_generation_function = False
              output_sequences.extend(
                  output_sequence.cpu().tolist()
              )  # TODO: to support generate multiple sentence
          generated_sentences = self.post_processing(
              output_sequences=output_sequences,
              input_lengths=input_real_lens.cpu().tolist(),
          )
      if verbose:
          logger.info(f"Generated:{generated_sentences}")
      return output_sequences, generated_sentences
  
imports: from pickle import FALSE
  from torch.utils.data.sampler import RandomSampler
  from transformers.configuration_utils import PretrainedConfig
  from transformers.generation_utils import GenerationMixin
  import torch
  import torch.nn
  from torch.utils.data import Dataset
  from typing import *
  from openprompt.data_utils import InputExample, InputFeatures
  from torch.utils.data._utils.collate import default_collate
  from tqdm.std import tqdm
  from transformers.tokenization_utils import PreTrainedTokenizer
  from transformers.utils.dummy_pt_objects import PreTrainedModel
  from openprompt.plms.utils import TokenizerWrapper
  from openprompt.prompt_base import Template, Verbalizer
  from collections import defaultdict
  from openprompt.utils import round_list, signature
  import numpy
  from torch.utils.data import DataLoader
  from yacs.config import CfgNode
  from openprompt.utils.logging import logger
  from transformers import AdamW, get_linear_schedule_with_warmup
label_desc: Use image node_id 11 for calling the PromptForGeneration obj's underlying member method code with example usage: obj.generate(batch, verbose) and returns: output_sequences,  generated_sentences