-
Notifications
You must be signed in to change notification settings - Fork 23
/
run_onnx_util.cc
356 lines (319 loc) · 12.9 KB
/
run_onnx_util.cc
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
#include "tools/run_onnx_util.h"
#include <algorithm>
#include <chrono>
#include <cstdlib>
#include <fstream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <compiler/onnx.h>
#include <chainerx/array.h>
#include <chainerx/native/native_backend.h>
#include <chainerx/routines/creation.h>
#include <common/log.h>
#include <common/protoutil.h>
#include <common/strutil.h>
#include <compiler/custom_onnx_ops.h>
#include <compiler/flags.h>
#include <compiler/graph.h>
#include <compiler/model.h>
#include <compiler/passes.h>
#include <compiler/tensor.h>
#include <compiler/util.h>
#include <compiler/value.h>
#include <runtime/chainerx_util.h>
#include <runtime/chxvm.h>
#include <runtime/chxvm.pb.h>
#include <runtime/chxvm_var.h>
#include <tools/cmdline.h>
#include <tools/compiler_flags.h>
#include <tools/log.h>
#include <tools/util.h>
namespace chainer_compiler {
namespace runtime {
chainerx::Array MakeArrayFromONNX(const onnx::TensorProto& xtensor) {
Tensor tensor(xtensor);
int64_t size = tensor.ElementSize() * tensor.NumElements();
std::shared_ptr<void> data(new char[size], std::default_delete<char[]>());
std::memcpy(data.get(), tensor.GetRawData(), size);
chainerx::Shape shape(tensor.dims());
chainerx::Dtype dtype;
switch (tensor.dtype()) {
#define ASSIGN_DTYPE(n) \
case Dtype::n: \
dtype = chainerx::Dtype::n; \
break
ASSIGN_DTYPE(kBool);
ASSIGN_DTYPE(kInt8);
ASSIGN_DTYPE(kInt16);
ASSIGN_DTYPE(kInt32);
ASSIGN_DTYPE(kInt64);
ASSIGN_DTYPE(kUInt8);
ASSIGN_DTYPE(kFloat16);
ASSIGN_DTYPE(kFloat32);
ASSIGN_DTYPE(kFloat64);
default:
CHECK(false) << "Unknown data type: " << static_cast<int>(tensor.dtype());
}
chainerx::Array array(
chainerx::FromData(shape, dtype, data, absl::nullopt /* strides */, 0 /* offset */, chainerx::GetNativeBackend().GetDevice(0)));
return array;
}
std::string OnnxPathFromTestDir(const std::string& test_dir) {
std::ifstream ifs(test_dir + "/model.onnx");
if (ifs) {
return test_dir + "/model.onnx";
}
std::string candidate_onnx;
for (const std::string& fn : ListDir(test_dir)) {
if (HasSuffix(fn, ".onnx")) {
CHECK(candidate_onnx.empty()) << "Multiple onnx files detected: " << candidate_onnx << ", " << fn << ", ...";
candidate_onnx = fn;
continue;
}
}
return candidate_onnx;
}
void ReadTestDir(
const std::string& test_path,
const std::vector<std::string>& input_names,
const std::vector<std::string>& output_names,
std::vector<std::unique_ptr<TestCase>>* test_cases) {
for (const std::string& data_set_dir : ListDir(test_path)) {
if (!HasPrefix(Basename(data_set_dir), "test_data_set_") || !IsDir(data_set_dir)) {
continue;
}
std::unique_ptr<TestCase> test_case(new TestCase);
test_case->name = data_set_dir;
size_t input_index = 0;
size_t output_index = 0;
std::vector<std::tuple<std::string, std::string, chainerx::Array>> all_tensors;
for (const std::string& tensor_pb : ListDir(data_set_dir)) {
if (!HasSuffix(tensor_pb, ".pb")) continue;
// Ignore some files for test data like MobileNet v2
if (HasPrefix(*SplitString(tensor_pb, "/").rbegin(), "._")) continue;
onnx::TensorProto xtensor(LoadLargeProto<onnx::TensorProto>(tensor_pb));
chainerx::Array tensor(MakeArrayFromONNX(xtensor));
all_tensors.emplace_back(Basename(tensor_pb), xtensor.name(), tensor);
}
std::vector<std::tuple<std::string, std::string, ChxVMVar*>> all_vars;
for (size_t i = 0; i < all_tensors.size(); ++i) {
const std::string& filename = std::get<0>(all_tensors[i]);
const std::string& tensor_name = std::get<1>(all_tensors[i]);
size_t first_found = filename.find('_');
if (first_found == std::string::npos) continue;
size_t found = filename.find('_', first_found + 1);
if (found == std::string::npos) {
all_vars.emplace_back(filename, tensor_name, new ChxVMVar(std::get<2>(all_tensors[i])));
continue;
}
std::string prefix = filename.substr(0, found + 1);
auto seq = std::make_shared<ChxVMSequence>();
for (; i < all_tensors.size(); ++i) {
const std::string& filename = std::get<0>(all_tensors[i]);
if (HasPrefix(filename, prefix)) {
CHECK_EQ(tensor_name, std::get<1>(all_tensors[i]));
seq->emplace_back(std::get<2>(all_tensors[i]));
} else {
--i;
break;
}
}
all_vars.emplace_back(filename, tensor_name, new ChxVMVar(seq));
}
for (const auto& p : all_vars) {
const std::string& filename = std::get<0>(p);
std::string tensor_name = std::get<1>(p);
std::shared_ptr<ChxVMVar> var(std::get<2>(p));
if (HasPrefix(filename, "input_")) {
if (tensor_name.empty()) {
CHECK_LT(input_index, input_names.size());
tensor_name = input_names[input_index++];
}
CHECK(test_case->inputs.emplace(tensor_name, var).second) << "Duplicate input tensor: " << tensor_name;
} else if (HasPrefix(filename, "output_")) {
if (tensor_name.empty()) {
CHECK_LT(output_index, output_names.size());
tensor_name = output_names[output_index++];
}
CHECK(test_case->outputs.emplace(tensor_name, var).second) << "Duplicate output tensor:" << tensor_name;
} else if (HasPrefix(filename, "gradient_")) {
CHECK(!tensor_name.empty());
CHECK(test_case->outputs.emplace("grad_out@" + tensor_name, var).second) << "Duplicate gradient tensor:" << tensor_name;
}
}
test_cases->emplace_back(std::move(test_case));
}
CHECK(!test_cases->empty()) << "No test found in " << test_path;
}
chainerx::Shape ChainerXShapeFromONNX(const onnx::TensorShapeProto& xshape) {
chainerx::Shape shape;
for (const auto& dim : xshape.dim()) {
if (dim.has_dim_value()) {
shape.push_back(dim.dim_value());
} else {
LOG() << "Dimension " << dim.dim_param() << " was replaced by 1" << std::endl;
shape.push_back(1);
}
}
return shape;
}
chainerx::Array StageArray(chainerx::Array a) {
// TODO(hamaji): Figure out a better way to identify host inputs.
if (a.dtype() != chainerx::Dtype::kInt64) return a.ToDevice(chainerx::GetDefaultDevice());
return a;
}
void VerifyOutputs(
const InOuts& outputs,
const TestCase& test_case,
const cmdline::parser& args,
bool check_values,
bool show_diff,
std::vector<std::string> ordered_output_names) {
if (ordered_output_names.empty()) {
for (const auto& p : test_case.outputs) {
const std::string key = p.first;
ordered_output_names.push_back(key);
}
}
LOG() << "Verifying the result..." << std::endl;
size_t ok_cnt = 0;
for (const std::string& key : ordered_output_names) {
auto expected_found = test_case.outputs.find(key);
if (expected_found == test_case.outputs.end()) {
continue;
}
ChxVMVar* expected = expected_found->second.get();
auto actual_found = outputs.find(key);
CHECK(actual_found != outputs.end()) << "Output does not contain " << key;
ChxVMVar* actual = actual_found->second.get();
auto array_str = [&args](const absl::optional<chainerx::Array>& a) {
int size = a->GetTotalSize();
if (size < 100 || args.exist("verbose")) return a->ToString();
return a->shape().ToString() + " [0,20]=" + a->Reshape({size}).At({chainerx::Slice{20}}).ToString();
};
auto var_str = [&args, array_str](ChxVMVar* v) {
switch (v->kind()) {
case ChxVMVar::Kind::kScalar:
case ChxVMVar::Kind::kShape:
case ChxVMVar::Kind::kArray:
return array_str(v->GetArray());
case ChxVMVar::Kind::kSequence:
return '[' + JoinString(MapToString(NonOptional(*v->GetSequence()), array_str)) + ']';
case ChxVMVar::Kind::kString:
case ChxVMVar::Kind::kOpaque:
case ChxVMVar::Kind::kNull:
CHECK(false) << v->DebugString();
}
CHECK(false);
};
auto fail = [&](const std::string& type) {
LOG() << RED << "FAIL(" << type << "): " << key << RESET << "\nExpected: " << var_str(expected)
<< "\nActual: " << var_str(actual) << std::endl;
};
auto check_array = [&](const chainerx::Array& expected, const chainerx::Array& actual) {
if (expected.dtype() != actual.dtype()) {
fail("dtype");
return false;
}
if (expected.shape() != actual.shape()) {
fail("shape");
return false;
}
if (!check_values && !show_diff) return true;
int mismatch =
MismatchInAllClose(expected, actual, args.get<double>("rtol"), args.get<double>("atol"), args.exist("equal_nan"));
if (mismatch) {
fail("value");
int total_size = expected.GetTotalSize();
LOG() << "Mismatch: " << mismatch << " / " << total_size << " (" << static_cast<double>(mismatch) * 100.0 / total_size
<< "%)" << std::endl;
if (show_diff && !check_values) {
return true;
}
return false;
}
return true;
};
if (!expected->IsArray() && !actual->IsArray() && expected->kind() != actual->kind()) {
fail("kind");
continue;
}
bool ok = false;
switch (expected->kind()) {
case ChxVMVar::Kind::kScalar:
case ChxVMVar::Kind::kShape:
case ChxVMVar::Kind::kArray:
ok = check_array(expected->GetArray(), actual->GetArray());
break;
case ChxVMVar::Kind::kSequence: {
const auto& expected_seq = *expected->GetSequence();
const auto& actual_seq = *actual->GetSequence();
if (expected_seq.size() != actual_seq.size()) {
fail("seq_size");
ok = false;
break;
}
for (size_t i = 0; i < expected_seq.size(); ++i) {
ok = check_array(expected_seq[i].GetArray(), actual_seq[i].GetArray());
if (!ok) break;
}
break;
}
case ChxVMVar::Kind::kString:
case ChxVMVar::Kind::kOpaque:
case ChxVMVar::Kind::kNull:
CHECK(false) << expected->DebugString();
}
if (!ok) continue;
LOG() << "OK: " << key << std::endl;
++ok_cnt;
}
if (check_values) CHECK_EQ(ok_cnt, test_case.outputs.size());
}
static void AddCommonRuntimeFlags(cmdline::parser* args) {
args->add("trace", 't', "Tracing mode");
args->add("verbose", 'v', "Verbose mode");
args->add("quiet", 'q', "Quiet mode");
args->add<std::string>("backend", '\0', "The name of the backend", false, "chxvm");
}
void ParseArgs(cmdline::parser* args, int argc, char** argv) {
AddCompilerFlags(args);
AddCommonRuntimeFlags(args);
args->parse_check(argc, argv);
}
void ParseArgs(cmdline::parser* args, const std::vector<std::string>& argv) {
AddCompilerFlags(args);
AddCommonRuntimeFlags(args);
args->parse_check(argv);
}
void SetupGlobals(const cmdline::parser& args) {
ApplyCompilerFlags(args);
g_compiler_log |= args.exist("trace") || args.exist("verbose");
g_backend_name = args.get<std::string>("backend");
g_quiet = args.exist("quiet");
}
std::vector<std::string> GetOrderedOutputNames(const Graph& graph) {
typedef std::pair<Value*, int> Pair;
std::vector<Pair> values = graph.GetTopologicallySortedValuesWithDistance();
std::sort(values.begin(), values.end(), [](const Pair& l, const Pair& r) {
if (l.second < r.second) {
return true;
} else if (l.second > r.second) {
return false;
}
Value* lv = l.first;
Value* rv = r.first;
return lv->name() < rv->name();
});
std::vector<std::string> ordered;
for (const Pair& p : values) {
if (p.first->IsOutput()) {
ordered.push_back(p.first->name());
}
}
return ordered;
}
} // namespace runtime
} // namespace chainer_compiler