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.

mm: guard against double pin and unpin explicitly (#10672)

rattuscf97b033ee80
diff --git a/comfy/model_management.py b/comfy/model_management.py
index 7a30c4bec..a13b24cea 100644
--- a/comfy/model_management.py
+++ b/comfy/model_management.py
@@ -1103,6 +1103,12 @@ def pin_memory(tensor):
if not is_device_cpu(tensor.device):
return False
+ if tensor.is_pinned():
+ #NOTE: Cuda does detect when a tensor is already pinned and would
+ #error below, but there are proven cases where this also queues an error
+ #on the GPU async. So dont trust the CUDA API and guard here
+ return False
+
size = tensor.numel() * tensor.element_size()
if (TOTAL_PINNED_MEMORY + size) > MAX_PINNED_MEMORY:
return False
@@ -1123,6 +1129,12 @@ def unpin_memory(tensor):
if not is_device_cpu(tensor.device):
return False
+ if not tensor.is_pinned():
+ #NOTE: Cuda does detect when a tensor is already pinned and would
+ #error below, but there are proven cases where this also queues an error
+ #on the GPU async. So dont trust the CUDA API and guard here
+ return False
+
ptr = tensor.data_ptr()
if torch.cuda.cudart().cudaHostUnregister(ptr) == 0:
TOTAL_PINNED_MEMORY -= PINNED_MEMORY.pop(ptr)