Skip to content

[IR][fix] Implement copy() for graph inputs/outputs #2338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions onnxscript/ir/_core_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,16 @@ def test_take_inputs(self):
self.assertIs(self.value2.graph, self.graph)
self.assertIsNone(self.value3.graph)

def test_inputs_copy(self):
self.graph.inputs.extend([self.value1, self.value2])
inputs_copy = self.graph.inputs.copy()
self.assertEqual(inputs_copy, [self.value1, self.value2])
self.assertIsNot(inputs_copy, self.graph.inputs)
# Modifying the copy does not affect the original
inputs_copy.append(self.value3)
self.assertNotIn(self.value3, self.graph.inputs)
self.assertIn(self.value3, inputs_copy)

def test_append_to_outputs(self):
self.graph.outputs.append(self.value2)
self.assertIn(self.value2, self.graph.outputs)
Expand Down Expand Up @@ -1423,6 +1433,16 @@ def test_take_outputs(self):
self.assertIs(self.value2.graph, self.graph)
self.assertIsNone(self.value3.graph)

def test_outputs_copy(self):
self.graph.outputs.extend([self.value1, self.value2])
outputs_copy = self.graph.outputs.copy()
self.assertEqual(outputs_copy, [self.value1, self.value2])
self.assertIsNot(outputs_copy, self.graph.outputs)
# Modifying the copy does not affect the original
outputs_copy.append(self.value3)
self.assertNotIn(self.value3, self.graph.outputs)
self.assertIn(self.value3, outputs_copy)

def test_set_initializers(self):
self.graph.initializers["initializer1"] = self.value3
self.assertIn("initializer1", self.graph.initializers)
Expand Down
6 changes: 5 additions & 1 deletion onnxscript/ir/_graph_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ def clear(self) -> None:
self._maybe_unset_graph(value)
super().clear()

def copy(self) -> list[_core.Value]:
"""Return a shallow copy of the list."""
# This is a shallow copy, so the values are not copied, just the references
return self.data.copy()

def __setitem__(self, i, item) -> None:
"""Replace an input/output to the node."""
if isinstance(item, Iterable) and isinstance(i, slice):
Expand Down Expand Up @@ -124,7 +129,6 @@ def _unimplemented(self, *_args, **_kwargs):
__iadd__ = _unimplemented
__mul__ = _unimplemented
__rmul__ = _unimplemented
copy = _unimplemented


class GraphInputs(_GraphIO):
Expand Down
Loading