Skip to content

Commit a00e2a6

Browse files
authored
Back out "XNNPACK: Lift constant mul scalars for partitioning (pytorch#20515)"
Summary: Regressing stability of prod workloads. Reverting during investigation to stop crashes. Differential Revision: D120752871 Pull Request resolved: pytorch#22942
1 parent bd1a2e3 commit a00e2a6

8 files changed

Lines changed: 24 additions & 420 deletions

File tree

‎backends/xnnpack/_passes/__init__.py‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929
from executorch.backends.xnnpack._passes.fuse_activation_pass import FuseActivationPass
3030
from executorch.backends.xnnpack._passes.fuse_batch_norm import FuseBatchNormPass
3131
from executorch.backends.xnnpack._passes.insert_pad_qdq import InsertPadQDQPass
32-
from executorch.backends.xnnpack._passes.lift_constant_scalar_operands_pass import (
33-
LiftConstantScalarOperandsPass,
34-
)
3532
from executorch.backends.xnnpack._passes.prelu_reshape_pass import PReLUReshapePass
3633
from executorch.backends.xnnpack._passes.propagate_custom_meta_pass import (
3734
PropagateCustomMetaPass,
@@ -82,7 +79,6 @@ def __init__(
8279
ConvertToLinearPass,
8380
PropagateCustomMetaPass,
8481
ConvertToSDPAPass,
85-
LiftConstantScalarOperandsPass,
8682
ConstPropPass,
8783
FuseBatchNormPass,
8884
DecomposeBatchNorm,

‎backends/xnnpack/_passes/convert_to_sdpa.py‎

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
22
# All rights reserved.
3-
# Copyright 2026 Arm Limited and/or its affiliates.
43
#
54
# This source code is licensed under the BSD-style license found in the
65
# LICENSE file in the root directory of this source tree.
76

87
import logging
9-
from copy import deepcopy
108
from typing import Optional
119

1210
import torch
1311
from executorch.backends.transforms import get_shape
14-
from executorch.backends.xnnpack._passes.remove_noop_expand_copy_pass import (
15-
RemoveNoopExpandCopyPass,
16-
)
12+
1713
from executorch.backends.xnnpack._passes.xnnpack_pass import XNNPACKPass
1814
from executorch.backends.xnnpack.partition.graphs import sdpa
1915
from executorch.exir.dialects._ops import ops as exir_ops
16+
2017
from torch.fx.passes.infra.pass_base import PassResult
2118
from torch.fx.passes.utils.matcher_utils import InternalMatch, SubgraphMatcher
2219

@@ -27,31 +24,31 @@
2724
class ConvertToSDPAPass(XNNPACKPass):
2825
def get_scale(self, match: InternalMatch) -> Optional[float]:
2926
"""
30-
Return the SDPA scale recovered from the matched pre-QK^T multiplications.
27+
Returns the scale of the SDPA op.
3128
32-
The decomposition applies the square root of the attention scale before
33-
QK^T, so the extracted multiplier is squared to recover the original value.
29+
Scale: Optional[float] doesn't change the graph pattern.
30+
The default value can be calulated however we need to extract
31+
it for lowering when it is the user supplied value anyway.
3432
"""
3533
for node in match.nodes_map.values():
3634
if (
37-
node.op != "call_function"
38-
or node.target != exir_ops.edge.aten.mul.Scalar
35+
node.op == "call_function"
36+
and node.target == exir_ops.edge.aten.mul.Scalar
3937
):
40-
continue
38+
scale = node.args[1]
4139

42-
scale = node.args[1]
40+
dtype = torch.float
41+
mul_val = node.meta.get("val", None)
42+
if mul_val is not None:
43+
dtype = mul_val.dtype
4344

44-
dtype = torch.float
45-
mul_val = node.meta.get("val", None)
46-
if mul_val is not None:
47-
dtype = mul_val.dtype
45+
if isinstance(scale, float):
46+
# Convert scale value to fp16 (reducing precision)
47+
scale = torch.tensor(scale, dtype=dtype).item()
4848

49-
if isinstance(scale, float):
50-
# Convert scale value to fp16 (reducing precision)
51-
scale = torch.tensor(scale, dtype=dtype).item()
52-
53-
# since scale we extracted this before the QK^T.
54-
return scale**2
49+
# since scale we extracted this before the QK^T.
50+
return scale**2
51+
break
5552
return None
5653

5754
def assert_2d_mask(self, match: InternalMatch) -> None:
@@ -102,16 +99,11 @@ def call(self, graph_module: torch.fx.GraphModule):
10299
logger.debug("ConvertToSDPA Begin: ")
103100
logger.debug(graph_module.print_readable(print_output=False))
104101

105-
for scalar_pattern in sdpa.get_graphs():
106-
normalized_pattern = RemoveNoopExpandCopyPass()(
107-
deepcopy(scalar_pattern)
108-
).graph_module
109-
110-
for pattern in (scalar_pattern, normalized_pattern):
111-
sm = SubgraphMatcher(pattern.graph, ignore_literals=True)
112-
matches = list(sm.match(graph_module.graph))
113-
for partition_to_replace in matches:
114-
self.create_sdpa(graph_module, partition_to_replace)
102+
for pattern in sdpa.get_graphs():
103+
sm = SubgraphMatcher(pattern.graph, ignore_literals=True)
104+
matches = list(sm.match(graph_module.graph))
105+
for partition_to_replace in matches:
106+
self.create_sdpa(graph_module, partition_to_replace)
115107

116108
graph_module.recompile()
117109
graph_module = super().call(graph_module).graph_module

‎backends/xnnpack/_passes/lift_constant_scalar_operands_pass.py‎

Lines changed: 0 additions & 119 deletions
This file was deleted.

‎backends/xnnpack/partition/config/__init__.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
MeanDimConfig,
4444
MinimumConfig,
4545
MulConfig,
46-
MulScalarConfig,
4746
NegConfig,
4847
PermuteConfig,
4948
PowConfig,
@@ -108,7 +107,6 @@
108107
MinimumConfig,
109108
MMConfig,
110109
MulConfig,
111-
MulScalarConfig,
112110
NegConfig,
113111
PermuteConfig,
114112
PowConfig,

‎backends/xnnpack/partition/config/generic_node_configs.py‎

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
# pyre-unsafe
99

1010
import logging
11-
from numbers import Number
1211
from typing import cast, List, Optional
1312

1413
import numpy as np
@@ -440,38 +439,6 @@ def supported_precision_types(self) -> List[ConfigPrecisionType]:
440439
return [ConfigPrecisionType.FP32, ConfigPrecisionType.STATIC_QUANT]
441440

442441

443-
class MulScalarConfig(GenericNodePartitionerConfig):
444-
target_name = "mul.Scalar"
445-
446-
def check_constraints(self, node: torch.fx.Node, ep: ExportedProgram) -> bool:
447-
if node.graph is not ep.graph:
448-
return False
449-
450-
if not self.check_common_constraints(node, ep):
451-
return False
452-
453-
if (
454-
len(node.args) != 2
455-
or not isinstance(node.args[0], torch.fx.Node)
456-
or not isinstance(node.args[1], Number)
457-
):
458-
return False
459-
460-
input_value = node.args[0].meta.get("val")
461-
output_value = node.meta.get("val")
462-
return (
463-
isinstance(input_value, torch.Tensor)
464-
and isinstance(output_value, torch.Tensor)
465-
and input_value.dtype == output_value.dtype
466-
)
467-
468-
def supported_precision_types(self) -> List[ConfigPrecisionType]:
469-
return [ConfigPrecisionType.FP32]
470-
471-
def get_original_aten(self) -> Optional[torch._ops.OpOverload]:
472-
return torch.ops.aten.mul.Scalar
473-
474-
475442
class MaximumConfig(GenericNodePartitionerConfig):
476443
target_name = "maximum.default"
477444

‎backends/xnnpack/test/ops/test_multiply.py‎

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
22
# All rights reserved.
3-
# Copyright 2026 Arm Limited and/or its affiliates.
43
#
54
# This source code is licensed under the BSD-style license found in the
65
# LICENSE file in the root directory of this source tree.
@@ -30,10 +29,6 @@ def forward(self, x, y):
3029
z = torch.mul(x, y) * torch.functional.torch.mul(x, y)
3130
return z
3231

33-
class MulScalar(torch.nn.Module):
34-
def forward(self, x):
35-
return torch.ops.aten.mul.Scalar(x, 0.5)
36-
3732
class MulRelu(torch.nn.Module):
3833
def forward(self, x, y):
3934
z = x * y
@@ -63,23 +58,6 @@ def test_fp32_mul(self):
6358
inputs = (torch.randn((1, 3)), torch.randn((4, 3)))
6459
self._test_mul(inputs)
6560

66-
def test_fp32_mul_scalar(self):
67-
(
68-
Tester(self.MulScalar(), (torch.randn(2, 3),))
69-
.export()
70-
.to_edge_transform_and_lower()
71-
.check_count({"torch.ops.higher_order.executorch_call_delegate": 1})
72-
.check_not(
73-
[
74-
"executorch_exir_dialects_edge__ops_aten_mul_Tensor",
75-
"executorch_exir_dialects_edge__ops_aten_mul_Scalar",
76-
]
77-
)
78-
.to_executorch()
79-
.serialize()
80-
.run_method_and_compare_outputs()
81-
)
82-
8361
def test_qs8_mul(self):
8462
inputs = (torch.randn(1, 1, 4, 4), torch.randn(1, 1, 4, 1))
8563
(

0 commit comments

Comments
 (0)