class_docstr: 基于 **Transformer** 的 :class:`Encoder`
  
  :param embed: ``decoder`` 输入的 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 pos_embed: 位置 embedding
  :param d_model: 输入、输出的维度
  :param num_layers: :class:`TransformerSeq2SeqDecoderLayer` 的层数
  :param n_head: **多头注意力** head 的数目，需要能被 ``d_model`` 整除
  :param dim_ff: FFN 中间映射的维度
  :param dropout: :class:`~fastNLP.modules.torch.decoder.SelfAttention` 和 FFN 中的 dropout 的大小
class_docstr_tok: ["\u57fa\u4e8e","*","*","Transformer","*","*","\u7684",":","class",":","`","Encoder","`",":","param","embed",":","``","decoder","``","\u8f93\u5165\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","pos_embed",":","\u4f4d\u7f6e","embedding",":","param","d_model",":","\u8f93\u5165\u3001\u8f93\u51fa\u7684\u7ef4\u5ea6",":","param","num_layers",":",":","class",":","`","TransformerSeq2SeqDecoderLayer","`","\u7684\u5c42\u6570",":","param","n_head",":","*","*","\u591a\u5934\u6ce8\u610f\u529b","*","*","head","\u7684\u6570\u76ee\uff0c\u9700\u8981\u80fd\u88ab","``","d_model","``","\u6574\u9664",":","param","dim_ff",":","FFN","\u4e2d\u95f4\u6620\u5c04\u7684\u7ef4\u5ea6",":","param","dropout",":",":","class",":","`","~fastNLP.modules.torch.decoder.SelfAttention","`","\u548c","FFN","\u4e2d\u7684","dropout","\u7684\u5927\u5c0f"]
docstr: :param tokens: 输入序列，形状为 ``[batch_size, max_len]``
  :param seq_len: 序列长度，形状为 ``[batch_size, ]``，若为 ``None``，表示所有输入看做一样长
  :return: 一个元组，第一个元素形状为 ``[batch_size, max_len, d_model]`` 表示前向传播的结果，第二个元素形状为
      ``[batch_size, max_len]``， 表示产生的掩码 ``encoder_mask``，为 **0** 的地方为 padding
docstr_tok: [":","param","tokens",":","\u8f93\u5165\u5e8f\u5217\uff0c\u5f62\u72b6\u4e3a","``","[","batch_size",",","max_len","]","``",":","param","seq_len",":","\u5e8f\u5217\u957f\u5ea6\uff0c\u5f62\u72b6\u4e3a","``","[","batch_size",",","]","``","\uff0c\u82e5\u4e3a","``","None","``","\uff0c\u8868\u793a\u6240\u6709\u8f93\u5165\u770b\u505a\u4e00\u6837\u957f",":","return",":","\u4e00\u4e2a\u5143\u7ec4\uff0c\u7b2c\u4e00\u4e2a\u5143\u7d20\u5f62\u72b6\u4e3a","``","[","batch_size",",","max_len",",","d_model","]","``","\u8868\u793a\u524d\u5411\u4f20\u64ad\u7684\u7ed3\u679c\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u5f62\u72b6\u4e3a","``","[","batch_size",",","max_len","]","``","\uff0c","\u8868\u793a\u4ea7\u751f\u7684\u63a9\u7801","``","encoder_mask","``","\uff0c\u4e3a","*","*","0","*","*","\u7684\u5730\u65b9\u4e3a","padding"]
code: def forward(self, tokens, seq_len):
      """
  
      :param tokens: 输入序列，形状为 ``[batch_size, max_len]``
      :param seq_len: 序列长度，形状为 ``[batch_size, ]``，若为 ``None``，表示所有输入看做一样长
      :return: 一个元组，第一个元素形状为 ``[batch_size, max_len, d_model]`` 表示前向传播的结果，第二个元素形状为
          ``[batch_size, max_len]``， 表示产生的掩码 ``encoder_mask``，为 **0** 的地方为 padding
      """
      x = self.embed(tokens) * self.embed_scale  # batch, seq, dim
      batch_size, max_src_len, _ = x.size()
      device = x.device
      if self.pos_embed is not None:
          position = (
              torch.arange(1, max_src_len + 1)
              .unsqueeze(0)
              .long()
              .to(device)
          )
          x += self.pos_embed(position)
  
      x = self.input_fc(x)
      x = F.dropout(x, p=self.dropout, training=self.training)
  
      encoder_mask = seq_len_to_mask(seq_len, max_len=max_src_len)
      encoder_mask = encoder_mask.to(device)
  
      for layer in self.layer_stacks:
          x = layer(x, encoder_mask)
  
      x = self.layer_norm(x)
  
      return x, encoder_mask
  
imports: import torch.nn
  import torch
  from torch.nn import LayerNorm
  import torch.nn.functional
  from typing import Union, Tuple
  from ....core.utils import seq_len_to_mask
  import math
  from .lstm import LSTM
  from ..attention import MultiHeadAttention
  from ....embeddings.torch import StaticEmbedding
  from ....embeddings.torch.utils import get_embeddings
label_desc: Use image node_id 2 for calling the TransformerSeq2SeqEncoder obj's underlying member method code with example usage: obj.forward(tokens, seq_len) and returns: x,  encoder_mask