Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
f4da452
tracked list
justinchuby May 7, 2025
e64efe1
[IR] Record owning graph for input/output and initializers
justinchuby May 7, 2025
d95d276
GraphOutputs
justinchuby May 7, 2025
89ef173
format
justinchuby May 7, 2025
3c859c9
no init
justinchuby May 7, 2025
eaf0ca6
owning_graph
justinchuby May 7, 2025
510d0b9
quote the type
justinchuby May 7, 2025
a5ac719
# pylint: disable=protected-access
justinchuby May 7, 2025
24c7a42
core
justinchuby May 7, 2025
f626f07
init
justinchuby May 7, 2025
e0e6f0a
Update onnxscript/ir/_core.py
justinchuby May 7, 2025
e80de25
GraphInitializers
justinchuby May 8, 2025
b6a0fe0
owning_graph
justinchuby May 8, 2025
4b48d0d
docs
justinchuby May 8, 2025
847b48c
quote
justinchuby May 8, 2025
8e72931
syntax
justinchuby May 8, 2025
6cf7883
Rename
justinchuby May 8, 2025
41db1b2
Rename to graph to match node
justinchuby May 8, 2025
078074e
wip tests
justinchuby May 8, 2025
45898a3
Fix graph
justinchuby May 8, 2025
f1b330c
test
justinchuby May 8, 2025
6c76fb3
Check
justinchuby May 8, 2025
a4e2fc7
More tests
justinchuby May 8, 2025
751db58
wip
justinchuby May 8, 2025
66dfdb2
Data structures
justinchuby May 8, 2025
6431711
tests
justinchuby May 8, 2025
8a1635d
Apply suggestions from code review
justinchuby May 8, 2025
1108dc0
logger
justinchuby May 8, 2025
e6aa051
Fix if
justinchuby May 8, 2025
cb226dd
Fix tests
justinchuby May 8, 2025
91992b0
logger
justinchuby May 8, 2025
3739ded
Merge branch 'main' into justinchu/tracked-lists-2
justinchuby May 8, 2025
f467daf
Fix __getitem__
justinchuby May 8, 2025
9397c46
Use booleans
justinchuby May 8, 2025
4c3afc8
test
justinchuby May 8, 2025
42d678c
ref counter
justinchuby May 8, 2025
0933963
RuntimeError
justinchuby May 8, 2025
22096b4
test
justinchuby May 8, 2025
2f62c50
Fix constant lifting
justinchuby May 8, 2025
de6ad6f
Update onnxscript/ir/_graph_containers.py
justinchuby May 8, 2025
added12
Fix test
justinchuby May 9, 2025
dc0b8e2
typing
justinchuby May 9, 2025
6964109
Merge branch 'main' into justinchu/tracked-lists-2
justinchuby May 9, 2025
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
31 changes: 20 additions & 11 deletions onnxscript/ir/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
Generic,
Iterable,
Iterator,
MutableSequence,
NamedTuple,
OrderedDict,
Sequence,
Expand All @@ -50,6 +51,7 @@
_metadata,
_name_authority,
_protocols,
_tracked_containers,
_type_casting,
)

Expand Down Expand Up @@ -1757,6 +1759,9 @@

