onnx-ir 0.0.1__py3-none-any.whl → 0.1.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of onnx-ir might be problematic. Click here for more details.
- onnx_ir/__init__.py +23 -10
- onnx_ir/{_convenience.py → _convenience/__init__.py} +40 -102
- onnx_ir/_convenience/_constructors.py +213 -0
- onnx_ir/_core.py +874 -257
- onnx_ir/_display.py +2 -2
- onnx_ir/_enums.py +107 -5
- onnx_ir/_graph_comparison.py +2 -2
- onnx_ir/_graph_containers.py +373 -0
- onnx_ir/_io.py +57 -10
- onnx_ir/_linked_list.py +15 -7
- onnx_ir/_metadata.py +4 -3
- onnx_ir/_name_authority.py +2 -2
- onnx_ir/_polyfill.py +26 -0
- onnx_ir/_protocols.py +31 -13
- onnx_ir/_tape.py +139 -32
- onnx_ir/_thirdparty/asciichartpy.py +1 -4
- onnx_ir/_type_casting.py +18 -3
- onnx_ir/{_internal/version_utils.py → _version_utils.py} +2 -29
- onnx_ir/convenience.py +4 -2
- onnx_ir/external_data.py +401 -0
- onnx_ir/passes/__init__.py +8 -2
- onnx_ir/passes/_pass_infra.py +173 -56
- onnx_ir/passes/common/__init__.py +40 -0
- onnx_ir/passes/common/_c_api_utils.py +76 -0
- onnx_ir/passes/common/clear_metadata_and_docstring.py +60 -0
- onnx_ir/passes/common/common_subexpression_elimination.py +177 -0
- onnx_ir/passes/common/constant_manipulation.py +217 -0
- onnx_ir/passes/common/inliner.py +332 -0
- onnx_ir/passes/common/onnx_checker.py +57 -0
- onnx_ir/passes/common/shape_inference.py +112 -0
- onnx_ir/passes/common/topological_sort.py +33 -0
- onnx_ir/passes/common/unused_removal.py +196 -0
- onnx_ir/serde.py +288 -124
- onnx_ir/tape.py +15 -0
- onnx_ir/tensor_adapters.py +122 -0
- onnx_ir/testing.py +197 -0
- onnx_ir/traversal.py +4 -3
- onnx_ir-0.1.1.dist-info/METADATA +53 -0
- onnx_ir-0.1.1.dist-info/RECORD +42 -0
- {onnx_ir-0.0.1.dist-info → onnx_ir-0.1.1.dist-info}/WHEEL +1 -1
- onnx_ir-0.1.1.dist-info/licenses/LICENSE +202 -0
- onnx_ir/_external_data.py +0 -323
- onnx_ir-0.0.1.dist-info/LICENSE +0 -22
- onnx_ir-0.0.1.dist-info/METADATA +0 -73
- onnx_ir-0.0.1.dist-info/RECORD +0 -26
- {onnx_ir-0.0.1.dist-info → onnx_ir-0.1.1.dist-info}/top_level.txt +0 -0
onnx_ir/tape.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Copyright (c) ONNX Project Contributors
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
"""Taping module to facilitate building IR graphs."""
|
|
4
|
+
|
|
5
|
+
# NOTE: Be *selective* about what this module exports because it is part of the public API.
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"Tape",
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
from onnx_ir._tape import Tape
|
|
14
|
+
|
|
15
|
+
Tape.__module__ = __name__
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# Copyright (c) ONNX Project Contributors
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
"""Compatible adapters implementing the TensorProtocol interface for various framework tensor types.
|
|
4
|
+
|
|
5
|
+
This module provides public classes that implement the :class:`onnx_ir.TensorProtocol`
|
|
6
|
+
interface for various tensor types from popular deep learning frameworks.
|
|
7
|
+
|
|
8
|
+
You can use these classes to create tensors and use them in the IR graph like any other tensor.
|
|
9
|
+
|
|
10
|
+
Example::
|
|
11
|
+
import torch
|
|
12
|
+
import onnx_ir as ir
|
|
13
|
+
|
|
14
|
+
# Create a PyTorch tensor
|
|
15
|
+
torch_tensor = torch.tensor([1, 2, 3])
|
|
16
|
+
|
|
17
|
+
# Wrap the PyTorch tensor in a TorchTensor object
|
|
18
|
+
ir_tensor = ir.tensor_adapters.TorchTensor(torch_tensor)
|
|
19
|
+
|
|
20
|
+
# Use the IR tensor in the graph
|
|
21
|
+
attr = ir.AttrTensor("x", ir_tensor)
|
|
22
|
+
print(attr)
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
# pylint: disable=import-outside-toplevel
|
|
26
|
+
|
|
27
|
+
# NOTE: DO NOT import any framework-specific modules here in the global namespace.
|
|
28
|
+
|
|
29
|
+
from __future__ import annotations
|
|
30
|
+
|
|
31
|
+
__all__ = [
|
|
32
|
+
"TorchTensor",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
import ctypes
|
|
36
|
+
from typing import TYPE_CHECKING, Any
|
|
37
|
+
|
|
38
|
+
import numpy.typing as npt
|
|
39
|
+
|
|
40
|
+
import onnx_ir as ir
|
|
41
|
+
from onnx_ir import _core
|
|
42
|
+
|
|
43
|
+
if TYPE_CHECKING:
|
|
44
|
+
import torch
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class TorchTensor(_core.Tensor):
|
|
48
|
+
def __init__(
|
|
49
|
+
self, tensor: torch.Tensor, name: str | None = None, doc_string: str | None = None
|
|
50
|
+
):
|
|
51
|
+
# Pass the tensor as the raw data to ir.Tensor's constructor
|
|
52
|
+
import torch
|
|
53
|
+
|
|
54
|
+
_TORCH_DTYPE_TO_ONNX: dict[torch.dtype, ir.DataType] = {
|
|
55
|
+
torch.bfloat16: ir.DataType.BFLOAT16,
|
|
56
|
+
torch.bool: ir.DataType.BOOL,
|
|
57
|
+
torch.complex128: ir.DataType.COMPLEX128,
|
|
58
|
+
torch.complex64: ir.DataType.COMPLEX64,
|
|
59
|
+
torch.float16: ir.DataType.FLOAT16,
|
|
60
|
+
torch.float32: ir.DataType.FLOAT,
|
|
61
|
+
torch.float64: ir.DataType.DOUBLE,
|
|
62
|
+
torch.float8_e4m3fn: ir.DataType.FLOAT8E4M3FN,
|
|
63
|
+
torch.float8_e4m3fnuz: ir.DataType.FLOAT8E4M3FNUZ,
|
|
64
|
+
torch.float8_e5m2: ir.DataType.FLOAT8E5M2,
|
|
65
|
+
torch.float8_e5m2fnuz: ir.DataType.FLOAT8E5M2FNUZ,
|
|
66
|
+
torch.int16: ir.DataType.INT16,
|
|
67
|
+
torch.int32: ir.DataType.INT32,
|
|
68
|
+
torch.int64: ir.DataType.INT64,
|
|
69
|
+
torch.int8: ir.DataType.INT8,
|
|
70
|
+
torch.uint8: ir.DataType.UINT8,
|
|
71
|
+
torch.uint16: ir.DataType.UINT16,
|
|
72
|
+
torch.uint32: ir.DataType.UINT32,
|
|
73
|
+
torch.uint64: ir.DataType.UINT64,
|
|
74
|
+
}
|
|
75
|
+
super().__init__(
|
|
76
|
+
tensor, dtype=_TORCH_DTYPE_TO_ONNX[tensor.dtype], name=name, doc_string=doc_string
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
def numpy(self) -> npt.NDArray:
|
|
80
|
+
import torch
|
|
81
|
+
|
|
82
|
+
self.raw: torch.Tensor
|
|
83
|
+
if self.dtype == ir.DataType.BFLOAT16:
|
|
84
|
+
return self.raw.view(torch.uint16).numpy(force=True).view(self.dtype.numpy())
|
|
85
|
+
if self.dtype in {
|
|
86
|
+
ir.DataType.FLOAT8E4M3FN,
|
|
87
|
+
ir.DataType.FLOAT8E4M3FNUZ,
|
|
88
|
+
ir.DataType.FLOAT8E5M2,
|
|
89
|
+
ir.DataType.FLOAT8E5M2FNUZ,
|
|
90
|
+
}:
|
|
91
|
+
return self.raw.view(torch.uint8).numpy(force=True).view(self.dtype.numpy())
|
|
92
|
+
|
|
93
|
+
return self.raw.numpy(force=True)
|
|
94
|
+
|
|
95
|
+
def __array__(self, dtype: Any = None, copy: bool | None = None) -> npt.NDArray:
|
|
96
|
+
del copy # Unused, but needed for the signature
|
|
97
|
+
if dtype is None:
|
|
98
|
+
return self.numpy()
|
|
99
|
+
return self.numpy().__array__(dtype)
|
|
100
|
+
|
|
101
|
+
def tobytes(self) -> bytes:
|
|
102
|
+
# Implement tobytes to support native PyTorch types so we can use types like bloat16
|
|
103
|
+
# Reading from memory directly is also more efficient because
|
|
104
|
+
# it avoids copying to a NumPy array
|
|
105
|
+
import torch._subclasses.fake_tensor
|
|
106
|
+
|
|
107
|
+
with torch._subclasses.fake_tensor.unset_fake_temporarily(): # pylint: disable=protected-access
|
|
108
|
+
# Disable any fake mode so calling detach() etc. will return a real tensor
|
|
109
|
+
tensor = self.raw.detach().cpu().contiguous()
|
|
110
|
+
|
|
111
|
+
if isinstance(tensor, torch._subclasses.fake_tensor.FakeTensor): # pylint: disable=protected-access
|
|
112
|
+
raise TypeError(
|
|
113
|
+
f"Cannot take content out from the FakeTensor ('{self.name}'). Please replace the tensor "
|
|
114
|
+
"with a tensor backed by real data using ONNXProgram.apply_weights() "
|
|
115
|
+
"or save the model without initializers by setting include_initializers=False."
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
return bytes(
|
|
119
|
+
(ctypes.c_ubyte * tensor.element_size() * tensor.numel()).from_address(
|
|
120
|
+
tensor.data_ptr()
|
|
121
|
+
)
|
|
122
|
+
)
|
onnx_ir/testing.py
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
# Copyright (c) ONNX Project Contributors
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
"""Utilities for testing."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"assert_onnx_proto_equal",
|
|
9
|
+
]
|
|
10
|
+
|
|
11
|
+
import difflib
|
|
12
|
+
import math
|
|
13
|
+
from collections.abc import Collection, Sequence
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
import google.protobuf.message
|
|
17
|
+
import onnx
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _opset_import_key(opset_import: onnx.OperatorSetIdProto) -> tuple[str, int]:
|
|
21
|
+
return (opset_import.domain, opset_import.version)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _value_info_key(value_info: onnx.ValueInfoProto) -> str:
|
|
25
|
+
return value_info.name
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _function_key(function: onnx.FunctionProto) -> tuple[str, str, str]:
|
|
29
|
+
return (function.domain, function.name, getattr(function, "overload", ""))
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _find_duplicates(with_duplicates: Collection[Any]) -> list[Any]:
|
|
33
|
+
"""Return a list of duplicated elements in a collection."""
|
|
34
|
+
seen = set()
|
|
35
|
+
duplicates = []
|
|
36
|
+
for x in with_duplicates:
|
|
37
|
+
if x in seen:
|
|
38
|
+
duplicates.append(x)
|
|
39
|
+
seen.add(x)
|
|
40
|
+
return duplicates
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def assert_onnx_proto_equal(
|
|
44
|
+
actual: google.protobuf.message.Message | Any,
|
|
45
|
+
expected: google.protobuf.message.Message | Any,
|
|
46
|
+
ignore_initializer_value_proto: bool = False,
|
|
47
|
+
) -> None:
|
|
48
|
+
"""Assert that two ONNX protos are equal.
|
|
49
|
+
|
|
50
|
+
Equality is defined as having the same fields with the same values. When
|
|
51
|
+
a field takes the default value, it is considered equal to the field
|
|
52
|
+
not being set.
|
|
53
|
+
|
|
54
|
+
Sequential fields with name `opset_import`, `value_info`, and `functions` are
|
|
55
|
+
compared disregarding the order of their elements.
|
|
56
|
+
|
|
57
|
+
Args:
|
|
58
|
+
actual: The first ONNX proto.
|
|
59
|
+
expected: The second ONNX proto.
|
|
60
|
+
ignore_initializer_value_proto: Ignore value protos for initializers if there
|
|
61
|
+
are extra ones in the actual proto.
|
|
62
|
+
"""
|
|
63
|
+
assert type(actual) is type(expected), (
|
|
64
|
+
f"Type not equal: {type(actual)} != {type(expected)}"
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
a_fields = {field.name: value for field, value in actual.ListFields()}
|
|
68
|
+
b_fields = {field.name: value for field, value in expected.ListFields()}
|
|
69
|
+
all_fields = sorted(set(a_fields.keys()) | set(b_fields.keys()))
|
|
70
|
+
if isinstance(actual, onnx.GraphProto) and isinstance(expected, onnx.GraphProto):
|
|
71
|
+
actual_initializer_names = {i.name for i in actual.initializer}
|
|
72
|
+
expected_initializer_names = {i.name for i in expected.initializer}
|
|
73
|
+
else:
|
|
74
|
+
actual_initializer_names = set()
|
|
75
|
+
expected_initializer_names = set()
|
|
76
|
+
|
|
77
|
+
# Record and report all errors
|
|
78
|
+
errors = []
|
|
79
|
+
for field in all_fields: # pylint: disable=too-many-nested-blocks
|
|
80
|
+
# Obtain the default value if the field is not set. This way we can compare the two fields.
|
|
81
|
+
a_value = getattr(actual, field)
|
|
82
|
+
b_value = getattr(expected, field)
|
|
83
|
+
if (
|
|
84
|
+
isinstance(a_value, Sequence)
|
|
85
|
+
and isinstance(b_value, Sequence)
|
|
86
|
+
and not isinstance(a_value, (str, bytes))
|
|
87
|
+
and not isinstance(b_value, (str, bytes))
|
|
88
|
+
):
|
|
89
|
+
# Check length first
|
|
90
|
+
a_keys: list[Any] = []
|
|
91
|
+
b_keys: list[Any] = []
|
|
92
|
+
if field == "opset_import":
|
|
93
|
+
a_value = sorted(a_value, key=_opset_import_key)
|
|
94
|
+
b_value = sorted(b_value, key=_opset_import_key)
|
|
95
|
+
a_keys = [_opset_import_key(opset_import) for opset_import in a_value]
|
|
96
|
+
b_keys = [_opset_import_key(opset_import) for opset_import in b_value]
|
|
97
|
+
elif field == "value_info":
|
|
98
|
+
if (
|
|
99
|
+
ignore_initializer_value_proto
|
|
100
|
+
and isinstance(actual, onnx.GraphProto)
|
|
101
|
+
and isinstance(expected, onnx.GraphProto)
|
|
102
|
+
):
|
|
103
|
+
# Filter out initializers from the value_info list
|
|
104
|
+
a_value = [
|
|
105
|
+
value_info
|
|
106
|
+
for value_info in a_value
|
|
107
|
+
if value_info.name not in actual_initializer_names
|
|
108
|
+
]
|
|
109
|
+
b_value = [
|
|
110
|
+
value_info
|
|
111
|
+
for value_info in b_value
|
|
112
|
+
if value_info.name not in expected_initializer_names
|
|
113
|
+
]
|
|
114
|
+
a_value = sorted(a_value, key=_value_info_key)
|
|
115
|
+
b_value = sorted(b_value, key=_value_info_key)
|
|
116
|
+
a_keys = [_value_info_key(value_info) for value_info in a_value]
|
|
117
|
+
b_keys = [_value_info_key(value_info) for value_info in b_value]
|
|
118
|
+
elif field == "functions":
|
|
119
|
+
a_value = sorted(a_value, key=_function_key)
|
|
120
|
+
b_value = sorted(b_value, key=_function_key)
|
|
121
|
+
a_keys = [_function_key(functions) for functions in a_value]
|
|
122
|
+
b_keys = [_function_key(functions) for functions in b_value]
|
|
123
|
+
|
|
124
|
+
if a_keys != b_keys:
|
|
125
|
+
keys_only_in_actual = set(a_keys) - set(b_keys)
|
|
126
|
+
keys_only_in_expected = set(b_keys) - set(a_keys)
|
|
127
|
+
error_message = (
|
|
128
|
+
f"Field {field} not equal: keys_only_in_actual={keys_only_in_actual}, keys_only_in_expected={keys_only_in_expected}. "
|
|
129
|
+
f"Field type: {type(a_value)}. "
|
|
130
|
+
f"Duplicated a_keys: {_find_duplicates(a_keys)}, duplicated b_keys: {_find_duplicates(b_keys)}"
|
|
131
|
+
)
|
|
132
|
+
errors.append(error_message)
|
|
133
|
+
elif len(a_value) != len(b_value):
|
|
134
|
+
error_message = (
|
|
135
|
+
f"Field {field} not equal: len(a)={len(a_value)}, len(b)={len(b_value)} "
|
|
136
|
+
f"Field type: {type(a_value)}"
|
|
137
|
+
)
|
|
138
|
+
errors.append(error_message)
|
|
139
|
+
else:
|
|
140
|
+
# Check every element
|
|
141
|
+
for i in range(len(a_value)): # pylint: disable=consider-using-enumerate
|
|
142
|
+
actual_value_i = a_value[i]
|
|
143
|
+
expected_value_i = b_value[i]
|
|
144
|
+
if isinstance(
|
|
145
|
+
actual_value_i, google.protobuf.message.Message
|
|
146
|
+
) and isinstance(expected_value_i, google.protobuf.message.Message):
|
|
147
|
+
try:
|
|
148
|
+
assert_onnx_proto_equal(
|
|
149
|
+
actual_value_i,
|
|
150
|
+
expected_value_i,
|
|
151
|
+
ignore_initializer_value_proto=ignore_initializer_value_proto,
|
|
152
|
+
)
|
|
153
|
+
except AssertionError as e:
|
|
154
|
+
error_message = f"Field {field} index {i} in sequence not equal. type(actual_value_i): {type(actual_value_i)}, type(expected_value_i): {type(expected_value_i)}, actual_value_i: {actual_value_i}, expected_value_i: {expected_value_i}"
|
|
155
|
+
error_message = (
|
|
156
|
+
str(e) + "\n\nCaused by the above error\n\n" + error_message
|
|
157
|
+
)
|
|
158
|
+
errors.append(error_message)
|
|
159
|
+
elif actual_value_i != expected_value_i:
|
|
160
|
+
if (
|
|
161
|
+
isinstance(actual_value_i, float)
|
|
162
|
+
and isinstance(expected_value_i, float)
|
|
163
|
+
and math.isnan(actual_value_i)
|
|
164
|
+
and math.isnan(expected_value_i)
|
|
165
|
+
):
|
|
166
|
+
# Consider NaNs equal
|
|
167
|
+
continue
|
|
168
|
+
error_message = f"Field {field} index {i} in sequence not equal. type(actual_value_i): {type(actual_value_i)}, type(expected_value_i): {type(expected_value_i)}"
|
|
169
|
+
for line in difflib.ndiff(
|
|
170
|
+
str(actual_value_i).splitlines(),
|
|
171
|
+
str(expected_value_i).splitlines(),
|
|
172
|
+
):
|
|
173
|
+
error_message += "\n" + line
|
|
174
|
+
errors.append(error_message)
|
|
175
|
+
elif isinstance(a_value, google.protobuf.message.Message) and isinstance(
|
|
176
|
+
b_value, google.protobuf.message.Message
|
|
177
|
+
):
|
|
178
|
+
assert_onnx_proto_equal(
|
|
179
|
+
a_value, b_value, ignore_initializer_value_proto=ignore_initializer_value_proto
|
|
180
|
+
)
|
|
181
|
+
elif a_value != b_value:
|
|
182
|
+
if (
|
|
183
|
+
isinstance(a_value, float)
|
|
184
|
+
and isinstance(b_value, float)
|
|
185
|
+
and math.isnan(a_value)
|
|
186
|
+
and math.isnan(b_value)
|
|
187
|
+
):
|
|
188
|
+
# Consider NaNs equal
|
|
189
|
+
continue
|
|
190
|
+
error_message = (
|
|
191
|
+
f"Field {field} not equal. field_actual: {a_value}, field_expected: {b_value}"
|
|
192
|
+
)
|
|
193
|
+
errors.append(error_message)
|
|
194
|
+
if errors:
|
|
195
|
+
raise AssertionError(
|
|
196
|
+
f"Protos not equal: {type(actual)} != {type(expected)}\n" + "\n".join(errors)
|
|
197
|
+
)
|
onnx_ir/traversal.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
# Copyright (c)
|
|
2
|
-
#
|
|
1
|
+
# Copyright (c) ONNX Project Contributors
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
"""Utilities for traversing the IR graph."""
|
|
4
4
|
|
|
5
5
|
from __future__ import annotations
|
|
@@ -8,7 +8,8 @@ __all__ = [
|
|
|
8
8
|
"RecursiveGraphIterator",
|
|
9
9
|
]
|
|
10
10
|
|
|
11
|
-
from
|
|
11
|
+
from collections.abc import Iterator, Reversible
|
|
12
|
+
from typing import Callable, Union
|
|
12
13
|
|
|
13
14
|
from typing_extensions import Self
|
|
14
15
|
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: onnx-ir
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Efficient in-memory representation for ONNX
|
|
5
|
+
Author-email: ONNX Contributors <onnx-technical-discuss@lists.lfaidata.foundation>
|
|
6
|
+
License: Apache License v2.0
|
|
7
|
+
Project-URL: Homepage, https://onnx.ai/onnx-ir
|
|
8
|
+
Project-URL: Issues, https://github.com/onnx/onnx-ir/issues
|
|
9
|
+
Project-URL: Repository, https://github.com/onnx/onnx-ir
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
17
|
+
Requires-Python: >=3.9
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Requires-Dist: numpy
|
|
21
|
+
Requires-Dist: onnx>=1.16
|
|
22
|
+
Requires-Dist: typing_extensions>=4.10
|
|
23
|
+
Requires-Dist: ml_dtypes
|
|
24
|
+
Dynamic: license-file
|
|
25
|
+
|
|
26
|
+
# ONNX IR
|
|
27
|
+
|
|
28
|
+
[](https://pypi.org/project/onnx-ir)
|
|
29
|
+
[](https://pypi.org/project/onnx-ir)
|
|
30
|
+
[](https://github.com/astral-sh/ruff)
|
|
31
|
+
[](https://codecov.io/gh/onnx/ir-py)
|
|
32
|
+
[](https://deepwiki.com/onnx/ir-py)
|
|
33
|
+
|
|
34
|
+
An in-memory IR that supports the full ONNX spec, designed for graph construction, analysis and transformation.
|
|
35
|
+
|
|
36
|
+
## Features ✨
|
|
37
|
+
|
|
38
|
+
- Full ONNX spec support: all valid models representable by ONNX protobuf, and a subset of invalid models (so you can load and fix them).
|
|
39
|
+
- Low memory footprint: mmap'ed external tensors; unified interface for ONNX TensorProto, Numpy arrays and PyTorch Tensors etc. No tensor size limitation. Zero copies.
|
|
40
|
+
- Straightforward access patterns: Access value information and traverse the graph topology at ease.
|
|
41
|
+
- Robust mutation: Create as many iterators as you like on the graph while mutating it.
|
|
42
|
+
- Speed: Performant graph manipulation, serialization/deserialization to Protobuf.
|
|
43
|
+
- Pythonic and familiar APIs: Classes define Pythonic apis and still map to ONNX protobuf concepts in an intuitive way.
|
|
44
|
+
- No protobuf dependency: The IR does not require protobuf once the model is converted to the IR representation, decoupling from the serialization format.
|
|
45
|
+
|
|
46
|
+
## Code Organization 🗺️
|
|
47
|
+
|
|
48
|
+
- [`_protocols.py`](src/onnx_ir/_protocols.py): Interfaces defined for all entities in the IR.
|
|
49
|
+
- [`_core.py`](src/onnx_ir/_core.py): Implementation of the core entities in the IR, including `Model`, `Graph`, `Node`, `Value`, and others.
|
|
50
|
+
- [`_enums.py`](src/onnx_ir/_enums.py): Definition of the type enums that correspond to the `DataType` and `AttributeType` in `onnx.proto`.
|
|
51
|
+
- [`_name_authority.py`](src/onnx_ir/_name_authority.py): The authority for giving names to entities in the graph, used internally.
|
|
52
|
+
- [`_linked_list.py`](src/onnx_ir/_linked_list.py): The data structure as the node container in the graph that supports robust iteration and mutation. Internal.
|
|
53
|
+
- [`_metadata.py`](src/onnx_ir/_metadata.py): Metadata store for all entities in the IR.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
onnx_ir/__init__.py,sha256=0fD02tkU7-bC9BfPS68TP2500619oJ8NZyGx3CdGmVk,3352
|
|
2
|
+
onnx_ir/_core.py,sha256=7nufz-9r8J3d6R4BzmRKq0DwmWosOZp3ICNr9MfMG0E,128316
|
|
3
|
+
onnx_ir/_display.py,sha256=230bMN_hVy47Ug3HkA4o5Tf5Hr21AnBEoq5w0fxjyTs,1300
|
|
4
|
+
onnx_ir/_enums.py,sha256=zMvRvYyxOg0Rf3DCQ5Sn1TyZ5znj4NuGO-OAOKZCiDM,7880
|
|
5
|
+
onnx_ir/_graph_comparison.py,sha256=8_D1gu547eCDotEUqxfIJhUGU_Ufhfji7sfsSraOj3g,727
|
|
6
|
+
onnx_ir/_graph_containers.py,sha256=hK3R3OrQTMXF8_z9Kx1DBtJriq_NQx8MUAFy7GpTZ2U,14154
|
|
7
|
+
onnx_ir/_io.py,sha256=XmVqvM2lyX7QtXGr0KcV4bboRGTOPJ8BP4YtQ-jh4dg,3886
|
|
8
|
+
onnx_ir/_linked_list.py,sha256=PXVcbHLMXHLZ6DxZnElnJLWfhBPvYcXUxM8Y3K4J7lM,10592
|
|
9
|
+
onnx_ir/_metadata.py,sha256=lzmCaYy4kAJrPW-PSGKF4a78LisxF0hiofySNQ3Mwhg,1544
|
|
10
|
+
onnx_ir/_name_authority.py,sha256=PnoV9TRgMLussZNufWavJXosDWx5avPfldVjMWEEz18,3036
|
|
11
|
+
onnx_ir/_polyfill.py,sha256=LzAGBKQbVDlURC0tgQgaxgkYU4rESgCYnqVs-u-Vsx8,887
|
|
12
|
+
onnx_ir/_protocols.py,sha256=M29sIOAvtdlis3QtBvCQPH4pnvSwhJCQNCvs3IrN9FY,21276
|
|
13
|
+
onnx_ir/_tape.py,sha256=nEGY6VZVKuB8FDyXeYr0MTq8j7E4HKOE2yN8qpz4ia0,7007
|
|
14
|
+
onnx_ir/_type_casting.py,sha256=evx6P4A0lI_V68SfKLqTN8pH7Q8GZb0So5wvf1eKCNw,3315
|
|
15
|
+
onnx_ir/_version_utils.py,sha256=A51xvGq4I81vV4VuvDx7zc4Xe0XPSp0CTjsh_M7yX4A,2669
|
|
16
|
+
onnx_ir/convenience.py,sha256=48mqMeva9Sb39P_9IUOud8V1Zc79wZUNcQEuMv-fT-Y,871
|
|
17
|
+
onnx_ir/external_data.py,sha256=Aul9O5j7zNCayFP77sMHUU-FrUnwK9BL7mXm8wJmgHY,16511
|
|
18
|
+
onnx_ir/serde.py,sha256=xtMaSdOW_JfSkvM_cdYzVx1By6Z-R9NVsVEZNECIvL8,70131
|
|
19
|
+
onnx_ir/tape.py,sha256=4FyfAHmVhQoMsfHMYnBwP2azi6UF6b6pj--ercObqZs,350
|
|
20
|
+
onnx_ir/tensor_adapters.py,sha256=J2z0gxkxwZqBrob1pYT53lgz1XQ1r7kCxhoSZa5NHaQ,4469
|
|
21
|
+
onnx_ir/testing.py,sha256=WTrjf2joWizDWaYMJlV1KjZMQw7YmZ8NvuBTVn1uY6s,8803
|
|
22
|
+
onnx_ir/traversal.py,sha256=Z69wzYBNljn1S7PhVTYgwMftrfsdEBLoa0JYteOhLL0,2863
|
|
23
|
+
onnx_ir/_convenience/__init__.py,sha256=szllgzSyKafBsmrTFRazkxURjUYVjIEzwQRA593uSo4,14389
|
|
24
|
+
onnx_ir/_convenience/_constructors.py,sha256=nA0tytizoFhQeN6gpxVx3khJQXq-tRtIh0UBM0CdTOg,8174
|
|
25
|
+
onnx_ir/_thirdparty/asciichartpy.py,sha256=afQ0fsqko2uYRPAR4TZBrQxvCb4eN8lxZ2yDFbVQq_s,10533
|
|
26
|
+
onnx_ir/passes/__init__.py,sha256=M_Tcl_-qGSNPluFIvOoeDyh0qAwNayaYyXDS5UJUJPQ,764
|
|
27
|
+
onnx_ir/passes/_pass_infra.py,sha256=HEzxDbXjIUPVubv4pxsPTFXiCDPoiM_tPEoEH1mHO70,9560
|
|
28
|
+
onnx_ir/passes/common/__init__.py,sha256=aHjx2y7L7LJChixmKsSUCdiaTP1u-zSmcmEISduqeG4,1335
|
|
29
|
+
onnx_ir/passes/common/_c_api_utils.py,sha256=cr0vOhnZ-0lOcZV_mOS3Gn-cUK73CPzjAjfbYA-PJuQ,2891
|
|
30
|
+
onnx_ir/passes/common/clear_metadata_and_docstring.py,sha256=YwouLfsNFSaTuGd7uMOGjdvVwG9yHQTkSphUgDlM0ME,2365
|
|
31
|
+
onnx_ir/passes/common/common_subexpression_elimination.py,sha256=WMsTAI-12A3iVqptmWw0tiBmGwVKsls5VNxZEbjvp2A,6527
|
|
32
|
+
onnx_ir/passes/common/constant_manipulation.py,sha256=_fGDwn0Axl2Q8APfc2m_mLMH28T-Mc9kIlpzBXoe3q4,8779
|
|
33
|
+
onnx_ir/passes/common/inliner.py,sha256=wBoO6yXt6F1AObQjYZHMQ0wn3YH681N4HQQVyaMAYd4,13702
|
|
34
|
+
onnx_ir/passes/common/onnx_checker.py,sha256=4RdWgleYHs36pRRiUCbojkBrw80b1LX88xmj5NLclMg,1675
|
|
35
|
+
onnx_ir/passes/common/shape_inference.py,sha256=J5VWsLbx9dPwV1JTuaRBObliiVHEb978AxHq_9dOGII,3976
|
|
36
|
+
onnx_ir/passes/common/topological_sort.py,sha256=Vcu1YhBdfRX4LROr0NScjB1Pwz2DjBFD0Z_GxqaxPF8,999
|
|
37
|
+
onnx_ir/passes/common/unused_removal.py,sha256=n1Vr8kSv3HGZyxFin_Kyx79GasfmhlQRVdJ0hGeZnv0,7597
|
|
38
|
+
onnx_ir-0.1.1.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
|
|
39
|
+
onnx_ir-0.1.1.dist-info/METADATA,sha256=W3i284mv7QuWNNkjRy7x_zHEsMwgUpXvmoux6VE0vZQ,4586
|
|
40
|
+
onnx_ir-0.1.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
41
|
+
onnx_ir-0.1.1.dist-info/top_level.txt,sha256=W5tROO93YjO0XRxIdjMy4wocp-5st5GiI2ukvW7UhDo,8
|
|
42
|
+
onnx_ir-0.1.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|