class_docstr: Branchformer encoder layer module.
  
  Args:
      size (int): model dimension
      attn: standard self-attention or efficient attention, optional
      cgmlp: ConvolutionalGatingMLP, optional
      dropout_rate (float): dropout probability
      merge_method (str): concat, learned_ave, fixed_ave
      cgmlp_weight (float): weight of the cgmlp branch, between 0 and 1,
          used if merge_method is fixed_ave
      attn_branch_drop_rate (float): probability of dropping the attn branch,
          used if merge_method is learned_ave
      stochastic_depth_rate (float): stochastic depth probability
code: def __init__(
      self,
      size: int,
      attn: Optional[torch.nn.Module],
      cgmlp: Optional[torch.nn.Module],
      dropout_rate: float,
      merge_method: str,
      cgmlp_weight: float = 0.5,
      attn_branch_drop_rate: float = 0.0,
      stochastic_depth_rate: float = 0.0,
  ):
      super().__init__()
      assert (attn is not None) or (
          cgmlp is not None
      ), "At least one branch should be valid"
  
      self.size = size
      self.attn = attn
      self.cgmlp = cgmlp
      self.merge_method = merge_method
      self.cgmlp_weight = cgmlp_weight
      self.attn_branch_drop_rate = attn_branch_drop_rate
      self.stochastic_depth_rate = stochastic_depth_rate
      self.use_two_branches = (attn is not None) and (cgmlp is not None)
  
      if attn is not None:
          self.norm_mha = LayerNorm(size)  # for the MHA module
      if cgmlp is not None:
          self.norm_mlp = LayerNorm(size)  # for the MLP module
      self.norm_final = LayerNorm(
          size
      )  # for the final output of the block
  
      self.dropout = torch.nn.Dropout(dropout_rate)
  
      if self.use_two_branches:
          if merge_method == "concat":
              self.merge_proj = torch.nn.Linear(size + size, size)
  
          elif merge_method == "learned_ave":
              # attention-based pooling for two branches
              self.pooling_proj1 = torch.nn.Linear(size, 1)
              self.pooling_proj2 = torch.nn.Linear(size, 1)
  
              # linear projections for calculating merging weights
              self.weight_proj1 = torch.nn.Linear(size, 1)
              self.weight_proj2 = torch.nn.Linear(size, 1)
  
              # linear projection after weighted average
              self.merge_proj = torch.nn.Linear(size, size)
  
          elif merge_method == "fixed_ave":
              assert (
                  0.0 <= cgmlp_weight <= 1.0
              ), "cgmlp weight should be between 0.0 and 1.0"
  
              # remove the other branch if only one branch is used
              if cgmlp_weight == 0.0:
                  self.use_two_branches = False
                  self.cgmlp = None
                  self.norm_mlp = None
              elif cgmlp_weight == 1.0:
                  self.use_two_branches = False
                  self.attn = None
                  self.norm_mha = None
  
              # linear projection after weighted average
              self.merge_proj = torch.nn.Linear(size, size)
  
          else:
              raise ValueError(f"unknown merge method: {merge_method}")
  
      else:
          self.merge_proj = torch.nn.Identity()
  
imports: import logging
  from typing import List, Optional, Tuple, Union
  import numpy
  import torch
  from typeguard import check_argument_types
  from espnet2.asr.encoder.abs_encoder import AbsEncoder
  from espnet2.asr.layers.cgmlp import ConvolutionalGatingMLP
  from espnet2.asr.layers.fastformer import FastSelfAttention
  from espnet.nets.pytorch_backend.nets_utils import make_pad_mask
  from espnet.nets.pytorch_backend.transformer.attention import LegacyRelPositionMultiHeadedAttention, MultiHeadedAttention, RelPositionMultiHeadedAttention
  from espnet.nets.pytorch_backend.transformer.embedding import LegacyRelPositionalEncoding, PositionalEncoding, RelPositionalEncoding, ScaledPositionalEncoding
  from espnet.nets.pytorch_backend.transformer.layer_norm import LayerNorm
  from espnet.nets.pytorch_backend.transformer.repeat import repeat
  from espnet.nets.pytorch_backend.transformer.subsampling import Conv2dSubsampling, Conv2dSubsampling1, Conv2dSubsampling2, Conv2dSubsampling6, Conv2dSubsampling8, TooShortUttError, check_short_utt
label_desc: Use image node_id 1 to create a new BranchformerEncoderLayer object from inherited base classes: torch.nn with example: obj = BranchformerEncoderLayer(size, attn, cgmlp, dropout_rate, merge_method, cgmlp_weight, attn_branch_drop_rate, stochastic_depth_rate)