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: Generate the sequence of features given the sequences of characters.
  
  Args:
      text (LongTensor): Input sequence of characters (T_text,).
      feats (Optional[Tensor]): Feature sequence to extract style embedding
          (T_feats', idim).
      spembs (Optional[Tensor]): Speaker embedding (spk_embed_dim,).
      sids (Optional[Tensor]): Speaker ID (1,).
      lids (Optional[Tensor]): Language ID (1,).
      threshold (float): Threshold in inference.
      minlenratio (float): Minimum length ratio in inference.
      maxlenratio (float): Maximum length ratio in inference.
      use_teacher_forcing (bool): Whether to use teacher forcing.
  
  Returns:
      Dict[str, Tensor]: Output dict including the following items:
          * feat_gen (Tensor): Output sequence of features (T_feats, odim).
          * prob (Tensor): Output sequence of stop probabilities (T_feats,).
          * att_w (Tensor): Source attn weight (#layers, #heads, T_feats, T_text).
docstr_tok: ["Generate","the","sequence","of","features","given","the","sequences","of","characters",".","Args",":","text","(","LongTensor",")",":","Input","sequence","of","characters","(","T_text",",",")",".","feats","(","Optional","[","Tensor","]",")",":","Feature","sequence","to","extract","style","embedding","(","T_feats","'",",","idim",")",".","spembs","(","Optional","[","Tensor","]",")",":","Speaker","embedding","(","spk_embed_dim",",",")",".","sids","(","Optional","[","Tensor","]",")",":","Speaker","ID","(","1",",",")",".","lids","(","Optional","[","Tensor","]",")",":","Language","ID","(","1",",",")",".","threshold","(","float",")",":","Threshold","in","inference",".","minlenratio","(","float",")",":","Minimum","length","ratio","in","inference",".","maxlenratio","(","float",")",":","Maximum","length","ratio","in","inference",".","use_teacher_forcing","(","bool",")",":","Whether","to","use","teacher","forcing",".","Returns",":","Dict","[","str",",","Tensor","]",":","Output","dict","including","the","following","items",":","*","feat_gen","(","Tensor",")",":","Output","sequence","of","features","(","T_feats",",","odim",")",".","*","prob","(","Tensor",")",":","Output","sequence","of","stop","probabilities","(","T_feats",",",")",".","*","att_w","(","Tensor",")",":","Source","attn","weight","(","#","layers",",","#","heads",",","T_feats",",","T_text",")","."]
code: def inference(
      self,
      text: torch.Tensor,
      feats: Optional[torch.Tensor] = None,
      spembs: Optional[torch.Tensor] = None,
      sids: Optional[torch.Tensor] = None,
      lids: Optional[torch.Tensor] = None,
      threshold: float = 0.5,
      minlenratio: float = 0.0,
      maxlenratio: float = 10.0,
      use_teacher_forcing: bool = False,
  ) -> Dict[str, torch.Tensor]:
      """Generate the sequence of features given the sequences of characters.
  
      Args:
          text (LongTensor): Input sequence of characters (T_text,).
          feats (Optional[Tensor]): Feature sequence to extract style embedding
              (T_feats', idim).
          spembs (Optional[Tensor]): Speaker embedding (spk_embed_dim,).
          sids (Optional[Tensor]): Speaker ID (1,).
          lids (Optional[Tensor]): Language ID (1,).
          threshold (float): Threshold in inference.
          minlenratio (float): Minimum length ratio in inference.
          maxlenratio (float): Maximum length ratio in inference.
          use_teacher_forcing (bool): Whether to use teacher forcing.
  
      Returns:
          Dict[str, Tensor]: Output dict including the following items:
              * feat_gen (Tensor): Output sequence of features (T_feats, odim).
              * prob (Tensor): Output sequence of stop probabilities (T_feats,).
              * att_w (Tensor): Source attn weight (#layers, #heads, T_feats, T_text).
  
      """
      x = text
      y = feats
      spemb = spembs
  
      # add eos at the last of sequence
      x = F.pad(x, [0, 1], "constant", self.eos)
  
      # inference with teacher forcing
      if use_teacher_forcing:
          assert (
              feats is not None
          ), "feats must be provided with teacher forcing."
  
          # get teacher forcing outputs
          xs, ys = x.unsqueeze(0), y.unsqueeze(0)
          spembs = None if spemb is None else spemb.unsqueeze(0)
          ilens = x.new_tensor([xs.size(1)]).long()
          olens = y.new_tensor([ys.size(1)]).long()
          outs, *_ = self._forward(
              xs=xs,
              ilens=ilens,
              ys=ys,
              olens=olens,
              spembs=spembs,
              sids=sids,
              lids=lids,
          )
  
          # get attention weights
          att_ws = []
          for i in range(len(self.decoder.decoders)):
              att_ws += [self.decoder.decoders[i].src_attn.attn]
          att_ws = torch.stack(
              att_ws, dim=1
          )  # (B, L, H, T_feats, T_text)
  
          return dict(feat_gen=outs[0], att_w=att_ws[0])
  
      # forward encoder
      xs = x.unsqueeze(0)
      hs, _ = self.encoder(xs, None)
  
      # integrate GST
      if self.use_gst:
          style_embs = self.gst(y.unsqueeze(0))
          hs = hs + style_embs.unsqueeze(1)
  
      # integrate spk & lang 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:
          spembs = spemb.unsqueeze(0)
          hs = self._integrate_with_spk_embed(hs, spembs)
  
      # set limits of length
      maxlen = int(hs.size(1) * maxlenratio / self.reduction_factor)
      minlen = int(hs.size(1) * minlenratio / self.reduction_factor)
  
      # initialize
      idx = 0
      ys = hs.new_zeros(1, 1, self.odim)
      outs, probs = [], []
  
      # forward decoder step-by-step
      z_cache = self.decoder.init_state(x)
      while True:
          # update index
          idx += 1
  
          # calculate output and stop prob at idx-th step
          y_masks = subsequent_mask(idx).unsqueeze(0).to(x.device)
          z, z_cache = self.decoder.forward_one_step(
              ys, y_masks, hs, cache=z_cache
          )  # (B, adim)
          outs += [
              self.feat_out(z).view(self.reduction_factor, self.odim)
          ]  # [(r, odim), ...]
          probs += [torch.sigmoid(self.prob_out(z))[0]]  # [(r), ...]
  
          # update next inputs
          ys = torch.cat(
              (ys, outs[-1][-1].view(1, 1, self.odim)), dim=1
          )  # (1, idx + 1, odim)
  
          # get attention weights
          att_ws_ = []
          for name, m in self.named_modules():
              if isinstance(m, MultiHeadedAttention) and "src" in name:
                  att_ws_ += [
                      m.attn[0, :, -1].unsqueeze(1)
                  ]  # [(#heads, 1, T),...]
          if idx == 1:
              att_ws = att_ws_
          else:
              # [(#heads, l, T), ...]
              att_ws = [
                  torch.cat([att_w, att_w_], dim=1)
                  for att_w, att_w_ in zip(att_ws, att_ws_)
              ]
  
          # check whether to finish generation
          if int(sum(probs[-1] >= threshold)) > 0 or idx >= maxlen:
              # check mininum length
              if idx < minlen:
                  continue
              outs = (
                  torch.cat(outs, dim=0).unsqueeze(0).transpose(1, 2)
              )  # (T_feats, odim) -> (1, T_feats, odim) -> (1, odim, T_feats)
              if self.postnet is not None:
                  outs = outs + self.postnet(outs)  # (1, odim, T_feats)
              outs = outs.transpose(2, 1).squeeze(0)  # (T_feats, odim)
              probs = torch.cat(probs, dim=0)
              break
  
      # concatenate attention weights -> (#layers, #heads, T_feats, T_text)
      att_ws = torch.stack(att_ws, dim=0)
  
      return dict(feat_gen=outs, prob=probs, att_w=att_ws)
  
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 5 for calling the Transformer obj's underlying member method code with example usage: obj.inference(text, feats, spembs, sids, lids, threshold, minlenratio, maxlenratio, use_teacher_forcing) and returns: dict, dict