TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

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

73 点作者 dicroce大约 1 个月前

7 条评论

antirez大约 1 个月前
Yes, HSNWs are not <i>so</i> complex, and they work great. I wrote an implementation myself, it&#x27;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:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;redis&#x2F;redis&#x2F;blob&#x2F;unstable&#x2F;modules&#x2F;vector-sets&#x2F;hnsw.c">https:&#x2F;&#x2F;github.com&#x2F;redis&#x2F;redis&#x2F;blob&#x2F;unstable&#x2F;modules&#x2F;vector-...</a><p>Still, to be honest, I&#x27;m reading the 500 lines of code with great interest because I didn&#x27;t thought it was possible to go so small, maybe part of the trick is that&#x27;s in C++ and not in C, as for instance you don&#x27;t have the queue code.<p>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)<p>EDIT: Ok after carefully inspection this makes more sense :D<p>1. Yes, C++ helps, the vector class, the priority queue, ...<p>2. I forgot to say that I implemented serialization other than quantization, and this also includes quite some code.<p>3. Support for threads is another complexity &#x2F; code-size price to pay.<p>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 &quot;but how to...&quot; questions, maybe my C implementation is a useful read.
oersted大约 1 个月前
I particularly appreciated the concise and plain explanation of the data-structure, it really demystifies it.<p>&gt; 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.<p>&gt; 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.
jasonjmcghee大约 1 个月前
Implementations like these are incredibly useful for understanding how something like HNSW works, and being able to make derivative work.<p>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).<p>Definitely isn&#x27;t beating any benchmarks, but free (or wildly cheap) to host, as you serve it directly from a CDN and processing is done locally.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;jasonjmcghee&#x2F;portable-hnsw">https:&#x2F;&#x2F;github.com&#x2F;jasonjmcghee&#x2F;portable-hnsw</a>
评论 #43628727 未加载
vismit2000大约 1 个月前
A very nice intro to HNSW that previously appeared on HN[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;brtholomy&#x2F;hnsw&#x2F;blob&#x2F;master&#x2F;README.md">https:&#x2F;&#x2F;github.com&#x2F;brtholomy&#x2F;hnsw&#x2F;blob&#x2F;master&#x2F;README.md</a><p>[1] <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=41694631">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=41694631</a>
imurray大约 1 个月前
Looks neat. It would be useful to compare to other implementations: <a href="https:&#x2F;&#x2F;ann-benchmarks.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;ann-benchmarks.com&#x2F;</a> -- potentially not just speed, but implementation details that might change recall.
评论 #43624957 未加载
评论 #43626361 未加载
mertleee大约 1 个月前
I guess I&#x27;m too simple to understand why this is useful? Just because it&#x27;s been implemented in so few lines or?
评论 #43629369 未加载
aboardRat4大约 1 个月前
I almost read it as NSFW.