Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 1 addition & 16 deletions grain/_src/python/data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from grain._src.core import monitoring
from grain._src.core import sharding
from grain._src.core import transforms
from grain._src.core import tree_lib
from grain._src.python import operations as ops
from grain._src.python import options
from grain._src.python import record
Expand Down Expand Up @@ -96,21 +95,7 @@ class CopyNumPyArrayToSharedMemory(transforms.Map):
"""If `element` contains NumPy array copy it to SharedMemoryArray."""

def map(self, element: Any) -> Any:
def copy_if_applied(element: Any) -> Any:
if (
not isinstance(element, np.ndarray)
or element.dtype.hasobject
or not element.flags.c_contiguous
):
return element

shared_memory_arr = shared_memory_array.SharedMemoryArray(
element.shape, element.dtype
)
np.copyto(shared_memory_arr, element, casting="no")
return shared_memory_arr.metadata

return tree_lib.map_structure(copy_if_applied, element)
return shared_memory_array.copy_to_shm(element)


class _SamplerMapDataset(dataset.MapDataset[record.Record]):
Expand Down
55 changes: 55 additions & 0 deletions grain/_src/python/dataset/transformations/batch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,61 @@ def test_batch_parallel_shared_memory_object_dtype(self):
self.assertIsInstance(batched_values, np.ndarray)


class SharedMemoryOutputTest(parameterized.TestCase):

@parameterized.named_parameters(
dict(
testcase_name="make_batch",
parallel=False,
values=[np.arange(3), np.arange(3, 6)],
),
dict(
testcase_name="make_batch_strings",
parallel=False,
values=[np.asarray("a"), np.asarray("bc")],
),
dict(
testcase_name="parallel_batch_serial_path",
parallel=True,
values=[np.arange(3), np.arange(3, 6)],
),
dict(
testcase_name="parallel_batch_parallel_path",
parallel=True,
values=[np.arange(3), np.arange(3, 6)],
min_parallel_bytes=0,
),
dict(
testcase_name="parallel_batch_strings",
parallel=True,
values=[np.asarray("a"), np.asarray("bc")],
),
)
def test_copy_to_shm_hands_over_batch_without_copy(
self,
parallel: bool,
values: Sequence[np.ndarray],
min_parallel_bytes: int = batch._PARALLEL_BATCHING_MIN_TOTAL_BYTES,
):
if parallel:
batch_fn = batch._MakeBatchParallel()
batch_fn.enable_shared_memory_output()
else:
batch_fn = functools.partial(
batch.make_batch, output_to_shared_memory=True
)
with mock.patch.object(
batch, "_PARALLEL_BATCHING_MIN_TOTAL_BYTES", min_parallel_bytes
):
batched = batch_fn(values)
self.assertIsInstance(batched, shared_memory_array.SharedMemoryArray)

# The batch's shared memory block is handed over instead of being copied.
shm_metadata = shared_memory_array.copy_to_shm(batched)
self.assertEqual(shm_metadata.name, batched.metadata.name)
shared_memory_array.unlink_shm(shm_metadata)


class StringTruncationTest(absltest.TestCase):
"""Tests exposing silent string truncation when batching variable-length strings via shared memory."""

