def METHOD_NAME(self, q, k, v, sequence_mask):
    w = torch.matmul(q, k)
    if self.scale:
        w = w / math.sqrt(v.size(-1))
    b_subset = self.b[:, :, : w.size(-2), : w.size(-1)]
    if sequence_mask is not None:
        b_subset = b_subset * sequence_mask.view(sequence_mask.size(0), 1, -1)
        b_subset = b_subset.permute(1, 0, 2, 3)
    w = w * b_subset + -1e9 * (1 - b_subset)
    w = nn.Softmax(dim=-1)(w)
    w = self.attn_dropout(w)
    return torch.matmul(w, v)