class_docstr: This class defines the pre-norm `Transformer encoder <https://arxiv.org/abs/1706.03762>`_
  Args:
      opts: Command line arguments.
      embed_dim: :math:`C_{in}` from an expected input of size :math:`(N, P, C_{in})`.
      ffn_latent_dim: Inner dimension of the FFN.
      num_heads: Number of heads in multi-head attention. Default: 8.
      attn_dropout: Dropout rate for attention in multi-head attention. Default: 0.0
      dropout: Dropout rate. Default: 0.0.
      ffn_dropout: Dropout between FFN layers. Default: 0.0.
      transformer_norm_layer: Normalization layer. Default: layer_norm.
      stochastic_dropout: Stochastic dropout setting. Default: 0.0.
  
  Shape:
      - Input: :math:`(N, P, C_{in})` where :math:`N` is batch size, :math:`P` is number of patches,
      and :math:`C_{in}` is input embedding dim
      - Output: same shape as the input
class_docstr_tok: ["This","class","defines","the","pre-norm","`","Transformer","encoder","<","https",":","\/\/arxiv.org\/abs\/1706.03762",">","`","_","Args",":","opts",":","Command","line","arguments",".","embed_dim",":",":","math",":","`","C_","{","in","}","`","from","an","expected","input","of","size",":","math",":","`","(","N",",","P",",","C_","{","in","}",")","`",".","ffn_latent_dim",":","Inner","dimension","of","the","FFN",".","num_heads",":","Number","of","heads","in","multi-head","attention",".","Default",":","8.","attn_dropout",":","Dropout","rate","for","attention","in","multi-head","attention",".","Default",":","0.0","dropout",":","Dropout","rate",".","Default",":","0.0.","ffn_dropout",":","Dropout","between","FFN","layers",".","Default",":","0.0.","transformer_norm_layer",":","Normalization","layer",".","Default",":","layer_norm",".","stochastic_dropout",":","Stochastic","dropout","setting",".","Default",":","0.0",".","Shape",":","-","Input",":",":","math",":","`","(","N",",","P",",","C_","{","in","}",")","`","where",":","math",":","`","N","`","is","batch","size",",",":","math",":","`","P","`","is","number","of","patches",",","and",":","math",":","`","C_","{","in","}","`","is","input","embedding","dim","-","Output",":","same","shape","as","the","input"]
code: def forward(
      self,
      x: Tensor,
      x_prev: Optional[Tensor] = None,
      key_padding_mask: Optional[Tensor] = None,
      attn_mask: Optional[Tensor] = None,
      *args,
      **kwargs,
  ) -> Tensor:
      # Multi-head attention
      res = x
      x = self.pre_norm_mha[0](x)  # norm
      x = self.pre_norm_mha[1](
          x_q=x,
          x_kv=x_prev,
          key_padding_mask=key_padding_mask,
          attn_mask=attn_mask,
          *args,
          **kwargs,
      )  # mha
  
      x = self.drop_path(
          self.pre_norm_mha[2](x)
      )  # applying stochastic depth
      x = x + res
  
      # Feed forward network
      x = x + self.drop_path(self.pre_norm_ffn(x))
      return x
  
file_path: ml-cvnets/cvnets/modules/transformer.py
imports: import argparse
  from typing import Optional
  from torch import Tensor, nn
  from cvnets.layers import ConvLayer2d, Dropout, Identity, LinearLayer, LinearSelfAttention, MultiHeadAttention, SingleHeadAttention, StochasticDepth, get_normalization_layer
  from cvnets.layers.activation import build_activation_layer
  from cvnets.modules import BaseModule
  from utils import logger
label_desc: Use image node_id 3 for calling the TransformerEncoder obj's underlying member method code with example usage: obj.forward(x, x_prev, key_padding_mask, attn_mask) and returns: x