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> |
| 20 | |
| 21 | |
| 22 | class RAddExpMinusMaxMicrokernelTester { |
| 23 | public: |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 24 | inline RAddExpMinusMaxMicrokernelTester& elements(size_t elements) { |
| 25 | assert(elements != 0); |
| 26 | this->elements_ = elements; |
Marat Dukhan | 9757953 | 2019-10-18 16:40:39 -0700 | [diff] [blame] | 27 | return *this; |
| 28 | } |
| 29 | |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 30 | inline size_t elements() const { |
| 31 | return this->elements_; |
Marat Dukhan | 9757953 | 2019-10-18 16:40:39 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | inline RAddExpMinusMaxMicrokernelTester& iterations(size_t iterations) { |
| 35 | this->iterations_ = iterations; |
| 36 | return *this; |
| 37 | } |
| 38 | |
| 39 | inline size_t iterations() const { |
| 40 | return this->iterations_; |
| 41 | } |
| 42 | |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 43 | void Test(xnn_f32_raddexpminusmax_ukernel_function raddexpminusmax) const { |
Marat Dukhan | 9757953 | 2019-10-18 16:40:39 -0700 | [diff] [blame] | 44 | std::random_device random_device; |
| 45 | auto rng = std::mt19937(random_device()); |
| 46 | // Choose such range that expf(x[i]) overflows, but expf(x[i] - x_max) doesn't. |
| 47 | // However, the range is still narrow enough that double-precision exp doesn't overflow. |
| 48 | auto f32rng = std::bind(std::uniform_real_distribution<float>(90.0f, 100.0f), rng); |
| 49 | |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 50 | std::vector<float> x(elements() + XNN_EXTRA_BYTES / sizeof(float)); |
Marat Dukhan | 9757953 | 2019-10-18 16:40:39 -0700 | [diff] [blame] | 51 | for (size_t iteration = 0; iteration < iterations(); iteration++) { |
| 52 | std::generate(x.begin(), x.end(), std::ref(f32rng)); |
| 53 | |
| 54 | // Compute reference results. |
| 55 | double sum_ref = 0.0f; |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 56 | const float x_max = *std::max_element(x.begin(), x.begin() + elements()); |
| 57 | for (size_t i = 0; i < elements(); i++) { |
Marat Dukhan | 9757953 | 2019-10-18 16:40:39 -0700 | [diff] [blame] | 58 | sum_ref += exp(x[i] - x_max); |
| 59 | } |
| 60 | |
| 61 | // Call optimized micro-kernel. |
| 62 | float sum = std::nanf(""); |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 63 | raddexpminusmax(elements() * sizeof(float), x.data(), &sum, x_max); |
Marat Dukhan | 9757953 | 2019-10-18 16:40:39 -0700 | [diff] [blame] | 64 | |
| 65 | // Verify results. |
| 66 | 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] | 67 | << "elements = " << elements() << ", x_max = " << x_max; |
Marat Dukhan | 9757953 | 2019-10-18 16:40:39 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| 71 | private: |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 72 | size_t elements_{1}; |
Marat Dukhan | 9757953 | 2019-10-18 16:40:39 -0700 | [diff] [blame] | 73 | size_t iterations_{15}; |
| 74 | }; |