relu_test.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2020-2021 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <malloc.h>
  19. #include <esp_nn.h>
  20. #include "test_utils.h"
  21. void esp_nn_relu6_s8_test()
  22. {
  23. const int size = 1600 + 8 + 7;
  24. int8_t *input, *inout_ansi, *inout_opt;
  25. input = memalign(16, size);
  26. inout_ansi = memalign(16, size);
  27. inout_opt = memalign(16, size);
  28. if (input == NULL || inout_ansi == NULL || inout_opt == NULL) {
  29. printf(ANSI_COLOR_RED"%s allocations failed\n"ANSI_COLOR_RESET, __FUNCTION__);
  30. goto relu6_s8_cleanup;
  31. }
  32. /* Generate filter data between -128 -> +127 */
  33. for (int i = 0; i < size; ++i) {
  34. input[i] = rand() % 255 - 128;
  35. inout_ansi[i] = input[i];
  36. inout_opt[i] = input[i];
  37. }
  38. /* enable profiler */
  39. profile_c_start();
  40. /* C function */
  41. esp_nn_relu6_s8_ansi(inout_ansi, size);
  42. profile_c_end();
  43. profile_opt_start();
  44. /* Optimized function */
  45. esp_nn_relu6_s8(inout_opt, size);
  46. /* disable profiler */
  47. profile_opt_end();
  48. bool ret = CHECK_EQUAL(inout_ansi, inout_opt, size);
  49. if (ret == false) {
  50. printf(ANSI_COLOR_RED"%s failed\n"ANSI_COLOR_RESET, __FUNCTION__);
  51. printf("Output: \n");
  52. PRINT_ARRAY_HEX(inout_opt, size, 1);
  53. printf("Expected: \n");
  54. PRINT_ARRAY_HEX(inout_ansi, size, 1);
  55. printf("Input:\n");
  56. PRINT_ARRAY_HEX(input, size, 1);
  57. goto relu6_s8_cleanup;
  58. }
  59. printf(ANSI_COLOR_GREEN"%s passed\n"ANSI_COLOR_RESET, __FUNCTION__);
  60. relu6_s8_cleanup:
  61. if (input) {
  62. free (input);
  63. }
  64. if (inout_ansi) {
  65. free (inout_ansi);
  66. }
  67. if (inout_opt) {
  68. free (inout_opt);
  69. }
  70. }