class_docstr: Implements a single Transformer encoder layer.
class_docstr_tok: ["Implements","a","single","Transformer","encoder","layer","."]
docstr: Forward pass.
docstr_tok: ["Forward","pass","."]
code: def forward(
      self, tensor: torch.Tensor, mask: torch.Tensor, **kwargs
  ) -> torch.Tensor:
      """
      Forward pass.
      """
      residual = tensor
      if self.variant == "prelayernorm":
          tensor = self.norm1(tensor)
      attended_tensor = self.attention(tensor, mask=mask)[0]
      tensor = residual + self.dropout(attended_tensor)
      if (
          self.variant == "aiayn"
          or self.variant == "xlm"
          or self.variant == "bart"
      ):
          tensor = self.norm1(tensor)
      residual = tensor
      if self.variant == "prelayernorm":
          tensor = self.norm2(tensor)
      tensor = residual + self.dropout(self.ffn(tensor))
      if (
          self.variant == "aiayn"
          or self.variant == "xlm"
          or self.variant == "bart"
      ):
          tensor = self.norm2(tensor)
      tensor *= mask.unsqueeze(-1).type_as(tensor)
      return tensor
  
imports: from __future__ import annotations
  from typing import Tuple, Optional, Union
  import numpy
  import torch
  import torch.nn
  from parlai.agents.transformer.modules import create_position_codes, get_n_positions_from_options, LAYER_NORM_EPS, MultiHeadAttention, TransformerFFN
  from parlai.agents.transformer.modules.modular import swappable
  from parlai.core.opt import Opt
  from parlai.core.params import default
  from parlai.utils.misc import warn_once
  from parlai.utils.torch import PipelineHelper
  from parlai.utils.fsdp import fsdp_wrap
  from parlai.nn.checkpoint import checkpoint_wrapper
label_desc: Use image node_id 2 for calling the TransformerEncoderLayer obj's underlying member method code with example usage: obj.forward(tensor, mask) and returns: tensor