import torch


def _get_anchor_positive_mask(labels):
    """Return a 2D mask where mask[a, p] is True if
    a and p are distinct and have same label."""

    indices_equal = torch.eye(labels.size(0)).bool().cuda()
    indices_not_equal = ~indices_equal

    labels_equal = labels.unsqueeze(0) == labels.unsqueeze(1)
    return labels_equal & indices_not_equal