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
3 changes: 2 additions & 1 deletion tests/link/jax/test_elemwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ def test_logsumexp_benchmark(size, axis, benchmark):
X_max = at.switch(at.isinf(X_max), 0, X_max)
X_lse = at.log(at.sum(at.exp(X - X_max), axis=axis, keepdims=True)) + X_max

X_val = np.random.normal(size=size)
rng = np.random.default_rng(23920)
X_val = rng.normal(size=size)

X_lse_fn = pytensor.function([X], X_lse, mode="JAX")

Expand Down
23 changes: 23 additions & 0 deletions tests/link/numba/test_elemwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import numpy as np
import pytest
import scipy.special

import pytensor
import pytensor.tensor as at
import pytensor.tensor.inplace as ati
import pytensor.tensor.math as aem
Expand Down Expand Up @@ -532,3 +534,24 @@ def test_MaxAndArgmax(x, axes, exc):
if not isinstance(i, (SharedVariable, Constant))
],
)


@pytest.mark.parametrize("size", [(10, 10), (1000, 1000), (10000, 10000)])
@pytest.mark.parametrize("axis", [0, 1])
def test_logsumexp_benchmark(size, axis, benchmark):

X = at.matrix("X")
X_max = at.max(X, axis=axis, keepdims=True)
X_max = at.switch(at.isinf(X_max), 0, X_max)
X_lse = at.log(at.sum(at.exp(X - X_max), axis=axis, keepdims=True)) + X_max

rng = np.random.default_rng(23920)
X_val = rng.normal(size=size)

X_lse_fn = pytensor.function([X], X_lse, mode="JAX")

# JIT compile first
_ = X_lse_fn(X_val)
res = benchmark(X_lse_fn, X_val)
exp_res = scipy.special.logsumexp(X_val, axis=axis, keepdims=True)
np.testing.assert_array_almost_equal(res, exp_res)