Skip to content
Merged
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
18 changes: 18 additions & 0 deletions pytensor/link/numba/dispatch/scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Composite,
Identity,
Mul,
Pow,
Reciprocal,
ScalarOp,
Second,
Expand Down Expand Up @@ -165,6 +166,23 @@ def {binary_op_name}({input_signature}):
return nary_fn


@register_funcify_and_cache_key(Pow)
def numba_funcify_Pow(op, node, **kwargs):
pow_dtype = node.inputs[1].type.dtype
if pow_dtype.startswith("int"):
# Numba power fails when exponents are non 64-bit discrete integers and fasthmath=True
# https://github.com/numba/numba/issues/9554

def pow(x, y):
return x ** np.asarray(y, dtype=np.int64).item()
else:

def pow(x, y):
return x**y

return numba_basic.numba_njit(pow), scalar_op_cache_key(op)


@register_funcify_and_cache_key(Add)
def numba_funcify_Add(op, node, **kwargs):
nary_add_fn = binary_to_nary_func(node.inputs, "add", "+")
Expand Down
10 changes: 10 additions & 0 deletions tests/link/numba/test_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@ def test_Softplus(dtype):
)


def test_discrete_power():
# Test we don't fail to compile power with discrete exponents due to https://github.com/numba/numba/issues/9554
x = pt.scalar("x", dtype="float64")
exponent = pt.scalar("exponent", dtype="int8")
out = pt.power(x, exponent)
compare_numba_and_py(
[x, exponent], [out], [np.array(0.5), np.array(2, dtype="int8")]
)


def test_cython_obj_mode_fallback():
"""Test that unsupported cython signatures fallback to obj-mode"""

Expand Down
Loading