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.6.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.6-cp312-cp312-win_amd64.whl (121.2 kB view details)

Uploaded CPython 3.12Windows x86-64

pyhnsw-0.0.6-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.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (182.1 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

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

Uploaded CPython 3.11Windows x86-64

pyhnsw-0.0.6-cp311-cp311-musllinux_1_1_x86_64.whl (697.0 kB view details)

Uploaded CPython 3.11musllinux: musl 1.1+ x86-64

pyhnsw-0.0.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (182.2 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

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

Uploaded CPython 3.10Windows x86-64

pyhnsw-0.0.6-cp310-cp310-musllinux_1_1_x86_64.whl (696.6 kB view details)

Uploaded CPython 3.10musllinux: musl 1.1+ x86-64

pyhnsw-0.0.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (181.0 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

pyhnsw-0.0.6-cp39-cp39-win_amd64.whl (120.7 kB view details)

Uploaded CPython 3.9Windows x86-64

pyhnsw-0.0.6-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.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (181.3 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

pyhnsw-0.0.6-cp38-cp38-win_amd64.whl (118.6 kB view details)

Uploaded CPython 3.8Windows x86-64

pyhnsw-0.0.6-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.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (181.0 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

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

File metadata

  • Download URL: pyhnsw-0.0.6.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.6.tar.gz
Algorithm Hash digest
SHA256 74b7135a8030899bdad8390a8856d76d2c32920c4d3b7dcc5b563130138a34a0
MD5 0a6b3058bb121041540c11c7642d68de
BLAKE2b-256 a8a9c8e0b76bb1449ae23c06f7fba3184193d90c70d59895439451444930ba95

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhnsw-0.0.6.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.6-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: pyhnsw-0.0.6-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 121.2 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.6-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 813db3c25bca70da4b2e87aa3c073417f234f625963e32e5746549dd8e96ad38
MD5 8fc7686539f2cec613e284296eb80748
BLAKE2b-256 c543dca70a76055707d170d78ba38c3111f81aec876b3d8b2aaee3a10e540b67

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyhnsw-0.0.6-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 1bb199ba534086a1b0674cc55e956d427b544bdbad574ef9ebe14b7e44162b8e
MD5 0134b67e5a3993a18f13eda10ae7cdf9
BLAKE2b-256 5fd35b4d8b690c20589022c126494a76378c381ce7d08ce908301aa33e41c89b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyhnsw-0.0.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5f3b3e7ca39dd49e80e7fb459aaa52747dfcb021aea37f485e490beaa9ceb4bb
MD5 d5a0539071cf7e14465a67d9d52e2292
BLAKE2b-256 faf88d2ad3a5f53dc1de27e5c082bdf053dff8c2ed35429d345bd2f5d41d6ec6

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: pyhnsw-0.0.6-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.6-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 a4299f3ef8e5dff713342e0c5c4d7633f8cf6c6c4831dd1fa829d64570ee9c48
MD5 35eaf7c079cccf8e90d4f431952bc164
BLAKE2b-256 25474629580edff814f18c90fe627d68f3aceed92eaa2f30ae7dc5a928a26e86

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyhnsw-0.0.6-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 d54d932e26a9d91ce2ec9c50f453fe33403867fe959149438f422269a9f2a691
MD5 3bb727a5ecabc47faadec215d05867ca
BLAKE2b-256 dac5c6490544643e045137a132b7f4c4b9bbaec779b520933180e724a7ffef76

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyhnsw-0.0.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4556ff3595a400164fc11f3fb68d736d26b171c3a2257e5826f3a2b7030901fb
MD5 91a04b2f1eb93a0244e28293b4cec0f4
BLAKE2b-256 8f2661e05ae77a2f1699fb49f9287200a8bf544a8690afc162d989ab970a542e

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: pyhnsw-0.0.6-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.6-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 29f8cd78494bd8f130e89c190d6347c09a0f787c24c6341dd25f5c1fab744644
MD5 7762947c48aa62f2cf38fa2a411610e4
BLAKE2b-256 d7787aebc81c201fe168706bca0343120782c49b193028dab9fa12b1385472d5

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyhnsw-0.0.6-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 89758622b8c8ddd53e8a66b7ab9de055620c1da465ac9c45c53f2fc7d6421a89
MD5 9a804e5f581b41f4a3e2647a9d29cdfb
BLAKE2b-256 9f7fd0b91baba791d00452b0cde43efbde8231fda7cdecd9864a1b0a0deb8b9b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyhnsw-0.0.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 acefeba944d8edc8115beca3824d16cd53861ab0affc39ec4145b7b354062aa1
MD5 4729c86462b8686e79f847bd32f21cd3
BLAKE2b-256 c3827872f3452a7bde02733a67a4900b066e4297dc3aa807d0627a6f2a1ff64a

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: pyhnsw-0.0.6-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 120.7 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.6-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 103ec915592d0cdd706712c9c2603c76f2db514a54272d7c1495b3e272fc34ea
MD5 7e56fd92f4818a767ac12631ef8a8cb5
BLAKE2b-256 b6a30f66561ce0591113e53f09e29a25f64bcc69210cc1225f0eb009cc38d24a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyhnsw-0.0.6-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 eb9861fcc9830c5d819b9db857b19ce0ceb40c7cd6898e6bac48fa33a7ae3e69
MD5 e7fb121215914751ede77489fa72c750
BLAKE2b-256 8dca8043b2348e1be556701a0db46ecbbf7d2dbe9c350cfd777ab784e196e92a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyhnsw-0.0.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 eac5dc993c375083689958476162b9b70ee9af38a72acb5d4cdd076a4b5b6a89
MD5 3d01721d2a4990503e2e5aa5e8df0b19
BLAKE2b-256 124976c09f171950b7946d71cd59d4a7f2704d302d208d74b1aded0883bf2e6f

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: pyhnsw-0.0.6-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 118.6 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.6-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 0ba6ba1dd1228e7256c29541baf1ef51f751c00c20a561366013eb0e22c9cb9f
MD5 9a7298b39837fff0182d8b9ebc699d6f
BLAKE2b-256 e1285453b814c745a38d81232681c6d229eea072e30f2f552eb416e70494f307

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyhnsw-0.0.6-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 1ffe69f568bb163bd7ac91c873435c0b73285a4542ca944c6c93c3b90d407ec1
MD5 f05d301cebe6ca67ffd373832bfb1a2e
BLAKE2b-256 c4148bf5581b2a5271d8e9075cf94fc5cb5f72b54b393d8a585171c4fe6d38be

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyhnsw-0.0.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ffaa0162c118dccec86831f24213cff79c57cf5f313f26d3f19af12765c1ec1b
MD5 7a24b6124167624af386d8c17c5d4450
BLAKE2b-256 d7d1224862d25defee531e663513aadd74c999654146c99925ed545beccd705b

See more details on using hashes here.

Provenance

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