class_docstr: Transformer encoder module.
  
  For documentation on parameters that are take directly from opt,
  see parlai/agents/transformer/transformer.py
  
  :param opt: ParlAI-parsed options.
  :param vocabulary_size: Count of tokens/words in the dictionary.
  :param embedding: an embedding matrix for the bottom layer of the transformer.
      If none, one is created for this encoder.
  :param int padding_idx: Reserved padding index in the embeddings matrix.
  :param str reduction_type: Type of reduction at the end of the encoder.
  :param int n_positions: Size of the position embeddings matrix.
  :param int n_segments: Number of segments/lang/sentence embeddings.
  :param bool embeddings_scale: Scale embeddings relative to their dimensionality.
      Found useful in fairseq.
class_docstr_tok: ["Transformer","encoder","module",".","For","documentation","on","parameters","that","are","take","directly","from","opt",",","see","parlai\/agents\/transformer\/transformer.py",":","param","opt",":","ParlAI-parsed","options",".",":","param","vocabulary_size",":","Count","of","tokens\/words","in","the","dictionary",".",":","param","embedding",":","an","embedding","matrix","for","the","bottom","layer","of","the","transformer",".","If","none",",","one","is","created","for","this","encoder",".",":","param","int","padding_idx",":","Reserved","padding","index","in","the","embeddings","matrix",".",":","param","str","reduction_type",":","Type","of","reduction","at","the","end","of","the","encoder",".",":","param","int","n_positions",":","Size","of","the","position","embeddings","matrix",".",":","param","int","n_segments",":","Number","of","segments\/lang\/sentence","embeddings",".",":","param","bool","embeddings_scale",":","Scale","embeddings","relative","to","their","dimensionality",".","Found","useful","in","fairseq","."]
code: def __init__(
      self,
      opt: Opt,
      vocabulary_size: int,
      embedding: Optional[nn.Embedding] = None,
      padding_idx: int = 0,
      reduction_type: str = "mean",
      n_positions: Optional[int] = None,
      n_segments: Optional[int] = None,
      embeddings_scale: Optional[bool] = None,
      dropout: Optional[float] = None,
      activation: Optional[str] = None,
      variant: Optional[str] = None,
      output_scaling: Optional[float] = None,
      **kwargs,
  ):
      super().__init__()
  
      self.opt = opt
      self.embedding_size = opt["embedding_size"]
      self.ffn_size = opt["ffn_size"]
      self.n_layers = (
          opt["n_encoder_layers"]
          if opt.get("n_encoder_layers", -1) > 0
          else opt["n_layers"]
      )
      self.n_heads = opt["n_heads"]
      self.dim = self.embedding_size
      self.embeddings_scale = default(
          embeddings_scale, opt.get("embeddings_scale", False)
      )
      self.reduction_type = reduction_type
      self.padding_idx = padding_idx
      # this is --dropout, not --relu-dropout or --attention-dropout
      self.dropout_frac = default(dropout, opt.get("dropout", 0.0))
      self.dropout = nn.Dropout(p=self.dropout_frac)
      self.activation = default(
          activation, opt.get("activation", "relu")
      )
      self.variant = default(variant, opt.get("variant", "aiayn"))
      self.n_segments = default(n_segments, opt.get("n_segments", 0))
  
      self.n_positions = default(
          n_positions, get_n_positions_from_options(opt)
      )
      self.out_dim = self.embedding_size
      assert (
          self.embedding_size % self.n_heads == 0
      ), "Transformer embedding size must be a multiple of n_heads"
  
      # check input formats:
      if embedding is not None:
          assert (
              self.embedding_size is None
              or self.embedding_size == embedding.weight.shape[1]
          ), "Embedding dim must match the embedding size."
  
      if embedding is not None:
          self.embeddings = embedding
      else:
          raise AssertionError(
              "This code should not execute. Left here in case we want to enable it."
          )
          assert self.padding_idx is not None
          self.embeddings = nn.Embedding(
              vocabulary_size,
              self.embedding_size,
              padding_idx=padding_idx,
          )
          nn.init.normal_(
              self.embeddings.weight, 0, self.embedding_size**-0.5
          )
  
      # create the positional embeddings
      self.position_embeddings = nn.Embedding(
          self.n_positions, self.embedding_size
      )
      if not opt.get("learn_positional_embeddings", False):
          create_position_codes(
              self.n_positions,
              self.embedding_size,
              out=self.position_embeddings.weight,
          )
      else:
          nn.init.normal_(
              self.position_embeddings.weight,
              0,
              self.embedding_size**-0.5,
          )
  
      # embedding normalization
      if (
          self.variant == "xlm"
          or self.variant == "prelayernorm"
          or self.variant == "bart"
      ):
          self.norm_embeddings = torch.nn.LayerNorm(
              self.dim, eps=LAYER_NORM_EPS
          )
      elif self.variant == "aiayn":
          pass
      else:
          raise ValueError(
              "Can't handle --variant {}".format(self.variant)
          )
  
      if self.n_segments >= 1:
          self.segment_embeddings = nn.Embedding(
              self.n_segments, self.dim
          )
          nn.init.normal_(
              self.segment_embeddings.weight, 0, self.dim**-0.5
          )
  
      # build the model
      self.layers = self.build_layers()
      self.output_scaling = default(
          output_scaling, opt.get("output_scaling", 1.0)
      )
  
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 1 to create a new TransformerEncoder object from inherited base classes: nn with example: obj = TransformerEncoder(opt, vocabulary_size, embedding, padding_idx, reduction_type, n_positions, n_segments, embeddings_scale, dropout, activation, variant, output_scaling)