forked from OpenPPL/ppl.nn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pplnn.lua
296 lines (245 loc) · 9.29 KB
/
pplnn.lua
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
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.
--------------------------------------------------------------------------------
local lua_path = os.getenv('LUAPATH')
if lua_path ~= nil then
package.cpath = lua_path .. '/?.so'
end
--------------------------------------------------------------------------------
local pplnn = require("luappl.nn")
local pplcommon = require("luappl.common")
--------------------------------------------------------------------------------
local logging = {}
function logging.debug(msg)
print("[debug]", msg)
end
function logging.info(msg)
print("[info]", msg)
end
function logging.warning(msg)
print("[warning]", msg)
end
function logging.error(msg)
print("[error]", msg)
end
--------------------------------------------------------------------------------
function GenerateArgs()
return {
onnx_model = "tests/testdata/conv.onnx",
use_x86 = true,
use_cuda = false,
save_data_dir = ".",
save_inputs = false,
save_outputs = false,
}
end
--------------------------------------------------------------------------------
function RegisterEngines(args)
local engines = {}
if args.use_x86 then
local x86_options = pplnn.x86.EngineOptions()
local x86_engine = pplnn.x86.EngineFactory:Create(x86_options)
table.insert(engines, x86_engine)
end
if args.use_cuda then
local cuda_options = pplnn.cuda.EngineOptions()
local cuda_engine = pplnn.cuda.EngineFactory:Create(cuda_options)
table.insert(engines, cuda_engine)
end
return engines
end
--------------------------------------------------------------------------------
function SetRandomInputs(runtime)
function GenerateRandomData(dims)
local nr_element = 1
for _, d in ipairs(dims) do
nr_element = nr_element * d
end
local random_data = ""
for i = 1, nr_element do
random_data = random_data .. string.pack("f", math.random(-100, 100) / 100)
end
return random_data
end
for i = 1, runtime:GetInputCount() do
local tensor = runtime:GetInputTensor(i - 1)
local shape = tensor:GetShape()
local data_type = shape:GetDataType()
local dims = shape:GetDims()
local in_data = GenerateRandomData(dims)
local status = tensor:ConvertFromHost(in_data, dims, data_type)
if status ~= pplcommon.RC_SUCCESS then
logging.error("copy data to tensor[" .. tensor:GetName() .. "] failed: " ..
pplcommon.GetRetCodeStr(status))
os.exit(-1)
end
end
end
--------------------------------------------------------------------------------
function CalcBytes(dims, item_size)
local nbytes = item_size
for _, d in ipairs(dims) do
nbytes = nbytes * d
end
return nbytes
end
function GenDimsStr(dims)
if next(dims) == nil then
return ""
end
local content = tostring(dims[1])
for i = 2, #dims do
content = content .. "_" .. tostring(dims[i])
end
return content
end
--------------------------------------------------------------------------------
function WriteFileContent(filepath, content)
local fd = io.open(filepath, "wb")
fd:write(content)
fd:close()
end
local g_data_type_str = {
[pplcommon.DATATYPE_INT8] = "int8",
[pplcommon.DATATYPE_INT16] = "int16",
[pplcommon.DATATYPE_INT32] = "int32",
[pplcommon.DATATYPE_INT64] = "int64",
[pplcommon.DATATYPE_UINT8] = "uint8",
[pplcommon.DATATYPE_UINT16] = "uint16",
[pplcommon.DATATYPE_UINT32] = "uint32",
[pplcommon.DATATYPE_UINT64] = "uint64",
[pplcommon.DATATYPE_FLOAT16] = "fp16",
[pplcommon.DATATYPE_FLOAT32] = "fp32",
[pplcommon.DATATYPE_FLOAT64] = "fp64",
[pplcommon.DATATYPE_BOOL] = "bool",
[pplcommon.DATATYPE_UNKNOWN] = "unknown",
}
function SaveInputsOneByOne(save_data_dir, runtime)
for i = 1, runtime:GetInputCount() do
local tensor = runtime:GetInputTensor(i - 1)
local shape = tensor:GetShape()
local tensor_data = tensor:ConvertToHost()
if tensor_data == nil then
logging.error("copy data from tensor[" .. tensor:GetName() .. "] failed.")
os.exit(-1)
end
WriteFileContent(save_data_dir .. "/pplnn_input_" .. tostring(i - 1) .. "_" ..
tensor:GetName() .. "-" .. GenDimsStr(shape:GetDims()) .. "-" ..
g_data_type_str[shape:GetDataType()] .. ".dat",
tensor_data)
end
end
--------------------------------------------------------------------------------
function SaveOutputsOneByOne(save_data_dir, runtime)
for i = 1, runtime:GetOutputCount() do
local tensor = runtime:GetOutputTensor(i - 1)
local tensor_data = tensor:ConvertToHost()
if tensor_data == nil then
logging.error("copy data from tensor[" .. tensor:GetName() .. "] failed.")
os.exit(-1)
end
WriteFileContent(save_data_dir .. "/pplnn_output-" .. tensor:GetName() .. ".dat",
tensor_data)
end
end
--------------------------------------------------------------------------------
function PrintInputOutputInfo(runtime)
function Dims2Str(dims)
local ret = ""
for i = 1, #dims do
ret = ret .. " " .. tostring(dims[i])
end
return ret
end
print("----- input info -----")
for i = 1, runtime:GetInputCount() do
local tensor = runtime:GetInputTensor(i - 1)
local shape = tensor:GetShape()
local dims = shape:GetDims()
local data_type = shape:GetDataType()
print("input[" .. tostring(i - 1) .. "]")
print(" name: " .. tensor:GetName())
print(" dim(s):" .. Dims2Str(dims))
print(" data type: " .. pplcommon.GetDataTypeStr(data_type))
print(" data format: " .. pplcommon.GetDataFormatStr(shape:GetDataFormat()))
print(" byte(s) excluding padding: " .. tostring(CalcBytes(dims, pplcommon.GetSizeOfDataType(data_type))))
end
print("----- output info -----")
for i = 1, runtime:GetOutputCount() do
local tensor = runtime:GetOutputTensor(i - 1)
local shape = tensor:GetShape()
local dims = shape:GetDims()
local data_type = shape:GetDataType()
print("output[" .. tostring(i - 1) .. "]")
print(" name: " .. tensor:GetName())
print(" dim(s):" .. Dims2Str(dims))
print(" type: " .. pplcommon.GetDataTypeStr(data_type))
print(" format: " .. pplcommon.GetDataFormatStr(shape:GetDataFormat()))
print(" byte(s) excluding padding: " .. tostring(CalcBytes(dims, pplcommon.GetSizeOfDataType(data_type))))
end
print("----------------------")
end
------------------------------------ main --------------------------------------
print("PPLNN version: [" .. pplnn.PPLNN_VERSION_MAJOR .. "." .. pplnn.PPLNN_VERSION_MINOR .. "." ..
pplnn.PPLNN_VERSION_PATCH .. "], commit: [" .. pplnn.PPLNN_COMMIT_STR .. "]")
local args = GenerateArgs()
local runtime_builder = pplnn.onnx.RuntimeBuilderFactory:Create()
if runtime_builder == nil then
logging.error("create OnnxRuntimeBuilder failed.")
os.exit(-1)
end
local status = runtime_builder:LoadModelFromFile(args.onnx_model)
if status ~= pplcommon.RC_SUCCESS then
logging.error("init OnnxRuntimeBuilder failed: " .. pplcommon.GetRetCodeStr(status))
os.exit(-1)
end
local engines = RegisterEngines(args)
if next(engines) == nil then
logging.info("no engine is specified.")
os.exit(-1)
end
resources = pplnn.onnx.RuntimeBuilderResources()
resources.engines = engines
status = runtime_builder:SetResources(resources)
if status ~= pplcommon.RC_SUCCESS then
logging.error("OnnxRuntimeBuilder set resources failed: " .. pplcommon.GetRetCodeStr(status))
os.exit(-1)
end
status = runtime_builder:Preprocess()
if status ~= pplcommon.RC_SUCCESS then
logging.error("OnnxRuntimeBuilder preprocess failed: " .. pplcommon.GetRetCodeStr(status))
os.exit(-1)
end
local runtime = runtime_builder:CreateRuntime()
if runtime == nil then
logging.error("create Runtime instance failed.")
os.exit(-1)
end
SetRandomInputs(runtime)
if args.save_inputs then
SaveInputsOneByOne(args.save_data_dir, runtime)
end
local status = runtime:Run()
if status ~= pplcommon.RC_SUCCESS then
logging.error("Run() failed: " .. pplcommon.GetRetCodeStr(status))
os.exit(-1)
end
PrintInputOutputInfo(runtime)
if args.save_outputs then
SaveOutputsOneByOne(args.save_data_dir, runtime)
end
logging.info("Run ok")