Skip to content

Commit 60cb889

Browse files
authored
Arm backend: Fix corner case in convert int64 pass (#22342)
Range propagation examines every call-function node while following bounded argmax/argmin outputs. It queried fake-tensor metadata before checking whether a node was supported, so generated cast nodes without meta["val"] raised a KeyError during Stable Diffusion lowering. cc @digantdesai @freddan80 @per @zingo @oscarandersson8218 @Sebastian-Larsson @robell @rascani Signed-off-by: Måns Nilsson <[email protected]>
1 parent 4866659 commit 60cb889

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎backends/arm/_passes/convert_int64_output_ops_to_int32.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ def _infer_binary_range(
242242
ranges: Dict[torch.fx.Node, Tuple[int, int]],
243243
graph_module: torch.fx.GraphModule,
244244
) -> Optional[Tuple[int, int]]:
245+
if node.target not in self.aten_index_binary_ops + self.edge_index_binary_ops:
246+
return None
245247
if get_first_fake_tensor(node).dtype != torch.int64 or len(node.args) < 2:
246248
return None
247249
lhs = self._operand_range(node.args[0], ranges, graph_module)
@@ -482,7 +484,7 @@ def call(self, graph_module: torch.fx.GraphModule):
482484
for node in list(graph.nodes):
483485
if node.op != "call_function":
484486
continue
485-
if node.target not in self.aten_ops + self.edge_ops:
487+
if node.target not in self.aten_ops + self.edge_ops or not node.users:
486488
continue
487489
output_dtype = get_first_fake_tensor(node).dtype
488490
if output_dtype != torch.int64:

‎backends/arm/test/passes/test_convert_int64_output_ops_to_int32.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,45 @@ def forward(self, x: torch.Tensor):
221221
assert result.graph_module(torch.randn(2, 8))[0].dtype == torch.int64
222222

223223

224+
def test_arg_op_ignores_unrelated_node_without_value_metadata():
225+
from torch._subclasses import FakeTensorMode
226+
227+
graph = Graph()
228+
with FakeTensorMode():
229+
fake_input = torch.empty(2, 8)
230+
fake_output = torch.empty(2, dtype=torch.int64)
231+
x = graph.placeholder("x")
232+
x.meta["val"] = fake_input
233+
argmax = graph.call_function(torch.ops.aten.argmax.default, (x, 1))
234+
argmax.meta["val"] = fake_output
235+
graph.call_function(
236+
exir_ops.edge.dim_order_ops._to_dim_order_copy.default,
237+
(x,),
238+
{"dtype": torch.int64},
239+
)
240+
graph.output(argmax)
241+
graph_module = GraphModule(torch.nn.Module(), graph)
242+
243+
result = ConvertInt64OutputOpsToInt32Pass().call(graph_module)
244+
245+
assert not result.modified
246+
247+
248+
def test_live_supported_node_without_value_metadata_raises():
249+
graph = Graph()
250+
x = graph.placeholder("x")
251+
cast = graph.call_function(
252+
exir_ops.edge.dim_order_ops._to_dim_order_copy.default,
253+
(x,),
254+
{"dtype": torch.int64},
255+
)
256+
graph.output(cast)
257+
graph_module = GraphModule(torch.nn.Module(), graph)
258+
259+
with pytest.raises(KeyError, match="val"):
260+
ConvertInt64OutputOpsToInt32Pass().call(graph_module)
261+
262+
224263
##############################################################
225264
## Test on_overflow range check for argmax/argmin ##
226265
##############################################################

0 commit comments

Comments
 (0)