-
Notifications
You must be signed in to change notification settings - Fork 477
/
Copy pathonnx_helper.h
561 lines (522 loc) · 22.6 KB
/
onnx_helper.h
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed 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.
#pragma once
#include <onnx/onnx_pb.h>
#include <memory>
#include <string>
#include <vector>
#include "paddle2onnx/mapper/register_mapper.h"
#include "paddle2onnx/parser/parser.h"
namespace paddle2onnx {
void AddAttribute(std::shared_ptr<ONNX_NAMESPACE::NodeProto> node,
const std::string& name, const int64_t& value);
void AddAttribute(std::shared_ptr<ONNX_NAMESPACE::NodeProto> node,
const std::string& name, const float& value);
void AddAttribute(std::shared_ptr<ONNX_NAMESPACE::NodeProto> node,
const std::string& name, const std::string& value);
void AddAttribute(std::shared_ptr<ONNX_NAMESPACE::NodeProto> node,
const std::string& name, const std::vector<int64_t>& values);
void AddAttribute(std::shared_ptr<ONNX_NAMESPACE::NodeProto> node,
const std::string& name, const std::vector<float>& values);
void AddAttribute(std::shared_ptr<ONNX_NAMESPACE::NodeProto> node,
const std::string& name,
ONNX_NAMESPACE::TensorProto_DataType dtype);
ONNX_NAMESPACE::TensorProto_DataType GetOnnxDtype(int32_t paddle_dtype);
std::shared_ptr<ONNX_NAMESPACE::NodeProto> MakeConstant(const std::string& name,
const Weight& weight);
std::shared_ptr<ONNX_NAMESPACE::ValueInfoProto> MakeValueInfo(
const TensorInfo& info);
struct QuantizeInfo {
public:
std::vector<float> scale_;
std::vector<int64_t> zeros_;
std::string zeros_node_;
std::string scale_node_;
int64_t quantize_axis_;
QuantizeInfo() {}
QuantizeInfo(const std::vector<float>& scale,
const std::vector<int64_t>& zeros, const std::string& scale_node,
const std::string& zeros_node, const int64_t& quantize_axis) {
zeros_node_ = zeros_node;
scale_node_ = scale_node;
quantize_axis_ = quantize_axis;
scale_.resize(scale.size());
memcpy(scale_.data(), scale.data(), scale.size() * sizeof(float));
zeros_.resize(zeros.size());
memcpy(zeros_.data(), zeros.data(), zeros.size() * sizeof(int64_t));
}
};
class OnnxHelper {
public:
std::vector<std::shared_ptr<ONNX_NAMESPACE::NodeProto>> nodes;
std::vector<std::shared_ptr<ONNX_NAMESPACE::ValueInfoProto>> value_infos;
int32_t opset_version = 7;
// Use updated_params to store params that were changed during conversion
std::map<std::string, Weight> updated_params;
// Use quantize_info to record quantization-related information, scale and
// zero information corresponding to each tensor
std::map<std::string, QuantizeInfo> quantize_info;
void Clear() { nodes.clear(); }
void SetOpsetVersion(int32_t op_v) { opset_version = op_v; }
int32_t GetOpsetVersion() { return opset_version; }
template <typename T>
bool TryGetTensorValue(const std::string& name, std::vector<T>* value);
std::shared_ptr<ONNX_NAMESPACE::ValueInfoProto> MakeValueInfo(
const std::string& name, const int32_t& dtype,
std::vector<int64_t>& shape);
std::shared_ptr<ONNX_NAMESPACE::NodeProto> MakeNode(
const std::string& op_type, const std::vector<std::string>& inputs,
const std::vector<std::string>& outputs);
// we use this function to generate some temporary node
// we do not need to define the outputs, because the outputs
// is generate by MapperHelper, which will make sure there's no
// name confict problem
// the parameter `num_outputs` will define the number of output names
std::shared_ptr<ONNX_NAMESPACE::NodeProto> MakeNode(
const std::string& op_type, const std::vector<std::string>& inputs,
int num_outputs = 1);
template <typename T>
std::string ConstOfShape(const std::string& input, const std::string& output,
ONNX_NAMESPACE::TensorProto_DataType dtype, T value);
template <typename T>
std::string ConstOfShape(const std::string& input,
ONNX_NAMESPACE::TensorProto_DataType dtype, T value);
std::string AutoCast(const std::string& input, int32_t input_paddle_dtype,
int32_t to_paddle_dtype);
std::string AutoCast(const std::string& input, const std::string& output,
int32_t input_paddle_dtype, int32_t to_paddle_dtype);
// Helper function for PaddlePaddle's shape tensor list inputs
// will cast all data type to int64
// will make sure all inputs to be 1-D tensor
// will concat them as output
std::string ConcatIndices(const std::vector<TensorInfo>& indices);
std::vector<std::string> DtypeAlignment(
const std::vector<TensorInfo>& input_info, int32_t* out_dtype);
std::string Clip(const std::string& input, const float& min, const float& max,
const int32_t& in_dtype);
std::string Clip(const std::string& input, const std::string& output,
const float& min, const float& max, const int32_t& in_dtype);
std::string Squeeze(const std::string& input,
const std::vector<int64_t>& axes);
std::string Squeeze(const std::string& input, const std::string& output,
const std::vector<int64_t>& axes);
std::string Unsqueeze(const std::string& input,
const std::vector<int64_t>& axes);
std::string Unsqueeze(const std::string& input, const std::string& output,
const std::vector<int64_t>& axes);
std::string Reshape(const std::string& input, const std::string& output,
const std::vector<int64_t>& shape);
std::string Reshape(const std::string& input,
const std::vector<int64_t>& shape);
std::string Flatten(const std::string& input, const std::string& output);
std::string Flatten(const std::string& input);
std::string Slice(const std::string& input, const std::string& output,
const std::vector<int64_t>& axes,
const std::vector<int64_t>& starts,
const std::vector<int64_t>& ends);
std::string Slice(const std::string& input, const std::vector<int64_t>& axes,
const std::vector<int64_t>& starts,
const std::vector<int64_t>& ends);
std::string Concat(const std::vector<std::string>& input,
const std::string& output, int64_t axis);
std::string Concat(const std::vector<std::string>& input, int64_t axis);
std::string Transpose(const std::string& input, const std::string& output,
const std::vector<int64_t>& perm);
std::string Transpose(const std::string& input,
const std::vector<int64_t>& perm);
std::vector<std::string> Split(const std::string& input,
const std::vector<std::string>& outputs,
const std::vector<int64_t>& split,
int64_t axis);
std::vector<std::string> Split(const std::string& input,
const std::vector<int64_t>& split,
int64_t axis);
template <typename T>
std::string Constant(const std::string& output,
ONNX_NAMESPACE::TensorProto_DataType dtype,
const std::vector<T>& value);
template <typename T>
std::string Constant(ONNX_NAMESPACE::TensorProto_DataType dtype,
const std::vector<T>& value);
template <typename T>
std::string Constant(const std::string& output,
const std::vector<int64_t>& shape,
ONNX_NAMESPACE::TensorProto_DataType dtype, T value);
template <typename T>
std::string Constant(const std::vector<int64_t>& shape,
ONNX_NAMESPACE::TensorProto_DataType dtype, T value);
template <typename T>
std::string Constant(const std::vector<int64_t>& shape,
ONNX_NAMESPACE::TensorProto_DataType dtype,
std::vector<T>& value);
template <typename T>
std::string Assign(const std::string& output,
const ONNX_NAMESPACE::TensorProto_DataType& dtype,
const std::vector<int64_t>& shape,
const std::vector<T>& value);
template <typename T>
std::string Assign(const ONNX_NAMESPACE::TensorProto_DataType& dtype,
const std::vector<int64_t>& shape,
const std::vector<T>& value);
};
template <typename T>
std::string OnnxHelper::Constant(const std::vector<int64_t>& shape,
ONNX_NAMESPACE::TensorProto_DataType dtype,
std::vector<T>& value) {
auto node = std::make_shared<ONNX_NAMESPACE::NodeProto>();
node->set_op_type("Constant");
auto name = MapperHelper::Get()->GenName("const");
node->add_output(name);
auto attr = node->add_attribute();
attr->set_name("value");
attr->set_type(ONNX_NAMESPACE::AttributeProto::TENSOR);
auto tensor = attr->mutable_t();
tensor->set_name(name);
int numel = 1;
for (size_t i = 0; i < shape.size(); ++i) {
tensor->add_dims(shape[i]);
numel *= shape[i];
}
Assert(numel == value.size(),
"numel and val number is not equal in Constant "
"function.");
tensor->set_data_type(dtype);
if (dtype == ONNX_NAMESPACE::TensorProto::FLOAT) {
std::vector<float> data;
data.reserve(numel);
for (auto& i : value) {
data.push_back(static_cast<float>(i));
}
tensor->set_raw_data(std::string((const char*)(data.data()), numel * 4));
} else if (dtype == ONNX_NAMESPACE::TensorProto::DOUBLE) {
std::vector<double> data;
data.reserve(numel);
for (auto& i : value) {
data.push_back(static_cast<double>(i));
}
tensor->set_raw_data(std::string((const char*)(data.data()), numel * 8));
} else if (dtype == ONNX_NAMESPACE::TensorProto::INT64) {
std::vector<int64_t> data;
data.reserve(numel);
for (auto& i : value) {
data.push_back(static_cast<int64_t>(i));
}
tensor->set_raw_data(std::string((const char*)(data.data()), numel * 8));
} else if (dtype == ONNX_NAMESPACE::TensorProto::BOOL) {
bool* data = new bool[numel];
for (size_t i = 0; i < numel; ++i) {
data[i] = static_cast<bool>(value[i]);
}
tensor->set_raw_data(std::string((const char*)(data), numel));
delete[] data;
} else {
Assert(false,
"Only support data type of BOOL/FLOAT/DOUBLE/INT64 in Constant "
"function.");
}
nodes.push_back(node);
return node->output(0);
}
template <typename T>
std::string OnnxHelper::Constant(const std::string& output,
ONNX_NAMESPACE::TensorProto_DataType dtype,
const std::vector<T>& value) {
auto node = std::make_shared<ONNX_NAMESPACE::NodeProto>();
node->set_op_type("Constant");
node->add_output(output);
auto attr = node->add_attribute();
attr->set_name("value");
attr->set_type(ONNX_NAMESPACE::AttributeProto::TENSOR);
auto tensor = attr->mutable_t();
tensor->set_name(output);
int numel = value.size();
tensor->add_dims(numel);
tensor->set_data_type(dtype);
if (value.size() == 0) {
nodes.push_back(node);
return output;
}
if (dtype == ONNX_NAMESPACE::TensorProto::FLOAT) {
std::vector<float> data;
for (auto& item : value) {
data.push_back(static_cast<float>(item));
}
tensor->set_raw_data(std::string((const char*)(data.data()), numel * 4));
} else if (dtype == ONNX_NAMESPACE::TensorProto::DOUBLE) {
std::vector<double> data;
for (auto& item : value) {
data.push_back(static_cast<double>(item));
}
tensor->set_raw_data(std::string((const char*)(data.data()), numel * 8));
} else if (dtype == ONNX_NAMESPACE::TensorProto::INT64) {
std::vector<int64_t> data;
for (auto& item : value) {
data.push_back(static_cast<int64_t>(item));
}
tensor->set_raw_data(std::string((const char*)(data.data()), numel * 8));
} else if (dtype == ONNX_NAMESPACE::TensorProto::INT32) {
std::vector<int32_t> data;
for (auto& item : value) {
data.push_back(static_cast<int32_t>(item));
}
tensor->set_raw_data(std::string((const char*)(data.data()), numel * 4));
} else if (dtype == ONNX_NAMESPACE::TensorProto::BOOL) {
bool* data = new bool[numel];
for (size_t i = 0; i < numel; ++i) {
data[i] = static_cast<bool>(value[i]);
}
tensor->set_raw_data(std::string((const char*)(data), numel));
delete[] data;
} else if (dtype == ONNX_NAMESPACE::TensorProto::INT8) {
std::vector<int8_t> data;
data.reserve(numel);
for (auto& i : value) {
data.push_back(static_cast<int8_t>(i));
}
tensor->set_raw_data(std::string((const char*)(data.data()), numel));
} else {
Assert(false,
"Only support data type of BOOL/FLOAT/DOUBLE/INT32/INT64/INT8 in "
"Constant "
"function.");
}
nodes.push_back(node);
return output;
}
template <typename T>
std::string OnnxHelper::Constant(ONNX_NAMESPACE::TensorProto_DataType dtype,
const std::vector<T>& value) {
auto output = MapperHelper::Get()->GenName("helper.constant");
return Constant(output, dtype, value);
}
template <typename T>
std::string OnnxHelper::Constant(const std::string& output,
const std::vector<int64_t>& shape,
ONNX_NAMESPACE::TensorProto_DataType dtype,
T value) {
auto node = std::make_shared<ONNX_NAMESPACE::NodeProto>();
node->set_op_type("Constant");
node->add_output(output);
auto attr = node->add_attribute();
attr->set_name("value");
attr->set_type(ONNX_NAMESPACE::AttributeProto::TENSOR);
auto tensor = attr->mutable_t();
tensor->set_name(output);
int numel = 1;
for (size_t i = 0; i < shape.size(); ++i) {
tensor->add_dims(shape[i]);
numel *= shape[i];
}
tensor->set_data_type(dtype);
if (dtype == ONNX_NAMESPACE::TensorProto::FLOAT) {
std::vector<float> data(numel, static_cast<float>(value));
tensor->set_raw_data(std::string((const char*)(data.data()), numel * 4));
} else if (dtype == ONNX_NAMESPACE::TensorProto::DOUBLE) {
std::vector<double> data(numel, static_cast<double>(value));
tensor->set_raw_data(std::string((const char*)(data.data()), numel * 8));
} else if (dtype == ONNX_NAMESPACE::TensorProto::INT64) {
std::vector<int64_t> data(numel, static_cast<int64_t>(value));
tensor->set_raw_data(std::string((const char*)(data.data()), numel * 8));
} else if (dtype == ONNX_NAMESPACE::TensorProto::INT32) {
std::vector<int32_t> data(numel, static_cast<int32_t>(value));
tensor->set_raw_data(std::string((const char*)(data.data()), numel * 4));
} else if (dtype == ONNX_NAMESPACE::TensorProto::INT8) {
std::vector<int8_t> data(numel, static_cast<int8_t>(value));
tensor->set_raw_data(std::string((const char*)(data.data()), numel));
} else if (dtype == ONNX_NAMESPACE::TensorProto::BOOL) {
bool* data = new bool[numel];
for (size_t i = 0; i < numel; ++i) {
data[i] = static_cast<bool>(value);
}
tensor->set_raw_data(std::string((const char*)(data), numel));
delete[] data;
} else {
Assert(
false,
"Only support data type of BOOL/FLOAT/DOUBLE/INT32/INT64 in Constant "
"function.");
}
nodes.push_back(node);
return output;
}
template <typename T>
std::string OnnxHelper::Constant(const std::vector<int64_t>& shape,
ONNX_NAMESPACE::TensorProto_DataType dtype,
T value) {
auto output = MapperHelper::Get()->GenName("helper.constant");
return Constant(output, shape, dtype, value);
}
template <typename T>
std::string OnnxHelper::ConstOfShape(const std::string& input,
ONNX_NAMESPACE::TensorProto_DataType dtype,
T value) {
auto output = MapperHelper::Get()->GenName("helper.constofshape");
return ConstOfShape(input, output, dtype, value);
}
template <typename T>
std::string OnnxHelper::ConstOfShape(const std::string& input,
const std::string& output,
ONNX_NAMESPACE::TensorProto_DataType dtype,
T value) {
auto node = MakeNode("ConstantOfShape", {input}, {output});
auto attr = node->add_attribute();
attr->set_name("value");
attr->set_type(ONNX_NAMESPACE::AttributeProto::TENSOR);
auto tensor = attr->mutable_t();
tensor->set_name("tensor_value");
std::vector<int64_t> shape = {1};
int numel = 1;
for (size_t i = 0; i < shape.size(); ++i) {
tensor->add_dims(shape[i]);
numel *= shape[i];
}
tensor->set_data_type(dtype);
if (dtype == ONNX_NAMESPACE::TensorProto::FLOAT) {
std::vector<float> data(numel, static_cast<float>(value));
tensor->set_raw_data(std::string((const char*)(data.data()), numel * 4));
} else if (dtype == ONNX_NAMESPACE::TensorProto::DOUBLE) {
std::vector<double> data(numel, static_cast<double>(value));
tensor->set_raw_data(std::string((const char*)(data.data()), numel * 8));
} else if (dtype == ONNX_NAMESPACE::TensorProto::INT64) {
std::vector<int64_t> data(numel, static_cast<int64_t>(value));
tensor->set_raw_data(std::string((const char*)(data.data()), numel * 8));
} else if (dtype == ONNX_NAMESPACE::TensorProto::INT32) {
std::vector<int32_t> data(numel, static_cast<int32_t>(value));
tensor->set_raw_data(std::string((const char*)(data.data()), numel * 4));
} else {
Assert(false,
"Only support data type of FLOAT/DOUBLE/INT64/INT32 in ConstOfShape "
"function.");
}
return output;
}
template <typename T>
std::string OnnxHelper::Assign(
const std::string& output,
const ONNX_NAMESPACE::TensorProto_DataType& dtype,
const std::vector<int64_t>& shape, const std::vector<T>& value) {
auto node = std::make_shared<ONNX_NAMESPACE::NodeProto>();
node->set_op_type("Constant");
node->add_output(output);
auto attr = node->add_attribute();
attr->set_name("value");
attr->set_type(ONNX_NAMESPACE::AttributeProto::TENSOR);
auto tensor = attr->mutable_t();
tensor->set_name(output);
int numel = std::accumulate(std::begin(shape), std::end(shape), 1,
std::multiplies<int64_t>());
Assert(numel == value.size(),
"Numel of value not satisfy the input shape while creating contant "
"tensor.");
for (size_t i = 0; i < shape.size(); ++i) {
tensor->add_dims(shape[i]);
}
tensor->set_data_type(dtype);
if (dtype == ONNX_NAMESPACE::TensorProto::FLOAT) {
std::vector<float> data;
for (auto& item : value) {
data.push_back(static_cast<float>(item));
}
tensor->set_raw_data(std::string((const char*)(data.data()), numel * 4));
} else if (dtype == ONNX_NAMESPACE::TensorProto::DOUBLE) {
std::vector<double> data;
for (auto& item : value) {
data.push_back(static_cast<double>(item));
}
tensor->set_raw_data(std::string((const char*)(data.data()), numel * 8));
} else if (dtype == ONNX_NAMESPACE::TensorProto::INT64) {
std::vector<int64_t> data;
for (auto& item : value) {
data.push_back(static_cast<int64_t>(item));
}
tensor->set_raw_data(std::string((const char*)(data.data()), numel * 8));
} else if (dtype == ONNX_NAMESPACE::TensorProto::INT32) {
std::vector<int32_t> data;
for (auto& item : value) {
data.push_back(static_cast<int32_t>(item));
}
tensor->set_raw_data(std::string((const char*)(data.data()), numel * 4));
} else {
Assert(false,
"Only support data type of FLOAT/DOUBLE/INT32/INT64 in Constant "
"function.");
}
nodes.push_back(node);
return output;
}
template <typename T>
std::string OnnxHelper::Assign(
const ONNX_NAMESPACE::TensorProto_DataType& dtype,
const std::vector<int64_t>& shape, const std::vector<T>& value) {
auto output = MapperHelper::Get()->GenName("helper.constant");
return Assign(output, dtype, shape, value);
}
template <typename T>
bool OnnxHelper::TryGetTensorValue(const std::string& name,
std::vector<T>* value) {
for (auto iter = nodes.begin(); iter != nodes.end(); iter++) {
auto node = *iter;
if (node->op_type() != "Constant") {
continue;
}
if (node->output(0) == name) {
for (auto i = 0; i < node->attribute_size(); i++) {
auto attr = node->attribute(i);
if (attr.name() == "value") {
auto tensor = attr.mutable_t();
auto dtype = tensor->data_type();
std::vector<int64_t> shape;
for (int64_t i = 0; i < tensor->dims_size(); i++) {
shape.push_back(tensor->dims(i));
}
int64_t nums = 1;
for (auto& i : shape) nums *= i;
value->resize(nums);
if (dtype == ONNX_NAMESPACE::TensorProto::INT64) {
std::vector<int64_t> val(nums, 0);
memcpy(val.data(), tensor->raw_data().data(),
nums * sizeof(int64_t));
value->assign(val.begin(), val.end());
return true;
} else if (dtype == ONNX_NAMESPACE::TensorProto::INT32) {
std::vector<int32_t> val(nums, 0);
memcpy(val.data(), tensor->raw_data().data(),
nums * sizeof(int32_t));
value->assign(val.begin(), val.end());
return true;
} else if (dtype == ONNX_NAMESPACE::TensorProto::FLOAT) {
std::vector<float> val(nums, 0);
memcpy(val.data(), tensor->raw_data().data(), nums * sizeof(float));
value->assign(val.begin(), val.end());
return true;
} else if (dtype == ONNX_NAMESPACE::TensorProto::DOUBLE) {
std::vector<double> val(nums, 0);
memcpy(val.data(), tensor->raw_data().data(),
nums * sizeof(double));
value->assign(val.begin(), val.end());
return true;
} else {
P2OLogger() << "[WARNING] OnnxHelper function TryGetTensorValue "
"only support get int64_t/int32_t/float/double "
"value from Constant now."
<< std::endl;
return false;
}
}
}
}
}
return false;
}
} // namespace paddle2onnx