[Bugfix] Fix lost async diffusion outputs in request-level batching (#6023)

Signed-off-by: samithuang <285365963@qq.com>
This commit is contained in:
Samit authored and GitHub committed 2026-08-15 18:27:51 +08:00
1 parent ce6ab0ca46
commit 136cb27b05
9 files changed
+475 -55

No files matched your search

+13 -2
View File
@@ -121,8 +121,19 @@ def _pack_tensor_if_large(
val: torch.Tensor,
d2h_stream: torch.Stream | None = None,
) -> torch.Tensor | dict:
"""Replace a tensor with an SHM handle if it exceeds the threshold."""
if val.nelement() * val.element_size() > _SHM_TENSOR_THRESHOLD:
"""Replace a tensor with an SHM handle if it exceeds the threshold.
Batch outputs are split into per-request views that share one storage.
Pickle serializes a view's whole storage, so a batch of small views costs
the full batch tensor once per request. Size the decision on the storage
to keep those views off the wire; packing copies just the view.
"""
view_bytes = val.nelement() * val.element_size()
try:
storage_bytes = val.untyped_storage().nbytes()
except Exception:
storage_bytes = view_bytes
if max(view_bytes, storage_bytes) > _SHM_TENSOR_THRESHOLD:
return _tensor_to_shm(val, d2h_stream=d2h_stream)
return val