Skip to main content

GitHub repository https://github.com/QunBB/fastannoy

FastAnnoy

This library is a pybind11 port of spotify/annoy.

Installation

To install, just do pip install fastannoy to pull down from PyPI.

Install from source code

  • clone this repository
  • pip install ./fastannoy

Backgroud

First of all, thanks for spotify/annoy's awesome work, it provides efficient implement for Approximate Nearest Neighbors Search. But when i find that batch search is missing, so this project's initial purpose is for batch search.

However, it's written in pybind11 for python interface, and discovered better performance.

Usage

All basic interfaces is same as spotify/annoy.

from fastannoy import AnnoyIndex
import random

f = 40  # Length of item vector that will be indexed

t = AnnoyIndex(f, 'angular')
for i in range(1000):
    v = [random.gauss(0, 1) for _ in range(f)]
    t.add_item(i, v)

t.build(10) # 10 trees
t.save('test.ann')

# ...

u = AnnoyIndex(f, 'angular')
u.load('test.ann') # super fast, will just mmap the file
print(u.get_nns_by_item(0, 100)) # will find the 100 nearest neighbors
"""
[0, 17, 389, 90, 363, 482, ...]
"""

print(u.get_nns_by_vector([random.gauss(0, 1) for _ in range(f)], 100)) # will find the 100 nearest neighbors by vector
"""
[378, 664, 296, 409, 14, 618]
"""

Batch Search

Corresponding to get_nns_by_item, the batch search version is get_batch_nns_by_items. The first argument should be a list of int.

In the same way, corresponding to get_nns_by_vector, the batch search version is get_batch_nns_by_vectors. The first argument should be a list of list[int].

And the batch search's implement supports multiple threads. You can set the argument n_threads, the default is 1.

# will find the 100 nearest neighbors

print(u.get_batch_nns_by_items([0, 1, 2], 100))
"""
[[0, 146, 858, 64, 833, 350, 70, ...], 
[1, 205, 48, 396, 382, 149, 305, 125, ...], 
[2, 898, 503, 618, 23, 959, 244, 10, 445, ...]]
"""

print(u.get_batch_nns_by_vectors([
    [random.gauss(0, 1) for _ in range(f)]
    for _ in range(3)
], 100))
"""
[[862, 604, 495, 638, 3, 246, 778, 486, ...], 
[260, 722, 215, 709, 49, 248, 539, 126, 8, ...], 
[288, 764, 965, 320, 631, 505, 350, 821, 540, ...]]
"""

Benchmark

The results are running in my macbook with the test script, so focus on time consumption relatively between fastannoy and annoy.

fastannoy annoy
50W items with 128 dimension
- build+add_item 13.810 seconds 19.633 seconds
- 5W times search 20.613 seconds 39.760 seconds
- 5k times search with 10 batch size and 5 threads 6.542 seconds /

Release files for fastannoy 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastannoy 1.1.1
File Size Uploaded
fastannoy-1.1.1.tar.gz 26.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastannoy 1.1.1
File Interpreter ABI Platform
fastannoy-1.1.1-cp39-cp39-macosx_10_12_x86_64.whl CPython 3.9 CPython 3.9 macOS 10.12+ x86-64 Details

Total release size: 173.2 kB

Release files / fastannoy-1.1.1.tar.gz

Download URL fastannoy-1.1.1.tar.gz
Size 26.0 kB
Tags Source
SHA-256 checksum
How to use checksums
0848b21d697748cbac167103ac0d57985f370b4dbd6f2875c650f92502063f76
BLAKE2b-256 checksum
How to use checksums
75dc6d538f5e9c7ef89a5ef43214971e8df97fba77f11c5980696f4092ebda8f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.6

Release files / fastannoy-1.1.1-cp39-cp39-macosx_10_12_x86_64.whl

Download URL fastannoy-1.1.1-cp39-cp39-macosx_10_12_x86_64.whl
Size 147.2 kB
Tags CPython 3.9 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
26f4dd3775a024bde8f4a3f49a45696871cd197e4615c54ec6d6f0a3ddc96f1c
BLAKE2b-256 checksum
How to use checksums
e74bf946fecd0cfc0137df171c0456a14f9cd33b57c0a17dcd030a969f06aa4b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.6

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page