class_docstr: XiaoiceSing module for Singing Voice Synthesis.
  
  This is a module of XiaoiceSing. A high-quality singing voice synthesis system which
  employs an integrated network for spectrum, F0 and duration modeling. It follows the
  main architecture of FastSpeech while proposing some singing-specific design:
      1) Add features from musical score (e.g.note pitch and length)
      2) Add a residual connection in F0 prediction to attenuate off-key issues
      3) The duration of all the phonemes in a musical note is accumulated to
      calculate the syllable duration loss for rhythm enhancement (syllable loss)
  .. _`XiaoiceSing: A High-Quality and Integrated Singing Voice Synthesis System`:
      https://arxiv.org/pdf/2006.06261.pdf
docstr: Calculate forward propagation.
  
  Args:
      text (LongTensor): Batch of padded character 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,).
      label (Optional[Dict]): key is "lab" or "score";
          value (LongTensor): Batch of padded label ids (B, Tmax).
      label_lengths (Optional[Dict]): key is "lab" or "score";
          value (LongTensor): Batch of the lengths of padded label ids (B, ).
      melody (Optional[Dict]): key is "lab" or "score";
          value (LongTensor): Batch of padded melody (B, Tmax).
      melody_lengths (Optional[Dict]): key is "lab" or "score";
          value (LongTensor): Batch of the lengths of padded melody (B, ).
      pitch (FloatTensor): Batch of padded f0 (B, Tmax).
      pitch_lengths (LongTensor): Batch of the lengths of padded f0 (B, ).
      duration (Optional[Dict]): key is "lab", "score_phn" or "score_syb";
          value (LongTensor): Batch of padded duration (B, Tmax).
      duration_length (Optional[Dict]): key is "lab", "score_phn" or "score_syb";
          value (LongTensor): Batch of the lengths of padded duration (B, ).
      slur (LongTensor): Batch of padded slur (B, Tmax).
      slur_lengths (LongTensor): Batch of the lengths of padded slur (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","(","B",",",")",".","feats","(","Tensor",")",":","Batch","of","padded","target","features","(","B",",","T_feats",",","odim",")",".","feats_lengths","(","LongTensor",")",":","Batch","of","the","lengths","of","each","target","(","B",",",")",".","label","(","Optional","[","Dict","]",")",":","key","is","``","lab","''","or","``","score","''",";","value","(","LongTensor",")",":","Batch","of","padded","label","ids","(","B",",","Tmax",")",".","label_lengths","(","Optional","[","Dict","]",")",":","key","is","``","lab","''","or","``","score","''",";","value","(","LongTensor",")",":","Batch","of","the","lengths","of","padded","label","ids","(","B",",",")",".","melody","(","Optional","[","Dict","]",")",":","key","is","``","lab","''","or","``","score","''",";","value","(","LongTensor",")",":","Batch","of","padded","melody","(","B",",","Tmax",")",".","melody_lengths","(","Optional","[","Dict","]",")",":","key","is","``","lab","''","or","``","score","''",";","value","(","LongTensor",")",":","Batch","of","the","lengths","of","padded","melody","(","B",",",")",".","pitch","(","FloatTensor",")",":","Batch","of","padded","f0","(","B",",","Tmax",")",".","pitch_lengths","(","LongTensor",")",":","Batch","of","the","lengths","of","padded","f0","(","B",",",")",".","duration","(","Optional","[","Dict","]",")",":","key","is","``","lab","''",",","``","score_phn","''","or","``","score_syb","''",";","value","(","LongTensor",")",":","Batch","of","padded","duration","(","B",",","Tmax",")",".","duration_length","(","Optional","[","Dict","]",")",":","key","is","``","lab","''",",","``","score_phn","''","or","``","score_syb","''",";","value","(","LongTensor",")",":","Batch","of","the","lengths","of","padded","duration","(","B",",",")",".","slur","(","LongTensor",")",":","Batch","of","padded","slur","(","B",",","Tmax",")",".","slur_lengths","(","LongTensor",")",":","Batch","of","the","lengths","of","padded","slur","(","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,
      label: Optional[Dict[str, torch.Tensor]] = None,
      label_lengths: Optional[Dict[str, torch.Tensor]] = None,
      melody: Optional[Dict[str, torch.Tensor]] = None,
      melody_lengths: Optional[Dict[str, torch.Tensor]] = None,
      pitch: Optional[torch.Tensor] = None,
      pitch_lengths: Optional[torch.Tensor] = None,
      duration: Optional[Dict[str, torch.Tensor]] = None,
      duration_lengths: Optional[Dict[str, torch.Tensor]] = None,
      slur: torch.LongTensor = None,
      slur_lengths: torch.Tensor = None,
      spembs: Optional[torch.Tensor] = None,
      sids: Optional[torch.Tensor] = None,
      lids: Optional[torch.Tensor] = None,
      joint_training: bool = False,
      flag_IsValid=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 (B,).
          feats (Tensor): Batch of padded target features (B, T_feats, odim).
          feats_lengths (LongTensor): Batch of the lengths of each target (B,).
          label (Optional[Dict]): key is "lab" or "score";
              value (LongTensor): Batch of padded label ids (B, Tmax).
          label_lengths (Optional[Dict]): key is "lab" or "score";
              value (LongTensor): Batch of the lengths of padded label ids (B, ).
          melody (Optional[Dict]): key is "lab" or "score";
              value (LongTensor): Batch of padded melody (B, Tmax).
          melody_lengths (Optional[Dict]): key is "lab" or "score";
              value (LongTensor): Batch of the lengths of padded melody (B, ).
          pitch (FloatTensor): Batch of padded f0 (B, Tmax).
          pitch_lengths (LongTensor): Batch of the lengths of padded f0 (B, ).
          duration (Optional[Dict]): key is "lab", "score_phn" or "score_syb";
              value (LongTensor): Batch of padded duration (B, Tmax).
          duration_length (Optional[Dict]): key is "lab", "score_phn" or "score_syb";
              value (LongTensor): Batch of the lengths of padded duration (B, ).
          slur (LongTensor): Batch of padded slur (B, Tmax).
          slur_lengths (LongTensor): Batch of the lengths of padded slur (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.
      """
  
      if joint_training:
          label = label
          midi = melody
          label_lengths = label_lengths
          midi_lengths = melody_lengths
          duration_lengths = duration_lengths
          duration_ = duration
          ds = duration
      else:
          label = label["score"]
          midi = melody["score"]
          duration_ = duration["score_phn"]
          label_lengths = label_lengths["score"]
          midi_lengths = melody_lengths["score"]
          duration_lengths = duration_lengths["score_phn"]
          ds = duration["lab"]
  
      feats = feats[:, : feats_lengths.max()]  # for data-parallel
      midi = midi[:, : midi_lengths.max()]  # for data-parallel
      label = label[:, : label_lengths.max()]  # for data-parallel
      duration_ = duration_[
          :, : duration_lengths.max()
      ]  # for data-parallel
      olens = feats_lengths
  
      if self.loss_function == "XiaoiceSing2":
          pitch = pitch[:, : pitch_lengths.max()]
          log_f0 = torch.clamp(pitch, min=0)
          vuv = log_f0 != 0
      batch_size = text.size(0)
  
      label_emb = self.phone_encode_layer(label)
      midi_emb = self.midi_encode_layer(midi)
      duration_emb = self.duration_encode_layer(duration_)
      input_emb = label_emb + midi_emb + duration_emb
  
      x_masks = self._source_mask(label_lengths)
      hs, _ = self.encoder(input_emb, x_masks)  # (B, T_text, adim)
  
      # integrate with SID and LID embeddings
      if self.spks is not None:
          sid_embs = self.sid_emb(sids.view(-1))
          hs = hs + sid_embs.unsqueeze(1)
      if self.langs is not None:
          lid_embs = self.lid_emb(lids.view(-1))
          hs = hs + lid_embs.unsqueeze(1)
  
      # integrate speaker embedding
      if self.spk_embed_dim is not None:
          hs = self._integrate_with_spk_embed(hs, spembs)
  
      # forward duration predictor and length regulator
      d_masks = make_pad_mask(label_lengths).to(input_emb.device)
      d_outs = self.duration_predictor(hs, d_masks)  # (B, T_text)
  
      hs = self.length_regulator(hs, ds)  # (B, T_feats, adim)
  
      # forward decoder
      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
      h_masks = self._source_mask(olens_in)
  
      zs, _ = self.decoder(hs, h_masks)  # (B, T_feats, adim)
      before_outs, log_f0_outs, vuv_outs = (
          self.linear_projection(zs)
          .view((zs.size(0), -1, self.odim + 2))
          .split_with_sizes([self.odim, 1, 1], dim=2)
      )
      # (B. T_feats, odim), (B. T_feats, 1), (B. T_feats, 1)
  
      # postnet -> (B, Lmax//r * r, odim)
      if self.postnet is None:
          after_outs = before_outs
      else:
          after_outs = before_outs + self.postnet(
              before_outs.transpose(1, 2)
          ).transpose(1, 2)
  
      # modifiy mod part of groundtruth
      if self.reduction_factor > 1:
          assert feats_lengths.ge(
              self.reduction_factor
          ).all(), "Output length must be greater than or equal to reduction factor."
          olens = feats_lengths.new(
              [
                  olen - olen % self.reduction_factor
                  for olen in feats_lengths
              ]
          )
          max_olen = max(olens)
          ys = feats[:, :max_olen]
          if self.loss_function == "XiaoiceSing2":
              log_f0 = log_f0[:, :max_olen]
              vuv = vuv[:, :max_olen]
      else:
          ys = feats
          olens = feats_lengths
  
      ilens = label_lengths
      if self.loss_function == "FastSpeech1":
          mel_loss, duration_loss = self.criterion(
              after_outs, before_outs, d_outs, ys, ds, ilens, olens
          )
      elif self.loss_function == "XiaoiceSing2":
          (
              mel_loss,
              duration_loss,
              pitch_loss,
              vuv_loss,
          ) = self.criterion(
              after_outs=after_outs,
              before_outs=before_outs,
              d_outs=d_outs,
              p_outs=log_f0_outs,
              v_outs=vuv_outs,
              ys=ys,
              ds=ds,
              ps=log_f0,
              vs=vuv,
              ilens=ilens,
              olens=olens,
              loss_type=self.loss_type,
          )
  
      mel_loss = mel_loss * self.lambda_mel
      duration_loss = duration_loss * self.lambda_dur
      loss = mel_loss + duration_loss
      stats = dict(
          mel_loss=mel_loss.item(), duration_loss=duration_loss.item()
      )
      if self.loss_function == "XiaoiceSing2":
          pitch_loss = pitch_loss * self.lambda_pitch
          vuv_loss = vuv_loss * self.lambda_vuv
          loss += pitch_loss + vuv_loss
          stats["pitch_loss"] = pitch_loss.item()
          stats["vuv_loss"] = vuv_loss.item()
      stats["loss"] = 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(),
          )
  
      loss, stats, weight = force_gatherable(
          (loss, stats, batch_size), loss.device
      )
  
      if joint_training:
          return (
              loss,
              stats,
              after_outs if after_outs is not None else before_outs,
          )
      else:
          if flag_IsValid is False:
              return loss, stats, weight
          else:
              return (
                  loss,
                  stats,
                  weight,
                  after_outs[:, : olens.max()],
                  ys,
                  olens,
              )
  
imports: import logging
  from typing import Dict, Optional, Tuple
  import torch
  import torch.nn.functional
  from typeguard import check_argument_types
  from espnet2.svs.abs_svs import AbsSVS
  from espnet2.svs.xiaoice.loss import XiaoiceSing2Loss
  from espnet2.torch_utils.device_funcs import force_gatherable
  from espnet2.torch_utils.initialize import initialize
  from espnet.nets.pytorch_backend.conformer.encoder import Encoder
  from espnet.nets.pytorch_backend.e2e_tts_fastspeech import FeedForwardTransformerLoss
  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 XiaoiceSing obj's underlying member method code with example usage: obj.forward(text, text_lengths, feats, feats_lengths, label, label_lengths, melody, melody_lengths, pitch, pitch_lengths, duration, duration_lengths, slur, slur_lengths, spembs, sids, lids, joint_training, flag_IsValid) and returns: loss,  stats,  unknown, loss,  stats,  weight, loss,  stats,  weight,  after_outs,  ys,  olens