Skip to content

Commit cca95a5

Browse files
committed
Fix: handle ReferenceError when iterating over objects in memory inspection logic. (#236)
1 parent aefb7ec commit cca95a5

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

mipcandy/data/io.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,19 @@ def dump_allocated_tensors() -> tuple[float, list[tuple[
7575
"""
7676
:return: (total size in MB, [(size in MB, shape, dtype, device, requires_grad, grad_fn)])
7777
"""
78-
tensors = [
79-
(obj, obj.numel() * obj.element_size() / 1048576) for obj in get_objects() if isinstance(obj, torch.Tensor)
80-
]
81-
tensors.sort(key=lambda t: t[1], reverse=True)
82-
return sum(t[1] for t in tensors), [
83-
(sz, tuple(t.shape), t.dtype, t.device, t.requires_grad, str(t.grad_fn)) for t, sz in tensors
84-
]
78+
tensors = []
79+
for obj in get_objects():
80+
try:
81+
if not isinstance(obj, torch.Tensor):
82+
continue
83+
except ReferenceError:
84+
continue
85+
try:
86+
tensors.append((
87+
obj.numel() * obj.element_size() / 1048576, tuple(obj.shape), obj.dtype, obj.device, obj.requires_grad,
88+
str(obj.grad_fn)
89+
))
90+
except ReferenceError:
91+
continue
92+
tensors.sort(key=lambda t: t[0], reverse=True)
93+
return sum(t[0] for t in tensors), tensors

0 commit comments

Comments
 (0)