segment.py 12.6 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
import torch
rusty1s's avatar
rusty1s committed
2
from torch_scatter.helpers import min_value, max_value
rusty1s's avatar
rusty1s committed
3

rusty1s's avatar
rusty1s committed
4

rusty1s's avatar
rusty1s committed
5
6
7
class SegmentCOO(torch.autograd.Function):
    @staticmethod
    def forward(ctx, src, index, out, dim_size, reduce):
rusty1s's avatar
rusty1s committed
8
        assert reduce in ['sum', 'add', 'mean', 'min', 'max']
rusty1s's avatar
rusty1s committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
        if out is not None:
            ctx.mark_dirty(out)
        ctx.reduce = reduce
        ctx.src_size = list(src.size())

        fill_value = 0
        if out is None:
            dim_size = index.max().item() + 1 if dim_size is None else dim_size
            size = list(src.size())
            size[index.dim() - 1] = dim_size

            if reduce == 'min':
                fill_value = max_value(src.dtype)
            elif reduce == 'max':
                fill_value = min_value(src.dtype)

            out = src.new_full(size, fill_value)

rusty1s's avatar
rusty1s committed
27
28
29
30
31
32
        if src.is_cuda:
            out, arg_out = torch.ops.torch_scatter_cuda.segment_coo(
                src, index, out, reduce)
        else:
            out, arg_out = torch.ops.torch_scatter_cpu.segment_coo(
                src, index, out, reduce)
rusty1s's avatar
rusty1s committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

        if fill_value != 0:
            out.masked_fill_(out == fill_value, 0)

        ctx.save_for_backward(index, arg_out)

        if reduce == 'min' or reduce == 'max':
            return out, arg_out
        else:
            return out

    @staticmethod
    def backward(ctx, grad_out, *args):
        (index, arg_out), src_size = ctx.saved_tensors, ctx.src_size

        grad_src = None
        if ctx.needs_input_grad[0]:
rusty1s's avatar
rusty1s committed
50
            if ctx.reduce == 'sum' or ctx.reduce == 'add':
rusty1s's avatar
rusty1s committed
51
52
53
54
55
56
57
                if grad_out.is_cuda:
                    grad_src = torch.ops.torch_scatter_cuda.gather_coo(
                        grad_out, index, grad_out.new_empty(src_size))
                else:
                    grad_src = torch.ops.torch_scatter_cpu.gather_coo(
                        grad_out, index, grad_out.new_empty(src_size))

rusty1s's avatar
rusty1s committed
58
            elif ctx.reduce == 'mean':
rusty1s's avatar
rusty1s committed
59
60
61
62
63
64
                if grad_out.is_cuda:
                    grad_src = torch.ops.torch_scatter_cuda.gather_coo(
                        grad_out, index, grad_out.new_empty(src_size))
                else:
                    grad_src = torch.ops.torch_scatter_cpu.gather_coo(
                        grad_out, index, grad_out.new_empty(src_size))
rusty1s's avatar
rusty1s committed
65
66
67
68
69

                count = arg_out  # Gets pre-computed on GPU but not on CPU.
                if count is None:
                    size = list(index.size())
                    size[-1] = grad_out.size(index.dim() - 1)
rusty1s's avatar
rusty1s committed
70
                    count = torch.ops.torch_scatter_cpu.segment_coo(
rusty1s's avatar
rusty1s committed
71
                        torch.ones_like(index, dtype=grad_out.dtype), index,
rusty1s's avatar
rusty1s committed
72
                        grad_out.new_zeros(size), 'sum')[0].clamp_(min=1)
rusty1s's avatar
rusty1s committed
73

rusty1s's avatar
rusty1s committed
74
75
76
77
78
79
                if grad_out.is_cuda:
                    count = torch.ops.torch_scatter_cuda.gather_coo(
                        count, index, count.new_empty(src_size[:index.dim()]))
                else:
                    count = torch.ops.torch_scatter_cpu.gather_coo(
                        count, index, count.new_empty(src_size[:index.dim()]))
rusty1s's avatar
rusty1s committed
80
81
                for _ in range(grad_out.dim() - index.dim()):
                    count = count.unsqueeze(-1)
rusty1s's avatar
rusty1s committed
82
                grad_src.div_(count)
