|
20 | 20 | from pytensor.scalar import float64, int64 |
21 | 21 | from pytensor.scalar.loop import ScalarLoop |
22 | 22 | from pytensor.tensor import alloc, arange, as_tensor, empty, eye |
| 23 | +from pytensor.tensor.elemwise import Elemwise |
23 | 24 | from pytensor.tensor.type import matrices, matrix, scalar, vector |
24 | 25 |
|
25 | 26 |
|
@@ -375,3 +376,33 @@ def test_ScalarLoop_while(): |
375 | 376 | ): |
376 | 377 | np.testing.assert_allclose(res[0], np.array(expected[0])) |
377 | 378 | np.testing.assert_allclose(res[1], np.array(expected[1])) |
| 379 | + |
| 380 | +def test_pytorch_OpFromGraph(): |
| 381 | + x, y, z = matrices("xyz") |
| 382 | + ofg_1 = OpFromGraph([x, y], [x + y]) |
| 383 | + ofg_2 = OpFromGraph([x, y], [x * y, x - y]) |
| 384 | + |
| 385 | + o1, o2 = ofg_2(y, z) |
| 386 | + out = ofg_1(x, o1) + o2 |
| 387 | + |
| 388 | + xv = np.ones((2, 2), dtype=config.floatX) |
| 389 | + yv = np.ones((2, 2), dtype=config.floatX) * 3 |
| 390 | + zv = np.ones((2, 2), dtype=config.floatX) * 5 |
| 391 | + |
| 392 | + f = FunctionGraph([x, y, z], [out]) |
| 393 | + compare_pytorch_and_py(f, [xv, yv, zv]) |
| 394 | + |
| 395 | + |
| 396 | +def test_ScalarLoop_Elemwise(): |
| 397 | + n_steps = int64("n_steps") |
| 398 | + x0 = float64("x0") |
| 399 | + x = x0 * 2 |
| 400 | + until = x >= 10 |
| 401 | + |
| 402 | + op = ScalarLoop(init=[x0], update=[x], until=until) |
| 403 | + fn = function([n_steps, x0], Elemwise(op)(n_steps, x0), mode=pytorch_mode) |
| 404 | + |
| 405 | + states, dones = fn(10, np.array(range(5))) |
| 406 | + |
| 407 | + np.testing.assert_allclose(states, [0, 4, 8, 12, 16]) |
| 408 | + np.testing.assert_allclose(dones, [False, False, False, True, True]) |
0 commit comments