class_docstr: Generator module in VITS.
  
  This is a module of VITS described in `Conditional Variational Autoencoder
  with Adversarial Learning for End-to-End Text-to-Speech`_.
  
  As text encoder, we use conformer architecture instead of the relative positional
  Transformer, which contains additional convolution layers.
  
  .. _`Conditional Variational Autoencoder with Adversarial Learning for End-to-End
      Text-to-Speech`: https://arxiv.org/abs/2006.04558
class_docstr_tok: ["Generator","module","in","VITS",".","This","is","a","module","of","VITS","described","in","`","Conditional","Variational","Autoencoder","with","Adversarial","Learning","for","End-to-End","Text-to-Speech","`","_",".","As","text","encoder",",","we","use","conformer","architecture","instead","of","the","relative","positional","Transformer",",","which","contains","additional","convolution","layers",".","..","_","`","Conditional","Variational","Autoencoder","with","Adversarial","Learning","for","End-to-End","Text-to-Speech","`",":","https",":","\/\/arxiv.org\/abs\/2006.04558"]
docstr: Calculate forward propagation.
  
  Args:
      text (Tensor): Text index tensor (B, T_text).
      text_lengths (Tensor): Text length tensor (B,).
      feats (Tensor): Feature tensor (B, aux_channels, T_feats).
      feats_lengths (Tensor): Feature length tensor (B,).
      sids (Optional[Tensor]): Speaker index tensor (B,) or (B, 1).
      spembs (Optional[Tensor]): Speaker embedding tensor (B, spk_embed_dim).
      lids (Optional[Tensor]): Language index tensor (B,) or (B, 1).
  
  Returns:
      Tensor: Waveform tensor (B, 1, segment_size * upsample_factor).
      Tensor: Duration negative log-likelihood (NLL) tensor (B,).
      Tensor: Monotonic attention weight tensor (B, 1, T_feats, T_text).
      Tensor: Segments start index tensor (B,).
      Tensor: Text mask tensor (B, 1, T_text).
      Tensor: Feature mask tensor (B, 1, T_feats).
      tuple[Tensor, Tensor, Tensor, Tensor, Tensor, Tensor]:
          - Tensor: Posterior encoder hidden representation (B, H, T_feats).
          - Tensor: Flow hidden representation (B, H, T_feats).
          - Tensor: Expanded text encoder projected mean (B, H, T_feats).
          - Tensor: Expanded text encoder projected scale (B, H, T_feats).
          - Tensor: Posterior encoder projected mean (B, H, T_feats).
          - Tensor: Posterior encoder projected scale (B, H, T_feats).
