Skip to content

Commit 8426a13

Browse files
authored
Refresh bypassed op metadata in FuseQuantDequantToRequantizePass (pytorch#22919)
Summary: `FuseQuantDequantToRequantizePass` cancels a matched `dequantize`/`quantize` pair even when a bypassable op such as `view_copy` sits between them. The inherited `fuse()` rewires those bypassed ops onto the producer's input without refreshing their `meta["val"]`, so they keep the dtype of the operand they no longer consume and the edge verifier rejects the graph: ``` SpecViolationError: These operators are taking Tensor inputs with mismatched dtypes: Operator: <EdgeOpOverload: aten.view_copy.default>: {'self': torch.float32, '__ret_0': torch.int8} ``` A pass-local `ExportPass` replay used to hide this by recomputing metadata for the whole graph. pytorch#22156 removed those replays and added bounded metadata recompute to the passes it converted; this path was missed. The pattern arises when an earlier pass legitimately postpones a dequantize below a reshape. The reshape then carries quantized data and is correctly retyped to `int8`; this pass later removes the quantize that justified that dtype, and does not put it back. ## Change Override `fuse()` to capture the bypassed ops before the rewire and recompute their `meta["val"]` after it. - `_get_bypassed_nodes` walks the producer's users, skipping the fuse candidates, then emits the set in graph-list order. fx keeps that list topologically sorted, so each node is refreshed after its predecessor, and the walk stops once the set is drained -- it costs the chain's span, not the graph. - The refresh sits after the rewire and before the `get_fused_node` loop, because `get_fused_node` reads `consumer.args[0].meta["val"]` when it builds the requantize node. `FuseOpPairsAcrossBranchesPass` is deliberately left alone. It is shared with `FuseTransposeOrPermuteOpPairsPass` and the Arm backend, and the dtype change here is specific to cancelling a quantize/dequantize pair. No qparams are read or written, no node is added or removed, and no edge is rewired beyond what `fuse()` already did -- only a recorded dtype is corrected to match the input the node now has. Authored with assistance from Claude Code. Differential Revision: D120466515 Pull Request resolved: pytorch#22919
1 parent 3fd80c7 commit 8426a13

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎backends/cadence/aot/fuse_ops.py‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
from executorch.exir.pass_base import PassResult
5050
from executorch.exir.passes.cse_pass import CSEPass
5151
from torch.nn.utils.fusion import fuse_conv_bn_weights
52+
from torch.utils import _pytree as pytree
5253

5354

5455
def get_tensor_arg(node: torch.fx.Node, arg_name: str) -> torch.Tensor:
@@ -683,6 +684,58 @@ def check_ok_to_fuse(
683684
return False
684685
return True
685686

687+
def _get_bypassed_nodes(
688+
self,
689+
producer: torch.fx.Node,
690+
removal_candidates: list[torch.fx.Node],
691+
) -> list[torch.fx.Node]:
692+
"""Return the bypassed ops between producer and consumers, in order."""
693+
candidates = set(removal_candidates)
694+
bypassed: set[torch.fx.Node] = set()
695+
stack = list(producer.users)
696+
while stack:
697+
user = stack.pop()
698+
if user in candidates or user in bypassed:
699+
continue
700+
bypassed.add(user)
701+
stack.extend(user.users)
702+
703+
# The graph's node list is topologically sorted, so walking it forward
704+
# from the producer refreshes each node after its predecessor.
705+
ordered: list[torch.fx.Node] = []
706+
cursor = producer.next
707+
while bypassed and cursor.op != "root":
708+
if cursor in bypassed:
709+
bypassed.remove(cursor)
710+
ordered.append(cursor)
711+
cursor = cursor.next
712+
return ordered
713+
714+
def fuse(
715+
self,
716+
node: torch.fx.Node,
717+
removal_candidates: list[torch.fx.Node],
718+
graph_module: torch.fx.GraphModule,
719+
) -> None:
720+
# Capture the bypassed ops before the rewire erases the chain's root.
721+
bypassed_nodes = self._get_bypassed_nodes(node, removal_candidates)
722+
node.replace_all_uses_with(cast(torch.fx.Node, node.args[0]))
723+
graph_module.graph.erase_node(node)
724+
# They now consume the producer's input, so their metadata describes an
725+
# operand they no longer have. Refresh before get_fused_node reads it.
726+
for bypassed in bypassed_nodes:
727+
args, kwargs = pytree.tree_map_only(
728+
torch.fx.Node,
729+
lambda arg: arg.meta["val"],
730+
(bypassed.args, bypassed.kwargs),
731+
)
732+
assert callable(bypassed.target)
733+
bypassed.meta["val"] = bypassed.target(*args, **kwargs)
734+
bypassed.meta["tensor_meta"] = None
735+
for rnode in removal_candidates:
736+
rnode.replace_all_uses_with(self.get_fused_node(node, rnode, graph_module))
737+
graph_module.graph.erase_node(rnode)
738+
686739
def get_fused_node(
687740
self,
688741
producer: torch.fx.Node,

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,33 @@ def test_replace_quant_view_dequant_with_requantize(self) -> None:
520520
},
521521
)
522522

523+
def test_quant_view_dequant_fusion_refreshes_bypassed_meta(self) -> None:
524+
builder = GraphBuilder()
525+
x = builder.placeholder("x", torch.randn(2, 12, 1, 6, dtype=torch.float32))
526+
quant = builder.call_operator(
527+
op=exir_ops.edge.quantized_decomposed.quantize_per_tensor.default,
528+
args=(x, 1.2, 3, 0, 127, torch.int8),
529+
)
530+
view = builder.call_operator(
531+
op=exir_ops.edge.aten.view_copy.default, args=(quant, [-1])
532+
)
533+
dequant = builder.call_operator(
534+
op=exir_ops.edge.quantized_decomposed.dequantize_per_tensor.default,
535+
args=(view, 1.2, 3, 0, 127, torch.int8),
536+
)
537+
builder.output([dequant])
538+
original_graph = builder.get_graph_module()
539+
540+
p = FuseQuantDequantToRequantizePass()
541+
converted_graph = cast(PassResult, p(original_graph)).graph_module
542+
543+
# The view now consumes the quantize's float input, so its metadata must
544+
# follow; a stale int8 val fails the edge verifier's dtype check.
545+
(view_node,) = converted_graph.graph.find_nodes(
546+
op="call_function", target=exir_ops.edge.aten.view_copy.default
547+
)
548+
self.assertEqual(view_node.meta["val"].dtype, torch.float32)
549+
523550
def test_replace_dequant_quant_with_requantize(self) -> None:
524551
builder = GraphBuilder()
525552
x_input = torch.randint(low=0, high=5, size=(2, 12, 1, 6), dtype=torch.int8)

0 commit comments

Comments
 (0)