class_docstr: General forward TTS model implementation that uses an encoder-decoder architecture with an optional alignment
  network and a pitch predictor.
  
  If the alignment network is used, the model learns the text-to-speech alignment
  from the data instead of using pre-computed durations.
  
  If the pitch predictor is used, the model trains a pitch predictor that predicts average pitch value for each
  input character as in the FastPitch model.
  
  `ForwardTTS` can be configured to one of these architectures,
  
      - FastPitch
      - SpeedySpeech
      - FastSpeech
      - FastSpeech2 (requires average speech energy predictor)
  
  Args:
      config (Coqpit): Model coqpit class.
      speaker_manager (SpeakerManager): Speaker manager for multi-speaker training. Only used for multi-speaker models.
          Defaults to None.
  
  Examples:
      >>> from TTS.tts.models.fast_pitch import ForwardTTS, ForwardTTSArgs
      >>> config = ForwardTTSArgs()
      >>> model = ForwardTTS(config)
class_docstr_tok: ["General","forward","TTS","model","implementation","that","uses","an","encoder-decoder","architecture","with","an","optional","alignment","network","and","a","pitch","predictor",".","If","the","alignment","network","is","used",",","the","model","learns","the","text-to-speech","alignment","from","the","data","instead","of","using","pre-computed","durations",".","If","the","pitch","predictor","is","used",",","the","model","trains","a","pitch","predictor","that","predicts","average","pitch","value","for","each","input","character","as","in","the","FastPitch","model",".","`","ForwardTTS","`","can","be","configured","to","one","of","these","architectures",",","-","FastPitch","-","SpeedySpeech","-","FastSpeech","-","FastSpeech2","(","requires","average","speech","energy","predictor",")","Args",":","config","(","Coqpit",")",":","Model","coqpit","class",".","speaker_manager","(","SpeakerManager",")",":","Speaker","manager","for","multi-speaker","training",".","Only","used","for","multi-speaker","models",".","Defaults","to","None",".","Examples",":",">",">",">","from","TTS.tts.models.fast_pitch","import","ForwardTTS",",","ForwardTTSArgs",">",">",">","config","=","ForwardTTSArgs","(",")",">",">",">","model","=","ForwardTTS","(","config",")"]
docstr: Encoding forward pass.
  
  1. Embed speaker IDs if multi-speaker mode.
  2. Embed character sequences.
  3. Run the encoder network.
  4. Sum encoder outputs and speaker embeddings
  
  Args:
      x (torch.LongTensor): Input sequence IDs.
      x_mask (torch.FloatTensor): Input squence mask.
      g (torch.FloatTensor, optional): Conditioning vectors. In general speaker embeddings. Defaults to None.
  
  Returns:
      Tuple[torch.tensor, torch.tensor, torch.tensor, torch.tensor, torch.tensor]:
          encoder output, encoder output for the duration predictor, input sequence mask, speaker embeddings,
          character embeddings
  
  Shapes:
      - x: :math:`(B, T_{en})`
      - x_mask: :math:`(B, 1, T_{en})`
      - g: :math:`(B, C)`
code: def _forward_encoder(
      self,
      x: torch.LongTensor,
      x_mask: torch.FloatTensor,
      g: torch.FloatTensor = None,
  ) -> Tuple[
      torch.FloatTensor,
      torch.FloatTensor,
      torch.FloatTensor,
      torch.FloatTensor,
      torch.FloatTensor,
  ]:
      """Encoding forward pass.
  
      1. Embed speaker IDs if multi-speaker mode.
      2. Embed character sequences.
      3. Run the encoder network.
      4. Sum encoder outputs and speaker embeddings
  
      Args:
          x (torch.LongTensor): Input sequence IDs.
          x_mask (torch.FloatTensor): Input squence mask.
          g (torch.FloatTensor, optional): Conditioning vectors. In general speaker embeddings. Defaults to None.
  
      Returns:
          Tuple[torch.tensor, torch.tensor, torch.tensor, torch.tensor, torch.tensor]:
              encoder output, encoder output for the duration predictor, input sequence mask, speaker embeddings,
              character embeddings
  
      Shapes:
          - x: :math:`(B, T_{en})`
          - x_mask: :math:`(B, 1, T_{en})`
          - g: :math:`(B, C)`
      """
      if hasattr(self, "emb_g"):
          g = g.type(torch.LongTensor)
          g = self.emb_g(g)  # [B, C, 1]
      if g is not None:
          g = g.unsqueeze(-1)
      # [B, T, C]
      x_emb = self.emb(x)
      # encoder pass
      # o_en = self.encoder(torch.transpose(x_emb, 1, -1), x_mask)
      o_en = self.encoder(torch.transpose(x_emb, 1, -1), x_mask, g)
      # speaker conditioning
      # TODO: try different ways of conditioning
      if g is not None:
          if hasattr(self, "proj_g"):
              g = self.proj_g(g.view(g.shape[0], -1)).unsqueeze(-1)
          o_en = o_en + g
      return o_en, x_mask, g, x_emb
  
imports: from dataclasses import dataclass, field
  from typing import Dict, List, Tuple, Union
  import torch
  from coqpit import Coqpit
  from torch import nn
  from torch.cuda.amp.autocast_mode import autocast
  from TTS.tts.layers.feed_forward.decoder import Decoder
  from TTS.tts.layers.feed_forward.encoder import Encoder
  from TTS.tts.layers.generic.aligner import AlignmentNetwork
  from TTS.tts.layers.generic.pos_encoding import PositionalEncoding
  from TTS.tts.layers.glow_tts.duration_predictor import DurationPredictor
  from TTS.tts.models.base_tts import BaseTTS
  from TTS.tts.utils.helpers import average_over_durations, generate_path, maximum_path, sequence_mask
  from TTS.tts.utils.speakers import SpeakerManager
  from TTS.tts.utils.text.tokenizer import TTSTokenizer
  from TTS.tts.utils.visual import plot_alignment, plot_avg_energy, plot_avg_pitch, plot_spectrogram
  from TTS.utils.io import load_fsspec
label_desc: Use image node_id 6 for calling the ForwardTTS obj's underlying member method code with example usage: obj._forward_encoder(x, x_mask, g) and returns: o_en,  x_mask,  g,  x_emb