Marat Dukhan | 9757953 | 2019-10-18 16:40:39 -0700 | [diff] [blame] | 1 | // Copyright 2019 Google LLC |
| 2 | // |
| 3 | // This source code is licensed under the BSD-style license found in the |
| 4 | // LICENSE file in the root directory of this source tree. |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include <gtest/gtest.h> |
| 9 | |
| 10 | #include <algorithm> |
| 11 | #include <cassert> |
| 12 | #include <cstddef> |
| 13 | #include <cstdlib> |
| 14 | #include <functional> |
| 15 | #include <random> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include <xnnpack.h> |
| 19 | #include <xnnpack/params.h> |
Marat Dukhan | 4a5c771 | 2022-01-05 22:43:13 -0800 | [diff] [blame] | 20 | #include <xnnpack/params-init.h> |
Marat Dukhan | 9757953 | 2019-10-18 16:40:39 -0700 | [diff] [blame] | 21 | |
| 22 | |
| 23 | class RAddStoreExpMinusMaxMicrokernelTester { |
| 24 | public: |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 25 | inline RAddStoreExpMinusMaxMicrokernelTester& elements(size_t elements) { |
| 26 | assert(elements != 0); |
| 27 | this->elements_ = elements; |
Marat Dukhan | 9757953 | 2019-10-18 16:40:39 -0700 | [diff] [blame] | 28 | return *this; |
| 29 | } |
| 30 | |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 31 | inline size_t elements() const { |
| 32 | return this->elements_; |
Marat Dukhan | 9757953 | 2019-10-18 16:40:39 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | inline RAddStoreExpMinusMaxMicrokernelTester& iterations(size_t iterations) { |
| 36 | this->iterations_ = iterations; |
| 37 | return *this; |
| 38 | } |
| 39 | |
| 40 | inline size_t iterations() const { |
| 41 | return this->iterations_; |
| 42 | } |
| 43 | |
Marat Dukhan | 4a5c771 | 2022-01-05 22:43:13 -0800 | [diff] [blame] | 44 | void Test(xnn_f32_raddstoreexpminusmax_ukernel_function raddstoreexpminusmax, xnn_init_f32_expminus_params_fn init_params) const { |
Marat Dukhan | 9757953 | 2019-10-18 16:40:39 -0700 | [diff] [blame] | 45 | std::random_device random_device; |
| 46 | auto rng = std::mt19937(random_device()); |
| 47 | // Choose such range that expf(x[i]) overflows, but expf(x[i] - x_max) doesn't. |
| 48 | // However, the range is still narrow enough that double-precision exp doesn't overflow. |
| 49 | auto f32rng = std::bind(std::uniform_real_distribution<float>(90.0f, 100.0f), rng); |
| 50 | |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 51 | std::vector<float> x(elements() + XNN_EXTRA_BYTES / sizeof(float)); |
| 52 | std::vector<float> y(elements()); |
| 53 | std::vector<double> y_ref(elements()); |
Marat Dukhan | 9757953 | 2019-10-18 16:40:39 -0700 | [diff] [blame] | 54 | for (size_t iteration = 0; iteration < iterations(); iteration++) { |
| 55 | std::generate(x.begin(), x.end(), std::ref(f32rng)); |
| 56 | std::fill(y.begin(), y.end(), std::nanf("")); |
| 57 | |
| 58 | // Compute reference results. |
| 59 | double sum_ref = 0.0f; |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 60 | const float x_max = *std::max_element(x.begin(), x.begin() + elements()); |
| 61 | for (size_t i = 0; i < elements(); i++) { |
Marat Dukhan | 9757953 | 2019-10-18 16:40:39 -0700 | [diff] [blame] | 62 | const double y_ref_value = exp(double(x[i]) - double(x_max)); |
| 63 | y_ref[i] = y_ref_value; |
| 64 | sum_ref += y_ref_value; |
| 65 | } |
| 66 | |
| 67 | // Call optimized micro-kernel. |
| 68 | float sum = std::nanf(""); |
Marat Dukhan | 4a5c771 | 2022-01-05 22:43:13 -0800 | [diff] [blame] | 69 | xnn_f32_expminus_params params; |
| 70 | init_params(¶ms); |
| 71 | raddstoreexpminusmax(elements() * sizeof(float), x.data(), &x_max, y.data(), &sum, ¶ms); |
Marat Dukhan | 9757953 | 2019-10-18 16:40:39 -0700 | [diff] [blame] | 72 | |
| 73 | // Verify results. |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 74 | for (size_t i = 0; i < elements(); i++) { |
Marat Dukhan | 9757953 | 2019-10-18 16:40:39 -0700 | [diff] [blame] | 75 | ASSERT_NEAR(y_ref[i], double(y[i]), std::abs(y_ref[i]) * 1.0e-6) |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 76 | << "i = " << i << ", elements = " << elements() << ", x_max = " << x_max; |
Marat Dukhan | 9757953 | 2019-10-18 16:40:39 -0700 | [diff] [blame] | 77 | } |
| 78 | ASSERT_NEAR(sum_ref, double(sum), std::abs(sum_ref) * 1.0e-6) |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 79 | << "elements = " << elements() << ", x_max = " << x_max; |
Marat Dukhan | 9757953 | 2019-10-18 16:40:39 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | private: |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 84 | size_t elements_{1}; |
Marat Dukhan | 9757953 | 2019-10-18 16:40:39 -0700 | [diff] [blame] | 85 | size_t iterations_{15}; |
| 86 | }; |