Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GraphBolt] Add gb.numpy_save_aligned. #7524

Merged
merged 8 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[GraphBolt] Add gb.numpy_save_aligned.
  • Loading branch information
mfbalin committed Jul 15, 2024
commit 24804be09579ab432cd52e568afb7220b5c247b3
6 changes: 5 additions & 1 deletion python/dgl/graphbolt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ def load_graphbolt():
from .negative_sampler import *
from .sampled_subgraph import *
from .subgraph_sampler import *
from .external_utils import add_reverse_edges, exclude_seed_edges
from .external_utils import (
add_reverse_edges,
exclude_seed_edges,
numpy_save_aligned,
)
from .internal import (
compact_csc_format,
unique_and_compact,
Expand Down
12 changes: 12 additions & 0 deletions python/dgl/graphbolt/external_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from typing import Dict, Union

import numpy
import torch

from .minibatch import MiniBatch
Expand Down Expand Up @@ -101,3 +102,14 @@ def exclude_seed_edges(
for subgraph in minibatch.sampled_subgraphs
]
return minibatch


def numpy_save_aligned(*args, **kwargs):
"""A wrapper for numpy.save(), ensures the array is stored 4KiB aligned."""
has_array_align = hasattr(numpy.lib.format, "ARRAY_ALIGN")
if has_array_align:
default_alignment = numpy.lib.format.ARRAY_ALIGN
numpy.lib.format.ARRAY_ALIGN = 4096
numpy.save(*args, **kwargs)
if has_array_align:
numpy.lib.format.ARRAY_ALIGN = default_alignment
4 changes: 3 additions & 1 deletion python/dgl/graphbolt/internal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import torch
from numpy.lib.format import read_array_header_1_0, read_array_header_2_0

from ..external_utils import numpy_save_aligned


def _read_torch_data(path):
return torch.load(path)
Expand Down Expand Up @@ -54,7 +56,7 @@ def save_data(data, path, fmt):
"so it will be copied to contiguous memory."
)
data = np.ascontiguousarray(data)
np.save(path, data)
numpy_save_aligned(path, data)
elif fmt == "torch":
if not data.is_contiguous():
Warning(
Expand Down
Loading