tests: lib: cmsis_dsp: bayes: Add F16 tests for 1.9.0
This commit adds the bayes F16 test patterns and implementations for the CMSIS-DSP 1.9.0. Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
This commit is contained in:
parent
198c0845b9
commit
112136e6ba
4 changed files with 164 additions and 3 deletions
|
@ -4,5 +4,9 @@ cmake_minimum_required(VERSION 3.20.0)
|
|||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||
project(cmsis_dsp_bayes)
|
||||
|
||||
FILE(GLOB app_sources src/*.c)
|
||||
target_sources(app PRIVATE ${app_sources})
|
||||
target_sources(app PRIVATE
|
||||
src/f32.c
|
||||
src/main.c
|
||||
)
|
||||
|
||||
target_sources_ifdef(CONFIG_CMSIS_DSP_FLOAT16 app PRIVATE src/f16.c)
|
||||
|
|
91
tests/lib/cmsis_dsp/bayes/src/f16.c
Normal file
91
tests/lib/cmsis_dsp/bayes/src/f16.c
Normal file
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Stephanos Ioannidis <root@stephanos.io>
|
||||
* Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <ztest.h>
|
||||
#include <zephyr.h>
|
||||
#include <stdlib.h>
|
||||
#include <arm_math_f16.h>
|
||||
#include "../../common/test_common.h"
|
||||
|
||||
#include "f16.pat"
|
||||
|
||||
#define REL_ERROR_THRESH (3.0e-3)
|
||||
|
||||
void test_gaussian_naive_bayes_predict_f16(void)
|
||||
{
|
||||
arm_gaussian_naive_bayes_instance_f16 inst;
|
||||
|
||||
size_t index;
|
||||
const uint16_t pattern_count = in_dims[0];
|
||||
const uint16_t class_count = in_dims[1];
|
||||
const uint16_t vec_dims = in_dims[2];
|
||||
|
||||
const float16_t *params = (const float16_t *)in_param;
|
||||
const float16_t *input = (const float16_t *)in_val;
|
||||
float16_t *output_probs_buf, *output_probs;
|
||||
uint16_t *output_preds_buf, *output_preds;
|
||||
float16_t *temp;
|
||||
|
||||
/* Initialise instance */
|
||||
inst.vectorDimension = vec_dims;
|
||||
inst.numberOfClasses = class_count;
|
||||
inst.theta = params;
|
||||
inst.sigma = params + (class_count * vec_dims);
|
||||
inst.classPriors = params + (2 * class_count * vec_dims);
|
||||
inst.epsilon = params[class_count + (2 * class_count * vec_dims)];
|
||||
|
||||
/* Allocate output buffers */
|
||||
output_probs_buf =
|
||||
malloc(pattern_count * class_count * sizeof(float16_t));
|
||||
zassert_not_null(output_probs_buf, ASSERT_MSG_BUFFER_ALLOC_FAILED);
|
||||
|
||||
output_preds_buf =
|
||||
malloc(pattern_count * sizeof(uint16_t));
|
||||
zassert_not_null(output_preds_buf, ASSERT_MSG_BUFFER_ALLOC_FAILED);
|
||||
|
||||
output_probs = output_probs_buf;
|
||||
output_preds = output_preds_buf;
|
||||
|
||||
temp = malloc(pattern_count * class_count * sizeof(float16_t));
|
||||
zassert_not_null(temp, ASSERT_MSG_BUFFER_ALLOC_FAILED);
|
||||
|
||||
/* Enumerate patterns */
|
||||
for (index = 0; index < pattern_count; index++) {
|
||||
/* Run test function */
|
||||
*output_preds =
|
||||
arm_gaussian_naive_bayes_predict_f16(
|
||||
&inst, input, output_probs, temp);
|
||||
|
||||
/* Increment pointers */
|
||||
input += vec_dims;
|
||||
output_probs += class_count;
|
||||
output_preds++;
|
||||
}
|
||||
|
||||
/* Validate output */
|
||||
zassert_true(
|
||||
test_rel_error_f16(pattern_count, output_probs_buf,
|
||||
(float16_t *)ref_prob, REL_ERROR_THRESH),
|
||||
ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
|
||||
|
||||
zassert_true(
|
||||
test_equal_q15(pattern_count, output_preds_buf, ref_pred),
|
||||
ASSERT_MSG_INCORRECT_COMP_RESULT);
|
||||
|
||||
/* Free output buffers */
|
||||
free(output_probs_buf);
|
||||
free(output_preds_buf);
|
||||
}
|
||||
|
||||
void test_bayes_f16(void)
|
||||
{
|
||||
ztest_test_suite(bayes_f16,
|
||||
ztest_unit_test(test_gaussian_naive_bayes_predict_f16)
|
||||
);
|
||||
|
||||
ztest_run_test_suite(bayes_f16);
|
||||
}
|
62
tests/lib/cmsis_dsp/bayes/src/f16.pat
generated
Normal file
62
tests/lib/cmsis_dsp/bayes/src/f16.pat
generated
Normal file
|
@ -0,0 +1,62 @@
|
|||
static const uint16_t in_val[140] = {
|
||||
0x3c1e, 0xa961, 0xa49b, 0x2232, 0xa444, 0x28c6, 0x2ada, 0x280d,
|
||||
0xa471, 0x267e, 0x948c, 0x288f, 0x2d63, 0x15d6, 0x3be7, 0x9e3d,
|
||||
0xa169, 0x2fa6, 0x2d48, 0xa6f3, 0x2a0d, 0xa85d, 0x283a, 0x1f02,
|
||||
0xa405, 0xa6f4, 0x2b7b, 0x1d11, 0x2845, 0x3be6, 0xa917, 0xa295,
|
||||
0x1e37, 0x26cd, 0xa4d8, 0x2f8a, 0x1320, 0x9c85, 0x2e63, 0x2118,
|
||||
0xa975, 0xaab2, 0x3c4e, 0xa7ba, 0x28b1, 0xa6ad, 0x2d1a, 0xa741,
|
||||
0x1fc4, 0x1e39, 0x28a3, 0x2dd2, 0xaa89, 0x2c79, 0x2d1d, 0x26bd,
|
||||
0x3bf9, 0xa8ec, 0xa94b, 0xa851, 0x2461, 0x2771, 0xad41, 0xa99a,
|
||||
0x9edf, 0x2c3b, 0x29af, 0x2d62, 0x2d12, 0xa232, 0x3c23, 0xa278,
|
||||
0xa920, 0x2944, 0xe9a, 0xb062, 0xac9d, 0xa80f, 0xa811, 0x271c,
|
||||
0x2d61, 0x1da6, 0x28cc, 0xa203, 0x28c7, 0xa45d, 0xa96b, 0x3c19,
|
||||
0x21a0, 0x2483, 0xaa2c, 0x249a, 0x979b, 0x9b43, 0x8c99, 0x253b,
|
||||
0xa8c8, 0xab4e, 0xa5b5, 0x298c, 0xa691, 0x3bd3, 0x9a78, 0xa4cc,
|
||||
0xa6b9, 0x1efe, 0x2a66, 0x2c36, 0xa45c, 0x2a72, 0x20f4, 0x2615,
|
||||
0x223a, 0x277e, 0xa80b, 0xa5e2, 0x3c2b, 0x9ce8, 0xa1cc, 0x10c1,
|
||||
0x2770, 0xa538, 0xac90, 0x215f, 0xa8fb, 0x2c1a, 0x3c0d, 0xa002,
|
||||
0x25b7, 0xa56b, 0xa901, 0xa7d3, 0x241e, 0xa97a, 0x2865, 0x9ae7,
|
||||
0x2b14, 0x2d47, 0xa0a8, 0xa407
|
||||
};
|
||||
|
||||
static const uint16_t in_dims[3] = {
|
||||
0x000A, 0x0005, 0x000E
|
||||
};
|
||||
|
||||
static const uint16_t in_param[146] = {
|
||||
0x3be1, 0xaac6, 0x25a0, 0xa745, 0x26b7, 0xa0fb, 0x2507, 0xadc6,
|
||||
0xa496, 0xa097, 0xa35f, 0xabab, 0xa854, 0xa852, 0x2426, 0x3b7c,
|
||||
0x1bc0, 0x2b2c, 0x1e38, 0xa502, 0xac0a, 0x24e3, 0xa548, 0x9ba1,
|
||||
0x24c3, 0xa33b, 0x21d5, 0x2665, 0xa054, 0x23cd, 0x3c10, 0xaeb6,
|
||||
0xa8f3, 0xa5f8, 0xa482, 0x1464, 0xa861, 0x2d3a, 0xa55e, 0x9e0f,
|
||||
0x22af, 0xa3b0, 0x2aa8, 0x26b0, 0x2c9a, 0x3bbc, 0xa67f, 0xa135,
|
||||
0xad50, 0x2a61, 0xa998, 0x2464, 0xa4f9, 0xa991, 0x20a0, 0x9fc3,
|
||||
0xa217, 0xa95b, 0xa797, 0x9ea6, 0x3bed, 0xa880, 0x192e, 0xa2e2,
|
||||
0xa8b1, 0x2893, 0x2465, 0x1d0f, 0xac94, 0x98ac, 0x218c, 0x1b1c,
|
||||
0x2094, 0x2057, 0x1f9a, 0x1e5b, 0x18ae, 0x1eec, 0x16e6, 0x1cef,
|
||||
0x19c4, 0x1e47, 0x2462, 0x1814, 0x1c9f, 0x2006, 0x2018, 0x1d6a,
|
||||
0x1bba, 0x1cda, 0x1cc0, 0x20b6, 0x1d1b, 0x2007, 0x20b4, 0x1e9a,
|
||||
0x1d28, 0x20fb, 0x18ee, 0x1d33, 0x1c04, 0x1dd4, 0x24c8, 0x20dc,
|
||||
0x181e, 0x1ebc, 0x231c, 0x1df2, 0x2064, 0x1dd6, 0x110d, 0x1c3d,
|
||||
0x2528, 0x1a3c, 0x2185, 0x1d87, 0x1d33, 0x1d3a, 0x1f79, 0x2339,
|
||||
0x17d2, 0x1c26, 0x2231, 0x1bba, 0x1c11, 0x1ddf, 0x1886, 0x1c6c,
|
||||
0x1bab, 0x13fa, 0x1df6, 0x1c1c, 0x1c05, 0x1cd4, 0x1afe, 0x2046,
|
||||
0x1d82, 0x1613, 0x2052, 0x1d59, 0x3155, 0x34cd, 0x3155, 0x3155,
|
||||
0x3266, 0x0
|
||||
};
|
||||
|
||||
static const uint16_t ref_prob[50] = {
|
||||
0x4ca9, 0xd8df, 0xdd65, 0xd5b5, 0xdcdc, 0x4c99, 0xd877, 0xdd1b,
|
||||
0xd481, 0xdc6e, 0xd9b1, 0x4cad, 0xdada, 0xdaf5, 0xd9cc, 0x4bff,
|
||||
0xd935, 0xdd79, 0xd675, 0xdd00, 0x4bfc, 0xd8a3, 0xdd43, 0xd633,
|
||||
0xdc93, 0x4bb0, 0xd8c6, 0xdd7a, 0xd55c, 0xdcdf, 0xd57c, 0xd7e0,
|
||||
0xdb63, 0x4c84, 0xe0bb, 0xd5ae, 0xd6f2, 0xdadf, 0x4c16, 0xe063,
|
||||
0xd60f, 0xd978, 0xd8cd, 0xd9cb, 0x4d4c, 0x4c7d, 0xd8a1, 0xdd0e,
|
||||
0xd618, 0xdcd0
|
||||
};
|
||||
|
||||
static const uint16_t ref_pred[10] = {
|
||||
0x0000, 0x0000, 0x0001, 0x0000, 0x0000, 0x0000, 0x0003, 0x0003,
|
||||
0x0004, 0x0000
|
||||
};
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2020 Stephanos Ioannidis <root@stephanos.io>
|
||||
* Copyright (c) 2021 Stephanos Ioannidis <root@stephanos.io>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
@ -7,9 +7,13 @@
|
|||
#include <ztest.h>
|
||||
#include <zephyr.h>
|
||||
|
||||
extern void test_bayes_f16(void);
|
||||
extern void test_bayes_f32(void);
|
||||
|
||||
void test_main(void)
|
||||
{
|
||||
#ifdef CONFIG_CMSIS_DSP_FLOAT16
|
||||
test_bayes_f16();
|
||||
#endif
|
||||
test_bayes_f32();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue