class_docstr: This is the automatic template search implementation for `LM-BFF <https://arxiv.org/pdf/2012.15723.pdf>`_. It uses a generation model to generate multi-part text to fill in the template. By jointly considering all samples in the dataset, it uses beam search decoding method to generate a designated number of templates with the highest probability. The generated template may be uniformly used for all samples in the dataset.
  
  Args:
      model (:obj:`PretrainedModel`): A pretrained model for generation.
      tokenizer (:obj:`PretrainedTokenizer`): A corresponding type tokenizer.
      tokenizer_wrapper (:obj:`TokenizerWrapper`): A corresponding type tokenizer wrapper class.
      max_length (:obj:`Optional[int]`): The maximum length of total generated template. Defaults to 20.
      target_number (:obj:`Optional[int]`): The number of separate parts to generate, e.g. in T5, every <extra_id_{}> token stands for one part. Defaults to 2.
      beam_width (:obj:`Optional[int]`): The beam search width.  Defaults to 100.
      length_limit (:obj:`Optional[List[int]]`): The length limit for each part of content, if None, there is no limit. If not None, the list should have a length equal to `target_number`. Defaults to None.
      forbidden_word_ids (:obj:`Optional[List[int]]`): Any tokenizer-specific token_id you want to prevent from generating. Defaults to `[]`, i.e. all tokens in the vocabulary are allowed in the generated template.
