Skip to content

Commit f0fcffb

Browse files
authored
Improve value renaming (#208)
Do not modify the value name if the new name is invalid. This makes the setter of Value.name safer in that things will not be left in an inconsistent state if a check fails --------- Signed-off-by: Justin Chu <[email protected]>
1 parent 49a361c commit f0fcffb

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/onnx_ir/_core.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2214,23 +2214,38 @@ def name(self) -> str | None:
22142214

22152215
@name.setter
22162216
def name(self, value: str | None) -> None:
2217-
if self._const_value is not None:
2218-
self._const_value.name = value
2219-
old_name = self._name
2220-
self._name = value
2221-
if self.is_initializer():
2217+
if self._name == value:
2218+
return
2219+
2220+
# First check if renaming is valid. Do not change anything if it is invalid
2221+
# to prevent the value from being in an inconsistent state.
2222+
is_initializer = self.is_initializer()
2223+
if is_initializer:
22222224
if value is None:
22232225
raise ValueError(
2224-
"Initializer value cannot have name set to None. Please pop() the value from initializers first"
2226+
"Initializer value cannot have name set to None. Please pop() the value from initializers first to do so."
22252227
)
2226-
# Rename the initializer entry in the graph
22272228
graph = self._graph
22282229
assert graph is not None
2229-
assert old_name is not None
22302230
if value in graph.initializers and graph.initializers[value] is not self:
22312231
raise ValueError(
2232-
f"Cannot rename initializer to '{value}': an initializer with that name already exists."
2232+
f"Cannot rename initializer '{self}' to '{value}': an initializer with that name already exists."
22332233
)
2234+
2235+
# Rename the backing constant tensor
2236+
if self._const_value is not None:
2237+
self._const_value.name = value
2238+
2239+
# Rename self
2240+
old_name = self._name
2241+
self._name = value
2242+
2243+
if is_initializer:
2244+
# Rename the initializer entry in the graph
2245+
assert value is not None, "debug: Should be guarded above"
2246+
graph = self._graph
2247+
assert graph is not None
2248+
assert old_name is not None
22342249
graph.initializers.pop(old_name)
22352250
graph.initializers[value] = self
22362251

src/onnx_ir/_core_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,9 @@ def test_initializer_name_setter_raises_when_set_to_none(self):
869869
):
870870
value.name = None
871871

872+
# Name should remain unchanged
873+
self.assertEqual(value.name, "initializer1")
874+
872875
def test_initializer_name_setter_updates_graph_initializers_dict(self):
873876
"""Test that renaming an initializer value updates the graph's initializers dictionary."""
874877
tensor = ir.tensor([1, 2, 3])

0 commit comments

Comments
 (0)