code: def train_ivf_index_with_2level(index, xt, **args):
      """
      Applies 2-level clustering to an index_ivf embedded in an index.
      """
      # handle PreTransforms
      index = faiss.downcast_index(index)
      if isinstance(index, faiss.IndexPreTransform):
          for i in range(index.chain.size()):
              vt = index.chain.at(i)
              vt.train(xt)
              xt = vt.apply(xt)
          train_ivf_index_with_2level(index.index, xt, **args)
          index.is_trained = True
          return
      assert isinstance(index, faiss.IndexIVF)
      assert index.metric_type == faiss.METRIC_L2
      # now do 2-level clustering
      nc1 = int(np.sqrt(index.nlist))
      print("REBALANCE=", args)
  
      centroids, _ = two_level_clustering(xt, nc1, index.nlist, **args)
      index.quantizer.train(centroids)
      index.quantizer.add(centroids)
      # finish training
      index.train(xt)
  
imports: import numpy
  import faiss
  import time
  from multiprocessing.pool import ThreadPool
label_desc: Use image node_id 3 for calling a global function with example usage: train_ivf_index_with_2level(index, xt) without return types