text: import torch
  
  from espnet.nets.pytorch_backend.transformer.attention import MultiHeadedAttention
  from espnet.nets.pytorch_backend.transformer.embedding import PositionalEncoding
  from espnet.nets.pytorch_backend.transformer.encoder_layer import EncoderLayer
  from espnet.nets.pytorch_backend.transformer.layer_norm import LayerNorm
  from espnet.nets.pytorch_backend.transformer.multi_layer_conv import MultiLayeredConv1d
  from espnet.nets.pytorch_backend.transformer.positionwise_feed_forward import PositionwiseFeedForward
  from espnet.nets.pytorch_backend.transformer.repeat import repeat
  from espnet.nets.pytorch_backend.transformer.subsampling import Conv2dSubsampling
  
  
  class Encoder(torch.nn.Module):
      """Transformer encoder module
  
      :param int idim: input dim
      :param int attention_dim: dimention of attention
      :param int attention_heads: the number of heads of multi head attention
      :param int linear_units: the number of units of position-wise feed forward
      :param int num_blocks: the number of decoder blocks
      :param float dropout_rate: dropout rate
      :param float attention_dropout_rate: dropout rate in attention
      :param float positional_dropout_rate: dropout rate after adding positional encoding
      :param str or torch.nn.Module input_layer: input layer type
      :param class pos_enc_class: PositionalEncoding or ScaledPositionalEncoding
      :param bool normalize_before: whether to use layer_norm before the first block
      :param bool concat_after: whether to concat attention layer's input and output
          if True, additional linear will be applied. i.e. x -> x + linear(concat(x, att(x)))
          if False, no additional linear will be applied. i.e. x -> x + att(x)
      :param str positionwise_layer_type: linear of conv1d
      :param int positionwise_conv_kernel_size: kernel size of positionwise conv1d layer
      :param int padding_idx: padding_idx for input_layer=embed
      """
  
      def __init__(self, idim,
                   attention_dim=256,
                   attention_heads=4,
                   linear_units=2048,
                   num_blocks=6,
                   dropout_rate=0.1,
                   positional_dropout_rate=0.1,
                   attention_dropout_rate=0.0,
                   input_layer="conv2d",
                   pos_enc_class=PositionalEncoding,
                   normalize_before=True,
                   concat_after=False,
                   positionwise_layer_type="linear",
                   positionwise_conv_kernel_size=1,
                   padding_idx=-1):
          super(Encoder, self).__init__()
          if input_layer == "linear":
              self.embed = torch.nn.Sequential(
                  torch.nn.Linear(idim, attention_dim),
                  torch.nn.LayerNorm(attention_dim),
                  torch.nn.Dropout(dropout_rate),
                  torch.nn.ReLU(),
                  pos_enc_class(attention_dim, positional_dropout_rate)
              )
          elif input_layer == "conv2d":
              self.embed = Conv2dSubsampling(idim, attention_dim, dropout_rate)
          elif input_layer == "embed":
              self.embed = torch.nn.Sequential(
                  torch.nn.Embedding(idim, attention_dim, padding_idx=padding_idx),
                  pos_enc_class(attention_dim, positional_dropout_rate)
              )
          elif isinstance(input_layer, torch.nn.Module):
              self.embed = torch.nn.Sequential(
                  input_layer,
                  pos_enc_class(attention_dim, positional_dropout_rate),
              )
          elif input_layer is None:
              self.embed = torch.nn.Sequential(
                  pos_enc_class(attention_dim, positional_dropout_rate)
              )
          else:
              raise ValueError("unknown input_layer: " + input_layer)
          self.normalize_before = normalize_before
          if positionwise_layer_type == "linear":
              positionwise_layer = PositionwiseFeedForward
              positionwise_layer_args = (attention_dim, linear_units, dropout_rate)
          elif positionwise_layer_type == "conv1d":
              positionwise_layer = MultiLayeredConv1d
              positionwise_layer_args = (attention_dim, linear_units, positionwise_conv_kernel_size, dropout_rate)
          else:
              raise NotImplementedError("Support only linear or conv1d.")
          self.encoders = repeat(
              num_blocks,
              lambda: EncoderLayer(
                  attention_dim,
                  MultiHeadedAttention(attention_heads, attention_dim, attention_dropout_rate),
                  positionwise_layer(*positionwise_layer_args),
                  dropout_rate,
                  normalize_before,
                  concat_after
              )
          )
          if self.normalize_before:
              self.after_norm = LayerNorm(attention_dim)
  
      def forward(self, xs, masks):
          """Embed positions in tensor
  
          :param torch.Tensor xs: input tensor
          :param torch.Tensor masks: input mask
          :return: position embedded tensor and mask
          :rtype Tuple[torch.Tensor, torch.Tensor]:
          """
          if isinstance(self.embed, Conv2dSubsampling):
              xs, masks = self.embed(xs, masks)
          else:
              xs = self.embed(xs)
          xs, masks = self.encoders(xs, masks)
          if self.normalize_before:
              xs = self.after_norm(xs)
          return xs, masks
  