model-explorer-onnx 0.0.1__tar.gz

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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Justin Chu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,54 @@
1
+ Metadata-Version: 2.1
2
+ Name: model-explorer-onnx
3
+ Version: 0.0.1
4
+ Summary: Adapter for ai-edge-model-explorer to support ONNX models
5
+ Author-email: Justin Chu <justinchu@microsoft.com>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2024 Justin Chu
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Classifier: Development Status :: 3 - Alpha
29
+ Classifier: Environment :: Console
30
+ Classifier: Intended Audience :: Developers
31
+ Classifier: Operating System :: POSIX
32
+ Classifier: Operating System :: MacOS :: MacOS X
33
+ Classifier: Operating System :: Microsoft :: Windows
34
+ Classifier: Programming Language :: Python :: 3.8
35
+ Classifier: Programming Language :: Python :: 3.9
36
+ Classifier: Programming Language :: Python :: 3.10
37
+ Classifier: Programming Language :: Python :: 3.11
38
+ Classifier: Programming Language :: Python :: 3.12
39
+ Classifier: License :: OSI Approved :: MIT License
40
+ Requires-Python: >=3.8
41
+ Description-Content-Type: text/markdown
42
+ License-File: LICENSE
43
+ Requires-Dist: ai-edge-model-explorer
44
+ Requires-Dist: onnx
45
+ Requires-Dist: onnxscript
46
+
47
+ # model-explorer-onnx
48
+ ONNX Adapter for model-explorer
49
+
50
+ ## Usage
51
+
52
+ ```bash
53
+ model-explorer model.test --extensions=model-explorer-onnx
54
+ ```
@@ -0,0 +1,8 @@
1
+ # model-explorer-onnx
2
+ ONNX Adapter for model-explorer
3
+
4
+ ## Usage
5
+
6
+ ```bash
7
+ model-explorer model.test --extensions=model-explorer-onnx
8
+ ```
@@ -0,0 +1,32 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "model-explorer-onnx"
7
+ version = "0.0.1"
8
+ description = "Adapter for ai-edge-model-explorer to support ONNX models"
9
+ authors = [{ name = "Justin Chu", email = "justinchu@microsoft.com" }]
10
+ readme = "README.md"
11
+ requires-python = ">=3.8"
12
+ license = { file = "LICENSE" }
13
+ classifiers = [
14
+ "Development Status :: 3 - Alpha",
15
+ "Environment :: Console",
16
+ "Intended Audience :: Developers",
17
+ "Operating System :: POSIX",
18
+ "Operating System :: MacOS :: MacOS X",
19
+ "Operating System :: Microsoft :: Windows",
20
+ "Programming Language :: Python :: 3.8",
21
+ "Programming Language :: Python :: 3.9",
22
+ "Programming Language :: Python :: 3.10",
23
+ "Programming Language :: Python :: 3.11",
24
+ "Programming Language :: Python :: 3.12",
25
+ "License :: OSI Approved :: MIT License",
26
+ ]
27
+
28
+ dependencies = [
29
+ "ai-edge-model-explorer",
30
+ "onnx",
31
+ "onnxscript",
32
+ ]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,242 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Any, Mapping, Sequence
4
+ import model_explorer
5
+ from model_explorer import graph_builder
6
+ import onnx
7
+ from onnxscript import ir
8
+ import logging
9
+
10
+ logger = logging.getLogger(__name__)
11
+
12
+ _TENSOR_DISPLAY_LIMIT = 1024
13
+
14
+
15
+ def display_tensor(tensor: ir.TensorProtocol) -> str:
16
+ if tensor.size < _TENSOR_DISPLAY_LIMIT:
17
+ try:
18
+ return str(tensor.numpy())
19
+ except Exception as e:
20
+ logger.warning("Failed to display tensor: %s", e)
21
+ return str(tensor)
22
+ return str(tensor)
23
+
24
+
25
+ def add_inputs_metadata(onnx_node: ir.Node, node: graph_builder.GraphNode):
26
+ for i, input_value in enumerate(onnx_node.inputs):
27
+ metadata = graph_builder.MetadataItem(id=str(i), attrs=[])
28
+ if input_value is None:
29
+ metadata.attrs.append(graph_builder.KeyValue(key="__tensor_tag", value=""))
30
+ else:
31
+ metadata.attrs.append(
32
+ graph_builder.KeyValue(key="__tensor_tag", value=input_value.name or "")
33
+ )
34
+ node.inputsMetadata.append(metadata)
35
+
36
+
37
+ def add_outputs_metadata(onnx_node: ir.Node, node: graph_builder.GraphNode):
38
+ for output in onnx_node.outputs:
39
+ metadata = graph_builder.MetadataItem(id=str(output.index()), attrs=[])
40
+ type_str = str(output.type)
41
+ shape_text = str(output.shape) if output.shape is not None else "[?]"
42
+
43
+ metadata.attrs.append(
44
+ graph_builder.KeyValue(key="__tensor_tag", value=output.name or "")
45
+ )
46
+ # tensor_shape is a special key that is used to display the type and shape of the tensor
47
+ metadata.attrs.append(
48
+ graph_builder.KeyValue(key="tensor_shape", value=f"{type_str}{shape_text}")
49
+ )
50
+ node.outputsMetadata.append(metadata)
51
+
52
+
53
+ def add_node_attrs(onnx_node: ir.Node, node: graph_builder.GraphNode):
54
+ for attr in onnx_node.attributes.values():
55
+ if isinstance(attr, ir.Attr):
56
+ if attr.type == ir.AttributeType.TENSOR:
57
+ attr_value = display_tensor(attr.value)
58
+ else:
59
+ attr_value = str(attr.value)
60
+ node.attrs.append(graph_builder.KeyValue(key=attr.name, value=attr_value))
61
+ elif isinstance(attr, ir.RefAttr):
62
+ node.attrs.append(
63
+ graph_builder.KeyValue(
64
+ key=attr.name, value=f"Ref({attr.ref_attr_name})"
65
+ )
66
+ )
67
+
68
+
69
+ def add_incoming_edges(
70
+ onnx_node: ir.Node,
71
+ node: graph_builder.GraphNode,
72
+ graph_inputs: Mapping[ir.Value, int],
73
+ ):
74
+ for target_input_id, input_value in enumerate(onnx_node.inputs):
75
+ if input_value is None:
76
+ continue
77
+ if input_value in graph_inputs:
78
+ # The input is a graph input. Create an input edge.
79
+ node.incomingEdges.append(
80
+ graph_builder.IncomingEdge(
81
+ sourceNodeId=input_value.name, # type: ignore
82
+ sourceNodeOutputId=str(graph_inputs[input_value]),
83
+ targetNodeInputId=str(target_input_id),
84
+ )
85
+ )
86
+ continue
87
+ input_node = input_value.producer()
88
+ if input_node is None:
89
+ logger.warning(
90
+ "Input value %s does not have a producer. Skipping incoming edge.",
91
+ input_value,
92
+ )
93
+ continue
94
+ if not input_node.name:
95
+ logger.debug(
96
+ "Node %s does not have a name. Skipping incoming edge.", input_node
97
+ )
98
+ continue
99
+ node.incomingEdges.append(
100
+ graph_builder.IncomingEdge(
101
+ sourceNodeId=input_node.name,
102
+ sourceNodeOutputId=str(input_value.index()),
103
+ targetNodeInputId=str(target_input_id),
104
+ )
105
+ )
106
+
107
+
108
+ def create_op_label(domain: str, op_type: str) -> str:
109
+ if domain in {"", "ai.onnx"}:
110
+ return op_type
111
+ return f"{domain}::{op_type}"
112
+
113
+
114
+ def create_node(
115
+ onnx_node: ir.Node, graph_inputs: Mapping[ir.Value, int], namespace: str = ""
116
+ ) -> graph_builder.GraphNode | None:
117
+ if onnx_node.name is None:
118
+ logger.warning("Node does not have a name. Skipping node %s.", onnx_node)
119
+ return None
120
+
121
+ embedded_namespace = onnx_node.name.lstrip("/").split("/")[0:-1]
122
+ embedded_namespace = [ns or "<anonymous>" for ns in embedded_namespace]
123
+ if embedded_namespace:
124
+ namespace = namespace + "/" + "/".join(embedded_namespace)
125
+ node = graph_builder.GraphNode(
126
+ id=onnx_node.name,
127
+ label=create_op_label(onnx_node.domain, onnx_node.op_type),
128
+ namespace=namespace,
129
+ )
130
+ add_incoming_edges(onnx_node, node, graph_inputs)
131
+ add_node_attrs(onnx_node, node)
132
+ add_inputs_metadata(onnx_node, node)
133
+ add_outputs_metadata(onnx_node, node)
134
+ return node
135
+
136
+
137
+ def add_graph_io(graph: graph_builder.Graph, input_or_outputs: Sequence[ir.Value]):
138
+ for value in input_or_outputs:
139
+ node = graph_builder.GraphNode(
140
+ id=value.name, # type: ignore
141
+ label=value.name, # type: ignore
142
+ )
143
+ producer = value.producer()
144
+ if producer is not None and producer.name is not None:
145
+ node.incomingEdges.append(
146
+ graph_builder.IncomingEdge(
147
+ sourceNodeId=producer.name,
148
+ sourceNodeOutputId=str(value.index()),
149
+ targetNodeInputId="0",
150
+ )
151
+ )
152
+
153
+ graph.nodes.append(node)
154
+
155
+
156
+ def add_initializers(
157
+ graph: graph_builder.Graph, initializers: Sequence[ir.Value], namespace: str
158
+ ):
159
+ for initializer in initializers:
160
+ node = graph_builder.GraphNode(
161
+ id=initializer.name, # type: ignore
162
+ label=initializer.name, # type: ignore
163
+ namespace=namespace,
164
+ )
165
+ # Annotate the initializer node as an initializer
166
+ node.attrs.append(graph_builder.KeyValue(key="type", value="Initializer"))
167
+ # Add metadata for the output tensor
168
+ if initializer.const_value is None:
169
+ logger.warning(
170
+ "Initializer %s does not have a const value. Skipping.", initializer
171
+ )
172
+ graph.nodes.append(node)
173
+ continue
174
+ metadata = graph_builder.MetadataItem(id="0", attrs=[])
175
+ shape_text = (
176
+ str(initializer.const_value.shape)
177
+ if initializer.const_value.shape is not None
178
+ else "[?]"
179
+ )
180
+ metadata.attrs.append(
181
+ graph_builder.KeyValue(key="__tensor_tag", value=initializer.name or "")
182
+ )
183
+ # tensor_shape is a special key that is used to display the type and shape of the tensor
184
+ metadata.attrs.append(
185
+ graph_builder.KeyValue(key="tensor_shape", value=f"{initializer.const_value.dtype}{shape_text}")
186
+ )
187
+ metadata.attrs.append(
188
+ graph_builder.KeyValue(
189
+ key="value", value=display_tensor(initializer.const_value)
190
+ )
191
+ )
192
+ node.outputsMetadata.append(metadata)
193
+ graph.nodes.append(node)
194
+
195
+
196
+ def create_graph(onnx_graph: ir.Graph | ir.Function) -> graph_builder.Graph | None:
197
+ if onnx_graph.name is None:
198
+ logger.warning("Graph does not have a name. skipping graph: %s", onnx_graph)
199
+ return None
200
+ graph = graph_builder.Graph(id=onnx_graph.name or "graph", nodes=[])
201
+ graph_inputs: dict[ir.Value, int] = {
202
+ input_value: i for i, input_value in enumerate(onnx_graph.inputs)
203
+ }
204
+ for onnx_node in onnx_graph:
205
+ node = create_node(onnx_node, graph_inputs, namespace=onnx_graph.name)
206
+ if node is None:
207
+ continue
208
+ graph.nodes.append(node)
209
+ add_graph_io(graph, onnx_graph.inputs)
210
+ # Add initializers
211
+ if isinstance(onnx_graph, ir.Graph):
212
+ add_initializers(graph, list(onnx_graph.initializers.values()), onnx_graph.name)
213
+ add_graph_io(graph, onnx_graph.outputs)
214
+ return graph
215
+
216
+
217
+ class ONNXAdapter(model_explorer.Adapter):
218
+ metadata = model_explorer.AdapterMetadata(
219
+ id="onnx_adapter",
220
+ name="ONNX adapter",
221
+ description="ONNX adapter for Model Explorer",
222
+ source_repo="https://github.com/justinchuby/model_explorer_onnx",
223
+ fileExts=["onnx", "onnxtext", "onnxtxt"],
224
+ )
225
+
226
+ def convert(
227
+ self, model_path: str, settings: dict[str, Any]
228
+ ) -> model_explorer.ModelExplorerGraphs:
229
+ # TODO: Add shape inference and tensor display size as settings
230
+ onnx_model = onnx.load(model_path)
231
+ onnx_model = onnx.shape_inference.infer_shapes(onnx_model)
232
+ model = ir.serde.deserialize_model(onnx_model)
233
+ main_graph = create_graph(model.graph)
234
+ graphs = []
235
+ main_graph = create_graph(model.graph)
236
+ assert main_graph is not None
237
+ graphs.append(main_graph)
238
+ for function in model.functions.values():
239
+ function_graph = create_graph(function)
240
+ assert function_graph is not None
241
+ graphs.append(function_graph)
242
+ return {"graphs": graphs}
@@ -0,0 +1,54 @@
1
+ Metadata-Version: 2.1
2
+ Name: model-explorer-onnx
3
+ Version: 0.0.1
4
+ Summary: Adapter for ai-edge-model-explorer to support ONNX models
5
+ Author-email: Justin Chu <justinchu@microsoft.com>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2024 Justin Chu
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Classifier: Development Status :: 3 - Alpha
29
+ Classifier: Environment :: Console
30
+ Classifier: Intended Audience :: Developers
31
+ Classifier: Operating System :: POSIX
32
+ Classifier: Operating System :: MacOS :: MacOS X
33
+ Classifier: Operating System :: Microsoft :: Windows
34
+ Classifier: Programming Language :: Python :: 3.8
35
+ Classifier: Programming Language :: Python :: 3.9
36
+ Classifier: Programming Language :: Python :: 3.10
37
+ Classifier: Programming Language :: Python :: 3.11
38
+ Classifier: Programming Language :: Python :: 3.12
39
+ Classifier: License :: OSI Approved :: MIT License
40
+ Requires-Python: >=3.8
41
+ Description-Content-Type: text/markdown
42
+ License-File: LICENSE
43
+ Requires-Dist: ai-edge-model-explorer
44
+ Requires-Dist: onnx
45
+ Requires-Dist: onnxscript
46
+
47
+ # model-explorer-onnx
48
+ ONNX Adapter for model-explorer
49
+
50
+ ## Usage
51
+
52
+ ```bash
53
+ model-explorer model.test --extensions=model-explorer-onnx
54
+ ```
@@ -0,0 +1,9 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/model_explorer_onnx/main.py
5
+ src/model_explorer_onnx.egg-info/PKG-INFO
6
+ src/model_explorer_onnx.egg-info/SOURCES.txt
7
+ src/model_explorer_onnx.egg-info/dependency_links.txt
8
+ src/model_explorer_onnx.egg-info/requires.txt
9
+ src/model_explorer_onnx.egg-info/top_level.txt
@@ -0,0 +1,3 @@
1
+ ai-edge-model-explorer
2
+ onnx
3
+ onnxscript
@@ -0,0 +1 @@
1
+ model_explorer_onnx