-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
save.jl
375 lines (310 loc) · 11.3 KB
/
save.jl
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import Pkg
modelproto(graph;kwargs...) = ModelProto(;
ir_version=7,
opset_import=[OperatorSetIdProto(version=14)],
producer_name="ONNX.jl",
producer_version=string(Pkg.Types.Context().env.project.version), # TODO: Ugh....
graph=graph,
kwargs...)
"""
graphproto()
Return an [`ONNX.GraphProto`](@ref) with all fields initialized to empty arrays.
"""
graphproto(name; kwargs...) = GraphProto(;
node = NodeProto[],
initializer = TensorProto[],
input = ValueInfoProto[],
output = ValueInfoProto[],
value_info = ValueInfoProto[],
name = name,
kwargs...
)
add!(gp::GraphProto, np::NodeProto) = push!(gp.node, np)
add!(gp::GraphProto, tp::TensorProto) = push!(gp.initializer, tp)
##############################################################################
# Utils #
##############################################################################
if VERSION < v"1.9"
# can we make it more robust?
iskwfunc(f) = endswith(string(f), "##kw")
else
iskwfunc(f) = (f === Core.kwcall)
end
function kwargs2dict(op::Umlaut.Call)
kw = iskwfunc(op.fn) ? op.args[1] : (;)
return Dict(zip(keys(kw), values(kw)))
end
if VERSION < v"1.9.0"
macro opconfig_kw(backend, fn)
return quote
$OpConfig{$backend, <:Union{typeof($fn), typeof(Core.kwfunc($fn))}}
end
end
else
macro opconfig_kw(backend, fn)
return quote
$OpConfig{$backend, <:Union{typeof($fn)}}
end
end
end
function NodeProto(op_type::String, op::Umlaut.Call, attrs::Dict=Dict())
args = iskwfunc(op.fn) ? op.args[3:end] : op.args
return NodeProto(
input=[onnx_name(v) for v in args],
output=[onnx_name(op)],
name=onnx_name(op),
attribute=AttributeProto[AttributeProto(k, v) for (k, v) in attrs],
op_type=op_type
)
end
ValueInfoProto(op::Umlaut.AbstractOp) = ValueInfoProto(
onnx_name(op),
# utils in write.jl reverse the shape, so we don't do it here
# try the following for example:
# TypeProto_Tensor((4, 3), Float64).shape.dim[1].dim_value
# which gives 3 instead of 4
size(op.val),
eltype(op.val)
)
##############################################################################
# Methods #
##############################################################################
onnx_name(v::Variable) = "x$(v.id)"
onnx_name(op::Umlaut.AbstractOp) = "x$(op.id)"
"""
save_node!(g::GraphProto, op::Umlaut.Call)
save_node!(g::GraphProto, ::OpConfig{:Backend, Fn}, op::Umlaut.Call)
Serialize a single operation from a tape to graph.
"""
function save_node!(g::GraphProto, op::Umlaut.Call)
if VERSION >= v"1.9" && op.fn == Core.kwcall
v_fn = op.args[2]
fn = v_fn isa V ? op.tape[v_fn].val : v_fn
save_node!(g, OpConfig{:ONNX, typeof(fn)}(), op)
else
save_node!(g, OpConfig{:ONNX, typeof(op.fn)}(), op)
end
end
function save_node!(g::GraphProto, ::OpConfig{:ONNX, typeof(getfield)}, op::Umlaut.Call)
# Do nothing: getfield is only used to destructure multi-ouput nodes
# and doesn't need to be written to ONNX graph.
# Using getfield() for anything other then destructuring is thus a mistake.
end
function save_node!(g::GraphProto, ::OpConfig{:ONNX, typeof(*)}, op::Umlaut.Call)
nd = NodeProto(
input=[onnx_name(v) for v in reverse(op.args)],
output=[onnx_name(op)],
name=onnx_name(op),
attribute=AttributeProto[],
op_type="Gemm"
)
push!(g.node, nd)
end
function save_node!(g::GraphProto, ::@opconfig_kw(:ONNX, onnx_gemm), op::Umlaut.Call)
kw_dict = kwargs2dict(op)
attrs = rename_keys(kw_dict, Dict(
:tA => :transA,
:tB => :transB,
:α => :alpha,
:β => :beta
))
nd = NodeProto("Gemm", op, attrs)
push!(g.node, nd)
end
function save_node!(g::GraphProto, ::@opconfig_kw(:ONNX, conv), op::Umlaut.Call)
args = iskwfunc(op.fn) ? op.args[3:end] : op.args
w = args[2]._op.val
# ONNXRuntime gives the following error for Float64:
# NOT_IMPLEMENTED : Could not find an implementation for the node x3:Conv(11)')
eltype(w) == Float64 && @warn "Not all ONNX runtimes support input & weights as Float64"
attrs = from_nnlib_conv(kwargs2dict(op), ndims(w) - 2)
nd = NodeProto("Conv", op, attrs)
push!(g.node, nd)
end
function save_node!(g::GraphProto, ::@opconfig_kw(:ONNX, maxpool), op::Umlaut.Call)
args = iskwfunc(op.fn) ? op.args[3:end] : op.args
x = args[1]._op.val
attrs = from_nnlib_conv(kwargs2dict(op), ndims(x) - 2)
nd = NodeProto("MaxPool", op, attrs)
push!(g.node, nd)
end
function save_node!(g::GraphProto, ::OpConfig{:ONNX, typeof(global_average_pool)}, op::Umlaut.Call)
nd = NodeProto("GlobalAveragePool", op)
push!(g.node, nd)
end
function save_node!(g::GraphProto, ::@opconfig_kw(:ONNX, onnx_flatten), op::Umlaut.Call)
nd = NodeProto("Flatten", op)
push!(g.node, nd)
end
function save_node!(g::GraphProto, ::OpConfig{:ONNX, typeof(add)}, op::Umlaut.Call)
nd = NodeProto("Add", op)
push!(g.node, nd)
end
function save_node!(g::GraphProto, ::OpConfig{:ONNX, typeof(mul)}, op::Umlaut.Call)
nd = NodeProto("Mul", op)
push!(g.node, nd)
end
function save_node!(g::GraphProto, ::OpConfig{:ONNX, typeof(_min)}, op::Umlaut.Call)
nd = NodeProto("Min", op)
push!(g.node, nd)
end
function save_node!(g::GraphProto, ::OpConfig{:ONNX, typeof(_max)}, op::Umlaut.Call)
nd = NodeProto("Max", op)
push!(g.node, nd)
end
function save_node!(g::GraphProto, ::OpConfig{:ONNX, typeof(relu)}, op::Umlaut.Call)
nd = NodeProto("Relu", op)
push!(g.node, nd)
end
function save_node!(g::GraphProto, ::OpConfig{:ONNX, typeof(elu)}, op::Umlaut.Call)
nd = NodeProto("Elu", op)
push!(g.node, nd)
end
function save_node!(g::GraphProto, ::OpConfig{:ONNX, typeof(tanh)}, op::Umlaut.Call)
nd = NodeProto("Tanh", op)
push!(g.node, nd)
end
function save_node!(g::GraphProto, ::OpConfig{:ONNX, typeof(NNlib.batched_mul)}, op::Umlaut.Call)
nd = NodeProto(
input=[onnx_name(v) for v in reverse(op.args)],
output=[onnx_name(op)],
name=onnx_name(op),
attribute=AttributeProto[],
op_type="MatMul"
)
push!(g.node, nd)
end
function save_node!(g::GraphProto, ::@opconfig_kw(:ONNX, batch_norm), op::Umlaut.Call)
kw_dict = kwargs2dict(op)
attrs = from_nnlib_norm(kw_dict)
args = iskwfunc(op.fn) ? op.args[3:end] : op.args
output = if Bool(get(attrs, :training_mode, 0))
vars = unpacked_vars(op)
@assert(all([v isa V for v in vars]),
"Not all output vars of batch_norm are unpacked to the tape")
[onnx_name(v) for v in vars]
else
[onnx_name(op)]
end
nd = NodeProto(
input=[onnx_name(v) for v in args],
output=output,
name=onnx_name(op),
attribute=AttributeProto[AttributeProto(k, v) for (k, v) in attrs],
op_type="BatchNormalization"
)
push!(g.node, nd)
end
function save_node!(g::GraphProto, ::OpConfig{:ONNX, typeof(size)}, op::Umlaut.Call)
nd = NodeProto("Shape", op)
push!(g.node, nd)
end
function save_node!(g::GraphProto, ::OpConfig{:ONNX, <:Any}, op::Umlaut.Constant)
@assert(
op.val isa AbstractArray,
"ONNX.jl currently doesn't support saving constants of type $(typeof(op.val))"
)
attr_name = :value
attr_value = from_nnlib(op.val)
nd = NodeProto(
input=[],
output=[onnx_name(op)],
name=onnx_name(op),
attribute=AttributeProto.([attr_name], [attr_value]),
op_type="Constant"
)
push!(g.node, nd)
end
function save_node!(g::GraphProto, ::@opconfig_kw(:ONNX, onnx_gather), op::Umlaut.Call)
data = iskwfunc(op.fn) ? op.args[3]._op.val : op.args[1]._op.val
kw_dict = kwargs2dict(op)
dim = get(kw_dict, :dim, ndims(data))
axis = ndims(data) - dim
nd = NodeProto("Gather", op, Dict(:axis => axis))
push!(g.node, nd)
end
function save_node!(g::GraphProto, ::@opconfig_kw(:ONNX, onnx_unsqueeze), op::Umlaut.Call)
nd = NodeProto("Unsqueeze", op)
push!(g.node, nd)
end
function save_node!(g::GraphProto, ::OpConfig{:ONNX, typeof(onnx_slice)}, op::Umlaut.Call)
nd = NodeProto("Slice", op)
push!(g.node, nd)
end
function save_node!(g::GraphProto, ::@opconfig_kw(:ONNX, onnx_split), op::Umlaut.Call)
attrs = kwargs2dict(op)
args = iskwfunc(op.fn) ? op.args[3:end] : op.args
vars = unpacked_vars(op)
@assert(all([v isa V for v in vars]),
"Not all output vars of split are unpacked to the tape")
output = [onnx_name(v) for v in vars]
nd = NodeProto(
input=[onnx_name(v) for v in args],
output=output,
name=onnx_name(op),
attribute=AttributeProto.(keys(attrs), values(attrs)),
op_type="Split"
)
push!(g.node, nd)
end
function save_node!(g::GraphProto, ::@opconfig_kw(:ONNX, onnx_concat), op::Umlaut.Call)
nd = NodeProto("Concat", op, kwargs2dict(op))
push!(g.node, nd)
end
function save_node!(g::GraphProto, ::OpConfig{:ONNX, typeof(tuple)}, op::Umlaut.Call)
@assert(
op.id == op.tape.result.id,
"tuple() doesn't have a corresponding ONNX op and is only allowed as " *
"the result of the tape, in which case it represents multiple outputs " *
"of the graph"
)
# do nothing
end
##############################################################################
# API #
##############################################################################
"""
save(io::IO, tape::Umlaut.Tape{ONNXCtx})
save(filename::String, tape::Umlaut.Tape{ONNXCtx})
Save tape as an ONNX model. The way a particular operation is serialized is
controlled by methods of [save_node!](@ref).
See also: [`load!`](@ref)
"""
function save(io::IO, tape::Tape{ONNXCtx})
g = graphproto("generated_model")
for (i, op) in enumerate(tape)
if op isa Umlaut.Input
# add input to g.input, but not to g.initializer
push!(g.input, ValueInfoProto(op))
elseif op isa Umlaut.Constant
# add constant to g.initializer, but not to g.input
# some models out there also put constants & parameters
# to g.init, but it seems to be an outdated practise
push!(g.initializer, TensorProto(op.val, onnx_name(op)))
elseif op isa Umlaut.Call
save_node!(g, op)
else
error("$(typeof(op)) is not yet supported in model export")
end
end
res = tape[tape.result]
if res.val isa Tuple
# if the last operation in the graph is multi-output, there must be
# unpacked elements of that var
vars = res.fn === tuple ? res.args : unpacked_vars(res)
@assert(all(v isa V for v in vars), "Cannot save the tape because the result " *
"is multi-output, but its elements aren't destructured to the tape")
for v in vars
push!(g.output, ValueInfoProto(tape[v]))
end
else
push!(g.output, ValueInfoProto(tape[tape.result]))
end
m = modelproto(g);
PB.encode(ProtoEncoder(io), m)
end
function save(filename::String, tape::Tape{ONNXCtx})
open(filename, "w") do io
save(io, tape)
end
end