class_docstr: Tacotron2 module for end-to-end text-to-speech.
  
  This is a module of Spectrogram prediction network in Tacotron2 described
  in `Natural TTS Synthesis by Conditioning WaveNet on Mel Spectrogram Predictions`_,
  which converts the sequence of characters into the sequence of Mel-filterbanks.
  
  .. _`Natural TTS Synthesis by Conditioning WaveNet on Mel Spectrogram Predictions`:
     https://arxiv.org/abs/1712.05884
class_docstr_tok: ["Tacotron2","module","for","end-to-end","text-to-speech",".","This","is","a","module","of","Spectrogram","prediction","network","in","Tacotron2","described","in","`","Natural","TTS","Synthesis","by","Conditioning","WaveNet","on","Mel","Spectrogram","Predictions","`","_",",","which","converts","the","sequence","of","characters","into","the","sequence","of","Mel-filterbanks",".","..","_","`","Natural","TTS","Synthesis","by","Conditioning","WaveNet","on","Mel","Spectrogram","Predictions","`",":","https",":","\/\/arxiv.org\/abs\/1712.05884"]
docstr: Calculate forward propagation.
  
  Args:
      text (LongTensor): Batch of padded character ids (B, T_text).
      text_lengths (LongTensor): Batch of lengths of each input batch (B,).
      feats (Tensor): Batch of padded target features (B, T_feats, odim).
      feats_lengths (LongTensor): Batch of the lengths of each target (B,).
      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","character","ids","(","B",",","T_text",")",".","text_lengths","(","LongTensor",")",":","Batch","of","lengths","of","each","input","batch","(","B",",",")",".","feats","(","Tensor",")",":","Batch","of","padded","target","features","(","B",",","T_feats",",","odim",")",".","feats_lengths","(","LongTensor",")",":","Batch","of","the","lengths","of","each","target","(","B",",",")",".","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,
      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 character ids (B, T_text).
          text_lengths (LongTensor): Batch of lengths of each input batch (B,).
          feats (Tensor): Batch of padded target features (B, T_feats, odim).
          feats_lengths (LongTensor): Batch of the lengths of each target (B,).
          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
  
      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 = feats
      olens = feats_lengths
  
      # make labels for stop prediction
      labels = make_pad_mask(olens - 1).to(ys.device, ys.dtype)
      labels = F.pad(labels, [0, 1], "constant", 1.0)
  
      # calculate tacotron2 outputs
      after_outs, before_outs, logits, att_ws = self._forward(
          xs=xs,
          ilens=ilens,
          ys=ys,
          olens=olens,
          spembs=spembs,
          sids=sids,
          lids=lids,
      )
  
      # modify mod part of groundtruth
      if self.reduction_factor > 1:
          assert olens.ge(
              self.reduction_factor
          ).all(), "Output length must be greater than or equal to reduction factor."
          olens = olens.new(
              [olen - olen % self.reduction_factor for olen in olens]
          )
          max_out = max(olens)
          ys = ys[:, :max_out]
          labels = labels[:, :max_out]
          labels = torch.scatter(
              labels, 1, (olens - 1).unsqueeze(1), 1.0
          )  # see #3388
  
      # calculate taco2 loss
      l1_loss, mse_loss, bce_loss = self.taco2_loss(
          after_outs, before_outs, logits, ys, labels, olens
      )
      if self.loss_type == "L1+L2":
          loss = l1_loss + mse_loss + bce_loss
      elif self.loss_type == "L1":
          loss = l1_loss + bce_loss
      elif self.loss_type == "L2":
          loss = mse_loss + bce_loss
      else:
          raise ValueError(f"unknown --loss-type {self.loss_type}")
  
      stats = dict(
          l1_loss=l1_loss.item(),
          mse_loss=mse_loss.item(),
          bce_loss=bce_loss.item(),
      )
  
      # calculate attention loss
      if self.use_guided_attn_loss:
          # NOTE(kan-bayashi): length of output for auto-regressive
          # input will be changed when r > 1
          if self.reduction_factor > 1:
              olens_in = olens.new(
                  [
                      torch.div(
                          olen,
                          self.reduction_factor,
                          rounding_mode="trunc",
                      )
                      for olen in olens
                  ]
              )
          else:
              olens_in = olens
          attn_loss = self.attn_loss(att_ws, ilens, olens_in)
          loss = loss + attn_loss
          stats.update(attn_loss=attn_loss.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
  
file_path: espnet/espnet2/tts/tacotron2/tacotron2.py
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.tts.abs_tts import AbsTTS
  from espnet2.tts.gst.style_encoder import StyleEncoder
  from espnet.nets.pytorch_backend.e2e_tts_tacotron2 import GuidedAttentionLoss, Tacotron2Loss
  from espnet.nets.pytorch_backend.nets_utils import make_pad_mask
  from espnet.nets.pytorch_backend.rnn.attentions import AttForward, AttForwardTA, AttLoc
  from espnet.nets.pytorch_backend.tacotron2.decoder import Decoder
  from espnet.nets.pytorch_backend.tacotron2.encoder import Encoder
label_desc: Use image node_id 2 for calling the Tacotron2 obj's underlying member method code with example usage: obj.forward(text, text_lengths, feats, feats_lengths, spembs, sids, lids, joint_training) and returns: loss,  stats,  weight, loss,  stats,  after_outs