Skip to content

Commit 59f1269

Browse files
gerbauzdzakhar
authored andcommitted
[user_tests] Implemented user tests for mli_hlp_create_subtensor.
1 parent 8edb47f commit 59f1269

File tree

2 files changed

+308
-0
lines changed

2 files changed

+308
-0
lines changed

user_tests/tests/mli_hlp_tensor_struct/tests_mli_hlp_tensor_struct.cc

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,17 @@ using mli::tst::tensor_quantizer;
3737
using mli::tst::quality_metrics;
3838
using mli::tst::crc32_calc;
3939
using mli::tst::reporter_basic;
40+
using mli::tst::reporter_full;
4041
using mli::tst::memory_manager;
4142

4243
constexpr int kMemSize = 1000;
4344
static IO_DATA_ATTR int8_t scratch_mem_in[kMemSize] = { 0 };
45+
static IO_DATA_ATTR int8_t scratch_mem_out[kMemSize] = { 0 };
4446

4547
bool run_test_count_and_size();
4648
bool run_test_quant_params_getters();
4749
bool run_test_accu_bits_getters();
50+
bool run_test_create_subtensor();
4851

4952
// Main entry point. Running test procedues for various helpers
5053
//=============================================================
@@ -58,6 +61,9 @@ int main() {
5861
if (final_status == kSuccess)
5962
final_status = run_test_accu_bits_getters();
6063

64+
if (final_status == kSuccess)
65+
final_status = run_test_create_subtensor();
66+
6167
return (final_status == kSuccess) ? 0 : 1;
6268
}
6369

@@ -338,3 +344,117 @@ bool run_test_accu_bits_getters() {
338344
return test_status;
339345
}
340346

