blob: eef7698263f54d5736de65c054c057e737495b3d [file] [log] [blame]
XNNPACK Teamb455b122019-09-27 18:10:33 -07001// 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>
Marat Dukhaneeaa7bd2019-10-25 17:31:25 -070019#include <xnnpack/params-init.h>
Frank Barcharde0601b52019-10-25 17:43:34 -070020#include <xnnpack/params.h>
XNNPACK Teamb455b122019-09-27 18:10:33 -070021
22
23class HSwishMicrokernelTester {
24 public:
25 enum class Variant {
26 Native,
27 Scalar,
28 };
29
Marat Dukhan662faa02019-12-09 22:48:16 -080030 inline HSwishMicrokernelTester& batch_size(size_t batch_size) {
31 assert(batch_size != 0);
32 this->batch_size_ = batch_size;
XNNPACK Teamb455b122019-09-27 18:10:33 -070033 return *this;
34 }
35
Marat Dukhan662faa02019-12-09 22:48:16 -080036 inline size_t batch_size() const {
37 return this->batch_size_;
XNNPACK Teamb455b122019-09-27 18:10:33 -070038 }
39
40 inline HSwishMicrokernelTester& inplace(bool inplace) {
41 this->inplace_ = inplace;
42 return *this;
43 }
44
45 inline bool inplace() const {
46 return this->inplace_;
47 }
48
49 inline HSwishMicrokernelTester& iterations(size_t iterations) {
50 this->iterations_ = iterations;
51 return *this;
52 }
53
54 inline size_t iterations() const {
55 return this->iterations_;
56 }
57
58 void Test(xnn_f32_hswish_ukernel_function hswish, Variant variant = Variant::Native) const {
59 std::random_device random_device;
60 auto rng = std::mt19937(random_device());
61 auto f32rng = std::bind(std::uniform_real_distribution<float>(-1.0f, 1.0f), rng);
62
Marat Dukhan662faa02019-12-09 22:48:16 -080063 std::vector<float> x(batch_size() + XNN_EXTRA_BYTES / sizeof(float));
64 std::vector<float> y(batch_size() + (inplace() ? XNN_EXTRA_BYTES / sizeof(float) : 0));
65 std::vector<float> y_ref(batch_size());
XNNPACK Teamb455b122019-09-27 18:10:33 -070066 for (size_t iteration = 0; iteration < iterations(); iteration++) {
67 std::generate(x.begin(), x.end(), std::ref(f32rng));
68 if (inplace()) {
69 std::generate(y.begin(), y.end(), std::ref(f32rng));
70 } else {
71 std::fill(y.begin(), y.end(), std::nanf(""));
72 }
73 const float* x_data = inplace() ? y.data() : x.data();
74
75 // Prepare micro-kernel parameters.
76 union xnn_f32_hswish_params params = { };
77 switch (variant) {
78 case Variant::Native:
Marat Dukhaneeaa7bd2019-10-25 17:31:25 -070079 params = xnn_init_f32_hswish_params();
XNNPACK Teamb455b122019-09-27 18:10:33 -070080 break;
81 case Variant::Scalar:
Marat Dukhaneeaa7bd2019-10-25 17:31:25 -070082 params = xnn_init_scalar_f32_hswish_params();
XNNPACK Teamb455b122019-09-27 18:10:33 -070083 break;
84 }
85
86 // Compute reference results.
Marat Dukhan662faa02019-12-09 22:48:16 -080087 for (size_t i = 0; i < batch_size(); i++) {
XNNPACK Teamb455b122019-09-27 18:10:33 -070088 y_ref[i] = x_data[i] * std::max(std::min(x_data[i] + 3.0f, 6.0f), 0.0f) / 6.0f;
89 }
90
91 // Call optimized micro-kernel.
Marat Dukhan662faa02019-12-09 22:48:16 -080092 hswish(batch_size() * sizeof(float), x_data, y.data(), &params);
XNNPACK Teamb455b122019-09-27 18:10:33 -070093
94 // Verify results.
Marat Dukhan662faa02019-12-09 22:48:16 -080095 for (size_t i = 0; i < batch_size(); i++) {
XNNPACK Teamb455b122019-09-27 18:10:33 -070096 ASSERT_NEAR(y_ref[i], y[i], std::abs(y_ref[i]) * 1.0e-6f)
Marat Dukhan662faa02019-12-09 22:48:16 -080097 << "at position " << i << ", batch_size = " << batch_size();
XNNPACK Teamb455b122019-09-27 18:10:33 -070098 }
99 }
100 }
101
102 private:
Marat Dukhan662faa02019-12-09 22:48:16 -0800103 size_t batch_size_{1};
XNNPACK Teamb455b122019-09-27 18:10:33 -0700104 bool inplace_{false};
Marat Dukhan662faa02019-12-09 22:48:16 -0800105 size_t iterations_{5};
XNNPACK Teamb455b122019-09-27 18:10:33 -0700106};