Revert "Revert "do not block gc in UOp.toposort (#9623)" (#9624)" (#9639)

This reverts commit 7ef02d0e1c.
This commit is contained in:
George Hotz
2025-03-31 14:03:38 +08:00
committed by GitHub
parent 49b1c46d16
commit ec405b919f

View File

@@ -241,6 +241,15 @@ class UOpMetaClass(type):
buffers:weakref.WeakKeyDictionary[UOp, Buffer] = weakref.WeakKeyDictionary() # this maps BUFFER uops to their device Buffers
all_metadata:weakref.WeakKeyDictionary[UOp, Metadata] = weakref.WeakKeyDictionary()
def _toposort(u:UOp, cache:set[UOp]):
if u in cache: return {}
nodes: dict[UOp, None] = {}
# NOTE: this is a lot faster than the comprehension in parents
for parent in u.src: nodes.update(_toposort(parent, cache))
nodes[u] = None
cache.add(u)
return nodes
# NOTE: this should be frozen, but frozen is slower
@dataclass(eq=False, slots=True)
class UOp(MathTrait, metaclass=UOpMetaClass):
@@ -271,14 +280,6 @@ class UOp(MathTrait, metaclass=UOpMetaClass):
@property
def toposort(self) -> dict[UOp, None]:
def _toposort(u:UOp, cache:set[UOp]):
if u in cache: return {}
nodes: dict[UOp, None] = {}
# NOTE: this is a lot faster than the comprehension in parents
for parent in u.src: nodes.update(_toposort(parent, cache))
nodes[u] = None
cache.add(u)
return nodes
return _toposort(self, cache=set())
# returns map of UOps to their children in the graph rooted by self