class_docstr: FastSpeech2 module.
  
  This is a module of FastSpeech2 described in `FastSpeech 2: Fast and
  High-Quality End-to-End Text to Speech`_. Instead of quantized pitch and
  energy, we use token-averaged value introduced in `FastPitch: Parallel
  Text-to-speech with Pitch Prediction`_.
  
  .. _`FastSpeech 2: Fast and High-Quality End-to-End Text to Speech`:
      https://arxiv.org/abs/2006.04558
  .. _`FastPitch: Parallel Text-to-speech with Pitch Prediction`:
      https://arxiv.org/abs/2006.06873
class_docstr_tok: ["FastSpeech2","module",".","This","is","a","module","of","FastSpeech2","described","in","`","FastSpeech","2",":","Fast","and","High-Quality","End-to-End","Text","to","Speech","`","_",".","Instead","of","quantized","pitch","and","energy",",","we","use","token-averaged","value","introduced","in","`","FastPitch",":","Parallel","Text-to-speech","with","Pitch","Prediction","`","_",".","..","_","`","FastSpeech","2",":","Fast","and","High-Quality","End-to-End","Text","to","Speech","`",":","https",":","\/\/arxiv.org\/abs\/2006.04558","..","_","`","FastPitch",":","Parallel","Text-to-speech","with","Pitch","Prediction","`",":","https",":","\/\/arxiv.org\/abs\/2006.06873"]
docstr: Calculate forward propagation.
  
  Args:
      text (LongTensor): Batch of padded token ids (B, T_text).
      text_lengths (LongTensor): Batch of lengths of each input (B,).
      feats (Tensor): Batch of padded target features (B, T_feats, odim).
      feats_lengths (LongTensor): Batch of the lengths of each target (B,).
      durations (LongTensor): Batch of padded durations (B, T_text + 1).
      durations_lengths (LongTensor): Batch of duration lengths (B, T_text + 1).
      pitch (Tensor): Batch of padded token-averaged pitch (B, T_text + 1, 1).
      pitch_lengths (LongTensor): Batch of pitch lengths (B, T_text + 1).
      energy (Tensor): Batch of padded token-averaged energy (B, T_text + 1, 1).
      energy_lengths (LongTensor): Batch of energy lengths (B, T_text + 1).
      spembs (Optional[Tensor]): Batch of speaker embeddings (B, spk_embed_dim).
      sids (Optional[Tensor]): Batch of speaker IDs (B, 1).
      lids (Optional[Tensor]): Batch of language IDs (B, 1).
      joint_training (bool): Whether to perform joint training with vocoder.
  
  Returns:
      Tensor: Loss scalar value.
      Dict: Statistics to be monitored.
      Tensor: Weight value if not joint training else model outputs.
