Skip to content

MemmapFeatureStore

MemmapFeatureStore(root: str, num_threads: int = 8)

Bases: FeatureStore

A FeatureStore keeping every array in a .npy file on disk.

Only the rows that are asked for are read, so the arrays (e.g. node features of a large graph) can be larger than the memory. Rows given as an index array are read with positional reads (os.preadv), in increasing order and spread over num_threads threads: nearby rows (less than 64 KB apart) are read with one request, isolated rows one by one. The process only holds the requested rows (plus at most 16 MB per thread for gaps), without the read-ahead and resident pages of a memory mapping (the OS may still keep the file in its cache). Other reads (all rows, slices) and writes go through np.memmap.

Arrays are stored as <root>/<group_name or "_">/<attr_name>.npy. Large arrays can be written in chunks without holding them in memory, with create_tensor():

from mlx_graphs.data import MemmapFeatureStore

store = MemmapFeatureStore("features/")
x = store.create_tensor("x", shape=(num_nodes, 512), dtype="float32")
for start in range(0, num_nodes, 100_000):
    x[start : start + 100_000] = compute_features(start)
x.flush()
del x

store.get_tensor(attr_name="x", index=mx.array([3, 1, 4]))

Parameters:

Name Type Description Default
root str

Directory of the store (created if needed). Arrays already in it are available.

required
num_threads int

Number of threads reading rows in parallel (more requests in flight help SSDs). Default: 8.

8

Methods:

Name Description
from_data

Writes the array attributes of a

create_tensor

Creates an array on disk and returns it as a writable

Source code in mlx_graphs/data/feature_store.py
def __init__(self, root: str, num_threads: int = 8):
    self.root = os.path.expanduser(root)
    os.makedirs(self.root, exist_ok=True)
    self.num_threads = max(1, int(num_threads))
    self._meta: dict[tuple, tuple[str, np.dtype, tuple, int]] = {}
    self._pool: Optional[ThreadPoolExecutor] = None
    self._scan()

from_data classmethod

from_data(
    data: Any, root: str, attrs: Optional[list[str]] = None
) -> MemmapFeatureStore

Writes the array attributes of a GraphData (all but edge_index unless attrs lists the ones to keep) to a new store in root.

Source code in mlx_graphs/data/feature_store.py
@classmethod
def from_data(
    cls, data: Any, root: str, attrs: Optional[list[str]] = None
) -> "MemmapFeatureStore":
    """Writes the array attributes of a
    :class:`~mlx_graphs.data.GraphData` (all but ``edge_index`` unless
    ``attrs`` lists the ones to keep) to a new store in ``root``."""
    store = cls(root)
    for name, value in _graph_arrays(data, attrs).items():
        store.put_tensor(value, attr_name=name)
    return store

create_tensor

create_tensor(
    attr_name: str,
    shape: tuple[int, ...],
    dtype: Any = "float32",
    group_name: Optional[str] = None,
) -> memmap

Creates an array on disk and returns it as a writable np.memmap, to be filled in chunks (call flush() and drop it when done).

Source code in mlx_graphs/data/feature_store.py
def create_tensor(
    self,
    attr_name: str,
    shape: tuple[int, ...],
    dtype: Any = "float32",
    group_name: Optional[str] = None,
) -> np.memmap:
    """Creates an array on disk and returns it as a writable
    ``np.memmap``, to be filled in chunks (call ``flush()`` and drop it
    when done)."""
    attr = TensorAttr(group_name, attr_name)
    path = self._path(attr)
    os.makedirs(os.path.dirname(path), exist_ok=True)
    out = np.lib.format.open_memmap(
        path, mode="w+", dtype=np.dtype(dtype), shape=tuple(shape)
    )
    self._register(attr)
    return out

from mlx_graphs.data import MemmapFeatureStore · mlx_graphs/data/feature_store.py