Best Practices
This document describes how to test the optimized hnswlib on the Kunpeng platform. The test depends on the Kunpeng optimization patch file 0001-hnswlib_0.8.0-optimize-fp32_fp16_dis_idsort_prefetch.patch.
The following example uses the fashion-mnist-784-euclidean.hdf5 dataset.
Install hnswlib as described in Installation Guide.
Obtain a test program. Assume that the path is
/path/to/sra_test.git clone https://gitcode.com/openeuler/sra_test.gitCreate a
datafolder and obtain the test dataset.cd /path/to/sra_test mkdir data && cd data wget http://ann-benchmarks.com/fashion-mnist-784-euclidean.hdf5 --no-check-certificateCompile the executable file.
make hnswlib_test // FP32 data type, using NEON vectorization instructions. make hnswlib_fp16_test // FP16 data type, using NEON vectorization instructions.The complete directory structure is as follows:
├── configs // Stores configuration files for the algorithm and dataset. │ └── hnswlib_fp16/ │ └── hnswlib_fp16_fashion-mnist-784-euclidean.config ├── data // Stores the datasets. │ └── fashion-mnist-784-euclidean.hdf5 ├── include // Stores header files of the test framework. ├── indexes/ │ └── hnswlib_fp16/ // Stores the built index, which needs to be manually created. │ └── fashion.hnswlib // Built index, generated when the executable file hnswlib_fp16_test runs and the save_or_load parameter in the dataset configuration file is set to save. ├── scripts/ │ └── build.sh // Script file ├── src // Stores source files of the test framework. ├── get_result.py ├── LICENSE ├── Makefile ├── README.md ├── test.sh // Script for performing performance tests ├── test_multi-numas.sh // Script for performing performance tests on multiple NUMA nodes ├── hnswlib_fp16_test // Executable file generated after compilation └── hnswlib_test- During the first compilation, as prompted by the command line, enter the
hnswlibpath and the macros to be enabled. The script automatically saves the corresponding configuration in thebuildfolder. In subsequent compilations, you can directly modify the configuration in theconfig_hnswlib.shorconfig_hnswlib_fp16.shfile under thebuildfolder and runhnswlib_testorhnswlib_fp16_testagain. - To compile
hnswlib_test, enable the macro-DUSE_NEON. - To compile
hnswlib_fp16_test, enable the macros-DUSE_NEONand-DUSE_FP16.
- During the first compilation, as prompted by the command line, enter the
Run the test scripts.
sh test.sh hnswlib // FP32 data type, using NEON vectorization instructions. sh test.sh hnswlib_fp16 // FP16 data type, using NEON vectorization instructions.The test results are stored in the
outputfolder. Pay attention to the QPS metric in the test results and ensure that the recall rate is greater than 0.99.- The
/configsfolder contains the configuration information for different datasets.index_pathis the path for saving the index, andsave_or_loadis the option for building or loading the graph index. - When
save_or_loadis set tosave, the graph construction mode is used. In this case, the constructed graph index is saved to/index_path. - When
save_or_loadis set toload, the loading mode is used. In this case, the graph index is loaded from/index_pathfor subsequent search. - During the first run, execute the performance test script in
savemode on a single NUMA node and save the graph index. - In subsequent runs, if the values of
k_fandefcdo not need to be changed, you can run the script inloadmode across all 4 NUMA nodes to obtain performance data. - If the values of
k_fandefcneed to be changed, you need to reconstruct the graph insavemode.
- The
Performance Optimization Suggestions
- Select a proper data type. When high precision is not required, preferentially use FP16 to reduce memory footprint and improve performance.
- Adjust parameters. Adjust the
M,efc, andefsparameters. For details, see section Tuning Guide for Performance Test Parameters. - Perform batch queries. Process queries in batches as much as possible to reduce thread creation overhead.
Tuning Guide for Performance Test Parameters
Assume that the path to the configuration file for parameter tuning is /path/to/sra_test/configs. The directory structure of the folder is as follows:
├── configs/
├── hnswlib/
├── hnswlib_fp16/
└── hnswlib_fp16_fashion-mnist-784-euclidean.configThe content of the config file is as follows:
# HNSWLIB
k_f = 16
efs = 30
efc = 200
metric = L2
nloop = 3
num_threads = 80
top_k = 10
batch_mode = false
batch_size = 100
save_or_load = save
index_path = indexes/hnswlib_fp16/fashion.hnswlibTable 1 Parameters to be tuned describes the parameters to be tuned.
Table 1 Parameters to be tuned
Tune the above parameters when save_or_load is set to save. It is recommended that a single NUMA node is used for index construction. The tuning objective is to identify the parameter combination that achieves the highest QPS while maintaining a recall rate greater than 0.99.
Run the following commands to check whether the compilation and execution environments are normal and whether the algorithm functions correctly.
Create a unit test (UT) execution script.
cd /path/to/hnswlib vim run.shThe content of the
run.shscript is as follows:rm -rf build mkdir build cd build cmake .. make ./distance_calculation_neon_testPaste the preceding content to the script, press
Esc, enter:wq, and pressEnterto save the file and exit.Run the UT test.
sh run.sh