rusty1s's avatar
rusty1s committed
83

rusty1s's avatar
rusty1s committed
84
85
86
87
88
89
            elif ctx.reduce == 'min' or ctx.reduce == 'max':
                src_size[index.dim() - 1] += 1
                grad_src = grad_out.new_zeros(src_size).scatter_(
                    index.dim() - 1, arg_out, grad_out)
                grad_src = grad_src.narrow(index.dim() - 1, 0,
                                           src_size[index.dim() - 1] - 1)
rusty1s's avatar
rusty1s committed
90

rusty1s's avatar
rusty1s committed
91
        return grad_src, None, None, None, None
rusty1s's avatar
rusty1s committed
92
93


rusty1s's avatar
rusty1s committed
94
95
96
class SegmentCSR(torch.autograd.Function):
    @staticmethod
    def forward(ctx, src, indptr, out, reduce):
rusty1s's avatar
rusty1s committed
97
        assert reduce in ['sum', 'add', 'mean', 'min', 'max']
rusty1s's avatar
rusty1s committed
98
99
100
101

        if out is not None:
            ctx.mark_dirty(out)
        ctx.reduce = reduce
rusty1s's avatar
rusty1s committed
102
        ctx.src_size = list(src.size())
rusty1s's avatar
rusty1s committed
103

rusty1s's avatar
rusty1s committed
104
105
106
107
108
109
110
        if src.is_cuda:
            out, arg_out = torch.ops.torch_scatter_cuda.segment_csr(
                src, indptr, out, reduce)
        else:
            out, arg_out = torch.ops.torch_scatter_cpu.segment_csr(
                src, indptr, out, reduce)

rusty1s's avatar
rusty1s committed
111
        ctx.save_for_backward(indptr, arg_out)
rusty1s's avatar
rusty1s committed
112
113
114
115
        return out if arg_out is None else (out, arg_out)

    @staticmethod
    def backward(ctx, grad_out, *args):
rusty1s's avatar
rusty1s committed
116
        (indptr, arg_out), src_size = ctx.saved_tensors, ctx.src_size
rusty1s's avatar
rusty1s committed
117
118
119

        grad_src = None
        if ctx.needs_input_grad[0]:
rusty1s's avatar
rusty1s committed
120
            if ctx.reduce == 'sum' or ctx.reduce == 'add':
rusty1s's avatar
rusty1s committed
121
122
123
124
125
126
127
                if grad_out.is_cuda:
                    grad_src = torch.ops.torch_scatter_cuda.gather_csr(
                        grad_out, indptr, grad_out.new_empty(src_size))
                else:
                    grad_src = torch.ops.torch_scatter_cpu.gather_csr(
                        grad_out, indptr, grad_out.new_empty(src_size))

rusty1s's avatar
rusty1s committed
128
            elif ctx.reduce == 'mean':
rusty1s's avatar
rusty1s committed
129
130
131
132
133
134
                if grad_out.is_cuda:
                    grad_src = torch.ops.torch_scatter_cuda.gather_csr(
                        grad_out, indptr, grad_out.new_empty(src_size))
                else:
                    grad_src = torch.ops.torch_scatter_cpu.gather_csr(
                        grad_out, indptr, grad_out.new_empty(src_size))
rusty1s's avatar
rusty1s committed
135
136
137
                indptr1 = indptr.narrow(-1, 0, indptr.size(-1) - 1)
                indptr2 = indptr.narrow(-1, 1, indptr.size(-1) - 1)
                count = (indptr2 - indptr1).to(grad_src.dtype)
rusty1s's avatar
rusty1s committed
138
139
140
141
142
143
144
145
                if grad_out.is_cuda:
                    count = torch.ops.torch_scatter_cuda.gather_csr(
                        count, indptr,
                        count.new_empty(src_size[:indptr.dim()]))
                else:
                    count = torch.ops.torch_scatter_cpu.gather_csr(
                        count, indptr,
                        count.new_empty(src_size[:indptr.dim()]))
rusty1s's avatar
rusty1s committed
146
147
                for _ in range(grad_out.dim() - indptr.dim()):
                    count = count.unsqueeze(-1)
