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 Distribution

pyhnsw-0.0.7.tar.gz (19.6 kB view details)

Uploaded Source

Built Distributions

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

pyhnsw-0.0.7-cp312-cp312-win_amd64.whl (121.3 kB view details)

Uploaded CPython 3.12Windows x86-64

pyhnsw-0.0.7-cp312-cp312-musllinux_1_1_x86_64.whl (697.7 kB view details)

Uploaded CPython 3.12musllinux: musl 1.1+ x86-64

pyhnsw-0.0.7-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (181.6 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

pyhnsw-0.0.7-cp311-cp311-win_amd64.whl (119.7 kB view details)

Uploaded CPython 3.11Windows x86-64

pyhnsw-0.0.7-cp311-cp311-musllinux_1_1_x86_64.whl (697.1 kB view details)

Uploaded CPython 3.11musllinux: musl 1.1+ x86-64

pyhnsw-0.0.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (182.0 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

pyhnsw-0.0.7-cp310-cp310-win_amd64.whl (118.9 kB view details)

Uploaded CPython 3.10Windows x86-64

pyhnsw-0.0.7-cp310-cp310-musllinux_1_1_x86_64.whl (696.4 kB view details)

Uploaded CPython 3.10musllinux: musl 1.1+ x86-64

pyhnsw-0.0.7-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.7-cp39-cp39-win_amd64.whl (120.8 kB view details)

Uploaded CPython 3.9Windows x86-64

pyhnsw-0.0.7-cp39-cp39-musllinux_1_1_x86_64.whl (696.6 kB view details)

Uploaded CPython 3.9musllinux: musl 1.1+ x86-64

pyhnsw-0.0.7-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (180.9 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

pyhnsw-0.0.7-cp38-cp38-win_amd64.whl (118.7 kB view details)

Uploaded CPython 3.8Windows x86-64

pyhnsw-0.0.7-cp38-cp38-musllinux_1_1_x86_64.whl (696.2 kB view details)

Uploaded CPython 3.8musllinux: musl 1.1+ x86-64

pyhnsw-0.0.7-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (180.6 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

Details for the file pyhnsw-0.0.7.tar.gz.

File metadata

  • Download URL: pyhnsw-0.0.7.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyhnsw-0.0.7.tar.gz
Algorithm Hash digest
SHA256 0d9e7dcb3818cc936a1bb25372fdb78744af89ad90ca4e1852841b1115eb4eaf
MD5 7e93cd913c8a6708c16d1d0b44779e00
BLAKE2b-256 88b992b7142d786bbbf2847fa814e7dcce52b7965cb6a432dd73cb16b3e40027

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.7.tar.gz:

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.7-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: pyhnsw-0.0.7-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 121.3 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.7-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 347bfe55f5a53250fad6716a5eda22f16a3a18ddc46a7fa4d37c95d58af652ef
MD5 ca7782d5b4af99cbd1e3174d1a3db3d7
BLAKE2b-256 b889cde593660f82eae7c346a22a483b8cf7ef9661380e22b254581288238fe3

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.7-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.7-cp312-cp312-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for pyhnsw-0.0.7-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 7b4a037cfea2c8867f297cc8088b34b597adcaf059c66dacc80a6fa948b86d84
MD5 88de4248d9d5a844f94bf340b24d2414
BLAKE2b-256 9e0c3e64ac11169433c4878034b36652ec9095d201908b204b2bc3df75ec8b1f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.7-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.7-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyhnsw-0.0.7-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0aaf9704481413e0593c3bda2dea9c01e3f3f3adefcee7bfae5fa3b51a2c1516
MD5 e9440b197313102ec7c110316285117e
BLAKE2b-256 1a6cd79424411d569878db936f4c7101125d10b51ba823826b133873a9652813

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.7-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.7-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: pyhnsw-0.0.7-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 119.7 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.7-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 f9c0af6409e64351537e6620dab3bca4014a21e1a031bc17cb0b54827bf1b4d2
MD5 2a991cc5df21394945648a91eb76d2fc
BLAKE2b-256 50a93fdb54f05edc8f1bfcfb6d5ff0d349f09c335a079f983d07f3762279dfc6

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.7-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.7-cp311-cp311-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for pyhnsw-0.0.7-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 3d6c49d6db55ebda1da53d25b2eef4635a96149955f751dd2a69f2fbeac8b8c5
MD5 f1a23d2bf03e9c2c8ec2f9d8aa41b798
BLAKE2b-256 ef8b90b1d96bd93907bf61e19173206e8320bec6f783574831959488cabbfd25

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.7-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.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyhnsw-0.0.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d268e1660a2093c633f434c1148d2996b304bade91a0f66cc0eed8cc13edc067
MD5 6518865ce4b41b14555eda4dcfe87880
BLAKE2b-256 aed141a004f638f6771d4a3a85f4d6545b3c3e13b0eef72533f57fa864932c62

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.7-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.7-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: pyhnsw-0.0.7-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 118.9 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.7-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 8e5dc4b8a5f12e414c0cc737aa28da8ffd6ff56f2f8db25398a72a357563117c
MD5 2d905c7b60d93217e0115efc0bf1224d
BLAKE2b-256 ba258f85f24120b4839aa59f44669b397a8a70d5a7f6a06f57801c09944c79d8

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.7-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.7-cp310-cp310-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for pyhnsw-0.0.7-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 9bccc875270a95ef93358faa1c6e29c7682724b21f433dbfdc5fd9bc19ab127f
MD5 1f53f1b6012d79ba265914a7338c9762
BLAKE2b-256 c0775b3b7dcb3feb6839bf5cf79eed22ddb604182f0496c721f3f1edf3fa5445

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.7-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.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyhnsw-0.0.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fad0ae44c98d9807845c0eda2cc939c49e33acd520433b02f4fb8b9fe649c3b2
MD5 50d750dd337881725c53972746d08306
BLAKE2b-256 d7067df34c30d9ae7f5e81046d6cf918199155558330550d0978d6e0d9fadd8f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.7-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.7-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: pyhnsw-0.0.7-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 120.8 kB
  • Tags: CPython 3.9, 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.7-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 817610f409b1c876a4fc501d7ba655cc00304f484f87875eb1209c251968bf5f
MD5 a7aa13efe8ca44867ecbe4455e649f86
BLAKE2b-256 f62fab5a952db7a78c692d275beb701ffab643a9bd65078f8f8f9827d34b0a80

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.7-cp39-cp39-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.7-cp39-cp39-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for pyhnsw-0.0.7-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 9a50b77b8295db295f91748b1631587b850c8956b59f560dedeaa92a4e655d21
MD5 47bd478585acfc78eb8bf30eff51e813
BLAKE2b-256 3433204a41f7795739fe182d1d19ffa14c8fa20a3dfc2c80f8c9d03d199a6624

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.7-cp39-cp39-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.7-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyhnsw-0.0.7-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9ac3a7a8af97b50e92cfd62b9280bede8869d75aabdcb4edcd497abc441e7f08
MD5 539e4e9802bd3d8018f90a6885a3488c
BLAKE2b-256 14ac9901106daa38aa8e1bfea3f1d6261ba867f730a878d9e2ac4b7293bc1de6

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.7-cp39-cp39-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.7-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: pyhnsw-0.0.7-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 118.7 kB
  • Tags: CPython 3.8, 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.7-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 7e072e110f9a4cc87053d4effb8969e0fc0dca693398265e79e9d60959b36676
MD5 b3d06333cb9d098a387f7a0121a83964
BLAKE2b-256 771455e261b99e9358515eb36714a854759aa66459ba5331295cd378f3882d6e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.7-cp38-cp38-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.7-cp38-cp38-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for pyhnsw-0.0.7-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 eb24bcb024ef56a7851a5f7ec14a42b81dfcf21f22d5232b65777c51ca30735e
MD5 70a0e935720f318b2a66a31c3d05f8f8
BLAKE2b-256 c74b6ae8ff5385647e8887ff1c5c4c334a14b3fb255c829399c6148f195c5d10

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.7-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.7-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyhnsw-0.0.7-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 243f9034668cc683a5e9850d1609a98c759a5d58e4d470237c96198441d0b491
MD5 f5357502b69e1416846ca4efaaa19915
BLAKE2b-256 92f5532096474eea7073fc2ea3909731e6b0a09044d1c2b069e0b2006ef38875

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.7-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