Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
torch-cluster
Commits
2bf5e763
Commit
2bf5e763
authored
Aug 24, 2020
by
rusty1s
Browse files
fix cosine knn computation bug
parent
2de4d541
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
10 additions
and
7 deletions
+10
-7
csrc/cuda/knn_cuda.cu
csrc/cuda/knn_cuda.cu
+9
-6
test/test_knn.py
test/test_knn.py
+1
-1
No files found.
csrc/cuda/knn_cuda.cu
View file @
2bf5e763
...
...
@@ -8,18 +8,20 @@
template
<
typename
scalar_t
>
struct
Cosine
{
static
inline
__device__
scalar_t
dot
(
const
scalar_t
*
a
,
const
scalar_t
*
b
,
int64_t
n_a
,
int64_t
n_b
,
int64_t
size
)
{
scalar_t
result
=
0
;
for
(
int64_t
i
=
0
;
i
<
size
;
i
++
)
{
result
+=
a
[
i
]
*
b
[
i
];
result
+=
a
[
n_a
*
size
+
i
]
*
b
[
n_b
*
size
+
i
];
}
return
result
;
}
static
inline
__device__
scalar_t
norm
(
const
scalar_t
*
a
,
int64_t
size
)
{
static
inline
__device__
scalar_t
norm
(
const
scalar_t
*
a
,
int64_t
n_a
,
int64_t
size
)
{
scalar_t
result
=
0
;
for
(
int64_t
i
=
0
;
i
<
size
;
i
++
)
{
result
+=
a
[
i
]
*
a
[
i
];
result
+=
a
[
n_a
*
size
+
i
]
*
a
[
n_a
*
size
+
i
];
}
return
sqrt
(
result
);
}
...
...
@@ -50,9 +52,10 @@ __global__ void knn_kernel(const scalar_t *x, const scalar_t *y,
scalar_t
tmp_dist
=
0
;
if
(
cosine
)
{
tmp_dist
=
Cosine
<
scalar_t
>::
norm
(
x
,
dim
)
*
Cosine
<
scalar_t
>::
norm
(
y
,
dim
)
-
Cosine
<
scalar_t
>::
dot
(
x
,
y
,
dim
);
tmp_dist
=
Cosine
<
scalar_t
>::
dot
(
x
,
y
,
n_x
,
n_y
,
dim
)
/
(
Cosine
<
scalar_t
>::
norm
(
x
,
n_x
,
dim
)
*
Cosine
<
scalar_t
>::
norm
(
y
,
n_y
,
dim
));
tmp_dist
=
1.
-
tmp_dist
;
}
else
{
for
(
int64_t
d
=
0
;
d
<
dim
;
d
++
)
{
tmp_dist
+=
(
x
[
n_x
*
dim
+
d
]
-
y
[
n_y
*
dim
+
d
])
*
...
...
test/test_knn.py
View file @
2bf5e763
...
...
@@ -40,7 +40,7 @@ def test_knn(dtype, device):
if
x
.
is_cuda
:
edge_index
=
knn
(
x
,
y
,
2
,
batch_x
,
batch_y
,
cosine
=
True
)
assert
to_set
(
edge_index
)
==
set
([(
0
,
0
),
(
0
,
1
),
(
1
,
4
),
(
1
,
5
)])
assert
to_set
(
edge_index
)
==
set
([(
0
,
2
),
(
0
,
3
),
(
1
,
4
),
(
1
,
5
)])
@
pytest
.
mark
.
parametrize
(
'dtype,device'
,
product
(
grad_dtypes
,
devices
))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment