xref: /aosp_15_r20/external/libaom/test/av1_softmax_test.cc (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2021, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #include <memory>
13*77c1e3ccSAndroid Build Coastguard Worker #include <new>
14*77c1e3ccSAndroid Build Coastguard Worker #include <tuple>
15*77c1e3ccSAndroid Build Coastguard Worker 
16*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_integer.h"
17*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/aom_timer.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/ml.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
20*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_dsp_rtcd.h"
21*77c1e3ccSAndroid Build Coastguard Worker #include "config/av1_rtcd.h"
22*77c1e3ccSAndroid Build Coastguard Worker #include "gtest/gtest.h"
23*77c1e3ccSAndroid Build Coastguard Worker #include "test/acm_random.h"
24*77c1e3ccSAndroid Build Coastguard Worker #include "test/register_state_check.h"
25*77c1e3ccSAndroid Build Coastguard Worker #include "test/util.h"
26*77c1e3ccSAndroid Build Coastguard Worker 
27*77c1e3ccSAndroid Build Coastguard Worker namespace {
28*77c1e3ccSAndroid Build Coastguard Worker using FastSoftmaxFn = void (*)(const float *const input, float *output);
29*77c1e3ccSAndroid Build Coastguard Worker using FastSoftmaxTestParams = std::tuple<const FastSoftmaxFn, int>;
30*77c1e3ccSAndroid Build Coastguard Worker 
31*77c1e3ccSAndroid Build Coastguard Worker // Error thresholds for functional equivalence
32*77c1e3ccSAndroid Build Coastguard Worker constexpr float kRelEpsilon = 5e-2f;
33*77c1e3ccSAndroid Build Coastguard Worker constexpr float kAbsEpsilon = 5e-3f;
34*77c1e3ccSAndroid Build Coastguard Worker 
35*77c1e3ccSAndroid Build Coastguard Worker class FastSoftmaxTest : public ::testing::TestWithParam<FastSoftmaxTestParams> {
36*77c1e3ccSAndroid Build Coastguard Worker  public:
FastSoftmaxTest()37*77c1e3ccSAndroid Build Coastguard Worker   FastSoftmaxTest() : target_fn_(GET_PARAM(0)), num_classes_(GET_PARAM(1)) {}
SetUp()38*77c1e3ccSAndroid Build Coastguard Worker   void SetUp() override {
39*77c1e3ccSAndroid Build Coastguard Worker     ref_buf_.reset(new (std::nothrow) float[num_classes_]());
40*77c1e3ccSAndroid Build Coastguard Worker     ASSERT_NE(ref_buf_, nullptr);
41*77c1e3ccSAndroid Build Coastguard Worker     dst_buf_.reset(new (std::nothrow) float[num_classes_]());
42*77c1e3ccSAndroid Build Coastguard Worker     ASSERT_NE(dst_buf_, nullptr);
43*77c1e3ccSAndroid Build Coastguard Worker     input_.reset(new (std::nothrow) float[num_classes_]());
44*77c1e3ccSAndroid Build Coastguard Worker     ASSERT_NE(input_, nullptr);
45*77c1e3ccSAndroid Build Coastguard Worker   }
46*77c1e3ccSAndroid Build Coastguard Worker   void RunSoftmaxTest();
47*77c1e3ccSAndroid Build Coastguard Worker   void RunSoftmaxSpeedTest(const int run_times);
48*77c1e3ccSAndroid Build Coastguard Worker   void FillInputBuf();
49*77c1e3ccSAndroid Build Coastguard Worker 
50*77c1e3ccSAndroid Build Coastguard Worker  private:
51*77c1e3ccSAndroid Build Coastguard Worker   const FastSoftmaxFn target_fn_;
52*77c1e3ccSAndroid Build Coastguard Worker   const int num_classes_;
53*77c1e3ccSAndroid Build Coastguard Worker   std::unique_ptr<float[]> ref_buf_, dst_buf_, input_;
54*77c1e3ccSAndroid Build Coastguard Worker   libaom_test::ACMRandom rng_;
55*77c1e3ccSAndroid Build Coastguard Worker };
56*77c1e3ccSAndroid Build Coastguard Worker 
FillInputBuf()57*77c1e3ccSAndroid Build Coastguard Worker void FastSoftmaxTest::FillInputBuf() {
58*77c1e3ccSAndroid Build Coastguard Worker   for (int idx = 0; idx < num_classes_; idx++) {
59*77c1e3ccSAndroid Build Coastguard Worker     input_[idx] = ((float)rng_.Rand31() - (1 << 30)) / (1u << 30);
60*77c1e3ccSAndroid Build Coastguard Worker   }
61*77c1e3ccSAndroid Build Coastguard Worker }
62*77c1e3ccSAndroid Build Coastguard Worker 
RunSoftmaxTest()63*77c1e3ccSAndroid Build Coastguard Worker void FastSoftmaxTest::RunSoftmaxTest() {
64*77c1e3ccSAndroid Build Coastguard Worker   av1_nn_softmax(input_.get(), ref_buf_.get(), num_classes_);
65*77c1e3ccSAndroid Build Coastguard Worker   target_fn_(input_.get(), dst_buf_.get());
66*77c1e3ccSAndroid Build Coastguard Worker 
67*77c1e3ccSAndroid Build Coastguard Worker   for (int idx = 0; idx < num_classes_; idx++) {
68*77c1e3ccSAndroid Build Coastguard Worker     if (ref_buf_[idx] < kAbsEpsilon) {
69*77c1e3ccSAndroid Build Coastguard Worker       ASSERT_LE(dst_buf_[idx], kAbsEpsilon)
70*77c1e3ccSAndroid Build Coastguard Worker           << "Reference output was near-zero, test output was not" << std::endl;
71*77c1e3ccSAndroid Build Coastguard Worker     } else {
72*77c1e3ccSAndroid Build Coastguard Worker       const float error = dst_buf_[idx] - ref_buf_[idx];
73*77c1e3ccSAndroid Build Coastguard Worker       const float relative_error = fabsf(error / ref_buf_[idx]);
74*77c1e3ccSAndroid Build Coastguard Worker       ASSERT_LE(relative_error, kRelEpsilon)
75*77c1e3ccSAndroid Build Coastguard Worker           << "Excessive relative error between reference and test output"
76*77c1e3ccSAndroid Build Coastguard Worker           << std::endl;
77*77c1e3ccSAndroid Build Coastguard Worker       ASSERT_LE(error, kAbsEpsilon)
78*77c1e3ccSAndroid Build Coastguard Worker           << "Excessive absolute error between reference and test output"
79*77c1e3ccSAndroid Build Coastguard Worker           << std::endl;
80*77c1e3ccSAndroid Build Coastguard Worker     }
81*77c1e3ccSAndroid Build Coastguard Worker   }
82*77c1e3ccSAndroid Build Coastguard Worker }
83*77c1e3ccSAndroid Build Coastguard Worker 
RunSoftmaxSpeedTest(const int run_times)84*77c1e3ccSAndroid Build Coastguard Worker void FastSoftmaxTest::RunSoftmaxSpeedTest(const int run_times) {
85*77c1e3ccSAndroid Build Coastguard Worker   aom_usec_timer timer;
86*77c1e3ccSAndroid Build Coastguard Worker   aom_usec_timer_start(&timer);
87*77c1e3ccSAndroid Build Coastguard Worker   for (int idx = 0; idx < run_times; idx++) {
88*77c1e3ccSAndroid Build Coastguard Worker     target_fn_(input_.get(), dst_buf_.get());
89*77c1e3ccSAndroid Build Coastguard Worker   }
90*77c1e3ccSAndroid Build Coastguard Worker   aom_usec_timer_mark(&timer);
91*77c1e3ccSAndroid Build Coastguard Worker   const int64_t time = aom_usec_timer_elapsed(&timer);
92*77c1e3ccSAndroid Build Coastguard Worker   std::cout << "Test with " << num_classes_ << " classes took " << time
93*77c1e3ccSAndroid Build Coastguard Worker             << " us." << std::endl;
94*77c1e3ccSAndroid Build Coastguard Worker }
95*77c1e3ccSAndroid Build Coastguard Worker 
TEST_P(FastSoftmaxTest,RandomValues)96*77c1e3ccSAndroid Build Coastguard Worker TEST_P(FastSoftmaxTest, RandomValues) {
97*77c1e3ccSAndroid Build Coastguard Worker   FillInputBuf();
98*77c1e3ccSAndroid Build Coastguard Worker   RunSoftmaxTest();
99*77c1e3ccSAndroid Build Coastguard Worker }
100*77c1e3ccSAndroid Build Coastguard Worker 
TEST_P(FastSoftmaxTest,DISABLED_Speed)101*77c1e3ccSAndroid Build Coastguard Worker TEST_P(FastSoftmaxTest, DISABLED_Speed) {
102*77c1e3ccSAndroid Build Coastguard Worker   constexpr int kNumTimes = 1000000;
103*77c1e3ccSAndroid Build Coastguard Worker   RunSoftmaxSpeedTest(kNumTimes);
104*77c1e3ccSAndroid Build Coastguard Worker }
105*77c1e3ccSAndroid Build Coastguard Worker 
AnchorSoftmax16Fn(const float * input,float * output)106*77c1e3ccSAndroid Build Coastguard Worker void AnchorSoftmax16Fn(const float *input, float *output) {
107*77c1e3ccSAndroid Build Coastguard Worker   av1_nn_softmax(input, output, 16);
108*77c1e3ccSAndroid Build Coastguard Worker }
109*77c1e3ccSAndroid Build Coastguard Worker 
110*77c1e3ccSAndroid Build Coastguard Worker const FastSoftmaxTestParams kArrayParams_c[] = {
111*77c1e3ccSAndroid Build Coastguard Worker   FastSoftmaxTestParams(AnchorSoftmax16Fn, 16),
112*77c1e3ccSAndroid Build Coastguard Worker   FastSoftmaxTestParams(av1_nn_fast_softmax_16_c, 16)
113*77c1e3ccSAndroid Build Coastguard Worker };
114*77c1e3ccSAndroid Build Coastguard Worker INSTANTIATE_TEST_SUITE_P(C, FastSoftmaxTest,
115*77c1e3ccSAndroid Build Coastguard Worker                          ::testing::ValuesIn(kArrayParams_c));
116*77c1e3ccSAndroid Build Coastguard Worker 
117*77c1e3ccSAndroid Build Coastguard Worker #if HAVE_SSE3 && !CONFIG_EXCLUDE_SIMD_MISMATCH
118*77c1e3ccSAndroid Build Coastguard Worker INSTANTIATE_TEST_SUITE_P(
119*77c1e3ccSAndroid Build Coastguard Worker     SSE3, FastSoftmaxTest,
120*77c1e3ccSAndroid Build Coastguard Worker     ::testing::Values(FastSoftmaxTestParams(av1_nn_fast_softmax_16_sse3, 16)));
121*77c1e3ccSAndroid Build Coastguard Worker #endif
122*77c1e3ccSAndroid Build Coastguard Worker }  // namespace
123