code: def batch_hard_triplet_loss(labels, embeddings, margin, p):
      pairwise_dist = torch.cdist(embeddings, embeddings, p=p)
  
      mask_anchor_positive = _get_anchor_positive_triplet_mask(
          labels
      ).float()
      anchor_positive_dist = mask_anchor_positive * pairwise_dist
  
      # hardest positive for every anchor
      hardest_positive_dist, _ = anchor_positive_dist.max(
          1, keepdim=True
      )
  
      mask_anchor_negative = _get_anchor_negative_triplet_mask(
          labels
      ).float()
  
      # Add max value in each row to invalid negatives
      max_anchor_negative_dist, _ = pairwise_dist.max(1, keepdim=True)
      anchor_negative_dist = (
          pairwise_dist
          + max_anchor_negative_dist * (1.0 - mask_anchor_negative)
      )
  
      # hardest negative for every anchor
      hardest_negative_dist, _ = anchor_negative_dist.min(
          1, keepdim=True
      )
  
      triplet_loss = (
          hardest_positive_dist - hardest_negative_dist + margin
      )
      triplet_loss[triplet_loss < 0] = 0
  
      triplet_loss = triplet_loss.mean()
  
      return triplet_loss, -1
  
file_path: vision/references/similarity/loss.py
imports: import torch
  import torch.nn
label_desc: Use image node_id 1 for calling a global function with example usage: batch_hard_triplet_loss(labels, embeddings, margin, p) and returns: triplet_loss,  unknown