|
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 |
|
@@ -407,3 +408,33 @@ def test_ScalarLoop_while():
|
407 | 408 | ):
|
408 | 409 | np.testing.assert_allclose(res[0], np.array(expected[0]))
|
409 | 410 | np.testing.assert_allclose(res[1], np.array(expected[1]))
|
| 411 | + |
| 412 | +def test_pytorch_OpFromGraph(): |
| 413 | + x, y, z = matrices("xyz") |
| 414 | + ofg_1 = OpFromGraph([x, y], [x + y]) |
| 415 | + ofg_2 = OpFromGraph([x, y], [x * y, x - y]) |
| 416 | + |
| 417 | + o1, o2 = ofg_2(y, z) |
| 418 | + out = ofg_1(x, o1) + o2 |
| 419 | + |
| 420 | + xv = np.ones((2, 2), dtype=config.floatX) |
| 421 | + yv = np.ones((2, 2), dtype=config.floatX) * 3 |
| 422 | + zv = np.ones((2, 2), dtype=config.floatX) * 5 |
| 423 | + |
| 424 | + f = FunctionGraph([x, y, z], [out]) |
| 425 | + compare_pytorch_and_py(f, [xv, yv, zv]) |
| 426 | + |
| 427 | + |
| 428 | +def test_ScalarLoop_Elemwise(): |
| 429 | + n_steps = int64("n_steps") |
| 430 | + x0 = float64("x0") |
| 431 | + x = x0 * 2 |
| 432 | + until = x >= 10 |
| 433 | + |
| 434 | + op = ScalarLoop(init=[x0], update=[x], until=until) |
| 435 | + fn = function([n_steps, x0], Elemwise(op)(n_steps, x0), mode=pytorch_mode) |
| 436 | + |
| 437 | + states, dones = fn(10, np.array(range(5))) |
| 438 | + |
| 439 | + np.testing.assert_allclose(states, [0, 4, 8, 12, 16]) |
| 440 | + np.testing.assert_allclose(dones, [False, False, False, True, True]) |
0 commit comments