attention.py 851 B

12345678910111213141516171819202122
  1. #!/usr/bin/env python3
  2. # -*- encoding: utf-8 -*-
  3. # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved.
  4. # MIT License (https://opensource.org/licenses/MIT)
  5. import torch
  6. from funasr.models.sanm.attention import MultiHeadedAttentionSANM
  7. class MultiHeadedAttentionSANMwithMask(MultiHeadedAttentionSANM):
  8. def __init__(self, *args, **kwargs):
  9. super().__init__(*args, **kwargs)
  10. def forward(self, x, mask, mask_shfit_chunk=None, mask_att_chunk_encoder=None):
  11. q_h, k_h, v_h, v = self.forward_qkv(x)
  12. fsmn_memory = self.forward_fsmn(v, mask[0], mask_shfit_chunk)
  13. q_h = q_h * self.d_k ** (-0.5)
  14. scores = torch.matmul(q_h, k_h.transpose(-2, -1))
  15. att_outs = self.forward_attention(v_h, scores, mask[1], mask_att_chunk_encoder)
  16. return att_outs + fsmn_memory