class_docstr: Generator module in VISinger.
class_docstr_tok: ["Generator","module","in","VISinger","."]
docstr: Run inference.
  
  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,).
      label (LongTensor): Batch of padded label ids (B, Tmax).
      label_lengths (LongTensor): Batch of the lengths of padded label ids (B, ).
      melody (LongTensor): Batch of padded midi (B, Tmax).
      gt_dur (LongTensor): Batch of padded ground truth duration (B, Tmax).
      score_dur (LongTensor): Batch of padded score duration (B, Tmax).
      pitch (FloatTensor): Batch of padded f0 (B, Tmax).
      ying (Optional[Tensor]): Batch of padded ying (B, Tmax).
      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).
      noise_scale (float): Noise scale parameter for flow.
      noise_scale_dur (float): Noise scale parameter for duration predictor.
      alpha (float): Alpha parameter to control the speed of generated speech.
      max_len (Optional[int]): Maximum length of acoustic feature sequence.
      use_teacher_forcing (bool): Whether to use teacher forcing.
  
  Returns:
      Tensor: Generated waveform tensor (B, T_wav).
docstr_tok: ["Run","inference",".","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",",",")",".","label","(","LongTensor",")",":","Batch","of","padded","label","ids","(","B",",","Tmax",")",".","label_lengths","(","LongTensor",")",":","Batch","of","the","lengths","of","padded","label","ids","(","B",",",")",".","melody","(","LongTensor",")",":","Batch","of","padded","midi","(","B",",","Tmax",")",".","gt_dur","(","LongTensor",")",":","Batch","of","padded","ground","truth","duration","(","B",",","Tmax",")",".","score_dur","(","LongTensor",")",":","Batch","of","padded","score","duration","(","B",",","Tmax",")",".","pitch","(","FloatTensor",")",":","Batch","of","padded","f0","(","B",",","Tmax",")",".","ying","(","Optional","[","Tensor","]",")",":","Batch","of","padded","ying","(","B",",","Tmax",")",".","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",")",".","noise_scale","(","float",")",":","Noise","scale","parameter","for","flow",".","noise_scale_dur","(","float",")",":","Noise","scale","parameter","for","duration","predictor",".","alpha","(","float",")",":","Alpha","parameter","to","control","the","speed","of","generated","speech",".","max_len","(","Optional","[","int","]",")",":","Maximum","length","of","acoustic","feature","sequence",".","use_teacher_forcing","(","bool",")",":","Whether","to","use","teacher","forcing",".","Returns",":","Tensor",":","Generated","waveform","tensor","(","B",",","T_wav",")","."]
code: def inference(
      self,
      text: torch.Tensor,
      text_lengths: torch.Tensor,
      feats: Optional[torch.Tensor] = None,
      feats_lengths: Optional[torch.Tensor] = None,
      label: torch.Tensor = None,
      label_lengths: torch.Tensor = None,
      melody: torch.Tensor = None,
      score_dur: torch.Tensor = None,
      slur: torch.Tensor = None,
      gt_dur: Optional[torch.Tensor] = None,
      pitch: Optional[torch.Tensor] = None,
      sids: Optional[torch.Tensor] = None,
      spembs: Optional[torch.Tensor] = None,
      lids: Optional[torch.Tensor] = None,
      noise_scale: float = 0.667,
      noise_scale_dur: float = 0.8,
      alpha: float = 1.0,
      max_len: Optional[int] = None,
      use_teacher_forcing: bool = False,
  ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
      """Run inference.
  
      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,).
          label (LongTensor): Batch of padded label ids (B, Tmax).
          label_lengths (LongTensor): Batch of the lengths of padded label ids (B, ).
          melody (LongTensor): Batch of padded midi (B, Tmax).
          gt_dur (LongTensor): Batch of padded ground truth duration (B, Tmax).
          score_dur (LongTensor): Batch of padded score duration (B, Tmax).
          pitch (FloatTensor): Batch of padded f0 (B, Tmax).
          ying (Optional[Tensor]): Batch of padded ying (B, Tmax).
          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).
          noise_scale (float): Noise scale parameter for flow.
          noise_scale_dur (float): Noise scale parameter for duration predictor.
          alpha (float): Alpha parameter to control the speed of generated speech.
          max_len (Optional[int]): Maximum length of acoustic feature sequence.
          use_teacher_forcing (bool): Whether to use teacher forcing.
  
      Returns:
          Tensor: Generated waveform tensor (B, T_wav).
  
      """
      # encoder
      x, x_mask, dur_input, x_pitch = self.text_encoder(
          label, label_lengths, melody, score_dur, slur
      )
      g = None
      if self.spks is not None:
          # (B, global_channels, 1)
          g = self.global_emb(sids.view(-1)).unsqueeze(-1)
      if self.spk_embed_dim is not None:
          # (B, global_channels, 1)
          g_ = self.spemb_proj(
              F.normalize(spembs.unsqueeze(0))
          ).unsqueeze(-1)
          if g is None:
              g = g_
          else:
              g = g + g_
      if self.langs is not None:
          # (B, global_channels, 1)
          g_ = self.lang_emb(lids.view(-1)).unsqueeze(-1)
          if g is None:
              g = g_
          else:
              g = g + g_
  
      if use_teacher_forcing:
          # forward posterior encoder
          z, m_q, logs_q, y_mask = self.posterior_encoder(
              feats, feats_lengths, g=g
          )
  
          # forward flow
          if self.use_flow:
              z_p = self.flow(z, y_mask, g=g)  # (B, H, T_feats)
  
          # decoder
          pitch = pitch.transpose(0, 1).reshape(1, 1, -1)
  
          if self.vocoder_generator_type == "uhifigan":
              pitch_segments_expended = expand_f0(
                  pitch, self.hop_length, method=self.expand_f0_method
              )
              pitch_segments_expended = pitch_segments_expended.reshape(
                  -1, pitch_segments_expended.shape[-1], 1
              )
              sine_waves, uv, noise = self.sine_generator(
                  pitch_segments_expended
              )
              sine_waves = sine_waves.transpose(1, 2)
              wav = self.decoder(
                  (z * y_mask)[:, :, :max_len],
                  excitation=sine_waves,
                  g=g,
              )
          elif self.vocoder_generator_type == "avocodo":
              wav = self.decoder((z * y_mask)[:, :, :max_len], g=g)[-1]
          elif self.vocoder_generator_type == "visinger2":
              pitch_ = upsample(pitch.transpose(1, 2), self.hop_length)
              omega = torch.cumsum(
                  2 * math.pi * pitch_ / self.sample_rate, 1
              )
              sin = torch.sin(omega).transpose(1, 2)
  
              # dsp synthesize
              noise_x = self.dec_noise(z, y_mask)
              harm_x = self.dec_harm(pitch, z, y_mask)
  
              # dsp waveform
              dsp_o = torch.cat([harm_x, noise_x], axis=1)
  
              # decoder_condition = torch.cat([harm_x, noise_x, sin], axis=1)
              decoder_condition = self.sin_prenet(sin)
  
              # dsp based HiFiGAN vocoder
              wav = self.decoder(
                  (z * y_mask)[:, :, :max_len], decoder_condition, g=g
              )
              # wav = dsp_o.sum(1)
              # wav = noise_x
              # wav = harm_x.sum(1)
          else:
              wav = self.decoder((z * y_mask)[:, :, :max_len], g=g)
      else:
          # dur
          predict_dur = self.duration_predictor(dur_input, x_mask, g=g)
          predict_dur = (torch.exp(predict_dur) - 1) * x_mask
          predict_dur = predict_dur * self.sample_rate / self.hop_length
  
          predict_dur = torch.max(
              predict_dur, torch.ones_like(predict_dur).to(x)
          )
          predict_dur = torch.ceil(predict_dur).long()
          predict_dur = predict_dur[:, 0, :]
          y_lengths = torch.clamp_min(
              torch.sum(predict_dur, [1]), 1
          ).long()
  
          # LR
          decoder_input, mel_len = self.lr(
              x, predict_dur, use_state_info=True
          )
          decoder_input_pitch, mel_len = self.lr(
              x_pitch, predict_dur, use_state_info=True
          )
  
          # aam
          predict_lf0, predict_bn_mask = self.f0_decoder(
              decoder_input + decoder_input_pitch, y_lengths, g=g
          )
  
          if self.generator_type == "visinger2":
              predict_mel, predict_bn_mask = self.mel_decoder(
                  decoder_input + self.f0_prenet(predict_lf0),
                  y_lengths,
                  g=g,
              )
              predict_energy = (
                  predict_mel.sum(1).unsqueeze(1) / self.aux_channels
              )
  
          predict_lf0 = torch.max(
              predict_lf0, torch.zeros_like(predict_lf0).to(predict_lf0)
          )
  
          decoder_input = decoder_input + self.f0_prenet(predict_lf0)
          if self.generator_type == "visinger2":
              decoder_input = (
                  decoder_input
                  + self.energy_prenet(predict_energy)
                  + self.mel_prenet(predict_mel)
              )
  
          decoder_output, y_mask = self.prior_decoder(
              decoder_input, y_lengths, g=g
          )
  
          prior_info = decoder_output
          m_p = prior_info[:, : self.hidden_channels, :]
          logs_p = prior_info[:, self.hidden_channels :, :]
  
          # decoder
          z_p = (
              m_p
              + torch.randn_like(m_p) * torch.exp(logs_p) * noise_scale
          )
  
          if self.use_flow:
              z = self.flow(z_p, y_mask, g=g, inverse=True)
          else:
              z = z_p
  
          F0_std = 500
          F0 = predict_lf0 * F0_std
          F0 = F0 / 2595
          F0 = torch.pow(10, F0)
          F0 = (F0 - 1) * 700.0
  
          if self.vocoder_generator_type == "uhifigan":
              pitch_segments_expended = expand_f0(
                  F0, self.hop_length, method=self.expand_f0_method
              )
              pitch_segments_expended = pitch_segments_expended.reshape(
                  -1, pitch_segments_expended.shape[-1], 1
              )
              sine_waves, uv, noise = self.sine_generator(
                  pitch_segments_expended
              )
              sine_waves = sine_waves.transpose(1, 2)
              wav = self.decoder(
                  (z * y_mask)[:, :, :max_len],
                  excitation=sine_waves,
                  g=g,
              )
          elif self.vocoder_generator_type == "avocodo":
              wav = self.decoder((z * y_mask)[:, :, :max_len], g=g)[-1]
          elif self.vocoder_generator_type == "visinger2":
              pitch_ = upsample(F0.transpose(1, 2), self.hop_length)
              omega = torch.cumsum(
                  2 * math.pi * pitch_ / self.sample_rate, 1
              )
              sin = torch.sin(omega).transpose(1, 2)
  
              # dsp synthesize
              noise_x = self.dec_noise(z, y_mask)
              harm_x = self.dec_harm(F0, z, y_mask)
  
              # dsp waveform
              dsp_o = torch.cat([harm_x, noise_x], axis=1)
  
              # decoder_condition = torch.cat([harm_x, noise_x, sin], axis=1)
              decoder_condition = self.sin_prenet(sin)
  
              # dsp based HiFiGAN vocoder
              wav = self.decoder(
                  (z * y_mask)[:, :, :max_len], decoder_condition, g=g
              )
              # wav = dsp_o.sum(1)
              # wav = noise_x
              # wav = harm_x.sum(1)
          else:
              wav = self.decoder((z * y_mask)[:, :, :max_len], g=g)
  
      return wav.squeeze(1)
  
imports: import math
  from typing import List, Optional, Tuple
  import numpy
  import torch
  import torch.nn.functional
  from espnet2.gan_svs.avocodo import AvocodoGenerator
  from espnet2.gan_svs.uhifigan import UHiFiGANGenerator
  from espnet2.gan_svs.uhifigan.sine_generator import SineGen
  from espnet2.gan_svs.utils.expand_f0 import expand_f0
  from espnet2.gan_svs.visinger2 import Generator_Harm, Generator_Noise, VISinger2VocoderGenerator
  from espnet2.gan_svs.visinger2.ddsp import upsample
  from espnet2.gan_svs.vits.duration_predictor import DurationPredictor
  from espnet2.gan_svs.vits.length_regulator import LengthRegulator
  from espnet2.gan_svs.vits.phoneme_predictor import PhonemePredictor
  from espnet2.gan_svs.vits.pitch_predictor import Decoder
  from espnet2.gan_svs.vits.prior_decoder import PriorDecoder
  from espnet2.gan_svs.vits.text_encoder import TextEncoder
  from espnet2.gan_tts.hifigan import HiFiGANGenerator
  from espnet2.gan_tts.utils import get_random_segments, get_segments
  from espnet2.gan_tts.vits.posterior_encoder import PosteriorEncoder
  from espnet2.gan_tts.vits.residual_coupling import ResidualAffineCouplingBlock
label_desc: Use image node_id 3 for calling the VISingerGenerator obj's underlying member method code with example usage: obj.inference(text, text_lengths, feats, feats_lengths, label, label_lengths, melody, score_dur, slur, gt_dur, pitch, sids, spembs, lids, noise_scale, noise_scale_dur, alpha, max_len, use_teacher_forcing) and returns: wav