-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Buffers, IP, Factories & more #40
Conversation
@asilvas Thanks a lot for this great PR, please allow me some time to review. |
src/faiss.cc
Outdated
{ | ||
Napi::Env env = info.Env(); | ||
if (info[0].IsExternal()) | ||
|
||
if (!info.IsConstructCall()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
greatly simplified the constructors responsibilities and removed the need to pass around external data. This moves the responsibility of each feature to different functions.
Let me know if there's anything I can do to progress this PR. Thanks |
@asilvas Sorry for slow reply. I have been occupied with work recently. I'll deal with it later this week. |
test/Index.test.js
Outdated
}); | ||
|
||
it('Flat /w IP', () => { | ||
const index = Index.fromFactory(2, 'Flat', 0 /* METRIC_INNER_PRODUCT */); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add MetricType
enum into lib/index.js, so that we can use enum directly
const faiss = require('bindings')('faiss-node');
faiss.MetricType = void 0;
var MetricType;
(function (MetricType) {
MetricType[MetricType["METRIC_INNER_PRODUCT"] = 0] = "METRIC_INNER_PRODUCT";
MetricType[MetricType["METRIC_L2"] = 1] = "METRIC_L2";
MetricType[MetricType["METRIC_L1"] = 2] = "METRIC_L1";
MetricType[MetricType["METRIC_Linf"] = 3] = "METRIC_Linf";
MetricType[MetricType["METRIC_Lp"] = 4] = "METRIC_Lp";
MetricType[MetricType["METRIC_Canberra"] = 20] = "METRIC_Canberra";
MetricType[MetricType["METRIC_BrayCurtis"] = 21] = "METRIC_BrayCurtis";
MetricType[MetricType["METRIC_JensenShannon"] = 22] = "METRIC_JensenShannon";
MetricType[MetricType["METRIC_Jaccard"] = 23] = "METRIC_Jaccard";
})(MetricType || (faiss.MetricType = MetricType = {}));
module.exports = faiss;
src/faiss.cc
Outdated
std::string fname; | ||
if (info[0].IsExternal()) | ||
{ | ||
fname = *info[0].As<Napi::External<std::string>>().Data(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that this branch will not reach. maybe we can get fname
directly:
std::string fname = info[0].As<Napi::String>().Utf8Value();
src/faiss.cc
Outdated
std::string description; | ||
if (info[1].IsExternal()) | ||
{ | ||
description = *info[1].As<Napi::External<std::string>>().Data(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as previous comment
src/faiss.cc
Outdated
static Napi::Object NewInstance(Napi::Env env, const std::vector<napi_value> &args) | ||
{ | ||
Napi::EscapableHandleScope scope(env); | ||
Napi::Object obj = GetInstanceData(env, T::CLASS_NAME).New(args); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found before in this experimental branch that it is simpler to store the pointer to the constructor using the following way:
https://github.com/ewfian/faiss-node/blob/feat/support-IndexFlatIP/src/faiss.cc#L71
inline static Napi::FunctionReference *constructor = new Napi::FunctionReference();
This eliminates the need to use GetInstanceData
and AttachInstanceData
Please confirm if you can modify it as above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My original changes had used that pattern but was having stability issues with the Bun runtime so I opted for the more complex pattern. It resolved one of the bugs, but still has stability issues, so I'll revert for now until Bun is more reliable with NAPI packages.
src/faiss.cc
Outdated
|
||
static Napi::Object Init(Napi::Env env, Napi::Object exports) | ||
{ | ||
Napi::HandleScope scope(env); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that HandleScope
is no longer needed.
}); | ||
// clang-format on | ||
|
||
constructor = new Napi::FunctionReference(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not as clean as constructing within Base, but that was causing stability issues. I don't fully understand the difference, but this works around whatever was going on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What kind of stability issues is it and how to reproduce it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bun runtime (https://bun.sh/) segfaults via bun test
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I'm fine merging with that issue for now since I created the issue on the Bun side. oven-sh/bun#4526
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes all tests which rely on throwing won't work in Bun until the issue I linked is resolved. That's fine, and not this packages problem. Technically we can work around it by replacing all the exception handling to use native exceptions, but I can throw up another PR later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Invalid argument
is not thrown by our code and it was thrown by napi, maybe the bun was not well handle this case
Another point that I'm a little concerned about is that btw, I'm sorry that it's time for me to go to bed. i will reply tomorrow. |
Index is abstract, and any index you've created that does not implement a function (ala |
I reconfirmed and it's as you said. But I will remove |
Resolves #37 and much more:
toBuffer
&fromBuffer
(10x+ faster than re-adding vectors, but still somehow 2x slower than reading from disk)Sorry for the big sweeping changes. I don't like to put this much into 1 PR, but due to considerable complications with NAPI they were necessary.