[Bugfix][Distributed] Own SHM out-of-band buffers before releasing read slots (#61000)
Signed-off-by: Hubert Marek <163992334+hubert-marek@users.noreply.github.com> Signed-off-by: Ao Shen <aoshen@inferact.ai> Co-authored-by: Codex <noreply@openai.com> Co-authored-by: Ao Shen <aoshen@inferact.ai>
This commit is contained in:
@@ -408,8 +408,8 @@ def _rebuild_tensor(buf: Any, shape: tuple[int, ...], dtype_str: str) -> torch.T
|
||||
Counterpart of `_reduce_tensor`. Note that pickle passes the original
|
||||
buffer-providing object from `loads(buffers=...)` straight to this
|
||||
function (no `PickleBuffer` wrapper on the receiving side), so `buf` is
|
||||
a `zmq.Frame`, a `memoryview` of a shared-memory ring chunk, or `bytes`
|
||||
if the buffer was serialized in-band.
|
||||
a `zmq.Frame`, an owned `bytearray` from the shared-memory ring, or
|
||||
`bytes`/`bytearray` if the buffer was serialized in-band.
|
||||
"""
|
||||
dtype = getattr(torch, dtype_str)
|
||||
assert isinstance(dtype, torch.dtype)
|
||||
@@ -423,11 +423,8 @@ def _rebuild_tensor(buf: Any, shape: tuple[int, ...], dtype_str: str) -> torch.T
|
||||
except ValueError:
|
||||
# Empty or read-only frame buffer; fall through to the copy path.
|
||||
pass
|
||||
# Shared-memory ring buffer chunks are reused by the writer once all
|
||||
# readers have marked them read, so we must copy out of them. bytearray
|
||||
# (vs bytes) keeps the resulting tensor writable, matching normal tensor
|
||||
# semantics.
|
||||
raw = bytearray(buf)
|
||||
# dequeue already owns SHM buffers. Copy other buffers to keep them writable.
|
||||
raw = buf if isinstance(buf, bytearray) else bytearray(buf)
|
||||
if not raw:
|
||||
assert 0 in shape
|
||||
return torch.empty(shape, dtype=dtype)
|
||||
@@ -981,7 +978,9 @@ class MessageQueue:
|
||||
buf_offset = offset + 4
|
||||
buf_len = from_bytes_big(buf[offset:buf_offset])
|
||||
offset = buf_offset + buf_len
|
||||
all_buffers.append(buf[buf_offset:offset])
|
||||
buffer = buf[buf_offset:offset]
|
||||
# Out-of-band arrays may outlive this reusable ring slot.
|
||||
all_buffers.append(buffer if i == 0 else bytearray(buffer))
|
||||
obj = pickle.loads(all_buffers[0], buffers=all_buffers[1:])
|
||||
if overflow:
|
||||
obj = MessageQueue.recv(self.local_socket, timeout)
|
||||
|
||||
Reference in New Issue
Block a user