mirror of
https://github.com/jomjol/AI-on-the-edge-device.git
synced 2025-12-08 20:46:52 +03:00
Rolling 20220526
This commit is contained in:
15
code/components/esp-nn/tests/CMakeLists.txt
Normal file
15
code/components/esp-nn/tests/CMakeLists.txt
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
set(COMPONENT_ADD_INCLUDEDIRS ./include/)
|
||||
set(COMPONENT_SRCS "src/basic_math_test.c"
|
||||
"src/convolution_test.c"
|
||||
"src/fully_connected_test.c"
|
||||
"src/pooling_test.c"
|
||||
"src/relu_test.c"
|
||||
"src/softmax_test.c")
|
||||
|
||||
set(COMPONENT_REQUIRES )
|
||||
set(COMPONENT_PRIV_REQUIRES esp-nn)
|
||||
|
||||
register_component()
|
||||
|
||||
target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-unused-function)
|
||||
4
code/components/esp-nn/tests/README.md
Normal file
4
code/components/esp-nn/tests/README.md
Normal file
@@ -0,0 +1,4 @@
|
||||
# Tests for esp_nn library
|
||||
|
||||
- Include these in your test framework and run the framework.
|
||||
- For IDF test please refer `test_app`
|
||||
5
code/components/esp-nn/tests/component.mk
Normal file
5
code/components/esp-nn/tests/component.mk
Normal file
@@ -0,0 +1,5 @@
|
||||
#FIXME
|
||||
|
||||
COMPONENT_ADD_INCLUDEDIRS := include/
|
||||
|
||||
COMPONENT_SRCDIRS := src/
|
||||
48
code/components/esp-nn/tests/include/test_functions.h
Normal file
48
code/components/esp-nn/tests/include/test_functions.h
Normal file
@@ -0,0 +1,48 @@
|
||||
// Copyright 2020-2021 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// 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.
|
||||
|
||||
|
||||
/* int8_t ops tests */
|
||||
void esp_nn_add_elementwise_s8_test();
|
||||
void esp_nn_mul_elementwise_s8_test();
|
||||
|
||||
void esp_nn_depthwise_conv_s8_test();
|
||||
void esp_nn_conv_s8_test();
|
||||
|
||||
void esp_nn_avg_pool_s8_test();
|
||||
void esp_nn_max_pool_s8_test();
|
||||
|
||||
void esp_nn_fully_connected_s8_test();
|
||||
|
||||
void esp_nn_relu6_s8_test();
|
||||
|
||||
void esp_nn_softmax_s8_test();
|
||||
|
||||
/* uint8_t ops tests */
|
||||
void esp_nn_add_elementwise_u8_test();
|
||||
|
||||
void esp_nn_depthwise_conv_u8_test();
|
||||
void esp_nn_conv_u8_test();
|
||||
|
||||
void esp_nn_avg_pool_u8_test();
|
||||
void esp_nn_max_pool_u8_test();
|
||||
|
||||
void esp_nn_fully_connected_u8_test();
|
||||
|
||||
/* instructions test functions */
|
||||
void compare_instructions_test();
|
||||
void arith_instructions_test();
|
||||
void min_max_instructions_test();
|
||||
void bitwise_instructions_test();
|
||||
void load_store_instructions_test();
|
||||
87
code/components/esp-nn/tests/include/test_utils.h
Normal file
87
code/components/esp-nn/tests/include/test_utils.h
Normal file
@@ -0,0 +1,87 @@
|
||||
// Copyright 2020-2021 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// 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.
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <common_functions.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/* mult value range */
|
||||
#define MULT_MAX INT32_MAX
|
||||
#define MULT_MIN 0
|
||||
|
||||
/* shift value range */
|
||||
#define SHIFT_MIN -31
|
||||
#define SHIFT_MAX 30
|
||||
|
||||
/**
|
||||
* @brief callback function to run before C function
|
||||
*/
|
||||
void profile_c_start();
|
||||
|
||||
/**
|
||||
* @brief callback function to run after C function
|
||||
*/
|
||||
void profile_c_end();
|
||||
|
||||
/**
|
||||
* @brief callback function to run before optimized function
|
||||
*/
|
||||
void profile_opt_start();
|
||||
|
||||
/**
|
||||
* @brief callback function to run after optimized function
|
||||
*/
|
||||
void profile_opt_end();
|
||||
|
||||
#define ANSI_COLOR_RED "\x1b[31m"
|
||||
#define ANSI_COLOR_GREEN "\x1b[32m"
|
||||
#define ANSI_COLOR_YELLOW "\x1b[33m"
|
||||
#define ANSI_COLOR_BLUE "\x1b[34m"
|
||||
#define ANSI_COLOR_MAGENTA "\x1b[35m"
|
||||
#define ANSI_COLOR_CYAN "\x1b[36m"
|
||||
#define ANSI_COLOR_RESET "\x1b[0m"
|
||||
|
||||
#define CHECK_EQUAL(ARRAY1, ARRAY2, size) ({ \
|
||||
bool res = true; \
|
||||
for (int _i = 0; _i < size; _i++) { \
|
||||
if (ARRAY1[_i] != ARRAY2[_i]) { \
|
||||
res = false; \
|
||||
break; \
|
||||
} \
|
||||
} \
|
||||
res; \
|
||||
})
|
||||
|
||||
#define PRINT_ARRAY_INT(ARRAY, width, height) ({ \
|
||||
int *_array = (int *) ARRAY; \
|
||||
for (int _j = 0; _j < height; _j++) { \
|
||||
for (int _i = 0; _i < width; _i++) { \
|
||||
printf("%d\t", _array[width * _j + _i]); \
|
||||
} \
|
||||
printf("\n"); \
|
||||
} \
|
||||
printf("\n"); \
|
||||
})
|
||||
|
||||
#define PRINT_ARRAY_HEX(ARRAY, width, height) ({ \
|
||||
uint8_t *_array = (uint8_t *) ARRAY; \
|
||||
for (int _j = 0; _j < height; _j++) { \
|
||||
for (int _i = 0; _i < width; _i++) { \
|
||||
printf("%02x\t", _array[width * _j + _i]); \
|
||||
} \
|
||||
printf("\n"); \
|
||||
} \
|
||||
printf("\n"); \
|
||||
})
|
||||
343
code/components/esp-nn/tests/src/basic_math_test.c
Normal file
343
code/components/esp-nn/tests/src/basic_math_test.c
Normal file
@@ -0,0 +1,343 @@
|
||||
// Copyright 2020-2021 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// 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.
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
|
||||
#include <common_functions.h>
|
||||
#include <esp_nn.h>
|
||||
#include "test_utils.h"
|
||||
|
||||
#if CONFIG_IDF_CMAKE
|
||||
#define IDF_HEAP_CAPS 1
|
||||
|
||||
#if IDF_HEAP_CAPS
|
||||
#include "esp_heap_caps.h"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
void esp_nn_add_elementwise_s8_test()
|
||||
{
|
||||
/* prepare data */
|
||||
const int size = 1600 + 8 + 7; /* odd len to test leftover */
|
||||
int8_t *input1;
|
||||
int8_t *input2;
|
||||
int8_t *out_data_c;
|
||||
int8_t *out_data_opt;
|
||||
int8_t *input1_orig = NULL;
|
||||
int8_t *input2_orig = NULL;
|
||||
int8_t *out_c_orig = NULL;
|
||||
int8_t *out_opt_orig = NULL;
|
||||
int32_t input1_offset = 34;
|
||||
int32_t input2_offset = 35;
|
||||
int32_t output_offset = 36;
|
||||
int32_t input1_shift = -8; // right_shift amt always <= 0
|
||||
int32_t input2_shift = -8; // right_shift amt always <= 0
|
||||
int32_t output_shift = -9; // right_shift amt always <= 0
|
||||
int32_t left_shift = 15; // always +ve
|
||||
int32_t input1_mult = INT32_MAX;
|
||||
int32_t input2_mult = INT32_MAX;
|
||||
int32_t output_mult = INT32_MAX;
|
||||
int32_t activation_min = -128;
|
||||
int32_t activation_max = 127;
|
||||
|
||||
for (int itr = 0; itr < 10; itr++) {
|
||||
switch (itr) {
|
||||
case 0: // all zeros
|
||||
input1_offset = 0;
|
||||
input2_offset = 0;
|
||||
output_offset = 0;
|
||||
input1_mult = 0;
|
||||
input2_mult = 0;
|
||||
output_mult = 0;
|
||||
input1_shift = 0;
|
||||
input2_shift = 0;
|
||||
output_shift = 0;
|
||||
left_shift = 0;
|
||||
break;
|
||||
case 1: // hit min
|
||||
input1_offset = -127;
|
||||
input2_offset = -127;
|
||||
output_offset = -128;
|
||||
input1_mult = MULT_MIN;
|
||||
input2_mult = MULT_MIN;
|
||||
output_mult = MULT_MIN;
|
||||
input1_shift = 0;
|
||||
input2_shift = 0;
|
||||
output_shift = 0;
|
||||
left_shift = 0;
|
||||
break;
|
||||
case 2: // hit max
|
||||
input1_offset = 128;
|
||||
input2_offset = 128;
|
||||
output_offset = -127;
|
||||
input1_mult = MULT_MAX;
|
||||
input2_mult = MULT_MAX;
|
||||
output_mult = MULT_MAX;
|
||||
input1_shift = SHIFT_MIN;
|
||||
input2_shift = SHIFT_MIN;
|
||||
output_shift = SHIFT_MIN;
|
||||
left_shift = 30 - 8; // since input is 8 bits
|
||||
break;
|
||||
case 3: // hit extreme max
|
||||
input1_offset = 128;
|
||||
input2_offset = 128;
|
||||
output_offset = -127;
|
||||
input1_mult = MULT_MAX;
|
||||
input2_mult = MULT_MAX;
|
||||
output_mult = MULT_MAX;
|
||||
input1_shift = 0;
|
||||
input2_shift = 0;
|
||||
output_shift = 0;
|
||||
left_shift = 30 - 8; // -8 since input is 8 bit
|
||||
break;
|
||||
default: // practical random input
|
||||
input1_offset = rand() % 256 - 127; // range [-127, 128]
|
||||
input2_offset = rand() % 256 - 127; // range [-127, 128]
|
||||
output_offset = rand() % 256 - 128; // range [-128, 127]
|
||||
input1_mult = MULT_MAX / 2 + rand() % INT16_MAX;
|
||||
input2_mult = MULT_MAX / 2 + rand() % INT16_MAX;
|
||||
output_mult = MULT_MAX / 2 + rand() % INT16_MAX;
|
||||
input1_shift = -8 + rand() % 4;
|
||||
input2_shift = -8 + rand() % 4;
|
||||
output_shift = -8 + rand() % 4;
|
||||
left_shift = rand() % 15;
|
||||
}
|
||||
#if IDF_HEAP_CAPS
|
||||
input1_orig = (int8_t *) heap_caps_malloc(size + 32, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
input2_orig = (int8_t *) heap_caps_malloc(size + 32, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
out_c_orig = (int8_t *) heap_caps_malloc(size + 32, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
out_opt_orig = (int8_t *) heap_caps_malloc(size + 32, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
|
||||
input1 = 16 + input1_orig - ((uint32_t) input1_orig & 0xf);
|
||||
input2 = 16 + input2_orig - ((uint32_t) input2_orig & 0xf);
|
||||
out_data_c = 16 + out_c_orig - ((uint32_t) out_c_orig & 0xf);
|
||||
out_data_opt = 16 + out_opt_orig - ((uint32_t) out_opt_orig & 0xf);
|
||||
#else
|
||||
input1 = memalign(16, size);
|
||||
input2 = memalign(16, size);
|
||||
out_data_c = memalign(16, size);
|
||||
out_data_opt = memalign(16, size);
|
||||
|
||||
input1_orig = input1;
|
||||
input2_orig = input2;
|
||||
out_c_orig = out_data_c;
|
||||
out_opt_orig = out_data_opt;
|
||||
#endif
|
||||
|
||||
for (int i = 0; i < size; ++i) {
|
||||
input1[i] = rand() % 256 - 128;
|
||||
input2[i] = rand() % 256 - 128;
|
||||
}
|
||||
|
||||
if (itr == 0) {
|
||||
/* enable profiler */
|
||||
profile_c_start();
|
||||
}
|
||||
/* C function */
|
||||
esp_nn_add_elementwise_s8_ansi(input1, input2, input1_offset, input2_offset,
|
||||
input1_mult, input2_mult, input1_shift, input2_shift,
|
||||
left_shift, out_data_c, output_offset, output_mult,
|
||||
output_shift, activation_min, activation_max, size);
|
||||
|
||||
if (itr == 0) {
|
||||
profile_c_end();
|
||||
profile_opt_start();
|
||||
}
|
||||
|
||||
/* Optimized function */
|
||||
esp_nn_add_elementwise_s8(input1, input2, input1_offset, input2_offset,
|
||||
input1_mult, input2_mult, input1_shift, input2_shift,
|
||||
left_shift, out_data_opt, output_offset, output_mult,
|
||||
output_shift, activation_min, activation_max, size);
|
||||
if (itr == 0) {
|
||||
/* disable profiler */
|
||||
profile_opt_end();
|
||||
}
|
||||
|
||||
bool ret = CHECK_EQUAL(out_data_c, out_data_opt, size);
|
||||
if (ret == false) {
|
||||
printf(ANSI_COLOR_RED"%s[%d] failed\n"ANSI_COLOR_RESET, __FUNCTION__, itr);
|
||||
printf("Output: \n");
|
||||
PRINT_ARRAY_HEX(out_data_opt, size, 1);
|
||||
printf("Expected: \n");
|
||||
PRINT_ARRAY_HEX(out_data_c, size, 1);
|
||||
printf("Input1:\n");
|
||||
PRINT_ARRAY_HEX(input1, size, 1);
|
||||
printf("Input2:\n");
|
||||
PRINT_ARRAY_HEX(input2, size, 1);
|
||||
printf("in1_shift %d, in2_shift %d, left_shift %d, out_shift %d\n",
|
||||
input1_shift, input2_shift, left_shift, output_shift);
|
||||
printf("in1_mult %d, in2_mult %d, out_mult %d\n", input1_mult, input2_mult, output_mult);
|
||||
goto elementwise_add_test_cleanup;
|
||||
}
|
||||
printf(ANSI_COLOR_GREEN"%s[%d] passed\n"ANSI_COLOR_RESET, __FUNCTION__, itr);
|
||||
|
||||
elementwise_add_test_cleanup:
|
||||
if (input1_orig) {
|
||||
free(input1_orig);
|
||||
}
|
||||
if (input2_orig) {
|
||||
free(input2_orig);
|
||||
}
|
||||
if (out_data_c) {
|
||||
free(out_c_orig);
|
||||
}
|
||||
if (out_data_opt) {
|
||||
free(out_opt_orig);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void esp_nn_mul_elementwise_s8_test()
|
||||
{
|
||||
/* prepare data */
|
||||
const int size = 1600 + 8 + 7; /* odd len to test leftover */
|
||||
int8_t *input1;
|
||||
int8_t *input2;
|
||||
int8_t *out_data_c;
|
||||
int8_t *out_data_opt;
|
||||
int32_t input1_offset = 34;
|
||||
int32_t input2_offset = 35;
|
||||
int32_t output_offset = 36;
|
||||
int32_t output_shift = -7;
|
||||
int32_t output_mult = MULT_MAX; // max out_mult
|
||||
int32_t activation_min = -128;
|
||||
int32_t activation_max = 127;
|
||||
int8_t *input1_orig = NULL;
|
||||
int8_t *input2_orig = NULL;
|
||||
int8_t *out_c_orig = NULL;
|
||||
int8_t *out_opt_orig = NULL;
|
||||
|
||||
for (int itr = 0; itr < 10; itr++) {
|
||||
switch (itr) {
|
||||
case 0: // all zeros
|
||||
input1_offset = 0;
|
||||
input2_offset = 0;
|
||||
output_offset = 0;
|
||||
output_mult = 0;
|
||||
output_shift = 0;
|
||||
break;
|
||||
case 1: // hit min
|
||||
input1_offset = -127;
|
||||
input2_offset = -127;
|
||||
output_offset = -128;
|
||||
output_mult = MULT_MIN;
|
||||
output_shift = 0;
|
||||
break;
|
||||
case 2: // hit max
|
||||
input1_offset = 128;
|
||||
input2_offset = 128;
|
||||
output_offset = -127;
|
||||
output_mult = MULT_MAX;
|
||||
output_shift = SHIFT_MIN;
|
||||
break;
|
||||
case 3: // hit extreme max
|
||||
input1_offset = 128;
|
||||
input2_offset = 128;
|
||||
output_offset = -127;
|
||||
output_mult = MULT_MAX;
|
||||
output_shift = 0;
|
||||
break;
|
||||
default: // practical random input
|
||||
input1_offset = rand() % 256 - 127; // range [-127, 128]
|
||||
input2_offset = rand() % 256 - 127; // range [-127, 128]
|
||||
output_offset = rand() % 256 - 128; // range [-128, 127]
|
||||
output_mult = MULT_MAX / 2 + rand() % INT16_MAX;
|
||||
output_shift = -8 + rand() % 4;
|
||||
}
|
||||
|
||||
#if IDF_HEAP_CAPS
|
||||
input1_orig = (int8_t *) heap_caps_malloc(size + 32, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
input2_orig = (int8_t *) heap_caps_malloc(size + 32, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
out_c_orig = (int8_t *) heap_caps_malloc(size + 32, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
out_opt_orig = (int8_t *) heap_caps_malloc(size + 32, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
|
||||
input1 = 16 + input1_orig - ((uint32_t) input1_orig & 0xf);
|
||||
input2 = 16 + input2_orig - ((uint32_t) input2_orig & 0xf);
|
||||
out_data_c = 16 + out_c_orig - ((uint32_t) out_c_orig & 0xf);
|
||||
out_data_opt = 16 + out_opt_orig - ((uint32_t) out_opt_orig & 0xf);
|
||||
#else
|
||||
input1 = memalign(16, size);
|
||||
input2 = memalign(16, size);
|
||||
out_data_c = memalign(16, size);
|
||||
out_data_opt = memalign(16, size);
|
||||
|
||||
input1_orig = input1;
|
||||
input2_orig = input2;
|
||||
out_c_orig = out_data_c;
|
||||
out_opt_orig = out_data_opt;
|
||||
#endif
|
||||
|
||||
for (int i = 0; i < size; ++i) {
|
||||
input1[i] = rand() % 256 - 128;
|
||||
input2[i] = rand() % 256 - 128;
|
||||
}
|
||||
|
||||
if (itr == 0) {
|
||||
/* enable profiler */
|
||||
profile_c_start();
|
||||
}
|
||||
/* C function */
|
||||
esp_nn_mul_elementwise_s8_ansi(input1, input2, input1_offset, input2_offset,
|
||||
out_data_c, output_offset, output_mult, output_shift,
|
||||
activation_min, activation_max, size);
|
||||
|
||||
if (itr == 0) {
|
||||
profile_c_end();
|
||||
profile_opt_start();
|
||||
}
|
||||
/* Optimized function */
|
||||
esp_nn_mul_elementwise_s8(input1, input2, input1_offset, input2_offset,
|
||||
out_data_opt, output_offset, output_mult, output_shift,
|
||||
activation_min, activation_max, size);
|
||||
|
||||
if (itr == 0) {
|
||||
/* disable profiler */
|
||||
profile_opt_end();
|
||||
}
|
||||
|
||||
bool ret = CHECK_EQUAL(out_data_c, out_data_opt, size);
|
||||
if (ret == false) {
|
||||
printf(ANSI_COLOR_RED"%s[%d] failed\n"ANSI_COLOR_RESET, __FUNCTION__, itr);
|
||||
printf("Output: \n");
|
||||
PRINT_ARRAY_HEX(out_data_opt, size, 1);
|
||||
printf("Expected: \n");
|
||||
PRINT_ARRAY_HEX(out_data_c, size, 1);
|
||||
printf("Input1:\n");
|
||||
PRINT_ARRAY_HEX(input1, size, 1);
|
||||
printf("Input2:\n");
|
||||
PRINT_ARRAY_HEX(input2, size, 1);
|
||||
goto elementwise_mult_test_cleanup;
|
||||
}
|
||||
printf(ANSI_COLOR_GREEN"%s[%d] passed\n"ANSI_COLOR_RESET, __FUNCTION__, itr);
|
||||
|
||||
elementwise_mult_test_cleanup:
|
||||
if (input1_orig) {
|
||||
free(input1_orig);
|
||||
}
|
||||
if (input2_orig) {
|
||||
free(input2_orig);
|
||||
}
|
||||
if (out_data_c) {
|
||||
free(out_c_orig);
|
||||
}
|
||||
if (out_data_opt) {
|
||||
free(out_opt_orig);
|
||||
}
|
||||
}
|
||||
}
|
||||
571
code/components/esp-nn/tests/src/convolution_test.c
Normal file
571
code/components/esp-nn/tests/src/convolution_test.c
Normal file
@@ -0,0 +1,571 @@
|
||||
// Copyright 2020-2021 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// 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.
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
|
||||
#include <esp_nn.h>
|
||||
#include "test_utils.h"
|
||||
|
||||
#if CONFIG_IDF_CMAKE
|
||||
#define IDF_HEAP_CAPS 1
|
||||
|
||||
#if IDF_HEAP_CAPS
|
||||
#include "esp_heap_caps.h"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
void esp_nn_depthwise_conv_s8_test()
|
||||
{
|
||||
int8_t *input = NULL, *filter_data = NULL, *out_data_c = NULL, *out_data_opt = NULL;
|
||||
int32_t *bias = NULL;
|
||||
int32_t input_offset = 5; /* some number in [-128, 127] */
|
||||
int32_t out_offset = 7;
|
||||
int32_t activation_min = -125;
|
||||
int32_t activation_max = 120;
|
||||
void *scratch_buf = NULL;
|
||||
|
||||
/* independent variables */
|
||||
int input_wd, input_ht, channels;
|
||||
uint16_t filter_ht, filter_wd, ch_mult;
|
||||
uint16_t pad_wd, pad_ht, stride_wd, stride_ht;
|
||||
|
||||
// run for 10 iterations
|
||||
for (int itr = 0; itr < 10; itr++) {
|
||||
/* prepare data */
|
||||
switch (itr) {
|
||||
case 0: // (ch_mult 1, (channels % 16) = 0), filter (3,3), pad (0,0)
|
||||
input_wd = 18;
|
||||
input_ht = 18;
|
||||
filter_ht = 3;
|
||||
filter_wd = 3;
|
||||
ch_mult = 1;
|
||||
channels = 16;
|
||||
pad_wd = 0;
|
||||
pad_ht = 0;
|
||||
stride_wd = 1;
|
||||
stride_ht = 1;
|
||||
break;
|
||||
case 1: // (ch_mult 1, (channels % 16) = 0), filter (3,3), pad (1,1)
|
||||
input_wd = 10;
|
||||
input_ht = 10;
|
||||
filter_ht = 3;
|
||||
filter_wd = 3;
|
||||
ch_mult = 1;
|
||||
channels = 16;
|
||||
pad_wd = 1;
|
||||
pad_ht = 1;
|
||||
stride_wd = 1;
|
||||
stride_ht = 1;
|
||||
break;
|
||||
case 2: // (ch_mult 1, (channels % 8) = 0), filter (3,3), pad (1,1)
|
||||
input_wd = 10;
|
||||
input_ht = 10;
|
||||
filter_ht = 3;
|
||||
filter_wd = 3;
|
||||
ch_mult = 1;
|
||||
channels = 24;
|
||||
pad_wd = 1;
|
||||
pad_ht = 1;
|
||||
stride_wd = 1;
|
||||
stride_ht = 1;
|
||||
break;
|
||||
case 3: // other filter sizes (ch_mult 1, (channels % 8) = 0)
|
||||
input_wd = 10;
|
||||
input_ht = 10;
|
||||
filter_ht = 3;
|
||||
filter_wd = 3;
|
||||
ch_mult = 1;
|
||||
channels = 24;
|
||||
pad_wd = 1;
|
||||
pad_ht = 1;
|
||||
stride_wd = 1;
|
||||
stride_ht = 1;
|
||||
break;
|
||||
case 4: // other filter sizes (ch_mult 8 = 0)
|
||||
input_wd = 6;
|
||||
input_ht = 6;
|
||||
filter_ht = 3;
|
||||
filter_wd = 3;
|
||||
ch_mult = 8;
|
||||
channels = 4;
|
||||
pad_wd = 1;
|
||||
pad_ht = 1;
|
||||
stride_wd = 1;
|
||||
stride_ht = 1;
|
||||
break;
|
||||
case 5: // other filter sizes (ch_mult 8 = 0)
|
||||
input_wd = 12;
|
||||
input_ht = 12;
|
||||
filter_ht = 5;
|
||||
filter_wd = 5;
|
||||
ch_mult = 8;
|
||||
channels = 4;
|
||||
pad_wd = 1;
|
||||
pad_ht = 1;
|
||||
stride_wd = 1;
|
||||
stride_ht = 1;
|
||||
break;
|
||||
case 6: // other filter sizes (ch_mult 4 = 0)
|
||||
input_wd = 6;
|
||||
input_ht = 6;
|
||||
filter_ht = 3;
|
||||
filter_wd = 3;
|
||||
ch_mult = 4;
|
||||
channels = 4;
|
||||
pad_wd = 1;
|
||||
pad_ht = 1;
|
||||
stride_wd = 1;
|
||||
stride_ht = 1;
|
||||
break;
|
||||
case 7: // (ch_mult 1, (channels % 16) = 0), filter (3,3), pad (0,0) stride (2,2)
|
||||
input_wd = 6;
|
||||
input_ht = 6;
|
||||
filter_ht = 3;
|
||||
filter_wd = 3;
|
||||
ch_mult = 1;
|
||||
channels = 16;
|
||||
pad_wd = 0;
|
||||
pad_ht = 0;
|
||||
stride_wd = 2;
|
||||
stride_ht = 2;
|
||||
break;
|
||||
default:
|
||||
input_wd = 4;
|
||||
input_ht = 4;
|
||||
filter_ht = 3;
|
||||
filter_wd = 3;
|
||||
ch_mult = 4;
|
||||
channels = 4;
|
||||
pad_wd = 1;
|
||||
pad_ht = 1;
|
||||
stride_wd = 1;
|
||||
stride_ht = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
uint16_t out_wd = (input_wd - filter_wd + 1) / stride_wd;
|
||||
uint16_t out_ht = (input_ht - filter_ht + 1) / stride_ht;
|
||||
int in_size = input_wd * input_ht * channels;
|
||||
int out_size = out_wd * out_ht * channels * ch_mult;
|
||||
int filter_size = filter_wd * filter_ht * channels * ch_mult + 4;
|
||||
int bias_size = channels * ch_mult + 1;
|
||||
int32_t out_shift[channels * ch_mult];
|
||||
int32_t out_mult[channels * ch_mult];
|
||||
|
||||
#if IDF_HEAP_CAPS
|
||||
int8_t *input_orig = (int8_t *) heap_caps_malloc(in_size + 32, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
int8_t *out_c_orig = (int8_t *) heap_caps_malloc(out_size + 32, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
int8_t *out_opt_orig = (int8_t *) heap_caps_malloc(out_size + 32, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
filter_data = (int8_t *) heap_caps_malloc(filter_size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
bias = (int32_t *) heap_caps_malloc(bias_size * 4, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
|
||||
input = 16 + input_orig - ((uint32_t) input_orig & 0xf);
|
||||
out_data_c = 16 + out_c_orig - ((uint32_t) out_c_orig & 0xf);
|
||||
out_data_opt = 16 + out_opt_orig - ((uint32_t) out_opt_orig & 0xf);
|
||||
#else
|
||||
input = memalign(16, in_size + 16);
|
||||
filter_data = memalign(16, filter_size);
|
||||
out_data_c = memalign(16, out_size + 16);
|
||||
out_data_opt = memalign(16, out_size + 16);
|
||||
bias = memalign(16, bias_size * 4);
|
||||
int8_t *input_orig = input;
|
||||
int8_t *out_c_orig = out_data_c;
|
||||
int8_t *out_opt_orig = out_data_opt;
|
||||
#endif
|
||||
if (bias == NULL || input == NULL || filter_data == NULL ||
|
||||
out_data_c == NULL || out_data_opt == NULL || bias == NULL) {
|
||||
printf(ANSI_COLOR_RED"%s[%d] allocations failed\n"ANSI_COLOR_RESET, __FUNCTION__, itr);
|
||||
goto dc_s8_cleanup;
|
||||
}
|
||||
|
||||
/* Generate input data */
|
||||
for (int i = 0; i < in_size; ++i) {
|
||||
input[i] = rand() % 128;
|
||||
}
|
||||
|
||||
/* Generate filter data */
|
||||
for (int i = 0; i < filter_size; ++i) {
|
||||
filter_data[i] = rand() % 256 - 128;
|
||||
}
|
||||
|
||||
/* Generate bias data */
|
||||
for (int i = 0; i < channels * ch_mult; ++i) {
|
||||
bias[i + 1] = rand() % INT16_MAX; //0th index left for unalignment
|
||||
out_shift[i] = -8 + rand() % 3;
|
||||
out_mult[i] = 0x7eb0e200 + rand() % 50;
|
||||
}
|
||||
|
||||
int scratch_buf_size = esp_nn_get_depthwise_conv_scratch_size(input_wd, input_ht,
|
||||
channels, ch_mult,
|
||||
filter_wd, filter_ht);
|
||||
if (scratch_buf_size > 0) {
|
||||
#if IDF_HEAP_CAPS
|
||||
scratch_buf = heap_caps_malloc(scratch_buf_size + 32, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
int align_sz = 16 - (((int32_t) scratch_buf) & 0xf);
|
||||
#else
|
||||
scratch_buf = memalign(16, scratch_buf_size);
|
||||
int align_sz = 0;
|
||||
#endif
|
||||
if (scratch_buf == NULL) {
|
||||
printf(ANSI_COLOR_RED"%s[%d] scratch_buf alloc failed size %d\n"ANSI_COLOR_RESET,
|
||||
__FUNCTION__, itr, scratch_buf_size);
|
||||
goto dc_s8_cleanup;
|
||||
}
|
||||
esp_nn_set_depthwise_conv_scratch_buf(scratch_buf + align_sz);
|
||||
}
|
||||
if (itr == 0) {
|
||||
/* enable profiler */
|
||||
profile_c_start();
|
||||
}
|
||||
|
||||
/* C function */
|
||||
esp_nn_depthwise_conv_s8_ansi(input, input_wd, input_ht, channels, input_offset,
|
||||
pad_wd, pad_ht, stride_wd, stride_ht, ch_mult,
|
||||
filter_data + 4, filter_wd, filter_ht,
|
||||
bias + 1, out_data_c, out_wd, out_ht, out_offset, out_shift,
|
||||
out_mult, activation_min, activation_max);
|
||||
|
||||
if (itr == 0) {
|
||||
profile_c_end();
|
||||
profile_opt_start();
|
||||
}
|
||||
|
||||
/* Optimized function */
|
||||
esp_nn_depthwise_conv_s8(input, input_wd, input_ht, channels, input_offset,
|
||||
pad_wd, pad_ht, stride_wd, stride_ht, ch_mult,
|
||||
filter_data + 4, filter_wd, filter_ht,
|
||||
bias + 1, out_data_opt, out_wd, out_ht, out_offset, out_shift,
|
||||
out_mult, activation_min, activation_max);
|
||||
|
||||
if (itr == 0) {
|
||||
/* disable profiler */
|
||||
profile_opt_end();
|
||||
}
|
||||
|
||||
bool ret = CHECK_EQUAL(out_data_c, out_data_opt, out_size);
|
||||
if (ret == false) {
|
||||
printf(ANSI_COLOR_RED"%s[%d] failed\n"ANSI_COLOR_RESET, __FUNCTION__, itr);
|
||||
printf("Output: \n");
|
||||
PRINT_ARRAY_HEX(out_data_opt, out_size / out_ht, out_ht);
|
||||
printf("Expected: \n");
|
||||
PRINT_ARRAY_HEX(out_data_c, out_size / out_ht, out_ht);
|
||||
printf("Input:\n");
|
||||
PRINT_ARRAY_HEX(input, in_size / input_ht, input_ht);
|
||||
printf("Filter data:\n");
|
||||
PRINT_ARRAY_HEX(filter_data + 4, (filter_size - 4) / filter_ht, filter_ht);
|
||||
printf("bias data:\n");
|
||||
PRINT_ARRAY_INT(bias + 1, ch_mult * channels, 1);
|
||||
goto dc_s8_cleanup;
|
||||
}
|
||||
printf(ANSI_COLOR_GREEN"%s[%d] passed\n"ANSI_COLOR_RESET, __FUNCTION__, itr);
|
||||
|
||||
dc_s8_cleanup:
|
||||
if (input) {
|
||||
free(input_orig);
|
||||
}
|
||||
if (filter_data) {
|
||||
free(filter_data);
|
||||
}
|
||||
if (out_data_c) {
|
||||
free(out_c_orig);
|
||||
}
|
||||
if (out_data_opt) {
|
||||
free(out_opt_orig);
|
||||
}
|
||||
if (bias) {
|
||||
free(bias);
|
||||
}
|
||||
if (scratch_buf) {
|
||||
free(scratch_buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void esp_nn_conv_s8_test()
|
||||
{
|
||||
const int32_t input_offset = 5; /* some number in [-128, 127] */
|
||||
const int32_t activation_min = -125;
|
||||
const int32_t activation_max = 122;
|
||||
const int32_t out_offset = 3;
|
||||
|
||||
void *scratch_buf = NULL;
|
||||
int8_t *input_orig;
|
||||
int8_t *out_c_orig;
|
||||
int8_t *out_opt_orig;
|
||||
int8_t *filter_data;
|
||||
int32_t *bias;
|
||||
|
||||
/* independent variable */
|
||||
int in_wd, in_ht, in_channels, out_channels;
|
||||
uint16_t filter_ht, filter_wd;
|
||||
uint16_t pad_wd, pad_ht, stride_wd, stride_ht;
|
||||
|
||||
// run for 10 iterations
|
||||
for (int itr = 0; itr < 10; itr++) {
|
||||
switch (itr) {
|
||||
case 0: // ch % 8 == 0 && filter (1,1), padding (0,0)
|
||||
in_wd = 10;
|
||||
in_ht = 10;
|
||||
in_channels = 64;
|
||||
out_channels = 64;
|
||||
filter_ht = 1;
|
||||
filter_wd = 1;
|
||||
pad_wd = 0;
|
||||
pad_ht = 0;
|
||||
stride_wd = 1;
|
||||
stride_ht = 1;
|
||||
break;
|
||||
case 1: // ch % 4 == 0 && (in_wd * in_ht) % 16 == 0
|
||||
in_wd = 4;
|
||||
in_ht = 4;
|
||||
in_channels = 20;
|
||||
out_channels = 8;
|
||||
filter_ht = 1;
|
||||
filter_wd = 1;
|
||||
pad_wd = 0;
|
||||
pad_ht = 0;
|
||||
stride_wd = 1;
|
||||
stride_ht = 1;
|
||||
break;
|
||||
case 2: // ch, filter (3x3x3)
|
||||
in_wd = 10;
|
||||
in_ht = 10;
|
||||
in_channels = 3;
|
||||
out_channels = 64;
|
||||
filter_ht = 3;
|
||||
filter_wd = 3;
|
||||
pad_wd = 0;
|
||||
pad_ht = 0;
|
||||
stride_wd = 1;
|
||||
stride_ht = 1;
|
||||
break;
|
||||
case 3: // remaining pad (0, 0)
|
||||
in_wd = 10;
|
||||
in_ht = 10;
|
||||
in_channels = 3;
|
||||
out_channels = 64;
|
||||
filter_ht = 1;
|
||||
filter_wd = 1;
|
||||
pad_wd = 0;
|
||||
pad_ht = 0;
|
||||
stride_wd = 1;
|
||||
stride_ht = 1;
|
||||
break;
|
||||
case 4: // unopt case
|
||||
in_wd = 10;
|
||||
in_ht = 10;
|
||||
in_channels = 12;
|
||||
out_channels = 64;
|
||||
filter_ht = 3;
|
||||
filter_wd = 3;
|
||||
pad_wd = 1;
|
||||
pad_ht = 1;
|
||||
stride_wd = 1;
|
||||
stride_ht = 1;
|
||||
break;
|
||||
case 5: // ch % 8 == 0 & stride (2,2)
|
||||
in_wd = 16;
|
||||
in_ht = 16;
|
||||
in_channels = 16;
|
||||
out_channels = 16;
|
||||
filter_ht = 1;
|
||||
filter_wd = 1;
|
||||
pad_wd = 0;
|
||||
pad_ht = 0;
|
||||
stride_wd = 2;
|
||||
stride_ht = 2;
|
||||
break;
|
||||
case 6: // ch % 8 == 0 && filter (1,1), padding (0,0)
|
||||
in_wd = 2;
|
||||
in_ht = 2;
|
||||
in_channels = 8;
|
||||
out_channels = 8;
|
||||
filter_ht = 1;
|
||||
filter_wd = 1;
|
||||
pad_wd = 0;
|
||||
pad_ht = 0;
|
||||
stride_wd = 1;
|
||||
stride_ht = 1;
|
||||
break;
|
||||
default: // ch % 8 == 0
|
||||
in_wd = 8;
|
||||
in_ht = 8;
|
||||
in_channels = 16;
|
||||
out_channels = 16;
|
||||
filter_ht = 1;
|
||||
filter_wd = 1;
|
||||
pad_wd = 0;
|
||||
pad_ht = 0;
|
||||
stride_wd = 1;
|
||||
stride_ht = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
/* prepare data */
|
||||
uint16_t out_wd = (in_wd - filter_wd + 1) / stride_wd;
|
||||
uint16_t out_ht = (in_ht - filter_ht + 1) / stride_ht;
|
||||
|
||||
int in_size = in_wd * in_ht * in_channels;
|
||||
int filter_size = filter_wd * filter_ht * in_channels * out_channels + 2;
|
||||
int out_size = out_wd * out_ht * out_channels;
|
||||
|
||||
#if IDF_HEAP_CAPS
|
||||
input_orig = (int8_t *) heap_caps_malloc(in_size + 32, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
out_c_orig = (int8_t *) heap_caps_malloc(out_size + 32, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
out_opt_orig = (int8_t *) heap_caps_malloc(out_size + 32, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
filter_data = (int8_t *) heap_caps_malloc(filter_size + 32, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
bias = (int32_t *) heap_caps_malloc(128 + sizeof (int32_t) * out_channels, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
|
||||
int8_t *input = 16 + input_orig - ((uint32_t) input_orig & 0xf);
|
||||
int8_t *out_data_c = 16 + out_c_orig - ((uint32_t) out_c_orig & 0xf);
|
||||
int8_t *out_data_opt = 16 + out_opt_orig - ((uint32_t) out_opt_orig & 0xf);
|
||||
#else
|
||||
int8_t *input = memalign(16, in_size);
|
||||
int8_t *out_data_c = memalign(16, out_size);
|
||||
int8_t *out_data_opt = memalign(16, out_size);
|
||||
filter_data = memalign(16, filter_size);
|
||||
bias = calloc(1, 128 + sizeof (int32_t) * out_channels);
|
||||
input_orig = input;
|
||||
out_c_orig = out_data_c;
|
||||
out_opt_orig = out_data_opt;
|
||||
#endif
|
||||
int32_t *out_shift = calloc(1, 128 + sizeof (int32_t) * out_channels);
|
||||
int32_t *out_mult = calloc(1, 128 + sizeof (int32_t) * out_channels);
|
||||
|
||||
if (input == NULL || filter_data == NULL ||
|
||||
out_data_c == NULL || out_data_opt == NULL) {
|
||||
printf(ANSI_COLOR_RED"%s allocations failed\n"ANSI_COLOR_RESET, __FUNCTION__);
|
||||
goto conv_s8_cleanup;
|
||||
}
|
||||
|
||||
if (bias == NULL || out_shift == NULL || out_mult == NULL) {
|
||||
printf(ANSI_COLOR_RED"%s allocations failed\n"ANSI_COLOR_RESET, __FUNCTION__);
|
||||
goto conv_s8_cleanup;
|
||||
}
|
||||
|
||||
/* Generate input data between -128 -> +127 */
|
||||
for (int i = 0; i < in_size; ++i) {
|
||||
input[i] = rand() % 255 - 128;
|
||||
}
|
||||
|
||||
/* Generate filter data between -128 -> +127 */
|
||||
for (int i = 0; i < filter_size; ++i) {
|
||||
filter_data[i] = rand() % 256 - 128;
|
||||
}
|
||||
|
||||
/* Generate bias data */
|
||||
for (int i = 0; i < out_channels; ++i) {
|
||||
bias[i] = (int32_t)rand() % UINT16_MAX + UINT8_MAX;
|
||||
}
|
||||
|
||||
/* Shift and multiplier */
|
||||
for (int i = 0; i < out_channels; ++i) {
|
||||
out_shift[i] = -10 + rand() % 2;
|
||||
out_mult[i] = 0x7f67f4f8 + rand() % 50;
|
||||
}
|
||||
|
||||
int scratch_buf_size = esp_nn_get_conv_scratch_size(in_wd, in_ht, in_channels,
|
||||
out_channels, filter_wd, filter_ht);
|
||||
if (scratch_buf_size > 0) {
|
||||
#if IDF_HEAP_CAPS
|
||||
void *scratch_buf = heap_caps_malloc(scratch_buf_size + 32, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
int align_sz = 16 - (((int32_t) scratch_buf) & 0xf);
|
||||
#else
|
||||
void *scratch_buf = memalign(16, scratch_buf_size);
|
||||
int align_sz = 0;
|
||||
#endif
|
||||
if (scratch_buf == NULL) {
|
||||
printf(ANSI_COLOR_RED"%s scratch_buf alloc failed size %d\n"ANSI_COLOR_RESET, __FUNCTION__, scratch_buf_size);
|
||||
goto conv_s8_cleanup;
|
||||
}
|
||||
esp_nn_set_conv_scratch_buf(scratch_buf + align_sz);
|
||||
}
|
||||
|
||||
if (itr == 0) {
|
||||
/* enable profiler */
|
||||
profile_c_start();
|
||||
}
|
||||
|
||||
/* C function */
|
||||
esp_nn_conv_s8_ansi(input, in_wd, in_ht, in_channels, input_offset,
|
||||
pad_wd, pad_ht, stride_wd, stride_ht,
|
||||
filter_data + 2, filter_wd, filter_ht, bias,
|
||||
out_data_c, out_wd, out_ht, out_channels, out_offset, out_shift,
|
||||
out_mult, activation_min, activation_max);
|
||||
|
||||
if (itr == 0) {
|
||||
profile_c_end();
|
||||
profile_opt_start();
|
||||
}
|
||||
|
||||
/* Optimized function */
|
||||
esp_nn_conv_s8(input, in_wd, in_ht, in_channels, input_offset,
|
||||
pad_wd, pad_ht, stride_wd, stride_ht,
|
||||
filter_data + 2, filter_wd, filter_ht, bias,
|
||||
out_data_opt, out_wd, out_ht, out_channels, out_offset, out_shift,
|
||||
out_mult, activation_min, activation_max);
|
||||
|
||||
if (itr == 0) {
|
||||
/* disable profiler */
|
||||
profile_opt_end();
|
||||
}
|
||||
|
||||
bool ret = CHECK_EQUAL(out_data_c, out_data_opt, out_size);
|
||||
if (ret == false) {
|
||||
printf(ANSI_COLOR_RED"%s[%d] failed\n"ANSI_COLOR_RESET, __FUNCTION__, itr);
|
||||
printf("Output: \n");
|
||||
PRINT_ARRAY_HEX(out_data_opt, out_size / out_ht, out_ht);
|
||||
printf("Expected: \n");
|
||||
PRINT_ARRAY_HEX(out_data_c, out_size / out_ht, out_ht);
|
||||
printf("Input:\n");
|
||||
PRINT_ARRAY_HEX(input, in_size / in_ht, in_ht);
|
||||
printf("Filter data:\n");
|
||||
PRINT_ARRAY_HEX(filter_data + 2, (filter_size - 2) / filter_ht, filter_ht);
|
||||
printf("bias data:\n");
|
||||
PRINT_ARRAY_INT(bias, out_channels, 1);
|
||||
goto conv_s8_cleanup;
|
||||
}
|
||||
printf(ANSI_COLOR_GREEN"%s[%d] passed\n"ANSI_COLOR_RESET, __FUNCTION__, itr);
|
||||
|
||||
conv_s8_cleanup:
|
||||
if (input) {
|
||||
free(input_orig);
|
||||
}
|
||||
if (filter_data) {
|
||||
free(filter_data);
|
||||
}
|
||||
if (out_data_c) {
|
||||
free(out_c_orig);
|
||||
}
|
||||
if (out_data_opt) {
|
||||
free(out_opt_orig);
|
||||
}
|
||||
if (bias) {
|
||||
free(bias);
|
||||
}
|
||||
if (out_shift) {
|
||||
free(out_shift);
|
||||
}
|
||||
if (out_mult) {
|
||||
free(out_mult);
|
||||
}
|
||||
if (scratch_buf) {
|
||||
free(scratch_buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
111
code/components/esp-nn/tests/src/fully_connected_test.c
Normal file
111
code/components/esp-nn/tests/src/fully_connected_test.c
Normal file
@@ -0,0 +1,111 @@
|
||||
// Copyright 2020-2021 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// 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.
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <esp_nn.h>
|
||||
#include "test_utils.h"
|
||||
|
||||
|
||||
void esp_nn_fully_connected_s8_test()
|
||||
{
|
||||
/* prepare data */
|
||||
static uint16_t row_len = 256 + 8 + 7; /* odd len to test unaligned+left-over */
|
||||
static uint16_t out_channels = 3;
|
||||
int8_t input[row_len];
|
||||
int8_t filter_data[row_len * out_channels];
|
||||
int8_t output_c[out_channels], output_opt[out_channels];
|
||||
static int32_t activation_min = -128;
|
||||
static int32_t activation_max = 127;
|
||||
static int32_t input_offset = 0;
|
||||
static int32_t filter_offset = 0;
|
||||
int32_t out_shift = -10;
|
||||
static int32_t out_offset = 127;
|
||||
int32_t out_mult = 0x59e492c4;
|
||||
for (int itr = 0; itr < 5; itr++) {
|
||||
out_mult = INT32_MAX / row_len + rand() % INT16_MAX;
|
||||
switch (itr) {
|
||||
case 0:
|
||||
out_shift = -10;
|
||||
break;
|
||||
case 1:
|
||||
out_shift = SHIFT_MIN;
|
||||
break;
|
||||
case 2:
|
||||
out_shift = SHIFT_MAX;
|
||||
break;
|
||||
case 3:
|
||||
out_shift = 0;
|
||||
break;
|
||||
default:
|
||||
out_shift = -10 + rand() % 5;
|
||||
break;
|
||||
}
|
||||
if (itr == 0) {
|
||||
out_shift = SHIFT_MAX;
|
||||
}
|
||||
/* Generate input and filter data */
|
||||
for (int i = 0; i < row_len; ++i) {
|
||||
input[i] = rand() % 256 - 128;
|
||||
}
|
||||
for (int i = 0; i < row_len * out_channels; ++i) {
|
||||
filter_data[i] = rand() % 256 - 128;
|
||||
}
|
||||
|
||||
if (itr == 0) {
|
||||
/* enable profiler */
|
||||
profile_c_start();
|
||||
}
|
||||
|
||||
/* C function */
|
||||
esp_nn_fully_connected_s8_ansi(input, input_offset, row_len, filter_data, filter_offset,
|
||||
NULL, output_c, out_channels, out_offset, out_shift, out_mult,
|
||||
activation_min, activation_max);
|
||||
|
||||
if (itr == 0) {
|
||||
profile_c_end();
|
||||
profile_opt_start();
|
||||
}
|
||||
|
||||
/* Optimized function */
|
||||
esp_nn_fully_connected_s8(input, input_offset, row_len, filter_data, filter_offset,
|
||||
NULL, output_opt, out_channels, out_offset, out_shift, out_mult,
|
||||
activation_min, activation_max);
|
||||
|
||||
if (itr == 0) {
|
||||
/* disable profiler */
|
||||
profile_opt_end();
|
||||
}
|
||||
|
||||
bool ret = CHECK_EQUAL(output_c, output_opt, out_channels);
|
||||
if (ret == false) {
|
||||
printf(ANSI_COLOR_RED"%s[%d] failed\n"ANSI_COLOR_RESET, __FUNCTION__, itr);
|
||||
printf("Output: \n");
|
||||
PRINT_ARRAY_HEX(output_opt, out_channels, 1);
|
||||
printf("Expected: \n");
|
||||
PRINT_ARRAY_HEX(output_c, out_channels, 1);
|
||||
printf("Input:\n");
|
||||
PRINT_ARRAY_HEX(input, row_len, 1);
|
||||
printf("Filter data:\n");
|
||||
PRINT_ARRAY_HEX(filter_data, row_len, out_channels);
|
||||
printf("Out shift: %d\n", out_shift);
|
||||
printf("Out mult: %x\n", out_mult);
|
||||
return;
|
||||
}
|
||||
printf(ANSI_COLOR_GREEN"%s[%d] passed\n"ANSI_COLOR_RESET, __FUNCTION__, itr);
|
||||
}
|
||||
}
|
||||
184
code/components/esp-nn/tests/src/pooling_test.c
Normal file
184
code/components/esp-nn/tests/src/pooling_test.c
Normal file
@@ -0,0 +1,184 @@
|
||||
// Copyright 2020-2021 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// 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.
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
|
||||
#include <esp_nn.h>
|
||||
#include "test_utils.h"
|
||||
|
||||
|
||||
void esp_nn_avg_pool_s8_test()
|
||||
{
|
||||
/* prepare data */
|
||||
const uint16_t input_wd = 16;
|
||||
const uint16_t input_ht = 16;
|
||||
const uint16_t channels = 16; /* With TFLite example, I have seen it 256 */
|
||||
const int size = input_wd * input_ht * channels;
|
||||
int8_t *input, *output_c, *output_opt;
|
||||
const int32_t activation_min = -128;
|
||||
const int32_t activation_max = 127;
|
||||
const uint16_t pad_wd = 1;
|
||||
const uint16_t pad_ht = 1;
|
||||
const uint16_t stride_wd = 1;
|
||||
const uint16_t stride_ht = 1;
|
||||
const uint16_t filter_ht = 3;
|
||||
const uint16_t filter_wd = 3;
|
||||
const uint16_t out_wd = input_wd / stride_wd;
|
||||
const uint16_t out_ht = input_ht / stride_ht;
|
||||
const int out_size = out_wd * out_ht * channels;
|
||||
|
||||
input = memalign(16, size);
|
||||
output_c = memalign(16, out_size);
|
||||
output_opt = memalign(16, out_size);
|
||||
|
||||
if (input == NULL || output_c == NULL || output_opt == NULL) {
|
||||
printf(ANSI_COLOR_RED"%s allocations failed\n"ANSI_COLOR_RESET, __FUNCTION__);
|
||||
goto avg_pool_s8_cleanup;
|
||||
}
|
||||
/**
|
||||
* width/height, channels etc look suspicious but it it true.
|
||||
* It actually depends upon where in model this is actually placed.
|
||||
* If at the end wd/ht tends to be smaller and depth larger.
|
||||
*/
|
||||
|
||||
for (int i = 0; i < size; ++i) {
|
||||
input[i] = rand() % 256 - 128;
|
||||
}
|
||||
|
||||
/* enable profiler */
|
||||
profile_c_start();
|
||||
|
||||
/* C function */
|
||||
esp_nn_avg_pool_s8_ansi(input, input_wd, input_ht, output_c, out_wd, out_ht,
|
||||
stride_wd, stride_ht, filter_wd, filter_ht, pad_wd, pad_ht,
|
||||
activation_min, activation_max, channels);
|
||||
|
||||
profile_c_end();
|
||||
profile_opt_start();
|
||||
|
||||
/* Optimized function */
|
||||
esp_nn_avg_pool_s8(input, input_wd, input_ht, output_opt, out_wd, out_ht,
|
||||
stride_wd, stride_ht, filter_wd, filter_ht, pad_wd, pad_ht,
|
||||
activation_min, activation_max, channels);
|
||||
|
||||
/* disable profiler */
|
||||
profile_opt_end();
|
||||
|
||||
|
||||
bool ret = CHECK_EQUAL(output_c, output_opt, out_size);
|
||||
if (ret == false) {
|
||||
printf(ANSI_COLOR_RED"%s failed\n"ANSI_COLOR_RESET, __FUNCTION__);
|
||||
printf("Output: \n");
|
||||
PRINT_ARRAY_HEX(output_opt, out_wd * channels, out_ht);
|
||||
printf("Expected: \n");
|
||||
PRINT_ARRAY_HEX(output_c, out_wd * channels, out_ht);
|
||||
printf("Input:\n");
|
||||
PRINT_ARRAY_HEX(input, input_wd * channels, input_ht);
|
||||
goto avg_pool_s8_cleanup;
|
||||
}
|
||||
printf(ANSI_COLOR_GREEN"%s passed\n"ANSI_COLOR_RESET, __FUNCTION__);
|
||||
|
||||
avg_pool_s8_cleanup:
|
||||
if (input) {
|
||||
free(input);
|
||||
}
|
||||
if (output_c) {
|
||||
free(output_c);
|
||||
}
|
||||
if (output_opt) {
|
||||
free(output_opt);
|
||||
}
|
||||
}
|
||||
|
||||
void esp_nn_max_pool_s8_test()
|
||||
{
|
||||
/* prepare data */
|
||||
const uint16_t input_wd = 16;
|
||||
const uint16_t input_ht = 16;
|
||||
const uint16_t channels = 16; /* With TFLite example, I have seen it 256 */
|
||||
int8_t *input, *output_c, *output_opt;
|
||||
const int size = input_wd * input_ht * channels;
|
||||
const int32_t activation_min = -128;
|
||||
const int32_t activation_max = 127;
|
||||
const uint16_t pad_wd = 1;
|
||||
const uint16_t pad_ht = 1;
|
||||
const uint16_t stride_wd = 1;
|
||||
const uint16_t stride_ht = 1;
|
||||
const uint16_t filter_ht = 3;
|
||||
const uint16_t filter_wd = 3;
|
||||
const uint16_t out_wd = input_wd / stride_wd;
|
||||
const uint16_t out_ht = input_ht / stride_ht;
|
||||
const int out_size = out_wd * out_ht * channels;
|
||||
|
||||
input = memalign(16, size);
|
||||
output_c = memalign(16, out_size);
|
||||
output_opt = memalign(16, out_size);
|
||||
|
||||
if (input == NULL || output_c == NULL || output_opt == NULL) {
|
||||
printf(ANSI_COLOR_RED"%s allocations failed\n"ANSI_COLOR_RESET, __FUNCTION__);
|
||||
goto max_pool_s8_cleanup;
|
||||
}
|
||||
|
||||
for (int i = 0; i < size; ++i) {
|
||||
input[i] = rand() % 256 - 128;
|
||||
}
|
||||
|
||||
/* enable profiler */
|
||||
profile_c_start();
|
||||
|
||||
/* C function */
|
||||
esp_nn_max_pool_s8_ansi(input, input_wd, input_ht, output_c, out_wd, out_ht,
|
||||
stride_wd, stride_ht, filter_wd, filter_ht, pad_wd, pad_ht,
|
||||
activation_min, activation_max, channels);
|
||||
|
||||
profile_c_end();
|
||||
profile_opt_start();
|
||||
|
||||
/* Optimized function */
|
||||
esp_nn_max_pool_s8(input, input_wd, input_ht, output_opt, out_wd, out_ht,
|
||||
stride_wd, stride_ht, filter_wd, filter_ht, pad_wd, pad_ht,
|
||||
activation_min, activation_max, channels);
|
||||
|
||||
/* disable profiler */
|
||||
profile_opt_end();
|
||||
|
||||
|
||||
bool ret = CHECK_EQUAL(output_c, output_opt, out_wd * out_ht * channels);
|
||||
if (ret == false) {
|
||||
printf(ANSI_COLOR_RED"%s failed\n"ANSI_COLOR_RESET, __FUNCTION__);
|
||||
printf("Output: \n");
|
||||
PRINT_ARRAY_HEX(output_opt, out_wd * out_ht * channels, 1);
|
||||
printf("Expected: \n");
|
||||
PRINT_ARRAY_HEX(output_c, out_wd * out_ht * channels, 1);
|
||||
printf("Input:\n");
|
||||
PRINT_ARRAY_HEX(input, 8, size / 8);
|
||||
goto max_pool_s8_cleanup;
|
||||
}
|
||||
printf(ANSI_COLOR_GREEN"%s passed\n"ANSI_COLOR_RESET, __FUNCTION__);
|
||||
|
||||
max_pool_s8_cleanup:
|
||||
if (input) {
|
||||
free(input);
|
||||
}
|
||||
if (output_c) {
|
||||
free(output_c);
|
||||
}
|
||||
if (output_opt) {
|
||||
free(output_opt);
|
||||
}
|
||||
}
|
||||
83
code/components/esp-nn/tests/src/relu_test.c
Normal file
83
code/components/esp-nn/tests/src/relu_test.c
Normal file
@@ -0,0 +1,83 @@
|
||||
// Copyright 2020-2021 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// 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.
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
|
||||
#include <esp_nn.h>
|
||||
#include "test_utils.h"
|
||||
|
||||
void esp_nn_relu6_s8_test()
|
||||
{
|
||||
const int size = 1600 + 8 + 7;
|
||||
int8_t *input, *inout_ansi, *inout_opt;
|
||||
|
||||
input = memalign(16, size);
|
||||
inout_ansi = memalign(16, size);
|
||||
inout_opt = memalign(16, size);
|
||||
|
||||
if (input == NULL || inout_ansi == NULL || inout_opt == NULL) {
|
||||
printf(ANSI_COLOR_RED"%s allocations failed\n"ANSI_COLOR_RESET, __FUNCTION__);
|
||||
goto relu6_s8_cleanup;
|
||||
}
|
||||
/* Generate filter data between -128 -> +127 */
|
||||
for (int i = 0; i < size; ++i) {
|
||||
input[i] = rand() % 255 - 128;
|
||||
inout_ansi[i] = input[i];
|
||||
inout_opt[i] = input[i];
|
||||
}
|
||||
|
||||
/* enable profiler */
|
||||
profile_c_start();
|
||||
|
||||
/* C function */
|
||||
esp_nn_relu6_s8_ansi(inout_ansi, size);
|
||||
|
||||
profile_c_end();
|
||||
profile_opt_start();
|
||||
|
||||
/* Optimized function */
|
||||
esp_nn_relu6_s8(inout_opt, size);
|
||||
|
||||
/* disable profiler */
|
||||
profile_opt_end();
|
||||
|
||||
bool ret = CHECK_EQUAL(inout_ansi, inout_opt, size);
|
||||
if (ret == false) {
|
||||
printf(ANSI_COLOR_RED"%s failed\n"ANSI_COLOR_RESET, __FUNCTION__);
|
||||
printf("Output: \n");
|
||||
PRINT_ARRAY_HEX(inout_opt, size, 1);
|
||||
printf("Expected: \n");
|
||||
PRINT_ARRAY_HEX(inout_ansi, size, 1);
|
||||
printf("Input:\n");
|
||||
PRINT_ARRAY_HEX(input, size, 1);
|
||||
goto relu6_s8_cleanup;
|
||||
}
|
||||
printf(ANSI_COLOR_GREEN"%s passed\n"ANSI_COLOR_RESET, __FUNCTION__);
|
||||
|
||||
relu6_s8_cleanup:
|
||||
if (input) {
|
||||
free (input);
|
||||
}
|
||||
if (inout_ansi) {
|
||||
free (inout_ansi);
|
||||
}
|
||||
if (inout_opt) {
|
||||
free (inout_opt);
|
||||
}
|
||||
|
||||
}
|
||||
101
code/components/esp-nn/tests/src/softmax_test.c
Normal file
101
code/components/esp-nn/tests/src/softmax_test.c
Normal file
@@ -0,0 +1,101 @@
|
||||
// Copyright 2022 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// 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.
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
|
||||
#include <esp_nn.h>
|
||||
#include "test_utils.h"
|
||||
|
||||
void esp_nn_softmax_s8_test()
|
||||
{
|
||||
const int32_t height = 8;
|
||||
const int32_t width = 32;
|
||||
const int32_t diff_min = -128;
|
||||
const int32_t mult = INT32_MAX / 2;
|
||||
const int32_t shift = 7;
|
||||
void *scratch_buf = NULL;
|
||||
const int size = width * height;
|
||||
int8_t *input, *out_ansi, *out_opt;
|
||||
|
||||
input = memalign(16, size);
|
||||
out_ansi = memalign(16, size);
|
||||
out_opt = memalign(16, size);
|
||||
|
||||
if (input == NULL || out_ansi == NULL || out_opt == NULL) {
|
||||
printf(ANSI_COLOR_RED"%s buffer allocations failed\n"ANSI_COLOR_RESET, __FUNCTION__);
|
||||
goto softmax_s8_cleanup;
|
||||
}
|
||||
|
||||
/* Generate input data between -128 -> +127 */
|
||||
for (int i = 0; i < size; ++i) {
|
||||
input[i] = rand() % 255 - 128;
|
||||
}
|
||||
|
||||
/* enable profiler */
|
||||
profile_c_start();
|
||||
|
||||
/* C function */
|
||||
esp_nn_softmax_s8_ansi(input, height, width, mult, shift, diff_min, out_ansi);
|
||||
|
||||
profile_c_end();
|
||||
|
||||
int32_t scratch_buf_size = esp_nn_get_softmax_scratch_size(width, height);
|
||||
if (scratch_buf_size) {
|
||||
scratch_buf = memalign(4, scratch_buf_size);
|
||||
if (scratch_buf == NULL) {
|
||||
printf(ANSI_COLOR_RED"%s scratch_buf alloc failed size %d\n"ANSI_COLOR_RESET, __FUNCTION__, scratch_buf_size);
|
||||
goto softmax_s8_cleanup;
|
||||
}
|
||||
esp_nn_set_softmax_scratch_buf(scratch_buf);
|
||||
}
|
||||
|
||||
profile_opt_start();
|
||||
|
||||
/* Optimized function */
|
||||
esp_nn_softmax_s8(input, height, width, mult, shift, diff_min, out_opt);
|
||||
|
||||
/* disable profiler */
|
||||
profile_opt_end();
|
||||
|
||||
bool ret = CHECK_EQUAL(out_ansi, out_opt, size);
|
||||
if (ret == false) {
|
||||
printf(ANSI_COLOR_RED"%s failed\n"ANSI_COLOR_RESET, __FUNCTION__);
|
||||
printf("Output: \n");
|
||||
PRINT_ARRAY_HEX(out_opt, width, height);
|
||||
printf("Expected: \n");
|
||||
PRINT_ARRAY_HEX(out_ansi, width, height);
|
||||
printf("Input:\n");
|
||||
PRINT_ARRAY_HEX(input, width, height);
|
||||
goto softmax_s8_cleanup;
|
||||
}
|
||||
printf(ANSI_COLOR_GREEN"%s passed\n"ANSI_COLOR_RESET, __FUNCTION__);
|
||||
|
||||
softmax_s8_cleanup:
|
||||
if (input) {
|
||||
free (input);
|
||||
}
|
||||
if (out_ansi) {
|
||||
free (out_ansi);
|
||||
}
|
||||
if (out_opt) {
|
||||
free (out_opt);
|
||||
}
|
||||
if (scratch_buf) {
|
||||
free (scratch_buf);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user