Skip to content

Commit 238506a

Browse files
committed
Revert "Remove unused exported tensor constants after Cortex-M lowering (pytorch#22862)"
This reverts commit d1c0b0d.
1 parent 82ccece commit 238506a

8 files changed

Lines changed: 13 additions & 426 deletions

File tree

‎backends/cortex_m/passes/BUCK‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ fbcode_target(_kind = runtime.python_library,
5858
"//executorch/backends/transforms:convert_conv1d_to_conv2d_pass",
5959
"//executorch/backends/transforms:remove_getitem_op",
6060
"//executorch/backends/transforms:remove_permutes_around_elementwise_ops",
61-
"//executorch/backends/transforms:remove_unused_constants_pass",
6261
"//executorch/backends/transforms:replace_scalar_with_tensor",
6362
"//executorch/backends/transforms:replace_ops_with_channels_last_variants",
6463
"//executorch/backends/transforms:replace_squeeze_unsqueeze_with_view",

‎backends/cortex_m/passes/cortex_m_pass_manager.py‎

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# LICENSE file in the root directory of this source tree.
55

66

7-
import copy
87
import inspect
98
from typing import Any, Optional, Type
109

@@ -21,9 +20,6 @@
2120
from executorch.backends.transforms.remove_permutes_around_elementwise_ops import (
2221
RemovePermutesAroundElementwiseOps,
2322
)
24-
from executorch.backends.transforms.remove_unused_constants_pass import (
25-
RemoveUnusedConstantsPass,
26-
)
2723
from executorch.backends.transforms.replace_scalar_with_tensor import (
2824
ReplaceScalarWithTensorArgPass,
2925
)
@@ -38,7 +34,6 @@
3834
from executorch.exir.pass_manager import ExportedProgramPassManager, PassType
3935
from executorch.exir.program._program import _transform, lift_constant_tensor_pass
4036
from torch.export import ExportedProgram
41-
from torch.fx import GraphModule
4237

4338
from .activation_fusion_pass import ActivationFusionPass
4439
from .aten_to_cortex_m_pass import AtenToCortexMPass
@@ -57,29 +52,7 @@
5752
from .quantized_clamp_activation_pass import QuantizedClampActivationPass
5853
from .replace_quant_nodes_pass import ReplaceQuantNodesPass
5954

60-
PassClass = Type[ExportPass | ExportedProgramPassBase]
61-
62-
63-
class LiftConstantTensorsPass(ExportedProgramPassBase):
64-
def call(self, exported_program: ExportedProgram) -> ExportedProgramPassResult:
65-
# The pass manager shallow-copies programs; lifting mutates shared structures.
66-
graph = copy.deepcopy(exported_program.graph)
67-
for original, cloned in zip(exported_program.graph.nodes, graph.nodes):
68-
cloned.name = original.name
69-
graph_module = GraphModule(exported_program.graph_module, graph)
70-
graph_module.meta = exported_program.graph_module.meta.copy()
71-
exported_program._graph_module = graph_module
72-
exported_program._graph_signature = copy.deepcopy(
73-
exported_program.graph_signature
74-
)
75-
exported_program._state_dict = exported_program.state_dict.copy()
76-
77-
buffer_count = len(exported_program.graph_signature.buffers)
78-
exported_program = lift_constant_tensor_pass(exported_program)
79-
return ExportedProgramPassResult(
80-
exported_program,
81-
len(exported_program.graph_signature.buffers) != buffer_count,
82-
)
55+
PassClass = Type[ExportPass]
8356

8457

8558
class _CortexMLoweringPass(ExportedProgramPassBase):
@@ -104,6 +77,10 @@ def call(self, exported_program: ExportedProgram) -> ExportedProgramPassResult:
10477
modified |= transformed is not exported_program
10578
exported_program = transformed
10679

80+
# Passes can introduce tensor attributes that must become program inputs.
81+
buffer_count = len(exported_program.graph_signature.buffers)
82+
exported_program = lift_constant_tensor_pass(exported_program)
83+
modified |= len(exported_program.graph_signature.buffers) != buffer_count
10784
return ExportedProgramPassResult(exported_program, modified)
10885

10986

@@ -120,8 +97,6 @@ class CortexMPassManager(ExportedProgramPassManager):
12097
AtenToCortexMPass,
12198
FuseConvPaddingPass,
12299
InitializeScratchBuffersPass,
123-
LiftConstantTensorsPass,
124-
RemoveUnusedConstantsPass,
125100
]
126101

127102
explicit_layout_pass_list: list[PassClass] = [
@@ -142,13 +117,11 @@ class CortexMPassManager(ExportedProgramPassManager):
142117
AtenToCortexMPass,
143118
FuseConvPaddingPass,
144119
InitializeScratchBuffersPass,
145-
LiftConstantTensorsPass,
146-
RemoveUnusedConstantsPass,
147120
]
148121

