softmax_test.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2022 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_softmax_s8_test()
  22. {
  23. const int32_t height = 8;
  24. const int32_t width = 32;
  25. const int32_t diff_min = -128;
  26. const int32_t mult = INT32_MAX / 2;
  27. const int32_t shift = 7;
  28. void *scratch_buf = NULL;
  29. const int size = width * height;
  30. int8_t *input, *out_ansi, *out_opt;
  31. input = memalign(16, size);
  32. out_ansi = memalign(16, size);
  33. out_opt = memalign(16, size);
  34. if (input == NULL || out_ansi == NULL || out_opt == NULL) {
  35. printf(ANSI_COLOR_RED"%s buffer allocations failed\n"ANSI_COLOR_RESET, __FUNCTION__);
  36. goto softmax_s8_cleanup;
  37. }
  38. /* Generate input data between -128 -> +127 */
  39. for (int i = 0; i < size; ++i) {
  40. input[i] = rand() % 255 - 128;
  41. }
  42. /* enable profiler */
  43. profile_c_start();
  44. /* C function */
  45. esp_nn_softmax_s8_ansi(input, height, width, mult, shift, diff_min, out_ansi);
  46. profile_c_end();
  47. int32_t scratch_buf_size = esp_nn_get_softmax_scratch_size(width, height);
  48. if (scratch_buf_size) {
  49. scratch_buf = memalign(4, scratch_buf_size);
  50. if (scratch_buf == NULL) {
  51. printf(ANSI_COLOR_RED"%s scratch_buf alloc failed size %d\n"ANSI_COLOR_RESET, __FUNCTION__, scratch_buf_size);
  52. goto softmax_s8_cleanup;
  53. }
  54. esp_nn_set_softmax_scratch_buf(scratch_buf);
  55. }
  56. profile_opt_start();
  57. /* Optimized function */
  58. esp_nn_softmax_s8(input, height, width, mult, shift, diff_min, out_opt);
  59. /* disable profiler */
  60. profile_opt_end();
  61. bool ret = CHECK_EQUAL(out_ansi, out_opt, size);
  62. if (ret == false) {
  63. printf(ANSI_COLOR_RED"%s failed\n"ANSI_COLOR_RESET, __FUNCTION__);
  64. printf("Output: \n");
  65. PRINT_ARRAY_HEX(out_opt, width, height);
  66. printf("Expected: \n");
  67. PRINT_ARRAY_HEX(out_ansi, width, height);
  68. printf("Input:\n");
  69. PRINT_ARRAY_HEX(input, width, height);
  70. goto softmax_s8_cleanup;
  71. }
  72. printf(ANSI_COLOR_GREEN"%s passed\n"ANSI_COLOR_RESET, __FUNCTION__);
  73. softmax_s8_cleanup:
  74. if (input) {
  75. free (input);
  76. }
  77. if (out_ansi) {
  78. free (out_ansi);
  79. }
  80. if (out_opt) {
  81. free (out_opt);
  82. }
  83. if (scratch_buf) {
  84. free (scratch_buf);
  85. }
  86. }