Skip to content

Commit 3b47321

Browse files
authored
Preserve full_like dtype in AOT passes (pytorch#21560)
Differential Revision: D114433185 Pull Request resolved: pytorch#21560
1 parent 4647d62 commit 3b47321

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎backends/cadence/aot/replace_ops.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1867,12 +1867,13 @@ def maybe_remove_or_replace(self, node: torch.fx.Node) -> bool:
18671867
assert isinstance(input_arg, torch.fx.Node)
18681868
shape = input_arg.meta["val"].shape
18691869
fill_value = node.args[1]
1870+
dtype = node.meta["val"].dtype
18701871

18711872
with node.graph.inserting_before(node):
18721873
new_node = node.graph.call_function(
18731874
exir_ops.edge.aten.full.default,
18741875
args=(shape, fill_value),
1875-
kwargs={},
1876+
kwargs={"dtype": dtype},
18761877
)
18771878
new_node.meta = node.meta
18781879
node.replace_all_uses_with(new_node)

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
ReplaceConvWithChannelLastConvPass,
2828
ReplaceConvWithIm2RowAndLinear,
2929
ReplaceEmptyTensorsWithFullPass,
30+
ReplaceFullLikeWithFullPass,
3031
ReplaceFunctionallyEquivalentOpTargets,
3132
ReplaceIm2RowWithViewPass,
3233
ReplaceLinearWithFullyConnectedOpPass,
@@ -122,6 +123,43 @@ def assertTargetCountsEqual(
122123
for target, expected_count in targets_and_counts:
123124
self.assertTargetCountEqual(graph_module, target, expected_count)
124125

126+
@torch.no_grad()
127+
def test_replace_full_like_preserves_output_dtype(self) -> None:
128+
builder = GraphBuilder()
129+
x_tensor = torch.randn(2, 3, dtype=torch.float32)
130+
x = builder.placeholder("x", x_tensor)
131+
full_like = builder.call_operator(
132+
op=exir_ops.edge.aten.full_like.default,
133+
args=(x, 0),
134+
)
135+
builder.output([full_like])
136+
original_gm = builder.get_graph_module()
137+
138+
gm_before = copy.deepcopy(original_gm)
139+
result = cast(PassResult, ReplaceFullLikeWithFullPass()(original_gm))
140+
self.assertTrue(result.modified)
141+
graph_after_passes = result.graph_module
142+
143+
self.assertTargetCountsEqual(
144+
graph_after_passes,
145+
[
146+
(exir_ops.edge.aten.full_like.default, 0),
147+
(exir_ops.edge.aten.full.default, 1),
148+
],
149+
)
150+
full_node = next(
151+
node
152+
for node in graph_after_passes.graph.nodes
153+
if node.target == exir_ops.edge.aten.full.default
154+
)
155+
self.assertEqual(full_node.kwargs["dtype"], torch.float32)
156+
validate(
157+
gm_before,
158+
graph_after_passes,
159+
(x_tensor,),
160+
"ReplaceFullLikeWithFullPass",
161+
)
162+
125163
@expand(
126164
[
127165
(

0 commit comments

Comments
 (0)