rusty1s's avatar
rusty1s committed
148
149
150
151
152
153
154
                grad_src.div_(count)
            elif ctx.reduce == 'min' or ctx.reduce == 'max':
                src_size[indptr.dim() - 1] += 1
                grad_src = grad_out.new_zeros(src_size).scatter_(
                    indptr.dim() - 1, arg_out, grad_out)
                grad_src = grad_src.narrow(indptr.dim() - 1, 0,
                                           src_size[indptr.dim() - 1] - 1)
rusty1s's avatar
rusty1s committed
155
156
157
158

        return grad_src, None, None, None


rusty1s's avatar
rusty1s committed
159
def segment_coo(src, index, out=None, dim_size=None, reduce="sum"):
rusty1s's avatar
rusty1s committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    r"""
    |

    .. image:: https://raw.githubusercontent.com/rusty1s/pytorch_scatter/
            master/docs/source/_figures/segment_coo.svg?sanitize=true
        :align: center
        :width: 400px

    |

    Reduces all values from the :attr:`src` tensor into :attr:`out` at the
    indices specified in the :attr:`index` tensor along the last dimension of
    :attr:`index`.
    For each value in :attr:`src`, its output index is specified by its index
    in :attr:`src` for dimensions outside of :obj:`index.dim() - 1` and by the
    corresponding value in :attr:`index` for dimension :obj:`index.dim() - 1`.
    The applied reduction is defined via the :attr:`reduce` argument.

    Formally, if :attr:`src` and :attr:`index` are :math:`n`-dimensional and
    :math:`m`-dimensional tensors with
    size :math:`(x_0, ..., x_{m-1}, x_m, x_{m+1}, ..., x_{n-1})` and
    :math:`(x_0, ..., x_{m-1}, x_m)`, respectively, then :attr:`out` must be an
    :math:`n`-dimensional tensor with size
    :math:`(x_0, ..., x_{m-1}, y, x_{m+1}, ..., x_{n-1})`.
    Moreover, the values of :attr:`index` must be between :math:`0` and
    :math:`y - 1` in ascending order.
    The :attr:`index` tensor supports broadcasting in case its dimensions do
    not match with :attr:`src`.
rusty1s's avatar
rusty1s committed
188
    For one-dimensional tensors with :obj:`reduce="sum"`, the operation
rusty1s's avatar
rusty1s committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
    computes

    .. math::
        \mathrm{out}_i = \mathrm{out}_i + \sum_j~\mathrm{src}_j

    where :math:`\sum_j` is over :math:`j` such that
    :math:`\mathrm{index}_j = i`.

    In contrast to :meth:`scatter`, this method expects values in :attr:`index`
    **to be sorted** along dimension :obj:`index.dim() - 1`.
    Due to the use of sorted indices, :meth:`segment_coo` is usually faster
    than the more general :meth:`scatter` operation.

    For reductions :obj:`"min"` and :obj:`"max"`, this operation returns a
    second tensor representing the :obj:`argmin` and :obj:`argmax`,
    respectively.

    .. note::

        This operation is implemented via atomic operations on the GPU and is
        therefore **non-deterministic** since the order of parallel operations
        to the same value is undetermined.
        For floating-point variables, this results in a source of variance in
        the result.

    Args:
        src (Tensor): The source tensor.
        index (LongTensor): The sorted indices of elements to segment.
            The number of dimensions of :attr:`index` needs to be less than or
            equal to :attr:`src`.
        out (Tensor, optional): The destination tensor. (default: :obj:`None`)
        dim_size (int, optional): If :attr:`out` is not given, automatically
            create output with size :attr:`dim_size` at dimension
            :obj:`index.dim() - 1`.
            If :attr:`dim_size` is not given, a minimal sized output tensor
            according to :obj:`index.max() + 1` is returned.
            (default: :obj:`None`)
rusty1s's avatar
rusty1s committed
226
        reduce (string, optional): The reduce operation (:obj:`"sum"`,
rusty1s's avatar
rusty1s committed
227
            :obj:`"mean"`, :obj:`"min"` or :obj:`"max"`).
rusty1s's avatar
rusty1s committed
228
            (default: :obj:`"sum"`)
rusty1s's avatar
rusty1s committed
229
230
231
232
233
234
235
236
237
238
239

    :rtype: :class:`Tensor`, :class:`LongTensor` *(optional)*

    .. code-block:: python

        from torch_scatter import segment_coo

        src = torch.randn(10, 6, 64)
        index = torch.tensor([0, 0, 1, 1, 1, 2])
        index = index.view(1, -1)  # Broadcasting in the first and last dim.

rusty1s's avatar
rusty1s committed
240
        out = segment_coo(src, index, reduce="sum")
rusty1s's avatar
rusty1s committed
241
242
243
244
245
246
247

        print(out.size())

    .. code-block::

        torch.Size([10, 3, 64])
    """
