xref: /aosp_15_r20/external/libaom/test/av1_nn_predict_test.cc (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1 /*
2  * Copyright (c) 2018, Alliance for Open Media. All rights reserved.
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include <tuple>
13 
14 #include "gtest/gtest.h"
15 
16 #include "aom/aom_integer.h"
17 #include "aom_ports/aom_timer.h"
18 #include "av1/encoder/ml.h"
19 #include "config/aom_config.h"
20 #include "config/aom_dsp_rtcd.h"
21 #include "config/av1_rtcd.h"
22 #include "test/util.h"
23 #include "test/register_state_check.h"
24 #include "test/acm_random.h"
25 
26 namespace {
27 typedef void (*NnPredict_Func)(const float *const input_nodes,
28                                const NN_CONFIG *const nn_config,
29                                int reduce_prec, float *const output);
30 
31 typedef std::tuple<const NnPredict_Func> NnPredictTestParam;
32 
33 const float epsilon = 1e-3f;  // Error threshold for functional equivalence
34 
35 class NnPredictTest : public ::testing::TestWithParam<NnPredictTestParam> {
36  public:
SetUp()37   void SetUp() override {
38     const int MAX_NODES2 = NN_MAX_NODES_PER_LAYER * NN_MAX_NODES_PER_LAYER;
39     // Allocate two massive buffers on the heap for edge weights and node bias
40     // Then set-up the double-dimension arrays pointing into the big buffers
41     weights_buf = (float *)aom_malloc(MAX_NODES2 * (NN_MAX_HIDDEN_LAYERS + 1) *
42                                       sizeof(*weights_buf));
43     bias_buf =
44         (float *)aom_malloc(NN_MAX_NODES_PER_LAYER *
45                             (NN_MAX_HIDDEN_LAYERS + 1) * sizeof(*bias_buf));
46     ASSERT_NE(weights_buf, nullptr);
47     ASSERT_NE(bias_buf, nullptr);
48     for (int i = 0; i < NN_MAX_HIDDEN_LAYERS + 1; i++) {
49       weights[i] = &weights_buf[i * MAX_NODES2];
50       bias[i] = &bias_buf[i * NN_MAX_NODES_PER_LAYER];
51     }
52     target_func_ = GET_PARAM(0);
53   }
TearDown()54   void TearDown() override {
55     aom_free(weights_buf);
56     aom_free(bias_buf);
57   }
58   void RunNnPredictTest(const NN_CONFIG *const shape);
59   void RunNnPredictSpeedTest(const NN_CONFIG *const shape, const int run_times);
60   void RunNnPredictTest_all(const NN_CONFIG *const shapes,
61                             const int num_shapes);
62   void RunNnPredictSpeedTest_all(const NN_CONFIG *const shapes,
63                                  const int num_shapes, const int run_times);
64 
65  private:
66   NnPredict_Func target_func_;
67   libaom_test::ACMRandom rng_;
68   float *weights[NN_MAX_HIDDEN_LAYERS + 1] = {};
69   float *bias[NN_MAX_HIDDEN_LAYERS + 1] = {};
70   float *weights_buf = nullptr, *bias_buf = nullptr;
71 };
72 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(NnPredictTest);
73 
RunNnPredictTest(const NN_CONFIG * const shape)74 void NnPredictTest::RunNnPredictTest(const NN_CONFIG *const shape) {
75   float inputs[NN_MAX_NODES_PER_LAYER] = { 0 };
76   float outputs_test[NN_MAX_NODES_PER_LAYER] = { 0 };
77   float outputs_ref[NN_MAX_NODES_PER_LAYER] = { 0 };
78 
79   NN_CONFIG nn_config;
80   memcpy(&nn_config, shape, sizeof(nn_config));
81 
82   char shape_str[32] = { 0 };
83   snprintf(shape_str, sizeof(shape_str), "%d", shape->num_inputs);
84   for (int layer = 0; layer < shape->num_hidden_layers; layer++)
85     snprintf(&shape_str[strlen(shape_str)],
86              sizeof(shape_str) - strlen(shape_str), "x%d",
87              shape->num_hidden_nodes[layer]);
88   snprintf(&shape_str[strlen(shape_str)], sizeof(shape_str) - strlen(shape_str),
89            "x%d", shape->num_outputs);
90 
91   for (int i = 0; i < NN_MAX_HIDDEN_LAYERS + 1; i++) {
92     nn_config.weights[i] = weights[i];
93     nn_config.bias[i] = bias[i];
94   }
95 
96   for (int iter = 0; iter < 10000 && !HasFatalFailure(); ++iter) {
97     for (int node = 0; node < shape->num_inputs; node++) {
98       inputs[node] = ((float)rng_.Rand31() - (1 << 30)) / (1u << 31);
99     }
100     for (int layer = 0; layer < shape->num_hidden_layers; layer++) {
101       for (int node = 0; node < NN_MAX_NODES_PER_LAYER; node++) {
102         bias[layer][node] = ((float)rng_.Rand31() - (1 << 30)) / (1u << 31);
103       }
104       for (int node = 0; node < NN_MAX_NODES_PER_LAYER * NN_MAX_NODES_PER_LAYER;
105            node++) {
106         weights[layer][node] = ((float)rng_.Rand31() - (1 << 30)) / (1u << 31);
107       }
108     }
109     // Now the outputs:
110     int layer = shape->num_hidden_layers;
111     for (int node = 0; node < NN_MAX_NODES_PER_LAYER; node++) {
112       bias[layer][node] = ((float)rng_.Rand31() - (1 << 30)) / (1u << 31);
113     }
114     for (int node = 0; node < NN_MAX_NODES_PER_LAYER * NN_MAX_NODES_PER_LAYER;
115          node++) {
116       weights[layer][node] = ((float)rng_.Rand31() - (1 << 30)) / (1u << 31);
117     }
118 
119     av1_nn_predict_c(inputs, &nn_config, 0, outputs_ref);
120     target_func_(inputs, &nn_config, 0, outputs_test);
121 
122     for (int node = 0; node < shape->num_outputs; node++) {
123       if (outputs_ref[node] < epsilon) {
124         ASSERT_LE(outputs_test[node], epsilon)
125             << "Reference output was near-zero, test output was not ("
126             << shape_str << ")";
127       } else {
128         const float error = outputs_ref[node] - outputs_test[node];
129         const float relative_error = fabsf(error / outputs_ref[node]);
130         ASSERT_LE(relative_error, epsilon)
131             << "Excessive relative error between reference and test ("
132             << shape_str << ")";
133       }
134     }
135   }
136 }
137 
RunNnPredictSpeedTest(const NN_CONFIG * const shape,const int run_times)138 void NnPredictTest::RunNnPredictSpeedTest(const NN_CONFIG *const shape,
139                                           const int run_times) {
140   float inputs[NN_MAX_NODES_PER_LAYER] = { 0 };
141   float outputs_test[NN_MAX_NODES_PER_LAYER] = { 0 };
142   float outputs_ref[NN_MAX_NODES_PER_LAYER] = { 0 };
143 
144   NN_CONFIG nn_config;
145   memcpy(&nn_config, shape, sizeof(nn_config));
146 
147   for (int i = 0; i < NN_MAX_HIDDEN_LAYERS; i++) {
148     nn_config.weights[i] = weights[i];
149     nn_config.bias[i] = bias[i];
150   }
151   // Don't bother actually changing the values for inputs/weights/bias: it
152   // shouldn't make any difference for a speed test.
153 
154   aom_usec_timer timer;
155   aom_usec_timer_start(&timer);
156   for (int i = 0; i < run_times; ++i) {
157     av1_nn_predict_c(inputs, &nn_config, 0, outputs_ref);
158   }
159   aom_usec_timer_mark(&timer);
160   const double time1 = static_cast<double>(aom_usec_timer_elapsed(&timer));
161   aom_usec_timer_start(&timer);
162   for (int i = 0; i < run_times; ++i) {
163     target_func_(inputs, &nn_config, 0, outputs_test);
164   }
165   aom_usec_timer_mark(&timer);
166   const double time2 = static_cast<double>(aom_usec_timer_elapsed(&timer));
167 
168   printf("%d", shape->num_inputs);
169   for (int layer = 0; layer < shape->num_hidden_layers; layer++)
170     printf("x%d", shape->num_hidden_nodes[layer]);
171   printf("x%d: ", shape->num_outputs);
172   printf("%7.2f/%7.2fns (%3.2f)\n", time1, time2, time1 / time2);
173 }
174 
175 // This is all the neural network shapes observed executed in a few different
176 // runs of the encoder.  It also conveniently covers all the kernels
177 // implemented.
178 static const NN_CONFIG kShapes[] = {
179   { 37, 1, 2, { 16, 24 }, {}, {} }, { 24, 24, 1, { 12 }, {}, {} },
180   { 10, 16, 1, { 64 }, {}, {} },    { 12, 1, 1, { 12 }, {}, {} },
181   { 12, 1, 1, { 24 }, {}, {} },     { 12, 1, 1, { 32 }, {}, {} },
182   { 18, 4, 1, { 24 }, {}, {} },     { 18, 4, 1, { 32 }, {}, {} },
183   { 4, 1, 1, { 16 }, {}, {} },      { 8, 1, 0, { 0 }, {}, {} },
184   { 8, 4, 1, { 16 }, {}, {} },      { 8, 1, 1, { 32 }, {}, {} },
185   { 9, 3, 1, { 32 }, {}, {} },      { 8, 4, 0, { 0 }, {}, {} },
186   { 8, 8, 0, { 0 }, {}, {} },       { 4, 4, 1, { 8 }, {}, {} },
187   { 4, 3, 0, { 64 }, {}, {} },
188 };
189 
RunNnPredictTest_all(const NN_CONFIG * const shapes,const int num_shapes)190 void NnPredictTest::RunNnPredictTest_all(const NN_CONFIG *const shapes,
191                                          const int num_shapes) {
192   for (int i = 0; i < num_shapes; i++) RunNnPredictTest(&shapes[i]);
193 }
194 
RunNnPredictSpeedTest_all(const NN_CONFIG * const shapes,const int num_shapes,const int run_times)195 void NnPredictTest::RunNnPredictSpeedTest_all(const NN_CONFIG *const shapes,
196                                               const int num_shapes,
197                                               const int run_times) {
198   for (int i = 0; i < num_shapes; i++)
199     NnPredictTest::RunNnPredictSpeedTest(&shapes[i], run_times);
200 }
201 
TEST_P(NnPredictTest,RandomValues)202 TEST_P(NnPredictTest, RandomValues) {
203   RunNnPredictTest_all(kShapes, sizeof(kShapes) / sizeof(kShapes[0]));
204 }
205 
TEST_P(NnPredictTest,DISABLED_Speed)206 TEST_P(NnPredictTest, DISABLED_Speed) {
207   RunNnPredictSpeedTest_all(kShapes, sizeof(kShapes) / sizeof(kShapes[0]),
208                             10000000);
209 }
210 
211 #if !CONFIG_EXCLUDE_SIMD_MISMATCH
212 #if HAVE_SSE3
213 INSTANTIATE_TEST_SUITE_P(SSE3, NnPredictTest,
214                          ::testing::Values(av1_nn_predict_sse3));
215 #endif
216 
217 #if HAVE_AVX2
218 INSTANTIATE_TEST_SUITE_P(AVX2, NnPredictTest,
219                          ::testing::Values(av1_nn_predict_avx2));
220 #endif
221 
222 #if HAVE_NEON
223 INSTANTIATE_TEST_SUITE_P(NEON, NnPredictTest,
224                          ::testing::Values(av1_nn_predict_neon));
225 #endif
226 #endif  // !CONFIG_EXCLUDE_SIMD_MISMATCH
227 
228 }  // namespace
229