blob: 9025328b7ef409750e4b8fc5f2dff014ddd374ef [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>
Marat Dukhan5ce30d92020-04-14 03:31:26 -070018#include <limits>
XNNPACK Teamb455b122019-09-27 18:10:33 -070019#include <random>
20#include <vector>
21
22#include <xnnpack/params.h>
23
24
25class RMaxMicrokernelTester {
26 public:
27 inline RMaxMicrokernelTester& n(size_t n) {
28 assert(n != 0);
29 this->n_ = n;
30 return *this;
31 }
32
33 inline size_t n() const {
34 return this->n_;
35 }
36
37 inline RMaxMicrokernelTester& iterations(size_t iterations) {
38 this->iterations_ = iterations;
39 return *this;
40 }
41
42 inline size_t iterations() const {
43 return this->iterations_;
44 }
45
46 void Test(xnn_u8_rmax_ukernel_function rmax) const {
47 std::random_device random_device;
48 auto rng = std::mt19937(random_device());
Marat Dukhan5ce30d92020-04-14 03:31:26 -070049 auto u8rng = std::bind(std::uniform_int_distribution<uint32_t>(0, std::numeric_limits<uint8_t>::max()), rng);
XNNPACK Teamb455b122019-09-27 18:10:33 -070050
51 std::vector<uint8_t> x(n());
52 for (size_t iteration = 0; iteration < iterations(); iteration++) {
53 std::generate(x.begin(), x.end(), std::ref(u8rng));
54
55 // Compute reference results.
56 uint8_t y_ref = 0;
57 for (size_t i = 0; i < n(); i++) {
58 y_ref = std::max(y_ref, x[i]);
59 }
60
61 // Call optimized micro-kernel.
62 uint8_t y = u8rng();
63 rmax(n() * sizeof(uint8_t), x.data(), &y);
64
65 // Verify results.
66 ASSERT_EQ(y_ref, y) << "n = " << n();
67 }
68 }
69
70 void Test(xnn_f32_rmax_ukernel_function rmax) const {
71 std::random_device random_device;
72 auto rng = std::mt19937(random_device());
73 auto f32rng = std::bind(std::uniform_real_distribution<float>(), rng);
74
75 std::vector<float> x(n());
76 for (size_t iteration = 0; iteration < iterations(); iteration++) {
77 std::generate(x.begin(), x.end(), std::ref(f32rng));
78
79 // Compute reference results.
80 float y_ref = 0;
81 for (size_t i = 0; i < n(); i++) {
82 y_ref = std::max(y_ref, x[i]);
83 }
84
85 // Call optimized micro-kernel.
86 float y = std::nanf("");
87 rmax(n() * sizeof(float), x.data(), &y);
88
89 // Verify results.
90 ASSERT_EQ(y_ref, y) << "n = " << n();
91 }
92 }
93
94 private:
95 size_t n_{1};
96 size_t iterations_{15};
97};