Skip to content

Commit 78e737a

Browse files
authored
[release/2.13] Revert "Tighten generalized scatter graph target (#184075)" (#188277)
This reverts commit b35bc89 on the release/2.13 branch. It is the release-branch counterpart of the main-branch revert (#188219). PR #184075 reworked Inductor's temporary generalized scatter form to use private OpOverload targets (a torch.library FRAGMENT registering generalized_scatter / inplace_generalized_scatter_ with flattened, encoded view-op arguments) and added matching Piecewise support in the z3 validator. This revert restores the prior representation that passes view ops as a list of ViewOp dataclasses directly as call_function args. The reinplace.py hunks conflicted against the release branch; all were resolved to the pre-PR form with bare asserts (torch/_inductor/** is still S101-exempt on this branch, so no noqa is added), fully removing the encode/decode machinery, the torch.library registration, and the z3 validator additions. Test Plan: ``` lintrunner torch/_inductor/fx_passes/reinplace.py ``` This PR was authored with the assistance of an AI agent.
1 parent 0bb9b5b commit 78e737a

4 files changed

Lines changed: 39 additions & 558 deletions

File tree

‎test/inductor/test_inplacing_pass.py‎

Lines changed: 1 addition & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,13 @@
1111
auto_functionalized,
1212
auto_functionalized_v2,
1313
)
14-
from torch._inductor.fx_passes.reinplace import (
15-
_generalized_scatter,
16-
canonicalize_view_scatter_ops,
17-
decompose_generalized_scatter,
18-
reinplace_inplaceable_ops_core,
19-
)
14+
from torch._inductor.fx_passes.reinplace import reinplace_inplaceable_ops_core
2015
from torch._inductor.test_case import run_tests, TestCase as InductorTestCase
2116
from torch.testing._internal.common_utils import (
2217
instantiate_parametrized_tests,
2318
IS_LINUX,
2419
parametrize,
2520
subtest,
26-
TEST_Z3,
2721
)
2822
from torch.testing._internal.inductor_utils import GPU_TYPE, HAS_GPU
2923
from torch.testing._internal.logging_utils import logs_to_string
@@ -530,164 +524,6 @@ def fn(x_1):
530524
result = torch.compile(fn, fullgraph=True, backend="inductor")(x)
531525
self.assertEqual(result, expected)
532526

533-
def test_generalized_scatter_target_is_operator(self):
534-
from torch._guards import detect_fake_mode
535-
from torch._inductor.virtualized import V
536-
from torch.func import functionalize
537-
538-
def fn(x, y):
539-
z = x.sin()
540-
z[:, 0].copy_(y)
541-
return z.cos()
542-
543-
x = torch.randn(2, 3)
544-
y = torch.randn(2)
545-
gm = make_fx(functionalize(fn), tracing_mode="fake")(x, y)
546-
fake_mode = detect_fake_mode(
547-
[node.meta["val"] for node in gm.graph.nodes if "val" in node.meta]
548-
)
549-
550-
with V.set_fake_mode(fake_mode):
551-
canonicalize_view_scatter_ops(gm.graph)
552-
553-
generalized_scatter_nodes = [
554-
node
555-
for node in gm.graph.nodes
556-
if node.op == "call_function" and node.target is _generalized_scatter
557-
]
558-
self.assertEqual(len(generalized_scatter_nodes), 1)
559-
560-
unexpected_targets = [
561-
node.target
562-
for node in gm.graph.nodes
563-
if node.op == "call_function"
564-
and not isinstance(node.target, torch._ops.OpOverload)
565-
and node.target is not operator.getitem
566-
]
567-
self.assertFalse(
568-
unexpected_targets, f"unexpected targets: {unexpected_targets}"
569-
)
570-
571-
with V.set_fake_mode(fake_mode):
572-
decompose_generalized_scatter(gm.graph)
573-
574-
remaining_generalized_scatter_nodes = [
575-
node
576-
for node in gm.graph.nodes
577-
if node.op == "call_function" and node.target is _generalized_scatter
578-
]
579-
self.assertFalse(remaining_generalized_scatter_nodes)
580-
581-
def test_generalized_scatter_symbolic_view_args(self):
582-
from torch._guards import detect_fake_mode
583-
from torch._inductor.virtualized import V
584-
from torch.func import functionalize
585-
from torch.utils import _pytree as pytree
586-
587-
def fn(x, y):
588-
z = x.sin()
589-
z[: y.shape[0], 0].copy_(y)
590-
return z.cos()
591-
592-
x = torch.randn(4, 3)
593-
y = torch.randn(2)
594-
torch._dynamo.mark_dynamic(y, 0)
595-
gm = make_fx(functionalize(fn), tracing_mode="symbolic")(x, y)
596-
fake_mode = detect_fake_mode(
597-
[node.meta["val"] for node in gm.graph.nodes if "val" in node.meta]
598-
)
599-
600-
with V.set_fake_mode(fake_mode):
601-
canonicalize_view_scatter_ops(gm.graph)
602-
603-
generalized_scatter_nodes = [
604-
node
605-
for node in gm.graph.nodes
606-
if node.op == "call_function" and node.target is _generalized_scatter
607-
]
608-
self.assertEqual(len(generalized_scatter_nodes), 1)
609-
610-
node = generalized_scatter_nodes[0]
611-
fake_args, fake_kwargs = pytree.tree_map(
612-
lambda arg: arg.meta["val"] if isinstance(arg, torch.fx.Node) else arg,
613-
(node.args, node.kwargs),
614-
)
615-
fake_result = node.target(*fake_args, **fake_kwargs)
616-
self.assertEqual(fake_result.shape, fake_args[0].shape)
617-
618-
def test_generalized_scatter_dynamic_clamped_slice(self):
619-
def fn(x, y):
620-
z = x.sin()
621-
z[:9223372036854775807].copy_(y)
622-
return z.cos()
623-
624-
x = torch.randn(4, 3)
625-
y = torch.randn(4, 3)
626-
torch._dynamo.mark_dynamic(x, 0)
627-
torch._dynamo.mark_dynamic(y, 0)
628-
629-
expected = fn(x, y)
630-
result = torch.compile(fn, fullgraph=True, backend="inductor")(x, y)
631-
self.assertEqual(result, expected)
632-
633-
def test_generalized_scatter_dynamic_clamped_slice_translation_validation(self):
634-
if not TEST_Z3:
635-
self.skipTest("requires z3-solver")
636-
637-
import torch.fx.experimental._config as fx_config
638-
639-
def fn(x, y):
640-
z = x.sin()
641-
z[:9223372036854775807].copy_(y)
642-
return z.cos()
643-
644-
x = torch.randn(4, 3)
645-
y = torch.randn(4, 3)
646-
torch._dynamo.mark_dynamic(x, 0)
647-
torch._dynamo.mark_dynamic(y, 0)
648-
649-
expected = fn(x, y)
650-
with fx_config.patch(translation_validation=True):
651-
result = torch.compile(fn, fullgraph=True, backend="inductor")(x, y)
652-
self.assertEqual(result, expected)
653-
654-
def test_generalized_scatter_dynamic_negative_slice(self):
655-
def fn(x, y):
656-
z = x.sin()
657-
z[-y.shape[0] :].copy_(y)
658-
return z.cos()
659-
660-
x = torch.randn(4, 3)
661-
y = torch.randn(2, 3)
662-
torch._dynamo.mark_dynamic(x, 0)
663-
torch._dynamo.mark_dynamic(y, 0)
664-
665-
expected = fn(x, y)
666-
result = torch.compile(fn, fullgraph=True, backend="inductor")(x, y)
667-
self.assertEqual(result, expected)
668-
669-
def test_generalized_scatter_ignores_unrelated_view_ops(self):
670-
from torch._guards import detect_fake_mode
671-
from torch._inductor.virtualized import V
672-
from torch.func import functionalize
673-
674-
def fn(x, y):
675-
z = x.sin()
676-
t = x.t()
677-
z[:, 0].copy_(y)
678-
return z.cos(), t
679-
680-
x = torch.randn(2, 3)
681-
y = torch.randn(2)
682-
gm = make_fx(functionalize(fn), tracing_mode="fake")(x, y)
683-
fake_mode = detect_fake_mode(
684-
[node.meta["val"] for node in gm.graph.nodes if "val" in node.meta]
685-
)
686-
687-
with V.set_fake_mode(fake_mode):
688-
canonicalize_view_scatter_ops(gm.graph)
689-
decompose_generalized_scatter(gm.graph)
690-
691527
@parametrize(
692528
"factory_op",
693529
[

‎test/test_fx_experimental.py‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,10 +2374,6 @@ def test_sympy_to_z3(self):
23742374
Mod(s2, s0**3),
23752375
z2 - z3.ToReal(z3.ToInt(z3.ToReal(z2) / z0**3)) * z0**3,
23762376
),
2377-
(
2378-
sympy.Piecewise((s0 + s1, sympy.Eq(s0, 5)), (s2, True)),
2379-
z3.If(z3.IntVal(5) == z0, z0 + z1, z2),
2380-
),
23812377
]
23822378

23832379
toZ3 = SympyToZ3(validator)

0 commit comments

Comments
 (0)