__slots__ = (
"_const_value",
"_graph_initializer_of",
Comment thread
justinchuby marked this conversation as resolved.
Outdated
"_graph_input_of",
"_graph_output_of",
"_index",
"_metadata",
"_metadata_props",
Expand Down Expand Up @@ -1808,6 +1813,12 @@
self._uses: dict[Usage, None] = {}
self.doc_string = doc_string

# The graph this value belongs to. It is set *only* when the value is added as
# a graph input or a graph output.
# The two properties can only be set by the Graph class (GraphIO).
Comment thread
justinchuby marked this conversation as resolved.
Outdated
Comment thread
justinchuby marked this conversation as resolved.
Outdated
self._graph_input_of: Graph | None = None
self._graph_output_of: Graph | None = None

Check warning on line 1820 in onnxscript/ir/_core.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_core.py#L1819-L1820

Added lines #L1819 - L1820 were not covered by tests

def __repr__(self) -> str:
value_name = self.name if self.name else "anonymous:" + str(id(self))
type_text = f", type={self.type!r}" if self.type is not None else ""
Expand Down Expand Up @@ -1986,15 +1997,13 @@
self._metadata_props = {}
return self._metadata_props

def is_graph_input(self) -> bool:

Check warning on line 2000 in onnxscript/ir/_core.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_core.py#L2000

Added line #L2000 was not covered by tests
"""Whether the value is an input of a graph."""
return self._graph_input_of is not None

Check warning on line 2002 in onnxscript/ir/_core.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_core.py#L2002

Added line #L2002 was not covered by tests

def is_graph_output(self) -> bool:
"""Whether the value is an output of a graph."""
if (producer := self.producer()) is None:
return False
if (graph := producer.graph) is None:
return False
# Cannot use `in` because __eq__ may be defined by subclasses, even though
# it is not recommended
return any(output is self for output in graph.outputs)
return self._graph_output_of is not None

Check warning on line 2006 in onnxscript/ir/_core.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_core.py#L2006

Added line #L2006 was not covered by tests


def Input(
Expand Down Expand Up @@ -2104,8 +2113,8 @@
self.name = name

# Private fields that are not to be accessed by any other classes
self._inputs = list(inputs)
self._outputs = list(outputs)
self._inputs = _tracked_containers.GraphInputs(self, inputs)
self._outputs = _tracked_containers.GraphOutputs(self, outputs)

Check warning on line 2117 in onnxscript/ir/_core.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_core.py#L2116-L2117

Added lines #L2116 - L2117 were not covered by tests
self._initializers = {}
for initializer in initializers:
if isinstance(initializer, str):
Expand All @@ -2131,11 +2140,11 @@
self.extend(nodes)

@property
def inputs(self) -> list[Value]:
def inputs(self) -> MutableSequence[Value]:

Check warning on line 2143 in onnxscript/ir/_core.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_core.py#L2143

Added line #L2143 was not covered by tests
return self._inputs

@property
def outputs(self) -> list[Value]:
def outputs(self) -> MutableSequence[Value]:

Check warning on line 2147 in onnxscript/ir/_core.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_core.py#L2147

Added line #L2147 was not covered by tests
return self._outputs

@property
Expand Down
168 changes: 168 additions & 0 deletions onnxscript/ir/_tracked_containers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""Tracked containers for graph."""

from __future__ import annotations

__all__ = [
"GraphInputs",
"GraphOutputs",
]

import collections
import logging
from typing import TYPE_CHECKING, Iterable, SupportsIndex

import onnxscript

if TYPE_CHECKING:
from onnxscript.ir import _core

Check warning on line 19 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L19

Added line #L19 was not covered by tests


logger = logging.getLogger(__name__)


class _GraphIO(collections.UserList[_core.Value]):
Comment thread Fixed
Comment thread Fixed
"""The inputs and outputs of a Graph."""

def __init__(self, graph: _core.Graph, initlist=None):
super().__init__(initlist)
self._graph = graph

Check warning on line 30 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L28-L30

Added lines #L28 - L30 were not covered by tests

def _check_invariance(self) -> None:

Check warning on line 32 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L32

Added line #L32 was not covered by tests
"""Check the invariance of the graph."""
raise NotImplementedError

Check warning on line 34 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L34

Added line #L34 was not covered by tests

def _set_graph(self, value: _core.Value) -> None:

Check warning on line 36 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L36

Added line #L36 was not covered by tests
"""Set the graph for the value."""
raise NotImplementedError

Check warning on line 38 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L38

Added line #L38 was not covered by tests

def _unset_graph(self, value: _core.Value) -> None:

Check warning on line 40 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L40

Added line #L40 was not covered by tests
"""Unset the graph for the value."""
raise NotImplementedError

Check warning on line 42 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L42

Added line #L42 was not covered by tests

def append(self, item: _core.Value) -> None:

Check warning on line 44 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L44

Added line #L44 was not covered by tests
"""Add a new input to the graph."""
super().append(item)
self._set_graph(item)
self._check_invariance()

Check warning on line 48 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L46-L48

Added lines #L46 - L48 were not covered by tests

def extend(self, other) -> None:

Check warning on line 50 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L50

Added line #L50 was not covered by tests
"""Extend the list of inputs or outputs."""
super().extend(other)

Check warning on line 52 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L52

Added line #L52 was not covered by tests
for item in other:
self._set_graph(item)

Check warning on line 54 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L54

Added line #L54 was not covered by tests

def insert(self, i: int, item: _core.Value) -> None:

Check warning on line 56 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L56

Added line #L56 was not covered by tests
"""Insert an input/output to the graph."""
super().insert(i, item)
self._set_graph(item)
self._check_invariance()

Check warning on line 60 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L58-L60

Added lines #L58 - L60 were not covered by tests

def pop(self, i: int = -1) -> _core.Value:

Check warning on line 62 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L62

Added line #L62 was not covered by tests
"""Remove an input/output from the graph."""
value = super().pop(i)
self._unset_graph(value)
self._check_invariance()
return value

Check warning on line 67 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L64-L67

Added lines #L64 - L67 were not covered by tests

def remove(self, item: _core.Value) -> None:

Check warning on line 69 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L69

Added line #L69 was not covered by tests
"""Remove an input/output from the graph."""
super().remove(item)
self._unset_graph(item)
self._check_invariance()

Check warning on line 73 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L71-L73

Added lines #L71 - L73 were not covered by tests

def clear(self) -> None:

Check warning on line 75 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L75

Added line #L75 was not covered by tests
"""Clear the list."""
for value in self.data:
self._unset_graph(value)
super().clear()

Check warning on line 79 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L78-L79

Added lines #L78 - L79 were not covered by tests

def __setitem__(self, i, item) -> None:

Check warning on line 81 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L81

Added line #L81 was not covered by tests
Comment thread
justinchuby marked this conversation as resolved.
Outdated
"""Replace an input/output to the node."""
if isinstance(item, Iterable) and isinstance(i, slice):
# Modify a slice of the list
for value in self.data[i]:
self._unset_graph(value)

Check warning on line 86 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L86

Added line #L86 was not covered by tests
for value in item:
self._set_graph(value)
super().__setitem__(i, item)
self._check_invariance()
return

Check warning on line 91 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L88-L91

Added lines #L88 - L91 were not covered by tests
elif isinstance(item, _core.Value) and isinstance(i, SupportsIndex):
Comment thread Fixed
# Replace a single item
self._unset_graph(self.data[i])
self._set_graph(item)
super().__setitem__(i, item)
self._check_invariance()
return

Check warning on line 98 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L94-L98

Added lines #L94 - L98 were not covered by tests

raise TypeError(f"Invalid types for __setitem__: {type(i)} and {type(item)}")

Check warning on line 100 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L100

Added line #L100 was not covered by tests


class GraphInputs(_GraphIO):

Check warning on line 103 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L103

Added line #L103 was not covered by tests
"""The inputs of a Graph."""

def __init__(self, graph: _core.Graph, initlist=None):
Comment thread Fixed
super().__init__(graph, initlist)

Check warning on line 107 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L106-L107

Added lines #L106 - L107 were not covered by tests

def _check_invariance(self) -> None:

Check warning on line 109 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L109

Added line #L109 was not covered by tests
"""Check the invariance of the graph."""
if not onnxscript.DEBUG:
return

Check warning on line 112 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L112

Added line #L112 was not covered by tests
for value in self.data:
if value._graph_input_of is self._graph:
Comment thread Fixed
continue
raise ValueError(

Check warning on line 116 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L115-L116

Added lines #L115 - L116 were not covered by tests
f"Invariance error: Value '{value}' is not an input of the graph: {self._graph!r}"
)

def _set_graph(self, value: _core.Value) -> None:

Check warning on line 120 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L120

Added line #L120 was not covered by tests
"""Set the graph for the value."""
if value._graph_input_of is not None and value._graph_input_of is not self._graph:
Comment thread Fixed
Comment thread Fixed
Comment thread Fixed
Comment thread Fixed
logger.warning(

Check warning on line 123 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L123

Added line #L123 was not covered by tests
"Value '%s' is already an input of a different graph. Overwriting",
value,
)
value._graph_input_of = self._graph

Check warning on line 127 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L127

Added line #L127 was not covered by tests
Comment thread Fixed

def _unset_graph(self, value: _core.Value) -> None:

Check warning on line 129 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L129

Added line #L129 was not covered by tests
"""Unset the graph for the value."""
if value._graph_input_of is not self._graph:
Comment thread Fixed
# The value is already added to a different graph
return
value._graph_input_of = None

Check warning on line 134 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L133-L134

Added lines #L133 - L134 were not covered by tests
Comment thread Fixed


class GraphOutputs(_GraphIO):

Check warning on line 137 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L137

Added line #L137 was not covered by tests
"""The outputs of a Graph."""

def __init__(self, graph: _core.Graph, initlist=None):
Comment thread Fixed
super().__init__(graph, initlist)

Check warning on line 141 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L140-L141

Added lines #L140 - L141 were not covered by tests

def _check_invariance(self) -> None:

Check warning on line 143 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L143

Added line #L143 was not covered by tests
"""Check the invariance of the graph."""
if not onnxscript.DEBUG:
return

Check warning on line 146 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L146

Added line #L146 was not covered by tests
for value in self.data:
if value._graph_output_of is self._graph:
Comment thread Fixed
continue
raise ValueError(

Check warning on line 150 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L149-L150

Added lines #L149 - L150 were not covered by tests
f"Invariance error: Value '{value}' is not an output of the graph: {self._graph!r}"
)

def _set_graph(self, value: _core.Value) -> None:

Check warning on line 154 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L154

Added line #L154 was not covered by tests
"""Set the graph for the value."""
if value._graph_output_of is not None and value._graph_output_of is not self._graph:
Comment thread Fixed
Comment thread Fixed
Comment thread Fixed
Comment thread Fixed
logger.warning(

Check warning on line 157 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L157

Added line #L157 was not covered by tests
"Value '%s' is already an output of a different graph. Overwriting",
value,
)
value._graph_output_of = self._graph

Check warning on line 161 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L161

Added line #L161 was not covered by tests
Comment thread Fixed

def _unset_graph(self, value: _core.Value) -> None:

Check warning on line 163 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L163

Added line #L163 was not covered by tests
"""Unset the graph for the value."""
if value._graph_output_of is not self._graph:
Comment thread Fixed
# The value is already added to a different graph
return
value._graph_output_of = None

Check warning on line 168 in onnxscript/ir/_tracked_containers.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_tracked_containers.py#L167-L168

Added lines #L167 - L168 were not covered by tests
Comment thread Fixed
Loading