class_docstr: Encoder 为 :class:`~fastNLP.modules.torch.encoder.TransformerSeq2SeqEncoder` ，decoder 为 
  :class:`~fastNLP.modules.torch.decoder.TransformerSeq2SeqDecoder` 的 :class:`Seq2SeqModel` ，
  通过 :meth:`build_model` 方法初始化。
docstr: 初始化一个 :class:`TransformerSeq2SeqModel` 。
  
  :param src_embed: source 的 embedding，支持以下几种输入类型：
  
          - ``tuple(num_embedings, embedding_dim)``，即 embedding 的大小和每个词的维度，此时将随机初始化一个 :class:`torch.nn.Embedding` 实例；
          - :class:`torch.nn.Embedding` 或 **fastNLP** 的 ``Embedding`` 对象，此时就以传入的对象作为 embedding；
          - :class:`numpy.ndarray` ，将使用传入的 ndarray 作为 Embedding 初始化；
          - :class:`torch.Tensor`，此时将使用传入的值作为 Embedding 初始化；
  :param tgt_embed: target 的 embedding，如果 ``bind_encoder_decoder_embed``
      为 ``True`` ，则不要输入该值。支持以下几种输入类型：
  
          - ``tuple(num_embedings, embedding_dim)``，即 embedding 的大小和每个词的维度，此时将随机初始化一个 :class:`torch.nn.Embedding` 实例；
          - :class:`torch.nn.Embedding` 或 **fastNLP** 的 ``Embedding`` 对象，此时就以传入的对象作为 embedding；
          - :class:`numpy.ndarray` ，将使用传入的 ndarray 作为 Embedding 初始化；
          - :class:`torch.Tensor`，此时将使用传入的值作为 Embedding 初始化；
  :param pos_embed: 支持 ``['sin', 'learned']`` 两种
  :param max_position: 最大支持长度
  :param num_layers: ``encoder`` 和 ``decoder`` 的层数
  :param d_model: ``encoder`` 和 ``decoder`` 输入输出的大小
  :param n_head: ``encoder`` 和 ``decoder`` 的 head 的数量
  :param dim_ff: ``encoder`` 和 ``decoder`` 中 FFN 中间映射的维度
  :param dropout: Attention 和 FFN dropout的大小
  :param bind_encoder_decoder_embed: 是否对 ``encoder`` 和 ``decoder`` 使用相同的 embedding
  :param bind_decoder_input_output_embed: ``decoder`` 的输出 embedding 是否与其输入 embedding 是一样的权重
  :return: :class:`TransformerSeq2SeqModel` 模型
