Solved by vainaijr in post #3 
              
              
                I think, when using src_mask, we need to provide a matrix of shape (S, S), where S is our source sequence length, for example, 
import torch, torch.nn as nn
q = torch.randn(3, 1, 10) # source sequence length 3, batch size 1, embedding size 10
attn = nn.MultiheadAttention(10, 1) # embedding size 10, …