Skip to content
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

Labels are 64bit but JS bindings were limited to 32bit #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export interface SearchResult {
/** The disances of the nearest negihbors found, size n*k. */
distances: number[],
/** The labels of the nearest neighbors found, size n*k. */
labels: number[]
labels: BigInt[]
}

// See faiss/MetricType.h
Expand Down Expand Up @@ -103,10 +103,10 @@ export class Index {
mergeFrom(otherIndex: Index): void;
/**
* Remove IDs from the index.
* @param {number[]} ids IDs to read.
* @param {BigInt[]} ids IDs to read.
* @return {number} number of IDs removed.
*/
removeIds(ids: number[]): number
removeIds(ids: BigInt[]): number
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function was already implemented correctly but had invalid type notation.


}

Expand Down
2 changes: 1 addition & 1 deletion src/faiss.cc
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ class IndexBase : public Napi::ObjectWrap<T>
idx_t label = I[i];
float distance = D[i];
arr_distances[i] = Napi::Number::New(env, distance);
arr_labels[i] = Napi::Number::New(env, label);
arr_labels[i] = Napi::BigInt::New(env, label);
}
delete[] I;
delete[] D;
Expand Down
24 changes: 12 additions & 12 deletions test/IndexFlatL2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ describe('IndexFlatL2', () => {
});

it('returns search results', () => {
expect(index.search([1, 0], 1)).toMatchObject({ distances: [0], labels: [0] });
expect(index.search([1, 0], 4)).toMatchObject({ distances: [0, 1, 4, 9], labels: [0, 3, 1, 2] });
expect(index.search([1, 1], 4)).toMatchObject({ distances: [0, 1, 1, 4], labels: [3, 0, 1, 2] });
expect(index.search([1, 0], 1)).toMatchObject({ distances: [0], labels: [0n] });
expect(index.search([1, 0], 4)).toMatchObject({ distances: [0, 1, 4, 9], labels: [0n, 3n, 1n, 2n] });
expect(index.search([1, 1], 4)).toMatchObject({ distances: [0, 1, 1, 4], labels: [3n, 0n, 1n, 2n] });
});
});

Expand Down Expand Up @@ -156,15 +156,15 @@ describe('IndexFlatL2', () => {
it("returns search results on merged index", () => {
expect(index2.search([1, 0], 1)).toMatchObject({
distances: [0],
labels: [0],
labels: [0n],
});
expect(index2.search([1, 0], 4)).toMatchObject({
distances: [0, 1, 4, 9],
labels: [0, 3, 1, 2],
labels: [0n, 3n, 1n, 2n],
});
expect(index2.search([1, 1], 4)).toMatchObject({
distances: [0, 1, 1, 4],
labels: [3, 0, 1, 2],
labels: [3n, 0n, 1n, 2n],
});
});
});
Expand Down Expand Up @@ -201,21 +201,21 @@ describe('IndexFlatL2', () => {
});

it("correctly removed", () => {
expect(index.search([1, 1], 1)).toMatchObject({ distances: [0], labels: [1] });
expect(index.search([1, 1], 1)).toMatchObject({ distances: [0], labels: [1n] });
expect(index.removeIds([0])).toBe(1);
expect(index.search([1, 1], 1)).toMatchObject({ distances: [0], labels: [0] });
expect(index.search([1, 1], 1)).toMatchObject({ distances: [0], labels: [0n] });
});

it("correctly removed multiple elements", () => {
expect(index.search([1, 3], 1)).toMatchObject({ distances: [0], labels: [3] });
expect(index.search([1, 3], 1)).toMatchObject({ distances: [0], labels: [3n] });
expect(index.removeIds([0, 1])).toBe(2);
expect(index.search([1, 3], 1)).toMatchObject({ distances: [0], labels: [1] });
expect(index.search([1, 3], 1)).toMatchObject({ distances: [0], labels: [1n] });
});

it("correctly removed partal elements", () => {
expect(index.search([1, 3], 1)).toMatchObject({ distances: [0], labels: [3] });
expect(index.search([1, 3], 1)).toMatchObject({ distances: [0], labels: [3n] });
expect(index.removeIds([0, 1, 2, 4, 5])).toBe(3);
expect(index.search([1, 3], 1)).toMatchObject({ distances: [0], labels: [0] });
expect(index.search([1, 3], 1)).toMatchObject({ distances: [0], labels: [0n] });
});
});
});