docstr_tok: ["Calculate","forward","propagation",".","Args",":","text","(","LongTensor",")",":","Batch","of","padded","token","ids","(","B",",","T_text",")",".","text_lengths","(","LongTensor",")",":","Batch","of","lengths","of","each","input","(","B",",",")",".","feats","(","Tensor",")",":","Batch","of","padded","target","features","(","B",",","T_feats",",","odim",")",".","feats_lengths","(","LongTensor",")",":","Batch","of","the","lengths","of","each","target","(","B",",",")",".","durations","(","LongTensor",")",":","Batch","of","padded","durations","(","B",",","T_text","+","1",")",".","durations_lengths","(","LongTensor",")",":","Batch","of","duration","lengths","(","B",",","T_text","+","1",")",".","pitch","(","Tensor",")",":","Batch","of","padded","token-averaged","pitch","(","B",",","T_text","+","1",",","1",")",".","pitch_lengths","(","LongTensor",")",":","Batch","of","pitch","lengths","(","B",",","T_text","+","1",")",".","energy","(","Tensor",")",":","Batch","of","padded","token-averaged","energy","(","B",",","T_text","+","1",",","1",")",".","energy_lengths","(","LongTensor",")",":","Batch","of","energy","lengths","(","B",",","T_text","+","1",")",".","spembs","(","Optional","[","Tensor","]",")",":","Batch","of","speaker","embeddings","(","B",",","spk_embed_dim",")",".","sids","(","Optional","[","Tensor","]",")",":","Batch","of","speaker","IDs","(","B",",","1",")",".","lids","(","Optional","[","Tensor","]",")",":","Batch","of","language","IDs","(","B",",","1",")",".","joint_training","(","bool",")",":","Whether","to","perform","joint","training","with","vocoder",".","Returns",":","Tensor",":","Loss","scalar","value",".","Dict",":","Statistics","to","be","monitored",".","Tensor",":","Weight","value","if","not","joint","training","else","model","outputs","."]
code: def forward(
      self,
      text: torch.Tensor,
      text_lengths: torch.Tensor,
      feats: torch.Tensor,
      feats_lengths: torch.Tensor,
      durations: torch.Tensor,
      durations_lengths: torch.Tensor,
      pitch: torch.Tensor,
      pitch_lengths: torch.Tensor,
      energy: torch.Tensor,
      energy_lengths: torch.Tensor,
      spembs: Optional[torch.Tensor] = None,
      sids: Optional[torch.Tensor] = None,
      lids: Optional[torch.Tensor] = None,
      joint_training: bool = False,
  ) -> Tuple[torch.Tensor, Dict[str, torch.Tensor], torch.Tensor]:
      """Calculate forward propagation.
  
      Args:
          text (LongTensor): Batch of padded token ids (B, T_text).
          text_lengths (LongTensor): Batch of lengths of each input (B,).
          feats (Tensor): Batch of padded target features (B, T_feats, odim).
          feats_lengths (LongTensor): Batch of the lengths of each target (B,).
          durations (LongTensor): Batch of padded durations (B, T_text + 1).
          durations_lengths (LongTensor): Batch of duration lengths (B, T_text + 1).
          pitch (Tensor): Batch of padded token-averaged pitch (B, T_text + 1, 1).
          pitch_lengths (LongTensor): Batch of pitch lengths (B, T_text + 1).
          energy (Tensor): Batch of padded token-averaged energy (B, T_text + 1, 1).
          energy_lengths (LongTensor): Batch of energy lengths (B, T_text + 1).
          spembs (Optional[Tensor]): Batch of speaker embeddings (B, spk_embed_dim).
          sids (Optional[Tensor]): Batch of speaker IDs (B, 1).
          lids (Optional[Tensor]): Batch of language IDs (B, 1).
          joint_training (bool): Whether to perform joint training with vocoder.
  
      Returns:
          Tensor: Loss scalar value.
          Dict: Statistics to be monitored.
          Tensor: Weight value if not joint training else model outputs.
  
      """
      text = text[:, : text_lengths.max()]  # for data-parallel
      feats = feats[:, : feats_lengths.max()]  # for data-parallel
      durations = durations[
          :, : durations_lengths.max()
      ]  # for data-parallel
      pitch = pitch[:, : pitch_lengths.max()]  # for data-parallel
      energy = energy[:, : energy_lengths.max()]  # for data-parallel
  
      batch_size = text.size(0)
  
      # Add eos at the last of sequence
      xs = F.pad(text, [0, 1], "constant", self.padding_idx)
      for i, l in enumerate(text_lengths):
          xs[i, l] = self.eos
      ilens = text_lengths + 1
  
      ys, ds, ps, es = feats, durations, pitch, energy
      olens = feats_lengths
  
      # forward propagation
      before_outs, after_outs, d_outs, p_outs, e_outs = self._forward(
          xs,
          ilens,
          ys,
          olens,
          ds,
          ps,
          es,
          spembs=spembs,
          sids=sids,
          lids=lids,
          is_inference=False,
      )
  
      # modify mod part of groundtruth
      if self.reduction_factor > 1:
          olens = olens.new(
              [olen - olen % self.reduction_factor for olen in olens]
          )
          max_olen = max(olens)
          ys = ys[:, :max_olen]
  
      # calculate loss
      if self.postnet is None:
          after_outs = None
  
      # calculate loss
      l1_loss, duration_loss, pitch_loss, energy_loss = self.criterion(
          after_outs=after_outs,
          before_outs=before_outs,
          d_outs=d_outs,
          p_outs=p_outs,
          e_outs=e_outs,
          ys=ys,
          ds=ds,
          ps=ps,
          es=es,
          ilens=ilens,
          olens=olens,
      )
      loss = l1_loss + duration_loss + pitch_loss + energy_loss
  
      stats = dict(
          l1_loss=l1_loss.item(),
          duration_loss=duration_loss.item(),
          pitch_loss=pitch_loss.item(),
          energy_loss=energy_loss.item(),
      )
  
      # report extra information
      if self.encoder_type == "transformer" and self.use_scaled_pos_enc:
          stats.update(
              encoder_alpha=self.encoder.embed[-1].alpha.data.item(),
          )
      if self.decoder_type == "transformer" and self.use_scaled_pos_enc:
          stats.update(
              decoder_alpha=self.decoder.embed[-1].alpha.data.item(),
          )
  
      if not joint_training:
          stats.update(loss=loss.item())
          loss, stats, weight = force_gatherable(
              (loss, stats, batch_size), loss.device
          )
          return loss, stats, weight
      else:
          return (
              loss,
              stats,
              after_outs if after_outs is not None else before_outs,
          )
  
imports: import logging
  from typing import Dict, Optional, Sequence, Tuple
  import torch
  import torch.nn.functional
  from typeguard import check_argument_types
  from espnet2.torch_utils.device_funcs import force_gatherable
  from espnet2.torch_utils.initialize import initialize
  from espnet2.tts.abs_tts import AbsTTS
  from espnet2.tts.fastspeech2.loss import FastSpeech2Loss
  from espnet2.tts.fastspeech2.variance_predictor import VariancePredictor
  from espnet2.tts.gst.style_encoder import StyleEncoder
  from espnet.nets.pytorch_backend.conformer.encoder import Encoder
  from espnet.nets.pytorch_backend.fastspeech.duration_predictor import DurationPredictor
  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.embedding import PositionalEncoding, ScaledPositionalEncoding
  from espnet.nets.pytorch_backend.transformer.encoder import Encoder
label_desc: Use image node_id 2 for calling the FastSpeech2 obj's underlying member method code with example usage: obj.forward(text, text_lengths, feats, feats_lengths, durations, durations_lengths, pitch, pitch_lengths, energy, energy_lengths, spembs, sids, lids, joint_training) and returns: loss,  stats,  weight, loss,  stats,  unknown