code: def batch_all_triplet_loss(labels, embeddings, margin, p):
      pairwise_dist = torch.cdist(embeddings, embeddings, p=p)
  
      anchor_positive_dist = pairwise_dist.unsqueeze(2)
      anchor_negative_dist = pairwise_dist.unsqueeze(1)
  
      triplet_loss = (
          anchor_positive_dist - anchor_negative_dist + margin
      )
  
      mask = _get_triplet_mask(labels)
      triplet_loss = mask.float() * triplet_loss
  
      # Remove negative losses (easy triplets)
      triplet_loss[triplet_loss < 0] = 0
  
      # Count number of positive triplets (where triplet_loss > 0)
      valid_triplets = triplet_loss[triplet_loss > 1e-16]
      num_positive_triplets = valid_triplets.size(0)
      num_valid_triplets = mask.sum()
  
      fraction_positive_triplets = num_positive_triplets / (
          num_valid_triplets.float() + 1e-16
      )
  
      # Get final mean triplet loss over the positive valid triplets
      triplet_loss = triplet_loss.sum() / (
          num_positive_triplets + 1e-16
      )
  
      return triplet_loss, fraction_positive_triplets
  
file_path: vision/references/similarity/loss.py
imports: import torch
  import torch.nn
label_desc: Use image node_id 2 for calling a global function with example usage: batch_all_triplet_loss(labels, embeddings, margin, p) and returns: triplet_loss,  fraction_positive_triplets