347+
// Tests procedure for mli_hlp_create_subtensor function.
348+
//=======================================================================
349+
struct create_subtensor_test_operands {
350+
const char* descr;
351+
tensor_quantizer in;
352+
tensor_quantizer out;
353+
mli_sub_tensor_cfg cfg;
354+
const quality_metrics threshold;
355+
const crc32_calc check_sum;
356+
};
357+
358+
// Checksums of test tensors for various mli calculations mode.
359+
// When developer finished implementation of kernel and consider it as ok, one needs to populate
360+
// proper checksums for tests in order to highlight any change which affects results.
361+
362+
const crc32_calc test_1_chksum_fx16 { 0x418F5ED6 }, test_1_chksum_fx8 { 0x0820E5D9 },
363+
test_1_chksum_sa8 { 0xBB54537D }, test_1_chksum_sa8_pa { 0x63BAA2A1 },
364+
test_1_chksum_sa32 { 0xDC93E12C }, test_1_chksum_sa32_pa { 0x98D5A2D6 },
365+
test_2_chksum_fx16 { 0xD7B05DED }, test_2_chksum_fx8 { 0x7582D890 },
366+
test_2_chksum_sa8 { 0x4CB81C56 }, test_2_chksum_sa8_pa { 0x2F0B06B8 },
367+
test_2_chksum_sa32 { 0x0B379B11 }, test_2_chksum_sa32_pa { 0x87591FC2 };
368+
369+
const quality_metrics thresholds_test_1_general{ quality_metrics::kPassValueMaxAbsErr, quality_metrics::kPassValueSnr,
370+
/* SNR_DB = */35.9f, quality_metrics::kPassValueQuantErrPerc };
371+
372+
const quality_metrics thresholds_test_2_general{ quality_metrics::kPassValueMaxAbsErr, quality_metrics::kPassValueSnr,
373+
/* SNR_DB = */41.3f, quality_metrics::kPassValueQuantErrPerc };
374+
375+
bool run_test_create_subtensor() {
376+
bool test_status = true;
377+
const reporter_full reporter;
378+
static const create_subtensor_test_operands create_subtensor_tests_list[] = {
379+
{"FX16 tensor ", input_1_fx16, test_1_out_fx16, test_1_cfg, thresholds_test_1_general, test_1_chksum_fx16},
380+
{"FX8 tensor ", input_1_fx8, test_1_out_fx8, test_1_cfg, thresholds_test_1_general, test_1_chksum_fx8},
381+
{"SA8 tensor ", input_1_sa8, test_1_out_sa8, test_1_cfg, thresholds_test_1_general, test_1_chksum_sa8},
382+
{"SA8 tensor per axis", input_1_sa8_per_axis, test_1_out_sa8_per_axis, test_1_cfg, thresholds_test_1_general, test_1_chksum_sa8_pa},
383+
{"SA32 tensor ", input_1_sa32, test_1_out_sa32, test_1_cfg, thresholds_test_1_general, test_1_chksum_sa32},
384+
{"SA32 tensor per axis", input_1_sa32_per_axis, test_1_out_sa32_per_axis, test_1_cfg, thresholds_test_1_general, test_1_chksum_sa32_pa},
385+
{"FX16 tensor (rank & offset) ", input_1_fx16, test_2_out_fx16, test_2_cfg, thresholds_test_2_general, test_2_chksum_fx16},
386+
{"FX8 tensor (rank & offset)", input_1_fx8, test_2_out_fx8, test_2_cfg, thresholds_test_2_general, test_2_chksum_fx8},
387+
{"SA8 tensor (rank & offset)", input_1_sa8, test_2_out_sa8, test_2_cfg, thresholds_test_2_general, test_2_chksum_sa8},
388+
{"SA8 per axis (rank & offset)", input_1_sa8_per_axis, test_2_out_sa8_per_axis, test_2_cfg, thresholds_test_2_general, test_2_chksum_sa8_pa},
389+
{"SA32 tensor (rank & offset)", input_1_sa32, test_2_out_sa32, test_2_cfg, thresholds_test_2_general, test_2_chksum_sa32},
390+
{"SA32 per axis (rank & offset)", input_1_sa32_per_axis, test_2_out_sa32_per_axis, test_2_cfg, thresholds_test_2_general, test_2_chksum_sa32_pa}
391+
};
392+
constexpr int kTestsNum = sizeof(create_subtensor_tests_list) / sizeof(create_subtensor_tests_list[0]);
393+
394+
reporter.report_header("MLI|Helpers|Create Subtensor Tests");
395+
for (int i = 0; i < kTestsNum; ++i) {
396+
memory_manager mem_in_keeper((int8_t*)(scratch_mem_in), sizeof(scratch_mem_in));
397+
memory_manager mem_out_keeper((int8_t*)(scratch_mem_out), sizeof(scratch_mem_out));
398+
bool is_test_passed = true;
399+
const create_subtensor_test_operands* cur_test = &create_subtensor_tests_list[i];
400+
quality_metrics test_metrics;
401+
402+
if (!(cur_test->in.is_valid())) {
403+
is_test_passed = false;
404+
reporter.report_message(cur_test->descr, "At init: Bad source data for input tensor");
405+
}
406+
407+
const mli_tensor input = cur_test->in.get_quantized_tensor(mem_in_keeper.allocate_memory(cur_test->in));
408+
mli_tensor out = cur_test->out.get_not_quantized_tensor(mem_out_keeper.allocate_memory(cur_test->out));
409+
if (is_test_passed &&
410+
(tensor_quantizer::validate_tensor(input) != tensor_quantizer::kOk ||
411+
tensor_quantizer::validate_tensor(out) != tensor_quantizer::kOk)) {
412+
is_test_passed = false;
413+
reporter.report_message(cur_test->descr,
414+
"At quantization step: more memory for in or out tensor is be required");
415+
}
416+
417+
if (is_test_passed &&
418+
(mem_in_keeper.is_memory_corrupted() || mem_out_keeper.is_memory_corrupted())) {
419+
is_test_passed = false;
420+
reporter.report_message(cur_test->descr,
421+
"At quantization step: memory beside one of operands is corrupted");
422+
}
423+
424+
// Run specific kernel for test
425+
426+
crc32_calc data_crc_before, data_crc_after;
427+
if (is_test_passed &&
428+
mli_hlp_create_subtensor(&input, &cur_test->cfg, &out) != MLI_STATUS_OK) {
429+
data_crc_before(input);
430+
reporter.report_message(cur_test->descr, "FAILED at kernel run: kernel returned bad status");
431+
is_test_passed = false;
432+
data_crc_after(input);
433+
}
434+
435+
if (is_test_passed &&
436+
(data_crc_before.get() != data_crc_after.get() ||
437+
mem_in_keeper.is_memory_corrupted())) {
438+
is_test_passed = false;
439+
reporter.report_message(cur_test->descr,
440+
"At function run: memory is corrupted after functions invokation");
441+
}
442+
443+
if (is_test_passed &&
444+
test_metrics.calculate_metrics(out, cur_test->out) == false) {
445+
reporter.report_message(cur_test->descr, "FAILED at comparison output with reference");
446+
is_test_passed = false;
447+
}
448+
449+
if (is_test_passed) {
450+
crc32_calc data_crc;
451+
data_crc(input);
452+
data_crc(out);
453+
is_test_passed &= reporter.evaluate_and_report_case(cur_test->descr, test_metrics, cur_test->threshold,
454+
data_crc, cur_test->check_sum);
455+
}
456+
test_status &= is_test_passed;
457+
}
458+
reporter.report_outline("[AUTO] Group: mli_hlp_create_subtensor", test_status);
459+
return test_status;
460+
}

user_tests/tests/mli_hlp_tensor_struct/vectors_mli_hlp_tensor_struct.inc

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ extern mli::tst::tensor_quantizer input_2_sa8;
3030
extern mli::tst::tensor_quantizer input_2_sa32;
3131
extern mli::tst::tensor_quantizer input_2_fp32;
3232

33+
// Extracted Output vectors Declaration
34+
//===================================================
35+
extern mli::tst::tensor_quantizer test_1_out_fx16;
36+
extern mli::tst::tensor_quantizer test_1_out_fx8;
37+
extern mli::tst::tensor_quantizer test_1_out_sa8;
38+
extern mli::tst::tensor_quantizer test_1_out_sa8_per_axis;
39+
extern mli::tst::tensor_quantizer test_1_out_sa32;
40+
extern mli::tst::tensor_quantizer test_1_out_sa32_per_axis;
41+
extern mli::tst::tensor_quantizer test_1_out_fp32;
42+
43+
// Tests configuration structures Declaration
44+
//========================================
45+
extern const mli_sub_tensor_cfg test_1_cfg;
46+
extern const mli_sub_tensor_cfg test_2_cfg;
3347

3448
// Generated input vectors
3549
//========================================
@@ -199,3 +213,177 @@ tensor_quantizer input_2_sa32(input_2_tsr_sa32, input_2_sa_dim, input_2_data,
199213
&input_2_zero_point, 1, input_2_scale_frac, 1);
200214

