Skip to content

Commit c1c6eed

Browse files
committed
Lift Subtensor over AdvancedSubtensor
1 parent b89487d commit c1c6eed

File tree

2 files changed

+125
-2
lines changed

2 files changed

+125
-2
lines changed

pytensor/tensor/rewriting/subtensor_lift.py

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from numpy.core.numeric import normalize_axis_index, normalize_axis_tuple
55

66
from pytensor import Variable
7+
from pytensor.compile import optdb
78
from pytensor.graph import Constant, FunctionGraph, node_rewriter
89
from pytensor.graph.rewriting.basic import NodeRewriter, copy_stack_trace
910
from pytensor.scalar import basic as ps
@@ -38,15 +39,17 @@
3839
from pytensor.tensor.shape import Shape, SpecifyShape, Unbroadcast, unbroadcast
3940
from pytensor.tensor.special import Softmax, softmax
4041
from pytensor.tensor.subtensor import (
42+
AdvancedSubtensor,
4143
AdvancedSubtensor1,
4244
Subtensor,
45+
_non_contiguous_adv_indexing,
4346
as_index_literal,
4447
get_canonical_form_slice,
4548
get_constant_idx,
4649
get_idx_list,
4750
indices_from_subtensor,
4851
)
49-
from pytensor.tensor.type_other import SliceType
52+
from pytensor.tensor.type_other import NoneTypeT, SliceType
5053

5154

5255
def _dims_dropped_by_basic_index(idxs) -> tuple[int, ...]:
@@ -809,3 +812,79 @@ def local_subtensor_shape_constant(fgraph, node):
809812
return [as_tensor([1] * len(shape_parts), dtype=np.int64, ndim=1)]
810813
elif shape_parts:
811814
return [as_tensor(1, dtype=np.int64)]
815+
816+
817+
@node_rewriter([Subtensor])
818+
def local_subtensor_of_adv_subtensor(fgraph, node):
819+
"""Lift a simple Subtensor through an AdvancedSubtensor, when basic index dimensions are to the left of any advanced ones.
820+
821+
x[:, :, vec_idx][i, j] -> x[i, j][vec_idx]
822+
x[:, vec_idx][i, j, k] -> x[i][vec_idx][j, k]
823+
824+
Restricted to a single advanced indexing dimension.
825+
826+
An alternative approach could have fused the basic and advanced indices,
827+
so it is not clear this rewrite should be canonical or a specialization.
828+
Users must include it manually if it fits their use case.
829+
"""
830+
adv_subtensor, *idxs = node.inputs
831+
832+
if not (
833+
adv_subtensor.owner and isinstance(adv_subtensor.owner.op, AdvancedSubtensor)
834+
):
835+
return None
836+
837+
if len(fgraph.clients[adv_subtensor]) > 1:
838+
# AdvancedSubtensor involves a full_copy, so we don't want to do it twice
839+
return None
840+
841+
x, *adv_idxs = adv_subtensor.owner.inputs
842+
843+
# Advanced indexing is a minefield, avoid all cases except for consecutive integer indices
844+
if any(
845+
(
846+
isinstance(adv_idx.type, NoneTypeT)
847+
or (isinstance(adv_idx.type, TensorType) and adv_idx.type.dtype == "bool")
848+
or (isinstance(adv_idx.type, SliceType) and not is_full_slice(adv_idx))
849+
)
850+
for adv_idx in adv_idxs
851+
) or _non_contiguous_adv_indexing(adv_idxs):
852+
return None
853+
854+
for first_adv_idx_dim, adv_idx in enumerate(adv_idxs):
855+
# We already made sure there were only None slices besides integer indexes
856+
if isinstance(adv_idx.type, TensorType):
857+
break
858+
else: # no-break
859+
# Not sure if this should ever happen, but better safe than sorry
860+
return None
861+
862+
basic_idxs = indices_from_subtensor(idxs, node.op.idx_list)
863+
basic_idxs_lifted = basic_idxs[:first_adv_idx_dim]
864+
basic_idxs_kept = ((slice(None),) * len(basic_idxs_lifted)) + basic_idxs[
865+
first_adv_idx_dim:
866+
]
867+
868+
if all(basic_idx == slice(None) for basic_idx in basic_idxs_lifted):
869+
# All basic indices happen to the right of the advanced indices
870+
return None
871+
872+
[basic_subtensor] = node.outputs
873+
dropped_dims = _dims_dropped_by_basic_index(basic_idxs_lifted)
874+
875+
x_indexed = x[basic_idxs_lifted]
876+
copy_stack_trace([basic_subtensor, adv_subtensor], x_indexed)
877+
878+
x_after_index_lift = expand_dims(x_indexed, dropped_dims)
879+
x_after_adv_idx = adv_subtensor.owner.op(x_after_index_lift, *adv_idxs)
880+
copy_stack_trace([basic_subtensor, adv_subtensor], x_after_adv_idx)
881+
882+
new_out = squeeze(x_after_adv_idx[basic_idxs_kept], dropped_dims)
883+
return [new_out]
884+
885+
886+
# Rewrite will only be included if tagged by name
887+
r = local_subtensor_of_adv_subtensor
888+
optdb["canonicalize"].register(r.__name__, r, use_db_name_as_tag=False)
889+
optdb["specialize"].register(r.__name__, r, use_db_name_as_tag=False)
890+
del r