class_docstr_tok: ["This","is","the","automatic","template","search","implementation","for","`","LM-BFF","<","https",":","\/\/arxiv.org\/pdf\/2012.15723.pdf",">","`","_",".","It","uses","a","generation","model","to","generate","multi-part","text","to","fill","in","the","template",".","By","jointly","considering","all","samples","in","the","dataset",",","it","uses","beam","search","decoding","method","to","generate","a","designated","number","of","templates","with","the","highest","probability",".","The","generated","template","may","be","uniformly","used","for","all","samples","in","the","dataset",".","Args",":","model","(",":","obj",":","`","PretrainedModel","`",")",":","A","pretrained","model","for","generation",".","tokenizer","(",":","obj",":","`","PretrainedTokenizer","`",")",":","A","corresponding","type","tokenizer",".","tokenizer_wrapper","(",":","obj",":","`","TokenizerWrapper","`",")",":","A","corresponding","type","tokenizer","wrapper","class",".","max_length","(",":","obj",":","`","Optional","[","int","]","`",")",":","The","maximum","length","of","total","generated","template",".","Defaults","to","20.","target_number","(",":","obj",":","`","Optional","[","int","]","`",")",":","The","number","of","separate","parts","to","generate",",","e.g",".","in","T5",",","every","<","extra_id_","{","}",">","token","stands","for","one","part",".","Defaults","to","2.","beam_width","(",":","obj",":","`","Optional","[","int","]","`",")",":","The","beam","search","width",".","Defaults","to","100.","length_limit","(",":","obj",":","`","Optional","[","List","[","int","]","]","`",")",":","The","length","limit","for","each","part","of","content",",","if","None",",","there","is","no","limit",".","If","not","None",",","the","list","should","have","a","length","equal","to","`","target_number","`",".","Defaults","to","None",".","forbidden_word_ids","(",":","obj",":","`","Optional","[","List","[","int","]","]","`",")",":","Any","tokenizer-specific","token_id","you","want","to","prevent","from","generating",".","Defaults","to","`","[","]","`",",","i.e",".","all","tokens","in","the","vocabulary","are","allowed","in","the","generated","template","."]
code: def _get_templates(self):
      inner_model = (
          self.model.module
          if isinstance(self.model, DataParallel)
          else self.model
      )
      input_ids = self.input_ids_buffer
      attention_mask = self.attention_mask_buffer
  
      ori_decoder_input_ids = torch.zeros(
          (input_ids.size(0), self.max_length)
      ).long()
      ori_decoder_input_ids[
          ..., 0
      ] = inner_model.config.decoder_start_token_id
  
      # decoder_input_ids: decoder inputs for next regressive generation
      # ll: log likelihood
      # output_id: which part of generated contents we are at
      # output: generated content so far
      # last_length (deprecated): how long we have generated for this part
      current_output = [
          {
              "decoder_input_ids": ori_decoder_input_ids,
              "ll": 0,
              "output_id": 1,
              "output": [],
              "last_length": -1,
          }
      ]
      for i in tqdm(range(self.max_length - 2)):
          new_current_output = []
          for item in current_output:
              if item["output_id"] > self.target_number:
                  # Enough contents
                  new_current_output.append(item)
                  continue
              decoder_input_ids = item["decoder_input_ids"]
  
              # Forward
              batch_size = 32
              turn = input_ids.size(0) // batch_size
              if input_ids.size(0) % batch_size != 0:
                  turn += 1
              aggr_output = []
              for t in range(turn):
                  start = t * batch_size
                  end = min((t + 1) * batch_size, input_ids.size(0))
  
                  with torch.no_grad():
                      aggr_output.append(
                          self.model(
                              input_ids[start:end],
                              attention_mask=attention_mask[start:end],
                              decoder_input_ids=decoder_input_ids.to(
                                  input_ids.device
                              )[start:end],
                          )[0]
                      )
              aggr_output = torch.cat(aggr_output, 0)
  
              # Gather results across all input sentences, and sort generated tokens by log likelihood
              aggr_output = aggr_output.mean(0)
              log_denominator = torch.logsumexp(
                  aggr_output[i], -1
              ).item()
              ids = list(range(inner_model.config.vocab_size))
              ids.sort(
                  key=lambda x: aggr_output[i][x].item(), reverse=True
              )
              ids = ids[: self.beam_width + 3]
  
              for word_id in ids:
                  output_id = item["output_id"]
  
                  if (
                      word_id == self.get_part_token_id(output_id)
                      or word_id == self.tokenizer.eos_token_id
                  ):
                      # Finish one part
                      if (
                          self.length_limit is not None
                          and item["last_length"]
                          < self.length_limit[output_id - 1]
                      ):
                          check = False
                      else:
                          check = True
                      output_id += 1
                      last_length = 0
                  else:
                      last_length = item["last_length"] + 1
                      check = True
  
                  output_text = item["output"] + [word_id]
                  ll = (
                      item["ll"]
                      + aggr_output[i][word_id]
                      - log_denominator
                  )
                  new_decoder_input_ids = decoder_input_ids.new_zeros(
                      decoder_input_ids.size()
                  )
                  new_decoder_input_ids[:] = decoder_input_ids
                  new_decoder_input_ids[..., i + 1] = word_id
  
                  if word_id in self.forbidden_word_ids:
                      check = False
  
                  # Forbid continuous "."
                  if (
                      len(output_text) > 1
                      and output_text[-2] == self.sent_end_id
                      and output_text[-1] == self.sent_end_id
                  ):
                      check = False
  
                  if check:
                      # Add new results to beam search pool
                      new_item = {
                          "decoder_input_ids": new_decoder_input_ids,
                          "ll": ll,
                          "output_id": output_id,
                          "output": output_text,
                          "last_length": last_length,
                      }
                      new_current_output.append(new_item)
  
          if len(new_current_output) == 0:
              break
  
          new_current_output.sort(key=lambda x: x["ll"], reverse=True)
          new_current_output = new_current_output[: self.beam_width]
          current_output = new_current_output
  
      return [
          self.tokenizer.convert_ids_to_tokens(item["output"])
          for item in current_output
      ]
  
imports: from abc import abstractmethod
  from builtins import ValueError
  from typing import List, Optional, Dict, Union
  from tokenizers import Tokenizer
  import json
  import torch
  import torch.nn.functional
  from yacs.config import CfgNode
  from openprompt.data_utils.utils import InputExample, InputFeatures
  from openprompt.pipeline_base import PromptDataLoader, PromptModel
  from openprompt.prompt_base import Template, Verbalizer
  from openprompt.prompts import ManualTemplate, ManualVerbalizer
  from ..utils import logger
  from transformers import T5Tokenizer, T5ForConditionalGeneration, BertForMaskedLM, RobertaForMaskedLM, RobertaTokenizer, PreTrainedModel, PreTrainedTokenizer
  from tqdm import tqdm
  from typing import List, Optional, Dict
  import itertools
  import numpy
  from ..utils import signature
  from ..config import convert_cfg_to_dict
  from torch.nn.parallel import DataParallel
label_desc: Use image node_id 6 for calling the TemplateGenerator obj's underlying member method code with example usage: obj._get_templates() and returns: unknown