Expand Down
11 changes: 7 additions & 4 deletions grain/_src/python/dataset/transformations/process_prefetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,9 @@ def _stop_prefetch(self):
self._prefetch_process.kill()
self._prefetch_process.join(timeout=_PROCESS_KILL_TIMEOUT_S)
else:
# Only drain the buffer if the worker exited cleanly; killing a worker
# mid-`queue.put` can leave the underlying pipe/locks in an
# inconsistent state and cause `get_nowait()` to hang.
_clear_queue_and_maybe_unlink_shm(self._buffer)
finally:
self._prefetch_process = None
Expand Down Expand Up @@ -711,17 +714,17 @@ def multiprocess_prefetch(
if num_workers == 0:
return ds

# Enable SharedMemoryArray outputs if the transform supports it.
if isinstance(ds, base.SupportsSharedMemoryOutput):
ds.enable_shared_memory_output()

dataset_options = prefetch.get_dataset_options(ds)

shards = []
for i in range(num_workers):
if num_workers == 1:
worker_ds = ds
else:
# Enable SharedMemoryArray outputs if the transform supports it.
if isinstance(ds, base.SupportsSharedMemoryOutput):
ds.enable_shared_memory_output()

worker_ds = _LazyWorkerSliceIterDataset(
ds,
slice(i, None, num_workers),
Expand Down
1 change: 1 addition & 0 deletions grain/_src/python/ipc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ py_library(
srcs_version = "PY3",
deps = [
"//grain/_src/core:tree_lib",
"@abseil-py//absl/logging",
"@pypi//numpy:pkg",
],
)
Expand Down
95 changes: 76 additions & 19 deletions grain/_src/python/ipc/shared_memory_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,66 @@
import threading
from typing import Any, Iterable

from absl import logging
from grain._src.core import tree_lib
import numpy as np
import numpy.typing as npt


@dataclasses.dataclass(slots=True, frozen=True)
class SharedMemoryArrayMetadata:
"""Metadata for a SharedMemoryArray."""

name: str
shape: Iterable[int]
dtype: npt.DTypeLike

def close_and_unlink_shm(self) -> None:
"""Closes and unlinks the shared memory referred to by this instance."""
shm = shared_memory.SharedMemory(self.name)
shm.close()
shm.unlink()
"""Closes and unlinks the shared memory. No-op if already unlinked."""
try:
shm = shared_memory.SharedMemory(self.name)
except FileNotFoundError:
# Each block has a single owner, so this signals an anomaly; don't fail
# cleanup over it.
logging.log_first_n(
logging.WARNING,
"Shared memory %s was already unlinked.",
5,
self.name,
)
return
_del_shm(shm, unlink=True)


def _del_shm(shm: shared_memory.SharedMemory, unlink: bool) -> None:
shm.close()
if unlink:
shm.unlink()
try:
shm.unlink()
except FileNotFoundError:
logging.log_first_n(
logging.WARNING,
"Shared memory %s was already unlinked.",
5,
shm.name,
)


class SharedMemoryArray(np.ndarray):
"""A NumPy array subclass which is backed by shared memory.

This should be used in combination with Python multiprocessing.
Compared with the normal NumPy ndarray it avoids expensive serialization
when sending the array to another Python process (on the same machine).
It also doesn't require a copy on the receiving side.

The last processes using the array must call unlink_on_del()! Otherwise
the memory will not be freed.
when sending a root array to another Python process (on the same machine).
It also doesn't require a copy on the receiving side. Views/slices of a
SharedMemoryArray do not carry offset or stride metadata and are therefore
serialized by value as standard NumPy ndarrays (or copied to a new shared
memory block by `copy_to_shm`).

Exactly one owning process (typically the consumer opening the array via
`open_from_shm`) must call `unlink_on_del()` on the root array, or call
`metadata.close_and_unlink_shm()`. Otherwise the shared memory block will not
be freed.
"""

_lock: threading.Lock = threading.Lock()
Expand Down Expand Up @@ -125,6 +151,10 @@ def from_shared_memory(
return obj

def __reduce_ex__(self, protocol):
if not isinstance(self.base, mmap.mmap):
# A view (slice) of a shared memory array does not carry offset/strides in
# `(shm, shape, dtype)`. Pickle it by value as a plain ndarray instead.
return self.view(np.ndarray).__reduce_ex__(protocol)
# For out-of-band pickling we don't need a PickleBuffer because the
# `SharedMemory` class automatically pickles itself using only its name
return self.from_shared_memory, (self.shm, self.shape, self.dtype)
Expand Down Expand Up @@ -164,9 +194,11 @@ def close_shm_async(
shm: shared_memory.SharedMemory,
unlink: bool,
) -> None:
_del_shm(shm, unlink)
assert cls._outstanding_del_requests is not None
cls._outstanding_del_requests.release()
try:
_del_shm(shm, unlink)
finally:
assert cls._outstanding_del_requests is not None
cls._outstanding_del_requests.release()

def unlink_on_del(self) -> None:
"""Mark this object responsible for unlinking the shared memory."""
Expand Down Expand Up @@ -196,24 +228,49 @@ def __del__(self) -> None:

def _copy_leaf_to_shm(leaf: Any, min_size: int = 0) -> Any:
"""Copies `leaf` to shared memory if it's a big enough numpy array."""
if isinstance(leaf, SharedMemoryArray):
if not isinstance(leaf, np.ndarray):
return leaf

# Root SharedMemoryArrays have leaf.base == mmap.mmap, whereas views/slices
# have leaf.base pointing to the parent ndarray.
if (
isinstance(leaf, SharedMemoryArray)
and isinstance(leaf.base, mmap.mmap)
and not leaf._unlink_on_del # pylint: disable=protected-access
):
return leaf.metadata

if (
not isinstance(leaf, np.ndarray)
or leaf.dtype.hasobject
leaf.dtype.hasobject
or not leaf.flags.c_contiguous
or math.prod(leaf.shape) == 0
or leaf.nbytes == 0
or leaf.nbytes < min_size
):
return leaf
return (
leaf.view(np.ndarray) if isinstance(leaf, SharedMemoryArray) else leaf
)

shared_memory_arr = SharedMemoryArray(leaf.shape, leaf.dtype)
np.copyto(shared_memory_arr, leaf, casting="no")
return shared_memory_arr.metadata


def copy_to_shm(struct: Any, min_size: int = 0) -> Any:
"""Copies leaf ndarrays of the structure to shared memory."""
"""Copies leaf ndarrays of the structure to shared memory.

Root `SharedMemoryArray` leaves not marked with `unlink_on_del()` are handed
over without a copy, and the caller must not modify them afterwards. Arrays
marked with `unlink_on_del()` remain owned by the caller and are copied.

Args:
struct: The structure whose leaf ndarrays to copy.
min_size: Arrays smaller than this many bytes are not copied.

Returns:
The structure with leaf ndarrays in shared memory replaced by their
`SharedMemoryArrayMetadata`. It must be passed to `open_from_shm` or
`unlink_shm` to free the shared memory.
"""
return tree_lib.map_structure(
functools.partial(_copy_leaf_to_shm, min_size=min_size), struct
)
Expand Down
Loading
Loading
⌂ 换网址