class_docstr: Feed Forward Transformer for TTS a.k.a. FastSpeech.
  
  This is a module of FastSpeech,
  feed-forward Transformer with duration predictor described in
  `FastSpeech: Fast, Robust and Controllable Text to Speech`_,
  which does not require any auto-regressive
  processing during inference,
  resulting in fast decoding compared with auto-regressive Transformer.
  
  .. _`FastSpeech: Fast, Robust and Controllable Text to Speech`:
      https://arxiv.org/pdf/1905.09263.pdf
class_docstr_tok: ["Feed","Forward","Transformer","for","TTS","a.k.a",".","FastSpeech",".","This","is","a","module","of","FastSpeech",",","feed-forward","Transformer","with","duration","predictor","described","in","`","FastSpeech",":","Fast",",","Robust","and","Controllable","Text","to","Speech","`","_",",","which","does","not","require","any","auto-regressive","processing","during","inference",",","resulting","in","fast","decoding","compared","with","auto-regressive","Transformer",".","..","_","`","FastSpeech",":","Fast",",","Robust","and","Controllable","Text","to","Speech","`",":","https",":","\/\/arxiv.org\/pdf\/1905.09263.pdf"]
docstr: Initialize feed-forward Transformer module.
  
  Args:
      idim (int): Dimension of the inputs.
      odim (int): Dimension of the outputs.
      args (Namespace, optional):
          - elayers (int): Number of encoder layers.
          - eunits (int): Number of encoder hidden units.
          - adim (int): Number of attention transformation dimensions.
          - aheads (int): Number of heads for multi head attention.
          - dlayers (int): Number of decoder layers.
          - dunits (int): Number of decoder hidden units.
          - use_scaled_pos_enc (bool):
              Whether to use trainable scaled positional encoding.
          - encoder_normalize_before (bool):
              Whether to perform layer normalization before encoder block.
          - decoder_normalize_before (bool):
              Whether to perform layer normalization before decoder block.
          - encoder_concat_after (bool): Whether to concatenate attention
              layer's input and output in encoder.
          - decoder_concat_after (bool): Whether to concatenate attention
              layer's input and output in decoder.
          - duration_predictor_layers (int): Number of duration predictor layers.
          - duration_predictor_chans (int): Number of duration predictor channels.
          - duration_predictor_kernel_size (int):
              Kernel size of duration predictor.
          - spk_embed_dim (int): Number of speaker embedding dimensions.
          - spk_embed_integration_type: How to integrate speaker embedding.
          - teacher_model (str): Teacher auto-regressive transformer model path.
          - reduction_factor (int): Reduction factor.
          - transformer_init (float): How to initialize transformer parameters.
          - transformer_lr (float): Initial value of learning rate.
          - transformer_warmup_steps (int): Optimizer warmup steps.
          - transformer_enc_dropout_rate (float):
              Dropout rate in encoder except attention & positional encoding.
          - transformer_enc_positional_dropout_rate (float):
              Dropout rate after encoder positional encoding.
          - transformer_enc_attn_dropout_rate (float):
              Dropout rate in encoder self-attention module.
          - transformer_dec_dropout_rate (float):
              Dropout rate in decoder except attention & positional encoding.
          - transformer_dec_positional_dropout_rate (float):
              Dropout rate after decoder positional encoding.
          - transformer_dec_attn_dropout_rate (float):
              Dropout rate in deocoder self-attention module.
          - transformer_enc_dec_attn_dropout_rate (float):
              Dropout rate in encoder-deocoder attention module.
          - use_masking (bool):
              Whether to apply masking for padded part in loss calculation.
          - use_weighted_masking (bool):
              Whether to apply weighted masking in loss calculation.
          - transfer_encoder_from_teacher:
              Whether to transfer encoder using teacher encoder parameters.
          - transferred_encoder_module:
              Encoder module to be initialized using teacher parameters.
