MemmapFeatureStore
¶
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
|
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
from_data
classmethod
¶
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
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
from mlx_graphs.data import MemmapFeatureStore · mlx_graphs/data/feature_store.py