hhy3 / pyglass

Graph Library for Approximate Nearest Search

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Graph Library for Approximate Nearest Search

pyglass is a library for fast inference of graph index for approximate nearest search.

Installation

(Recommanded)Installation from Wheel

pyglass can be installed using pip as follows:

pip3 install glassppy

Installation from Source

sudo apt-get update && sudo apt-get install -y build-essential git python3 python3-distutils python3-venv
pip3 install numpy
pip3 install pybind11
bash build.sh

Quick Tour

>>> import glassppy as glass
>>> import numpy as np
>>> n, d = 10000, 128
>>> X = np.random.randn(n, d)
>>> Y = np.random.randn(d)
>>> index = glass.Index("HNSW", dim=d, metric="L2", R=32, L=50)
>>> graph = index.build(X)
>>> searcher = glass.Searcher(graph, X, "L2", 0)
>>> searcher.optimize()
>>> searcher.set_ef(32)
>>> print(searcher.search(Y, 10))

Usage

Import library

>>> import glassppy as glass

Load Data

>>> n, d = 10000, 128
>>> X = np.random.randn(n, d)
>>> Y = np.random.randn(d)

Create Index pyglass supports HNSW and NSG index currently

>>> index = glass.Index(index_type="HNSW", dim=d, metric="L2", R=32, L=50)
>>> index = glass.Index(index_type="NSG", dim=d, metric="L2", R=32, L=50)

Build Graph

>>> graph = index.build(X)

Create Searcher Searcher accepts level parameter as the optimization level. You can set level as 0 or 1 or 2. The higher the level, the faster the searching, but it may cause unstable recall.

>>> optimize_level = 2
>>> searcher = glass.Searcher(graph=graph, data=X, metric="L2", level=optimize_level)
>>> searcher.set_ef(32)

(Optional) Optimize Searcher

>>> searcher.optimize()

Searching

>>> ret = searcher.search(query=Y, k=10)
>>> print(ret)

Performance

Glass is among one of the top performant ann algorithms on ann-benchmarks

fashion-mnist-784-euclidean

gist-960-euclidean

sift-128-euclidean

Quick Benchmark

  1. Change configuration file examples/config.json
  2. Run benchmark
python3 examples/main.py
  1. You could check plots on results folder

About

Graph Library for Approximate Nearest Search

License:MIT License


Languages

Language:C++ 97.6%Language:Python 2.1%Language:CMake 0.3%Language:Shell 0.1%