def batch_all(labels: torch.Tensor, exclude_negatives: bool = True) -> torch.Tensor:
    """Create a 3D mask of all possible triplets.
    @TODO: Docs. Contribution is welcome.
    """
    batch_size = labels.size(0)
    indices_equal = torch.eye(batch_size, device=labels.device).type(torch.bool)
    indices_not_equal = ~indices_equal

    i_not_equal_j = indices_not_equal.unsqueeze(2)
    i_not_equal_k = indices_not_equal.unsqueeze(1)
    j_not_equal_k = indices_not_equal.unsqueeze(0)

    distinct_indices = i_not_equal_j & i_not_equal_k & j_not_equal_k

    label_equal = torch.eq(labels.unsqueeze(0), labels.unsqueeze(1))

    yi_equal_yj = label_equal.unsqueeze(2)
    yi_equal_yk = label_equal.unsqueeze(1)

    yi_not_equal_yk = ~yi_equal_yk
    valid_labels = yi_equal_yj & yi_not_equal_yk

    mask = distinct_indices & valid_labels

    if exclude_negatives:
        mask = mask & create_negative_mask(labels)

    return mask.float()
Create a 3D mask of all possible triplets.
    @TODO: Docs. Contribution is welcome.
    
catalyst/contrib/losses/functional.py