Skip to main content

Python bindings for HNSW (Hierarchical Navigable Small World) vector search

Project description

pyhnsw - Python Bindings for HNSW

Fast approximate nearest neighbor search using Hierarchical Navigable Small World (HNSW) graphs.

Features

  • Fast: SIMD-accelerated distance calculations using Eigen
  • Simple: Clean Python API similar to popular ANN libraries
  • Flexible: Supports both L2 and cosine distance metrics
  • Efficient: Batch operations for adding and searching multiple vectors
  • Type Support: Both single (float32) and double (float64) precision

Installation

From Source

First, ensure you have the required dependencies:

pip install numpy pybind11

Then build and install the package:

cd bindings/pyhnsw
pip install .

Build with CMake (Alternative)

cd bindings/pyhnsw
mkdir build && cd build
cmake ..
make

Quick Start

import numpy as np
import pyhnsw

# Create an index for 128-dimensional vectors
index = pyhnsw.HNSW(dim=128, M=16, ef_construction=200, ef_search=100, metric="l2")

# Generate some random data
data = np.random.randn(10000, 128).astype(np.float32)

# Add vectors to the index
index.add_items(data)

# Search for nearest neighbors
query = np.random.randn(128).astype(np.float32)
indices, distances = index.search(query, k=10)

print(f"Found {len(indices)} nearest neighbors")
print(f"Indices: {indices}")
print(f"Distances: {distances}")

API Reference

HNSW Class

pyhnsw.HNSW(dim, M=16, ef_construction=200, ef_search=100, metric="l2")

Creates a new HNSW index.

Parameters:

  • dim (int): Dimensionality of the vectors
  • M (int): Maximum number of connections per element (default: 16)
  • ef_construction (int): Size of the dynamic candidate list during construction (default: 200)
  • ef_search (int): Size of the dynamic candidate list during search (default: 100)
  • metric (str): Distance metric to use ('l2' or 'cosine', default: 'l2')

Methods

add_item(vec)

Add a single vector to the index.

Parameters:

  • vec (numpy.ndarray): 1D array of shape (dim,) containing the vector

add_items(data)

Add multiple vectors to the index.

Parameters:

  • data (numpy.ndarray): 2D array of shape (n_items, dim) containing the vectors

search(query, k=10)

Search for k nearest neighbors of a query vector.

Parameters:

  • query (numpy.ndarray): 1D array of shape (dim,) containing the query vector
  • k (int): Number of nearest neighbors to return (default: 10)

Returns:

  • indices (numpy.ndarray): 1D array of shape (k,) containing the indices of nearest neighbors
  • distances (numpy.ndarray): 1D array of shape (k,) containing the distances to nearest neighbors

batch_search(queries, k=10)

Search for k nearest neighbors of multiple query vectors.

Parameters:

  • queries (numpy.ndarray): 2D array of shape (n_queries, dim) containing the query vectors
  • k (int): Number of nearest neighbors to return (default: 10)

Returns:

  • indices (numpy.ndarray): 2D array of shape (n_queries, k) containing the indices
  • distances (numpy.ndarray): 2D array of shape (n_queries, k) containing the distances

size()

Get the number of items in the index.

dim()

Get the dimensionality of vectors in the index.

Examples

Using Cosine Similarity

import numpy as np
import pyhnsw

# Create index with cosine metric
index = pyhnsw.HNSW(dim=512, metric="cosine")

# For cosine similarity, it's recommended to normalize vectors
data = np.random.randn(1000, 512).astype(np.float32)
data = data / np.linalg.norm(data, axis=1, keepdims=True)

index.add_items(data)

# Search with normalized query
query = np.random.randn(512).astype(np.float32)
query = query / np.linalg.norm(query)

indices, distances = index.search(query, k=5)

Batch Processing

import numpy as np
import pyhnsw

# Create index
index = pyhnsw.HNSW(dim=256)

# Add data in batches
batch_size = 1000
for i in range(0, 10000, batch_size):
    batch = np.random.randn(batch_size, 256).astype(np.float32)
    index.add_items(batch)

