class_docstr: A vanilla Transformer Encoder block
class_docstr_tok: ["A","vanilla","Transformer","Encoder","block"]
code: def __init__(self, config: xFormerEncoderConfig, **kwargs):
      super().__init__()
      deprecated_function(self)
  
      self.reversible_f = None
      self.reversible_g = None
      self.residual_norm_style = config.residual_norm_style
      self.dim_model = config.dim_model
  
      # If this layer is the first one, and a pose encoding has been requested
      if (
          config.position_encoding_config is not None
          and config.layer_position.is_first()
      ):
          self.pose_encoding = build_positional_embedding(
              asdict(config.position_encoding_config)
          )
  
          pos_encoding_dim = config.position_encoding_config.dim_model
          mha_dim = config.multi_head_config["dim_model"]
  
          if pos_encoding_dim != mha_dim:
              logger.warning(
                  f"The embedding dim and model dim do not match ({pos_encoding_dim} vs {mha_dim}), adding a projector layer."  # noqa
              )
              self.embedding_projector = nn.Linear(
                  pos_encoding_dim, mha_dim
              )
      else:
          self.pose_encoding = None
  
      if config.residual_norm_style == ResidualNormStyle.DeepNorm:
          # Just use the layer norm coefficient here,
          # the init will be handled at the xformers level (knows about encoder and decoder blocks)
          deep_norm_coefficients, _ = get_deepnorm_coefficients(
              encoder_layers=config.num_layers, decoder_layers=0
          )
          assert deep_norm_coefficients is not None
          residual_scale = deep_norm_coefficients.alpha
      else:
          residual_scale = 1.0
  
      # mini helper, builds a normalization layer with the right Pre/Post config, residuals, and the right dimensions
      ln_factory = _get_ln_factory(
          config.dim_model,
          config.residual_norm_style,
          use_triton=config.use_triton,
          residual=True,
          residual_scale=residual_scale,
          normalization=config.normalization,
      )
  
      mha = build_multi_head_attention(config.multi_head_config)
      feedforward = build_feedforward(asdict(config.feedforward_config))
  
      # Expose attention specific capabilities
      self.supports_attention_mask = (
          mha.attention.supports_attention_mask
      )
      self.requires_same_k_q_dimensions = (
          mha.attention.requires_same_k_q_dimensions
      )
      self.causal = (
          mha.attention.causal
          if hasattr(mha.attention, "causal")
          else False
      )
  
      # Wrappers handle the different layer norm styles (pre- and post-) and the residual path
      self.wrap_att = ln_factory(mha)
      self.wrap_ff: Union[Residual, PostNorm] = ln_factory(feedforward)
      if (
          config.residual_norm_style == ResidualNormStyle.Pre
          and config.layer_position.is_last()
      ):
          self.wrap_ff = PostNorm(
              config.dim_model,
              self.wrap_ff,
              normalization=config.normalization,
              use_triton=config.use_triton,
          )
  
      # Simplicial embeddings are only used if specified, and on the last layer
      self.simplicial_embedding: Optional[SimplicialEmbedding] = None
      if (
          config.simplicial_embeddings is not None
          and config.layer_position.is_last()
      ):
          self.simplicial_embedding = SimplicialEmbedding(
              **config.simplicial_embeddings
          )
  
      # Optional patch embedding
      self.patch_emb: Optional[nn.Module] = None
  
      if config.patch_embedding_config is not None:
          self.patch_emb = build_patch_embedding(
              PatchEmbeddingConfig(**config.patch_embedding_config)
          )
  
imports: import logging
  from dataclasses import asdict
  from typing import Optional, Tuple, Union
  import torch
  import torch.nn
  from xformers._deprecation_warning import deprecated_function
  from xformers.components import PatchEmbeddingConfig, PostNorm, PreNorm, Residual, ResidualNormStyle, build_multi_head_attention, build_patch_embedding
  from xformers.components.attention import AttentionMask
  from xformers.components.feedforward import build_feedforward
  from xformers.components.positional_embedding import build_positional_embedding
  from xformers.components.residual import get_deepnorm_coefficients
  from xformers.components.simplicial_embedding import SimplicialEmbedding
  from xformers.factory.block_configs import NormalizationType, xFormerDecoderConfig, xFormerEncoderConfig
label_desc: Use image node_id 1 to create a new xFormerEncoderBlock object from inherited base classes: torch.nn with example: obj = xFormerEncoderBlock(config)