scatter.py 6.03 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
import warnings
rusty1s's avatar
rusty1s committed
2
3
4
5
6
import os.path as osp
from typing import Optional, Tuple

import torch

7
8
from .utils import broadcast

rusty1s's avatar
rusty1s committed
9
10
11
12
13
14
try:
    torch.ops.load_library(
        osp.join(osp.dirname(osp.abspath(__file__)), '_scatter.so'))
except OSError:
    warnings.warn('Failed to load `scatter` binaries.')

rusty1s's avatar
rusty1s committed
15
16
17
    def scatter_placeholder(src: torch.Tensor, index: torch.Tensor, dim: int,
                            out: Optional[torch.Tensor],
                            dim_size: Optional[int]) -> torch.Tensor:
rusty1s's avatar
rusty1s committed
18
        raise ImportError
rusty1s's avatar
rusty1s committed
19
        return src
rusty1s's avatar
rusty1s committed
20

rusty1s's avatar
rusty1s committed
21
22
23
24
    def scatter_with_arg_placeholder(src: torch.Tensor, index: torch.Tensor,
                                     dim: int, out: Optional[torch.Tensor],
                                     dim_size: Optional[int]
                                     ) -> Tuple[torch.Tensor, torch.Tensor]:
rusty1s's avatar
rusty1s committed
25
        raise ImportError
rusty1s's avatar
rusty1s committed
26
        return src, index
rusty1s's avatar
rusty1s committed
27

rusty1s's avatar
rusty1s committed
28
29
30
    torch.ops.torch_scatter.scatter_mean = scatter_placeholder
    torch.ops.torch_scatter.scatter_min = scatter_with_arg_placeholder
    torch.ops.torch_scatter.scatter_max = scatter_with_arg_placeholder
rusty1s's avatar
rusty1s committed
31
32
33
34
35
36


@torch.jit.script
def scatter_sum(src: torch.Tensor, index: torch.Tensor, dim: int = -1,
                out: Optional[torch.Tensor] = None,
                dim_size: Optional[int] = None) -> torch.Tensor:
37
38
39
40
41
42
43
44
45
46
47
    index = broadcast(index, src, dim)
    if out is None:
        size = src.size()
        if dim_size is None:
            size[dim] = int(index.max()) + 1
        else:
            size[dim] = dim_size
        out = src.new_zeros(size)
        return out.scatter_add_(dim, index, src)
    else:
        return out.scatter_add_(dim, index, src)
rusty1s's avatar
rusty1s committed
48
49
50
51
52
53


@torch.jit.script
def scatter_add(src: torch.Tensor, index: torch.Tensor, dim: int = -1,
                out: Optional[torch.Tensor] = None,
                dim_size: Optional[int] = None) -> torch.Tensor:
54
    return scatter_sum(src, index, dim, out, dim_size)
rusty1s's avatar
rusty1s committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82


@torch.jit.script
def scatter_mean(src: torch.Tensor, index: torch.Tensor, dim: int = -1,
                 out: Optional[torch.Tensor] = None,
                 dim_size: Optional[int] = None) -> torch.Tensor:
    return torch.ops.torch_scatter.scatter_mean(src, index, dim, out, dim_size)


@torch.jit.script
def scatter_min(src: torch.Tensor, index: torch.Tensor, dim: int = -1,
                out: Optional[torch.Tensor] = None,
                dim_size: Optional[int] = None
                ) -> Tuple[torch.Tensor, torch.Tensor]:
    return torch.ops.torch_scatter.scatter_min(src, index, dim, out, dim_size)


@torch.jit.script
def scatter_max(src: torch.Tensor, index: torch.Tensor, dim: int = -1,
                out: Optional[torch.Tensor] = None,
                dim_size: Optional[int] = None
                ) -> Tuple[torch.Tensor, torch.Tensor]:
    return torch.ops.torch_scatter.scatter_max(src, index, dim, out, dim_size)


