Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cond.not.where(T, F) -> cond.where(F, T) #548

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion test/unit/test_uop_symbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,6 @@ def test_div_neg_all_range(self):
self.helper_test_variable((-gidx*8-lidx+1001)//-4 + 250, 0, 250, "(((((gidx*-8)+(lidx*-1))+1001)//-4)+250)")
self.helper_test_variable((-gidx*8-lidx+1002)//-4 + 250, 0, 250, "(((((gidx*-8)+(lidx*-1))+1002)//-4)+250)")

# NOTE: tests are not correct in symbolic
def test_div_neg_then_neg(self):
# taken from arange opts
lidx0 = Variable("lidx0", 0, 7)
Expand Down Expand Up @@ -525,6 +524,13 @@ def test_where_combine(self):
# not combining # TODO: can combine if one is identity element const
self.helper_test_variable(aa+ab, 0, 6, "((a if (x<2) else b)+(a if (x<2) else 0))")

def test_where_flip(self):
cond = Variable("a", 0, 3).lt(2).logical_not()
t, f = Variable("t", 0, 3), Variable("f", 0, 3)
self.helper_test_variable(cond, 0, 1, "((a<2)!=True)")
self.helper_test_variable(cond.where(t, f), 0, 3, "(f if (a<2) else t)")
self.helper_test_variable(cond.where(f, t), 0, 3, "(t if (a<2) else f)")

class TestSymbolicNumeric(unittest.TestCase):
def helper_test_numeric(self, f):
MIN, MAX = 0, 10
Expand Down
6 changes: 5 additions & 1 deletion tinygrad/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1085,12 +1085,16 @@ def sint_to_uop(x:sint) -> UOp: return UOp.const(dtypes.int, x) if isinstance(x,
(UPat.var("x") + UPat.var("x"), lambda x: x*2), # (x+x)-> x*2
((UPat.var("x") / UPat.var("x2")) / UPat.var("x3"), lambda x,x2,x3: x/(x2*x3)), # (x/x2)/x3 -> x/(x2*x3)
(-1 * (UPat.var("x") + UPat.cvar("c")), lambda x,c: (-x)+(-c)), # -(x+c) -> -x + -c
# a conditional with the same results either way is a noop, also fold const conditionals
# ** where **
# a conditional with the same results either way is a noop
(UPat.var().where(UPat.var("val"), UPat.var("val")), lambda val: val),
# fold const conditionals
(UPat.cvar("gate", vec=False).where(UPat.var("c0"), UPat.var("c1")), lambda gate, c0, c1: c0 if gate.arg else c1),
# alu of two where with same conds can combine, only do if true branch or false branch is const
(UPat(GroupOp.Binary, name="alu", src=(UPat.var("c").where(UPat.var("t"), UPat.var("f")), UPat.var("c").where(UPat.var("tt"), UPat.var("ff")))), \
lambda alu,c,t,tt,f,ff: c.where(t.alu(alu.op, tt), f.alu(alu.op, ff)) if t.op == tt.op == Ops.CONST or f.op == ff.op == Ops.CONST else None),
# flip if cond has a not
(UPat.var("cond", dtype=dtypes.bool).logical_not().where(UPat.var("c0"), UPat.var("c1")), lambda cond,c0,c1: cond.where(c1, c0)),
# ALU min==max -> CONST (slow!)
(UPat(GroupOp.ALU, name="x"), lambda x: x.const_like(x.vmin) if x.vmin == x.vmax else None),
# max folding
Expand Down
Loading