damccorm opened a new pull request, #41823:
URL: https://github.com/apache/beam/pull/41823
Note: this PR was auto-generated by tooling. Before moving it out of draft,
the author has done a full review of the PR, specifically evaluating it on the
following criteria:
- The PR is an appropriate fix for the issue (e.g. we're not changing a test
to avoid a bug)
- The PR is a simple fix to the issue (e.g. we're not adding infra when we
could just modify a test)
- The PR is not covering for other problems (e.g. if we're bumping timeouts,
it is not because we're covering for a performance problem)
Full details of the methodology of generation and review can be found here -
https://docs.google.com/document/d/14lj7ECVov4T4EdwmIzNlWrRGE3lLDMOwX5xUMGzaLoc/edit?usp=sharing
If you have concerns with the PR or with the underlying methodology used to
generate this, please comment below - they will be addressed by the author not
the tooling :)
## Summary
Fixes `RuntimeError: unable to allocate shared memory(shm) for file
</torch_...>` when returning (or sending) large PyTorch CPU tensors across
`MultiProcessShared` RPC connections in containers with small `/dev/shm` mounts
(such as 64 MB default Docker/Kubernetes/Dataflow worker containers).
## Root Cause
In [PostCommit Python run
45811568551](https://github.com/apache/beam/actions/runs/45811568551/job/139880472235)
(`:sdks:python:test-suites:direct:py313:torchInferenceTest`,
`maskrcnn_resnet50_fpn_v2` via `KeyedModelHandler`), `BaseManager` serialized
large Mask R-CNN output tensors from the spawned `MultiProcessShared` server
process (`spawn_process=True`, introduced in #40816) using Python's
`ForkingPickler`. PyTorch's `torch.multiprocessing.reductions.reduce_storage`
allocates shared memory via `storage._share_fd_cpu_()`, which fails once
`/dev/shm` is full (`multiprocessing.managers.RemoteError: Unserializable
message: RuntimeError: unable to allocate shared memory(shm) for file
</torch_51294_1748666028_95>: Success (0)`).
## Changes
- Registered a custom `multiprocessing.managers` serializer
(`beam_mps_pickle`) used by `_SingletonRegistrar` in
`sdks/python/apache_beam/utils/multi_process_shared.py`.
- When `torch.multiprocessing.reductions` is not imported,
`_ShmFallbackConnection.send()` delegates straight to `conn.send()` with zero
overhead.
- When `torch.multiprocessing.reductions` is imported,
`_ShmFallbackForkingPickler` wraps `torch.multiprocessing.*` reducers per
object:
- Preserves zero-copy `/dev/shm` transfer whenever `/dev/shm` has room.
- On a shared-memory allocation `RuntimeError`, logs a single warning per
RPC message and falls back to inline socket pickling
(`obj.__reduce_ex__(protocol)`) for that object and remaining non-CUDA objects
in the same message (without discarding earlier already-registered
`resource_sharer.DupFd` handles, avoiding FD leaks).
- Leaves CUDA objects (`is_cuda == True`) on the `torch.multiprocessing`
reducer path and re-raises unrelated `RuntimeError`s unchanged.
- Added unit tests in
`sdks/python/apache_beam/utils/multi_process_shared_test.py`
(`ShmFallbackSerializationTest`).
## Verification
- Ran `ShmFallbackSerializationTest` locally (`Ran 3 tests in 0.506s - OK`),
including partial `/dev/shm` exhaustion, CUDA preservation, non-shm
`RuntimeError` propagation, and end-to-end `MultiProcessShared` TCP socket RPC
round-trip.
- Triggering `beam_PostCommit_Python.yml` workflow verification.
### Verification
- Triggered `.github/workflows/beam_PostCommit_Python.yml` workflow run on
branch: [`46545041138`](https://github.com/apache/beam/actions/runs/46545041138)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]