code: def test_ivfpq_cpu_coarse(self):
      res = faiss.StandardGpuResources()
      d = 32
      nb = 50000
      nq = 20
      nlist_lvl_1 = 10
      nlist_lvl_2 = 1000
      nprobe_lvl_1 = 3
      nprobe_lvl_2 = 10
  
      rs = np.random.RandomState(567)
      coarse_centroids = rs.rand(nlist_lvl_2, d).astype("float32")
  
      # Construct an IVFFlat index for usage as a coarse quantizer
      idx_coarse_cpu = faiss.IndexIVFFlat(
          faiss.IndexFlatL2(d), d, nlist_lvl_1
      )
      idx_coarse_cpu.set_direct_map_type(faiss.DirectMap.Hashtable)
      idx_coarse_cpu.nprobe = nprobe_lvl_1
  
      idx_coarse_cpu.train(coarse_centroids)
      idx_coarse_cpu.add(coarse_centroids)
      idx_coarse_cpu.make_direct_map()
  
      assert idx_coarse_cpu.ntotal == nlist_lvl_2
  
      idx_cpu = faiss.IndexIVFPQ(idx_coarse_cpu, d, nlist_lvl_2, 4, 8)
  
      xb = rs.rand(nb, d).astype("float32")
      idx_cpu.train(xb)
      idx_cpu.add(xb)
      idx_cpu.nprobe = nprobe_lvl_2
  
      # construct a GPU index using the same trained coarse quantizer
      # from the CPU index
      idx_gpu = faiss.GpuIndexIVFPQ(
          res, idx_coarse_cpu, d, nlist_lvl_2, 4, 8
      )
      assert not idx_gpu.is_trained
  
      idx_gpu.train(xb)
      idx_gpu.add(xb)
      idx_gpu.nprobe = nprobe_lvl_2
  
      k = 10
  
      # precomputed codes also utilize the coarse quantizer
      for use_precomputed in [False, True]:
          idx_gpu.setPrecomputedCodes(use_precomputed)
  
          xq = rs.rand(nq, d).astype("float32")
          d_g, i_g = idx_gpu.search(xq, k)
          d_c, i_c = idx_cpu.search(xq, k)
  
          self.assertGreaterEqual(
              knn_intersection_measure(i_c, i_g), 0.9
          )
  
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import math
  import unittest
  import numpy
  import faiss
  from faiss.contrib import datasets
  from faiss.contrib import ivf_tools
  from faiss.contrib.evaluation import knn_intersection_measure
label_desc: Use image node_id 3 for calling the TestIVFPluggableCoarseQuantizer obj's underlying member method code with example usage: obj.test_ivfpq_cpu_coarse() without return types