tests/tensor/rewriting/test_subtensor_lift.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
)
5353
from pytensor.tensor.shape import SpecifyShape, Unbroadcast, _shape
5454
from pytensor.tensor.special import softmax
55-
from pytensor.tensor.subtensor import Subtensor
55+
from pytensor.tensor.subtensor import AdvancedSubtensor, Subtensor
5656

5757

5858
NO_OPTIMIZATION_MODE = Mode(linker="py", optimizer=None)
@@ -756,3 +756,47 @@ def __eq__(self, other):
756756
x = shape(Variable(MyType(), None, None))[0]
757757

758758
assert not local_subtensor_shape_constant.transform(None, x.owner)
759+
760+
761+
@pytest.mark.parametrize(
762+
"original_fn, supported",
763+
[
764+
(lambda x: x[:, [0, 1]][0], True),
765+
(lambda x: x[:, [0, 1], [0, 0]][1:], True),
766+
(lambda x: x[:, [[0, 1], [0, 0]]][1:], True),
767+
# Not supported, basic indexing on advanced indexing dim
768+
(lambda x: x[[0, 1]][0], False),
769+
# Not implemented, basic indexing on the right of advanced indexing
770+
(lambda x: x[[0, 1]][:, 0], False),
771+
# Not implemented, complex flavors of advanced indexing
772+
(lambda x: x[:, None, [0, 1]][0], False),
773+
(lambda x: x[:, 5:, [0, 1]][0], False),
774+
(lambda x: x[:, :, np.array([True, False, False])][0], False),
775+
(lambda x: x[[0, 1], :, [0, 1]][:, 0], False),
776+
],
777+
)
778+
def test_local_subtensor_of_adv_subtensor(original_fn, supported):
779+
rng = np.random.default_rng(257)
780+
x = pt.tensor3("x", shape=(7, 5, 3))
781+
x_test = rng.normal(size=x.type.shape)
782+
783+
out = original_fn(x)
784+
opt_out = rewrite_graph(
785+
out, include=("canonicalize", "local_subtensor_of_adv_subtensor")
786+
)
787+
# The graphs generated are too complicated to assert
788+
# We simply check that the happens before the advanced subtensor
789+
toposort = FunctionGraph(outputs=[opt_out], clone=False).toposort()
790+
[idx_subtensor] = [
791+
i for i, node in enumerate(toposort) if isinstance(node.op, Subtensor)
792+
]
793+
[idx_adv_subtensor] = [
794+
i for i, node in enumerate(toposort) if isinstance(node.op, AdvancedSubtensor)
795+
]
796+
swapped = idx_subtensor < idx_adv_subtensor
797+
correct = swapped if supported else not swapped
798+
assert correct, debugprint(opt_out, print_type=True)
799+
np.testing.assert_allclose(
800+
opt_out.eval({x: x_test}, mode=NO_OPTIMIZATION_MODE),
801+
out.eval({x: x_test}, mode=NO_OPTIMIZATION_MODE),
802+
)

0 commit comments

Comments
 (0)