mask.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # Copyright 2019 Shigeki Karita
  2. # Apache 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
  3. """Mask module."""
  4. import torch
  5. def subsequent_mask(size, device="cpu", dtype=torch.bool):
  6. """Create mask for subsequent steps (size, size).
  7. :param int size: size of mask
  8. :param str device: "cpu" or "cuda" or torch.Tensor.device
  9. :param torch.dtype dtype: result dtype
  10. :rtype: torch.Tensor
  11. >>> subsequent_mask(3)
  12. [[1, 0, 0],
  13. [1, 1, 0],
  14. [1, 1, 1]]
  15. """
  16. ret = torch.ones(size, size, device=device, dtype=dtype)
  17. return torch.tril(ret, out=ret)
  18. def target_mask(ys_in_pad, ignore_id):
  19. """Create mask for decoder self-attention.
  20. :param torch.Tensor ys_pad: batch of padded target sequences (B, Lmax)
  21. :param int ignore_id: index of padding
  22. :param torch.dtype dtype: result dtype
  23. :rtype: torch.Tensor (B, Lmax, Lmax)
  24. """
  25. ys_mask = ys_in_pad != ignore_id
  26. m = subsequent_mask(ys_mask.size(-1), device=ys_mask.device).unsqueeze(0)
  27. return ys_mask.unsqueeze(-2) & m