hnswlib icon indicating copy to clipboard operation
hnswlib copied to clipboard

Normalization should not be done in the wrapper

Open DavidGOrtega opened this issue 2 years ago • 2 comments

Normalization should be done on the C++ side (if any, my vectors come out from my models already normalised). If not every time the C++ code is used by another programming languages (i.e. nodejs) normalization has to be reimplemented

https://github.com/nmslib/hnswlib/blob/359b2ba87358224963986f709e593d799064ace6/python_bindings/bindings.cpp#L273

Proposed solution is to move this within the lib

void normalize_vector(float* data, float* norm_array) {
        float norm = 0.0f;
        for (int i = 0; i < dim; i++)
            norm += data[i] * data[i];
        norm = 1.0f / (sqrtf(norm) + 1e-30f);
        for (int i = 0; i < dim; i++)
            norm_array[i] = data[i] * norm;
    }

DavidGOrtega avatar Apr 08 '23 11:04 DavidGOrtega

Hi @DavidGOrtega, Sure, we can move it. PRs are welcome. Thanks!

yurymalkov avatar Apr 10 '23 02:04 yurymalkov

I agree. I think it would be a good idea to have this functionality implemented internally, as opposed to being binding specific.

kiplingw avatar Oct 29 '24 05:10 kiplingw