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
e2ae2360
Commit
e2ae2360
authored
Aug 05, 2018
by
rusty1s
Browse files
g++4.9
parent
58a05cff
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
124 additions
and
127 deletions
+124
-127
.travis.yml
.travis.yml
+6
-0
test/test_backward.py
test/test_backward.py
+15
-17
test/test_forward.py
test/test_forward.py
+103
-110
No files found.
.travis.yml
View file @
e2ae2360
language
:
python
language
:
python
sudo
:
required
sudo
:
required
dist
:
trusty
dist
:
trusty
addons
:
apt
:
sources
:
-
ubuntu-toolchain-r-test
packages
:
-
g++-4.9
matrix
:
matrix
:
include
:
include
:
-
python
:
2.7
-
python
:
2.7
...
...
test/test_backward.py
View file @
e2ae2360
...
@@ -14,14 +14,13 @@ indices = [2, 0, 1, 1, 0]
...
@@ -14,14 +14,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
=
[{
...
@@ -45,13 +44,12 @@ tests = [{
...
@@ -45,13 +44,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 @
e2ae2360
...
@@ -6,116 +6,109 @@ import torch_scatter
...
@@ -6,116 +6,109 @@ import torch_scatter
from
.utils
import
dtypes
,
devices
,
tensor
from
.utils
import
dtypes
,
devices
,
tensor
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'
,
{
'src'
:
[[
5
,
2
],
[
2
,
5
],
[
4
,
3
],
[
1
,
3
]],
'name'
:
'add'
,
'index'
:
[
0
,
1
,
1
,
0
],
'src'
:
[[
5
,
2
],
[
2
,
5
],
[
4
,
3
],
[
1
,
3
]],
'dim'
:
0
,
'index'
:
[
0
,
1
,
1
,
0
],
'fill_value'
:
0
,
'dim'
:
0
,
'expected'
:
[[
6
,
5
],
[
6
,
8
]],
'fill_value'
:
0
,
},
{
'expected'
:
[[
6
,
5
],
[
6
,
8
]],
'name'
:
'sub'
,
},
'src'
:
[[
2
,
0
,
1
,
4
,
3
],
[
0
,
2
,
1
,
3
,
4
]],
{
'index'
:
[[
4
,
5
,
4
,
2
,
3
],
[
0
,
0
,
2
,
2
,
1
]],
'name'
:
'sub'
,
'dim'
:
-
1
,
'src'
:
[[
2
,
0
,
1
,
4
,
3
],
[
0
,
2
,
1
,
3
,
4
]],
'fill_value'
:
9
,
'index'
:
[[
4
,
5
,
4
,
2
,
3
],
[
0
,
0
,
2
,
2
,
1
]],
'expected'
:
[[
9
,
9
,
5
,
6
,
6
,
9
],
[
7
,
5
,
5
,
9
,
9
,
9
]],
'dim'
:
-
1
,
},
{
'fill_value'
:
9
,
'name'
:
'sub'
,
'expected'
:
[[
9
,
9
,
5
,
6
,
6
,
9
],
[
7
,
5
,
5
,
9
,
9
,
9
]],
'src'
:
[[
5
,
2
],
[
2
,
2
],
[
4
,
2
],
[
1
,
3
]],
},
'index'
:
[
0
,
1
,
1
,
0
],
{
'dim'
:
0
,
'name'
:
'sub'
,
'fill_value'
:
9
,
'src'
:
[[
5
,
2
],
[
2
,
2
],
[
4
,
2
],
[
1
,
3
]],
'expected'
:
[[
3
,
4
],
[
3
,
5
]],
'index'
:
[
0
,
1
,
1
,
0
],
},
{
'dim'
:
0
,
'name'
:
'mul'
,
'fill_value'
:
9
,
'src'
:
[[
2
,
0
,
1
,
4
,
3
],
[
0
,
2
,
1
,
3
,
4
]],
'expected'
:
[[
3
,
4
],
[
3
,
5
]],
'index'
:
[[
4
,
5
,
4
,
2
,
3
],
[
0
,
0
,
2
,
2
,
1
]],
},
'dim'
:
-
1
,
{
'fill_value'
:
1
,
'name'
:
'mul'
,
'expected'
:
[[
1
,
1
,
4
,
3
,
2
,
0
],
[
0
,
4
,
3
,
1
,
1
,
1
]],
'src'
:
[[
2
,
0
,
1
,
4
,
3
],
[
0
,
2
,
1
,
3
,
4
]],
},
{
'index'
:
[[
4
,
5
,
4
,
2
,
3
],
[
0
,
0
,
2
,
2
,
1
]],
'name'
:
'mul'
,
'dim'
:
-
1
,
'src'
:
[[
5
,
2
],
[
2
,
5
],
[
4
,
3
],
[
1
,
3
]],
'fill_value'
:
1
,
'index'
:
[
0
,
1
,
1
,
0
],
'expected'
:
[[
1
,
1
,
4
,
3
,
2
,
0
],
[
0
,
4
,
3
,
1
,
1
,
1
]],
'dim'
:
0
,
},
'fill_value'
:
1
,
{
'expected'
:
[[
5
,
6
],
[
8
,
15
]],
'name'
:
'mul'
,
},
{
'src'
:
[[
5
,
2
],
[
2
,
5
],
[
4
,
3
],
[
1
,
3
]],
'name'
:
'div'
,
'index'
:
[
0
,
1
,
1
,
0
],
'src'
:
[[
2
,
1
,
1
,
4
,
2
],
[
1
,
2
,
1
,
2
,
4
]],
'dim'
:
0
,
'index'
:
[[
4
,
5
,
4
,
2
,
3
],
[
0
,
0
,
2
,
2
,
1
]],
'fill_value'
:
1
,
'dim'
:
-
1
,
'expected'
:
[[
5
,
6
],
[
8
,
15
]],
'fill_value'
:
1
,
# }, {
'expected'
:
[[
1
,
1
,
0.25
,
0.5
,
0.5
,
1
],
[
0.5
,
0.25
,
0.5
,
1
,
1
,
1
]],
# 'name': 'div',
},
{
# 'src': [[2, 1, 1, 4, 2], [1, 2, 1, 2, 4]],
'name'
:
'div'
,
# 'index': [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
'src'
:
[[
4
,
2
],
[
2
,
1
],
[
4
,
2
],
[
1
,
2
]],
# 'dim': -1,
'index'
:
[
0
,
1
,
1
,
0
],
# 'fill_value': 1,
'dim'
:
0
,
# 'expected': [[1, 1, 0.25, 0.5, 0.5, 1], [0.5, 0.25, 0.5, 1, 1, 1]],
'fill_value'
:
1
,
# }, {
'expected'
:
[[
0.25
,
0.25
],
[
0.125
,
0.5
]],
# 'name': 'div',
},
{
# 'src': [[4, 2], [2, 1], [4, 2], [1, 2]],
'name'
:
'mean'
,
# 'index': [0, 1, 1, 0],
'src'
:
[[
2
,
0
,
1
,
4
,
3
],
[
0
,
2
,
1
,
3
,
4
]],
# 'dim': 0,
'index'
:
[[
4
,
5
,
4
,
2
,
3
],
[
0
,
0
,
2
,
2
,
1
]],
# 'fill_value': 1,
'dim'
:
-
1
,
# 'expected': [[0.25, 0.25], [0.125, 0.5]],
'fill_value'
:
0
,
# }, {
'expected'
:
[[
0
,
0
,
4
,
3
,
1.5
,
0
],
[
1
,
4
,
2
,
0
,
0
,
0
]],
# 'name': 'mean',
},
{
# 'src': [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
'name'
:
'mean'
,
# 'index': [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
'src'
:
[[
5
,
2
],
[
2
,
5
],
[
4
,
3
],
[
1
,
3
]],
# 'dim': -1,
'index'
:
[
0
,
1
,
1
,
0
],
# 'fill_value': 0,
'dim'
:
0
,
# 'expected': [[0, 0, 4, 3, 1.5, 0], [1, 4, 2, 0, 0, 0]],
'fill_value'
:
0
,
# }, {
'expected'
:
[[
3
,
2.5
],
[
3
,
4
]],
# 'name': 'mean',
},
{
# 'src': [[5, 2], [2, 5], [4, 3], [1, 3]],
'name'
:
'max'
,
# 'index': [0, 1, 1, 0],
'src'
:
[[
2
,
0
,
1
,
4
,
3
],
[
0
,
2
,
1
,
3
,
4
]],
# 'dim': 0,
'index'
:
[[
4
,
5
,
4
,
2
,
3
],
[
0
,
0
,
2
,
2
,
1
]],
# 'fill_value': 0,
'dim'
:
-
1
,
# 'expected': [[3, 2.5], [3, 4]],
'fill_value'
:
0
,
# }, {
'expected'
:
[[
0
,
0
,
4
,
3
,
2
,
0
],
[
2
,
4
,
3
,
0
,
0
,
0
]],
# 'name': 'max',
'expected_arg'
:
[[
-
1
,
-
1
,
3
,
4
,
0
,
1
],
[
1
,
4
,
3
,
-
1
,
-
1
,
-
1
]],
# 'src': [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
},
{
# 'index': [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
'name'
:
'max'
,
# 'dim': -1,
'src'
:
[[
5
,
2
],
[
2
,
5
],
[
4
,
3
],
[
1
,
3
]],
# 'fill_value': 0,
'index'
:
[
0
,
1
,
1
,
0
],
# 'expected': [[0, 0, 4, 3, 2, 0], [2, 4, 3, 0, 0, 0]],
'dim'
:
0
,
# 'expected_arg': [[-1, -1, 3, 4, 0, 1], [1, 4, 3, -1, -1, -1]],
'fill_value'
:
0
,
# }, {
'expected'
:
[[
5
,
3
],
[
4
,
5
]],
# 'name': 'max',
'expected_arg'
:
[[
0
,
3
],
[
2
,
1
]],
# 'src': [[5, 2], [2, 5], [4, 3], [1, 3]],
},
{
# 'index': [0, 1, 1, 0],
'name'
:
'min'
,
# 'dim': 0,
'src'
:
[[
2
,
0
,
1
,
4
,
3
],
[
0
,
2
,
1
,
3
,
4
]],
# 'fill_value': 0,
'index'
:
[[
4
,
5
,
4
,
2
,
3
],
[
0
,
0
,
2
,
2
,
1
]],
# 'expected': [[5, 3], [4, 5]],
'dim'
:
-
1
,
# 'expected_arg': [[0, 3], [2, 1]],
'fill_value'
:
9
,
# }, {
'expected'
:
[[
9
,
9
,
4
,
3
,
1
,
0
],
[
0
,
4
,
1
,
9
,
9
,
9
]],
# 'name': 'min',
'expected_arg'
:
[[
-
1
,
-
1
,
3
,
4
,
2
,
1
],
[
0
,
4
,
2
,
-
1
,
-
1
,
-
1
]],
# 'src': [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
},
{
# 'index': [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
'name'
:
'min'
,
# 'dim': -1,
'src'
:
[[
5
,
2
],
[
2
,
5
],
[
4
,
3
],
[
1
,
3
]],
# 'fill_value': 9,
'index'
:
[
0
,
1
,
1
,
0
],
# 'expected': [[9, 9, 4, 3, 1, 0], [0, 4, 1, 9, 9, 9]],
'dim'
:
0
,
# 'expected_arg': [[-1, -1, 3, 4, 2, 1], [0, 4, 2, -1, -1, -1]],
'fill_value'
:
9
,
# }, {
'expected'
:
[[
1
,
2
],
[
2
,
3
]],
# 'name': 'min',
'expected_arg'
:
[[
3
,
0
],
[
1
,
2
]],
# 'src': [[5, 2], [2, 5], [4, 3], [1, 3]],
}]
# 'index': [0, 1, 1, 0],
# 'dim': 0,
# 'fill_value': 9,
# 'expected': [[1, 2], [2, 3]],
# 'expected_arg': [[3, 0], [1, 2]],
}
]
@
pytest
.
mark
.
parametrize
(
'test,dtype,device'
,
product
(
tests
,
dtypes
,
devices
))
@
pytest
.
mark
.
parametrize
(
'test,dtype,device'
,
product
(
tests
,
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