rusty1s's avatar
rusty1s committed
248
    return SegmentCOO.apply(src, index, out, dim_size, reduce)
rusty1s's avatar
rusty1s committed
249
250


rusty1s's avatar
rusty1s committed
251
def segment_csr(src, indptr, out=None, reduce="sum"):
rusty1s's avatar
rusty1s committed
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
    r"""
    Reduces all values from the :attr:`src` tensor into :attr:`out` within the
    ranges specified in the :attr:`indptr` tensor along the last dimension of
    :attr:`indptr`.
    For each value in :attr:`src`, its output index is specified by its index
    in :attr:`src` for dimensions outside of :obj:`indptr.dim() - 1` and by the
    corresponding range index in :attr:`indptr` for dimension
    :obj:`indptr.dim() - 1`.
    The applied reduction is defined via the :attr:`reduce` argument.

    Formally, if :attr:`src` and :attr:`indptr` are :math:`n`-dimensional and
    :math:`m`-dimensional tensors with
    size :math:`(x_0, ..., x_{m-1}, x_m, x_{m+1}, ..., x_{n-1})` and
    :math:`(x_0, ..., x_{m-1}, y)`, respectively, then :attr:`out` must be an
    :math:`n`-dimensional tensor with size
    :math:`(x_0, ..., x_{m-1}, y - 1, x_{m+1}, ..., x_{n-1})`.
    Moreover, the values of :attr:`indptr` must be between :math:`0` and
    :math:`x_m` in ascending order.
    The :attr:`indptr` tensor supports broadcasting in case its dimensions do
    not match with :attr:`src`.
rusty1s's avatar
rusty1s committed
272
    For one-dimensional tensors with :obj:`reduce="sum"`, the operation
rusty1s's avatar
rusty1s committed
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
    computes

    .. math::
        \mathrm{out}_i =
        \sum_{j = \mathrm{indptr}[i]}^{\mathrm{indptr}[i+i]}~\mathrm{src}_j.

    Due to the use of index pointers, :meth:`segment_csr` is the fastest
    method to apply for grouped reductions.

    For reductions :obj:`"min"` and :obj:`"max"`, this operation returns a
    second tensor representing the :obj:`argmin` and :obj:`argmax`,
    respectively.

    .. note::

        In contrast to :meth:`scatter()` and :meth:`segment_coo`, this
        operation is **fully-deterministic**.

    Args:
        src (Tensor): The source tensor.
        indptr (LongTensor): The index pointers between elements to segment.
            The number of dimensions of :attr:`index` needs to be less than or
            equal to :attr:`src`.
        out (Tensor, optional): The destination tensor. (default: :obj:`None`)
rusty1s's avatar
rusty1s committed
297
        reduce (string, optional): The reduce operation (:obj:`"sum"`,
rusty1s's avatar
rusty1s committed
298
            :obj:`"mean"`, :obj:`"min"` or :obj:`"max"`).
rusty1s's avatar
rusty1s committed
299
            (default: :obj:`"sum"`)
rusty1s's avatar
rusty1s committed
300
301
302
303
304
305
306
307
308
309
310

    :rtype: :class:`Tensor`, :class:`LongTensor` *(optional)*

    .. code-block:: python

        from torch_scatter import segment_csr

        src = torch.randn(10, 6, 64)
        indptr = torch.tensor([0, 2, 5, 6])
        indptr = indptr.view(1, -1)  # Broadcasting in the first and last dim.

rusty1s's avatar
rusty1s committed
311
        out = segment_csr(src, indptr, reduce="sum")
rusty1s's avatar
rusty1s committed
312
313
314
315
316
317
318

        print(out.size())

    .. code-block::

        torch.Size([10, 3, 64])
    """
rusty1s's avatar
rusty1s committed
319
    return SegmentCSR.apply(src, indptr, out, reduce)