Skip to content

Add hnsw-zig: Zig-native HNSW with quantized search#624

Open
mdvsh wants to merge 1 commit into
erikbern:mainfrom
mdvsh:add-hnswzig
Open

Add hnsw-zig: Zig-native HNSW with quantized search#624
mdvsh wants to merge 1 commit into
erikbern:mainfrom
mdvsh:add-hnswzig

Conversation

@mdvsh
Copy link
Copy Markdown

@mdvsh mdvsh commented Mar 30, 2026

Summary

  • First Zig-native HNSW implementation on ann-benchmarks
  • SQ4 two-phase quantized search (4-bit graph traversal, f32 rescore)
  • comptime SIMD via @Vector: width=16 on x86_64 (AVX-512), width=8 on aarch64 (NEON)
  • Epoch-based visited set, BFS graph reorder, multi-threaded build
  • Multi-dimension support via comptime dispatch (25, 50, 100, 128, 200, 256, 384, 512, 768, 784, 960)
  • 20K QPS at 0.984 recall@10 on SIFT-128 (Apple M4, single-threaded search)

Source

https://github.com/mdvsh/hnsw-zig

Validation

  • Docker image builds on linux/amd64, shared library loads via ctypes
  • End-to-end tested: Docker -> Python ctypes -> Zig .so -> build index -> search -> correct results
  • Multi-dim verified: dim=25, 128, 256, 784 all pass end-to-end
  • 64 unit tests passing (zig build test)
  • CI: hnswzig added to benchmarks.yml matrix

Datasets supported

Covers all ann-benchmarks datasets: SIFT (128), GloVe (25, 50, 100, 200), Fashion-MNIST (784), GIST (960), NYTimes (256), COCO (512)

Each dimension is a separate comptime-monomorphized code path with fully unrolled SIMD. No runtime branching on dimension.

Test plan

  • Docker image builds and .so loads via ctypes
  • End-to-end: fit + query returns correct neighbors
  • Multi-dimension: tested dim=25, 128, 256, 784
  • CI matrix entry added
  • Supports euclidean and angular (cosine via normalize-at-insert)

Pure Zig HNSW with SQ4 quantized search, comptime SIMD, and
BFS graph reorder. Targets x86_64 with AVX-512 detection.

20K QPS at 0.984 recall on SIFT-128 (Apple M4, single-threaded search).
Competitive with hnswlib. First Zig implementation on ann-benchmarks.

Source: https://github.com/mdvsh/hnsw-zig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant