Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
32 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
"""Trivial version test.""" | ||
|
||
import unittest | ||
|
||
import torch | ||
|
||
from torch_max_mem import maximize_memory_utilization | ||
|
||
|
||
def knn(x, y, batch_size, k: int = 3): | ||
return torch.cat( | ||
[ | ||
torch.cdist(x[start: start + batch_size], y).topk(k=k, dim=0, largest=False).indices | ||
for start in range(0, x.shape[0], batch_size) | ||
], | ||
dim=0, | ||
) | ||
|
||
|
||
wrapped_knn = maximize_memory_utilization(parameter_name="batch_size")(knn) | ||
|
||
|
||
class TestDecorator(unittest.TestCase): | ||
"""Test the decorator.""" | ||
|
||
def test_knn(self): | ||
x = torch.rand(100, 100) | ||
y = torch.rand(200, 100) | ||
for batch_size in [1, 10, x.shape[0]]: | ||
self.assertEqual(knn(x, y, batch_size), wrapped_knn(x, y, batch_size=x.shape[0])) |
This file was deleted.
Oops, something went wrong.