code: def index_shard(self):
      assert os.path.exists(self.index_template_file)
      index = faiss.read_index(self.index_template_file)
      index_ivf = faiss.downcast_index(faiss.extract_index_ivf(index))
      assert self.nprobe <= index_ivf.quantizer.ntotal, (
          f"the number of vectors {index_ivf.quantizer.ntotal} is not enough"
          f" to retrieve {self.nprobe} neighbours, check."
      )
      cpu_quantizer = index_ivf.quantizer
      gpu_quantizer = faiss.index_cpu_to_all_gpus(cpu_quantizer)
  
      for i in range(0, self.nshards):
          sfn = f"{self.index_shard_prefix}{i}"
          try:
              index.reset()
              index_ivf.quantizer = gpu_quantizer
              with open(sfn, "xb"):
                  start = i * self.shard_size
                  jj = 0
                  embeddings_batch_size = min(
                      EMBEDDINGS_BATCH_SIZE, self.shard_size
                  )
                  assert (
                      self.shard_size % embeddings_batch_size == 0
                      or EMBEDDINGS_BATCH_SIZE % embeddings_batch_size
                      == 0
                  ), (
                      f"the shard size {self.shard_size} and embeddings"
                      f" shard size  {EMBEDDINGS_BATCH_SIZE} are not"
                      " divisible"
                  )
  
                  for xb_j in tqdm(
                      self._iterate_transformed(
                          self.xb_ds,
                          start,
                          embeddings_batch_size,
                          np.float32,
                      ),
                      file=sys.stdout,
                  ):
                      assert xb_j.shape[1] == index.d
                      index.add_with_ids(
                          xb_j,
                          np.arange(
                              start + jj, start + jj + xb_j.shape[0]
                          ),
                      )
                      jj += xb_j.shape[0]
                      logging.info(jj)
                      assert (
                          jj <= self.shard_size
                      ), f"jj {jj} and shard_zide {self.shard_size}"
                      if jj == self.shard_size:
                          break
              logging.info(f"writing {sfn}...")
              index_ivf.quantizer = cpu_quantizer
              faiss.write_index(index, sfn)
          except FileExistsError:
              logging.info(f"skipping shard: {i}")
              continue
      logging.info("done")
  
imports: import faiss
  import numpy
  import os
  from tqdm import tqdm, trange
  import sys
  import logging
  from faiss.contrib.ondisk import merge_ondisk
  from faiss.contrib.big_batch_search import big_batch_search
  from faiss.contrib.exhaustive_search import knn_ground_truth
  from faiss.contrib.evaluation import knn_intersection_measure
  from utils import get_intersection_cardinality_frequencies, margin, is_pretransform_index
  from dataset import create_dataset_from_oivf_config
label_desc: Use image node_id 7 for calling the OfflineIVF obj's underlying member method code with example usage: obj.index_shard() without return types