/
base_onnx_function.py
228 lines (206 loc) · 7.98 KB
/
base_onnx_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# pylint: disable=W0105
"""
@file
@brief Helper for :epkg:`onnxruntime-training`.
"""
import inspect
from io import BytesIO
import numpy
import onnx
from onnxruntime import SessionOptions, InferenceSession
from onnxruntime.capi._pybind_state import ( # pylint: disable=E0611
OrtValue as C_OrtValue)
from ..utils.onnxruntime_helper import ort_device_to_string
from .excs import ProviderError
class BaseLearningOnnx:
"""
Class handling ONNX function to manipulate OrtValue.
Base class for @see cl BaseLearningRate and
@see cl BaseLearningLoss.
"""
def __init__(self):
self.cache_in_ = {}
self.cache_out_ = {}
def __getstate__(self):
"""
Overwrites getstate to get rid of InferenceSession.
"""
atts = [k for k in self.__dict__ if not k.endswith('_')]
state = {k: getattr(self, k) for k in atts}
onx = [k for k in self.__dict__ if k.endswith('_onnx_')]
for o in onx:
state[o] = getattr(self, o).SerializeToString()
onx = [k for k in self.__dict__ if k.endswith('_sess_')]
bind = [k for k in self.__dict__ if k.endswith('_bind_')]
for k in bind:
state[k] = True
binds = [k for k in self.__dict__ if k.endswith('_binds_')]
for k in binds:
state[k] = len(getattr(self, k))
for o in onx:
state[o] = getattr(self, o).get_providers()
return state
def __setstate__(self, state):
"""
Overwrites getstate to get rid of InferenceSession.
"""
for k, v in state.items():
if not k.endswith('_onnx_') and not k.endswith('_sess_'):
setattr(self, k, v)
so = SessionOptions()
so.log_severity_level = 4
for k, v in state.items():
if k.endswith('_onnx_'):
setattr(self, k, onnx.load(BytesIO(v)))
k2 = k.replace("onnx", "sess")
prov = state[k2]
setattr(self, k2, InferenceSession(
getattr(self, k).SerializeToString(), so,
providers=prov))
for k, v in state.items():
if k.endswith('_bind_'):
k2 = k[:-5]
setattr(self, k, getattr(self, k2).io_binding()._iobinding)
elif k.endswith('_binds_'):
k2 = k[:-6]
n = v
setattr(self, k, [
getattr(self, k2).io_binding()._iobinding
for i in range(n)])
self.cache_in_ = {}
self.cache_out_ = {}
return self
def __repr_extended__(self):
return ''
def __repr__(self):
"""
Usual
"""
param = self._get_param_names()
ps = []
for k, v in param:
if k not in self.__dict__:
continue # pragma: no cover
ov = getattr(self, k)
if v is not inspect._empty or ov != v:
ro = repr(ov)
ps.append("%s=%s" % (k, ro))
return "%s(%s)%s" % (
self.__class__.__name__, ", ".join(ps), self.__repr_extended__())
def build_onnx_function(self, opset, device, *args):
"""
This class updates the weights.
It assumes it can do operator on *OrtValue*.
This can be done through ONNX graph.
This function creates :epkg:`InferenceSession`
which do that.
:param opset: opset to use
:param device: :epkg:`C_OrtDevice`
:param args: additional arguments
"""
raise NotImplementedError(
"This method must be overwritten.")
def clear_binding_inputs(self, name, bind, cache=False):
"""
Clears binding and empty cache.
"""
def _cache_in(name, bind):
key = id(bind)
if key in self.cache_in_:
if name in self.cache_in_[key]:
if self.cache_in_[key][name] == 0:
return True
self.cache_in_[key][name] = 0
return False
return True
if cache and _cache_in(name, bind):
return
bind.clear_binding_inputs()
def _bind_input_ortvalue(self, name, bind, c_ortvalue, device, cache=False):
"""
Binds :epkg:`C_OrtValue` to the structure used by
:epkg:`InferenceSession` to run inference.
:param name: str
:param bind: python structure
:param c_ortvalue: C structure for OrtValue (:epkg:`C_OrtValue`),
it can be also a numpy array
:param device: device
:param cache: avoids binding again the data pointer did not change,
only works when c_ortvalue is of :epkg:`C_OrtValue`
"""
def _cache_in(name, bind, c_ortvalue, ptr2):
key = id(bind)
if key in self.cache_in_:
if name in self.cache_in_[key]:
ptr = self.cache_in_[key][name]
if ptr == ptr2:
return True
self.cache_in_[key][name] = ptr2
else:
self.cache_in_[key] = {name: ptr2}
return False
def do_bind(name, bind, c_ortvalue):
bind.bind_ortvalue_input(name, c_ortvalue)
if isinstance(c_ortvalue, C_OrtValue):
if cache and _cache_in(
name, bind, c_ortvalue, c_ortvalue.data_ptr()):
return
do_bind(name, bind, c_ortvalue)
elif isinstance(c_ortvalue, numpy.ndarray):
if self.device_type() != device.cpu(): # pylint: disable=E1101
raise ProviderError(
"device=%s is not CPU." % ort_device_to_string(
device))
if cache and _cache_in(
name, bind, c_ortvalue,
c_ortvalue.__array_interface__['data'][0]):
return
bind.bind_input(
name, device, c_ortvalue.dtype, c_ortvalue.shape,
c_ortvalue.__array_interface__['data'][0])
else:
raise TypeError( # pragma: no cover
"Unable to bind type %r for name %r." % (
type(c_ortvalue), name))
def _bind_output_ortvalue(self, name, bind, c_ortvalue, cache=False):
"""
Binds :epkg:`C_OrtValue` to the structure used by
:epkg:`InferenceSession` to run inference.
:param name: str
:param bind: python structure
:param c_ortvalue: C structure for OrtValue (:epkg:`C_OrtValue`)
:param cache: avoids binding again the data pointer did not change,
only works when c_ortvalue is of :epkg:`C_OrtValue`
This method can be used for inplace computation.
"""
def _cache_out(name, bind, c_ortvalue, ptr2):
key = id(bind)
if key in self.cache_out_:
if name in self.cache_out_[key]:
ptr = self.cache_out_[key][name]
if ptr == ptr2:
return True
self.cache_out_[key][name] = ptr2
else:
self.cache_out_[key] = {name: ptr2}
return False
def do_bind(name, bind, c_ortvalue):
bind.bind_ortvalue_output(name, c_ortvalue)
if isinstance(c_ortvalue, C_OrtValue):
if cache and _cache_out(
name, bind, c_ortvalue,
c_ortvalue.data_ptr()):
return
do_bind(name, bind, c_ortvalue)
else:
raise TypeError( # pragma: no cover
"Unable to bind type %r for name %r." % (
type(c_ortvalue), name))
@classmethod
def _get_param_names(cls):
init = getattr(cls.__init__, "deprecated_original", cls.__init__)
init_signature = inspect.signature(init)
parameters = [
p for p in init_signature.parameters.values()
if p.name != "self" and p.kind != p.VAR_KEYWORD]
return [(p.name, p.default) for p in parameters]