nmslib / hnswlib

Header-only C++/python library for fast approximate nearest neighbors
https://github.com/nmslib/hnswlib
Apache License 2.0
4.11k stars 607 forks source link

InnerProduct possible BUG: 1.0 - sum(x * y). In faiss and milvus, its just sum(x * y) #552

Closed Arthur-Bi closed 3 months ago

Arthur-Bi commented 3 months ago

The original code is https://github.com/nmslib/hnswlib/commit/6e829d1f591619ee597938cae0c4c20112749d4e which is 1.0 - sum(-1 x y)

Then in https://github.com/nmslib/hnswlib/commit/a9a2cd5b110415dbd06fd89caac2d11e9c780f9b Code is changed to 1.0 - sum(x * y)

Q1: why is -1 deleted in second commit?

*Q2: why is 1.0 - sum(-1 x y) in the original code? Because I checked the IP in milvus and faiss, it's just sum(x y)**

Faiss

FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
float fvec_inner_product(const float* x, const float* y, size_t d) {
    float res = 0.F;
    FAISS_PRAGMA_IMPRECISE_LOOP
    for (size_t i = 0; i != d; ++i) {
        res += x[i] * y[i];
    }
    return res;
}

Milvus

func IPImplPure(a []float32, b []float32) float32 {
    var sum float32

    for i := range a {
        sum += a[i] * b[i]
    }

    return sum
}