def METHOD_NAME(self, sample, net_output):
    attn_prob = net_output[1]["attn"][0]
    bsz, tgt_sz, src_sz = attn_prob.shape
    attn = attn_prob.view(bsz * tgt_sz, src_sz)
    align = sample["alignments"]
    align_weights = sample["align_weights"].float()
    if len(align) > 0:
        # Alignment loss computation. align (shape [:, 2]) contains the src-tgt index pairs corresponding to
        # the alignments. align_weights (shape [:]) contains the 1 / frequency of a tgt index for normalizing.
        loss = -(
            (attn[align[:, 1][:, None], align[:, 0][:, None]]).log()
            * align_weights[:, None]
        ).sum()
    else:
        return None
    return loss