Final solution for HardTripletloss works pretty well with all numpy eq functions in pytorch , after upgrading Pytorch to unstable version (1.8…) where it consisted all functions i needed to do operation and sticking to nn , found Autograd too hard, will not use it at least for now.
class HardTripletLoss(nn.Module):
    def __init__(self):
        super(HardTripletLoss, self).__init__()

    def forward(self, v1, v2, margin):
        scores = v1 @ v2.T
        batch_size = len(scores)
        positive = torch.diag(scores)
        negative_without_positive = scores - 2.0 * torch.eye(batch_size)
        closest_negative = negative_without_positive.max(axis=1)[0]
        negative_zero_on_duplicate = scores * (1.0 - torch.eye(batch_size))
        mean_negative = torch.sum(negative_zero_on_duplicate, 1) / (batch_size - 1)
        triplet_loss1 = torch.maximum(margin - positive + mean_negative, torch.tensor(0))
        triplet_loss2 = torch.maximum(margin - positive + closest_negative, torch.tensor(0))
        triplet_loss = torch.mean(triplet_loss2 + triplet_loss1)
        return triplet_loss


Thanks