Skip to content

Commit

Permalink
python_alu sin(inf) is nan (#5020)
Browse files Browse the repository at this point in the history
* python_alu sin(inf) is nan

without special handling, it throws ValueError: math domain error

* skip CUDACPU
  • Loading branch information
chenyuxyz committed Jun 17, 2024
1 parent 4296507 commit c0139b0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 3 additions & 0 deletions test/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,9 @@ def test_rshift(self):
def test_sin(self):
helper_test_op([(45,65)], lambda x: x.sin())
helper_test_op([()], lambda x: x.sin())
# works on real CUDA but not CUDACPU
if not (getenv("CUDACPU") or (getenv("MOCKGPU") and Device.DEFAULT == "NV")):
helper_test_op(None, lambda x: x.sin(), vals=[[math.nan, math.inf, -math.inf]])
def test_cos(self):
helper_test_op([(45,65)], lambda x: x.cos())
helper_test_op([()], lambda x: x.cos())
Expand Down
3 changes: 2 additions & 1 deletion tinygrad/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ def wfxn(*args):
python_alu = {
UnaryOps.LOG2: lambda x: math.log2(x) if x > 0 else -math.inf if x == 0 else math.nan,
UnaryOps.EXP2: hook_overflow(math.inf, lambda x: math.exp(x*math.log(2))),
UnaryOps.SQRT: lambda x: math.sqrt(x) if x >= 0 else math.nan, UnaryOps.SIN: math.sin,
UnaryOps.SQRT: lambda x: math.sqrt(x) if x >= 0 else math.nan,
UnaryOps.SIN: lambda x: math.sin(x) if not math.isinf(x) else math.nan,
UnaryOps.RECIP: lambda x: 1/x if x != 0 else float('inf'),
UnaryOps.NEG: lambda x: (not x) if isinstance(x, bool) else -x,
BinaryOps.SHR: operator.rshift, BinaryOps.SHL: operator.lshift,
Expand Down

0 comments on commit c0139b0

Please sign in to comment.