code: def test_faiss_index_builder():
      num_data_points = 16
      num_dimensions = 16
  
      config = KNNConfig(
          num_data_points,
          num_dimensions,
          "float32",
          "int32",
          "Flat",
          -1,
      )
      builder = FaissIndexBuilder(config)
      index = builder.init_faiss_index()
  
      # build data
      states = np.outer(
          np.arange(num_data_points, dtype=np.float32),
          np.ones(num_dimensions, dtype=np.float32),
      )
  
      # offset should be < 0.5
      def query_tests(offset):
          # check by querying into the index
          for i in range(1, num_data_points - 1):
              query = np.expand_dims(states[i], axis=0) + offset
              dists, idxs = index.search(
                  query, 3
              )  # 3 because we expect to see itself and the two neighboring ones
  
              # check for idxs
              assert idxs[0][0] == i
  
              # check for dists
              # note that faiss won't do sqrt for the L2 distances
              # (reference: https://github.com/facebookresearch/faiss/wiki/MetricType-and-distances#metric_l2)
              gld_dists = np.array(
                  [
                      [
                          np.power(offset, 2) * num_dimensions,
                          np.power(1 - offset, 2) * num_dimensions,
                          np.power(1 + offset, 2) * num_dimensions,
                      ]
                  ]
              )
              assert np.all(np.isclose(dists, gld_dists))
  
      # test add_items
      builder.add_items(index, states)
      query_tests(0.1)
      index.reset()
  
      # test block_add_items
      builder.block_add_items(
          index, states, block_size=3
      )  # add items with block size of 3
      query_tests(0.1)
  
      # test build_train_sample
      train_sample = builder.build_train_sample(states, 8)
      assert train_sample.shape == (8, num_dimensions)
      assert states.dtype == train_sample.dtype
  
file_path: sockeye/test/unit/test_knn.py
imports: import numpy
  import os
  import pytest
  import tempfile
  import torch
  import sockeye
  import sockeye.constants
  from sockeye.generate_decoder_states import NumpyMemmapStorage, DecoderStateGenerator
  from sockeye.knn import KNNConfig, FaissIndexBuilder, get_config_path, get_state_store_path, get_word_store_path
  from sockeye.vocab import build_vocab
label_desc: Use image node_id 3 for calling a global function with example usage: test_faiss_index_builder() without return types