# Batch search
queries = np.random.randn(100, 256).astype(np.float32)
indices, distances = index.batch_search(queries, k=20)

# Process results
for i, (idx, dist) in enumerate(zip(indices, distances)):
    print(f"Query {i}: Found {len(idx)} neighbors")

Double Precision

import numpy as np
import pyhnsw

# Use HNSWDouble for float64 precision
index = pyhnsw.HNSWDouble(dim=128)

# Use float64 arrays
data = np.random.randn(1000, 128).astype(np.float64)
index.add_items(data)

query = np.random.randn(128).astype(np.float64)
indices, distances = index.search(query, k=10)

Performance Tips

  1. Parameter Tuning:

    • Increase M for better recall at the cost of memory and construction time
    • Increase ef_construction for better index quality at the cost of construction time
    • Increase ef_search for better search recall at the cost of search time
  2. Batch Operations:

    • Use add_items() instead of multiple add_item() calls
    • Use batch_search() for multiple queries
  3. Data Preprocessing:

    • Normalize vectors when using cosine similarity
    • Use float32 for better performance unless you need double precision

Testing

Run the test suite:

python test_pyhnsw.py

Dependencies

  • Python >= 3.7
  • NumPy >= 1.19.0
  • pybind11 >= 2.6.0
  • Eigen3 (automatically fetched during build)
  • C++17 compatible compiler

License

See the main project LICENSE file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pyhnsw-0.0.4-cp312-cp312-win_amd64.whl (121.0 kB view details)

Uploaded CPython 3.12Windows x86-64

pyhnsw-0.0.4-cp312-cp312-musllinux_1_1_x86_64.whl (697.5 kB view details)

Uploaded CPython 3.12musllinux: musl 1.1+ x86-64

