Debian Codegraph @

torch_cluster/radius.py

raw
1 from typing import Optional
2
3 import torch
4
5
6 def radius(
7 x: torch.Tensor,
8 y: torch.Tensor,
9 r: float,
10 batch_x: Optional[torch.Tensor] = None,
11 batch_y: Optional[torch.Tensor] = None,
12 max_num_neighbors: int = 32,
13 num_workers: int = 1,
14 batch_size: Optional[int] = None,
15 ) -> torch.Tensor:
16 r"""Finds for each element in :obj:`y` all points in :obj:`x` within
17 distance :obj:`r`.
18
19 Args:
20 x (Tensor): Node feature matrix
21 :math:`\mathbf{X} \in \mathbb{R}^{N \times F}`.
22 y (Tensor): Node feature matrix
23 :math:`\mathbf{Y} \in \mathbb{R}^{M \times F}`.
24 r (float): The radius.
25 batch_x (LongTensor, optional): Batch vector
26 :math:`\mathbf{b} \in {\{ 0, \ldots, B-1\}}^N`, which assigns each
27 node to a specific example. :obj:`batch_x` needs to be sorted.
28 (default: :obj:`None`)
29 batch_y (LongTensor, optional): Batch vector
30 :math:`\mathbf{b} \in {\{ 0, \ldots, B-1\}}^M`, which assigns each
31 node to a specific example. :obj:`batch_y` needs to be sorted.
32 (default: :obj:`None`)
33 max_num_neighbors (int, optional): The maximum number of neighbors to
34 return for each element in :obj:`y`.
35 If the number of actual neighbors is greater than
36 :obj:`max_num_neighbors`, returned neighbors are picked randomly.
37 (default: :obj:`32`)
38 num_workers (int): Number of workers to use for computation. Has no
39 effect in case :obj:`batch_x` or :obj:`batch_y` is not
40 :obj:`None`, or the input lies on the GPU. (default: :obj:`1`)
41 batch_size (int, optional): The number of examples :math:`B`.
42 Automatically calculated if not given. (default: :obj:`None`)
43
44 .. code-block:: python
45
46 import torch
47 from torch_cluster import radius
48
49 x = torch.Tensor([[-1, -1], [-1, 1], [1, -1], [1, 1]])
50 batch_x = torch.tensor([0, 0, 0, 0])
51 y = torch.Tensor([[-1, 0], [1, 0]])
52 batch_y = torch.tensor([0, 0])
53 assign_index = radius(x, y, 1.5, batch_x, batch_y)
54 """
55 if x.numel() == 0 or y.numel() == 0:
56 return torch.empty(2, 0, dtype=torch.long, device=x.device)
57
58 x = x.view(-1, 1) if x.dim() == 1 else x
59 y = y.view(-1, 1) if y.dim() == 1 else y
60 x, y = x.contiguous(), y.contiguous()
61
62 if batch_size is None:
63 batch_size = 1
64 if batch_x is not None:
65 assert x.size(0) == batch_x.numel()
66 batch_size = int(batch_x.max()) + 1
67 if batch_y is not None:
68 assert y.size(0) == batch_y.numel()
69 batch_size = max(batch_size, int(batch_y.max()) + 1)
70 assert batch_size > 0
71
72 ptr_x: Optional[torch.Tensor] = None
73 ptr_y: Optional[torch.Tensor] = None
74
75 if batch_size > 1:
76 assert batch_x is not None
77 assert batch_y is not None
78 arange = torch.arange(batch_size + 1, device=x.device)
79 ptr_x = torch.bucketize(arange, batch_x)
80 ptr_y = torch.bucketize(arange, batch_y)
81
82 return torch.ops.torch_cluster.radius(x, y, ptr_x, ptr_y, r,
83 max_num_neighbors, num_workers)
84
85
86 def radius_graph(
87 x: torch.Tensor,
88 r: float,
89 batch: Optional[torch.Tensor] = None,
90 loop: bool = False,
91 max_num_neighbors: int = 32,
92 flow: str = 'source_to_target',
93 num_workers: int = 1,
94 batch_size: Optional[int] = None,
95 ) -> torch.Tensor:
96 r"""Computes graph edges to all points within a given distance.
97
98 Args:
99 x (Tensor): Node feature matrix
100 :math:`\mathbf{X} \in \mathbb{R}^{N \times F}`.
101 r (float): The radius.
102 batch (LongTensor, optional): Batch vector
103 :math:`\mathbf{b} \in {\{ 0, \ldots, B-1\}}^N`, which assigns each
104 node to a specific example. :obj:`batch` needs to be sorted.
105 (default: :obj:`None`)
106 loop (bool, optional): If :obj:`True`, the graph will contain
107 self-loops. (default: :obj:`False`)
108 max_num_neighbors (int, optional): The maximum number of neighbors to
109 return for each element.
110 If the number of actual neighbors is greater than
111 :obj:`max_num_neighbors`, returned neighbors are picked randomly.
112 (default: :obj:`32`)
113 flow (string, optional): The flow direction when used in combination
114 with message passing (:obj:`"source_to_target"` or
115 :obj:`"target_to_source"`). (default: :obj:`"source_to_target"`)
116 num_workers (int): Number of workers to use for computation. Has no
117 effect in case :obj:`batch` is not :obj:`None`, or the input lies
118 on the GPU. (default: :obj:`1`)
119 batch_size (int, optional): The number of examples :math:`B`.
120 Automatically calculated if not given. (default: :obj:`None`)
121
122 :rtype: :class:`LongTensor`
123
124 .. code-block:: python
125
126 import torch
127 from torch_cluster import radius_graph
128
129 x = torch.Tensor([[-1, -1], [-1, 1], [1, -1], [1, 1]])
130 batch = torch.tensor([0, 0, 0, 0])
131 edge_index = radius_graph(x, r=1.5, batch=batch, loop=False)
132 """
133
134 assert flow in ['source_to_target', 'target_to_source']
135 edge_index = radius(x, x, r, batch, batch,
136 max_num_neighbors if loop else max_num_neighbors + 1,
137 num_workers, batch_size)
138 if flow == 'source_to_target':
139 row, col = edge_index[1], edge_index[0]
140 else:
141 row, col = edge_index[0], edge_index[1]
142
143 if not loop:
144 mask = row != col
145 row, col = row[mask], col[mask]
146
147 return torch.stack([row, col], dim=0)