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

[BUG] Distance return for inner_product metric is not expected #539

Open
wdongyu opened this issue Apr 10, 2024 · 0 comments
Open

[BUG] Distance return for inner_product metric is not expected #539

wdongyu opened this issue Apr 10, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@wdongyu
Copy link

wdongyu commented Apr 10, 2024

Expected Behavior

When inner_product is selected as metric, it compare by L2 distance internally, convert to inner_product distance correctly and return

Actual Behavior

When inner_product is selected as metric, it compare by L2 distance internally by adding an extra dimension. Let's use U and V as the two input vectors, and Q as the query vector.

Distance between U and V will compute like this (where m represents the max_norm for all data):
QianJianTec1712735290964

and distance between U and Q will compute like this(where m represents the max_norm for all data, and m_q represents the norm for the query) :
QianJianTec1712736147631

Error

There exist two problems:

  1. To return the negative inner_product distance, when constructing the graph, we should adding two times of the product of the extra dimension for unnormalized data, that's like:
    QianJianTec1712738409932

for normalized data, the extra dimension is zero for all input data, so it's ok to ignore it.

  1. To return the negative inner_product distance, when querying, the code current flip the distance by distances[i] = (-distances[i]), but instead, we should convert the distance by distances[i] = (distances[i] - 2) / 2 like following :
    QianJianTec1712739169957

Example Code

from tempfile import TemporaryDirectory

import diskannpy
import numpy as np

query = [1, 1]
search_space = [
    [10, 0], 
    [1, 0.1],  
]

with TemporaryDirectory() as tmpdir:
    diskannpy.build_disk_index(
        np.array(search_space, dtype=np.float32),
        "mips",
        tmpdir,
        75,
        60,
        1,
        1,
        2,
    )
    index = diskannpy.StaticDiskIndex(tmpdir, 2, 0)
    res = index.search(np.array(query, dtype=np.float32), 2, 2)
    assert res[0].shape == (2,)
    print(res)

Dataset Description

Please tell us about the shape and datatype of your data, (e.g. 128 dimensions, 12.3 billion points, floats)

  • Dimensions: 2
  • Number of Points: 2
  • Data type: f32

Your Environment

  • Operating system: Debian GNU/Linux 11
  • DiskANN version: 0.7.0
@wdongyu wdongyu added the bug Something isn't working label Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant