onnx-ir 0.1.15__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.
Files changed (53) hide show
  1. onnx_ir/__init__.py +176 -0
  2. onnx_ir/_cloner.py +229 -0
  3. onnx_ir/_convenience/__init__.py +558 -0
  4. onnx_ir/_convenience/_constructors.py +291 -0
  5. onnx_ir/_convenience/_extractor.py +191 -0
  6. onnx_ir/_core.py +4435 -0
  7. onnx_ir/_display.py +54 -0
  8. onnx_ir/_enums.py +474 -0
  9. onnx_ir/_graph_comparison.py +23 -0
  10. onnx_ir/_graph_containers.py +373 -0
  11. onnx_ir/_io.py +133 -0
  12. onnx_ir/_linked_list.py +284 -0
  13. onnx_ir/_metadata.py +45 -0
  14. onnx_ir/_name_authority.py +72 -0
  15. onnx_ir/_polyfill.py +26 -0
  16. onnx_ir/_protocols.py +627 -0
  17. onnx_ir/_safetensors/__init__.py +510 -0
  18. onnx_ir/_tape.py +242 -0
  19. onnx_ir/_thirdparty/asciichartpy.py +310 -0
  20. onnx_ir/_type_casting.py +89 -0
  21. onnx_ir/_version_utils.py +48 -0
  22. onnx_ir/analysis/__init__.py +21 -0
  23. onnx_ir/analysis/_implicit_usage.py +74 -0
  24. onnx_ir/convenience.py +38 -0
  25. onnx_ir/external_data.py +459 -0
  26. onnx_ir/passes/__init__.py +41 -0
  27. onnx_ir/passes/_pass_infra.py +351 -0
  28. onnx_ir/passes/common/__init__.py +54 -0
  29. onnx_ir/passes/common/_c_api_utils.py +76 -0
  30. onnx_ir/passes/common/clear_metadata_and_docstring.py +60 -0
  31. onnx_ir/passes/common/common_subexpression_elimination.py +207 -0
  32. onnx_ir/passes/common/constant_manipulation.py +230 -0
  33. onnx_ir/passes/common/default_attributes.py +99 -0
  34. onnx_ir/passes/common/identity_elimination.py +120 -0
  35. onnx_ir/passes/common/initializer_deduplication.py +179 -0
  36. onnx_ir/passes/common/inliner.py +223 -0
  37. onnx_ir/passes/common/naming.py +280 -0
  38. onnx_ir/passes/common/onnx_checker.py +57 -0
  39. onnx_ir/passes/common/output_fix.py +141 -0
  40. onnx_ir/passes/common/shape_inference.py +112 -0
  41. onnx_ir/passes/common/topological_sort.py +37 -0
  42. onnx_ir/passes/common/unused_removal.py +215 -0
  43. onnx_ir/py.typed +1 -0
  44. onnx_ir/serde.py +2043 -0
  45. onnx_ir/tape.py +15 -0
  46. onnx_ir/tensor_adapters.py +210 -0
  47. onnx_ir/testing.py +197 -0
  48. onnx_ir/traversal.py +118 -0
  49. onnx_ir-0.1.15.dist-info/METADATA +68 -0
  50. onnx_ir-0.1.15.dist-info/RECORD +53 -0
  51. onnx_ir-0.1.15.dist-info/WHEEL +5 -0
  52. onnx_ir-0.1.15.dist-info/licenses/LICENSE +202 -0
  53. onnx_ir-0.1.15.dist-info/top_level.txt +1 -0
