|
5 | 5 | from pytensor import Mode, function, grad
|
6 | 6 | from pytensor.compile.ops import DeepCopyOp
|
7 | 7 | from pytensor.configdefaults import config
|
8 |
| -from pytensor.graph.basic import Variable |
| 8 | +from pytensor.graph.basic import Variable, equal_computations |
9 | 9 | from pytensor.graph.fg import FunctionGraph
|
10 |
| -from pytensor.graph.replace import clone_replace |
| 10 | +from pytensor.graph.replace import clone_replace, vectorize_node |
11 | 11 | from pytensor.graph.type import Type
|
12 | 12 | from pytensor.misc.safe_asarray import _asarray
|
13 | 13 | from pytensor.scalar.basic import ScalarConstant
|
14 |
| -from pytensor.tensor import as_tensor_variable, get_vector_length, row |
15 |
| -from pytensor.tensor.basic import MakeVector, constant |
| 14 | +from pytensor.tensor import as_tensor_variable, broadcast_to, get_vector_length, row |
| 15 | +from pytensor.tensor.basic import MakeVector, as_tensor, constant |
16 | 16 | from pytensor.tensor.elemwise import DimShuffle, Elemwise
|
17 | 17 | from pytensor.tensor.rewriting.shape import ShapeFeature
|
18 | 18 | from pytensor.tensor.shape import (
|
@@ -706,3 +706,88 @@ def test_shape_tuple():
|
706 | 706 | assert isinstance(res[1], ScalarConstant)
|
707 | 707 | assert res[1].data == 2
|
708 | 708 | assert not isinstance(res[2], ScalarConstant)
|
| 709 | + |
| 710 | + |
| 711 | +class TestVectorize: |
| 712 | + def test_shape(self): |
| 713 | + vec = tensor(shape=(None,)) |
| 714 | + mat = tensor(shape=(None, None)) |
| 715 | + |
| 716 | + node = shape(vec).owner |
| 717 | + vect_node = vectorize_node(node, mat) |
| 718 | + assert equal_computations(vect_node.outputs, [shape(mat)]) |
| 719 | + |
| 720 | + def test_reshape(self): |
| 721 | + x = scalar("x", dtype=int) |
| 722 | + vec = tensor(shape=(None,)) |
| 723 | + mat = tensor(shape=(None, None)) |
| 724 | + |
| 725 | + shape = (2, x) |
| 726 | + node = reshape(vec, shape).owner |
| 727 | + vect_node = vectorize_node(node, mat, shape) |
| 728 | + assert equal_computations( |
| 729 | + vect_node.outputs, [reshape(mat, (*mat.shape[:1], 2, x))] |
| 730 | + ) |
| 731 | + |
| 732 | + new_shape = (5, 2, x) |
| 733 | + vect_node = vectorize_node(node, mat, new_shape) |
| 734 | + assert equal_computations(vect_node.outputs, [reshape(mat, new_shape)]) |
| 735 | + |
| 736 | + with pytest.raises(NotImplementedError): |
| 737 | + vectorize_node(node, vec, broadcast_to(as_tensor([5, 2, x]), (2, 3))) |
| 738 | + |
| 739 | + with pytest.raises( |
| 740 | + ValueError, |
| 741 | + match="Invalid shape length passed into vectorize node of Reshape", |
| 742 | + ): |
| 743 | + vectorize_node(node, vec, (5, 2, x)) |
| 744 | + |
| 745 | + with pytest.raises( |
| 746 | + ValueError, |
| 747 | + match="Invalid shape length passed into vectorize node of Reshape", |
| 748 | + ): |
| 749 | + vectorize_node(node, mat, (5, 3, 2, x)) |
| 750 | + |
| 751 | + def test_specify_shape(self): |
| 752 | + x = scalar("x", dtype=int) |
| 753 | + mat = tensor(shape=(None, None)) |
| 754 | + tns = tensor(shape=(None, None, None)) |
| 755 | + |
| 756 | + shape = (x, None) |
| 757 | + node = specify_shape(mat, shape).owner |
| 758 | + vect_node = vectorize_node(node, tns, *shape) |
| 759 | + assert equal_computations( |
| 760 | + vect_node.outputs, [specify_shape(tns, (None, x, None))] |
| 761 | + ) |
| 762 | + |
| 763 | + new_shape = (5, 2, x) |
| 764 | + vect_node = vectorize_node(node, tns, *new_shape) |
| 765 | + assert equal_computations(vect_node.outputs, [specify_shape(tns, (5, 2, x))]) |
| 766 | + |
| 767 | + with pytest.raises(NotImplementedError): |
| 768 | + vectorize_node(node, mat, *([x, x], None)) |
| 769 | + |
| 770 | + with pytest.raises( |
| 771 | + ValueError, |
| 772 | + match="Invalid number of shape arguments passed into vectorize node of SpecifyShape", |
| 773 | + ): |
| 774 | + vectorize_node(node, mat, *(5, 2, x)) |
| 775 | + |
| 776 | + with pytest.raises( |
| 777 | + ValueError, |
| 778 | + match="Invalid number of shape arguments passed into vectorize node of SpecifyShape", |
| 779 | + ): |
| 780 | + vectorize_node(node, tns, *(5, 3, 2, x)) |
| 781 | + |
| 782 | + def test_unbroadcast(self): |
| 783 | + mat = tensor( |
| 784 | + shape=( |
| 785 | + 1, |
| 786 | + 1, |
| 787 | + ) |
| 788 | + ) |
| 789 | + tns = tensor(shape=(4, 1, 1, 1)) |
| 790 | + |
| 791 | + node = unbroadcast(mat, 0).owner |
| 792 | + vect_node = vectorize_node(node, tns) |
| 793 | + assert equal_computations(vect_node.outputs, [unbroadcast(tns, 2)]) |
0 commit comments