Tiny NN Search
Vector and Keyword Search from Scratch
Exact, approximate and keyword search written from scratch in NumPy, benchmarked against faiss, hnswlib and rank_bm25.
- IVF: recall@10 0.984 at 16,425 q/s
- BM25 nDCG@10 0.663 on SciFact
- HNSW recall on par with hnswlib
Built withNumPy
Gradio
PyTorchMatplotlib
pytest
Code is in a private repository for now.

Overview
Tiny NN Search is a vector and keyword search engine I wrote from scratch in NumPy. Every index has the same interface: add vectors, search the top k, save and load.
On top of the indexes, a search engine handles text, images and audio. Each query is searched with its own kind of model, so a picture finds similar pictures and a sound finds similar sounds. A CLIP- or CLAP-style model adds text queries.
It is the retrieval engine of tiny-rag, and it loads the trained text, image and audio embedders of tiny-embedder from the family's shared model store.
The question
How close can from-scratch NumPy indexes get to faiss and hnswlib in recall and speed, on one CPU thread?
Key features
- Indexes: Flat, IVF, PQ, IVF-PQ, HNSW and LSH, plus k-means++ written by hand
- BM25 on a CSR inverted index, and hybrid search with RRF or weighted fusion
- Text, image and audio search engines, with a windowed-sinc resampler for audio
- Benchmark on synthetic vectors, GloVe-100 and SciFact against faiss, hnswlib and rank_bm25
- A Gradio test app: text, image and audio search, and an index lab
How it works
Indexes
IVF clusters the vectors with k-means and only scans the nearest lists. PQ compresses vectors into codes and scores them with lookup tables. HNSW walks a layered graph. LSH hashes with random hyperplanes and probes nearby buckets.
Keywords and hybrid
BM25 scores documents from an inverted index. Hybrid search fuses the BM25 and dense rankings, by reciprocal rank or by a weighted score.
One engine per modality
The engine tells the query's modality from its type (text, image file, array, waveform) and embeds it with the matching model, then searches the index and returns the stored payloads.
Examples




Tech stack
Every index, BM25, k-means and the baseline embedders
Test app for text, image and audio search
Optional GPU exact search and the family's trained embedders
Benchmark charts
Tests against brute force, save / load and fusion order
What I learned
- A good graph is not enough: HNSW in pure Python was slower than exact NumPy search at recall 0.9 on GloVe.
- Hybrid fusion only helps when the dense side is good: with a hashing embedder, RRF (0.542) fell below BM25 alone (0.663).
Limitations
- A learning project: faiss and hnswlib are much faster (C++, SIMD).
- Cross-modal search (text → image / audio) is supported by the engine but not benchmarked yet: it needs the trained tiny-embedder models.
Related projects

tiny-rag — Ask Your Texts, Images and Sounds
Code is in a private repository for now.
A retrieval-augmented generation system over texts, images and sounds, built on my own tiny-* models. Each query is searched with its own kind of model, then Qwen2.5-0.5B-Instruct, run inside tiny-llm, answers from numbered sources.
Built with:
PyTorch
Gradio
NumPy
Hugging Face Datasets
pytest

tiny-embedder — Text, Image and Audio Embeddings from Scratch
Code is in a private repository for now.
Three small embedding models built from scratch in PyTorch: text, images and sounds each become a 256-d vector, so similar things end up close. Every text tower starts from my tiny-bert.
Built with:
PyTorch
NumPy
Hugging Face Datasets
pytest
JavaScript

tiny-reranker — Text, Image and Audio Rerankers from Scratch
Code is in a private repository for now.
Three small cross-encoders that reorder the top results of a first search stage. One reads a query with a passage, one a caption with a picture, one a caption with a sound. All three are built from scratch in PyTorch on top of tiny-bert.
Built with:
PyTorch
NumPy
Hugging Face Datasets
pytest
JavaScript

Tiny BERT — A Small Encoder and Its BPE Tokenizer, From Scratch
Code is in a private repository for now.
A 9M-parameter BERT-style encoder and its byte-level BPE tokenizer, written and pre-trained from scratch in PyTorch, with no Hugging Face transformers. It is the encoder the rest of my tiny family starts from.
Built with:
PyTorch
Hugging Face Datasets
NumPy
scikit-learnMatplotlib
pytest