frontpage.
newsnewestaskshowjobs

Open Source @Github

fp.

Open in hackernews

Show HN: HNSW index for vector embeddings in approx 500 LOC

https://github.com/dicroce/hnsw
73•dicroce•1y ago

Comments

oersted•1y ago
I particularly appreciated the concise and plain explanation of the data-structure, it really demystifies it.

> HNSW is a graph structure that consists of levels that are more sparsely populated at the top and more densely populated at the bottom. Nodes within a layer have connections to other nodes that are near them on the same level. When a node is inserted a random level is picked and the node is inserted there. It is also inserted into all levels beneath that level down to 0.

> When searches arrive they start at the top and search that level (following connections) until they find the closest node in the top level. The search then descends and keeps searching nearby nodes. As the search progresses the code keeps track of the K nearest nodes it has seen. Eventually it either finds the value OR it finds the closest value on level 0 and the K nearest nodes seen are returned.

imurray•1y ago
Looks neat. It would be useful to compare to other implementations: https://ann-benchmarks.com/ -- potentially not just speed, but implementation details that might change recall.
swyx•1y ago
i think with small codebases like this is less about speed and more about education of essentials - i actually often encourage juniors to do small clones like this, feel proud, and then study the diffs with the at-scale repros and either feel humbled or feel like they have a contribution to make.
oersted•1y ago
I see they are still using GloVe word embeddings for the first benchmark. Ah good ol' days! Nothing wrong with it, should still yield a realistic distribution of vectors. Just brings a lot of memories :)
antirez•1y ago
Yes, HSNWs are not so complex, and they work great. I wrote an implementation myself, it's 2500 lines of code (5x the one of dicroce!), but inside there is binary and int8 quantization and many advanced features (include true deletions), and I commented it as much as possible. I hope you may find it useful to read alongside the one proposed by OP:

https://github.com/redis/redis/blob/unstable/modules/vector-...

Still, to be honest, I'm reading the 500 lines of code with great interest because I didn't thought it was possible to go so small, maybe part of the trick is that's in C++ and not in C, as for instance you don't have the queue code.

Also the strategy used by my implementation in order to re-link the orphaned nodes upon deletion adds complexity, too. (btw any feedback on that part is especially appreciated)

EDIT: Ok after carefully inspection this makes more sense :D

1. Yes, C++ helps, the vector class, the priority queue, ...

2. I forgot to say that I implemented serialization other than quantization, and this also includes quite some code.

3. Support for threads is another complexity / code-size price to pay.

And so forth. Ok, now it makes a lot of sense. Probably the 500 LOC implementation is a better first-exposure experience for newcomers. After accumulating all the "but how to..." questions, maybe my C implementation is a useful read.

mertleee•1y ago
I guess I'm too simple to understand why this is useful? Just because it's been implemented in so few lines or?
cluckindan•1y ago
It makes it possible to approximate k-nearest neighbor vector search without having to calculate cosine similarity, dot product or euclidean distance for all stored vectors on the fly.
jasonjmcghee•1y ago
Implementations like these are incredibly useful for understanding how something like HNSW works, and being able to make derivative work.

I put together a tiny little implementation a while ago, the key thing being, it writes the index as a few parquet files, so you can host the index on a CDN and read from it via http range requests (e.g. via duckdb wasm).

Definitely isn't beating any benchmarks, but free (or wildly cheap) to host, as you serve it directly from a CDN and processing is done locally.

https://github.com/jasonjmcghee/portable-hnsw

kamranjon•1y ago
This is incredibly cool, I’m surprised more people haven’t contributed to this - it seems like it’s only a few optimizations away from being performant enough for a pretty broad set of use cases.
jasonjmcghee•1y ago
Appreciate the kind words! Obviously feel free to hack on it.

When I first built it, I spent some time trying to tackle the issue of needing to update the entire file (and create an invalidation) if you want to update the database, which might be fine, but closes a lot of doors. I kind of hit a wall on finding a convincing approach to solving it, given the constraints of the setup.

aboardRat4•1y ago
I almost read it as NSFW.
vismit2000•1y ago
A very nice intro to HNSW that previously appeared on HN[1]: https://github.com/brtholomy/hnsw/blob/master/README.md

[1] https://news.ycombinator.com/item?id=41694631

Show HN: Scroll through all 43252003274489856000 Rubik's Cube states

https://everycube.alen.is/
56•Alen123•2h ago•11 comments

Show HN: Needle2: 14MB agentic LLM for phones, wearables, smart home and robots

