joint_network.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. """Transducer joint network implementation."""
  2. import torch
  3. from funasr.modules.nets_utils import get_activation
  4. class JointNetwork(torch.nn.Module):
  5. """Transducer joint network module.
  6. Args:
  7. output_size: Output size.
  8. encoder_size: Encoder output size.
  9. decoder_size: Decoder output size..
  10. joint_space_size: Joint space size.
  11. joint_act_type: Type of activation for joint network.
  12. **activation_parameters: Parameters for the activation function.
  13. """
  14. def __init__(
  15. self,
  16. output_size: int,
  17. encoder_size: int,
  18. decoder_size: int,
  19. joint_space_size: int = 256,
  20. joint_activation_type: str = "tanh",
  21. ) -> None:
  22. """Construct a JointNetwork object."""
  23. super().__init__()
  24. self.lin_enc = torch.nn.Linear(encoder_size, joint_space_size)
  25. self.lin_dec = torch.nn.Linear(decoder_size, joint_space_size, bias=False)
  26. self.lin_out = torch.nn.Linear(joint_space_size, output_size)
  27. self.joint_activation = get_activation(
  28. joint_activation_type
  29. )
  30. def forward(
  31. self,
  32. enc_out: torch.Tensor,
  33. dec_out: torch.Tensor,
  34. project_input: bool = True,
  35. ) -> torch.Tensor:
  36. """Joint computation of encoder and decoder hidden state sequences.
  37. Args:
  38. enc_out: Expanded encoder output state sequences (B, T, 1, D_enc)
  39. dec_out: Expanded decoder output state sequences (B, 1, U, D_dec)
  40. Returns:
  41. joint_out: Joint output state sequences. (B, T, U, D_out)
  42. """
  43. if project_input:
  44. joint_out = self.joint_activation(self.lin_enc(enc_out) + self.lin_dec(dec_out))
  45. else:
  46. joint_out = self.joint_activation(enc_out + dec_out)
  47. return self.lin_out(joint_out)