def scatter(src: torch.Tensor, index: torch.Tensor, dim: int = -1,
            out: Optional[torch.Tensor] = None, dim_size: Optional[int] = None,
            reduce: str = "sum") -> torch.Tensor:
rusty1s's avatar
rusty1s committed
83
84
85
86
87
88
89
90
91
92
    r"""
    |

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

    |

rusty1s's avatar
rusty1s committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
    Reduces all values from the :attr:`src` tensor into :attr:`out` at the
    indices specified in the :attr:`index` tensor along a given axis
    :attr:`dim`.
    For each value in :attr:`src`, its output index is specified by its index
    in :attr:`src` for dimensions outside of :attr:`dim` and by the
    corresponding value in :attr:`index` for dimension :attr:`dim`.
    The applied reduction is defined via the :attr:`reduce` argument.

    Formally, if :attr:`src` and :attr:`index` are :math:`n`-dimensional
    tensors with size :math:`(x_0, ..., x_{i-1}, x_i, x_{i+1}, ..., x_{n-1})`
    and :attr:`dim` = `i`, then :attr:`out` must be an :math:`n`-dimensional
    tensor with size :math:`(x_0, ..., x_{i-1}, y, x_{i+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`.

    For one-dimensional tensors with :obj:`reduce="sum"`, the operation
    computes
rusty1s's avatar
rusty1s committed
112
113

    .. math::
rusty1s's avatar
rusty1s committed
114
        \mathrm{out}_i = \mathrm{out}_i + \sum_j~\mathrm{src}_j
rusty1s's avatar
rusty1s committed
115
116
117
118

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

rusty1s's avatar
rusty1s committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
    .. 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.

    :param src: The source tensor.
    :param index: The indices of elements to scatter.
    :param dim: The axis along which to index. (default: :obj:`-1`)
    :param out: The destination tensor.
    :param dim_size: If :attr:`out` is not given, automatically create output
        with size :attr:`dim_size` at dimension :attr:`dim`.
        If :attr:`dim_size` is not given, a minimal sized output tensor
        according to :obj:`index.max() + 1` is returned.
    :param reduce: The reduce operation (:obj:`"sum"`, :obj:`"mean"`,
        :obj:`"min"` or :obj:`"max"`). (default: :obj:`"sum"`)
rusty1s's avatar
rusty1s committed
137
138
139

    :rtype: :class:`Tensor`

rusty1s's avatar
rusty1s committed
140
    .. code-block:: python
rusty1s's avatar
rusty1s committed
141

rusty1s's avatar
rusty1s committed
142
        from torch_scatter import scatter
rusty1s's avatar
rusty1s committed
143

rusty1s's avatar
rusty1s committed
144
145
        src = torch.randn(10, 6, 64)
        index = torch.tensor([0, 1, 0, 1, 2, 1])
rusty1s's avatar
rusty1s committed
146

rusty1s's avatar
rusty1s committed
147
148
        # Broadcasting in the first and last dim.
        out = scatter(src, index, dim=1, reduce="sum")
rusty1s's avatar
rusty1s committed
149

rusty1s's avatar
rusty1s committed
150
        print(out.size())
rusty1s's avatar
rusty1s committed
151

rusty1s's avatar
rusty1s committed
152
    .. code-block::
rusty1s's avatar
rusty1s committed
153

rusty1s's avatar
rusty1s committed
154
        torch.Size([10, 3, 64])
rusty1s's avatar
rusty1s committed
155
    """
rusty1s's avatar
rusty1s committed
156
157
158
159
160
161
162
163
164
165
    if reduce == 'sum' or reduce == 'add':
        return scatter_sum(src, index, dim, out, dim_size)
    elif reduce == 'mean':
        return scatter_mean(src, index, dim, out, dim_size)
    elif reduce == 'min':
        return scatter_min(src, index, dim, out, dim_size)[0]
    elif reduce == 'max':
        return scatter_max(src, index, dim, out, dim_size)[0]
    else:
        raise ValueError