class_docstr: Branchformer encoder module.
code: def __init__(
      self,
      input_size: int,
      output_size: int = 256,
      use_attn: bool = True,
      attention_heads: int = 4,
      attention_layer_type: str = "rel_selfattn",
      pos_enc_layer_type: str = "rel_pos",
      rel_pos_type: str = "latest",
      use_cgmlp: bool = True,
      cgmlp_linear_units: int = 2048,
      cgmlp_conv_kernel: int = 31,
      use_linear_after_conv: bool = False,
      gate_activation: str = "identity",
      merge_method: str = "concat",
      cgmlp_weight: Union[float, List[float]] = 0.5,
      attn_branch_drop_rate: Union[float, List[float]] = 0.0,
      num_blocks: int = 12,
      dropout_rate: float = 0.1,
      positional_dropout_rate: float = 0.1,
      attention_dropout_rate: float = 0.0,
      input_layer: Optional[str] = "conv2d",
      zero_triu: bool = False,
      padding_idx: int = -1,
      stochastic_depth_rate: Union[float, List[float]] = 0.0,
  ):
      assert check_argument_types()
      super().__init__()
      self._output_size = output_size
  
      if rel_pos_type == "legacy":
          if pos_enc_layer_type == "rel_pos":
              pos_enc_layer_type = "legacy_rel_pos"
          if attention_layer_type == "rel_selfattn":
              attention_layer_type = "legacy_rel_selfattn"
      elif rel_pos_type == "latest":
          assert attention_layer_type != "legacy_rel_selfattn"
          assert pos_enc_layer_type != "legacy_rel_pos"
      else:
          raise ValueError("unknown rel_pos_type: " + rel_pos_type)
  
      if pos_enc_layer_type == "abs_pos":
          pos_enc_class = PositionalEncoding
      elif pos_enc_layer_type == "scaled_abs_pos":
          pos_enc_class = ScaledPositionalEncoding
      elif pos_enc_layer_type == "rel_pos":
          assert attention_layer_type == "rel_selfattn"
          pos_enc_class = RelPositionalEncoding
      elif pos_enc_layer_type == "legacy_rel_pos":
          assert attention_layer_type == "legacy_rel_selfattn"
          pos_enc_class = LegacyRelPositionalEncoding
          logging.warning(
              "Using legacy_rel_pos and it will be deprecated in the future."
          )
      else:
          raise ValueError(
              "unknown pos_enc_layer: " + pos_enc_layer_type
          )
  
      if input_layer == "linear":
          self.embed = torch.nn.Sequential(
              torch.nn.Linear(input_size, output_size),
              torch.nn.LayerNorm(output_size),
              torch.nn.Dropout(dropout_rate),
              pos_enc_class(output_size, positional_dropout_rate),
          )
      elif input_layer == "conv2d":
          self.embed = Conv2dSubsampling(
              input_size,
              output_size,
              dropout_rate,
              pos_enc_class(output_size, positional_dropout_rate),
          )
      elif input_layer == "conv2d1":
          self.embed = Conv2dSubsampling1(
              input_size,
              output_size,
              dropout_rate,
              pos_enc_class(output_size, positional_dropout_rate),
          )
      elif input_layer == "conv2d2":
          self.embed = Conv2dSubsampling2(
              input_size,
              output_size,
              dropout_rate,
              pos_enc_class(output_size, positional_dropout_rate),
          )
      elif input_layer == "conv2d6":
          self.embed = Conv2dSubsampling6(
              input_size,
              output_size,
              dropout_rate,
              pos_enc_class(output_size, positional_dropout_rate),
          )
      elif input_layer == "conv2d8":
          self.embed = Conv2dSubsampling8(
              input_size,
              output_size,
              dropout_rate,
              pos_enc_class(output_size, positional_dropout_rate),
          )
      elif input_layer == "embed":
          self.embed = torch.nn.Sequential(
              torch.nn.Embedding(
                  input_size, output_size, padding_idx=padding_idx
              ),
              pos_enc_class(output_size, positional_dropout_rate),
          )
      elif isinstance(input_layer, torch.nn.Module):
          self.embed = torch.nn.Sequential(
              input_layer,
              pos_enc_class(output_size, positional_dropout_rate),
          )
      elif input_layer is None:
          if input_size == output_size:
              self.embed = None
          else:
              self.embed = torch.nn.Linear(input_size, output_size)
      else:
          raise ValueError("unknown input_layer: " + input_layer)
  
      if attention_layer_type == "selfattn":
          encoder_selfattn_layer = MultiHeadedAttention
          encoder_selfattn_layer_args = (
              attention_heads,
              output_size,
              attention_dropout_rate,
          )
      elif attention_layer_type == "legacy_rel_selfattn":
          assert pos_enc_layer_type == "legacy_rel_pos"
          encoder_selfattn_layer = LegacyRelPositionMultiHeadedAttention
          encoder_selfattn_layer_args = (
              attention_heads,
              output_size,
              attention_dropout_rate,
          )
          logging.warning(
              "Using legacy_rel_selfattn and it will be deprecated in the future."
          )
      elif attention_layer_type == "rel_selfattn":
          assert pos_enc_layer_type == "rel_pos"
          encoder_selfattn_layer = RelPositionMultiHeadedAttention
          encoder_selfattn_layer_args = (
              attention_heads,
              output_size,
              attention_dropout_rate,
              zero_triu,
          )
      elif attention_layer_type == "fast_selfattn":
          assert pos_enc_layer_type in ["abs_pos", "scaled_abs_pos"]
          encoder_selfattn_layer = FastSelfAttention
          encoder_selfattn_layer_args = (
              output_size,
              attention_heads,
              attention_dropout_rate,
          )
      else:
          raise ValueError(
              "unknown encoder_attn_layer: " + attention_layer_type
          )
  
      cgmlp_layer = ConvolutionalGatingMLP
      cgmlp_layer_args = (
          output_size,
          cgmlp_linear_units,
          cgmlp_conv_kernel,
          dropout_rate,
          use_linear_after_conv,
          gate_activation,
      )
  
      if isinstance(stochastic_depth_rate, float):
          stochastic_depth_rate = [stochastic_depth_rate] * num_blocks
      if len(stochastic_depth_rate) != num_blocks:
          raise ValueError(
              f"Length of stochastic_depth_rate ({len(stochastic_depth_rate)}) "
              f"should be equal to num_blocks ({num_blocks})"
          )
  
      if isinstance(cgmlp_weight, float):
          cgmlp_weight = [cgmlp_weight] * num_blocks
      if len(cgmlp_weight) != num_blocks:
          raise ValueError(
              f"Length of cgmlp_weight ({len(cgmlp_weight)}) should be equal to "
              f"num_blocks ({num_blocks})"
          )
  
      if isinstance(attn_branch_drop_rate, float):
          attn_branch_drop_rate = [attn_branch_drop_rate] * num_blocks
      if len(attn_branch_drop_rate) != num_blocks:
          raise ValueError(
              f"Length of attn_branch_drop_rate ({len(attn_branch_drop_rate)}) "
              f"should be equal to num_blocks ({num_blocks})"
          )
  
      self.encoders = repeat(
          num_blocks,
          lambda lnum: BranchformerEncoderLayer(
              output_size,
              encoder_selfattn_layer(*encoder_selfattn_layer_args)
              if use_attn
              else None,
              cgmlp_layer(*cgmlp_layer_args) if use_cgmlp else None,
              dropout_rate,
              merge_method,
              cgmlp_weight[lnum],
              attn_branch_drop_rate[lnum],
              stochastic_depth_rate[lnum],
          ),
      )
      self.after_norm = LayerNorm(output_size)
  
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 BranchformerEncoder object from inherited base classes: AbsEncoder with example: obj = BranchformerEncoder(input_size, output_size, use_attn, attention_heads, attention_layer_type, pos_enc_layer_type, rel_pos_type, use_cgmlp, cgmlp_linear_units, cgmlp_conv_kernel, use_linear_after_conv, gate_activation, merge_method, cgmlp_weight, attn_branch_drop_rate, num_blocks, dropout_rate, positional_dropout_rate, attention_dropout_rate, input_layer, zero_triu, padding_idx, stochastic_depth_rate)