Skip to content

Commit a1a7036

Browse files
Only unpin tensor if it was pinned by ComfyUI (Comfy-Org#10677)
1 parent cf97b03 commit a1a7036

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

comfy/model_management.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,13 +1129,18 @@ def unpin_memory(tensor):
11291129
if not is_device_cpu(tensor.device):
11301130
return False
11311131

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

1138-
ptr = tensor.data_ptr()
11391144
if torch.cuda.cudart().cudaHostUnregister(ptr) == 0:
11401145
TOTAL_PINNED_MEMORY -= PINNED_MEMORY.pop(ptr)
11411146
if len(PINNED_MEMORY) == 0:

0 commit comments

Comments
 (0)