onnxscript 0.1.0__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.
- onnxscript/__init__.py +131 -0
- onnxscript/_framework_apis/__init__.py +3 -0
- onnxscript/_framework_apis/torch_2_5.py +117 -0
- onnxscript/_framework_apis/torch_2_6.py +45 -0
- onnxscript/_internal/__init__.py +0 -0
- onnxscript/_internal/analysis.py +229 -0
- onnxscript/_internal/ast_utils.py +64 -0
- onnxscript/_internal/autocast.py +250 -0
- onnxscript/_internal/deprecation.py +78 -0
- onnxscript/_internal/param_manipulation.py +148 -0
- onnxscript/_internal/runtime_typing.py +43 -0
- onnxscript/_internal/utils.py +99 -0
- onnxscript/_internal/version_utils.py +118 -0
- onnxscript/_legacy_ir/__init__.py +341 -0
- onnxscript/_legacy_ir/visitor.py +937 -0
- onnxscript/_thirdparty/asciichartpy.py +313 -0
- onnxscript/backend/__init__.py +2 -0
- onnxscript/backend/onnx_backend.py +303 -0
- onnxscript/backend/onnx_export.py +885 -0
- onnxscript/converter.py +1470 -0
- onnxscript/evaluator.py +619 -0
- onnxscript/function_libs/tools/torch_lib/deduce_type_constraints.py +403 -0
- onnxscript/function_libs/tools/torch_lib/generate_aten_signatures.py +333 -0
- onnxscript/function_libs/tools/torch_lib/generate_prims_signatures.py +331 -0
- onnxscript/function_libs/torch_lib/__init__.py +12 -0
- onnxscript/function_libs/torch_lib/_constants.py +5 -0
- onnxscript/function_libs/torch_lib/_flags.py +58 -0
- onnxscript/function_libs/torch_lib/graph_building/__init__.py +56 -0
- onnxscript/function_libs/torch_lib/graph_building/_graph_building_ir.py +723 -0
- onnxscript/function_libs/torch_lib/graph_building/_graph_building_torch.py +1125 -0
- onnxscript/function_libs/torch_lib/ops/__init__.py +27 -0
- onnxscript/function_libs/torch_lib/ops/common.py +80 -0
- onnxscript/function_libs/torch_lib/ops/core.py +8935 -0
- onnxscript/function_libs/torch_lib/ops/fft.py +385 -0
- onnxscript/function_libs/torch_lib/ops/linalg.py +399 -0
- onnxscript/function_libs/torch_lib/ops/nested.py +25 -0
- onnxscript/function_libs/torch_lib/ops/nn.py +2713 -0
- onnxscript/function_libs/torch_lib/ops/prims.py +850 -0
- onnxscript/function_libs/torch_lib/ops/quantized_decomposed.py +63 -0
- onnxscript/function_libs/torch_lib/ops/sparse.py +23 -0
- onnxscript/function_libs/torch_lib/ops/special.py +387 -0
- onnxscript/function_libs/torch_lib/ops/vision.py +25 -0
- onnxscript/function_libs/torch_lib/registration.py +151 -0
- onnxscript/function_libs/torch_lib/tensor_typing.py +74 -0
- onnxscript/ir/__init__.py +153 -0
- onnxscript/ir/_convenience.py +447 -0
- onnxscript/ir/_core.py +3119 -0
- onnxscript/ir/_display.py +49 -0
- onnxscript/ir/_enums.py +163 -0
- onnxscript/ir/_graph_comparison.py +23 -0
- onnxscript/ir/_io.py +97 -0
- onnxscript/ir/_linked_list.py +276 -0
- onnxscript/ir/_metadata.py +44 -0
- onnxscript/ir/_name_authority.py +72 -0
- onnxscript/ir/_polyfill.py +25 -0
- onnxscript/ir/_protocols.py +598 -0
- onnxscript/ir/_schemas.py +548 -0
- onnxscript/ir/_tape.py +120 -0
- onnxscript/ir/_type_casting.py +106 -0
- onnxscript/ir/convenience.py +32 -0
- onnxscript/ir/external_data.py +396 -0
- onnxscript/ir/passes/__init__.py +33 -0
- onnxscript/ir/passes/_pass_infra.py +172 -0
- onnxscript/ir/serde.py +1620 -0
- onnxscript/ir/tensor_adapters.py +122 -0
- onnxscript/ir/traversal.py +82 -0
- onnxscript/irbuilder.py +542 -0
- onnxscript/main.py +167 -0
- onnxscript/onnx_opset/__init__.py +232 -0
- onnxscript/onnx_opset/_impl/opset1.py +4100 -0
- onnxscript/onnx_opset/_impl/opset10.py +1227 -0
- onnxscript/onnx_opset/_impl/opset11.py +4013 -0
- onnxscript/onnx_opset/_impl/opset12.py +1078 -0
- onnxscript/onnx_opset/_impl/opset13.py +3924 -0
- onnxscript/onnx_opset/_impl/opset14.py +999 -0
- onnxscript/onnx_opset/_impl/opset15.py +604 -0
- onnxscript/onnx_opset/_impl/opset16.py +1255 -0
- onnxscript/onnx_opset/_impl/opset17.py +561 -0
- onnxscript/onnx_opset/_impl/opset18.py +1803 -0
- onnxscript/onnx_opset/_impl/opset19.py +1942 -0
- onnxscript/onnx_opset/_impl/opset2.py +218 -0
- onnxscript/onnx_opset/_impl/opset20.py +675 -0
- onnxscript/onnx_opset/_impl/opset21.py +1976 -0
- onnxscript/onnx_opset/_impl/opset22.py +2588 -0
- onnxscript/onnx_opset/_impl/opset3.py +199 -0
- onnxscript/onnx_opset/_impl/opset4.py +77 -0
- onnxscript/onnx_opset/_impl/opset5.py +84 -0
- onnxscript/onnx_opset/_impl/opset6.py +944 -0
- onnxscript/onnx_opset/_impl/opset7.py +1243 -0
- onnxscript/onnx_opset/_impl/opset8.py +444 -0
- onnxscript/onnx_opset/_impl/opset9.py +1485 -0
- onnxscript/onnx_opset/_impl/opset_ai_onnx_ml1.py +974 -0
- onnxscript/onnx_opset/_impl/opset_ai_onnx_ml2.py +112 -0
- onnxscript/onnx_opset/_impl/opset_ai_onnx_ml3.py +308 -0
- onnxscript/onnx_opset/_impl/opset_ai_onnx_ml4.py +129 -0
- onnxscript/onnx_opset/_impl/opset_ai_onnx_ml5.py +158 -0
- onnxscript/onnx_opset/_impl/opset_ai_onnx_preview_training1.py +577 -0
- onnxscript/onnx_types.py +229 -0
- onnxscript/optimizer/__init__.py +39 -0
- onnxscript/optimizer/_constant_folding.py +1083 -0
- onnxscript/optimizer/_inliner.py +312 -0
- onnxscript/optimizer/_legacy/_optimizer.py +98 -0
- onnxscript/optimizer/_legacy/_remove_unused_proto.py +144 -0
- onnxscript/optimizer/_legacy/_simple_function_folding.py +243 -0
- onnxscript/optimizer/_legacy/constant_folding.py +293 -0
- onnxscript/optimizer/_legacy/evaluator.py +439 -0
- onnxscript/optimizer/_optimizer.py +61 -0
- onnxscript/optimizer/_remove_unused.py +106 -0
- onnxscript/optimizer/_remove_unused_function.py +72 -0
- onnxscript/py.typed +1 -0
- onnxscript/rewriter/__init__.py +56 -0
- onnxscript/rewriter/_ir_utils.py +111 -0
- onnxscript/rewriter/broadcast_to_matmul.py +180 -0
- onnxscript/rewriter/cast_constant_of_shape.py +50 -0
- onnxscript/rewriter/collapse_slices.py +141 -0
- onnxscript/rewriter/erfgelu.py +27 -0
- onnxscript/rewriter/function_rule.py +232 -0
- onnxscript/rewriter/gemm_to_matmul_add.py +21 -0
- onnxscript/rewriter/generic_pattern.py +700 -0
- onnxscript/rewriter/llama_rule_sets.py +287 -0
- onnxscript/rewriter/no_op.py +56 -0
- onnxscript/rewriter/onnxruntime/__init__.py +50 -0
- onnxscript/rewriter/onnxruntime/bfloat16_utils/bfloat16_converter.py +99 -0
- onnxscript/rewriter/onnxruntime/fused_matmul_rule_sets.py +177 -0
- onnxscript/rewriter/onnxruntime/group_normalization_merge_silu.py +64 -0
- onnxscript/rewriter/onnxruntime/instance_to_group_normalization.py +155 -0
- onnxscript/rewriter/onnxruntime/softmax.py +63 -0
- onnxscript/rewriter/onnxruntime/transformers/__init__.py +21 -0
- onnxscript/rewriter/onnxruntime/transformers/biassplitgelu.py +31 -0
- onnxscript/rewriter/onnxruntime/transformers/fastgelu.py +29 -0
- onnxscript/rewriter/onnxruntime/transformers/layernorm.py +47 -0
- onnxscript/rewriter/onnxruntime/transformers/multihead_attention.py +715 -0
- onnxscript/rewriter/ort_fusions/__init__.py +9 -0
- onnxscript/rewriter/ort_fusions/_core.py +28 -0
- onnxscript/rewriter/ort_fusions/_smollm_1.py +253 -0
- onnxscript/rewriter/ort_fusions/_smollm_2.py +467 -0
- onnxscript/rewriter/ort_fusions/_test_models.py +122 -0
- onnxscript/rewriter/ort_fusions/_test_utils.py +42 -0
- onnxscript/rewriter/ort_fusions/cos_sin_cache.py +154 -0
- onnxscript/rewriter/ort_fusions/gqa.py +156 -0
- onnxscript/rewriter/ort_fusions/mha.py +198 -0
- onnxscript/rewriter/ort_fusions/rms_normalization.py +95 -0
- onnxscript/rewriter/ort_fusions/rotary_embedding.py +64 -0
- onnxscript/rewriter/ort_fusions/sdpa.py +75 -0
- onnxscript/rewriter/ort_fusions/skip_normalization.py +46 -0
- onnxscript/rewriter/pattern.py +1714 -0
- onnxscript/rewriter/testing.py +77 -0
- onnxscript/sourceinfo.py +59 -0
- onnxscript/tensor.py +227 -0
- onnxscript/testing/__init__.py +482 -0
- onnxscript/tools/__init__.py +4 -0
- onnxscript/tools/benchmark/__init__.py +23 -0
- onnxscript/tools/benchmark/benchmark_helpers.py +783 -0
- onnxscript/tools/benchmark/benchmark_run.py +140 -0
- onnxscript/tools/benchmark/export_model.py +207 -0
- onnxscript/tools/benchmark/export_model_batch.py +146 -0
- onnxscript/tools/memory_peak.py +244 -0
- onnxscript/tools/training_helper.py +50 -0
- onnxscript/tools/transformers_models/__init__.py +190 -0
- onnxscript/tools/transformers_models/llama.py +168 -0
- onnxscript/tools/transformers_models/mistral.py +238 -0
- onnxscript/tools/transformers_models/phi.py +248 -0
- onnxscript/tools/transformers_models/phi3.py +259 -0
- onnxscript/type_annotation.py +281 -0
- onnxscript/utils/__init__.py +0 -0
- onnxscript/utils/evaluation_utils.py +56 -0
- onnxscript/utils/timing_utils.py +33 -0
- onnxscript/utils/utils.py +84 -0
- onnxscript/values.py +790 -0
- onnxscript/version_converter/__init__.py +21 -0
- onnxscript/version_converter/_version_converter.py +314 -0
- onnxscript-0.1.0.dist-info/LICENSE +21 -0
- onnxscript-0.1.0.dist-info/METADATA +370 -0
- onnxscript-0.1.0.dist-info/RECORD +176 -0
- onnxscript-0.1.0.dist-info/WHEEL +5 -0
- onnxscript-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# Copyright (c) Microsoft Corporation.
|
|
2
|
+
# Licensed under the MIT License.
|
|
3
|
+
"""Compatible adapters implementing the TensorProtocol interface for various framework tensor types.
|
|
4
|
+
|
|
5
|
+
This module provides public classes that implement the :class:`onnxscript.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
|
+
from onnxscript import 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
|
+
from onnxscript import ir
|
|
41
|
+
from onnxscript.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)
|
|
85
|
+
if self.dtype in {
|
|
86
|
+
ir.DataType.FLOAT8E4M3FN,
|
|
87
|
+
ir.DataType.FLOAT8E4M3FNUZ,
|
|
88
|
+
ir.DataType.FLOAT8E5M2,
|
|
89
|
+
ir.DataType.FLOAT8E5M2FNUZ,
|
|
90
|
+
}:
|
|
91
|
+
# TODO: Use ml_dtypes
|
|
92
|
+
return self.raw.view(torch.uint8).numpy(force=True)
|
|
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
|
+
)
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# Copyright (c) Microsoft Corporation.
|
|
2
|
+
# Licensed under the MIT License.
|
|
3
|
+
"""Utilities for traversing the IR graph."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"RecursiveGraphIterator",
|
|
9
|
+
]
|
|
10
|
+
|
|
11
|
+
from typing import Callable, Iterator, Reversible, Union
|
|
12
|
+
|
|
13
|
+
from typing_extensions import Self
|
|
14
|
+
|
|
15
|
+
from onnxscript.ir import _core, _enums
|
|
16
|
+
|
|
17
|
+
GraphLike = Union[_core.Graph, _core.Function, _core.GraphView]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class RecursiveGraphIterator(Iterator[_core.Node], Reversible[_core.Node]):
|
|
21
|
+
def __init__(
|
|
22
|
+
self,
|
|
23
|
+
graph_like: GraphLike,
|
|
24
|
+
*,
|
|
25
|
+
recursive: Callable[[_core.Node], bool] | None = None,
|
|
26
|
+
reverse: bool = False,
|
|
27
|
+
):
|
|
28
|
+
"""Iterate over the nodes in the graph, recursively visiting subgraphs.
|
|
29
|
+
|
|
30
|
+
Args:
|
|
31
|
+
graph_like: The graph to traverse.
|
|
32
|
+
recursive: A callback that determines whether to recursively visit the subgraphs
|
|
33
|
+
contained in a node. If not provided, all nodes in subgraphs are visited.
|
|
34
|
+
reverse: Whether to iterate in reverse order.
|
|
35
|
+
"""
|
|
36
|
+
self._graph = graph_like
|
|
37
|
+
self._recursive = recursive
|
|
38
|
+
self._reverse = reverse
|
|
39
|
+
self._iterator = self._recursive_node_iter(graph_like)
|
|
40
|
+
|
|
41
|
+
def __iter__(self) -> Self:
|
|
42
|
+
self._iterator = self._recursive_node_iter(self._graph)
|
|
43
|
+
return self
|
|
44
|
+
|
|
45
|
+
def __next__(self) -> _core.Node:
|
|
46
|
+
return next(self._iterator)
|
|
47
|
+
|
|
48
|
+
def _recursive_node_iter(
|
|
49
|
+
self, graph: _core.Graph | _core.Function | _core.GraphView
|
|
50
|
+
) -> Iterator[_core.Node]:
|
|
51
|
+
iterable = reversed(graph) if self._reverse else graph
|
|
52
|
+
for node in iterable: # type: ignore[union-attr]
|
|
53
|
+
yield node
|
|
54
|
+
if self._recursive is not None and not self._recursive(node):
|
|
55
|
+
continue
|
|
56
|
+
yield from self._iterate_subgraphs(node)
|
|
57
|
+
|
|
58
|
+
def _iterate_subgraphs(self, node: _core.Node):
|
|
59
|
+
for attr in node.attributes.values():
|
|
60
|
+
if not isinstance(attr, _core.Attr):
|
|
61
|
+
continue
|
|
62
|
+
if attr.type == _enums.AttributeType.GRAPH:
|
|
63
|
+
yield from RecursiveGraphIterator(
|
|
64
|
+
attr.value,
|
|
65
|
+
recursive=self._recursive,
|
|
66
|
+
reverse=self._reverse,
|
|
67
|
+
)
|
|
68
|
+
elif attr.type == _enums.AttributeType.GRAPHS:
|
|
69
|
+
graphs = reversed(attr.value) if self._reverse else attr.value
|
|
70
|
+
for graph in graphs:
|
|
71
|
+
yield from RecursiveGraphIterator(
|
|
72
|
+
graph,
|
|
73
|
+
recursive=self._recursive,
|
|
74
|
+
reverse=self._reverse,
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
def __reversed__(self) -> Iterator[_core.Node]:
|
|
78
|
+
return RecursiveGraphIterator(
|
|
79
|
+
self._graph,
|
|
80
|
+
recursive=self._recursive,
|
|
81
|
+
reverse=not self._reverse,
|
|
82
|
+
)
|