Skip to content

Commit 4cbe1c0

Browse files
authored
Skip ReplacePowWithMulPass when pow promotes the input dtype (pytorch#24177)
Summary: `pow.Tensor_Scalar(int32, 2.0)` returns float32, but the `mul.Tensor(x, x)` chain that `ReplacePowWithMulPass` emits stays int32. The pass copied the float32 `meta` onto the int32 `mul` nodes, so the lowered program's output dtype no longer matched the eager reference. The pass now gives up when the input dtype differs from the `pow` output dtype. Authored with Claude Code. Reviewed By: DrJessop Differential Revision: D127746754 Pull Request resolved: pytorch#24177
1 parent a02062d commit 4cbe1c0

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

‎backends/cadence/aot/replace_ops.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,6 +2280,11 @@ def maybe_remove_or_replace(self, node: torch.fx.Node) -> bool:
22802280
x = node.args[0]
22812281
assert isinstance(x, torch.fx.Node)
22822282

2283+
# mul keeps the input dtype, so it can't express a promoting pow
2284+
# (e.g. int32 ** 2.0 -> float32).
2285+
if x.meta["val"].dtype != node.meta["val"].dtype:
2286+
return False
2287+
22832288
graph = node.graph
22842289
result_node = x
22852290

‎backends/cadence/aot/tests/test_replace_ops_passes.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2375,6 +2375,53 @@ def test_replace_pow_with_mul_not_applied(self, exponent: float) -> None:
23752375
0,
23762376
)
23772377

2378+
@expand([[2.0], [3.0]])
2379+
def test_replace_pow_with_mul_not_applied_on_type_promotion(
2380+
self, exponent: float
2381+
) -> None:
2382+
# int32 ** float promotes to float32, but int32 * int32 stays int32.
2383+
x = torch.randint(0, 8, (2, 1, 64), dtype=torch.int32)
2384+
original_gm = single_op_builder(
2385+
placeholders=(x,),
2386+
op=exir_ops.edge.aten.pow.Tensor_Scalar,
2387+
args=(x, exponent),
2388+
)
2389+
p = ReplacePowWithMulPass()
2390+
result = cast(PassResult, p(original_gm))
2391+
2392+
self.assertFalse(result.modified)
2393+
self.assertEqual(
2394+
count_node(result.graph_module, exir_ops.edge.aten.pow.Tensor_Scalar),
2395+
1,
2396+
)
2397+
self.assertEqual(
2398+
count_node(result.graph_module, exir_ops.edge.aten.mul.Tensor), 0
2399+
)
2400+
2401+
@expand([[2], [3]])
2402+
def test_replace_pow_with_mul_int_input_int_exponent(self, exponent: int) -> None:
2403+
# int32 ** int stays int32, so the dtype guard still allows the rewrite.
2404+
x = torch.randint(0, 8, (2, 1, 64), dtype=torch.int32)
2405+
original_gm = single_op_builder(
2406+
placeholders=(x,),
2407+
op=exir_ops.edge.aten.pow.Tensor_Scalar,
2408+
args=(x, exponent),
2409+
)
2410+
gm_before = copy.deepcopy(original_gm)
2411+
p = ReplacePowWithMulPass()
2412+
result = cast(PassResult, p(original_gm))
2413+
2414+
self.assertTrue(result.modified)
2415+
validate(gm_before, result.graph_module, [x], "ReplacePowWithMulPass")
2416+
self.assertEqual(
2417+
count_node(result.graph_module, exir_ops.edge.aten.pow.Tensor_Scalar),
2418+
0,
2419+
)
2420+
self.assertEqual(
2421+
count_node(result.graph_module, exir_ops.edge.aten.mul.Tensor),
2422+
exponent - 1,
2423+
)
2424+
23782425

23792426
class TestReplaceIm2rowWithViewPass(unittest.TestCase):
23802427
def test_no_replacement_for_conv(self) -> None:

0 commit comments

Comments
 (0)