docstr_tok: ["Initialize","feed-forward","Transformer","module",".","Args",":","idim","(","int",")",":","Dimension","of","the","inputs",".","odim","(","int",")",":","Dimension","of","the","outputs",".","args","(","Namespace",",","optional",")",":","-","elayers","(","int",")",":","Number","of","encoder","layers",".","-","eunits","(","int",")",":","Number","of","encoder","hidden","units",".","-","adim","(","int",")",":","Number","of","attention","transformation","dimensions",".","-","aheads","(","int",")",":","Number","of","heads","for","multi","head","attention",".","-","dlayers","(","int",")",":","Number","of","decoder","layers",".","-","dunits","(","int",")",":","Number","of","decoder","hidden","units",".","-","use_scaled_pos_enc","(","bool",")",":","Whether","to","use","trainable","scaled","positional","encoding",".","-","encoder_normalize_before","(","bool",")",":","Whether","to","perform","layer","normalization","before","encoder","block",".","-","decoder_normalize_before","(","bool",")",":","Whether","to","perform","layer","normalization","before","decoder","block",".","-","encoder_concat_after","(","bool",")",":","Whether","to","concatenate","attention","layer","'s","input","and","output","in","encoder",".","-","decoder_concat_after","(","bool",")",":","Whether","to","concatenate","attention","layer","'s","input","and","output","in","decoder",".","-","duration_predictor_layers","(","int",")",":","Number","of","duration","predictor","layers",".","-","duration_predictor_chans","(","int",")",":","Number","of","duration","predictor","channels",".","-","duration_predictor_kernel_size","(","int",")",":","Kernel","size","of","duration","predictor",".","-","spk_embed_dim","(","int",")",":","Number","of","speaker","embedding","dimensions",".","-","spk_embed_integration_type",":","How","to","integrate","speaker","embedding",".","-","teacher_model","(","str",")",":","Teacher","auto-regressive","transformer","model","path",".","-","reduction_factor","(","int",")",":","Reduction","factor",".","-","transformer_init","(","float",")",":","How","to","initialize","transformer","parameters",".","-","transformer_lr","(","float",")",":","Initial","value","of","learning","rate",".","-","transformer_warmup_steps","(","int",")",":","Optimizer","warmup","steps",".","-","transformer_enc_dropout_rate","(","float",")",":","Dropout","rate","in","encoder","except","attention","&","positional","encoding",".","-","transformer_enc_positional_dropout_rate","(","float",")",":","Dropout","rate","after","encoder","positional","encoding",".","-","transformer_enc_attn_dropout_rate","(","float",")",":","Dropout","rate","in","encoder","self-attention","module",".","-","transformer_dec_dropout_rate","(","float",")",":","Dropout","rate","in","decoder","except","attention","&","positional","encoding",".","-","transformer_dec_positional_dropout_rate","(","float",")",":","Dropout","rate","after","decoder","positional","encoding",".","-","transformer_dec_attn_dropout_rate","(","float",")",":","Dropout","rate","in","deocoder","self-attention","module",".","-","transformer_enc_dec_attn_dropout_rate","(","float",")",":","Dropout","rate","in","encoder-deocoder","attention","module",".","-","use_masking","(","bool",")",":","Whether","to","apply","masking","for","padded","part","in","loss","calculation",".","-","use_weighted_masking","(","bool",")",":","Whether","to","apply","weighted","masking","in","loss","calculation",".","-","transfer_encoder_from_teacher",":","Whether","to","transfer","encoder","using","teacher","encoder","parameters",".","-","transferred_encoder_module",":","Encoder","module","to","be","initialized","using","teacher","parameters","."]
code: def __init__(self, idim, odim, args=None):
      """Initialize feed-forward Transformer module.
  
      Args:
          idim (int): Dimension of the inputs.
          odim (int): Dimension of the outputs.
          args (Namespace, optional):
              - elayers (int): Number of encoder layers.
              - eunits (int): Number of encoder hidden units.
              - adim (int): Number of attention transformation dimensions.
              - aheads (int): Number of heads for multi head attention.
              - dlayers (int): Number of decoder layers.
              - dunits (int): Number of decoder hidden units.
              - use_scaled_pos_enc (bool):
                  Whether to use trainable scaled positional encoding.
              - encoder_normalize_before (bool):
                  Whether to perform layer normalization before encoder block.
              - decoder_normalize_before (bool):
                  Whether to perform layer normalization before decoder block.
              - encoder_concat_after (bool): Whether to concatenate attention
                  layer's input and output in encoder.
              - decoder_concat_after (bool): Whether to concatenate attention
                  layer's input and output in decoder.
              - duration_predictor_layers (int): Number of duration predictor layers.
              - duration_predictor_chans (int): Number of duration predictor channels.
              - duration_predictor_kernel_size (int):
                  Kernel size of duration predictor.
              - spk_embed_dim (int): Number of speaker embedding dimensions.
              - spk_embed_integration_type: How to integrate speaker embedding.
              - teacher_model (str): Teacher auto-regressive transformer model path.
              - reduction_factor (int): Reduction factor.
              - transformer_init (float): How to initialize transformer parameters.
              - transformer_lr (float): Initial value of learning rate.
              - transformer_warmup_steps (int): Optimizer warmup steps.
              - transformer_enc_dropout_rate (float):
                  Dropout rate in encoder except attention & positional encoding.
              - transformer_enc_positional_dropout_rate (float):
                  Dropout rate after encoder positional encoding.
              - transformer_enc_attn_dropout_rate (float):
                  Dropout rate in encoder self-attention module.
              - transformer_dec_dropout_rate (float):
                  Dropout rate in decoder except attention & positional encoding.
              - transformer_dec_positional_dropout_rate (float):
                  Dropout rate after decoder positional encoding.
              - transformer_dec_attn_dropout_rate (float):
                  Dropout rate in deocoder self-attention module.
              - transformer_enc_dec_attn_dropout_rate (float):
                  Dropout rate in encoder-deocoder attention module.
              - use_masking (bool):
                  Whether to apply masking for padded part in loss calculation.
              - use_weighted_masking (bool):
                  Whether to apply weighted masking in loss calculation.
              - transfer_encoder_from_teacher:
                  Whether to transfer encoder using teacher encoder parameters.
              - transferred_encoder_module:
                  Encoder module to be initialized using teacher parameters.
  
      """
      # initialize base classes
      TTSInterface.__init__(self)
      torch.nn.Module.__init__(self)
  
      # fill missing arguments
      args = fill_missing_args(args, self.add_arguments)
  
      # store hyperparameters
      self.idim = idim
      self.odim = odim
      self.reduction_factor = args.reduction_factor
      self.use_scaled_pos_enc = args.use_scaled_pos_enc
      self.spk_embed_dim = args.spk_embed_dim
      if self.spk_embed_dim is not None:
          self.spk_embed_integration_type = (
              args.spk_embed_integration_type
          )
  
      # use idx 0 as padding idx
      padding_idx = 0
  
      # get positional encoding class
      pos_enc_class = (
          ScaledPositionalEncoding
          if self.use_scaled_pos_enc
          else PositionalEncoding
      )
  
      # define encoder
      encoder_input_layer = torch.nn.Embedding(
          num_embeddings=idim,
          embedding_dim=args.adim,
          padding_idx=padding_idx,
      )
      self.encoder = Encoder(
          idim=idim,
          attention_dim=args.adim,
          attention_heads=args.aheads,
          linear_units=args.eunits,
          num_blocks=args.elayers,
          input_layer=encoder_input_layer,
          dropout_rate=args.transformer_enc_dropout_rate,
          positional_dropout_rate=args.transformer_enc_positional_dropout_rate,
          attention_dropout_rate=args.transformer_enc_attn_dropout_rate,
          pos_enc_class=pos_enc_class,
          normalize_before=args.encoder_normalize_before,
          concat_after=args.encoder_concat_after,
          positionwise_layer_type=args.positionwise_layer_type,
          positionwise_conv_kernel_size=args.positionwise_conv_kernel_size,
      )
  
      # define additional projection for speaker embedding
      if self.spk_embed_dim is not None:
          if self.spk_embed_integration_type == "add":
              self.projection = torch.nn.Linear(
                  self.spk_embed_dim, args.adim
              )
          else:
              self.projection = torch.nn.Linear(
                  args.adim + self.spk_embed_dim, args.adim
              )
  
      # define duration predictor
      self.duration_predictor = DurationPredictor(
          idim=args.adim,
          n_layers=args.duration_predictor_layers,
          n_chans=args.duration_predictor_chans,
          kernel_size=args.duration_predictor_kernel_size,
          dropout_rate=args.duration_predictor_dropout_rate,
      )
  
      # define length regulator
      self.length_regulator = LengthRegulator()
  
      # define decoder
      # NOTE: we use encoder as decoder
      # because fastspeech's decoder is the same as encoder
      self.decoder = Encoder(
          idim=0,
          attention_dim=args.adim,
          attention_heads=args.aheads,
          linear_units=args.dunits,
          num_blocks=args.dlayers,
          input_layer=None,
          dropout_rate=args.transformer_dec_dropout_rate,
          positional_dropout_rate=args.transformer_dec_positional_dropout_rate,
          attention_dropout_rate=args.transformer_dec_attn_dropout_rate,
          pos_enc_class=pos_enc_class,
          normalize_before=args.decoder_normalize_before,
          concat_after=args.decoder_concat_after,
          positionwise_layer_type=args.positionwise_layer_type,
          positionwise_conv_kernel_size=args.positionwise_conv_kernel_size,
      )
  
      # define final projection
      self.feat_out = torch.nn.Linear(
          args.adim, odim * args.reduction_factor
      )
  
      # define postnet
      self.postnet = (
          None
          if args.postnet_layers == 0
          else Postnet(
              idim=idim,
              odim=odim,
              n_layers=args.postnet_layers,
              n_chans=args.postnet_chans,
              n_filts=args.postnet_filts,
              use_batch_norm=args.use_batch_norm,
              dropout_rate=args.postnet_dropout_rate,
          )
      )
  
      # initialize parameters
      self._reset_parameters(
          init_type=args.transformer_init,
          init_enc_alpha=args.initial_encoder_alpha,
          init_dec_alpha=args.initial_decoder_alpha,
      )
  
      # define teacher model
      if args.teacher_model is not None:
          self.teacher = self._load_teacher_model(args.teacher_model)
      else:
          self.teacher = None
  
      # define duration calculator
      if self.teacher is not None:
          self.duration_calculator = DurationCalculator(self.teacher)
      else:
          self.duration_calculator = None
  
      # transfer teacher parameters
      if (
          self.teacher is not None
          and args.transfer_encoder_from_teacher
      ):
          self._transfer_from_teacher(args.transferred_encoder_module)
  
      # define criterions
      self.criterion = FeedForwardTransformerLoss(
          use_masking=args.use_masking,
          use_weighted_masking=args.use_weighted_masking,
      )
  