docstr_tok: ["Calculate","forward","propagation",".","Args",":","text","(","Tensor",")",":","Text","index","tensor","(","B",",","T_text",")",".","text_lengths","(","Tensor",")",":","Text","length","tensor","(","B",",",")",".","feats","(","Tensor",")",":","Feature","tensor","(","B",",","aux_channels",",","T_feats",")",".","feats_lengths","(","Tensor",")",":","Feature","length","tensor","(","B",",",")",".","sids","(","Optional","[","Tensor","]",")",":","Speaker","index","tensor","(","B",",",")","or","(","B",",","1",")",".","spembs","(","Optional","[","Tensor","]",")",":","Speaker","embedding","tensor","(","B",",","spk_embed_dim",")",".","lids","(","Optional","[","Tensor","]",")",":","Language","index","tensor","(","B",",",")","or","(","B",",","1",")",".","Returns",":","Tensor",":","Waveform","tensor","(","B",",","1",",","segment_size","*","upsample_factor",")",".","Tensor",":","Duration","negative","log-likelihood","(","NLL",")","tensor","(","B",",",")",".","Tensor",":","Monotonic","attention","weight","tensor","(","B",",","1",",","T_feats",",","T_text",")",".","Tensor",":","Segments","start","index","tensor","(","B",",",")",".","Tensor",":","Text","mask","tensor","(","B",",","1",",","T_text",")",".","Tensor",":","Feature","mask","tensor","(","B",",","1",",","T_feats",")",".","tuple","[","Tensor",",","Tensor",",","Tensor",",","Tensor",",","Tensor",",","Tensor","]",":","-","Tensor",":","Posterior","encoder","hidden","representation","(","B",",","H",",","T_feats",")",".","-","Tensor",":","Flow","hidden","representation","(","B",",","H",",","T_feats",")",".","-","Tensor",":","Expanded","text","encoder","projected","mean","(","B",",","H",",","T_feats",")",".","-","Tensor",":","Expanded","text","encoder","projected","scale","(","B",",","H",",","T_feats",")",".","-","Tensor",":","Posterior","encoder","projected","mean","(","B",",","H",",","T_feats",")",".","-","Tensor",":","Posterior","encoder","projected","scale","(","B",",","H",",","T_feats",")","."]
code: def forward(
      self,
      text: torch.Tensor,
      text_lengths: torch.Tensor,
      feats: torch.Tensor,
      feats_lengths: torch.Tensor,
      sids: Optional[torch.Tensor] = None,
      spembs: Optional[torch.Tensor] = None,
      lids: Optional[torch.Tensor] = None,
  ) -> Tuple[
      torch.Tensor,
      torch.Tensor,
      torch.Tensor,
      torch.Tensor,
      torch.Tensor,
      torch.Tensor,
      Tuple[
          torch.Tensor,
          torch.Tensor,
          torch.Tensor,
          torch.Tensor,
          torch.Tensor,
          torch.Tensor,
      ],
  ]:
      """Calculate forward propagation.
  
      Args:
          text (Tensor): Text index tensor (B, T_text).
          text_lengths (Tensor): Text length tensor (B,).
          feats (Tensor): Feature tensor (B, aux_channels, T_feats).
          feats_lengths (Tensor): Feature length tensor (B,).
          sids (Optional[Tensor]): Speaker index tensor (B,) or (B, 1).
          spembs (Optional[Tensor]): Speaker embedding tensor (B, spk_embed_dim).
          lids (Optional[Tensor]): Language index tensor (B,) or (B, 1).
  
      Returns:
          Tensor: Waveform tensor (B, 1, segment_size * upsample_factor).
          Tensor: Duration negative log-likelihood (NLL) tensor (B,).
          Tensor: Monotonic attention weight tensor (B, 1, T_feats, T_text).
          Tensor: Segments start index tensor (B,).
          Tensor: Text mask tensor (B, 1, T_text).
          Tensor: Feature mask tensor (B, 1, T_feats).
          tuple[Tensor, Tensor, Tensor, Tensor, Tensor, Tensor]:
              - Tensor: Posterior encoder hidden representation (B, H, T_feats).
              - Tensor: Flow hidden representation (B, H, T_feats).
              - Tensor: Expanded text encoder projected mean (B, H, T_feats).
              - Tensor: Expanded text encoder projected scale (B, H, T_feats).
              - Tensor: Posterior encoder projected mean (B, H, T_feats).
              - Tensor: Posterior encoder projected scale (B, H, T_feats).
  
      """
      # forward text encoder
      x, m_p, logs_p, x_mask = self.text_encoder(text, text_lengths)
  
      # calculate global conditioning
      g = None
      if self.spks is not None:
          # speaker one-hot vector embedding: (B, global_channels, 1)
          g = self.global_emb(sids.view(-1)).unsqueeze(-1)
      if self.spk_embed_dim is not None:
          # pretreined speaker embedding, e.g., X-vector (B, global_channels, 1)
          g_ = self.spemb_proj(F.normalize(spembs)).unsqueeze(-1)
          if g is None:
              g = g_
          else:
              g = g + g_
      if self.langs is not None:
          # language one-hot vector embedding: (B, global_channels, 1)
          g_ = self.lang_emb(lids.view(-1)).unsqueeze(-1)
          if g is None:
              g = g_
          else:
              g = g + g_
  
      # forward posterior encoder
      z, m_q, logs_q, y_mask = self.posterior_encoder(
          feats, feats_lengths, g=g
      )
  
      # forward flow
      z_p = self.flow(z, y_mask, g=g)  # (B, H, T_feats)
  
      # monotonic alignment search
      with torch.no_grad():
          # negative cross-entropy
          s_p_sq_r = torch.exp(-2 * logs_p)  # (B, H, T_text)
          # (B, 1, T_text)
          neg_x_ent_1 = torch.sum(
              -0.5 * math.log(2 * math.pi) - logs_p,
              [1],
              keepdim=True,
          )
          # (B, T_feats, H) x (B, H, T_text) = (B, T_feats, T_text)
          neg_x_ent_2 = torch.matmul(
              -0.5 * (z_p**2).transpose(1, 2),
              s_p_sq_r,
          )
          # (B, T_feats, H) x (B, H, T_text) = (B, T_feats, T_text)
          neg_x_ent_3 = torch.matmul(
              z_p.transpose(1, 2),
              (m_p * s_p_sq_r),
          )
          # (B, 1, T_text)
          neg_x_ent_4 = torch.sum(
              -0.5 * (m_p**2) * s_p_sq_r,
              [1],
              keepdim=True,
          )
          # (B, T_feats, T_text)
          neg_x_ent = (
              neg_x_ent_1 + neg_x_ent_2 + neg_x_ent_3 + neg_x_ent_4
          )
          # (B, 1, T_feats, T_text)
          attn_mask = torch.unsqueeze(x_mask, 2) * torch.unsqueeze(
              y_mask, -1
          )
          # monotonic attention weight: (B, 1, T_feats, T_text)
          attn = (
              self.maximum_path(
                  neg_x_ent,
                  attn_mask.squeeze(1),
              )
              .unsqueeze(1)
              .detach()
          )
  
      # forward duration predictor
      w = attn.sum(2)  # (B, 1, T_text)
      dur_nll = self.duration_predictor(x, x_mask, w=w, g=g)
      dur_nll = dur_nll / torch.sum(x_mask)
  
      # expand the length to match with the feature sequence
      # (B, T_feats, T_text) x (B, T_text, H) -> (B, H, T_feats)
      m_p = torch.matmul(
          attn.squeeze(1), m_p.transpose(1, 2)
      ).transpose(1, 2)
      # (B, T_feats, T_text) x (B, T_text, H) -> (B, H, T_feats)
      logs_p = torch.matmul(
          attn.squeeze(1), logs_p.transpose(1, 2)
      ).transpose(1, 2)
  
      # get random segments
      z_segments, z_start_idxs = get_random_segments(
          z,
          feats_lengths,
          self.segment_size,
      )
  
      # forward decoder with random segments
      wav = self.decoder(z_segments, g=g)
  
      return (
          wav,
          dur_nll,
          attn,
          z_start_idxs,
          x_mask,
          y_mask,
          (z, z_p, m_p, logs_p, m_q, logs_q),
      )
  
file_path: espnet/espnet2/gan_tts/vits/generator.py
imports: import math
  from typing import List, Optional, Tuple
  import numpy
  import torch
  import torch.nn.functional
  from espnet2.gan_tts.hifigan import HiFiGANGenerator
  from espnet2.gan_tts.utils import get_random_segments
  from espnet2.gan_tts.vits.duration_predictor import StochasticDurationPredictor
  from espnet2.gan_tts.vits.posterior_encoder import PosteriorEncoder
  from espnet2.gan_tts.vits.residual_coupling import ResidualAffineCouplingBlock
  from espnet2.gan_tts.vits.text_encoder import TextEncoder
  from espnet.nets.pytorch_backend.nets_utils import make_non_pad_mask
label_desc: Use image node_id 2 for calling the VITSGenerator obj's underlying member method code with example usage: obj.forward(text, text_lengths, feats, feats_lengths, sids, spembs, lids) and returns: wav,  dur_nll,  attn,  z_start_idxs,  x_mask,  y_mask,  z,  z_p,  m_p,  logs_p,  m_q,  logs_q