code: def test_search_and_reconstruct(self):
      d = 32
      nlist = 10
      M = 4
      k = 5
      quantizer = faiss.IndexFlatL2(d)
      index = faiss.IndexIVFPQ(quantizer, d, nlist, M, 4)
  
      xb = torch.rand(1000, d, dtype=torch.float32)
      index.train(xb)
  
      # different set
      xb = torch.rand(500, d, dtype=torch.float32)
      index.add(xb)
  
      # torch cpu as ground truth
      xq = torch.rand(10, d, dtype=torch.float32)
      D, I, R = index.search_and_reconstruct(xq, k)
  
      # compare against numpy
      D_np, I_np, R_np = index.search_and_reconstruct(xq.numpy(), k)
  
      self.assertTrue(np.array_equal(D.numpy(), D_np))
      self.assertTrue(np.array_equal(I.numpy(), I_np))
      self.assertTrue(np.array_equal(R.numpy(), R_np))
  
      # numpy input values
      D_input = np.zeros((xq.shape[0], k), dtype=np.float32)
      I_input = np.zeros((xq.shape[0], k), dtype=np.int64)
      R_input = np.zeros((xq.shape[0], k, d), dtype=np.float32)
  
      index.search_and_reconstruct(
          xq.numpy(), k, D_input, I_input, R_input
      )
  
      self.assertTrue(np.array_equal(D.numpy(), D_input))
      self.assertTrue(np.array_equal(I.numpy(), I_input))
      self.assertTrue(np.array_equal(R.numpy(), R_input))
  
      # torch input values
      D_input = torch.zeros(xq.shape[0], k, dtype=torch.float32)
      I_input = torch.zeros(xq.shape[0], k, dtype=torch.int64)
      R_input = torch.zeros(xq.shape[0], k, d, dtype=torch.float32)
  
      index.search_and_reconstruct(xq, k, D_input, I_input, R_input)
  
      self.assertTrue(torch.equal(D, D_input))
      self.assertTrue(torch.equal(I, I_input))
      self.assertTrue(torch.equal(R, R_input))
  
file_path: faiss/tests/torch_test_contrib.py
imports: import faiss
  import torch
  import unittest
  import numpy
  import faiss.contrib.torch_utils
label_desc: Use image node_id 8 for calling the TestTorchUtilsCPU obj's underlying member method code with example usage: obj.test_search_and_reconstruct() without return types