201215
tensor_quantizer input_2_fp32(input_2_tsr_fp32, (float*)input_2_data, sizeof(input_2_data) / sizeof(input_2_data[0]));
216+
217+
// Extracted Output vectors
218+
//===================================================
219+
220+
static const float test_1_out_data[] = {
221+
0.061785f, -0.247152f, 0.193333f, -0.010555f, -0.124796f, 0.038729f, 0.018775f, 0.000024f
222+
};
223+
224+
static const float test_1_out_scales[] = {0.0017931332f, 0.0023502372f};
225+
static const float test_1_out_zero_points[] = {0.0f, 0.0f};
226+
static const int8_t test_1_out_scales_frac[] = {22, 23};
227+
static const int test_1_out_sa_dim_per_axis = 3;
228+
229+
#define TEST_1_OUT_TSR_SHARED_DESCR \
230+
/* .data = */ { 0 },\
231+
/* .mem_stride = */ {(3 * (3 * 2)) * 2, 3 * 2, 3, 1}, \
232+
/* .shape = */ {2, 2, 1, 2}, \
233+
/* .rank = */ 4
234+
235+
static const mli_tensor test_1_out_tsr_fx16 = {
236+
TEST_1_OUT_TSR_SHARED_DESCR,
237+
238+
/* .el_type = */ MLI_EL_FX_16,
239+
/* .el_params = */ { 0 }
240+
};
241+
242+
static const mli_tensor test_1_out_tsr_fx8 = {
243+
TEST_1_OUT_TSR_SHARED_DESCR,
244+
245+
/* .el_type = */ MLI_EL_FX_8,
246+
/* .el_params = */ { 0 }
247+
};
248+
249+
static const mli_tensor test_1_out_tsr_sa8 = {
250+
TEST_1_OUT_TSR_SHARED_DESCR,
251+
252+
/* .el_type = */ MLI_EL_SA_8,
253+
/* .el_params = */ { 0 }
254+
};
255+
256+
static const mli_tensor test_1_out_tsr_sa32 = {
257+
TEST_1_OUT_TSR_SHARED_DESCR,
258+
259+
/* .el_type = */ MLI_EL_SA_32,
260+
/* .el_params = */ { 0 }
261+
};
262+
263+
static const mli_tensor test_1_out_tsr_fp32 = {
264+
TEST_1_OUT_TSR_SHARED_DESCR,
265+
266+
/* .el_type = */ MLI_EL_FP_32,
267+
/* .el_params = */ { 0 }
268+
};
269+
270+
tensor_quantizer test_1_out_fx16(test_1_out_tsr_fx16, input_1_fx16_frac, test_1_out_data,
271+
sizeof(test_1_out_data) / sizeof(test_1_out_data[0]));
272+
273+
tensor_quantizer test_1_out_fx8(test_1_out_tsr_fx8, input_1_fx8_frac, test_1_out_data,
274+
sizeof(test_1_out_data) / sizeof(test_1_out_data[0]));
275+
276+
tensor_quantizer test_1_out_sa8(test_1_out_tsr_sa8, input_1_sa_dim, test_1_out_data,
277+
sizeof(test_1_out_data) / sizeof(test_1_out_data[0]), &input_1_scale, 1,
278+
&input_1_zero_point, 1, input_1_scale_frac, 1);
279+
280+
tensor_quantizer test_1_out_sa8_per_axis(test_1_out_tsr_sa8, test_1_out_sa_dim_per_axis, test_1_out_data,
281+
sizeof(test_1_out_data) / sizeof(test_1_out_data[0]), test_1_out_scales,
282+
sizeof(test_1_out_scales) / sizeof(test_1_out_scales[0]), test_1_out_zero_points,
283+
sizeof(test_1_out_zero_points) / sizeof(test_1_out_zero_points[0]), test_1_out_scales_frac,
284+
sizeof(test_1_out_scales_frac) / sizeof(test_1_out_scales_frac[0]));
285+
286+
tensor_quantizer test_1_out_sa32(test_1_out_tsr_sa32, input_1_sa_dim, test_1_out_data,
287+
sizeof(test_1_out_data) / sizeof(test_1_out_data[0]), &input_1_scale, 1,
288+
&input_1_zero_point, 1, input_1_scale_frac, 1);
289+
290+
tensor_quantizer test_1_out_sa32_per_axis(test_1_out_tsr_sa32, test_1_out_sa_dim_per_axis, test_1_out_data,
291+
sizeof(test_1_out_data) / sizeof(test_1_out_data[0]), test_1_out_scales,
292+
sizeof(test_1_out_scales) / sizeof(test_1_out_scales[0]), test_1_out_zero_points,
293+
sizeof(test_1_out_zero_points) / sizeof(test_1_out_zero_points[0]), test_1_out_scales_frac,
294+
sizeof(test_1_out_scales_frac) / sizeof(test_1_out_scales_frac[0]));
295+
296+
297+
298+
static const float test_2_out_data[] = {
299+
-0.298480f, 0.330821f, -0.190452f, 0.063674f, 0.239669f, 0.527339f
300+
};
301+
302+
static const float test_2_out_scales[] = {0.0023502372f, 0.004152277f};
303+
static const float test_2_out_zero_points[] = {0.0f, 0.0f};
304+
static const int8_t test_2_out_scales_frac[] = {23, 21};
305+
static const int test_2_out_sa_dim_per_axis = 2;
306+
307+
#define TEST_2_OUT_TSR_SHARED_DESCR \
308+
/* .data = */ { 0 },\
309+
/* .mem_stride = */ { 3 * 2, 3, 1 }, \
310+
/* .shape = */ {3, 1, 2}, \
311+
/* .rank = */ 3
312+
313+
static const mli_tensor test_2_out_tsr_fx16 = {
314+
TEST_2_OUT_TSR_SHARED_DESCR,
315+
316+
/* .el_type = */ MLI_EL_FX_16,
317+
/* .el_params = */ { 0 }
318+
};
319+
320+
static const mli_tensor test_2_out_tsr_fx8 = {
321+
TEST_2_OUT_TSR_SHARED_DESCR,
322+
323+
/* .el_type = */ MLI_EL_FX_8,
324+
/* .el_params = */ { 0 }
325+
};
326+
327+
static const mli_tensor test_2_out_tsr_sa8 = {
328+
TEST_2_OUT_TSR_SHARED_DESCR,
329+
330+
/* .el_type = */ MLI_EL_SA_8,
331+
/* .el_params = */ { 0 }
332+
};
333+
334+
static const mli_tensor test_2_out_tsr_sa32 = {
335+
TEST_2_OUT_TSR_SHARED_DESCR,
336+
337+
/* .el_type = */ MLI_EL_SA_32,
338+
/* .el_params = */ { 0 }
339+
};
340+
341+
static const mli_tensor test_2_out_tsr_fp32 = {
342+
TEST_2_OUT_TSR_SHARED_DESCR,
343+
344+
/* .el_type = */ MLI_EL_FP_32,
345+
/* .el_params = */ { 0 }
346+
};
347+
348+
tensor_quantizer test_2_out_fx16(test_2_out_tsr_fx16, input_1_fx16_frac, test_2_out_data,
349+
sizeof(test_2_out_data) / sizeof(test_2_out_data[0]));
350+
351+
tensor_quantizer test_2_out_fx8(test_2_out_tsr_fx8, input_1_fx8_frac, test_2_out_data,
352+
sizeof(test_2_out_data) / sizeof(test_2_out_data[0]));
353+
354+
tensor_quantizer test_2_out_sa8(test_2_out_tsr_sa8, input_1_sa_dim, test_2_out_data,
355+
sizeof(test_2_out_data) / sizeof(test_2_out_data[0]), &input_1_scale, 1,
356+
&input_1_zero_point, 1, input_1_scale_frac, 1);
357+
358+
tensor_quantizer test_2_out_sa8_per_axis(test_2_out_tsr_sa8, test_2_out_sa_dim_per_axis, test_2_out_data,
359+
sizeof(test_2_out_data) / sizeof(test_2_out_data[0]), test_2_out_scales,
360+
sizeof(test_2_out_scales) / sizeof(test_2_out_scales[0]), test_2_out_zero_points,
361+
sizeof(test_2_out_zero_points) / sizeof(test_2_out_zero_points[0]), test_2_out_scales_frac,
362+
sizeof(test_2_out_scales_frac) / sizeof(test_2_out_scales_frac[0]));
363+
364+
tensor_quantizer test_2_out_sa32(test_2_out_tsr_sa32, input_1_sa_dim, test_2_out_data,
365+
sizeof(test_2_out_data) / sizeof(test_2_out_data[0]), &input_1_scale, 1,
366+
&input_1_zero_point, 1, input_1_scale_frac, 1);
367+
368+
tensor_quantizer test_2_out_sa32_per_axis(test_2_out_tsr_sa32, test_2_out_sa_dim_per_axis, test_2_out_data,
369+
sizeof(test_2_out_data) / sizeof(test_2_out_data[0]), test_2_out_scales,
370+
sizeof(test_2_out_scales) / sizeof(test_2_out_scales[0]), test_1_out_zero_points,
371+
sizeof(test_1_out_zero_points) / sizeof(test_1_out_zero_points[0]), test_1_out_scales_frac,
372+
sizeof(test_1_out_scales_frac) / sizeof(test_1_out_scales_frac[0]));
373+
374+
375+
376+
// Tests configuration structures
377+
//========================================
378+
379+
const mli_sub_tensor_cfg test_1_cfg = {
380+
/* offset = */ { 0 },
381+
/* size = */ {2, 2, 1, 2},
382+
/* sub_tensor_rank = */ 4
383+
};
384+
385+
const mli_sub_tensor_cfg test_2_cfg = {
386+
/* offset = */ { 1, 2, 0, 1 },
387+
/* size = */ { 3, 1, 1, 2 },
388+
/* sub_tensor_rank = */ 3
389+
};

0 commit comments

Comments
 (0)