class_docstr: The Transformer encoder from "Attention is All You Need"
  :cite:`DBLP:journals/corr/VaswaniSPUJGKP17`
  
  Args:
      num_layers (int): number of encoder layers
      d_model (int): size of the model
      heads (int): number of heads
      d_ff (int): size of the inner FF layer
      dropout (float): dropout parameters
      embeddings (onmt.modules.Embeddings):
        embeddings to use, should have positional encodings
      pos_ffn_activation_fn (ActivationFunction):
          activation function choice for PositionwiseFeedForward layer
  
  Returns:
      (torch.FloatTensor, torch.FloatTensor):
  
      * enc_out ``(batch_size, src_len, model_dim)``
      * encoder final state: None in the case of Transformer
      * src_len ``(batch_size)``
class_docstr_tok: ["The","Transformer","encoder","from","``","Attention","is","All","You","Need","''",":","cite",":","`","DBLP",":","journals\/corr\/VaswaniSPUJGKP17","`","Args",":","num_layers","(","int",")",":","number","of","encoder","layers","d_model","(","int",")",":","size","of","the","model","heads","(","int",")",":","number","of","heads","d_ff","(","int",")",":","size","of","the","inner","FF","layer","dropout","(","float",")",":","dropout","parameters","embeddings","(","onmt.modules.Embeddings",")",":","embeddings","to","use",",","should","have","positional","encodings","pos_ffn_activation_fn","(","ActivationFunction",")",":","activation","function","choice","for","PositionwiseFeedForward","layer","Returns",":","(","torch.FloatTensor",",","torch.FloatTensor",")",":","*","enc_out","``","(","batch_size",",","src_len",",","model_dim",")","``","*","encoder","final","state",":","None","in","the","case","of","Transformer","*","src_len","``","(","batch_size",")","``"]
docstr: Alternate constructor.
docstr_tok: ["Alternate","constructor","."]
code: def from_opt(cls, opt, embeddings):
      """Alternate constructor."""
      return cls(
          opt.enc_layers,
          opt.enc_hid_size,
          opt.heads,
          opt.transformer_ff,
          opt.dropout[0] if type(opt.dropout) is list else opt.dropout,
          opt.attention_dropout[0]
          if type(opt.attention_dropout) is list
          else opt.attention_dropout,
          embeddings,
          opt.max_relative_positions,
          opt.relative_positions_buckets,
          pos_ffn_activation_fn=opt.pos_ffn_activation_fn,
          add_qkvbias=opt.add_qkvbias,
          num_kv=opt.num_kv,
          add_ffnbias=opt.add_ffnbias,
          parallel_residual=opt.parallel_residual,
          layer_norm=opt.layer_norm,
          norm_eps=opt.norm_eps,
          use_ckpting=opt.use_ckpting,
          parallel_gpu=opt.world_size
          if opt.parallel_mode == "tensor_parallel"
          else 1,
          rotary_interleave=opt.rotary_interleave,
          rotary_theta=opt.rotary_theta,
          rotary_dim=opt.rotary_dim,
      )
  
file_path: OpenNMT-py/onmt/encoders/transformer.py
imports: import torch.nn
  from onmt.encoders.encoder import EncoderBase
  from onmt.modules import MultiHeadedAttention
  from onmt.modules.position_ffn import PositionwiseFeedForward
  from onmt.modules.position_ffn import ActivationFunction
  from onmt.utils.misc import sequence_mask
label_desc: Use image node_id 2 for calling the TransformerEncoder obj's underlying member method code with example usage: obj.from_opt(cls, opt, embeddings) and returns: cls