Skip to content

Commit 5c60e24

Browse files
comfyanonymoussfinktah
authored andcommitted
Only unpin tensor if it was pinned by ComfyUI (#10677)
1 parent d4cdd2e commit 5c60e24

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

comfy/model_management.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,13 +1133,18 @@ def unpin_memory(tensor):
11331133
if not is_device_cpu(tensor.device):
11341134
return False
11351135

1136-
if not tensor.is_pinned():
1137-
#NOTE: Cuda does detect when a tensor is already pinned and would
1138-
#error below, but there are proven cases where this also queues an error
1139-
#on the GPU async. So dont trust the CUDA API and guard here
1136+
ptr = tensor.data_ptr()
1137+
size = tensor.numel() * tensor.element_size()
1138+
1139+
size_stored = PINNED_MEMORY.get(ptr, None)
1140+
if size_stored is None:
1141+
logging.warning("Tried to unpin tensor not pinned by ComfyUI")
1142+
return False
1143+
1144+
if size != size_stored:
1145+
logging.warning("Size of pinned tensor changed")
11401146
return False
11411147

1142-
ptr = tensor.data_ptr()
11431148
if torch.cuda.cudart().cudaHostUnregister(ptr) == 0:
11441149
TOTAL_PINNED_MEMORY -= PINNED_MEMORY.pop(ptr)
11451150
if len(PINNED_MEMORY) == 0:

0 commit comments

Comments
 (0)