1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 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 188 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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
|
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
import torch
import torch.nn.functional as F
from torch_geometric.data import Data, HeteroData
from torch_geometric.data.datapipes import functional_transform
from torch_geometric.data.storage import EdgeStorage, NodeStorage
from torch_geometric.transforms import BaseTransform
from torch_geometric.typing import EdgeType, NodeType
class Padding(ABC):
r"""An abstract class for specifying padding values."""
@abstractmethod
def get_value(
self,
store_type: Optional[Union[NodeType, EdgeType]] = None,
attr_name: Optional[str] = None,
) -> Union[int, float]:
pass
@dataclass(init=False)
class UniformPadding(Padding):
r"""Uniform padding independent of attribute name or node/edge type.
Args:
value (int or float, optional): The value to be used for padding.
(default: :obj:`0.0`)
"""
value: Union[int, float] = 0.0
def __init__(self, value: Union[int, float] = 0.0):
self.value = value
if not isinstance(self.value, (int, float)):
raise ValueError(f"Expected 'value' to be an integer or float "
f"(got '{type(value)}'")
def get_value(
self,
store_type: Optional[Union[NodeType, EdgeType]] = None,
attr_name: Optional[str] = None,
) -> Union[int, float]:
return self.value
@dataclass(init=False)
class MappingPadding(Padding):
r"""An abstract class for specifying different padding values."""
values: Dict[Any, Padding]
default: UniformPadding
def __init__(
self,
values: Dict[Any, Union[int, float, Padding]],
default: Union[int, float] = 0.0,
):
if not isinstance(values, dict):
raise ValueError(f"Expected 'values' to be a dictionary "
f"(got '{type(values)}'")
self.values = {
key: UniformPadding(val) if isinstance(val, (int, float)) else val
for key, val in values.items()
}
self.default = UniformPadding(default)
for key, value in self.values.items():
self.validate_key_value(key, value)
def validate_key_value(self, key: Any, value: Any) -> None:
pass
class AttrNamePadding(MappingPadding):
r"""Padding dependent on attribute names.
Args:
values (dict): The mapping from attribute names to padding values.
default (int or float, optional): The padding value to use for
attribute names not specified in :obj:`values`.
(default: :obj:`0.0`)
"""
def validate_key_value(self, key: Any, value: Any) -> None:
if not isinstance(key, str):
raise ValueError(f"Expected the attribute name '{key}' to be a "
f"string (got '{type(key)}')")
if not isinstance(value, UniformPadding):
raise ValueError(f"Expected the value of '{key}' to be of "
f"type 'UniformPadding' (got '{type(value)}')")
def get_value(
self,
store_type: Optional[Union[NodeType, EdgeType]] = None,
attr_name: Optional[str] = None,
) -> Union[int, float]:
padding = self.values.get(attr_name, self.default)
return padding.get_value()
class NodeTypePadding(MappingPadding):
r"""Padding dependent on node types.
Args:
values (dict): The mapping from node types to padding values.
default (int or float, optional): The padding value to use for node
types not specified in :obj:`values`. (default: :obj:`0.0`)
"""
def validate_key_value(self, key: Any, value: Any) -> None:
if not isinstance(key, str):
raise ValueError(f"Expected the node type '{key}' to be a string "
f"(got '{type(key)}')")
if not isinstance(value, (UniformPadding, AttrNamePadding)):
raise ValueError(f"Expected the value of '{key}' to be of "
f"type 'UniformPadding' or 'AttrNamePadding' "
f"(got '{type(value)}')")
def get_value(
self,
store_type: Optional[Union[NodeType, EdgeType]] = None,
attr_name: Optional[str] = None,
) -> Union[int, float]:
padding = self.values.get(store_type, self.default)
return padding.get_value(attr_name=attr_name)
class EdgeTypePadding(MappingPadding):
r"""Padding dependent on node types.
Args:
values (dict): The mapping from edge types to padding values.
default (int or float, optional): The padding value to use for edge
types not specified in :obj:`values`. (default: :obj:`0.0`)
"""
def validate_key_value(self, key: Any, value: Any) -> None:
if not isinstance(key, tuple):
raise ValueError(f"Expected the edge type '{key}' to be a tuple "
f"(got '{type(key)}')")
if len(key) != 3:
raise ValueError(f"Expected the edge type '{key}' to hold exactly "
f"three elements (got {len(key)})")
if not isinstance(value, (UniformPadding, AttrNamePadding)):
raise ValueError(f"Expected the value of '{key}' to be of "
f"type 'UniformPadding' or 'AttrNamePadding' "
f"(got '{type(value)}')")
def get_value(
self,
store_type: Optional[Union[NodeType, EdgeType]] = None,
attr_name: Optional[str] = None,
) -> Union[int, float]:
padding = self.values.get(store_type, self.default)
return padding.get_value(attr_name=attr_name)
class _NumNodes:
def __init__(
self,
value: Union[int, Dict[NodeType, int], None],
) -> None:
self.value = value
def get_value(self, key: Optional[NodeType] = None) -> Optional[int]:
if self.value is None or isinstance(self.value, int):
return self.value
assert isinstance(key, str)
return self.value[key]
class _NumEdges:
def __init__(
self,
value: Union[int, Dict[EdgeType, int], None],
num_nodes: _NumNodes,
) -> None:
if value is None:
if isinstance(num_nodes.value, int):
value = num_nodes.value * num_nodes.value
else:
value = {}
self.value = value
self.num_nodes = num_nodes
def get_value(self, key: Optional[EdgeType] = None) -> Optional[int]:
if self.value is None or isinstance(self.value, int):
return self.value
assert isinstance(key, tuple) and len(key) == 3
if key not in self.value:
num_src_nodes = self.num_nodes.get_value(key[0])
num_dst_nodes = self.num_nodes.get_value(key[-1])
assert num_src_nodes is not None and num_dst_nodes is not None
self.value[key] = num_src_nodes * num_dst_nodes
return self.value[key]
@functional_transform('pad')
class Pad(BaseTransform):
r"""Applies padding to enforce consistent tensor shapes
(functional name: :obj:`pad`).
This transform will pad node and edge features up to a maximum allowed size
in the node or edge feature dimension. By default :obj:`0.0` is used as the
padding value and can be configured by setting :obj:`node_pad_value` and
:obj:`edge_pad_value`.
In case of applying :class:`Pad` to a :class:`~torch_geometric.data.Data`
object, the :obj:`node_pad_value` value (or :obj:`edge_pad_value`) can be
either:
* an int, float or object of :class:`UniformPadding` class for cases when
all attributes are going to be padded with the same value;
* an object of :class:`AttrNamePadding` class for cases when padding is
going to differ based on attribute names.
In case of applying :class:`Pad` to a
:class:`~torch_geometric.data.HeteroData` object, the :obj:`node_pad_value`
value (or :obj:`edge_pad_value`) can be either:
* an int, float or object of :class:`UniformPadding` class for cases when
all attributes of all node (or edge) stores are going to be padded with
the same value;
* an object of :class:`AttrNamePadding` class for cases when padding is
going to differ based on attribute names (but not based on node or edge
types);
* an object of class :class:`NodeTypePadding` or :class:`EdgeTypePadding`
for cases when padding values are going to differ based on node or edge
types. Padding values can also differ based on attribute names for a
given node or edge type by using :class:`AttrNamePadding` objects as
values of its `values` argument.
Note that in order to allow for consistent padding across all graphs in a
dataset, below conditions must be met:
* if :obj:`max_num_nodes` is a single value, it must be greater than or
equal to the maximum number of nodes of any graph in the dataset;
* if :obj:`max_num_nodes` is a dictionary, value for every node type must
be greater than or equal to the maximum number of this type nodes of any
graph in the dataset.
Example below shows how to create a :class:`Pad` transform for an
:class:`~torch_geometric.data.HeteroData` object. The object is padded to
have :obj:`10` nodes of type :obj:`v0`, :obj:`20` nodes of type :obj:`v1`
and :obj:`30` nodes of type :obj:`v2`.
It is padded to have :obj:`80` edges of type :obj:`('v0', 'e0', 'v1')`.
All the attributes of the :obj:`v0` nodes are padded using a value of
:obj:`3.0`.
The :obj:`x` attribute of the :obj:`v1` node type is padded using a value
of :obj:`-1.0`, and the other attributes of this node type are padded using
a value of :obj:`0.5`.
All the attributes of node types other than :obj:`v0` and :obj:`v1` are
padded using a value of :obj:`1.0`.
All the attributes of the :obj:`('v0', 'e0', 'v1')` edge type are padded
using a value of :obj:`3.5`.
The :obj:`edge_attr` attributes of the :obj:`('v1', 'e0', 'v0')` edge type
are padded using a value of :obj:`-1.5`, and any other attributes of this
edge type are padded using a value of :obj:`5.5`.
All the attributes of edge types other than these two are padded using a
value of :obj:`1.5`.
.. code-block:: python
num_nodes = {'v0': 10, 'v1': 20, 'v2':30}
num_edges = {('v0', 'e0', 'v1'): 80}
node_padding = NodeTypePadding({
'v0': 3.0,
'v1': AttrNamePadding({'x': -1.0}, default=0.5),
}, default=1.0)
edge_padding = EdgeTypePadding({
('v0', 'e0', 'v1'): 3.5,
('v1', 'e0', 'v0'): AttrNamePadding({'edge_attr': -1.5},
default=5.5),
}, default=1.5)
transform = Pad(num_nodes, num_edges, node_padding, edge_padding)
Args:
max_num_nodes (int or dict): The number of nodes after padding.
In heterogeneous graphs, may also take in a dictionary denoting the
number of nodes for specific node types.
max_num_edges (int or dict, optional): The number of edges after
padding.
In heterogeneous graphs, may also take in a dictionary denoting the
number of edges for specific edge types. (default: :obj:`None`)
node_pad_value (int or float or Padding, optional): The fill value to
use for node features. (default: :obj:`0.0`)
edge_pad_value (int or float or Padding, optional): The fill value to
use for edge features. (default: :obj:`0.0`)
The :obj:`edge_index` tensor is padded with with the index of the
first padded node (which represents a set of self-loops on the
padded node). (default: :obj:`0.0`)
mask_pad_value (bool, optional): The fill value to use for
:obj:`train_mask`, :obj:`val_mask` and :obj:`test_mask` attributes
(default: :obj:`False`).
add_pad_mask (bool, optional): If set to :obj:`True`, will attach
node-level :obj:`pad_node_mask` and edge-level :obj:`pad_edge_mask`
attributes to the output which indicates which elements in the data
are real (represented by :obj:`True`) and which were added as a
result of padding (represented by :obj:`False`).
(default: :obj:`False`)
exclude_keys ([str], optional): Keys to be removed
from the input data object. (default: :obj:`None`)
"""
def __init__(
self,
max_num_nodes: Union[int, Dict[NodeType, int]],
max_num_edges: Optional[Union[int, Dict[EdgeType, int]]] = None,
node_pad_value: Union[int, float, Padding] = 0.0,
edge_pad_value: Union[int, float, Padding] = 0.0,
mask_pad_value: bool = False,
add_pad_mask: bool = False,
exclude_keys: Optional[List[str]] = None,
):
self.max_num_nodes = _NumNodes(max_num_nodes)
self.max_num_edges = _NumEdges(max_num_edges, self.max_num_nodes)
self.node_pad: Padding
if not isinstance(node_pad_value, Padding):
self.node_pad = UniformPadding(node_pad_value)
else:
self.node_pad = node_pad_value
self.edge_pad: Padding
if not isinstance(edge_pad_value, Padding):
self.edge_pad = UniformPadding(edge_pad_value)
else:
self.edge_pad = edge_pad_value
self.node_additional_attrs_pad = {
key: mask_pad_value
for key in ['train_mask', 'val_mask', 'test_mask']
}
self.add_pad_mask = add_pad_mask
self.exclude_keys = set(exclude_keys or [])
def __should_pad_node_attr(self, attr_name: str) -> bool:
if attr_name in self.node_additional_attrs_pad:
return True
if self.exclude_keys is None or attr_name not in self.exclude_keys:
return True
return False
def __should_pad_edge_attr(self, attr_name: str) -> bool:
if self.max_num_edges.value is None:
return False
if attr_name == 'edge_index':
return True
if self.exclude_keys is None or attr_name not in self.exclude_keys:
return True
return False
def __get_node_padding(
self,
attr_name: str,
node_type: Optional[NodeType] = None,
) -> Union[int, float]:
if attr_name in self.node_additional_attrs_pad:
return self.node_additional_attrs_pad[attr_name]
return self.node_pad.get_value(node_type, attr_name)
def __get_edge_padding(
self,
attr_name: str,
edge_type: Optional[EdgeType] = None,
) -> Union[int, float]:
return self.edge_pad.get_value(edge_type, attr_name)
def forward(
self,
data: Union[Data, HeteroData],
) -> Union[Data, HeteroData]:
if isinstance(data, Data):
assert isinstance(self.node_pad, (UniformPadding, AttrNamePadding))
assert isinstance(self.edge_pad, (UniformPadding, AttrNamePadding))
for key in self.exclude_keys:
del data[key]
num_nodes = data.num_nodes
assert num_nodes is not None
self.__pad_edge_store(data._store, data.__cat_dim__, num_nodes)
self.__pad_node_store(data._store, data.__cat_dim__)
data.num_nodes = self.max_num_nodes.get_value()
else:
assert isinstance(
self.node_pad,
(UniformPadding, AttrNamePadding, NodeTypePadding))
assert isinstance(
self.edge_pad,
(UniformPadding, AttrNamePadding, EdgeTypePadding))
for edge_type, edge_store in data.edge_items():
for key in self.exclude_keys:
del edge_store[key]
src_node_type, _, dst_node_type = edge_type
num_src_nodes = data[src_node_type].num_nodes
num_dst_nodes = data[dst_node_type].num_nodes
assert num_src_nodes is not None and num_dst_nodes is not None
self.__pad_edge_store(edge_store, data.__cat_dim__,
(num_src_nodes, num_dst_nodes),
edge_type)
for node_type, node_store in data.node_items():
for key in self.exclude_keys:
del node_store[key]
self.__pad_node_store(node_store, data.__cat_dim__, node_type)
data[node_type].num_nodes = self.max_num_nodes.get_value(
node_type)
return data
def __pad_node_store(
self,
store: NodeStorage,
get_dim_fn: Callable,
node_type: Optional[NodeType] = None,
) -> None:
attrs_to_pad = [key for key in store.keys() if store.is_node_attr(key)]
if len(attrs_to_pad) == 0:
return
num_target_nodes = self.max_num_nodes.get_value(node_type)
assert num_target_nodes is not None
assert store.num_nodes is not None
assert num_target_nodes >= store.num_nodes, \
f'The number of nodes after padding ({num_target_nodes}) cannot ' \
f'be lower than the number of nodes in the data object ' \
f'({store.num_nodes}).'
num_pad_nodes = num_target_nodes - store.num_nodes
if self.add_pad_mask:
pad_node_mask = torch.ones(num_target_nodes, dtype=torch.bool)
pad_node_mask[store.num_nodes:] = False
store.pad_node_mask = pad_node_mask
for attr_name in attrs_to_pad:
attr = store[attr_name]
pad_value = self.__get_node_padding(attr_name, node_type)
dim = get_dim_fn(attr_name, attr)
store[attr_name] = self._pad_tensor_dim(attr, dim, num_pad_nodes,
pad_value)
def __pad_edge_store(
self,
store: EdgeStorage,
get_dim_fn: Callable,
num_nodes: Union[int, Tuple[int, int]],
edge_type: Optional[EdgeType] = None,
) -> None:
attrs_to_pad = {
attr
for attr in store.keys()
if store.is_edge_attr(attr) and self.__should_pad_edge_attr(attr)
}
if not attrs_to_pad:
return
num_target_edges = self.max_num_edges.get_value(edge_type)
assert num_target_edges is not None
assert num_target_edges >= store.num_edges, \
f'The number of edges after padding ({num_target_edges}) cannot ' \
f'be lower than the number of edges in the data object ' \
f'({store.num_edges}).'
num_pad_edges = num_target_edges - store.num_edges
if self.add_pad_mask:
pad_edge_mask = torch.ones(num_target_edges, dtype=torch.bool)
pad_edge_mask[store.num_edges:] = False
store.pad_edge_mask = pad_edge_mask
if isinstance(num_nodes, tuple):
src_pad_value, dst_pad_value = num_nodes
else:
src_pad_value = dst_pad_value = num_nodes
for attr_name in attrs_to_pad:
attr = store[attr_name]
dim = get_dim_fn(attr_name, attr)
if attr_name == 'edge_index':
store[attr_name] = self._pad_edge_index(
attr, num_pad_edges, src_pad_value, dst_pad_value)
else:
pad_value = self.__get_edge_padding(attr_name, edge_type)
store[attr_name] = self._pad_tensor_dim(
attr, dim, num_pad_edges, pad_value)
@staticmethod
def _pad_tensor_dim(input: torch.Tensor, dim: int, length: int,
pad_value: float) -> torch.Tensor:
r"""Pads the input tensor in the specified dim with a constant value of
the given length.
"""
pads = [0] * (2 * input.ndim)
pads[-2 * dim - 1] = length
return F.pad(input, pads, 'constant', pad_value)
@staticmethod
def _pad_edge_index(input: torch.Tensor, length: int, src_pad_value: float,
dst_pad_value: float) -> torch.Tensor:
r"""Pads the edges :obj:`edge_index` feature with values specified
separately for src and dst nodes.
"""
pads = [0, length, 0, 0]
padded = F.pad(input, pads, 'constant', src_pad_value)
if src_pad_value != dst_pad_value:
padded[1, input.shape[1]:] = dst_pad_value
return padded
def __repr__(self) -> str:
s = f'{self.__class__.__name__}('
s += f'max_num_nodes={self.max_num_nodes.value}, '
s += f'max_num_edges={self.max_num_edges.value}, '
s += f'node_pad_value={self.node_pad}, '
s += f'edge_pad_value={self.edge_pad})'
return s
|