mirror of
https://github.com/tinygrad/tinygrad.git
synced 2026-01-07 22:23:55 -05:00
Reuse toposort in recursive_property (#13993)
This commit is contained in:
@@ -104,15 +104,7 @@ class recursive_property(property):
|
|||||||
self.__doc__ = fxn.__doc__
|
self.__doc__ = fxn.__doc__
|
||||||
def __get__(self, x:UOp|None, owner=None):
|
def __get__(self, x:UOp|None, owner=None):
|
||||||
if x is None: return self
|
if x is None: return self
|
||||||
# this is very similar to toposort/topovisit
|
for node in x.toposort(gate=lambda node: self.nm not in node.__dict__): node.__dict__[self.nm] = self.fxn(node)
|
||||||
stack: list[tuple[UOp, bool]] = [(x, False)]
|
|
||||||
while stack:
|
|
||||||
node, visited = stack.pop()
|
|
||||||
if self.nm in node.__dict__: continue
|
|
||||||
if not visited:
|
|
||||||
stack.append((node, True))
|
|
||||||
for s in reversed(node.src): stack.append((s, False))
|
|
||||||
else: node.__dict__[self.nm] = self.fxn(node)
|
|
||||||
return x.__dict__[self.nm]
|
return x.__dict__[self.nm]
|
||||||
|
|
||||||
# we import this late so we can use resolve/smax in mixins
|
# we import this late so we can use resolve/smax in mixins
|
||||||
|
|||||||
Reference in New Issue
Block a user