From 3c9cdccfcb4f390ccd13404b2301aa675f03f675 Mon Sep 17 00:00:00 2001 From: Manolis Papadakis Date: Tue, 20 Feb 2024 16:54:30 -0800 Subject: [PATCH 1/3] Basic np.load implementation, falling back to NumPy Mostly there to ensure that the NumPy ndarray coming out of np.load gets cast to a cuNumeric ndarray. As requested by ORNL. --- cunumeric/module.py | 52 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/cunumeric/module.py b/cunumeric/module.py index c076200f8..0205c38f3 100644 --- a/cunumeric/module.py +++ b/cunumeric/module.py @@ -59,7 +59,8 @@ from .utils import AxesPairLike, inner_modes, matmul_modes, tensordot_modes if TYPE_CHECKING: - from typing import Callable + from os import PathLike + from typing import BinaryIO, Callable import numpy.typing as npt @@ -616,6 +617,55 @@ def copy(a: ndarray) -> ndarray: return result +def load( + file: str | bytes | PathLike[Any] | BinaryIO, + *, + max_header_size: int = 10000, +) -> ndarray: + """ + Load an array from a ``.npy`` file. + + Parameters + ---------- + file : file-like object, string, or pathlib.Path + The file to read. File-like objects must support the + ``seek()`` and ``read()`` methods and must always + be opened in binary mode. + max_header_size : int, optional + Maximum allowed size of the header. Large headers may not be safe + to load securely and thus require explicitly passing a larger value. + See :py:func:`ast.literal_eval()` for details. + + Returns + ------- + result : array + Data stored in the file. + + Raises + ------ + OSError + If the input file does not exist or cannot be read. + + See Also + -------- + numpy.load + + Notes + ----- + cuNumeric does not currently support ``.npz`` and pickled files. + + Availability + -------- + Single CPU + """ + return array( + np.load( + file, + max_header_size=max_header_size, # type: ignore [call-arg] + ) + ) + + # Numerical ranges From 8cc4e4ff997b4179733c6c43a4b7b5632b523b41 Mon Sep 17 00:00:00 2001 From: Manolis Papadakis Date: Mon, 26 Feb 2024 18:59:49 -0800 Subject: [PATCH 2/3] Add .rst file --- docs/cunumeric/source/api/io.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 docs/cunumeric/source/api/io.rst diff --git a/docs/cunumeric/source/api/io.rst b/docs/cunumeric/source/api/io.rst new file mode 100644 index 000000000..0fd4ee4b3 --- /dev/null +++ b/docs/cunumeric/source/api/io.rst @@ -0,0 +1,11 @@ +Input and output +================ + +.. currentmodule:: cunumeric + +NumPy binary files (npy, npz) +----------------------------- +.. autosummary:: + :toctree: generated/ + + load From 7a1634a05ed82bfcd74f6e0c02ab4ead4ce3d19f Mon Sep 17 00:00:00 2001 From: Manolis Papadakis Date: Tue, 27 Feb 2024 14:32:05 -0800 Subject: [PATCH 3/3] Add IO page to top-level TOC --- docs/cunumeric/source/api/routines.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/cunumeric/source/api/routines.rst b/docs/cunumeric/source/api/routines.rst index e85a5c65b..302c7904a 100644 --- a/docs/cunumeric/source/api/routines.rst +++ b/docs/cunumeric/source/api/routines.rst @@ -9,6 +9,7 @@ Routines manipulation binary indexing + io linalg logic math