def generate_index_pairs()

in src/health_runner/nccl_runner.py [0:0]


def generate_index_pairs(length: int) -> list[tuple[int, int]]:
  """Returns random pairs of indices with no repeated items."""
  if length < 2:
    return []

  indices = list(range(length))
  random.shuffle(indices)
  pairs = []

  while len(indices) > 1:
    index1 = indices.pop()
    index2 = indices.pop()
    pairs.append((index1, index2))

  # If there's an odd number of indices, pair the last one randomly
  if indices:
    last_index = indices.pop()
    random_partner = random.randint(0, length - 1)
    # Ensure the index doesn't pair with itself
    while random_partner == last_index:
      random_partner = random.randint(0, length - 1)
    pairs.append((last_index, random_partner))

  return pairs