Skip to content

Commit

Permalink
free toposort cache after it goes out of scope [pr] (tinygrad#8264)
Browse files Browse the repository at this point in the history
  • Loading branch information
Qazalin authored Dec 15, 2024
1 parent 53603c4 commit 1d21651
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
1 change: 0 additions & 1 deletion test/test_gc.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def test_schedule_gc_with_inputs(self):
del y
self.assertEqual(bufs_allocated()-init, 0)

@unittest.expectedFailure
def test_toposort_blocks_gc(self):
init = bufs_allocated()
x = Tensor.ones(4,4).contiguous().realize()+1
Expand Down
9 changes: 5 additions & 4 deletions tinygrad/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,15 @@ def argstr(self): return f'({", ".join(map(str, self.arg))})' if self.op is Ops.

@property
def toposort(self) -> Dict[UOp, None]:
@functools.lru_cache(None)
def _toposort(u:UOp):
def _toposort(u:UOp, cache:Dict[UOp, Dict[UOp, None]]):
if (cret:=cache.get(u)) is not None: return cret
nodes: Dict[UOp, None] = {}
# NOTE: this is a lot faster than the comprehension in parents
for parent in u.src: nodes.update(_toposort(parent))
for parent in u.src: nodes.update(_toposort(parent, cache))
nodes[u] = None
cache[u] = nodes
return nodes
return _toposort(self)
return _toposort(self, cache={})

@functools.cached_property
def tuplize(self:UOp) -> Tuple[int, Any, Optional[DType], Tuple]:
Expand Down

0 comments on commit 1d21651

Please sign in to comment.