-
Notifications
You must be signed in to change notification settings - Fork 106
/
gen_case.h
685 lines (655 loc) · 28 KB
/
gen_case.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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/*************************************************************************
* Copyright (C) [2022] by Cambricon, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*************************************************************************/
#ifndef CORE_GEN_CASE_H_
#define CORE_GEN_CASE_H_
#include <vector>
#include <iomanip>
#include <string>
#include <limits>
#include <utility>
#include "mlu_op.h"
#include "core/tensor.h"
#include "core/tool.h"
// macro function for user
#define MLUOP_GEN_CASE_ON (mluop::gen_case::isGenCaseOn())
// #define MLUOP_GEN_CASE_ON_NEW (mluop::gen_case::isGenCaseOn())
#define MLUOP_GEN_CASE_ON_NEW (mluop::gen_case::genCaseModeGet(true) > 0)
#define GEN_CASE_START(op_name, op_type) \
mluop::gen_case::PbNode *node = \
mluop::gen_case::genCaseStart(op_name, op_type)
#define GEN_CASE_DATA(is_input, id, data, data_desc, upper_bound, lower_bound) \
mluop::gen_case::genCaseData(node, is_input, id, data, data_desc, \
upper_bound, lower_bound)
// when distribution is "GAUSSIAN", upper_bound is mu, lower_bound is sigma.
#define GEN_CASE_DATA_v2(is_input, id, data, data_desc, upper_bound, \
lower_bound, distribution) \
mluop::gen_case::genCaseData(node, is_input, id, data, data_desc, \
upper_bound, lower_bound, distribution)
#define GEN_CASE_DATA_UNFOLD(is_input, id, data, dim, dims, dtype, layout, \
upper_bound, lower_bound) \
mluop::gen_case::genCaseData(node, is_input, id, data, dim, dims, dtype, \
layout, upper_bound, lower_bound)
// the same with GEN_CASE_DATA_v2
#define GEN_CASE_DATA_UNFOLD_v2(is_input, id, data, dim, dims, dtype, layout, \
upper_bound, lower_bound, distribution) \
mluop::gen_case::genCaseData(node, is_input, id, data, dim, dims, dtype, \
layout, upper_bound, lower_bound, distribution)
#define GEN_CASE_DATA_REAL(is_input, id, data, data_desc) \
mluop::gen_case::genCaseData(node, is_input, id, data, data_desc, 10, -10, \
"UNIFORM", true)
#define GEN_CASE_DATA_REAL_V2(is_input, id, data, data_desc, upper_bound, \
lower_bound) \
mluop::gen_case::genCaseData(node, is_input, id, data, data_desc, \
upper_bound, lower_bound, "UNIFORM", true)
#define GEN_CASE_DATA_REAL_UNFOLD(is_input, id, data, dim, dims, dtype, \
layout) \
mluop::gen_case::genCaseData(node, is_input, id, data, dim, dims, dtype, \
layout, 10, -10, "UNIFORM", true)
// special for RNN
#define GEN_CASE_DATA_RNN(is_input, id, data, data_desc, upper_bound, \
lower_bound, have_onchip) \
mluop::gen_case::genCaseData(node, is_input, id, data, data_desc, \
upper_bound, lower_bound, have_onchip)
#define GEN_CASE_DATA_RNN_v2(is_input, id, data, data_desc, upper_bound, \
lower_bound, have_onchip, distribution) \
mluop::gen_case::genCaseData(node, is_input, id, data, data_desc, \
upper_bound, lower_bound, have_onchip, \
distribution)
#define GEN_CASE_OP_PARAM_SINGLE_HALF(pos, param_node_name, param_name, value) \
mluop::gen_case::genCaseOpParam( \
node, param_name, value, \
std::string(param_node_name) + std::string("_param"), MLUOP_DTYPE_HALF)
#define GEN_CASE_OP_PARAM_SINGLE(pos, param_node_name, param_name, value, ...) \
mluop::gen_case::genCaseOpParam( \
node, param_name, value, \
std::string(param_node_name) + std::string("_param"), ##__VA_ARGS__)
#define GEN_CASE_OP_PARAM_SINGLE_NAME(pos, param_node_name, param_name, value) \
mluop::gen_case::genCaseOpParam(node, param_name, value, param_node_name)
#define GEN_CASE_OP_PARAM_ARRAY(pos, param_node_name, param_name, value, num) \
mluop::gen_case::genCaseOpParam( \
node, param_name, value, num, \
std::string(param_node_name) + std::string("_param"))
#define GEN_CASE_OP_PARAM_SINGLE_SUB(pos, param_node_name, param_name, value, \
new_child) \
mluop::gen_case::genCaseOpParamSub(node, param_name, value, param_node_name, \
new_child)
#define GEN_CASE_OP_PARAM_ARRAY_SUB(pos, param_node_name, param_name, value, \
num, new_child) \
mluop::gen_case::genCaseOpParamSub(node, param_name, value, num, \
param_node_name, new_child)
#define GEN_CASE_HANDLE(handle) mluop::gen_case::genCaseHandle(node, handle)
#define GEN_CASE_HANDLE_PARAM() mluop::gen_case::genCaseHandleParam(node)
#define GEN_CASE_TEST_PARAM(is_diff1, is_diff2, is_diff3, diff1_threshold, \
diff2_threshold, diff3_threshold, ...) \
mluop::gen_case::genCaseTestParam(node, is_diff1, is_diff2, is_diff3, \
diff1_threshold, diff2_threshold, \
diff3_threshold, ##__VA_ARGS__); \
node->serialize(); \
node->reset()
#define GEN_CASE_TEST_PARAM_NEW(is_diff1, is_diff2, is_diff3, diff1_threshold, \
diff2_threshold, diff3_threshold, ...) \
mluop::gen_case::genCaseTestParam(node, is_diff1, is_diff2, is_diff3, \
diff1_threshold, diff2_threshold, \
diff3_threshold, ##__VA_ARGS__); \
node->serialize();
#define GEN_CASE_END() mluop::gen_case::genCaseEnd()
namespace mluop {
namespace gen_case {
bool ifNeedTensorStrideProcess(mluOpTensorDescriptor_t desc);
std::string descToString(mluOpTensorDescriptor_t desc, char delimiter);
// param node is structured like tree
struct ParamNode {
std::vector<std::pair<std::string, std::string>> params;
std::string name = "";
std::vector<ParamNode> childs;
};
struct TensorNode {
bool is_input;
std::string id;
const void *device_ptr;
mluOpTensorDescriptor_t desc;
bool inner_desc = false;
std::vector<double> params;
std::string distribution;
bool dump_data;
TensorNode(bool is_input, std::string id, const void *device_data,
mluOpTensorDescriptor_t desc, bool inner_desc,
std::vector<double> params, std::string distribution,
bool dump_data)
: is_input(is_input),
id(id),
device_ptr(device_data),
desc(desc),
inner_desc(inner_desc),
params(params),
distribution(distribution),
dump_data(dump_data) {}
TensorNode(const TensorNode &t) {
is_input = t.is_input;
id = t.id;
device_ptr = t.device_ptr;
inner_desc = t.inner_desc;
if (inner_desc) {
mluOpTensorDescriptor_t desc_;
mluOpCreateTensorDescriptor(&desc_);
int tensor_dim;
mluOpTensorLayout_t layout;
mluOpDataType_t dtype;
mluOpGetTensorDescriptor_v2(t.desc, &layout, &dtype, &tensor_dim,
nullptr);
int64_t *dims = new int64_t[tensor_dim];
int64_t *strides = new int64_t[tensor_dim];
mluOpGetTensorDescriptorEx_v2(t.desc, &layout, &dtype, &tensor_dim, dims,
strides);
mluOpSetTensorDescriptorEx_v2(desc_, layout, dtype, tensor_dim, dims,
strides);
desc = desc_;
delete[] dims;
delete[] strides;
} else {
desc = t.desc;
}
params = t.params;
distribution = t.distribution;
dump_data = t.dump_data;
}
~TensorNode() {
if (inner_desc) {
if (desc != nullptr) {
mluOpDestroyTensorDescriptor(desc);
}
desc = nullptr;
}
}
};
enum DATASTATE { INPUT, OUTPUT };
class PbNode {
public:
std::string op_name;
std::string op_type;
std::vector<TensorNode> tensors;
std::vector<std::string> criterions;
std::vector<double> thresholds;
std::vector<double> thresholds_imag;
std::string file_name; // pt file name
std::string case_file_name; // pt file name with dir
ParamNode op_param;
ParamNode handle_param;
mluOpHandle_t handle;
PbNode() {}
~PbNode() { reset(); }
void reset() {
op_name = "";
op_type = "";
file_name = "";
case_file_name = "";
for (auto &t : tensors) {
if (t.inner_desc) {
if (t.desc != nullptr) {
mluOpDestroyTensorDescriptor(t.desc);
}
t.desc = nullptr;
}
}
tensors.clear();
criterions.clear();
thresholds.clear();
thresholds_imag.clear();
op_param.name = "";
op_param.params.clear();
// only support one level children
op_param.childs.clear();
handle_param.name = "";
handle_param.params.clear();
}
void setOpNameAndType(std::string op_name, std::string op_type);
void appendTensor(bool is_input, std::string id, const void *device_data,
mluOpTensorDescriptor_t desc, bool inner_desc,
std::vector<double> params, std::string distribution,
bool dump_data);
// should specialization for pointer on device
template <typename paramType>
inline void appendOpParam(std::string param_name, paramType param_value,
std::string param_node_name,
mluOpDataType_t dtype) {
op_param.name = param_node_name;
if (dtype == MLUOP_DTYPE_HALF) {
op_param.params.push_back(
{param_name, get_float_string_of_half_or_bf16(¶m_value, dtype)});
} else if (dtype == MLUOP_DTYPE_BFLOAT16) {
op_param.params.push_back(
{param_name, get_float_string_of_half_or_bf16(¶m_value, dtype)});
} else if (std::is_same<paramType, int8_t>::value ||
std::is_same<paramType, uint8_t>::value) {
op_param.params.push_back(
{param_name, std::to_string(static_cast<int32_t>(param_value))});
} else {
std::stringstream param_ss;
param_ss.setf(std::ios::fixed);
param_ss << std::setprecision(
std::numeric_limits<paramType>::max_digits10)
<< param_value;
op_param.params.push_back({param_name, param_ss.str()});
}
}
// user should control order of children
template <typename paramType>
inline void appendOpParamSub(std::string param_name, paramType param_value,
std::string param_node_name, bool new_child) {
if (new_child) {
op_param.childs.push_back(ParamNode());
}
op_param.childs.back().name = param_node_name;
op_param.childs.back().params.push_back(
{param_name, std::to_string(param_value)});
}
template <typename paramType>
inline void appendOpParam(std::string param_name, paramType *param_value,
int num, std::string param_node_name,
mluOpDataType_t dtype) {
for (int i = 0; i < num; i++) {
appendOpParam(param_name, param_value[i], param_node_name, dtype);
}
}
template <typename paramType>
inline void appendOpParamSub(std::string param_name, paramType *param_value,
int num, std::string param_node_name,
bool new_child) {
for (int i = 0; i < num; i++) {
appendOpParamSub(param_name, param_value[i], param_node_name, new_child);
}
}
// helper function for dtype
inline int dtypeRatio(mluOpDataType_t dtype) {
switch (dtype) {
case MLUOP_DTYPE_INT31:
case MLUOP_DTYPE_COMPLEX_HALF:
case MLUOP_DTYPE_COMPLEX_FLOAT:
return 2;
default:
return 1;
}
}
bool dtypeFloat(mluOpDataType_t dtype) {
switch (dtype) {
case MLUOP_DTYPE_HALF:
case MLUOP_DTYPE_BFLOAT16:
case MLUOP_DTYPE_FLOAT:
case MLUOP_DTYPE_DOUBLE:
case MLUOP_DTYPE_COMPLEX_HALF:
case MLUOP_DTYPE_COMPLEX_FLOAT:
return true;
default:
return false;
}
}
inline std::string get_tensor_random_string(int index) {
std::stringstream random_str;
random_str.setf(std::ios::fixed);
random_str << " random_data: {\n seed: 233\n";
random_str << " distribution: " << tensors[index].distribution << "\n";
if (tensors[index].distribution == "UNIFORM") {
random_str << " upper_bound: " << tensors[index].params[0] << "\n";
random_str << " lower_bound: " << tensors[index].params[1]
<< "\n }\n";
} else {
random_str << " mu: " << tensors[index].params[0] << "\n";
random_str << " sigma: " << tensors[index].params[1] << "\n }\n";
}
return random_str.str();
}
inline std::string get_dtype_value_string(mluOpDataType_t dtype) {
switch (dtype) {
case MLUOP_DTYPE_HALF:
case MLUOP_DTYPE_FLOAT:
case MLUOP_DTYPE_DOUBLE:
case MLUOP_DTYPE_COMPLEX_HALF:
case MLUOP_DTYPE_COMPLEX_FLOAT:
return " value_f: ";
case MLUOP_DTYPE_INT8:
case MLUOP_DTYPE_INT16:
case MLUOP_DTYPE_INT32:
case MLUOP_DTYPE_BOOL:
case MLUOP_DTYPE_INT31:
return " value_i: ";
case MLUOP_DTYPE_INT64:
return " value_l: ";
case MLUOP_DTYPE_UINT8:
case MLUOP_DTYPE_UINT16:
case MLUOP_DTYPE_UINT32:
case MLUOP_DTYPE_BFLOAT16:
return " value_ui: ";
case MLUOP_DTYPE_UINT64:
return " value_ul: ";
default:
return " value_i: ";
}
}
inline std::string get_float_string_of_half_or_bf16(void *data,
mluOpDataType_t dtype) {
char buffer[128];
float dst = 0.0;
if (MLUOP_DTYPE_HALF == dtype) {
cnrtCastDataType_V2(data, cnrtHalf, &dst, cnrtFloat, 1, nullptr,
cnrtRounding_rm);
} else if (MLUOP_DTYPE_BFLOAT16 == dtype) {
cnrtCastDataType_V2(data, cnrtBfloat, &dst, cnrtFloat, 1, nullptr,
cnrtRounding_rm);
}
std::snprintf(buffer, sizeof(buffer), "%.9g", dst);
return std::string(buffer);
}
inline std::string get_data_string(mluOpDataType_t dtype, void *data,
uint64_t offset) {
switch (dtype) {
case MLUOP_DTYPE_HALF:
return get_float_string_of_half_or_bf16(((int16_t *)data) + offset,
dtype);
case MLUOP_DTYPE_BFLOAT16:
return std::to_string(((uint16_t *)data)[offset]);
case MLUOP_DTYPE_FLOAT:
return std::to_string(((float *)data)[offset]);
case MLUOP_DTYPE_DOUBLE:
return std::to_string(((double *)data)[offset]);
case MLUOP_DTYPE_COMPLEX_HALF:
return get_float_string_of_half_or_bf16(((int16_t *)data) + offset,
dtype);
case MLUOP_DTYPE_COMPLEX_FLOAT:
return std::to_string(((float *)data)[offset]);
case MLUOP_DTYPE_INT8:
return std::to_string(((int8_t *)data)[offset]);
case MLUOP_DTYPE_INT16:
return std::to_string(((int16_t *)data)[offset]);
case MLUOP_DTYPE_INT32:
return std::to_string(((int32_t *)data)[offset]);
case MLUOP_DTYPE_BOOL:
return std::to_string(((int8_t *)data)[offset]);
case MLUOP_DTYPE_INT31:
return std::to_string(((int16_t *)data)[offset]);
case MLUOP_DTYPE_INT64:
return std::to_string(((int64_t *)data)[offset]);
case MLUOP_DTYPE_UINT8:
return std::to_string(((uint8_t *)data)[offset]);
case MLUOP_DTYPE_UINT16:
return std::to_string(((uint16_t *)data)[offset]);
case MLUOP_DTYPE_UINT32:
return std::to_string(((uint32_t *)data)[offset]);
case MLUOP_DTYPE_UINT64:
return std::to_string(((uint64_t *)data)[offset]);
default:
return std::to_string(((int8_t *)data)[offset]);
}
}
inline std::string get_data_hex_string(mluOpDataType_t dtype, void *data,
uint64_t offset) {
std::stringstream s;
switch (dtype) {
case MLUOP_DTYPE_HALF:
s << std::hex << ((uint16_t *)data)[offset];
break;
case MLUOP_DTYPE_BFLOAT16:
s << std::hex << ((uint16_t *)data)[offset];
break;
case MLUOP_DTYPE_FLOAT:
s << std::hex << ((uint32_t *)data)[offset];
break;
case MLUOP_DTYPE_DOUBLE:
s << std::hex << ((uint64_t *)data)[offset];
break;
case MLUOP_DTYPE_COMPLEX_HALF:
s << std::hex << ((uint16_t *)data)[offset];
break;
case MLUOP_DTYPE_COMPLEX_FLOAT:
s << std::hex << ((uint32_t *)data)[offset];
break;
default:
s << std::hex << ((uint32_t *)data)[offset];
break;
}
return "\"" + s.str() + "\"";
}
inline uint64_t getTensorSize(int index) {
int dim;
mluOpTensorLayout_t layout;
mluOpDataType_t dtype;
mluOpGetTensorDescriptor_v2(tensors[index].desc, &layout, &dtype, &dim,
nullptr);
mluOpPointerMode_t pointer_mode;
mluOpGetTensorDescriptorPointerMode(tensors[index].desc, &pointer_mode);
int64_t *dims = new int64_t[dim];
int64_t *strides = new int64_t[dim];
mluOpGetTensorDescriptorEx_v2(tensors[index].desc, &layout, &dtype, &dim,
dims, strides);
// if tensor not be set, total_element_num will be 0
uint64_t count = 1;
for (int i = 0; i < dim; i++) {
count *= dims[i];
}
// some magic in here
uint64_t total_num = 1;
if (count != 1) {
if (mluop::gen_case::ifNeedTensorStrideProcess(tensors[index].desc)) {
for (int i = 0; i < dim; i++) {
if (dims[i] == 0) {
total_num = 0;
break;
}
total_num += (dims[i] - 1) * strides[i];
}
} else {
total_num = count;
}
}
delete[] dims;
delete[] strides;
return total_num;
}
inline void *getDeviceData(int index) {
uint64_t total_num = getTensorSize(index);
mluOpDataType_t dtype;
mluOpGetTensorDescriptor(tensors[index].desc, nullptr, &dtype, nullptr,
nullptr);
uint64_t data_size = total_num * mluop::getSizeOfDataType(dtype);
void *data = malloc(data_size);
auto memcpy_dir =
(tensors[index].desc->getPointerMode() == MLUOP_POINTER_MODE_HOST
? cnrtMemcpyHostToHost
: cnrtMemcpyDevToHost);
if (cnrtSuccess == cnrtMemcpy(data,
const_cast<void *>(tensors[index].device_ptr),
data_size, memcpy_dir)) {
return data;
} else {
LOG(ERROR) << "[gen_case] Dump data failed! cnrtMemcpy data size is "
<< data_size << " byte.";
return nullptr;
}
}
void appendCriterion(std::string criterion, double threshold,
double threshold_imag);
std::string getFileName();
std::string getFolderName();
int mkdir();
void setHandle(mluOpHandle_t handle) { this->handle = handle; }
void getHandleParam();
void dumpDataFile(std::string file_name, std::string folder_name, int index,
std::ofstream &case_file, enum DATASTATE data_state);
void dumpOutputFile();
void dumpToFile(bool valueDump = false);
void printOnScreen();
void serialize();
void debugTensorAddress();
};
template <>
inline void PbNode::appendOpParam<std::string>(std::string param_name,
std::string param_value,
std::string param_node_name,
mluOpDataType_t dtype) {
op_param.name = param_node_name;
op_param.params.push_back({param_name, param_value});
}
template <>
inline void PbNode::appendOpParam<const char *>(std::string param_name,
const char *param_value,
std::string param_node_name,
mluOpDataType_t dtype) {
op_param.name = param_node_name;
op_param.params.push_back({param_name, std::string(param_value)});
}
template <>
inline void PbNode::appendOpParam<char *>(std::string param_name,
char *param_value,
std::string param_node_name,
mluOpDataType_t dtype) {
op_param.name = param_node_name;
op_param.params.push_back({param_name, std::string(param_value)});
}
template <>
inline void PbNode::appendOpParam<const void *>(std::string param_name,
const void *param_value,
std::string param_node_name,
mluOpDataType_t dtype) {
op_param.name = param_node_name;
cnrtPointerAttributes_t attr;
cnrtPointerGetAttributes(&attr, param_value);
int data_width = mluop::getSizeOfDataType(dtype);
if (attr.type == cnrtMemTypeDevice) {
void *data = malloc(data_width);
if (cnrtSuccess == cnrtMemcpy(data, const_cast<void *>(param_value),
data_width, cnrtMemcpyDevToHost)) {
op_param.params.push_back({param_name, get_data_string(dtype, data, 0)});
} else {
LOG(ERROR) << "[gen_case] dump op param failed, param_name is "
<< param_name << " param_node_name is " << param_node_name;
}
free(data);
} else {
op_param.params.push_back(
{param_name,
get_data_string(dtype, const_cast<void *>(param_value), 0)});
}
}
template <>
inline void PbNode::appendOpParamSub<std::string>(std::string param_name,
std::string param_value,
std::string param_node_name,
bool new_child) {
if (new_child) {
op_param.childs.push_back(ParamNode());
}
op_param.childs.back().name = param_node_name;
op_param.childs.back().params.push_back({param_name, param_value});
}
template <>
inline void PbNode::appendOpParamSub<char *>(std::string param_name,
char *param_value,
std::string param_node_name,
bool new_child) {
if (new_child) {
op_param.childs.push_back(ParamNode());
}
op_param.childs.back().name = param_node_name;
op_param.childs.back().params.push_back(
{param_name, std::string(param_value)});
}
template <>
inline void PbNode::appendOpParamSub<const char *>(std::string param_name,
const char *param_value,
std::string param_node_name,
bool new_child) {
if (new_child) {
op_param.childs.push_back(ParamNode());
}
op_param.childs.back().name = param_node_name;
op_param.childs.back().params.push_back(
{param_name, std::string(param_value)});
}
bool isGenCaseOn();
// true is used in MLUOP_GEN_CASE_ON, false is for internal use
int genCaseModeGet(bool first);
void genCaseModeRestore();
void genCaseModeSet(int mode);
inline int getOpNameMask(const std::string op_name_, const std::string op_name);
PbNode *genCaseStart(std::string op_name, std::string op_type);
void genCaseData(PbNode *node, bool is_input, std::string id,
const void *device_data, mluOpTensorDescriptor_t desc,
double param1, double param2,
std::string distribution = "UNIFORM", bool dump_data = false);
void genCaseData(PbNode *node, bool is_input, std::string id,
const void *device_data, mluOpSeqDataDescriptor_t desc,
double param1, double param2, bool have_onchop,
std::string distribution = "UNIFORM", bool dump_data = false);
void genCaseData(PbNode *node, bool is_input, std::string id,
const void *device_data, int dim, int64_t *dims,
mluOpDataType_t dtype, mluOpTensorLayout_t layout,
double param1, double param2,
std::string distribution = "UNIFORM", bool dump_data = false);
void genCaseData(PbNode *node, bool is_input, std::string id,
const void *device_data, int dim, const int64_t *dims,
mluOpDataType_t dtype, mluOpTensorLayout_t layout,
double param1, double param2,
std::string distribution = "UNIFORM", bool dump_data = false);
void genCaseData(PbNode *node, bool is_input, std::string id,
const void *device_data, int dim, std::vector<int64_t> dims,
mluOpDataType_t dtype, mluOpTensorLayout_t layout,
double param1, double param2,
std::string distribution = "UNIFORM", bool dump_data = false);
template <typename paramType>
void genCaseOpParam(PbNode *node, std::string param_name, paramType param_value,
std::string param_node_name = "",
mluOpDataType_t dtype = MLUOP_DTYPE_FLOAT) {
node->appendOpParam(param_name, param_value, param_node_name, dtype);
}
template <typename paramType>
void genCaseOpParam(PbNode *node, std::string param_name,
paramType *param_value, int num,
std::string param_node_name = "",
mluOpDataType_t dtype = MLUOP_DTYPE_FLOAT) {
node->appendOpParam(param_name, param_value, num, param_node_name, dtype);
}
template <typename paramType>
void genCaseOpParamSub(PbNode *node, std::string param_name,
paramType param_value, std::string param_node_name = "",
bool new_child = false) {
node->appendOpParamSub(param_name, param_value, param_node_name, new_child);
}
template <typename paramType>
void genCaseOpParamSub(PbNode *node, std::string param_name,
paramType param_value, int num,
std::string param_node_name = "",
bool new_child = false) {
node->appendOpParamSub(param_name, param_value, num, param_node_name,
new_child);
}
void genCaseTestParam(PbNode *node, bool is_diff1, bool is_diff2, bool is_diff3,
const float diff1_threshold, const float diff2_threshold,
const float diff3_threshold,
const float diff1_threshold_imag = -1,
const float diff2_threshold_imag = -1,
const float diff3_threshold_imag = -1);
void genCaseHandle(PbNode *node, mluOpHandle_t handle);
void genCaseHandleParam(PbNode *node);
void genCaseEnd();
} // namespace gen_case
} // namespace mluop
#endif // CORE_GEN_CASE_H_