pyhnsw-0.0.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (182.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

pyhnsw-0.0.4-cp311-cp311-win_amd64.whl (119.5 kB view details)

Uploaded CPython 3.11Windows x86-64

pyhnsw-0.0.4-cp311-cp311-musllinux_1_1_x86_64.whl (696.9 kB view details)

Uploaded CPython 3.11musllinux: musl 1.1+ x86-64

pyhnsw-0.0.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (182.1 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

pyhnsw-0.0.4-cp310-cp310-win_amd64.whl (118.7 kB view details)

Uploaded CPython 3.10Windows x86-64

pyhnsw-0.0.4-cp310-cp310-musllinux_1_1_x86_64.whl (696.5 kB view details)

Uploaded CPython 3.10musllinux: musl 1.1+ x86-64

pyhnsw-0.0.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (180.9 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

pyhnsw-0.0.4-cp38-cp38-musllinux_1_1_x86_64.whl (696.0 kB view details)

Uploaded CPython 3.8musllinux: musl 1.1+ x86-64

pyhnsw-0.0.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (180.9 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

Details for the file pyhnsw-0.0.4-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: pyhnsw-0.0.4-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 121.0 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyhnsw-0.0.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 c1d9a6202679f8dff348e1f1c44f823ae883fc98246774c7b6fd040bc929c2fe
MD5 742899f9ecd6b6ee8ecb6022108b0901
BLAKE2b-256 242bd937f996c6e38e331346e110a1e2ef547f40813a9f9c5ffa0f4313ef7b81

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.4-cp312-cp312-win_amd64.whl:

Publisher: build-and-test.yml on dicroce/hnsw

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhnsw-0.0.4-cp312-cp312-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for pyhnsw-0.0.4-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 dd186f58fa7b87f47a955c4dc145262a511b8bd00395edb93669b834096ea0e6
MD5 dc5c7ad34aad060fc9e7e285fb650b63
BLAKE2b-256 2210d5282a4c66abae44babf91ee96ba3bb9f4fb538044031cd4d6b7781ab72b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.4-cp312-cp312-musllinux_1_1_x86_64.whl:

Publisher: build-and-test.yml on dicroce/hnsw

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhnsw-0.0.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyhnsw-0.0.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9fcc80d8dded5f7a200df6984f141d4c7f337b6ae0a677dc8d5374571eaaf5df
MD5 7cfbc631ab9d2a315de3fccfb9e193ae
BLAKE2b-256 48af8dc9d8461b6d95263d30a5beae46db2e1974427da413191f75c68bd160cd

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build-and-test.yml on dicroce/hnsw

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhnsw-0.0.4-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: pyhnsw-0.0.4-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 119.5 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyhnsw-0.0.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c62d93bcb2f638b5e543be073d74449705219cea5c66552704ac3316821cacc3
MD5 1fea056003fa71494d37da650f73c830
BLAKE2b-256 c2d71b11992ff8d1b63379572d7ef9bd1ee98a9d8fbe7f46f352b159a21ea57b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.4-cp311-cp311-win_amd64.whl:

Publisher: build-and-test.yml on dicroce/hnsw

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhnsw-0.0.4-cp311-cp311-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for pyhnsw-0.0.4-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 e2e0ca675a6f0290fb524d272bf0933fcd499f09537de970844cd5ab049c0b2e
MD5 b6ef789a76d29b1a8295aaa15d24cfe1
BLAKE2b-256 c6677a3b7caaf4246aa160cf4c9d0494fdcd9c4aea0306aa42caf2a6830b856d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.4-cp311-cp311-musllinux_1_1_x86_64.whl:

Publisher: build-and-test.yml on dicroce/hnsw

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhnsw-0.0.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyhnsw-0.0.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e49c264dfb6847051edcfa11b33e69782d12964452dbc12035890839a6f87c53
MD5 cafc56dea26bf786072ce053ad966636
BLAKE2b-256 db3848b05661f8202b8340ebbe9394f0b677db19eada54ebef8d84d32ef27a00

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build-and-test.yml on dicroce/hnsw

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhnsw-0.0.4-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: pyhnsw-0.0.4-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 118.7 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyhnsw-0.0.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 9e3ad747787c803222607d4c8cd16f2268a184ee5b87c8ce2ec6315f9f9d62b0
MD5 92e5d9584a2afb54581c8644efdb396a
BLAKE2b-256 906997b74904789a9ec621ad817221b18ce6d16e37aa64e1ec31c64414112cbb

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.4-cp310-cp310-win_amd64.whl:

Publisher: build-and-test.yml on dicroce/hnsw

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhnsw-0.0.4-cp310-cp310-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for pyhnsw-0.0.4-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 81dca4983f757fcf6d8fed9911be50d89a4ea672368995cbca2544a7568be894
MD5 dddb0912d2c691a08d9e20a553f8ae25
BLAKE2b-256 709b18f6a02403e948038c1a9502776d6d113609f5e016edd69d6907991c59d5

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.4-cp310-cp310-musllinux_1_1_x86_64.whl:

Publisher: build-and-test.yml on dicroce/hnsw

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhnsw-0.0.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyhnsw-0.0.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 530674cacfebcc4d4c8ff5f1a8cb90a782a024f019e16d44770dfa20f9663467
MD5 ae4e22d8446f14f42cdc720f02d1b1c6
BLAKE2b-256 5862c9758d96533e25dbfe9ba8a9cdc78ba9ce7e3b590cfae646f6b5eebf3e68

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build-and-test.yml on dicroce/hnsw

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhnsw-0.0.4-cp38-cp38-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for pyhnsw-0.0.4-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 dee08f51e3843b8dc59576aea34c6eac0a1c43588c8db761c392be905df2553a
MD5 1e31ef1eb3fcc4f37a4e488836111ef9
BLAKE2b-256 7a2b6529d5cd6a11ec20b6f8db4699e0ed8ea58d128a1f21047b7a827c3aaa87

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.4-cp38-cp38-musllinux_1_1_x86_64.whl:

Publisher: build-and-test.yml on dicroce/hnsw

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhnsw-0.0.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyhnsw-0.0.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 aaad0f030fa18c9e1bc33e8db5d682defad5b913ff7839ae75a2b69fc705dc83
MD5 7535643651ebd23d8495d9446822c1ee
BLAKE2b-256 c09a6890df8b370b999017e2b463c92ed7f519440587ad9d92cf5b1589db156b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build-and-test.yml on dicroce/hnsw

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page