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: iPhone app takes simultaneous images from 2 lenses, fuses into 1 photo

https://photosynthesis.camera
236•sajomes•3d ago•234 comments

Show HN: Tamron Lens Utility Alternative on Linux

https://github.com/yikerman/tamron-lens-control
44•xiaoyu2006•4d ago•0 comments

Show HN: Line9 – A Mermaid rendering engine with its own layout

https://line9.ai/diagram
44•jumpalongjim•5d ago•11 comments

Show HN: Find stale, orphaned, deleted-but-retrievable RAG vectors

https://github.com/rimironenko/rag-staleness-check
3•rimironenko•5d ago•0 comments

Show HN: Git-knife – Edit commit messages, authors, and dates like a spreadsheet

https://github.com/TheRealYT/git-knife
144•YonathanTesfaye•13h ago•91 comments

Show HN: Write.md – A free, open-source, themeable Markdown editor for macOS

https://writemd.app/
87•danielbilekq•15h ago•70 comments

Show HN: Kernelspace- interactive course on systems programming for LLM Serving

https://kernelspace.naigap.com/
2•praveer13•3h ago•0 comments

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

https://cactuscompute.com/needle
510•HenryNdubuaku•1d ago•171 comments

Show HN: Mcptoon – Token-efficient MCP CLI client

https://github.com/activeing123/mcptoon
70•mcptokensaver•23h ago•43 comments

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

https://github.com/AntigmaLabs/ante
159•ubermon•1d ago•88 comments

Show HN: TermDOM – HTML, CSS and JavaScript (With a Real DOM) for TUIs and CLIs

https://github.com/bikeshaving/termdom
10•bikeshaving•10h ago•0 comments

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

https://everycube.alen.is/
287•Alen123•1d ago•115 comments

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

https://www.whodunnitai.com/
208•MrRowTheBoat•2d ago•86 comments

Show HN: I wrote the emudev hello world (CHIP-8 emulator in C)

https://github.com/Tackx/c8
4•tack1234•6h ago•0 comments

Show HN: MacPacker – open-source archive manager for macOS, now edits ZIPs

https://github.com/sarensw/MacPacker
4•sarensw•6h ago•3 comments

Show HN: Supply-Wizard; Start Selling Data with an Easy Vendor Checklist

https://brickroad.network/supplier-wizard
3•dataranger•6h ago•1 comments

Show HN: Tokyo Trains

https://greggman.github.io/tokyo-trains/
10•greggman65•13h ago•1 comments

Show HN: Openleetcode – local LeetCode runner where tests live in the repo

https://github.com/therepanic/openleetcode
4•therepanic•8h ago•0 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/
75•mikeayles•1d ago•24 comments

Show HN: p0rt – see every dev server, port and Docker container in your menu bar

https://p0rt.app
3•ddeunert•9h ago•0 comments

Show HN: ThinkingType, an eval measuring if fonts change VLM judgments

https://github.com/wdanfort/thinkingtype
3•bwuckner-sf•9h ago•1 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
121•Rochus•2d ago•19 comments

Show HN: Pulp – Zero-allocation C11 telemetry engine (28M logs/SEC to disk)

https://github.com/superwired-labs/Pulp
5•superwired_labs•10h ago•1 comments

Show HN: HyperSAE – Hyperbolic Sparse Autoencoders for LLM Interpretability

https://github.com/vishal-dehurdle/hypersae
3•visha1v•10h ago•0 comments

Show HN: C# Game Engine with its own scripting language and IDE

https://github.com/ArcadeMakerSources/ArcadeMaker
3•am-gm•15h 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
18•leog_me•1d ago•12 comments

Show HN: Proxima serves 4x more requests with no hardware change on vLLM

https://github.com/Tenosra/Proxima
3•pythongiant•13h ago•1 comments

Show HN: The self-improving QA agent for software teams

https://vostride.com
3•pranshuchittora•14h ago•0 comments

Show HN: Parley – your coding agent can talk to a teammate's agent

https://parley.weldra.dev
7•tyagivivek•15h ago•4 comments

Show HN: drawtonomy – Draw and run driving scenarios in the browser

https://github.com/kosuke55/drawtonomy
3•kosuke55•15h ago•2 comments