Skip to content

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.

Sync offload streams before stealing a pin (#17297)

rattusa071826fd361
diff --git a/comfy/model_management.py b/comfy/model_management.py
index 6e6aaefa3..aa3e04822 100644
--- a/comfy/model_management.py
+++ b/comfy/model_management.py
@@ -1539,6 +1539,14 @@ def get_offload_stream(device):
return s
return None
+def sync_all_offload_streams():
+ if NUM_STREAMS == 0:
+ synchronize()
+ return
+ for streams in STREAMS.values():
+ for stream in streams:
+ stream.synchronize()
+
def sync_stream(device, stream):
if stream is None or current_stream(device) is None:
return
diff --git a/comfy/pinned_memory.py b/comfy/pinned_memory.py
index 76374d2d5..fd3d1f1c4 100644
--- a/comfy/pinned_memory.py
+++ b/comfy/pinned_memory.py
@@ -28,6 +28,8 @@ def _steal_pin(module, stack, buckets, size, priority, subset):
if priority <= -bucket[-1][0]:
return False
+ # The victim's pin may still be the source of a queued copy, and the caller overwrites it next.
+ comfy.model_management.sync_all_offload_streams()
*_, victim = bucket.pop()
module_pin = module._pins[subset]
victim_pin = victim._pins[subset]