code: def test_remove_self_comparisons(self):
      for dtype in TEST_DTYPES:
          batch_size = 32
          loss = CrossBatchMemory(
              loss=ContrastiveLoss(),
              embedding_size=self.embedding_size,
              memory_size=self.memory_size,
          )
          loss.embedding_memory = loss.embedding_memory.to(
              TEST_DEVICE
          ).type(dtype)
          loss.label_memory = loss.label_memory.to(TEST_DEVICE)
          embeddings = (
              torch.randn(batch_size, self.embedding_size)
              .to(TEST_DEVICE)
              .type(dtype)
          )
          labels = torch.randint(0, 10, (batch_size,)).to(TEST_DEVICE)
          num_tuples = 1000
          num_non_identical = 147
  
          for i in range(30):
              loss.add_to_memory(embeddings, labels, batch_size)
              for identical in [True, False]:
                  # triplets
                  a = torch.randint(0, batch_size, (num_tuples,)).to(
                      TEST_DEVICE
                  )
                  p = a + loss.curr_batch_idx[0]
                  if not identical:
                      rand_diff_idx = torch.randint(
                          0, num_tuples, (num_non_identical,)
                      ).to(TEST_DEVICE)
                      offsets = torch.randint(
                          -self.memory_size + 1,
                          self.memory_size,
                          (num_non_identical,),
                      ).to(TEST_DEVICE)
                      offsets[offsets == 0] = 1
                      p[rand_diff_idx] += offsets
                  p %= self.memory_size
                  n = torch.randint(0, batch_size, (num_tuples,)).to(
                      TEST_DEVICE
                  )
                  a_new, p_new, n_new = lmu.remove_self_comparisons(
                      (a, p, n), loss.curr_batch_idx, loss.memory_size
                  )
                  if identical:
                      self.assertTrue(
                          len(a_new) == len(p_new) == len(n_new) == 0
                      )
                  else:
                      triplets = set(
                          [
                              (x.item(), y.item(), z.item())
                              for x, y, z in zip(
                                  a[rand_diff_idx],
                                  p[rand_diff_idx],
                                  n[rand_diff_idx],
                              )
                          ]
                      )
                      triplets_new = set(
                          [
                              (x.item(), y.item(), z.item())
                              for x, y, z in zip(a_new, p_new, n_new)
                          ]
                      )
                      self.assertTrue(
                          set(triplets) == set(triplets_new)
                      )
  
                  # pairs
                  a1 = torch.randint(0, batch_size, (num_tuples,)).to(
                      TEST_DEVICE
                  )
                  p = a1 + loss.curr_batch_idx[0]
                  if not identical:
                      rand_diff_idx = torch.randint(
                          0, num_tuples, (num_non_identical,)
                      ).to(TEST_DEVICE)
                      offsets = torch.randint(
                          -self.memory_size + 1,
                          self.memory_size,
                          (num_non_identical,),
                      ).to(TEST_DEVICE)
                      offsets[offsets == 0] = 1
                      p[rand_diff_idx] += offsets
                  p %= self.memory_size
                  a2 = torch.randint(0, batch_size, (num_tuples,)).to(
                      TEST_DEVICE
                  )
                  n = (
                      torch.randint(0, batch_size, (num_tuples,)).to(
                          TEST_DEVICE
                      )
                      + loss.curr_batch_idx[0]
                  ) % self.memory_size
                  (
                      a1_new,
                      p_new,
                      a2_new,
                      n_new,
                  ) = lmu.remove_self_comparisons(
                      (a1, p, a2, n),
                      loss.curr_batch_idx,
                      loss.memory_size,
                  )
                  if identical:
                      self.assertTrue(len(a1_new) == len(p_new) == 0)
                  else:
                      pos_pairs = set(
                          [
                              (x.item(), y.item())
                              for x, y in zip(
                                  a1[rand_diff_idx], p[rand_diff_idx]
                              )
                          ]
                      )
                      pos_pairs_new = set(
                          [
                              (x.item(), y.item())
                              for x, y in zip(a1_new, p_new)
                          ]
                      )
                      self.assertTrue(
                          set(pos_pairs) == set(pos_pairs_new)
                      )
  
                  self.assertTrue(torch.equal(a2_new, a2))
                  self.assertTrue(torch.equal(n_new, n))
  
imports: import unittest
  import torch
  import pytorch_metric_learning.losses
  from pytorch_metric_learning.losses import ContrastiveLoss, CrossBatchMemory, MultiSimilarityLoss, NTXentLoss
  from pytorch_metric_learning.miners import DistanceWeightedMiner, MultiSimilarityMiner, PairMarginMiner, TripletMarginMiner
  from pytorch_metric_learning.utils import loss_and_miner_utils
  from ..None import TEST_DEVICE, TEST_DTYPES
  from ..zzz_testing_utils.testing_utils import angle_to_coord
label_desc: Use image node_id 3 for calling the TestCrossBatchMemory obj's underlying member method code with example usage: obj.test_remove_self_comparisons() without return types