meta-pytorch / meta-pytorch/data

Serialize np.ndarray via shared memory

Open
#1,089 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.3k
Forks
179
Avg merge
6d 1h
Merged PRs (30d)
2

Description

🚀 The feature

When transmitting np.ndarray via torch.multiprocessing (e.g. used by MPRS), back them by shared memory (SM) to significantly speed up transmission. This could potentially be expanded to other objects implementing the array interface in the future as well.

We can easily reuse exisiting implementations for this. E.g. to (pre-)copy a numpy array to SM (potentially before serialization):

def share_memory(arr: np.ndarray):
    view = torch.as_tensor(arr)
    sm = view.share_memory_()  # this will copy
    return sm.numpy()  # returned np.ndarray is a view into freshly copied SM storage

For serialization we would just need to call torch.as_tensor(arr).share_memory_() (plus the mandatory bookkeeping ofc, e.g. "this is a np.ndarray" etc.) which would either yield a view if backed by SM already or make a copy, and then reuse the serialization infrastructure for torch.Tensor to transmit it.

Since np.ndarray is a very foundational type used by many other libraries in turn, this could have a very significant impact.

Motivation, pitch

Transmitting np.ndarrays with the MPRS is very expensive since they get completely serialized via pickle and then pushed through a (named) pipe, c.f. https://github.com/pytorch/data/issues/1078.

However, the same does not hold true for torch.Tensor for which torch.multiprocessing provides a very cheap and fully fleshed out serialization method using shared memory. Since we can easily create views from torch -> numpy and numpy -> torch, we can easily reuse the existing serialization infrastructure in torch.

Thus it should be possible to deliver the very same performance boost to np.ndarray with very little effort.

Alternatives

No response

Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the torch.multiprocessing shared-memory serialization path and the existing torch.Tensor serialization infrastructure referenced in the issue, then trace how MPRS transmits np.ndarray values. The linked issue #1078 provides context for the current pickle-and-pipe cost. Done means ndarray transmission reuses shared-memory serialization with the required ndarray bookkeeping.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend, data
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.