https://cactuscompute.com/needle
173•HenryNdubuaku•8h ago•77 comments

Show HN: Insidedb, an interactive, animated explainer of database internals

https://david-g-3654.github.io/insidedb/
2•davai-g•35m ago•0 comments

Show HN: AI Pulse a fake LED strip beside the macOS Dock that shows agent status

https://github.com/leog/ai-pulse
6•leog_me•3h ago•3 comments

Show HN: My local climbing gym from photogrammetry

https://kmcheung12.github.io/climb-preview/tour/ae43c6e5
8•a_c•4h ago•0 comments

Show HN: Voice driven murder mystery, Interview AI suspects with your voice

https://www.whodunnitai.com/
190•MrRowTheBoat•22h ago•81 comments

Show HN: Higher-dimensional lattices unfolded into the 2D plane

https://number-garden.com/?@THLP@
13•unitX•4d ago•1 comments

Show HN: Opencodex, a free open-source coding agent for VSCode

https://github.com/matonhp5108/Opencodex
5•thephantomhider•2h ago•0 comments

Show HN: Ante, a coding agent in a single binary that runs offline

https://github.com/AntigmaLabs/ante
119•ubermon•9h ago•75 comments

Show HN: A tiny LLM running at 21,000 tok/s on a $250 FPGA (Live Demo)

https://www.mikeayles.com/blog/on-chip-llm-kv260/
43•mikeayles•14h ago•13 comments

Show HN: Pyrig – A tool that automates project setup and maintenance

https://github.com/Winipedia/pyrig
4•Winipedia•3h ago•0 comments

Show HN: Typegres 0.3 – SQL-as-your-API, safely (using Cap'n Web RPC)

https://typegres.com/
3•ryanrasti•4h ago•0 comments

Show HN: Calldiff – a CLI to see callstack diffs in all major languages

https://github.com/tanishqkancharla/calldiff
2•tanishqkanc•4h ago•0 comments

Show HN: Keen Code – an agentic-engineered coding agent

https://github.com/mochow13/keen-code
6•mochow13•4h ago•2 comments

Show HN: Graph2agent; Mermaid diagrams, explained for agents

https://graph2agent.github.io/
3•alexandroskyr•4h ago•0 comments

Show HN: PicPocket – Dedicated 'chats' for photo-sharing (share to organize)

https://picpocket.io/
5•amr_shawky•9h ago•0 comments

Show HN: A Project Oberon System version running on RISC-V instead of RISC-5

https://github.com/rochus-keller/OberonSystem/tree/op2-rv32
119•Rochus•1d ago•19 comments

Show HN: Design Engineering, as a Service

https://designshippers.com/?0
13•krm01•12h ago•3 comments

Show HN: 100% native Swift harness (NOT Electron)

https://github.com/Lore-Hex/QuillCode
11•ljlolel•12h ago•3 comments

Show HN: I made alchemical-cosmological PCB badges

https://github.com/KaiPereira/Alchemical-Cosmological-Badges
22•kaipereira•5d ago•3 comments

Show HN: Neolabs.fyi – 100 new AI labs by research area, valuation, and more

https://neolabs.fyi/
7•polyphilz•11h ago•2 comments

Show HN: Watching browser fingerprinting calls via Chromium's Blink layer

https://kameleo.io/blog/this-is-the-closest-well-ever-get-to-reverse-engineering-anti-bots
10•brnbs•16h ago•0 comments

Show HN: DocSift convert a PDF once, give the model only the passages it needs

https://github.com/anishmoncivarghese/docsift
3•anishvarghese•11h ago•0 comments

Show HN: Hawser – sign, notarize, DMG, auto-update and license a Mac app

https://hawserkit.com/
3•yaveez•12h ago•0 comments

Show HN: Issue Apple and Google wallet passes from Node.js

https://github.com/getpassmint/passmint
5•alexpate•16h ago•0 comments

Show HN: Airy – Free, fast, and simple voice content creation

https://airy.so
45•login588•1d ago•16 comments

Show HN: Bio Tools – install drug design tools easily

https://github.com/David-OConnor/bio_tools
2•the__alchemist•9h ago•0 comments

Show HN: Slashscore, an open developer graph built from public GitHub activity

https://www.slashscore.com
2•wingravity•10h ago•2 comments

Show HN: PrivateRedact – Offline PII redaction with a local LLM, no cloud

https://github.com/monjurulkarim/privateredact
4•rajubuet24•10h ago•0 comments

Show HN: My math notes for anything that touches ML

https://github.com/publications-and-more/pub
3•Amlal•10h ago•0 comments