Skip to content

Latest commit

 

History

History
82 lines (66 loc) · 3.8 KB

README.md

File metadata and controls

82 lines (66 loc) · 3.8 KB

scann-core tutorial

A hands-on walk through ScaNN on a real dataset: from exact search to a tuned index serving nearly half a million queries per second, then saving it, updating it, running it from C++ and Rust, and searching it from PyTorch and TensorFlow models.

Part You'll learn
1. A first index Building and querying a brute-force index; what the results mean
2. Measuring Recall, throughput and latency, measured properly; quantized brute force
3. The pipeline Partitioning, asymmetric hashing and reordering, one stage at a time
4. Tuning Trading speed for recall, at query time and at build time
5. Saving and serving Serialization, index size, batching and threads
6. Updating Inserting, updating and deleting points in a live index
7. C++ and Rust The same pipeline from C++ and Rust, sharing indexes with Python
8. PyTorch and TensorFlow scann.torch and scann.tf: tensors in and out, a compiled model that encodes and searches, GPU queries, exporting and saving models with the index inside, and when to use them rather than plain Python

Every number in this tutorial comes from running the scripts in code/. There is one script per part (two for part 8), and each part shows its script's actual output. They were run on an AMD Threadripper PRO 7975WX (32 cores, 64 threads, AVX-512), built with -march=native and clang 23; part 8's GPU queries ran on an NVIDIA RTX 3060 Ti. Your absolute speeds will differ, but the comparisons between configurations should hold. benchmarks.md has comparable numbers for the upstream wheel, and for aarch64 (AWS Graviton4).

Each output is from a single run, with file paths shortened. Rerunning a script gives throughput within a few percent. Recall is usually identical, but it can move by a few thousandths when an index is rebuilt: tree training starts from randomly chosen centres, the Python default.

For background on why the techniques work, the tutorial links to algorithms.md and the anisotropic quantization explainer; for every option's exact meaning, api_reference.md. For short programs on synthetic data, with no download, see the examples.

Setup

Build scann-core with the Python package (see the README):

cmake -S . -B build -G Ninja -DSCANN_ARCH_FLAGS="-march=native"
cmake --build build

-march=native lets the compiler use every instruction set your CPU has. The portable default (-mavx -mfma) works too, just more slowly.

Then make a Python environment with numpy, protobuf and h5py, and point it at the package:

python -m venv .venv
.venv/bin/pip install numpy "protobuf>=7.36.2" h5py
export PYTHONPATH=$PWD/build/python
cd docs/tutorial/code
../../../.venv/bin/python part1_first_index.py

The dataset

The tutorial uses GloVe-100 from ann-benchmarks:

  • 1,183,514 word vectors of dimension 100;
  • 10,000 held-out query vectors;
  • for every query, its true 100 nearest neighbours by cosine similarity.

The ScaNN paper benchmarks on this dataset. The first script to run downloads it (485 MB) into ~/.cache/scann-core-tutorial, or into $SCANN_TUTORIAL_DATA if set. code/tutorial_data.py loads it and provides two helpers every part uses: recall() and evaluate().

The vectors are L2-normalized on load, so that cosine similarity, which the ground truth uses, is the same as the dot product, which ScaNN searches with. Part 1 explains why that matters.