|
3 | 3 | import numpy as np
|
4 | 4 | import pytest
|
5 | 5 |
|
6 |
| -from pytensor.gradient import verify_grad |
| 6 | +from pytensor.gradient import NullTypeGradError, verify_grad |
7 | 7 | from pytensor.scalar import ScalarLoop
|
8 | 8 | from pytensor.tensor.elemwise import Elemwise
|
9 | 9 |
|
|
18 | 18 | from pytensor import tensor as pt
|
19 | 19 | from pytensor.compile.mode import get_default_mode
|
20 | 20 | from pytensor.configdefaults import config
|
21 |
| -from pytensor.tensor import gammaincc, inplace, vector |
| 21 | +from pytensor.tensor import gammaincc, inplace, kv, vector |
22 | 22 | from tests import unittest_tools as utt
|
23 | 23 | from tests.tensor.utils import (
|
24 | 24 | _good_broadcast_unary_chi2sf,
|
@@ -1196,3 +1196,23 @@ def test_unused_grad_loop_opt(self, wrt):
|
1196 | 1196 | [dd for i, dd in enumerate(expected_dds) if i in wrt],
|
1197 | 1197 | rtol=rtol,
|
1198 | 1198 | )
|
| 1199 | + |
| 1200 | + |
| 1201 | +def test_kv(): |
| 1202 | + rng = np.random.default_rng(3772) |
| 1203 | + v = vector("v") |
| 1204 | + x = vector("x") |
| 1205 | + |
| 1206 | + out = kv(v[:, None], x[None, :]) |
| 1207 | + test_v = np.array([-3.7, 4, 4.5, 5], dtype=v.type.dtype) |
| 1208 | + test_x = np.linspace(0, 5, 10, dtype=x.type.dtype) |
| 1209 | + |
| 1210 | + np.testing.assert_allclose( |
| 1211 | + out.eval({v: test_v, x: test_x}), |
| 1212 | + scipy.special.kv(test_v[:, None], test_x[None, :]), |
| 1213 | + ) |
| 1214 | + |
| 1215 | + with pytest.raises(NullTypeGradError): |
| 1216 | + grad(out.sum(), v) |
| 1217 | + |
| 1218 | + verify_grad(lambda x: kv(4.5, x), [test_x + 0.5], rng=rng) |
0 commit comments