reference.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. from typing import Optional
  2. import torch
  3. import torch.nn.functional as F
  4. from torch import nn
  5. from .wavenet import WaveNet
  6. class ReferenceEncoder(WaveNet):
  7. def __init__(
  8. self,
  9. input_channels: Optional[int] = None,
  10. output_channels: Optional[int] = None,
  11. residual_channels: int = 512,
  12. residual_layers: int = 20,
  13. dilation_cycle: Optional[int] = 4,
  14. num_heads: int = 8,
  15. latent_len: int = 4,
  16. ):
  17. super().__init__(
  18. input_channels=input_channels,
  19. residual_channels=residual_channels,
  20. residual_layers=residual_layers,
  21. dilation_cycle=dilation_cycle,
  22. )
  23. self.head_dim = residual_channels // num_heads
  24. self.num_heads = num_heads
  25. self.latent_len = latent_len
  26. self.latent = nn.Parameter(torch.zeros(1, self.latent_len, residual_channels))
  27. self.q = nn.Linear(residual_channels, residual_channels, bias=True)
  28. self.kv = nn.Linear(residual_channels, residual_channels * 2, bias=True)
  29. self.q_norm = nn.LayerNorm(self.head_dim)
  30. self.k_norm = nn.LayerNorm(self.head_dim)
  31. self.proj = nn.Linear(residual_channels, residual_channels)
  32. self.proj_drop = nn.Dropout(0.1)
  33. self.norm = nn.LayerNorm(residual_channels)
  34. self.mlp = nn.Sequential(
  35. nn.Linear(residual_channels, residual_channels * 4),
  36. nn.SiLU(),
  37. nn.Linear(residual_channels * 4, residual_channels),
  38. )
  39. self.output_projection_attn = nn.Linear(residual_channels, output_channels)
  40. torch.nn.init.trunc_normal_(self.latent, std=0.02)
  41. self.apply(self.init_weights)
  42. def init_weights(self, m):
  43. if isinstance(m, nn.Linear):
  44. torch.nn.init.trunc_normal_(m.weight, std=0.02)
  45. if m.bias is not None:
  46. torch.nn.init.constant_(m.bias, 0)
  47. def forward(self, x, attn_mask=None):
  48. x = super().forward(x).mT
  49. B, N, C = x.shape
  50. # Calculate mask
  51. if attn_mask is not None:
  52. assert attn_mask.shape == (B, N) and attn_mask.dtype == torch.bool
  53. attn_mask = attn_mask[:, None, None, :].expand(
  54. B, self.num_heads, self.latent_len, N
  55. )
  56. q_latent = self.latent.expand(B, -1, -1)
  57. q = (
  58. self.q(q_latent)
  59. .reshape(B, self.latent_len, self.num_heads, self.head_dim)
  60. .transpose(1, 2)
  61. )
  62. kv = (
  63. self.kv(x)
  64. .reshape(B, N, 2, self.num_heads, self.head_dim)
  65. .permute(2, 0, 3, 1, 4)
  66. )
  67. k, v = kv.unbind(0)
  68. q, k = self.q_norm(q), self.k_norm(k)
  69. x = F.scaled_dot_product_attention(q, k, v, attn_mask=attn_mask)
  70. x = x.transpose(1, 2).reshape(B, self.latent_len, C)
  71. x = self.proj(x)
  72. x = self.proj_drop(x)
  73. x = x + self.mlp(self.norm(x))
  74. x = self.output_projection_attn(x)
  75. x = x.mean(1)
  76. return x
  77. if __name__ == "__main__":
  78. with torch.autocast(device_type="cpu", dtype=torch.bfloat16):
  79. model = ReferenceEncoder(
  80. input_channels=128,
  81. output_channels=64,
  82. residual_channels=384,
  83. residual_layers=20,
  84. dilation_cycle=4,
  85. num_heads=8,
  86. )
  87. x = torch.randn(4, 128, 64)
  88. mask = torch.ones(4, 64, dtype=torch.bool)
  89. y = model(x, mask)
  90. print(y.shape)
  91. loss = F.mse_loss(y, torch.randn(4, 64))
  92. loss.backward()