Skip to content

Commit 6aab27f

Browse files
committed
Fix numerical errors
1 parent 8a875e1 commit 6aab27f

4 files changed

Lines changed: 9 additions & 6 deletions

File tree

benchmarks/test_benchmark_knn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def _assert_knn_within_cuda(
5252
tol=None,
5353
):
5454
if tol is None:
55-
tol = 2 * torch.finfo(x.dtype).eps
55+
tol = 5 * torch.finfo(x.dtype).eps
5656
m = y.size(0)
5757
cuda_rows = out_cuda[0]
5858
cuda_cols = out_cuda[1]

benchmarks/test_benchmark_nearest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def _assert_nearest_within_cuda(
4141
tol: float | None = None,
4242
) -> None:
4343
if tol is None:
44-
tol = 2 * torch.finfo(x.dtype).eps
44+
tol = 5 * torch.finfo(x.dtype).eps
4545
x_f = x.float()
4646
y_f = y.float()
4747
cuda_dist = ((x_f - y_f[out_cuda]) ** 2).sum(dim=1)

benchmarks/test_benchmark_radius.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def _assert_radius_within_cuda(
5353
if edge_index.numel() == 0:
5454
return
5555
if tol is None:
56-
tol = torch.finfo(x.dtype).eps
56+
tol = 5 * torch.finfo(x.dtype).eps
5757
row, col = edge_index
5858
x_f = x.float()
5959
y_f = y.float()

torch_cluster/triton/_kernels.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ def _nearest_kernel(
147147
)
148148
x_sq = x_t
149149
x_block_ptr_sq = tl.advance(x_block_ptr_sq, (BLOCK_K, 0))
150+
x_sq += x_sq_c
150151
if COSINE:
151152
inv_x = tl.rsqrt(x_sq + EPS) # 1/||x||.
152153

@@ -220,13 +221,15 @@ def _nearest_kernel(
220221
y_block_ptr = tl.advance(y_block_ptr, (0, BLOCK_K))
221222
x_block_ptr = tl.advance(x_block_ptr, (BLOCK_K, 0))
222223

224+
acc += acc_c
225+
y_sq += y_sq_c
223226
if COSINE:
224-
inv_y = tl.rsqrt(y_sq + y_sq_c + EPS) # 1/||y||.
227+
inv_y = tl.rsqrt(y_sq + EPS) # 1/||y||.
225228
dist = 1.0 - acc * (inv_y[:, None] * inv_x[None, :])
226229
else:
227230
dist = (
228-
(y_sq + y_sq_c)[:, None]
229-
+ (x_sq + x_sq_c)[None, :]
231+
(y_sq)[:, None]
232+
+ (x_sq)[None, :]
230233
- 2.0 * acc
231234
)
232235

0 commit comments

Comments
 (0)