DenseLayout
¶
DenseLayout(
index: Optional[array],
num_elements: int,
dim_size: Optional[int] = None,
max_num_elements: Optional[int] = None,
)
Host-side layout of a set of elements grouped by index into a dense
[num_sets, max_set_size, ...] array, as PyG's to_dense_batch.
Elements keep their input order within their set and index does not
need to be sorted. Unlike to_dense_batch(), the
layout can be reused to go back from the dense array to the elements
(from_dense()), with gathers only, so both directions are
differentiable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
Optional[array]
|
Set of each element, of shape |
required |
num_elements
|
int
|
Number of elements (needed when |
required |
dim_size
|
Optional[int]
|
Number of sets. Default: |
None
|
max_num_elements
|
Optional[int]
|
Maximum number of elements per set. Elements beyond it are dropped. Default: the size of the largest set. |
None
|
Methods:
| Name | Description |
|---|---|
from_sizes |
Builds the layout on the device, without reading |
to_dense |
Converts |
from_dense |
Gathers the rows of the (kept) elements of a dense array of shape |
Source code in mlx_graphs/nn/aggr/utils.py
from_sizes
classmethod
¶
from_sizes(
index: Optional[array],
num_elements: int,
num_sets: int,
max_num_elements: int,
) -> DenseLayout
Builds the layout on the device, without reading index on the
host: no GPU synchronization, and usable inside mx.compile().
The number of sets and the size of the largest set are not computed
from index but given, e.g. the number of graphs of a batch and an
upper bound of their number of nodes (max_num_elements is the
padded size of the dense array, so a tight bound saves memory).
Elements of sets larger than max_num_elements (beyond the first
max_num_elements of their set), or with an index outside
[0, num_sets), are dropped: to_dense() leaves them out and
from_dense() returns zeros for them.
With max_num_elements equal to the size of the largest set, the
layout is the one of DenseLayout(index, num_elements). A larger
bound only adds padding, which masked attentions (e.g.
GPSConv with attn_type="multihead")
ignore; the linear attentions of SGFormer,
Polynormer and the performer attention count
the padding rows in their normalizers (as in PyG, where the padding is
set by the largest graph of the batch), so their outputs then change
slightly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
Optional[array]
|
Set of each element, of shape |
required |
num_elements
|
int
|
Number of elements. |
required |
num_sets
|
int
|
Number of sets. |
required |
max_num_elements
|
int
|
Size of the sets in the dense array. |
required |
Example:
batch_indices = mx.array([0, 0, 1, 1, 1])
layout = DenseLayout.from_sizes(
batch_indices, 5, num_sets=2, max_num_elements=3
)
>>> layout.mask
array([[True, True, False],
[True, True, True]], dtype=bool)
Source code in mlx_graphs/nn/aggr/utils.py
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 | |
to_dense
¶
to_dense(x: array, fill_value: float = 0.0) -> array
Converts x of shape [num_elements, ...] to a dense array of
shape [num_sets, size, ...] padded with fill_value.
Source code in mlx_graphs/nn/aggr/utils.py
from_dense
¶
Gathers the rows of the (kept) elements of a dense array of shape
[num_sets, size, ...], in input order.
Source code in mlx_graphs/nn/aggr/utils.py
from mlx_graphs.nn.aggr import DenseLayout · mlx_graphs/nn/aggr/utils.py