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

fix: fix dot distance for cuda #2914

Merged
merged 1 commit into from
Sep 20, 2024
Merged
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: 4 additions & 2 deletions python/python/lance/torch/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,5 +260,7 @@ def dot_distance(x: torch.Tensor, y: torch.Tensor) -> Tuple[torch.Tensor, torch.
dists = 1 - x @ y.T
idx = torch.argmin(dists, dim=1, keepdim=True)
dists = dists.take_along_dim(idx, dim=1).reshape(-1)
idx = torch.where(dists.isnan(), torch.nan, idx)
return idx.reshape(-1), dists.reshape(-1)
idx = idx.reshape(-1)
dists = dists.reshape(-1)
idx = torch.where(dists.isnan(), -1, idx)
return idx, dists
21 changes: 13 additions & 8 deletions python/python/tests/test_indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ def test_ivf_centroids(tmpdir, rand_dataset):
assert ivf.centroids == reloaded.centroids


def test_ivf_centroids_mostly_null(mostly_null_dataset):
ivf = IndicesBuilder(mostly_null_dataset, "vectors").train_ivf(sample_rate=16)
@pytest.mark.parametrize("distance_type", ["l2", "cosine", "dot"])
def test_ivf_centroids_mostly_null(mostly_null_dataset, distance_type):
ivf = IndicesBuilder(mostly_null_dataset, "vectors").train_ivf(
sample_rate=16, distance_type=distance_type
)

assert ivf.distance_type == "l2"
assert ivf.distance_type == distance_type
assert len(ivf.centroids) == NUM_PARTITIONS


Expand All @@ -77,12 +80,13 @@ def test_ivf_centroids_cuda(rand_dataset):


@pytest.mark.cuda
def test_ivf_centroids_mostly_null_cuda(mostly_null_dataset):
@pytest.mark.parametrize("distance_type", ["l2", "cosine", "dot"])
def test_ivf_centroids_mostly_null_cuda(mostly_null_dataset, distance_type):
ivf = IndicesBuilder(mostly_null_dataset, "vectors").train_ivf(
sample_rate=16, accelerator="cuda"
sample_rate=16, accelerator="cuda", distance_type=distance_type
)

assert ivf.distance_type == "l2"
assert ivf.distance_type == distance_type
assert len(ivf.centroids) == NUM_PARTITIONS


Expand Down Expand Up @@ -156,10 +160,11 @@ def test_assign_partitions(rand_dataset, rand_ivf):


@pytest.mark.cuda
def test_assign_partitions_mostly_null(mostly_null_dataset):
@pytest.mark.parametrize("distance_type", ["l2", "cosine", "dot"])
def test_assign_partitions_mostly_null(mostly_null_dataset, distance_type):
centroids = np.random.rand(DIMENSION * 100).astype(np.float32)
centroids = pa.FixedSizeListArray.from_arrays(centroids, DIMENSION)
ivf = IvfModel(centroids, "l2")
ivf = IvfModel(centroids, distance_type)

builder = IndicesBuilder(mostly_null_dataset, "vectors")

Expand Down