Skip to content

Commit 33eee76

Browse files
committed
Avoid reused names when lifting constant tensors
Reserve names for constant tensors already present in the exported program before lifting new constants, so methods with folded tables retain distinct constants.
1 parent 1b88bb9 commit 33eee76

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

‎exir/program/_program.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ def _create_empty_etrecord():
245245
return ETRecord()
246246

247247

248+
def _unused_name(prefix: str, index: int, taken: Set[str]) -> str:
249+
while f"{prefix}{index}" in taken:
250+
index += 1
251+
return f"{prefix}{index}"
252+
253+
248254
def lift_constant_tensor_pass(ep):
249255
"""
250256
Takes an ExportedProgram and returns the ExportedProgram modified in-place,
@@ -255,6 +261,7 @@ def lift_constant_tensor_pass(ep):
255261

256262
graph_signature = ep.graph_signature
257263
buffers = list(graph_signature.buffers)
264+
taken = set(buffers) | set(ep.state_dict) | set(ep.constants)
258265

259266
fake_mode = _detect_fake_mode_from_gm(ep.graph_module)
260267

@@ -271,7 +278,10 @@ def lift_constant_tensor_pass(ep):
271278
if not isinstance(constant_tensor, torch.Tensor):
272279
continue
273280

274-
constant_tensor_fqn = f"_lifted_tensor_constant{len(buffers)}"
281+
constant_tensor_fqn = _unused_name(
282+
"_lifted_tensor_constant", len(buffers), taken
283+
)
284+
taken.add(constant_tensor_fqn)
275285

276286
with ep.graph.inserting_before(first_user_input):
277287
# Insert the constant node before the first user input

‎exir/tests/test_passes.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,6 +1716,31 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
17161716
self.assertNotIn(name, new_ep.state_dict)
17171717
self.assertIs(new_ep.state_dict["_prop_tensor_constant0"], stale)
17181718

1719+
def test_lift_constant_tensor_pass_avoids_buffer_name_collision(self) -> None:
1720+
class AddBuffer(torch.nn.Module):
1721+
def __init__(self):
1722+
super().__init__()
1723+
# A buffer named like a lifted constant, as re-exporting an
1724+
# unlifted program gives.
1725+
self.register_buffer("_lifted_tensor_constant1", torch.full((4,), 2.0))
1726+
1727+
def forward(self, x: torch.Tensor) -> torch.Tensor:
1728+
return x + self._lifted_tensor_constant1 + 3
1729+
1730+
edge = to_edge(
1731+
export(AddBuffer(), (torch.ones(4),), strict=True),
1732+
compile_config=EdgeCompileConfig(_skip_dim_order=False),
1733+
)
1734+
edge = edge.transform([ScalarToTensorPass(), RemoveMixedTypeOperators()])
1735+
exported_program = lift_constant_tensor_pass(edge.exported_program())
1736+
1737+
buffers = exported_program.graph_signature.buffers
1738+
self.assertEqual(len(buffers), len(set(buffers)))
1739+
torch.testing.assert_close(
1740+
exported_program.state_dict["_lifted_tensor_constant1"],
1741+
torch.full((4,), 2.0),
1742+
)
1743+
17191744
def test_pass_no_user_inputs(self) -> None:
17201745
class NoUserInputs(torch.nn.Module):
17211746
def __init__(self):

0 commit comments

Comments
 (0)