forked from google/XNNPACK
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlut-norm-microkernel-tester.h
105 lines (90 loc) · 2.62 KB
/
lut-norm-microkernel-tester.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) Facebook, Inc. and its affiliates.
// All rights reserved.
//
// Copyright 2019 Google LLC
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
#pragma once
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <functional>
#include <limits>
#include <random>
#include <vector>
#include <gtest/gtest.h>
#include "xnnpack/microfnptr.h"
#include "replicable_random_device.h"
class LUTNormMicrokernelTester {
public:
LUTNormMicrokernelTester& n(size_t n) {
assert(n != 0);
this->n_ = n;
return *this;
}
size_t n() const {
return this->n_;
}
LUTNormMicrokernelTester& inplace(bool inplace) {
this->inplace_ = inplace;
return *this;
}
bool inplace() const {
return this->inplace_;
}
LUTNormMicrokernelTester& iterations(size_t iterations) {
this->iterations_ = iterations;
return *this;
}
size_t iterations() const {
return this->iterations_;
}
void Test(xnn_u8_lut32norm_ukernel_fn lutnorm) const {
xnnpack::ReplicableRandomDevice rng;
auto u8rng = [&rng]() {
return std::uniform_int_distribution<uint32_t>(
0, std::numeric_limits<uint8_t>::max())(rng);
};
auto u32rng = [&]() {
return std::uniform_int_distribution<uint32_t>(
1, std::numeric_limits<uint32_t>::max() / (257 * n()))(rng);
};
std::vector<uint8_t> x(n());
std::vector<uint32_t> t(256);
std::vector<uint8_t> y(n());
std::vector<float> y_ref(n());
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(x.begin(), x.end(), std::ref(u8rng));
std::generate(t.begin(), t.end(), std::ref(u32rng));
if (inplace()) {
std::generate(y.begin(), y.end(), std::ref(u8rng));
} else {
std::fill(y.begin(), y.end(), 0xA5);
}
const uint8_t* x_data = inplace() ? y.data() : x.data();
// Compute reference results.
uint32_t sum = 0;
for (size_t i = 0; i < n(); i++) {
sum += t[x_data[i]];
}
for (size_t i = 0; i < n(); i++) {
y_ref[i] = 256.0f * float(t[x_data[i]]) / float(sum);
y_ref[i] = std::min(y_ref[i], 255.0f);
}
// Call optimized micro-kernel.
lutnorm(n(), x_data, t.data(), y.data());
// Verify results.
for (size_t i = 0; i < n(); i++) {
EXPECT_NEAR(y_ref[i], float(y[i]), 0.5f)
<< "at position " << i << ", n = " << n() << ", sum = " << sum;
}
}
}
private:
size_t n_{1};
bool inplace_{false};
size_t iterations_{15};
};