class_docstr: TransformerEncoder is a stack of N encoder layers.
  
  Users can build the BERT(https://arxiv.org/abs/1810.04805) model with corresponding parameters.
  
  Args:
      encoder_layer: an instance of the TransformerEncoderLayer() class (required).
      num_layers: the number of sub-encoder-layers in the encoder (required).
      norm: the layer normalization component (optional).
      enable_nested_tensor: if True, input will automatically convert to nested tensor
          (and convert back on output). This will improve the overall performance of
          TransformerEncoder when padding rate is high. Default: ``True`` (enabled).
  
  Examples::
      >>> encoder_layer = nn.TransformerEncoderLayer(d_model=512, nhead=8)
      >>> transformer_encoder = nn.TransformerEncoder(encoder_layer, num_layers=6)
      >>> src = torch.rand(10, 32, 512)
      >>> out = transformer_encoder(src)
class_docstr_tok: ["TransformerEncoder","is","a","stack","of","N","encoder","layers",".","Users","can","build","the","BERT","(","https",":","\/\/arxiv.org\/abs\/1810.04805",")","model","with","corresponding","parameters",".","Args",":","encoder_layer",":","an","instance","of","the","TransformerEncoderLayer","(",")","class","(","required",")",".","num_layers",":","the","number","of","sub-encoder-layers","in","the","encoder","(","required",")",".","norm",":","the","layer","normalization","component","(","optional",")",".","enable_nested_tensor",":","if","True",",","input","will","automatically","convert","to","nested","tensor","(","and","convert","back","on","output",")",".","This","will","improve","the","overall","performance","of","TransformerEncoder","when","padding","rate","is","high",".","Default",":","``","True","``","(","enabled",")",".","Examples",":",":",">",">",">","encoder_layer","=","nn.TransformerEncoderLayer","(","d_model=512",",","nhead=8",")",">",">",">","transformer_encoder","=","nn.TransformerEncoder","(","encoder_layer",",","num_layers=6",")",">",">",">","src","=","torch.rand","(","10",",","32",",","512",")",">",">",">","out","=","transformer_encoder","(","src",")"]
docstr: Pass the input through the encoder layers in turn.
  
  Args:
      src: the sequence to the encoder (required).
      mask: the mask for the src sequence (optional).
      src_key_padding_mask: the mask for the src keys per batch (optional).
      is_causal: If specified, applies a causal mask as ``mask``.
          Default: ``None``; try to detect a causal mask.
          Warning:
          ``is_causal`` provides a hint that ``mask`` is the
          causal mask. Providing incorrect hints can result in
          incorrect execution, including forward and backward
          compatibility.
  
  Shape:
      see the docs in Transformer class.
docstr_tok: ["Pass","the","input","through","the","encoder","layers","in","turn",".","Args",":","src",":","the","sequence","to","the","encoder","(","required",")",".","mask",":","the","mask","for","the","src","sequence","(","optional",")",".","src_key_padding_mask",":","the","mask","for","the","src","keys","per","batch","(","optional",")",".","is_causal",":","If","specified",",","applies","a","causal","mask","as","``","mask","``",".","Default",":","``","None","``",";","try","to","detect","a","causal","mask",".","Warning",":","``","is_causal","``","provides","a","hint","that","``","mask","``","is","the","causal","mask",".","Providing","incorrect","hints","can","result","in","incorrect","execution",",","including","forward","and","backward","compatibility",".","Shape",":","see","the","docs","in","Transformer","class","."]
code: def forward(
      self,
      src: Tensor,
      mask: Optional[Tensor] = None,
      src_key_padding_mask: Optional[Tensor] = None,
      is_causal: Optional[bool] = None,
  ) -> Tensor:
      r"""Pass the input through the encoder layers in turn.
  
      Args:
          src: the sequence to the encoder (required).
          mask: the mask for the src sequence (optional).
          src_key_padding_mask: the mask for the src keys per batch (optional).
          is_causal: If specified, applies a causal mask as ``mask``.
              Default: ``None``; try to detect a causal mask.
              Warning:
              ``is_causal`` provides a hint that ``mask`` is the
              causal mask. Providing incorrect hints can result in
              incorrect execution, including forward and backward
              compatibility.
  
      Shape:
          see the docs in Transformer class.
      """
      src_key_padding_mask = F._canonical_mask(
          mask=src_key_padding_mask,
          mask_name="src_key_padding_mask",
          other_type=F._none_or_dtype(mask),
          other_name="mask",
          target_type=src.dtype,
      )
  
      mask = F._canonical_mask(
          mask=mask,
          mask_name="mask",
          other_type=None,
          other_name="",
          target_type=src.dtype,
          check_other=False,
      )
  
      output = src
      convert_to_nested = False
      first_layer = self.layers[0]
      src_key_padding_mask_for_layers = src_key_padding_mask
      why_not_sparsity_fast_path = ""
      str_first_layer = "self.layers[0]"
      batch_first = first_layer.self_attn.batch_first
      is_fastpath_enabled = torch.backends.mha.get_fastpath_enabled()
  
      if not is_fastpath_enabled:
          why_not_sparsity_fast_path = (
              "torch.backends.mha.get_fastpath_enabled() was not True"
          )
      elif not hasattr(self, "use_nested_tensor"):
          why_not_sparsity_fast_path = (
              "use_nested_tensor attribute not present"
          )
      elif not self.use_nested_tensor:
          why_not_sparsity_fast_path = (
              "self.use_nested_tensor (set in init) was not True"
          )
      elif first_layer.training:
          why_not_sparsity_fast_path = (
              f"{str_first_layer} was in training mode"
          )
      elif not src.dim() == 3:
          why_not_sparsity_fast_path = f"input not batched; expected src.dim() of 3 but got {src.dim()}"
      elif src_key_padding_mask is None:
          why_not_sparsity_fast_path = "src_key_padding_mask was None"
      elif (
          (not hasattr(self, "mask_check")) or self.mask_check
      ) and not torch._nested_tensor_from_mask_left_aligned(
          src, src_key_padding_mask.logical_not()
      ):
          why_not_sparsity_fast_path = "mask_check enabled, and src and src_key_padding_mask was not left aligned"
      elif output.is_nested:
          why_not_sparsity_fast_path = (
              "NestedTensor input is not supported"
          )
      elif mask is not None:
          why_not_sparsity_fast_path = (
              "src_key_padding_mask and mask were both supplied"
          )
      elif torch.is_autocast_enabled():
          why_not_sparsity_fast_path = "autocast is enabled"
  
      if not why_not_sparsity_fast_path:
          tensor_args = (
              src,
              first_layer.self_attn.in_proj_weight,
              first_layer.self_attn.in_proj_bias,
              first_layer.self_attn.out_proj.weight,
              first_layer.self_attn.out_proj.bias,
              first_layer.norm1.weight,
              first_layer.norm1.bias,
              first_layer.norm2.weight,
              first_layer.norm2.bias,
              first_layer.linear1.weight,
              first_layer.linear1.bias,
              first_layer.linear2.weight,
              first_layer.linear2.bias,
          )
          _supported_device_type = [
              "cpu",
              "cuda",
              torch.utils.backend_registration._privateuse1_backend_name,
          ]
          if torch.overrides.has_torch_function(tensor_args):
              why_not_sparsity_fast_path = (
                  "some Tensor argument has_torch_function"
              )
          elif src.device.type not in _supported_device_type:
              why_not_sparsity_fast_path = f"src device is neither one of {_supported_device_type}"
          elif torch.is_grad_enabled() and any(
              x.requires_grad for x in tensor_args
          ):
              why_not_sparsity_fast_path = (
                  "grad is enabled and at least one of query or the "
                  "input/output projection weights or biases requires_grad"
              )
  
          if (not why_not_sparsity_fast_path) and (
              src_key_padding_mask is not None
          ):
              convert_to_nested = True
              output = torch._nested_tensor_from_mask(
                  output,
                  src_key_padding_mask.logical_not(),
                  mask_check=False,
              )
              src_key_padding_mask_for_layers = None
  
      seq_len = _get_seq_len(src, batch_first)
      is_causal = _detect_is_causal_mask(mask, is_causal, seq_len)
  
      for mod in self.layers:
          output = mod(
              output,
              src_mask=mask,
              is_causal=is_causal,
              src_key_padding_mask=src_key_padding_mask_for_layers,
          )
  
      if convert_to_nested:
          output = output.to_padded_tensor(0.0, src.size())
  
      if self.norm is not None:
          output = self.norm(output)
  
      return output
  
imports: import copy
  from typing import Optional, Any, Union, Callable
  import torch
  import warnings
  from torch import Tensor
  from ..None import functional
  from .module import Module
  from .activation import MultiheadAttention
  from .container import ModuleList
  from ..init import xavier_uniform_
  from .dropout import Dropout
  from .linear import Linear
  from .normalization import LayerNorm
label_desc: Use image node_id 2 for calling the TransformerEncoder obj's underlying member method code with example usage: obj.forward(src, mask, src_key_padding_mask, is_causal) and returns: output