blob: 729f2ebc3ededdeeecfd5fcf6b5f2d4079cd9dd7 [file] [log] [blame]
XNNPACK Teamb455b122019-09-27 18:10:33 -07001// Copyright (c) Facebook, Inc. and its affiliates.
2// All rights reserved.
3//
4// Copyright 2019 Google LLC
5//
6// This source code is licensed under the BSD-style license found in the
7// LICENSE file in the root directory of this source tree.
8
9#pragma once
10
11#include <gtest/gtest.h>
12
13#include <algorithm>
14#include <cassert>
15#include <cstddef>
16#include <cstdlib>
17#include <functional>
18#include <random>
19#include <vector>
20
21#include <xnnpack/params.h>
22
23
24class RMaxMicrokernelTester {
25 public:
26 inline RMaxMicrokernelTester& n(size_t n) {
27 assert(n != 0);
28 this->n_ = n;
29 return *this;
30 }
31
32 inline size_t n() const {
33 return this->n_;
34 }
35
36 inline RMaxMicrokernelTester& iterations(size_t iterations) {
37 this->iterations_ = iterations;
38 return *this;
39 }
40
41 inline size_t iterations() const {
42 return this->iterations_;
43 }
44
45 void Test(xnn_u8_rmax_ukernel_function rmax) const {
46 std::random_device random_device;
47 auto rng = std::mt19937(random_device());
48 auto u8rng = std::bind(std::uniform_int_distribution<uint8_t>(), rng);
49
50 std::vector<uint8_t> x(n());
51 for (size_t iteration = 0; iteration < iterations(); iteration++) {
52 std::generate(x.begin(), x.end(), std::ref(u8rng));
53
54 // Compute reference results.
55 uint8_t y_ref = 0;
56 for (size_t i = 0; i < n(); i++) {
57 y_ref = std::max(y_ref, x[i]);
58 }
59
60 // Call optimized micro-kernel.
61 uint8_t y = u8rng();
62 rmax(n() * sizeof(uint8_t), x.data(), &y);
63
64 // Verify results.
65 ASSERT_EQ(y_ref, y) << "n = " << n();
66 }
67 }
68
69 void Test(xnn_f32_rmax_ukernel_function rmax) const {
70 std::random_device random_device;
71 auto rng = std::mt19937(random_device());
72 auto f32rng = std::bind(std::uniform_real_distribution<float>(), rng);
73
74 std::vector<float> x(n());
75 for (size_t iteration = 0; iteration < iterations(); iteration++) {
76 std::generate(x.begin(), x.end(), std::ref(f32rng));
77
78 // Compute reference results.
79 float y_ref = 0;
80 for (size_t i = 0; i < n(); i++) {
81 y_ref = std::max(y_ref, x[i]);
82 }
83
84 // Call optimized micro-kernel.
85 float y = std::nanf("");
86 rmax(n() * sizeof(float), x.data(), &y);
87
88 // Verify results.
89 ASSERT_EQ(y_ref, y) << "n = " << n();
90 }
91 }
92
93 private:
94 size_t n_{1};
95 size_t iterations_{15};
96};