microsoft/onnxruntime-extensions

Public

mirrored fromhttps://github.com/microsoft/onnxruntime-extensionsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1ae69c0f7aeaab9911cf8ebf86ee92b34dadd26e

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

onnxruntime_extensions/_ortapi2.py

126lines · modecode

1# Copyright (c) Microsoft Corporation. All rights reserved.
2# Licensed under the MIT License. See License.txt in the project root for
3# license information.
4###############################################################################
5
6import numpy as np
7import onnxruntime as _ort
8from ._ocos import default_opset_domain, get_library_path # noqa
9from ._cuops import * # noqa
10
11
12def _get_opset_version_from_ort():
13 _ORT_OPSET_SUPPORT_TABLE = {
14 "1.5": 11,
15 "1.6": 12,
16 "1.7": 13,
17 "1.8": 14
18 }
19
20 ort_ver_string = '.'.join(_ort.__version__.split('.')[0:2])
21 return _ORT_OPSET_SUPPORT_TABLE.get(ort_ver_string, 11)
22
23
24def make_onnx_model(graph, opset_version=0, extra_domain=default_opset_domain(), extra_opset_version=1):
25 if opset_version == 0:
26 opset_version = _get_opset_version_from_ort()
27 fn_mm = onnx.helper.make_model_gen_version if hasattr(onnx.helper, 'make_model_gen_version'
28 ) else onnx.helper.make_model
29 model = fn_mm(graph, opset_imports=[
30 onnx.helper.make_operatorsetid('ai.onnx', opset_version)])
31 model.opset_import.extend([onnx.helper.make_operatorsetid(extra_domain, extra_opset_version)])
32 return model
33
34
35class EagerOp:
36
37 @classmethod
38 def get_ort_session_options(cls):
39 # ONNXRuntime has an issue to support reusing the SessionOptions object.
40 # Create a new one every time here
41 so = _ort.SessionOptions()
42 so.register_custom_ops_library(get_library_path())
43 return so
44
45 def __init__(self):
46 self._onnx_model = None
47 self.ort_session = None
48 self.default_inputs = {}
49
50 def create_from_customop(self, op_type, *args, **kwargs):
51 graph = SingleOpGraph.build_my_graph(op_type, *args, **kwargs)
52 self._bind(make_onnx_model(graph))
53 return self
54
55 def add_default_input(self, **kwargs):
56 inputs = {
57 ky_: val_ if isinstance(val_, (np.ndarray, np.generic)) else \
58 np.asarray(list(val_), dtype=np.uint8) for ky_, val_ in kwargs.items()
59 }
60
61 self.default_inputs.update(inputs)
62
63 @property
64 def onnx_model(self):
65 assert self._oxml is not None, "No onnx model attached yet."
66 return self._oxml
67
68 @property
69 def input_names(self):
70 return [vi_.name for vi_ in self.onnx_model.graph.input]
71
72 @property
73 def output_names(self):
74 return [vi_.name for vi_ in self.onnx_model.graph.output]
75
76 def _bind(self, oxml):
77 self.inputs = list(oxml.graph.input)
78 self.output = list(oxml.graph.output)
79 self._oxml = oxml
80 return self
81
82 def _ensure_ort_session(self):
83 if self.ort_session is None:
84 sess = _ort.InferenceSession(self.onnx_model.SerializeToString(), self.get_ort_session_options())
85 self.ort_session = sess
86
87 return self.ort_session
88
89 @classmethod
90 def from_customop(cls, op_type, *args, **kwargs):
91 return cls().create_from_customop(op_type, *args, **kwargs)
92
93 @classmethod
94 def from_model(cls, path_or_model, *args, **kwargs):
95 return cls()._bind(onnx.load_model(path_or_model) if isinstance(path_or_model, str) else path_or_model)
96
97 def _argument_map(self, *args, **kwargs):
98 idx = 0
99 feed = {}
100 for i_ in self.inputs:
101 if i_.name in self.default_inputs:
102 feed[i_.name] = self.default_inputs[i_.name]
103 continue
104
105 x = args[idx]
106 ts_x = np.array(x) if isinstance(x, (int, float, bool)) else x
107 # an annoying bug is numpy by default is int32, while pytorch is int64.
108 # so cast the input here automatically.
109 feed[i_.name] = \
110 ts_x.astype(np.int64) if i_.type.tensor_type.elem_type == onnx_proto.TensorProto.INT64 else ts_x
111 idx += 1
112
113 # feed.update(kwargs)
114 return feed
115
116 def __call__(self, *args, **kwargs):
117 self._ensure_ort_session()
118 outputs = self.ort_session.run(None, self._argument_map(*args, **kwargs))
119 return outputs[0] if len(outputs) == 1 else outputs
120
121
122def optimize_model(model_or_file, output_file):
123 sess_options = EagerOp.get_ort_session_options()
124 sess_options.graph_optimization_level = _ort.GraphOptimizationLevel.ORT_ENABLE_EXTENDED
125 sess_options.optimized_model_filepath = output_file
126 _ort.InferenceSession(model_or_file if isinstance(model_or_file, str) else model_or_file.SerializeToString(), sess_options)
127