Skip to content

Commit 4b654c2

Browse files
committed
scatter mul
1 parent 0b71aad commit 4b654c2

File tree

5 files changed

+109
-5
lines changed

5 files changed

+109
-5
lines changed

test/test_backward.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from .utils import devices
99

10-
funcs = ['add', 'sub', 'mean']
10+
funcs = ['add', 'sub', 'mul', 'mean']
1111
indices = [2, 0, 1, 1, 0]
1212

1313

test/test_forward.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,25 @@
3434
'dim': 0,
3535
'fill_value': 9,
3636
'expected': [[3, 4], [3, 5]]
37+
}, {
38+
'name': 'mul',
39+
'src': [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
40+
'index': [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
41+
'dim': -1,
42+
'fill_value': 1,
43+
'expected': [[1, 1, 4, 3, 2, 0], [0, 4, 3, 1, 1, 1]]
44+
}, {
45+
'name': 'mul',
46+
'src': [[5, 2], [2, 5], [4, 3], [1, 3]],
47+
'index': [[0, 0], [1, 1], [1, 1], [0, 0]],
48+
'dim': 0,
49+
'fill_value': 1,
50+
'expected': [[5, 6], [8, 15]]
3751
}, {
3852
'name': 'mean',
3953
'src': [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
4054
'index': [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
41-
'dim': 1,
55+
'dim': -1,
4256
'fill_value': 0,
4357
'expected': [[0, 0, 4, 3, 1.5, 0], [1, 4, 2, 0, 0, 0]]
4458
}, {

torch_scatter/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from .add import scatter_add
22
from .sub import scatter_sub
3+
from .mul import scatter_mul
34
from .mean import scatter_mean
45

56
__version__ = '1.0.0'
67

7-
__all__ = ['scatter_add', 'scatter_sub', 'scatter_mean', '__version__']
8+
__all__ = [
9+
'scatter_add', 'scatter_sub', 'scatter_mul', 'scatter_mean', '__version__'
10+
]

torch_scatter/mean.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
class ScatterMean(Function):
88
@staticmethod
99
def forward(ctx, out, src, index, dim):
10-
ctx.mark_dirty(out)
11-
1210
count = src.new_zeros(out.size())
1311
func = get_func('scatter_mean', src)
1412
func(dim, out, index, src, count)
1513
count[count == 0] = 1
1614
out /= count
1715

16+
ctx.mark_dirty(out)
1817
ctx.save_for_backward(index, count)
1918

2019
return out

torch_scatter/mul.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
from torch.autograd import Function
2+
3+
from .utils.ffi import get_func
4+
from .utils.gen import gen
5+
6+
7+
class ScatterMul(Function):
8+
@staticmethod
9+
def forward(ctx, out, src, index, dim):
10+
func = get_func('scatter_mul', src)
11+
func(dim, out, index, src)
12+
13+
ctx.dim = dim
14+
ctx.mark_dirty(out)
15+
ctx.save_for_backward(out, src, index)
16+
17+
return out
18+
19+
@staticmethod
20+
def backward(ctx, grad_out):
21+
out, src, index = ctx.saved_variables
22+
23+
grad_src = None
24+
if ctx.needs_input_grad[1]:
25+
grad_src = (grad_out * out)[index] / src
26+
27+
return None, grad_src, None, None
28+
29+
30+
def scatter_mul(src, index, dim=-1, out=None, dim_size=None, fill_value=1):
31+
r"""
32+
|
33+
34+
.. image:: https://raw.githubusercontent.com/rusty1s/pytorch_scatter/
35+
master/docs/source/_figures/mul.svg?sanitize=true
36+
:align: center
37+
:width: 400px
38+
39+
|
40+
41+
Multiplies all values from the :attr:`src` tensor into :attr:`out` at the
42+
indices specified in the :attr:`index` tensor along an given axis
43+
:attr:`dim`.If multiple indices reference the same location, their
44+
**contributions multiply** (`cf.` :meth:`~torch_scatter.scatter_add`).
45+
46+
For one-dimensional tensors, the operation computes
47+
48+
.. math::
49+
\mathrm{out}_i = \mathrm{out}_i \cdot \prod_j \mathrm{src}_j
50+
51+
where sum is over :math:`j` such that :math:`\mathrm{index}_j = i`.
52+
53+
Args:
54+
src (Tensor): The source tensor.
55+
index (LongTensor): The indices of elements to scatter.
56+
dim (int, optional): The axis along which to index.
57+
(default: :obj:`-1`)
58+
out (Tensor, optional): The destination tensor. (default: :obj:`None`)
59+
dim_size (int, optional): If :attr:`out` is not given, automatically
60+
create output with size :attr:`dim_size` at dimension :attr:`dim`.
61+
If :attr:`dim_size` is not given, a minimal sized output tensor is
62+
returned. (default: :obj:`None`)
63+
fill_value (int, optional): If :attr:`out` is not given, automatically
64+
fill output tensor with :attr:`fill_value`. (default: :obj:`0`)
65+
66+
:rtype: :class:`Tensor`
67+
68+
.. testsetup::
69+
70+
import torch
71+
72+
.. testcode::
73+
74+
from torch_scatter import scatter_mean
75+
src = torch.tensor([[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]])
76+
index = torch.tensor([[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]])
77+
out = src.new_zeros((2, 6))
78+
out = scatter_mean(src, index, out=out)
79+
print(out)
80+
81+
.. testoutput::
82+
83+
0.0000 0.0000 4.0000 3.0000 1.5000 0.0000
84+
1.0000 4.0000 2.0000 0.0000 0.0000 0.0000
85+
[torch.FloatTensor of size 2x6]
86+
"""
87+
src, out, index, dim = gen(src, index, dim, out, dim_size, fill_value)
88+
return ScatterMul.apply(out, src, index, dim)

0 commit comments

Comments
 (0)