imports: import logging
  import torch
  import torch.nn.functional
  from espnet.asr.asr_utils import get_model_conf, torch_load
  from espnet.nets.pytorch_backend.fastspeech.duration_calculator import DurationCalculator
  from espnet.nets.pytorch_backend.fastspeech.duration_predictor import DurationPredictor, DurationPredictorLoss
  from espnet.nets.pytorch_backend.fastspeech.length_regulator import LengthRegulator
  from espnet.nets.pytorch_backend.nets_utils import make_non_pad_mask, make_pad_mask
  from espnet.nets.pytorch_backend.tacotron2.decoder import Postnet
  from espnet.nets.pytorch_backend.transformer.attention import MultiHeadedAttention
  from espnet.nets.pytorch_backend.transformer.embedding import PositionalEncoding, ScaledPositionalEncoding
  from espnet.nets.pytorch_backend.transformer.encoder import Encoder
  from espnet.nets.pytorch_backend.transformer.initializer import initialize
  from espnet.nets.tts_interface import TTSInterface
  from espnet.utils.cli_utils import strtobool
  from espnet.utils.fill_missing_args import fill_missing_args
label_desc: Use image node_id 2 to create a new FeedForwardTransformer object from inherited base classes: TTSInterface, torch.nn with example: obj = FeedForwardTransformer(idim, odim, args)