onnx_ir/_tape.py ADDED
@@ -0,0 +1,242 @@
1
+ # Copyright (c) ONNX Project Contributors
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ """Convenience methods for constructing the IR."""
4
+
5
+ from __future__ import annotations
6
+
7
+ from collections.abc import Mapping, Sequence
8
+ from typing import (
9
+ Any,
10
+ Optional,
11
+ )
12
+
13
+ import onnx_ir as ir
14
+ from onnx_ir import _convenience
15
+
16
+ # A type representing the domains/versions used in creating nodes in IR.
17
+ UsedOpsets = set[tuple[str, Optional[int]]]
18
+
19
+
20
+ class Tape:
21
+ """Tape class.
22
+
23
+ A tape is a recorder that collects nodes and initializers that are created so
24
+ that they can be used for creating a graph.
25
+
26
+ Example::
27
+
28
+ >>> import onnx_ir as ir
29
+
30
+ >>> tape = ir.tape.Tape()
31
+ >>> a = tape.initializer(ir.tensor([1.0, 2.0, 3.0], name="a"))
32
+ >>> b: ir.Value = ir.val("b", dtype=ir.DataType.FLOAT, shape=(3,))
33
+ >>> c: ir.Value = ir.val("c", dtype=ir.DataType.FLOAT, shape=(3,))
34
+ >>> x = tape.op("Add", [a, b])
35
+ >>> y = tape.op("Elu", [x, c], attributes={"alpha": 2.0})
36
+ >>> y.shape = ir.Shape((3,))
37
+ >>> y.dtype = ir.DataType.FLOAT
38
+ >>> model = ir.Model(
39
+ ... ir.Graph(
40
+ ... inputs=[b, c],
41
+ ... outputs=[y],
42
+ ... nodes=tape.nodes,
43
+ ... initializers=tape.initializers,
44
+ ... opset_imports={"": 20},
45
+ ... name="main_graph",
46
+ ... ),
47
+ ... ir_version=10,
48
+ ... )
49
+ >>> print(model) # doctest: +NORMALIZE_WHITESPACE
50
+ <
51
+ ir_version=10,
52
+ opset_imports={'': 20},
53
+ producer_name=None,
54
+ producer_version=None,
55
+ domain=None,
56
+ model_version=None,
57
+ >
58
+ graph(
59
+ name=main_graph,
60
+ inputs=(
61
+ %"b"<FLOAT,[3]>,
62
+ %"c"<FLOAT,[3]>
63
+ ),
64
+ outputs=(
65
+ %"val_1"<FLOAT,[3]>
66
+ ),
67
+ initializers=(
68
+ %"a"<FLOAT,[3]>{Tensor<FLOAT,[3]>(array([1., 2., 3.], dtype=float32), name='a')}
69
+ ),
70
+ ) {
71
+ 0 | # node_Add_0
72
+ %"val_0"<?,?> ⬅️ ::Add(%"a"{[1.0, 2.0, 3.0]}, %"b")
73
+ 1 | # node_Elu_1
74
+ %"val_1"<FLOAT,[3]> ⬅️ ::Elu(%"val_0", %"c") {alpha=2.0}
75
+ return %"val_1"<FLOAT,[3]>
76
+ }
77
+
78
+ Attributes:
79
+ graph_like: The graph to append the new nodes and initializers to. When
80
+ it is None, the nodes and initializers are creating without owned by a graph.
81
+ Initializers will not be added to functions because it is not supported by ONNX.
82
+ """
83
+
84
+ def __init__(self, graph_like: ir.Graph | ir.Function | None = None) -> None:
85
+ self._nodes: list[ir.Node] = []
86
+ self._initializers: list[ir.Value] = []
87
+ self._used_opsets: UsedOpsets = set()
88
+ self.graph_like = graph_like
89
+
90
+ def __repr__(self) -> str:
91
+ return f"Tape(nodes={self._nodes}, initializers={self._initializers})"
92
+
93
+ @property
94
+ def nodes(self) -> Sequence[ir.Node]:
95
+ return tuple(self._nodes)
96
+
97
+ @property
98
+ def initializers(self) -> Sequence[ir.Value]:
99
+ return tuple(self._initializers)
100
+
101
+ @property
102
+ def used_opsets(self) -> UsedOpsets:
103
+ return self._used_opsets
104
+
105
+ def op(
106
+ self,
107
+ op_type: str,
108
+ inputs: Sequence[ir.Value | None],
109
+ attributes: Mapping[str, _convenience.SupportedAttrTypes] | None = None,
110
+ *,
111
+ domain: str = "",
112
+ overload: str = "",
113
+ version: int | None = None,
114
+ graph: ir.Graph | None = None,
115
+ name: str | None = None,
116
+ doc_string: str | None = None,
117
+ metadata_props: dict[str, str] | None = None,
118
+ output: ir.Value | None = None,
119
+ ) -> ir.Value:
120
+ if attributes is None:
121
+ attrs: Sequence[ir.Attr] = ()
122
+ else:
123
+ attrs = _convenience.convert_attributes(attributes)
124
+ output_kwargs: dict[str, Any]
125
+ if output is None:
126
+ output_kwargs = dict(num_outputs=1)
127
+ else:
128
+ output_kwargs = dict(outputs=[output])
129
+ node = ir.Node(
130
+ domain,
131
+ op_type,
132
+ inputs,
133
+ attributes=attrs,
134
+ **output_kwargs,
135
+ overload=overload,
136
+ version=version,
137
+ graph=graph or self.graph_like,
138
+ name=name,
139
+ doc_string=doc_string,
140
+ metadata_props=metadata_props,
141
+ )
142
+ self._nodes.append(node)
143
+ self._used_opsets.add((domain, version))
144
+
145
+ return node.outputs[0]
146
+
147
+ def op_multi_out(
148
+ self,
149
+ op_type: str,
150
+ inputs: Sequence[ir.Value | None],
151
+ attributes: Mapping[str, _convenience.SupportedAttrTypes] | None = None,
152
+ *,
153
+ num_outputs: int | None = None,
154
+ outputs: Sequence[ir.Value] | None = None,
155
+ domain: str = "",
156
+ overload: str = "",
157
+ version: int | None = None,
158
+ graph: ir.Graph | None = None,
159
+ name: str | None = None,
160
+ doc_string: str | None = None,
161
+ metadata_props: dict[str, str] | None = None,
162
+ ) -> Sequence[ir.Value]:
163
+ if num_outputs is None and outputs is None:
164
+ raise ValueError("Either num_outputs or outputs must be provided.")
165
+ if num_outputs is not None and outputs is not None:
166
+ raise ValueError("Both num_outputs and outputs cannot be provided simultaneously.")
167
+ output_kwargs: dict[str, Any]
168
+ if outputs is None:
169
+ output_kwargs = dict(num_outputs=num_outputs)
170
+ else:
171
+ output_kwargs = dict(outputs=outputs)
172
+ if attributes is None:
173
+ attrs: Sequence[ir.Attr] = ()
174
+ else:
175
+ attrs = _convenience.convert_attributes(attributes)
176
+ node = ir.Node(
177
+ domain,
178
+ op_type,
179
+ inputs,
180
+ attributes=attrs,
181
+ **output_kwargs,
182
+ overload=overload,
183
+ version=version,
184
+ graph=graph or self.graph_like,
185
+ name=name,
186
+ doc_string=doc_string,
187
+ metadata_props=metadata_props,
188
+ )
189
+ self._nodes.append(node)
190
+ self._used_opsets.add((domain, version))
191
+
192
+ return node.outputs
193
+
194
+ def initializer(self, tensor: ir.TensorProtocol, name: str | None = None) -> ir.Value:
195
+ name = name or tensor.name
196
+ if name is None:
197
+ raise ValueError("Name must be provided for initializer.")
198
+ shape = ir.Shape((d if isinstance(d, int) else d.value) for d in tensor.shape.dims)
199
+ value = ir.Value(
200
+ name=name, shape=shape, type=ir.TensorType(tensor.dtype), const_value=tensor
201
+ )
202
+ self._initializers.append(value)
203
+ if isinstance(self.graph_like, ir.Graph):
204
+ self.graph_like.register_initializer(value)
205
+ return value
206
+
207
+
208
+ class Builder(Tape):
209
+ """An extension of the tape that provides a more convenient API for constructing the IR."""
210
+
211
+ def __getattr__(self, op_type: str) -> Any:
212
+ return lambda *args, **kwargs: self._make_node(op_type, args, kwargs)
213
+
214
+ def _make_node(self, op_type: str, inputs: Sequence[ir.Value], kwargs: dict[str, Any]):
215
+ domain = kwargs.pop("_domain", "")
216
+ version = kwargs.pop("_version", None)
217
+ outputs = kwargs.pop("_outputs", 1)
218
+ if isinstance(outputs, Sequence):
219
+ num_outputs = len(outputs)
220
+ else:
221
+ assert isinstance(outputs, int)
222
+ num_outputs = outputs
223
+
224
+ if num_outputs == 1:
225
+ value = super().op(
226
+ op_type, inputs=inputs, attributes=kwargs, domain=domain, version=version
227
+ )
228
+ if isinstance(outputs, Sequence):
229
+ value.name = outputs[0]
230
+ return value
231
+ values = super().op_multi_out(
232
+ op_type,
233
+ inputs=inputs,
234
+ attributes=kwargs,
235
+ domain=domain,
236
+ version=version,
237
+ num_outputs=num_outputs,
238
+ )
239
+ if isinstance(outputs, Sequence):
240
+ for value, name in zip(values, outputs):
241
+ value.name = name
242
+ return values
@@ -0,0 +1,310 @@
1
+ # Copyright © 2016 Igor Kroitor
2
+ #
3
+ # MIT License
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.
22
+
23
+ """Module to generate ascii charts.
24
+
25
+ This module provides a single function `plot` that can be used to generate an
26
+ ascii chart from a series of numbers. The chart can be configured via several
27
+ options to tune the output.
28
+ """
29
+
30
+ from __future__ import annotations
31
+
32
+ from collections.abc import Mapping
33
+ from math import ceil, floor, isnan
34
+
35
+ black = "\033[30m"
36
+ red = "\033[31m"
37
+ green = "\033[32m"
38
+ yellow = "\033[33m"
39
+ blue = "\033[34m"
40
+ magenta = "\033[35m"
41
+ cyan = "\033[36m"
42
+ lightgray = "\033[37m"
43
+ default = "\033[39m"
44
+ darkgray = "\033[90m"
45
+ lightred = "\033[91m"
46
+ lightgreen = "\033[92m"
47
+ lightyellow = "\033[93m"
48
+ lightblue = "\033[94m"
49
+ lightmagenta = "\033[95m"
50
+ lightcyan = "\033[96m"
51
+ white = "\033[97m"
52
+ reset = "\033[0m"
53
+
54
+
55
+ __all__ = [
56
+ "plot",
57
+ "black",
58
+ "red",
59
+ "green",
60
+ "yellow",
61
+ "blue",
62
+ "magenta",
63
+ "cyan",
64
+ "lightgray",
65
+ "default",
66
+ "darkgray",
67
+ "lightred",
68
+ "lightgreen",
69
+ "lightyellow",
70
+ "lightblue",
71
+ "lightmagenta",
72
+ "lightcyan",
73
+ "white",
74
+ "reset",
75
+ ]
76
+
77
+
78
+ # Python 3.2 has math.isfinite, which could have been used, but to support older
79
+ # versions, this little helper is shorter than having to keep doing not isnan(),
80
+ # plus the double-negative of "not is not a number" is confusing, so this should
81
+ # help with readability.
82
+ def _isnum(n):
83
+ return not isnan(n)
84
+
85
+
86
+ def colored(char, color):
87
+ if not color:
88
+ return char
89
+ else:
90
+ return color + char + reset
91
+
92
+
93
+ _DEFAULT_SYMBOLS = ("┼", "┤", "╶", "╴", "─", "╰", "╭", "╮", "╯", "│")
94
+
95
+
96
+ def plot(series, *, bin_edges=None, cfg=None):
97
+ """Generate an ascii chart for a series of numbers.
98
+
99
+ `series` should be a list of ints or floats. Missing data values in the
100
+ series can be specified as a NaN. In Python versions less than 3.5, use
101
+ float("nan") to specify an NaN. With 3.5 onwards, use math.nan to specify a
102
+ NaN.
103
+
104
+ >>> series = [1,2,3,4,float("nan"),4,3,2,1]
105
+ >>> print(plot(series))
106
+ 4.00 ┤ ╭╴╶╮
107
+ 3.00 ┤ ╭╯ ╰╮
108
+ 2.00 ┤╭╯ ╰╮
109
+ 1.00 ┼╯ ╰
110
+
111
+ `series` can also be a list of lists to support multiple data series.
112
+
113
+ >>> series = [[10,20,30,40,30,20,10], [40,30,20,10,20,30,40]]
114
+ >>> print(plot(series, cfg={'height': 3}))
115
+ 40.00 ┤╮ ╭╮ ╭
116
+ 30.00 ┤╰╮╯╰╭╯
117
+ 20.00 ┤╭╰╮╭╯╮
118
+ 10.00 ┼╯ ╰╯ ╰
119
+
120
+ `bin_edges` is an optional list of bin edges to display on the x-axis. If
121
+ provided, the x-axis will be labeled with the bin edges. If there are too
122
+ many bin edges to fit on the x-axis, some labels will be dropped and they
123
+ will be spaced out evenly to fit the width of the chart.
124
+ The labels will be formatted using the `x_format` option in `cfg`.
125
+
126
+ `cfg` is an optional dictionary of various parameters to tune the appearance
127
+ of the chart. `min` and `max` will clamp the y-axis and all values:
128
+
129
+ >>> series = [1,2,3,4,float("nan"),4,3,2,1]
130
+ >>> print(plot(series, cfg={'min': 0}))
131
+ 4.00 ┼ ╭╴╶╮
132
+ 3.00 ┤ ╭╯ ╰╮
133
+ 2.00 ┤╭╯ ╰╮
134
+ 1.00 ┼╯ ╰
135
+ 0.00 ┤
136
+
137
+ >>> print(plot(series, cfg={'min': 2}))
138
+ 4.00 ┤ ╭╴╶╮
139
+ 3.00 ┤ ╭╯ ╰╮
140
+ 2.00 ┼─╯ ╰─
141
+
142
+ >>> print(plot(series, cfg={'min': 2, 'max': 3}))
143
+ 3.00 ┤ ╭─╴╶─╮
144
+ 2.00 ┼─╯ ╰─
145
+
146
+ `height` specifies the number of rows the graph should occupy. It can be
147
+ used to scale down a graph with large data values:
148
+
149
+ >>> series = [10,20,30,40,50,40,30,20,10]
150
+ >>> print(plot(series, cfg={'height': 4}))
151
+ 50.00 ┤ ╭╮
152
+ 40.00 ┤ ╭╯╰╮
153
+ 30.00 ┤ ╭╯ ╰╮
154
+ 20.00 ┤╭╯ ╰╮
155
+ 10.00 ┼╯ ╰
156
+
157
+ `format` specifies a Python format string used to format the labels on the
158
+ y-axis. The default value is "{:8.2f} ". This can be used to remove the
159
+ decimal point:
160
+
161
+ >>> series = [10,20,30,40,50,40,30,20,10]
162
+ >>> print(plot(series, cfg={'height': 4, 'format':'{:8.0f}'}))
163
+ 50 ┤ ╭╮
164
+ 40 ┤ ╭╯╰╮
165
+ 30 ┤ ╭╯ ╰╮
166
+ 20 ┤╭╯ ╰╮
167
+ 10 ┼╯ ╰
168
+ """
169
+ if len(series) == 0:
170
+ return ""
171
+
172
+ if not isinstance(series[0], list):
173
+ if all(isnan(n) for n in series):
174
+ return ""
175
+ else:
176
+ series = [series]
177
+
178
+ if cfg is not None and not isinstance(cfg, Mapping):
179
+ raise TypeError("cfg must be a dictionary or None")
180
+
181
+ cfg = cfg or {}
182
+
183
+ colors = cfg.get("colors", [None])
184
+
185
+ minimum = cfg.get("min", min(filter(_isnum, [j for i in series for j in i])))
186
+ maximum = cfg.get("max", max(filter(_isnum, [j for i in series for j in i])))
187
+
188
+ symbols = cfg.get("symbols", _DEFAULT_SYMBOLS)
189
+
190
+ if minimum > maximum:
191
+ raise ValueError("The min value cannot exceed the max value.")
192
+
193
+ interval = maximum - minimum
194
+ offset = cfg.get("offset", 3)
195
+ height = cfg.get("height", interval)
196
+ ratio = height / interval if interval > 0 else 1
197
+
198
+ min2 = floor(minimum * ratio)
199
+ max2 = ceil(maximum * ratio)
200
+
201
+ def clamp(n):
202
+ return min(max(n, minimum), maximum)
203
+
204
+ def scaled(y):
205
+ return int(round(clamp(y) * ratio) - min2)
206
+
207
+ rows = max2 - min2
208
+
209
+ width = 0
210
+ for series_i in series:
211
+ width = max(width, len(series_i))
212
+ width += offset
213
+
214
+ placeholder = cfg.get("format", "{:8.2f} ")
215
+ x_placeholder = cfg.get("x_format", "{:4.4f}")
216
+
217
+ result = [[" "] * width for i in range(rows + 1)]
218
+
219
+ # axis and labels
220
+ for y in range(min2, max2 + 1):
221
+ label = placeholder.format(maximum - ((y - min2) * interval / (rows if rows else 1)))
222
+ result[y - min2][max(offset - len(label), 0)] = label
223
+ result[y - min2][offset - 1] = symbols[0] if y == 0 else symbols[1] # zero tick mark
224
+
225
+ # first value is a tick mark across the y-axis
226
+ d0 = series[0][0]
227
+ if _isnum(d0):
228
+ result[rows - scaled(d0)][offset - 1] = symbols[0]
229
+
230
+ for i, series_i in enumerate(series):
231
+ color = colors[i % len(colors)]
232
+
233
+ # plot the line
234
+ for x in range(len(series_i) - 1):
235
+ d0 = series_i[x + 0]
236
+ d1 = series_i[x + 1]
237
+
238
+ if isnan(d0) and isnan(d1):
239
+ continue
240
+
241
+ if isnan(d0) and _isnum(d1):
242
+ result[rows - scaled(d1)][x + offset] = colored(symbols[2], color)
243
+ continue
244
+
245
+ if _isnum(d0) and isnan(d1):
246
+ result[rows - scaled(d0)][x + offset] = colored(symbols[3], color)
247
+ continue
248
+
249
+ y0 = scaled(d0)
250
+ y1 = scaled(d1)
251
+ if y0 == y1:
252
+ result[rows - y0][x + offset] = colored(symbols[4], color)
253
+ continue
254
+
255
+ result[rows - y1][x + offset] = (
256
+ colored(symbols[5], color) if y0 > y1 else colored(symbols[6], color)
257
+ )
258
+ result[rows - y0][x + offset] = (
259
+ colored(symbols[7], color) if y0 > y1 else colored(symbols[8], color)
260
+ )
261
+
262
+ start = min(y0, y1) + 1
263
+ end = max(y0, y1)
264
+ for y in range(start, end):
265
+ result[rows - y][x + offset] = colored(symbols[9], color)
266
+
267
+ the_plot = "\n".join(["".join(row).rstrip() for row in result])
268
+
269
+ if bin_edges is None or len(bin_edges) == 0:
270
+ return the_plot
271
+
272
+ # Plot x axis labels
273
+ current_location = 0
274
+ # Compute the amount of leading space for the first x-label using the old label size
275
+ leading_space = offset + len(label)
276
+ # Obtain the first x-label to compute its size
277
+ x_label = x_placeholder.format(bin_edges[0])
278
+ # Initialize the x-label text with the leading space. We allow the first label to
279
+ # recess so that the center of it is aligned with the first tick mark.
280
+ x_label_size = len(x_label)
281
+ x_leading_space = max(0, leading_space - x_label_size)
282
+
283
+ x_labels = []
284
+ # This is the amount of space we have to fit the x-labels. It can overflow the width
285
+ # by half of the x-label size
286
+ workable_width = width + x_label_size // 2
287
+ # Compute the spacing between x-labels
288
+ # If we fit labels and space them by 2 characters, we can fit this many labels:
289
+ min_spacing = 2
290
+ num_labels_can_fit = width // (x_label_size + min_spacing)
291
+ labels_count = len(bin_edges)
292
+ # Find out the actual number of labels we need to display
293
+ num_labels_to_display = min(labels_count, num_labels_can_fit)
294
+ num_spaces = num_labels_to_display - 1
295
+ spacing = max(
296
+ min_spacing,
297
+ (workable_width - num_labels_to_display * x_label_size) // num_spaces,
298
+ )
299
+ # Now start placing labels
300
+ while current_location < workable_width:
301
+ # Find the current label that would be suitable for the current location
302
+ bin_index = int((current_location / workable_width) * labels_count)
303
+ x_label = x_placeholder.format(bin_edges[bin_index])
304
+ x_labels.append(x_label)
305
+ # Move to the next location
306
+ current_location += len(x_label) + spacing
307
+ # Create the x-label row
308
+ x_labels_text = " " * x_leading_space + (" " * spacing).join(x_labels)
309
+
310
+ return the_plot + "\n" + x_labels_text
@@ -0,0 +1,89 @@
1
+ # Copyright (c) ONNX Project Contributors
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ """Numpy utilities for non-native type operation."""
4
+
5
+ from __future__ import annotations
6
+
7
+ import typing
8
+ from collections.abc import Sequence
9
+
10
+ import numpy as np
11
+
12
+ if typing.TYPE_CHECKING:
13
+ import numpy.typing as npt
14
+
15
+
16
+ def pack_4bitx2(array: np.ndarray) -> npt.NDArray[np.uint8]:
17
+ """Convert a numpy array to flatten, packed int4/uint4. Elements must be in the correct range."""
18
+ # Create a 1D copy
19
+ array_flat = array.ravel().view(np.uint8).copy()
20
+ size = array.size
21
+ odd_sized = size % 2 == 1
22
+ if odd_sized:
23
+ array_flat.resize([size + 1], refcheck=False)
24
+ array_flat &= 0x0F
25
+ array_flat[1::2] <<= 4
26
+ return array_flat[0::2] | array_flat[1::2] # type: ignore[return-type]
27
+
28
+
29
+ def unpack_4bitx2(data: npt.NDArray[np.uint8], dims: Sequence[int]) -> npt.NDArray[np.uint8]:
30
+ """Convert a packed uint4 array to unpacked uint4 array represented as uint8.
31
+
32
+ Args:
33
+ data: A numpy array.
34
+ dims: The dimensions are used to reshape the unpacked buffer.
35
+
36
+ Returns:
37
+ A numpy array of int8/uint8 reshaped to dims.
38
+ """
39
+ assert data.dtype == np.uint8, "Input data must be of type uint8"
40
+ result = np.empty([data.size * 2], dtype=data.dtype)
41
+ array_low = data & np.uint8(0x0F)
42
+ array_high = data & np.uint8(0xF0)
43
+ array_high >>= np.uint8(4)
44
+ result[0::2] = array_low
45
+ result[1::2] = array_high
46
+ if result.size == np.prod(dims) + 1:
47
+ # handle single-element padding due to odd number of elements
48
+ result = result[:-1]
49
+ result.resize(dims, refcheck=False)
50
+ return result
51
+
52
+
53
+ def pack_2bitx4(array: np.ndarray) -> npt.NDArray[np.uint8]:
54
+ """Convert a numpy array to flatten, packed int2/uint2. Elements must be in the correct range."""
55
+ # Create a 1D copy
56
+ array_flat = array.ravel().view(np.uint8).copy()
57
+ size = array.size
58
+ padding = (4 - (size % 4)) % 4
59
+ if padding > 0:
60
+ array_flat.resize([size + padding], refcheck=False)
61
+ array_flat &= 0x03
62
+ array_flat[1::4] <<= 2
63
+ array_flat[2::4] <<= 4
64
+ array_flat[3::4] <<= 6
65
+ return array_flat[0::4] | array_flat[1::4] | array_flat[2::4] | array_flat[3::4] # type: ignore[return-type]
66
+
67
+
68
+ def unpack_2bitx4(data: npt.NDArray[np.uint8], dims: Sequence[int]) -> npt.NDArray[np.uint8]:
69
+ """Convert a packed uint2 array to unpacked uint2 array represented as uint8.
70
+
71
+ Args:
72
+ data: A numpy array.
73
+ dims: The dimensions are used to reshape the unpacked buffer.
74
+
75
+ Returns:
76
+ A numpy array of int8/uint8 reshaped to dims.
77
+ """
78
+ assert data.dtype == np.uint8, "Input data must be of type uint8"
79
+ result = np.empty([data.size * 4], dtype=data.dtype)
80
+ result[0::4] = data & np.uint8(0x03)
81
+ result[1::4] = (data & np.uint8(0x0C)) >> np.uint8(2)
82
+ result[2::4] = (data & np.uint8(0x30)) >> np.uint8(4)
83
+ result[3::4] = (data & np.uint8(0xC0)) >> np.uint8(6)
84
+ total_elements = int(np.prod(dims))
85
+ if result.size > total_elements:
86
+ # handle padding due to element count not being a multiple of 4
87
+ result = result[:total_elements]
88
+ result.resize(dims, refcheck=False)
89
+ return result
@@ -0,0 +1,48 @@
1
+ # Copyright (c) ONNX Project Contributors
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ """Version utils for testing."""
4
+
5
+ # pylint: disable=import-outside-toplevel
6
+ from __future__ import annotations
7
+
8
+ import packaging.version
9
+
10
+
11
+ def onnx_older_than(version: str) -> bool:
12
+ """Returns True if the ONNX version is older than the given version."""
13
+ import onnx # noqa: TID251
14
+
15
+ return (
16
+ packaging.version.parse(onnx.__version__).release
17
+ < packaging.version.parse(version).release
18
+ )
19
+
20
+
21
+ def torch_older_than(version: str) -> bool:
22
+ """Returns True if the torch version is older than the given version."""
23
+ import torch
24
+
25
+ return (
26
+ packaging.version.parse(torch.__version__).release
27
+ < packaging.version.parse(version).release
28
+ )
29
+
30
+
31
+ def onnxruntime_older_than(version: str) -> bool:
32
+ """Returns True if the onnxruntime version is older than the given version."""
33
+ import onnxruntime
34
+
35
+ return (
36
+ packaging.version.parse(onnxruntime.__version__).release
37
+ < packaging.version.parse(version).release
38
+ )
39
+
40
+
41
+ def numpy_older_than(version: str) -> bool:
42
+ """Returns True if the numpy version is older than the given version."""
43
+ import numpy
44
+
45
+ return (
46
+ packaging.version.parse(numpy.__version__).release
47
+ < packaging.version.parse(version).release
48
+ )