Skip to content

Commit 9a58ce8

Browse files
authored
Enhance get_arg with generic type checking and refactor all usages
Differential Revision: D92884254 Pull Request resolved: pytorch#17358
1 parent 732225f commit 9a58ce8

4 files changed

Lines changed: 153 additions & 26 deletions

File tree

‎backends/cadence/aot/BUCK‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ fbcode_target(_kind = runtime.python_library,
8181
"pass_utils.py",
8282
],
8383
deps = [
84+
"fbsource//third-party/pypi/beartype:beartype",
8485
":utils",
8586
"//caffe2:torch",
8687
"//executorch/exir:pass_base",
@@ -175,6 +176,19 @@ fbcode_target(_kind = python_unittest,
175176
],
176177
)
177178

179+
fbcode_target(_kind = python_unittest,
180+
name = "test_pass_utils",
181+
srcs = [
182+
"tests/test_pass_utils.py",
183+
],
184+
typing = True,
185+
deps = [
186+
"fbsource//third-party/pypi/beartype:beartype",
187+
":pass_utils",
188+
"//caffe2:torch",
189+
],
190+
)
191+
178192
fbcode_target(_kind = runtime.python_library,
179193
name = "compiler_utils",
180194
srcs = [

‎backends/cadence/aot/pass_utils.py‎

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88

99
from abc import abstractmethod
1010
from dataclasses import dataclass
11-
from typing import Callable, List, Optional, Set, Type, Union
11+
from typing import Callable, List, Optional, Set, Type, TypeVar, Union
1212

1313
import torch
14+
from beartype.door import die_if_unbearable
1415
from executorch.backends.cadence.aot.utils import get_edge_overload_packet
15-
1616
from executorch.exir.dialects.edge._ops import EdgeOpOverload, EdgeOpOverloadPacket
1717
from executorch.exir.pass_base import ExportPass, PassBase, PassResult
18-
1918
from torch._ops import OpOverloadPacket
2019
from torch.fx import Node
20+
from torch.fx.node import Argument
21+
22+
T = TypeVar("T")
2123

2224

2325
# Is an overlap in tensor lifetime and storage allowed at the current opt level?
@@ -177,24 +179,50 @@ def nodes_not_adjacent_in_gm(
177179
def get_arg(
178180
node: torch.fx.Node,
179181
kwarg_name: str,
180-
) -> torch.fx.node.Argument:
182+
expected_type: Type[T] = Argument,
183+
) -> T:
181184
"""
182185
Get the arg with arg_name of the node, returns default value if not set.
186+
187+
Args:
188+
node: The FX node to extract the argument from
189+
kwarg_name: The name of the argument to extract
190+
expected_type: Optional type to validate and cast the argument to.
191+
If provided, asserts the argument is an instance of this type.
192+
193+
Returns:
194+
The argument value, optionally type-checked and cast to expected_type
195+
196+
Example:
197+
# Get a node argument with type checking
198+
conv_weight_node = get_arg(node, "weight", torch.fx.Node)
199+
200+
# Get a float argument with type checking
201+
eps = get_arg(node, "eps", float)
202+
203+
# Get an argument without type checking (returns Argument)
204+
value = get_arg(node, "some_arg")
183205
"""
184206
# Try to get the arg from kwargs first since this is faster
185207
if kwarg_name in node.kwargs:
186-
return node.kwargs[kwarg_name]
187-
188-
# If it's not found in kwargs, try to normalize the args
189-
normalized_args = node.normalized_arguments(
190-
node.graph.owning_module, normalize_to_only_use_kwargs=True
191-
)
192-
if not normalized_args:
193-
raise RuntimeError(
194-
f"get_arg: Node {node} does not support normalization of arguments"
208+
value = node.kwargs[kwarg_name]
209+
else:
210+
# If it's not found in kwargs, try to normalize the args
211+
normalized_args = node.normalized_arguments(
212+
node.graph.owning_module, normalize_to_only_use_kwargs=True
195213
)
196-
197-
return normalized_args.kwargs[kwarg_name]
214+
if not normalized_args:
215+
raise RuntimeError(
216+
f"get_arg: Node {node} does not support normalization of arguments"
217+
)
218+
value = normalized_args.kwargs[kwarg_name]
219+
220+
# Validate type using beartype's runtime type checker when a specific
221+
# type is requested (not the default Argument type alias, which contains
222+
# recursive forward references that beartype cannot resolve).
223+
if expected_type is not Argument:
224+
die_if_unbearable(value, expected_type)
225+
return value # type: ignore[return-value]
198226

199227

200228
def set_arg(

‎backends/cadence/aot/remove_ops.py‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# pyre-strict
88

99
from dataclasses import dataclass, field
10-
from typing import cast, List, Optional, Set, Type
10+
from typing import cast, List, Optional, Sequence, Set, Type
1111

1212
# Import these for the cadence function signatures.
1313
import executorch.backends.cadence.aot.ops_registrations # noqa: F401
@@ -585,7 +585,7 @@ def get_squeeze_indices(self, view_node: Node) -> List[int]:
585585
Returns the indices of the input dimensions that are squeezed in the output if
586586
view node is a squeeze. Returns an empty list otherwise.
587587
"""
588-
input_node = cast(Node, get_arg(view_node, "input"))
588+
input_node = get_arg(view_node, "input", Node)
589589
input_shape = input_node.meta["val"].shape
590590
output_shape = view_node.meta["val"].shape
591591

@@ -645,7 +645,7 @@ def handle_squeeze(self, view_node: Node, visited_view_nodes: Set[Node]) -> bool
645645
# Update the intermediate slices.
646646
for slice_node in intermediate_slices:
647647
slice_rank = len(slice_node.meta["val"].shape)
648-
slice_dim = cast(int, get_arg(slice_node, "dim"))
648+
slice_dim = get_arg(slice_node, "dim", int)
649649
if slice_dim < 0:
650650
slice_dim += slice_rank
651651
for squeeze_dim in squeeze_indices:
@@ -654,7 +654,7 @@ def handle_squeeze(self, view_node: Node, visited_view_nodes: Set[Node]) -> bool
654654
set_arg(slice_node, "dim", slice_dim)
655655

656656
# Skip the initial view node.
657-
input_node = cast(Node, get_arg(view_node, "input"))
657+
input_node = get_arg(view_node, "input", Node)
658658
view_node.replace_all_uses_with(input_node)
659659
return True
660660

@@ -755,17 +755,17 @@ def targets(self) -> list[EdgeOpOverload]:
755755
return [exir_ops.edge.aten.slice_copy.Tensor]
756756

757757
def maybe_remove_or_replace(self, node: Node) -> bool:
758-
cat_node = cast(Node, get_arg(node, "input"))
759-
slice_dim = cast(int, get_arg(node, "dim"))
760-
start_idx = cast(int, get_arg(node, "start"))
761-
end_idx = cast(int, get_arg(node, "end"))
762-
step = cast(int, get_arg(node, "step"))
758+
cat_node = get_arg(node, "input", Node)
759+
slice_dim = get_arg(node, "dim", int)
760+
start_idx = get_arg(node, "start", int)
761+
end_idx = get_arg(node, "end", int)
762+
step = get_arg(node, "step", int)
763763

764764
if cat_node.target != exir_ops.edge.aten.cat.default or step != 1:
765765
return False
766766

767767
# Make sure cat and slice happens on the same dimension.
768-
cat_dim = cast(int, get_arg(cat_node, "dim"))
768+
cat_dim = get_arg(cat_node, "dim", int)
769769
if cat_dim != slice_dim:
770770
return False
771771

@@ -781,7 +781,7 @@ def maybe_remove_or_replace(self, node: Node) -> bool:
781781
end_idx += cat_output_shape[cat_dim]
782782

783783
offset = 0
784-
for cat_input_node in cast(List[Node], get_arg(cat_node, "tensors")):
784+
for cat_input_node in get_arg(cat_node, "tensors", Sequence[Node]):
785785
cat_input_shape = cat_input_node.meta["val"].shape
786786

787787
# Check if the slice range overlaps with the cat input range.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
# pyre-strict
8+
9+
import unittest
10+
from typing import List
11+
12+
import torch
13+
from beartype.roar import BeartypeDoorHintViolation
14+
from executorch.backends.cadence.aot.pass_utils import get_arg
15+
16+
17+
class TestGetArg(unittest.TestCase):
18+
def _create_add_graph(self) -> torch.fx.GraphModule:
19+
"""Create a simple graph: output = input + 1."""
20+
21+
class AddModule(torch.nn.Module):
22+
def forward(self, x: torch.Tensor) -> torch.Tensor:
23+
return x + 1
24+
25+
m = AddModule()
26+
gm = torch.fx.symbolic_trace(m)
27+
return gm
28+
29+
def _create_graph_with_kwargs(
30+
self, **kwargs: torch.fx.node.Argument
31+
) -> tuple[torch.fx.GraphModule, torch.fx.Node]:
32+
"""Create a graph with a node that has specific kwargs."""
33+
graph = torch.fx.Graph()
34+
node = graph.create_node("call_function", torch.add, kwargs=kwargs)
35+
graph.output(node)
36+
gm = torch.fx.GraphModule(torch.nn.Module(), graph)
37+
return gm, node
38+
39+
def test_get_arg_with_int_type(self) -> None:
40+
"""Test get_arg returns the value and validates int type."""
41+
_, node = self._create_graph_with_kwargs(input=1, other=2)
42+
result = get_arg(node, "input", int)
43+
self.assertEqual(result, 1)
44+
45+
def test_get_arg_with_float_type(self) -> None:
46+
"""Test get_arg returns the value and validates float type."""
47+
_, node = self._create_graph_with_kwargs(input=3.14, other=2)
48+
result = get_arg(node, "input", float)
49+
self.assertAlmostEqual(result, 3.14)
50+
51+
def test_get_arg_with_str_type(self) -> None:
52+
"""Test get_arg returns the value and validates str type."""
53+
_, node = self._create_graph_with_kwargs(input="hello", other=2)
54+
result = get_arg(node, "input", str)
55+
self.assertEqual(result, "hello")
56+
57+
def test_get_arg_with_list_type(self) -> None:
58+
"""Test get_arg returns the value and validates list type."""
59+
_, node = self._create_graph_with_kwargs(input=[1, 2, 3], other=2)
60+
result = get_arg(node, "input", list)
61+
self.assertEqual(result, [1, 2, 3])
62+
63+
def test_get_arg_with_list_int_type(self) -> None:
64+
"""Test get_arg validates parameterized List[int] type."""
65+
_, node = self._create_graph_with_kwargs(input=[1, 2, 3], other=2)
66+
result = get_arg(node, "input", List[int])
67+
self.assertEqual(result, [1, 2, 3])
68+
69+
def test_get_arg_without_type_returns_value(self) -> None:
70+
"""Test get_arg without expected_type (default Argument) returns value."""
71+
_, node = self._create_graph_with_kwargs(input=42, other=2)
72+
result = get_arg(node, "input")
73+
self.assertEqual(result, 42)
74+
75+
def test_get_arg_type_mismatch_raises(self) -> None:
76+
"""Test get_arg raises BeartypeDoorHintViolation on type mismatch."""
77+
_, node = self._create_graph_with_kwargs(input="not_an_int", other=2)
78+
with self.assertRaises(BeartypeDoorHintViolation):
79+
get_arg(node, "input", int)
80+
81+
def test_get_arg_list_type_mismatch_raises(self) -> None:
82+
"""Test get_arg raises BeartypeDoorHintViolation when list elements mismatch."""
83+
_, node = self._create_graph_with_kwargs(input=["a", "b"], other=2)
84+
with self.assertRaises(BeartypeDoorHintViolation):
85+
get_arg(node, "input", List[int])

0 commit comments

Comments
 (0)