The PyTorch code that produces the error:
torch/nn/functional.py", line 3362, in multi_head_attention_forward:
if attn_mask is not None:
    attn_mask = attn_mask.unsqueeze(0)
    attn_output_weights += attn_mask

Commenting out the attn_mask.unsqueeze(0) line makes the code run without errors. Is this a bug in PyTorch? Why add a dimension to the beginning of the attention mask?