class_docstr: Transformer-TTS module.
  
  This is a module of text-to-speech Transformer described in `Neural Speech Synthesis
  with Transformer Network`_, which convert the sequence of tokens into the sequence
  of Mel-filterbanks.
  
  .. _`Neural Speech Synthesis with Transformer Network`:
      https://arxiv.org/pdf/1809.08895.pdf
docstr: Calculate forward propagation.
  
  Args:
      text (LongTensor): Batch of padded character ids (B, Tmax).
      text_lengths (LongTensor): Batch of lengths of each input batch (B,).
      feats (Tensor): Batch of padded target features (B, Lmax, 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",",","Tmax",")",".","text_lengths","(","LongTensor",")",":","Batch","of","lengths","of","each","input","batch","(","B",",",")",".","feats","(","Tensor",")",":","Batch","of","padded","target","features","(","B",",","Lmax",",","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, Tmax).
          text_lengths (LongTensor): Batch of lengths of each input batch (B,).
          feats (Tensor): Batch of padded target features (B, Lmax, 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 transformer outputs
      after_outs, before_outs, logits = self._forward(
          xs=xs,
          ilens=ilens,
          ys=ys,
          olens=olens,
          spembs=spembs,
          sids=sids,
          lids=lids,
      )
  
      # modifiy mod part of groundtruth
      olens_in = olens
      if self.reduction_factor > 1:
          assert olens.ge(
              self.reduction_factor
          ).all(), "Output length must be greater than or equal to reduction factor."
          olens_in = olens.new(
              [
                  torch.div(
                      olen, self.reduction_factor, rounding_mode="trunc"
                  )
                  for olen in olens
              ]
          )
          olens = olens.new(
              [olen - olen % self.reduction_factor for olen in olens]
          )
          max_olen = max(olens)
          ys = ys[:, :max_olen]
          labels = labels[:, :max_olen]
          labels = torch.scatter(
              labels, 1, (olens - 1).unsqueeze(1), 1.0
          )  # see #3388
  
      # calculate loss values
      l1_loss, l2_loss, bce_loss = self.criterion(
          after_outs, before_outs, logits, ys, labels, olens
      )
      if self.loss_type == "L1":
          loss = l1_loss + bce_loss
      elif self.loss_type == "L2":
          loss = l2_loss + bce_loss
      elif self.loss_type == "L1+L2":
          loss = l1_loss + l2_loss + bce_loss
      else:
          raise ValueError("unknown --loss-type " + self.loss_type)
  
      stats = dict(
          l1_loss=l1_loss.item(),
          l2_loss=l2_loss.item(),
          bce_loss=bce_loss.item(),
      )
  
      # calculate guided attention loss
      if self.use_guided_attn_loss:
          # calculate for encoder
          if "encoder" in self.modules_applied_guided_attn:
              att_ws = []
              for idx, layer_idx in enumerate(
                  reversed(range(len(self.encoder.encoders)))
              ):
                  att_ws += [
                      self.encoder.encoders[layer_idx].self_attn.attn[
                          :, : self.num_heads_applied_guided_attn
                      ]
                  ]
                  if idx + 1 == self.num_layers_applied_guided_attn:
                      break
              att_ws = torch.cat(
                  att_ws, dim=1
              )  # (B, H*L, T_text, T_text)
              enc_attn_loss = self.attn_criterion(att_ws, ilens, ilens)
              loss = loss + enc_attn_loss
              stats.update(enc_attn_loss=enc_attn_loss.item())
          # calculate for decoder
          if "decoder" in self.modules_applied_guided_attn:
              att_ws = []
              for idx, layer_idx in enumerate(
                  reversed(range(len(self.decoder.decoders)))
              ):
                  att_ws += [
                      self.decoder.decoders[layer_idx].self_attn.attn[
                          :, : self.num_heads_applied_guided_attn
                      ]
                  ]
                  if idx + 1 == self.num_layers_applied_guided_attn:
                      break
              att_ws = torch.cat(
                  att_ws, dim=1
              )  # (B, H*L, T_feats, T_feats)
              dec_attn_loss = self.attn_criterion(
                  att_ws, olens_in, olens_in
              )
              loss = loss + dec_attn_loss
              stats.update(dec_attn_loss=dec_attn_loss.item())
          # calculate for encoder-decoder
          if "encoder-decoder" in self.modules_applied_guided_attn:
              att_ws = []
              for idx, layer_idx in enumerate(
                  reversed(range(len(self.decoder.decoders)))
              ):
                  att_ws += [
                      self.decoder.decoders[layer_idx].src_attn.attn[
                          :, : self.num_heads_applied_guided_attn
                      ]
                  ]
                  if idx + 1 == self.num_layers_applied_guided_attn:
                      break
              att_ws = torch.cat(
                  att_ws, dim=1
              )  # (B, H*L, T_feats, T_text)
              enc_dec_attn_loss = self.attn_criterion(
                  att_ws, ilens, olens_in
              )
              loss = loss + enc_dec_attn_loss
              stats.update(enc_dec_attn_loss=enc_dec_attn_loss.item())
  
      # report extra information
      if self.use_scaled_pos_enc:
          stats.update(
              encoder_alpha=self.encoder.embed[-1].alpha.data.item(),
              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
  
imports: 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.gst.style_encoder import StyleEncoder
  from espnet.nets.pytorch_backend.e2e_tts_transformer import GuidedMultiHeadAttentionLoss, TransformerLoss
  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.tacotron2.decoder import Prenet
  from espnet.nets.pytorch_backend.tacotron2.encoder import Encoder
  from espnet.nets.pytorch_backend.transformer.attention import MultiHeadedAttention
  from espnet.nets.pytorch_backend.transformer.decoder import Decoder
  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.mask import subsequent_mask
label_desc: Use image node_id 3 for calling the Transformer 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