docstr_tok: ["\u521d\u59cb\u5316\u4e00\u4e2a",":","class",":","`","TransformerSeq2SeqModel","`","\u3002",":","param","src_embed",":","source","\u7684","embedding\uff0c\u652f\u6301\u4ee5\u4e0b\u51e0\u79cd\u8f93\u5165\u7c7b\u578b\uff1a","-","``","tuple","(","num_embedings",",","embedding_dim",")","``","\uff0c\u5373","embedding","\u7684\u5927\u5c0f\u548c\u6bcf\u4e2a\u8bcd\u7684\u7ef4\u5ea6\uff0c\u6b64\u65f6\u5c06\u968f\u673a\u521d\u59cb\u5316\u4e00\u4e2a",":","class",":","`","torch.nn.Embedding","`","\u5b9e\u4f8b\uff1b","-",":","class",":","`","torch.nn.Embedding","`","\u6216","*","*","fastNLP","*","*","\u7684","``","Embedding","``","\u5bf9\u8c61\uff0c\u6b64\u65f6\u5c31\u4ee5\u4f20\u5165\u7684\u5bf9\u8c61\u4f5c\u4e3a","embedding\uff1b","-",":","class",":","`","numpy.ndarray","`","\uff0c\u5c06\u4f7f\u7528\u4f20\u5165\u7684","ndarray","\u4f5c\u4e3a","Embedding","\u521d\u59cb\u5316\uff1b","-",":","class",":","`","torch.Tensor","`","\uff0c\u6b64\u65f6\u5c06\u4f7f\u7528\u4f20\u5165\u7684\u503c\u4f5c\u4e3a","Embedding","\u521d\u59cb\u5316\uff1b",":","param","tgt_embed",":","target","\u7684","embedding\uff0c\u5982\u679c","``","bind_encoder_decoder_embed","``","\u4e3a","``","True","``","\uff0c\u5219\u4e0d\u8981\u8f93\u5165\u8be5\u503c\u3002\u652f\u6301\u4ee5\u4e0b\u51e0\u79cd\u8f93\u5165\u7c7b\u578b\uff1a","-","``","tuple","(","num_embedings",",","embedding_dim",")","``","\uff0c\u5373","embedding","\u7684\u5927\u5c0f\u548c\u6bcf\u4e2a\u8bcd\u7684\u7ef4\u5ea6\uff0c\u6b64\u65f6\u5c06\u968f\u673a\u521d\u59cb\u5316\u4e00\u4e2a",":","class",":","`","torch.nn.Embedding","`","\u5b9e\u4f8b\uff1b","-",":","class",":","`","torch.nn.Embedding","`","\u6216","*","*","fastNLP","*","*","\u7684","``","Embedding","``","\u5bf9\u8c61\uff0c\u6b64\u65f6\u5c31\u4ee5\u4f20\u5165\u7684\u5bf9\u8c61\u4f5c\u4e3a","embedding\uff1b","-",":","class",":","`","numpy.ndarray","`","\uff0c\u5c06\u4f7f\u7528\u4f20\u5165\u7684","ndarray","\u4f5c\u4e3a","Embedding","\u521d\u59cb\u5316\uff1b","-",":","class",":","`","torch.Tensor","`","\uff0c\u6b64\u65f6\u5c06\u4f7f\u7528\u4f20\u5165\u7684\u503c\u4f5c\u4e3a","Embedding","\u521d\u59cb\u5316\uff1b",":","param","pos_embed",":","\u652f\u6301","``","[","'sin","'",",","'learned","'","]","``","\u4e24\u79cd",":","param","max_position",":","\u6700\u5927\u652f\u6301\u957f\u5ea6",":","param","num_layers",":","``","encoder","``","\u548c","``","decoder","``","\u7684\u5c42\u6570",":","param","d_model",":","``","encoder","``","\u548c","``","decoder","``","\u8f93\u5165\u8f93\u51fa\u7684\u5927\u5c0f",":","param","n_head",":","``","encoder","``","\u548c","``","decoder","``","\u7684","head","\u7684\u6570\u91cf",":","param","dim_ff",":","``","encoder","``","\u548c","``","decoder","``","\u4e2d","FFN","\u4e2d\u95f4\u6620\u5c04\u7684\u7ef4\u5ea6",":","param","dropout",":","Attention","\u548c","FFN","dropout\u7684\u5927\u5c0f",":","param","bind_encoder_decoder_embed",":","\u662f\u5426\u5bf9","``","encoder","``","\u548c","``","decoder","``","\u4f7f\u7528\u76f8\u540c\u7684","embedding",":","param","bind_decoder_input_output_embed",":","``","decoder","``","\u7684\u8f93\u51fa","embedding","\u662f\u5426\u4e0e\u5176\u8f93\u5165","embedding","\u662f\u4e00\u6837\u7684\u6743\u91cd",":","return",":",":","class",":","`","TransformerSeq2SeqModel","`","\u6a21\u578b"]
code: def build_model(
      cls,
      src_embed,
      tgt_embed=None,
      pos_embed: str = "sin",
      max_position: int = 1024,
      num_layers: int = 6,
      d_model: int = 512,
      n_head: int = 8,
      dim_ff: int = 2048,
      dropout: float = 0.1,
      bind_encoder_decoder_embed: bool = False,
      bind_decoder_input_output_embed: bool = True,
  ):
      """
      初始化一个 :class:`TransformerSeq2SeqModel` 。
  
      :param src_embed: source 的 embedding，支持以下几种输入类型：
  
              - ``tuple(num_embedings, embedding_dim)``，即 embedding 的大小和每个词的维度，此时将随机初始化一个 :class:`torch.nn.Embedding` 实例；
              - :class:`torch.nn.Embedding` 或 **fastNLP** 的 ``Embedding`` 对象，此时就以传入的对象作为 embedding；
              - :class:`numpy.ndarray` ，将使用传入的 ndarray 作为 Embedding 初始化；
              - :class:`torch.Tensor`，此时将使用传入的值作为 Embedding 初始化；
      :param tgt_embed: target 的 embedding，如果 ``bind_encoder_decoder_embed``
          为 ``True`` ，则不要输入该值。支持以下几种输入类型：
  
              - ``tuple(num_embedings, embedding_dim)``，即 embedding 的大小和每个词的维度，此时将随机初始化一个 :class:`torch.nn.Embedding` 实例；
              - :class:`torch.nn.Embedding` 或 **fastNLP** 的 ``Embedding`` 对象，此时就以传入的对象作为 embedding；
              - :class:`numpy.ndarray` ，将使用传入的 ndarray 作为 Embedding 初始化；
              - :class:`torch.Tensor`，此时将使用传入的值作为 Embedding 初始化；
      :param pos_embed: 支持 ``['sin', 'learned']`` 两种
      :param max_position: 最大支持长度
      :param num_layers: ``encoder`` 和 ``decoder`` 的层数
      :param d_model: ``encoder`` 和 ``decoder`` 输入输出的大小
      :param n_head: ``encoder`` 和 ``decoder`` 的 head 的数量
      :param dim_ff: ``encoder`` 和 ``decoder`` 中 FFN 中间映射的维度
      :param dropout: Attention 和 FFN dropout的大小
      :param bind_encoder_decoder_embed: 是否对 ``encoder`` 和 ``decoder`` 使用相同的 embedding
      :param bind_decoder_input_output_embed: ``decoder`` 的输出 embedding 是否与其输入 embedding 是一样的权重
      :return: :class:`TransformerSeq2SeqModel` 模型
      """
      if bind_encoder_decoder_embed and tgt_embed is not None:
          raise RuntimeError(
              "If you set `bind_encoder_decoder_embed=True`, please do not provide `tgt_embed`."
          )
  
      src_embed = get_embeddings(src_embed)
  
      if bind_encoder_decoder_embed:
          tgt_embed = src_embed
      else:
          assert (
              tgt_embed is not None
          ), "You need to pass `tgt_embed` when `bind_encoder_decoder_embed=False`"
          tgt_embed = get_embeddings(tgt_embed)
  
      if pos_embed == "sin":
          encoder_pos_embed = nn.Embedding.from_pretrained(
              get_sinusoid_encoding_table(
                  max_position + 1,
                  src_embed.embedding_dim,
                  padding_idx=0,
              ),
              freeze=True,
          )  # 这里规定0是padding
          deocder_pos_embed = nn.Embedding.from_pretrained(
              get_sinusoid_encoding_table(
                  max_position + 1,
                  tgt_embed.embedding_dim,
                  padding_idx=0,
              ),
              freeze=True,
          )  # 这里规定0是padding
      elif pos_embed == "learned":
          encoder_pos_embed = get_embeddings(
              (max_position + 1, src_embed.embedding_dim), padding_idx=0
          )
          deocder_pos_embed = get_embeddings(
              (max_position + 1, src_embed.embedding_dim), padding_idx=1
          )
      else:
          raise ValueError("pos_embed only supports sin or learned.")
  
      encoder = TransformerSeq2SeqEncoder(
          embed=src_embed,
          pos_embed=encoder_pos_embed,
          num_layers=num_layers,
          d_model=d_model,
          n_head=n_head,
          dim_ff=dim_ff,
          dropout=dropout,
      )
      decoder = TransformerSeq2SeqDecoder(
          embed=tgt_embed,
          pos_embed=deocder_pos_embed,
          d_model=d_model,
          num_layers=num_layers,
          n_head=n_head,
          dim_ff=dim_ff,
          dropout=dropout,
          bind_decoder_input_output_embed=bind_decoder_input_output_embed,
      )
  
      return cls(encoder, decoder)
  
imports: import torch
  from torch import nn
  import torch.nn.functional
  from fastNLP import seq_len_to_mask
  from ...embeddings.torch.utils import get_embeddings
  from ...embeddings.torch.utils import get_sinusoid_encoding_table
  from ...modules.torch.decoder.seq2seq_decoder import Seq2SeqDecoder, TransformerSeq2SeqDecoder, LSTMSeq2SeqDecoder
  from ...modules.torch.encoder.seq2seq_encoder import Seq2SeqEncoder, TransformerSeq2SeqEncoder, LSTMSeq2SeqEncoder
label_desc: Use image node_id 2 for calling the TransformerSeq2SeqModel obj's underlying member method code with example usage: obj.build_model(cls, src_embed, tgt_embed, pos_embed, max_position, num_layers, d_model, n_head, dim_ff, dropout, bind_encoder_decoder_embed, bind_decoder_input_output_embed) and returns: cls