Bug 1882465 - Update .hg-annotate-ignore-revs and .git-blame-ignore-revs to reflect...
[gecko.git] / third_party / aom / test / av1_nn_predict_test.cc
blob4201ea6ce61266ac4fb21fe53c8c478c168b2461
1 /*
2 * Copyright (c) 2018, Alliance for Open Media. All rights reserved
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.
12 #include <tuple>
14 #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
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"
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);
31 typedef std::tuple<const NnPredict_Func> NnPredictTestParam;
33 const float epsilon = 1e-3f; // Error threshold for functional equivalence
35 class NnPredictTest : public ::testing::TestWithParam<NnPredictTestParam> {
36 public:
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];
52 target_func_ = GET_PARAM(0);
54 void TearDown() override {
55 aom_free(weights_buf);
56 aom_free(bias_buf);
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);
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;
72 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(NnPredictTest);
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 };
79 NN_CONFIG nn_config;
80 memcpy(&nn_config, shape, sizeof(nn_config));
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);
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];
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);
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);
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);
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);
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);
119 av1_nn_predict_c(inputs, &nn_config, 0, outputs_ref);
120 target_func_(inputs, &nn_config, 0, outputs_test);
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 << ")";
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 };
144 NN_CONFIG nn_config;
145 memcpy(&nn_config, shape, sizeof(nn_config));
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];
151 // Don't bother actually changing the values for inputs/weights/bias: it
152 // shouldn't make any difference for a speed test.
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);
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);
165 aom_usec_timer_mark(&timer);
166 const double time2 = static_cast<double>(aom_usec_timer_elapsed(&timer));
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);
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 }, {}, {} },
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]);
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);
202 TEST_P(NnPredictTest, RandomValues) {
203 RunNnPredictTest_all(kShapes, sizeof(kShapes) / sizeof(kShapes[0]));
206 TEST_P(NnPredictTest, DISABLED_Speed) {
207 RunNnPredictSpeedTest_all(kShapes, sizeof(kShapes) / sizeof(kShapes[0]),
208 10000000);
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
217 #if HAVE_AVX2
218 INSTANTIATE_TEST_SUITE_P(AVX2, NnPredictTest,
219 ::testing::Values(av1_nn_predict_avx2));
220 #endif
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
228 } // namespace