Skip to content

METRIC_Lp issues with IndexHNSWFlat and IndexFlat #4224

@rohildshah

Description

@rohildshah

Distances are not calculated correctly for IndexHNSWFlat using METRIC_Lp.

#include <faiss/IndexHNSW.h>
#include <iostream>

int main() {
    faiss::IndexHNSWFlat index(1, 32, faiss::METRIC_Lp);
    index.metric_arg = 3;

    float data[1] = {0.0};
    index.add(1, data);

    float query[1] = {2.0};
    float distance;
    faiss::idx_t label;
    index.search(1, query, 1, &distance, &label);

    std::cout << "Distance: " << distance << std::endl;
    std::cout << "Label: " << label << std::endl;

    return 0;
}

Running the above outputs the following.

Distance: 1
Label: 0

I would expect to see a distance of (2 ^ 3) ^ 1/3 = 2.

For a simpler example, replace the index in the above code with faiss::IndexFlat index(1, faiss::METRIC_Lp);. This outputs the following.

Distance: 8
Label: 0

The output seems to skip the final exponent of 1/3.

I am curious if either behavior is reproducible on other machines. If so, is this intended functionality?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions