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-scatter
Commits
c1ac176e
"src/vscode:/vscode.git/clone" did not exist on "fff4be8e23b8b44e756cf54bc4f17e8bd637bbc5"
Commit
c1ac176e
authored
Aug 06, 2018
by
rusty1s
Browse files
reset
parent
5909accb
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
125 additions
and
139 deletions
+125
-139
cpu/scatter.cpp
cpu/scatter.cpp
+9
-13
test/test_backward.py
test/test_backward.py
+16
-18
test/test_forward.py
test/test_forward.py
+100
-105
torch_scatter/mul.py
torch_scatter/mul.py
+0
-3
No files found.
cpu/scatter.cpp
View file @
c1ac176e
...
@@ -4,19 +4,15 @@
...
@@ -4,19 +4,15 @@
void
scatter_mul
(
at
::
Tensor
src
,
at
::
Tensor
index
,
at
::
Tensor
out
,
void
scatter_mul
(
at
::
Tensor
src
,
at
::
Tensor
index
,
at
::
Tensor
out
,
int64_t
dim
)
{
int64_t
dim
)
{
printf
(
"HUHUHUHU"
);
int64_t
elems_per_row
=
index
.
size
(
dim
),
i
,
idx
;
// int64_t elems_per_row = index.size(dim), i, idx;
AT_DISPATCH_ALL_TYPES
(
src
.
type
(),
"scatter_mul"
,
[
&
]
{
// printf("elems_per_row: %lli\n", elems_per_row);
DIM_APPLY3
(
scalar_t
,
src
,
int64_t
,
index
,
scalar_t
,
out
,
dim
,
{
// AT_DISPATCH_ALL_TYPES(src.type(), "scatter_mul", [&] {
for
(
i
=
0
;
i
<
elems_per_row
;
i
++
)
{
// DIM_APPLY3(scalar_t, src, int64_t, index, scalar_t, out, dim, {
idx
=
index_data
[
i
*
index_stride
];
// for (i = 0; i < elems_per_row; i++) {
out_data
[
idx
*
out_stride
]
*=
src_data
[
i
*
src_stride
];
// idx = index_data[i * index_stride];
}
// printf("i: %lli, idx: %lli\n", i, idx);
});
// printf("src: %lli\n", (int64_t)src_data[i * src_stride]);
});
// out_data[idx * out_stride] *= src_data[i * src_stride];
// }
// });
// });
}
}
void
scatter_div
(
at
::
Tensor
src
,
at
::
Tensor
index
,
at
::
Tensor
out
,
void
scatter_div
(
at
::
Tensor
src
,
at
::
Tensor
index
,
at
::
Tensor
out
,
...
...
test/test_backward.py
View file @
c1ac176e
...
@@ -2,7 +2,7 @@ from itertools import product
...
@@ -2,7 +2,7 @@ from itertools import product
import
pytest
import
pytest
import
torch
import
torch
#
from torch.autograd import gradcheck
from
torch.autograd
import
gradcheck
import
torch_scatter
import
torch_scatter
from
.utils
import
grad_dtypes
as
dtypes
,
devices
,
tensor
from
.utils
import
grad_dtypes
as
dtypes
,
devices
,
tensor
...
@@ -13,14 +13,13 @@ indices = [2, 0, 1, 1, 0]
...
@@ -13,14 +13,13 @@ indices = [2, 0, 1, 1, 0]
@
pytest
.
mark
.
parametrize
(
'func,device'
,
product
(
funcs
,
devices
))
@
pytest
.
mark
.
parametrize
(
'func,device'
,
product
(
funcs
,
devices
))
def
test_backward
(
func
,
device
):
def
test_backward
(
func
,
device
):
pass
index
=
torch
.
tensor
(
indices
,
dtype
=
torch
.
long
,
device
=
device
)
# index = torch.tensor(indices, dtype=torch.long, device=device)
src
=
torch
.
rand
((
index
.
size
(
0
),
2
),
dtype
=
torch
.
double
,
device
=
device
)
# src = torch.rand((index.size(0), 2), dtype=torch.double, device=device)
src
.
requires_grad_
()
# src.requires_grad_()
#
op = getattr(torch_scatter, 'scatter_{}'.format(func))
op
=
getattr
(
torch_scatter
,
'scatter_{}'
.
format
(
func
))
#
data = (src, index, 0)
data
=
(
src
,
index
,
0
)
#
assert gradcheck(op, data, eps=1e-6, atol=1e-4) is True
assert
gradcheck
(
op
,
data
,
eps
=
1e-6
,
atol
=
1e-4
)
is
True
tests
=
[{
tests
=
[{
...
@@ -44,13 +43,12 @@ tests = [{
...
@@ -44,13 +43,12 @@ tests = [{
@
pytest
.
mark
.
parametrize
(
'test,dtype,device'
,
product
(
tests
,
dtypes
,
devices
))
@
pytest
.
mark
.
parametrize
(
'test,dtype,device'
,
product
(
tests
,
dtypes
,
devices
))
def
test_arg_backward
(
test
,
dtype
,
device
):
def
test_arg_backward
(
test
,
dtype
,
device
):
pass
src
=
tensor
(
test
[
'src'
],
dtype
,
device
)
# src = tensor(test['src'], dtype, device)
src
.
requires_grad_
()
# src.requires_grad_()
index
=
tensor
(
test
[
'index'
],
torch
.
long
,
device
)
# index = tensor(test['index'], torch.long, device)
grad
=
tensor
(
test
[
'grad'
],
dtype
,
device
)
# grad = tensor(test['grad'], dtype, device)
op
=
getattr
(
torch_scatter
,
'scatter_{}'
.
format
(
test
[
'name'
]))
# op = getattr(torch_scatter, 'scatter_{}'.format(test['name']))
out
,
_
=
op
(
src
,
index
,
test
[
'dim'
],
fill_value
=
test
[
'fill_value'
])
# out, _ = op(src, index, test['dim'], fill_value=test['fill_value'])
out
.
backward
(
grad
)
# out.backward(grad)
assert
src
.
grad
.
tolist
()
==
test
[
'expected'
]
# assert src.grad.tolist() == test['expected']
test/test_forward.py
View file @
c1ac176e
...
@@ -6,110 +6,108 @@ import torch_scatter
...
@@ -6,110 +6,108 @@ import torch_scatter
from
.utils
import
dtypes
,
devices
,
tensor
from
.utils
import
dtypes
,
devices
,
tensor
dtypes
=
[
torch
.
float
]
tests
=
[{
tests
=
[{
#
'name': 'add',
'name'
:
'add'
,
#
'src': [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
'src'
:
[[
2
,
0
,
1
,
4
,
3
],
[
0
,
2
,
1
,
3
,
4
]],
#
'index': [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
'index'
:
[[
4
,
5
,
4
,
2
,
3
],
[
0
,
0
,
2
,
2
,
1
]],
#
'dim': -1,
'dim'
:
-
1
,
#
'fill_value': 0,
'fill_value'
:
0
,
#
'expected': [[0, 0, 4, 3, 3, 0], [2, 4, 4, 0, 0, 0]],
'expected'
:
[[
0
,
0
,
4
,
3
,
3
,
0
],
[
2
,
4
,
4
,
0
,
0
,
0
]],
#
}, {
},
{
#
'name': 'add',
'name'
:
'add'
,
#
'src': [[5, 2], [2, 5], [4, 3], [1, 3]],
'src'
:
[[
5
,
2
],
[
2
,
5
],
[
4
,
3
],
[
1
,
3
]],
#
'index': [0, 1, 1, 0],
'index'
:
[
0
,
1
,
1
,
0
],
#
'dim': 0,
'dim'
:
0
,
#
'fill_value': 0,
'fill_value'
:
0
,
#
'expected': [[6, 5], [6, 8]],
'expected'
:
[[
6
,
5
],
[
6
,
8
]],
#
}, {
},
{
#
'name': 'sub',
'name'
:
'sub'
,
#
'src': [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
'src'
:
[[
2
,
0
,
1
,
4
,
3
],
[
0
,
2
,
1
,
3
,
4
]],
#
'index': [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
'index'
:
[[
4
,
5
,
4
,
2
,
3
],
[
0
,
0
,
2
,
2
,
1
]],
#
'dim': -1,
'dim'
:
-
1
,
#
'fill_value': 9,
'fill_value'
:
9
,
#
'expected': [[9, 9, 5, 6, 6, 9], [7, 5, 5, 9, 9, 9]],
'expected'
:
[[
9
,
9
,
5
,
6
,
6
,
9
],
[
7
,
5
,
5
,
9
,
9
,
9
]],
#
}, {
},
{
#
'name': 'sub',
'name'
:
'sub'
,
#
'src': [[5, 2], [2, 2], [4, 2], [1, 3]],
'src'
:
[[
5
,
2
],
[
2
,
2
],
[
4
,
2
],
[
1
,
3
]],
#
'index': [0, 1, 1, 0],
'index'
:
[
0
,
1
,
1
,
0
],
#
'dim': 0,
'dim'
:
0
,
#
'fill_value': 9,
'fill_value'
:
9
,
#
'expected': [[3, 4], [3, 5]],
'expected'
:
[[
3
,
4
],
[
3
,
5
]],
#
}, {
},
{
'name'
:
'mul'
,
'name'
:
'mul'
,
'src'
:
[[
2
,
0
,
1
,
4
,
3
],
[
0
,
2
,
1
,
3
,
4
]],
'src'
:
[[
2
,
0
,
1
,
4
,
3
],
[
0
,
2
,
1
,
3
,
4
]],
'index'
:
[[
4
,
5
,
4
,
2
,
3
],
[
0
,
0
,
2
,
2
,
1
]],
'index'
:
[[
4
,
5
,
4
,
2
,
3
],
[
0
,
0
,
2
,
2
,
1
]],
'dim'
:
-
1
,
'dim'
:
-
1
,
'fill_value'
:
1
,
'fill_value'
:
1
,
'expected'
:
[[
1
,
1
,
4
,
3
,
2
,
0
],
[
0
,
4
,
3
,
1
,
1
,
1
]],
'expected'
:
[[
1
,
1
,
4
,
3
,
2
,
0
],
[
0
,
4
,
3
,
1
,
1
,
1
]],
#
}, {
},
{
#
'name': 'mul',
'name'
:
'mul'
,
#
'src': [[5, 2], [2, 5], [4, 3], [1, 3]],
'src'
:
[[
5
,
2
],
[
2
,
5
],
[
4
,
3
],
[
1
,
3
]],
#
'index': [0, 1, 1, 0],
'index'
:
[
0
,
1
,
1
,
0
],
#
'dim': 0,
'dim'
:
0
,
#
'fill_value': 1,
'fill_value'
:
1
,
#
'expected': [[5, 6], [8, 15]],
'expected'
:
[[
5
,
6
],
[
8
,
15
]],
#
}, {
},
{
#
'name': 'div',
'name'
:
'div'
,
#
'src': [[2, 1, 1, 4, 2], [1, 2, 1, 2, 4]],
'src'
:
[[
2
,
1
,
1
,
4
,
2
],
[
1
,
2
,
1
,
2
,
4
]],
#
'index': [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
'index'
:
[[
4
,
5
,
4
,
2
,
3
],
[
0
,
0
,
2
,
2
,
1
]],
#
'dim': -1,
'dim'
:
-
1
,
#
'fill_value': 1,
'fill_value'
:
1
,
#
'expected': [[1, 1, 0.25, 0.5, 0.5, 1], [0.5, 0.25, 0.5, 1, 1, 1]],
'expected'
:
[[
1
,
1
,
0.25
,
0.5
,
0.5
,
1
],
[
0.5
,
0.25
,
0.5
,
1
,
1
,
1
]],
#
}, {
},
{
#
'name': 'div',
'name'
:
'div'
,
#
'src': [[4, 2], [2, 1], [4, 2], [1, 2]],
'src'
:
[[
4
,
2
],
[
2
,
1
],
[
4
,
2
],
[
1
,
2
]],
#
'index': [0, 1, 1, 0],
'index'
:
[
0
,
1
,
1
,
0
],
#
'dim': 0,
'dim'
:
0
,
#
'fill_value': 1,
'fill_value'
:
1
,
#
'expected': [[0.25, 0.25], [0.125, 0.5]],
'expected'
:
[[
0.25
,
0.25
],
[
0.125
,
0.5
]],
#
}, {
},
{
#
'name': 'mean',
'name'
:
'mean'
,
#
'src': [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
'src'
:
[[
2
,
0
,
1
,
4
,
3
],
[
0
,
2
,
1
,
3
,
4
]],
#
'index': [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
'index'
:
[[
4
,
5
,
4
,
2
,
3
],
[
0
,
0
,
2
,
2
,
1
]],
#
'dim': -1,
'dim'
:
-
1
,
#
'fill_value': 0,
'fill_value'
:
0
,
#
'expected': [[0, 0, 4, 3, 1.5, 0], [1, 4, 2, 0, 0, 0]],
'expected'
:
[[
0
,
0
,
4
,
3
,
1.5
,
0
],
[
1
,
4
,
2
,
0
,
0
,
0
]],
#
}, {
},
{
#
'name': 'mean',
'name'
:
'mean'
,
#
'src': [[5, 2], [2, 5], [4, 3], [1, 3]],
'src'
:
[[
5
,
2
],
[
2
,
5
],
[
4
,
3
],
[
1
,
3
]],
#
'index': [0, 1, 1, 0],
'index'
:
[
0
,
1
,
1
,
0
],
#
'dim': 0,
'dim'
:
0
,
#
'fill_value': 0,
'fill_value'
:
0
,
#
'expected': [[3, 2.5], [3, 4]],
'expected'
:
[[
3
,
2.5
],
[
3
,
4
]],
#
}, {
},
{
#
'name': 'max',
'name'
:
'max'
,
#
'src': [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
'src'
:
[[
2
,
0
,
1
,
4
,
3
],
[
0
,
2
,
1
,
3
,
4
]],
#
'index': [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
'index'
:
[[
4
,
5
,
4
,
2
,
3
],
[
0
,
0
,
2
,
2
,
1
]],
#
'dim': -1,
'dim'
:
-
1
,
#
'fill_value': 0,
'fill_value'
:
0
,
#
'expected': [[0, 0, 4, 3, 2, 0], [2, 4, 3, 0, 0, 0]],
'expected'
:
[[
0
,
0
,
4
,
3
,
2
,
0
],
[
2
,
4
,
3
,
0
,
0
,
0
]],
#
'expected_arg': [[-1, -1, 3, 4, 0, 1], [1, 4, 3, -1, -1, -1]],
'expected_arg'
:
[[
-
1
,
-
1
,
3
,
4
,
0
,
1
],
[
1
,
4
,
3
,
-
1
,
-
1
,
-
1
]],
#
}, {
},
{
#
'name': 'max',
'name'
:
'max'
,
#
'src': [[5, 2], [2, 5], [4, 3], [1, 3]],
'src'
:
[[
5
,
2
],
[
2
,
5
],
[
4
,
3
],
[
1
,
3
]],
#
'index': [0, 1, 1, 0],
'index'
:
[
0
,
1
,
1
,
0
],
#
'dim': 0,
'dim'
:
0
,
#
'fill_value': 0,
'fill_value'
:
0
,
#
'expected': [[5, 3], [4, 5]],
'expected'
:
[[
5
,
3
],
[
4
,
5
]],
#
'expected_arg': [[0, 3], [2, 1]],
'expected_arg'
:
[[
0
,
3
],
[
2
,
1
]],
#
}, {
},
{
#
'name': 'min',
'name'
:
'min'
,
#
'src': [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
'src'
:
[[
2
,
0
,
1
,
4
,
3
],
[
0
,
2
,
1
,
3
,
4
]],
#
'index': [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
'index'
:
[[
4
,
5
,
4
,
2
,
3
],
[
0
,
0
,
2
,
2
,
1
]],
#
'dim': -1,
'dim'
:
-
1
,
#
'fill_value': 9,
'fill_value'
:
9
,
#
'expected': [[9, 9, 4, 3, 1, 0], [0, 4, 1, 9, 9, 9]],
'expected'
:
[[
9
,
9
,
4
,
3
,
1
,
0
],
[
0
,
4
,
1
,
9
,
9
,
9
]],
#
'expected_arg': [[-1, -1, 3, 4, 2, 1], [0, 4, 2, -1, -1, -1]],
'expected_arg'
:
[[
-
1
,
-
1
,
3
,
4
,
2
,
1
],
[
0
,
4
,
2
,
-
1
,
-
1
,
-
1
]],
#
}, {
},
{
#
'name': 'min',
'name'
:
'min'
,
#
'src': [[5, 2], [2, 5], [4, 3], [1, 3]],
'src'
:
[[
5
,
2
],
[
2
,
5
],
[
4
,
3
],
[
1
,
3
]],
#
'index': [0, 1, 1, 0],
'index'
:
[
0
,
1
,
1
,
0
],
#
'dim': 0,
'dim'
:
0
,
#
'fill_value': 9,
'fill_value'
:
9
,
#
'expected': [[1, 2], [2, 3]],
'expected'
:
[[
1
,
2
],
[
2
,
3
]],
#
'expected_arg': [[3, 0], [1, 2]],
'expected_arg'
:
[[
3
,
0
],
[
1
,
2
]],
}]
}]
...
@@ -118,15 +116,12 @@ def test_forward(test, dtype, device):
...
@@ -118,15 +116,12 @@ def test_forward(test, dtype, device):
src
=
tensor
(
test
[
'src'
],
dtype
,
device
)
src
=
tensor
(
test
[
'src'
],
dtype
,
device
)
index
=
tensor
(
test
[
'index'
],
torch
.
long
,
device
)
index
=
tensor
(
test
[
'index'
],
torch
.
long
,
device
)
expected
=
tensor
(
test
[
'expected'
],
dtype
,
device
)
expected
=
tensor
(
test
[
'expected'
],
dtype
,
device
)
print
(
src
)
print
(
index
)
op
=
getattr
(
torch_scatter
,
'scatter_{}'
.
format
(
test
[
'name'
]))
op
=
getattr
(
torch_scatter
,
'scatter_{}'
.
format
(
test
[
'name'
]))
out
=
op
(
src
,
index
,
test
[
'dim'
],
fill_value
=
test
[
'fill_value'
])
out
=
op
(
src
,
index
,
test
[
'dim'
],
fill_value
=
test
[
'fill_value'
])
print
(
out
)
#
if isinstance(out, tuple):
if
isinstance
(
out
,
tuple
):
#
assert out[0].tolist() == expected.tolist()
assert
out
[
0
].
tolist
()
==
expected
.
tolist
()
#
assert out[1].tolist() == test['expected_arg']
assert
out
[
1
].
tolist
()
==
test
[
'expected_arg'
]
#
else:
else
:
#
assert out.tolist() == expected.tolist()
assert
out
.
tolist
()
==
expected
.
tolist
()
torch_scatter/mul.py
View file @
c1ac176e
...
@@ -7,11 +7,8 @@ from torch_scatter.utils.gen import gen
...
@@ -7,11 +7,8 @@ from torch_scatter.utils.gen import gen
class
ScatterMul
(
Function
):
class
ScatterMul
(
Function
):
@
staticmethod
@
staticmethod
def
forward
(
ctx
,
out
,
src
,
index
,
dim
):
def
forward
(
ctx
,
out
,
src
,
index
,
dim
):
print
(
"DRIN"
)
func
=
get_func
(
'scatter_mul'
,
src
)
func
=
get_func
(
'scatter_mul'
,
src
)
print
(
func
)
func
(
src
,
index
,
out
,
dim
)
func
(
src
,
index
,
out
,
dim
)
print
(
out
)
ctx
.
mark_dirty
(
out
)
ctx
.
mark_dirty
(
out
)
ctx
.
save_for_backward
(
out
,
src
,
index
)
ctx
.
save_for_backward
(
out
,
src
,
index
)
...
...
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