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

Memory optimization for validation stage #520

Closed
wants to merge 3 commits into from
Closed
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
17 changes: 17 additions & 0 deletions oml/utils/misc_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ def pairwise_dist(x1: Tensor, x2: Tensor, p: int = 2) -> Tensor:
return cdist(x1=x1, x2=x2, p=p)


def batched_knn(
query: torch.Tensor, gallery: torch.Tensor, k_neigh: int, batch_size: int
) -> Tuple[torch.FloatTensor, torch.LongTensor]:
ids_neigh = torch.LongTensor(query.shape[0], k_neigh)
distances_neigh = torch.FloatTensor(query.shape[0], k_neigh)

for i in range(0, query.shape[0], batch_size):
distances_batch = pairwise_dist(x1=query[i : i + batch_size], x2=gallery)
distances_batch_neigh, ids_batch_neigh = torch.topk(distances_batch, k=k_neigh, largest=False, sorted=True)

ids_neigh[i : i + batch_size] = ids_batch_neigh
distances_neigh[i : i + batch_size] = distances_batch_neigh

return distances_neigh, ids_neigh


def normalise(x: Tensor, p: int = 2) -> Tensor:
"""
Args:
Expand Down Expand Up @@ -456,6 +472,7 @@ def _check_dimensions(self, n_components: int) -> None:
__all__ = [
"elementwise_dist",
"pairwise_dist",
"batched_knn",
"OnlineCalc",
"AvgOnline",
"SumOnline",
Expand Down
18 changes: 18 additions & 0 deletions tests/test_oml/test_utils/test_misc_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
from oml.utils.misc_torch import (
PCA,
assign_2d,
batched_knn,
drop_duplicates_by_ids,
elementwise_dist,
pairwise_dist,
take_2d,
)

Expand All @@ -23,6 +25,22 @@ def test_elementwise_dist() -> None:
assert torch.isclose(val_torch, torch.tensor(val_custom)).all()


def test_batched_knn(n_generations: int = 5) -> None:
for _ in range(n_generations):
nq, ng, dim = 30, 50, 16
k, bs = 13, 9

query = torch.randn(nq, dim).float()
gallery = torch.randn(ng, dim).float()
distances_neigh, ids_neih = batched_knn(query, gallery, k_neigh=k, batch_size=bs)

distances = pairwise_dist(query, gallery)
distances_top_k_expected, ids_top_k_expected = torch.topk(distances, k=k, largest=False, sorted=True)

assert torch.isclose(distances_neigh, distances_top_k_expected).all()
assert (ids_neih == ids_top_k_expected).all()


# fmt: off
def test_take_2d() -> None:
x = torch.tensor([
Expand Down
Loading