Skip to content

Commit

Permalink
fix LtNode simplification when lhs and rhs contain same variables (ti…
Browse files Browse the repository at this point in the history
…nygrad#3451)

* fix LtNode simplification when lhs and rhs contain same variables

`(Variable("a", 1, 5) < Variable("a", 1, 5))` should eval to `NumNode(0)`

* fix with less perf impact
  • Loading branch information
chenyuxyz authored Feb 20, 2024
1 parent 1b6e890 commit 0d326a4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
8 changes: 5 additions & 3 deletions test/unit/test_symbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,11 @@ def test_node_lt_node(self):
c = Variable("c", 1, 10)
d = Variable("d", 5, 10)
# if the value is always the same, it folds to num
assert (a < b) == 1
assert (b < a) == 0
assert (d < a) == 0
assert (a < b) == NumNode(1)
assert (b < a) == NumNode(0)
assert (d < a) == NumNode(0)
assert (a < a) == NumNode(0)
assert (a > a) == NumNode(0)
# if it remains as a LtNode, bool is always true and (min, max) == (0, 1)
assert isinstance((a < c), LtNode) and (a < c).min == 0 and (a < c).max == 1
assert a < c
Expand Down
1 change: 1 addition & 0 deletions tinygrad/shape/symbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def get_bounds(self) -> Tuple[int, int]: raise NotImplementedError("must be impl

class LtNode(OpNode):
def get_bounds(self) -> Tuple[int, int]:
if self.a == self.b: return (0, 0)
if isinstance(self.b, int): return (1, 1) if self.a.max < self.b else (0, 0) if self.a.min >= self.b else (0, 1)
return (1, 1) if self.a.max < self.b.min else (0, 0) if self.a.min >= self.b.max else (0, 1)
def substitute(self, var_vals: Mapping[Variable, Union[NumNode, Variable]]) -> Node:
Expand Down

0 comments on commit 0d326a4

Please sign in to comment.