A header-only C++20 library for in-memory graph indexing, nearest-neighbor search, and updates on a single machine.
Requirements: CMake >= 3.25, a C++20 compiler, OpenMP, and Boost >= 1.71 (including Program_options).
./build.sh 128 # Build for 128-dimensional vectors
./build.sh # Alternatively, support dimensions 1..2048
./clean.sh # Clear the project-root build directoryRun commands from the project root. Executables are placed in build/.
Fixed-dimension builds require every input vector to match that dimension.
To configure CMake directly:
cmake -S . -B build -DCMAKE_DIM=128 -DCMAKE_BUILD_TYPE=Release
cmake --build build -j4Prefetch is enabled by default. Set -DPREFETCH=disabled to disable it;
use -DPREFETCH=enabled to enable it again.
Set CMAKE_DIM to an empty string for variable dimensions; CMAKE_MAX_DIM sets
the upper bound (default: 2048). Rebuild on the target machine when CPU instruction
sets differ, since the x86 build uses -march=native.
- Data types:
float/float32,uint8,int8, andint16. - Base vectors: XVECS, XBIN, or HNSW vector files.
- Queries: XVECS or XBIN with the same scalar type and dimension as the base.
- Ground truth: uint32 XVECS (
.ivecs) or XBIN, with at least k neighbors per query.
XVECS prefixes each row with its dimension. XBIN stores uint32 row-count and dimension fields followed by contiguous values. Select the scalar type explicitly for external vectors; dynamic indices store their own data type.
build/construction_test --data_path /path/to/base.fvecs --data_type float \
-c 64 -R 64 -b 0.2 -i 100 -t 32 -o /path/to/index.twingraph| Argument | Meaning |
|---|---|
-c |
Construction KNN capacity; must be smaller than the number of vectors |
-R |
Maximum index-graph degree |
-b |
Graph diversification parameter beta |
-i |
Maximum construction iterations; default 100 |
-t |
Thread count; default all available OpenMP threads |
-o |
Output file; default index.twingraph |
--maxnum |
Reserved physical vertex capacity; default input size |
--maxlabel |
External-label capacity; default vertex capacity |
Choose the output suffix:
.twingraph: static index; supply the original base vectors when searching..twingraph_dynamic: stores vectors and update state; required for updates..ivecs: legacy graph output; supply base vectors when searching.
Index file format version: 1.0. Version 0.x files are not accepted.
A suffix-free name receives .twingraph. --out_dir writes a legacy index directory.
To prepare a dynamic index for the sliding-window example below, use a one-million
vector input and reserve capacities:
build/construction_test --data_path /path/to/initial_1m.fbin --data_type float \
-c 64 -R 64 -b 0.2 -i 100 -t 32 \
--maxnum 1200000 --maxlabel 2000000 -o /path/to/index.twingraph_dynamicStatic index or ivecs graph:
build/search_test --index_path /path/to/index.twingraph \
--data_path /path/to/base.fvecs --data_type float \
--query_path /path/to/query.fvecs --gt_path /path/to/gt.ivecs \
--metric l2 -k 10 -t 1 -i 10 --efsearch 10 20 40 80 128 \
-o /path/to/search.csv --raw_out_path /path/to/raw.csvDynamic index (base vectors and their type are loaded from the index):
build/search_test --index_path /path/to/index.twingraph_dynamic \
--query_path /path/to/query.fbin --gt_path /path/to/gt.ivecs \
--metric l2 -k 10 -t 32 -i 10 --efsearch 10 20 40 80 128 \
-o /path/to/search.csv-k selects returned neighbors, -t selects threads (default 1), and -i selects
iterations (default 5). Each --efsearch value must be at least k; omitting the
option uses the default sweep. --expand multiplies the search candidate limit
(default 1). -o records the highest QPS at each efSearch; --raw_out_path records
every iteration's time, QPS and recall. Timing excludes loading and recall evaluation.
--metric accepts l2 (default) or ip (negative inner product). Construction
and updates use L2. Ground truth must match the search metric and returned external
labels. Vectors are not normalized automatically.
Use --index_dir instead of --index_path for a legacy directory.
--graph_path is an alias for --index_path; supply exactly one index input.
Additional search options:
| Option | Values (default) |
|---|---|
--schedule |
static, dynamic, guided (guided) |
--schedule_chunk |
Positive query chunk size (8) |
--entry_batch |
Batch entry-point distances: true / false (true) |
--search_numa |
off, auto, replicate (off); replication uses additional memory |
--prefetch_window |
0: default; 1..64: fixed window; -1: automatic calibration |
The update driver requires a float32 dynamic index. This example replaces 5% of a one-million-vector index per round for 20 rounds, reconstructing every four rounds:
build/sliding_window_test --index_path /path/to/index.twingraph_dynamic \
--update_path /path/to/incoming_1m.fbin \
--query_path /path/to/query.fbin --gt_path /path/to/final_gt.ivecs \
--gt_id_offset 1000000 --output_dir /path/to/results \
--rounds 20 --replace_percent 5 --rejuvenation_interval 4 \
--update_threads 32 --search_threads 1 -k 100 -i 1 \
--efsearch 100 128 192 256 384 512 768 1024--insertion_expansion sets the insertion search candidate capacity multiplier
(default: 2.0, finite and at least 1). The C++ API is
index.setInsertionExpansion(factor).
Incoming vectors must be float32 XBIN in insertion order. --update_begin selects
the first row to load (default 0). Initial labels must be [0,N) with no deletions.
For step = N * replace_percent / 100 (an integer), reserve at least
N + step * min(rounds, rejuvenation_interval) physical slots and
N + step * rounds labels during construction.
Ground truth must describe the final window. In this example its labels are
[1000000,2000000); --gt_id_offset 1000000 converts local truth IDs to those
labels. Omit the offset if the truth already contains external labels.
--metric l2 is the default; ip is also accepted for final search.
Outputs: rounds.csv for update timings, search.csv for final QPS and
Recall@1/@10/@100, and summary.txt for totals. The index stays in memory during
updates; the driver does not save the final index.
build/export_graph --index_path /path/to/index.twingraph_dynamic \
--out_path /path/to/graph.ivecs--head N limits exported external IDs. Use --index_dir for a legacy directory.
To export deletion state without reconstruction, add --deletion_mode include
or valid_only, together with --id_map /path/to/map.bin and
--base_count N (the original base vector count).
For the full argument lists:
build/construction_test --help
build/search_test --help
build/sliding_window_test --help
build/export_graph --help