149122
pass_list = legacy_pass_list
150123

151-
pass_list_transform_for_annotation: list[Type[ExportPass]] = [
124+
pass_list_transform_for_annotation: list[PassClass] = [
152125
DecomposeSDPAPass,
153126
ScalarsToAttributePass,
154127
ReplaceScalarWithTensorArgPass,

‎backends/cortex_m/test/targets.bzl‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ def define_common_targets(is_fbcode = False):
8282
"//executorch/backends/cortex_m/passes:cortex_passes",
8383
"//executorch/backends/cortex_m/quantizer:quantizer",
8484
"//executorch/backends/test/harness:tester",
85-
"//executorch/backends/transforms:remove_unused_constants_pass",
86-
"//executorch/exir:lib",
8785
"//executorch/exir/dialects:lib",
8886
":tester",
8987
"fbsource//third-party/pypi/pytest:pytest",

‎backends/cortex_m/test/test_explicit_layout_pipeline.py‎

Lines changed: 0 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,8 @@
55
# This source code is licensed under the BSD-style license found in the
66
# LICENSE file in the root directory of this source tree.
77

8-
import copy
9-
108
import pytest
119
import torch
12-
from executorch.backends.cortex_m.passes.cortex_m_pass_manager import (
13-
CortexMPassManager,
14-
LiftConstantTensorsPass,
15-
)
1610
from executorch.backends.cortex_m.quantizer.quantizer import CortexMQuantizer
1711
from executorch.backends.cortex_m.target_config import CortexM, CortexMTargetConfig
1812
from executorch.backends.cortex_m.test.tester import (
@@ -21,10 +15,6 @@
2115
CortexMTester,
2216
)
2317
from executorch.backends.test.harness.stages import Quantize, StageType
24-
from executorch.backends.transforms.remove_unused_constants_pass import (
25-
RemoveUnusedConstantsPass,
26-
)
27-
from executorch.exir import to_edge
2818
from executorch.exir.dialects._ops import ops as exir_ops
2919
from torch.fx import Node
3020

@@ -166,11 +156,6 @@ def test_layout_pipelines_select_distinct_spatial_operators():
166156
)
167157
explicit_program = explicit.get_artifact(StageType.RUN_PASSES).exported_program()
168158

169-
for program in (legacy_program, explicit_program):
170-
assert all(
171-
node.users for node in program.graph.nodes if node.op == "placeholder"
172-
)
173-
174159
assert _count(legacy_program, exir_ops.edge.cortex_m.quantized_conv2d.default) == 1
175160
assert (
176161
_count(
@@ -192,104 +177,6 @@ def test_layout_pipelines_select_distinct_spatial_operators():
192177
assert _count(explicit_program, exir_ops.edge.cortex_m.transpose.default) == 2
193178

194179

195-
@pytest.mark.parametrize("entry_point", ["legacy", "edge_passes", "edge_manager"])
196-
@pytest.mark.parametrize(
197-
"passes,lifted,pruned",
198-
[
199-
pytest.param([], False, False, id="empty"),
200-
pytest.param([LiftConstantTensorsPass], True, False, id="lift"),
201-
pytest.param(
202-
[LiftConstantTensorsPass, RemoveUnusedConstantsPass],
203-
True,
204-
True,
205-
id="lift_and_prune",
206-
),
207-
],
208-
)
209-
def test_constant_cleanup_respects_pass_list_and_lift_order(
210-
passes, lifted, pruned, entry_point
211-
):
212-
class Model(torch.nn.Module):
213-
def __init__(self):
214-
super().__init__()
215-
self.register_buffer("_lifted_tensor_constant0", torch.tensor([1.0]))
216-
self.register_buffer("_lifted_tensor_constant1", torch.tensor([2.0]))
217-
218-
def forward(self, input):
219-
return input + self._lifted_tensor_constant1
220-
221-
class OtherEntryPoint(torch.nn.Module):
222-
def __init__(self):
223-
super().__init__()
224-
self.register_buffer("_lifted_tensor_constant2", torch.tensor([7.0]))
225-
226-
def forward(self, x):
227-
return x + self._lifted_tensor_constant2
228-
229-
inputs = (torch.zeros(1),)
230-
edge = to_edge(torch.export.export(Model(), inputs))
231-
program = edge.exported_program()
232-
assert "_lifted_tensor_constant0" in program.graph_signature.buffers
233-
program.graph_module.register_buffer("new_tensor", torch.tensor([3.0]))
234-
output = next(node for node in program.graph.nodes if node.op == "output")
235-
original = output.args[0][0]
236-
with program.graph.inserting_before(original):
237-
constant = program.graph.get_attr("new_tensor")
238-
constant.meta = original.meta.copy()
239-
result = program.graph.call_function(
240-
exir_ops.edge.aten.add.Tensor, (original.args[1], constant)
241-
)
242-
result.meta = original.meta.copy()
243-
original.replace_input_with(original.args[1], result)
244-
program.graph_module.recompile()
245-
program.validate()
246-
247-
other = to_edge(torch.export.export(OtherEntryPoint(), inputs)).exported_program()
248-
program.state_dict.update(other.state_dict)
249-
other._state_dict = program.state_dict
250-
other.validate()
251-
original_code = program.graph_module.code
252-
original_signature = copy.deepcopy(program.graph_signature)
253-
original_state = program.state_dict.copy()
254-
program.graph_module.meta["constant_lifting_test"] = "preserved"
255-
256-
if entry_point == "edge_passes":
257-
transformed = edge.transform(
258-
[pass_cls() for pass_cls in passes]
259-
).exported_program()
260-
elif entry_point == "edge_manager":
261-
transformed = edge.transform(
262-
CortexMPassManager(passes=passes)
263-
).exported_program()
264-
else:
265-
transformed = CortexMPassManager(program, passes=passes).transform()
266-
267-
program.validate()
268-
other.validate()
269-
torch.testing.assert_close(program.module()(*inputs), torch.tensor([5.0]))
270-
torch.testing.assert_close(other.module()(*inputs), torch.tensor([7.0]))
271-
assert program.graph_module.code == original_code
272-
assert program.graph_signature == original_signature
273-
assert program.state_dict.keys() == original_state.keys()
274-
for name, tensor in original_state.items():
275-
assert program.state_dict[name] is tensor
276-
assert other.state_dict[name] is tensor
277-
transformed.validate()
278-
assert transformed.graph_signature.user_inputs == original_signature.user_inputs
279-
assert transformed.graph_module.meta["constant_lifting_test"] == "preserved"
280-
assert ("_lifted_tensor_constant0" in transformed.graph_signature.buffers) == (
281-
not pruned
282-
)
283-
assert sum(node.op == "get_attr" for node in transformed.graph.nodes) == (
284-
not lifted
285-
)
286-
assert (
287-
transformed.state_dict["_lifted_tensor_constant1"]
288-
is original_state["_lifted_tensor_constant1"]
289-
)
290-
torch.testing.assert_close(transformed.module()(*inputs), torch.tensor([5.0]))
291-
292-
293180
def test_conv1d_is_quantized_before_layout_conversion():
294181
tester = CortexMTester(Conv1d().eval(), (torch.randn(1, 2, 8),))
295182
tester.quantize(Quantize(CortexMQuantizer(use_explicit_layout=True)))

‎backends/cortex_m/test/test_pass_manager.py‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def test_empty_pass_list_reports_no_change():
142142
manager.transform()
143143

144144

145-
def test_empty_pass_list_preserves_unlifted_constants():
145+
def test_empty_pass_list_still_lifts_constants():
146146
class AddConstant(ExportPass):
147147
def call_operator(self, op, args, kwargs, meta):
148148
result = super().call_operator(op, args, kwargs, meta)
@@ -159,10 +159,9 @@ def call_operator(self, op, args, kwargs, meta):
159159
assert any(node.op == "get_attr" for node in program.graph.nodes)
160160

161161
result = CortexMPassManager(passes=[])(program)
162-
assert not result.modified
163-
assert result.exported_program.graph is program.graph
164-
assert result.exported_program.graph_signature is program.graph_signature
165-
program.validate()
166-
assert any(node.op == "get_attr" for node in program.graph.nodes)
167-
assert not program.graph_signature.buffers
168-
torch.testing.assert_close(program.module()(*inputs), torch.tensor([2.0, 4.0]))
162+
assert result.modified
163+
lowered = result.exported_program
164+
lowered.validate()
165+
assert all(node.op != "get_attr" for node in lowered.graph.nodes)
166+
assert len(lowered.graph_signature.buffers) == 1
167+
torch.testing.assert_close(lowered.module()(*inputs), torch.tensor([2.0, 4.0]))

‎backends/transforms/remove_unused_constants_pass.py‎

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

‎backends/transforms/targets.bzl‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,16 +214,6 @@ def define_common_targets():
214214
],
215215
)
216216

217-
runtime.python_library(
218-
name = "remove_unused_constants_pass",
219-
srcs = ["remove_unused_constants_pass.py"],
220-
visibility = ["PUBLIC"],
221-
deps = [
222-
"//caffe2:torch",
223-
"//executorch/exir:pass_base",
224-
],
225-
)
226-
227217
runtime.python_library(
228218
name = "mean_to_sum_div",
229219
srcs = ["mean_to_sum_div.py"],

0 commit comments

Comments
 (0)