fix: fix SHM pointer re-serialization in DP attention (#17930)

This commit is contained in:
Fan Yin
2026-01-30 17:03:30 +08:00
committed by GitHub
parent 77a27e728c
commit 8ce9609fa2
2 changed files with 75 additions and 2 deletions

View File

@@ -1513,6 +1513,27 @@ class ShmPointerMMData:
self.shm.close()
def __getstate__(self):
if not hasattr(self, "shm") or self.shm is None:
tensor = getattr(self, "cpu_tensor", None)
if tensor is None:
tensor = getattr(self, "tensor", None)
if tensor is None:
raise RuntimeError(
"ShmPointerMMData cannot recreate shared memory without tensor"
)
cpu_tensor = tensor.cpu().contiguous()
self.shape = cpu_tensor.shape
self.dtype = cpu_tensor.dtype
nbytes = cpu_tensor.numel() * cpu_tensor.element_size()
self.shm = shared_memory.SharedMemory(create=True, size=nbytes)
try:
shm_view = np.ndarray((nbytes,), dtype=np.uint8, buffer=self.shm.buf)
shm_view[:] = cpu_tensor.view(torch.uint8).numpy().flatten()
finally:
self.shm.close()
return {
"shm_name": self.shm.name,
"shape": self.shape,
@@ -1521,12 +1542,15 @@ class ShmPointerMMData:
def __setstate__(self, state):
self.shm_name = state["shm_name"]
self.shape = state["shape"]
self.dtype = state["dtype"]
self.shm = None
shm_handle = shared_memory.SharedMemory(name=self.shm_name)
try:
self.tensor = (
torch.frombuffer(shm_handle.buf, dtype=state["dtype"])
.reshape(state["shape"])
torch.frombuffer(shm_handle.buf, dtype=self.dtype)
.reshape(self.shape)
.clone()
)
finally: