class_docstr: 通过使用本模型封装 seq2seq_model 使得其既可以用于训练也可以用于生成。训练的时候，本模型的 :meth:`forward` 函数会被调用，
  生成的时候本模型的 :meth:`predict` 函数会被调用。
  
  :param seq2seq_model: 序列到序列模型
  :param bos_token_id: 句子开头的 token id
  :param eos_token_id: 句子结束的 token id
  :param max_length: 生成句子的最大长度, 每句话的 decode 长度为 ``max_length + max_len_a * src_len``
  :param max_len_a: 每句话的 decode 长度为 ``max_length + max_len_a*src_len``。如果不为 0，需要保证 State 中包含 encoder_mask
  :param num_beams: **beam search** 的大小
  :param do_sample: 是否通过采样的方式生成
  :param temperature: 只有在 do_sample 为 ``True`` 才有意义
  :param top_k: 只从 ``top_k`` 中采样
  :param top_p: 只从 ``top_p`` 的 token 中采样（ **nucleus sampling** ）
  :param repetition_penalty: 多大程度上惩罚重复的 token
  :param length_penalty: 对长度的惩罚，**小于 1** 鼓励长句，**大于 1** 鼓励短句
  :param pad_token_id: 当某句话生成结束之后，之后生成的内容用 ``pad_token_id`` 补充
class_docstr_tok: ["\u901a\u8fc7\u4f7f\u7528\u672c\u6a21\u578b\u5c01\u88c5","seq2seq_model","\u4f7f\u5f97\u5176\u65e2\u53ef\u4ee5\u7528\u4e8e\u8bad\u7ec3\u4e5f\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u3002\u8bad\u7ec3\u7684\u65f6\u5019\uff0c\u672c\u6a21\u578b\u7684",":","meth",":","`","forward","`","\u51fd\u6570\u4f1a\u88ab\u8c03\u7528\uff0c","\u751f\u6210\u7684\u65f6\u5019\u672c\u6a21\u578b\u7684",":","meth",":","`","predict","`","\u51fd\u6570\u4f1a\u88ab\u8c03\u7528\u3002",":","param","seq2seq_model",":","\u5e8f\u5217\u5230\u5e8f\u5217\u6a21\u578b",":","param","bos_token_id",":","\u53e5\u5b50\u5f00\u5934\u7684","token","id",":","param","eos_token_id",":","\u53e5\u5b50\u7ed3\u675f\u7684","token","id",":","param","max_length",":","\u751f\u6210\u53e5\u5b50\u7684\u6700\u5927\u957f\u5ea6",",","\u6bcf\u53e5\u8bdd\u7684","decode","\u957f\u5ea6\u4e3a","``","max_length","+","max_len_a","*","src_len","``",":","param","max_len_a",":","\u6bcf\u53e5\u8bdd\u7684","decode","\u957f\u5ea6\u4e3a","``","max_length","+","max_len_a","*","src_len","``","\u3002\u5982\u679c\u4e0d\u4e3a","0\uff0c\u9700\u8981\u4fdd\u8bc1","State","\u4e2d\u5305\u542b","encoder_mask",":","param","num_beams",":","*","*","beam","search","*","*","\u7684\u5927\u5c0f",":","param","do_sample",":","\u662f\u5426\u901a\u8fc7\u91c7\u6837\u7684\u65b9\u5f0f\u751f\u6210",":","param","temperature",":","\u53ea\u6709\u5728","do_sample","\u4e3a","``","True","``","\u624d\u6709\u610f\u4e49",":","param","top_k",":","\u53ea\u4ece","``","top_k","``","\u4e2d\u91c7\u6837",":","param","top_p",":","\u53ea\u4ece","``","top_p","``","\u7684","token","\u4e2d\u91c7\u6837\uff08","*","*","nucleus","sampling","*","*","\uff09",":","param","repetition_penalty",":","\u591a\u5927\u7a0b\u5ea6\u4e0a\u60e9\u7f5a\u91cd\u590d\u7684","token",":","param","length_penalty",":","\u5bf9\u957f\u5ea6\u7684\u60e9\u7f5a\uff0c","*","*","\u5c0f\u4e8e","1","*","*","\u9f13\u52b1\u957f\u53e5\uff0c","*","*","\u5927\u4e8e","1","*","*","\u9f13\u52b1\u77ed\u53e5",":","param","pad_token_id",":","\u5f53\u67d0\u53e5\u8bdd\u751f\u6210\u7ed3\u675f\u4e4b\u540e\uff0c\u4e4b\u540e\u751f\u6210\u7684\u5185\u5bb9\u7528","``","pad_token_id","``","\u8865\u5145"]
code: def __init__(
      self,
      seq2seq_model: Seq2SeqModel,
      bos_token_id: int = None,
      eos_token_id: int = None,
      max_length: int = 30,
      max_len_a: float = 0.0,
      num_beams: int = 1,
      do_sample: bool = True,
      temperature: float = 1.0,
      top_k: int = 50,
      top_p: float = 1.0,
      repetition_penalty: float = 1,
      length_penalty: float = 1.0,
      pad_token_id: int = 0,
  ):
      super().__init__()
      self.seq2seq_model = seq2seq_model
      self.generator = SequenceGenerator(
          seq2seq_model.decoder,
          max_length=max_length,
          max_len_a=max_len_a,
          num_beams=num_beams,
          do_sample=do_sample,
          temperature=temperature,
          top_k=top_k,
          top_p=top_p,
          bos_token_id=bos_token_id,
          eos_token_id=eos_token_id,
          repetition_penalty=repetition_penalty,
          length_penalty=length_penalty,
          pad_token_id=pad_token_id,
      )
  
file_path: fastNLP/fastNLP/models/torch/seq2seq_generator.py
imports: import torch
  from torch import nn
  import torch.nn.functional
  from fastNLP import seq_len_to_mask
  from .seq2seq_model import Seq2SeqModel
  from ...modules.torch.generator.seq2seq_generator import SequenceGenerator
label_desc: Use image node_id 1 to create a new SequenceGeneratorModel object from inherited base classes: nn with example: obj = SequenceGeneratorModel(seq2seq_model, bos_token_id, eos_token_id, max_length, max_len_a, num_beams, do_sample, temperature, top_k, top_p, repetition_penalty, length_penalty, pad_token_id)