elixir-nx / hnswlib

Elixir binding for the hnswlib library.
Apache License 2.0
52 stars 9 forks source link

HNSWLib.Index.new should not accept 0 for max_elements #5

Closed NickTaylor- closed 6 months ago

NickTaylor- commented 1 year ago

In other parts of the code a max size of 0 is interpreted to an unbounded size (e.g. in loading a saved index), however when creating an index a size of 0 will mean that the index is unable to used at all.

{:ok, index} = HNSWLib.Index.new(space, dim, 0)
HNSWLib.Index.add_items(index, Nx.tensor([0, 0]))

-> {:error, "The number of elements exceeds the specified limit"}

vs.

{:ok, index} = HNSWLib.Index.new(space, dim, 1)
HNSWLib.Index.add_items(index, Nx.tensor([0, 0]))

-> :ok

This is the error thrown by hnswlib: https://github.com/nmslib/hnswlib/blob/master/hnswlib/hnswalg.h#L1090-L1092

josevalim commented 1 year ago

Can you send a PR please?

NickTaylor- commented 1 year ago

yup! just wanted to document it in case I forgot 😅

It also showed up in https://elixirforum.com/t/unable-to-add-items-to-saved-index-of-hnswlib/56652

P.S. good calling it out José, much higher chance I actually write the PR now

cocoa-xu commented 9 months ago

Hi @NickTaylor-, v0.1.4 is out now, and this issue should be fixed in that one.