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/microfnptr.h> 19 20 21 class UnpoolMicrokernelTester { 22 public: p(size_t p)23 inline UnpoolMicrokernelTester& p(size_t p) { 24 assert(p != 0); 25 this->p_ = p; 26 return *this; 27 } 28 p()29 inline size_t p() const { 30 return this->p_; 31 } 32 c(size_t c)33 inline UnpoolMicrokernelTester& c(size_t c) { 34 assert(c != 0); 35 this->c_ = c; 36 return *this; 37 } 38 c()39 inline size_t c() const { 40 return this->c_; 41 } 42 f(uint32_t f)43 inline UnpoolMicrokernelTester& f(uint32_t f) { 44 this->f_ = f; 45 return *this; 46 } 47 f()48 inline uint32_t f() const { 49 return this->f_; 50 } 51 y_stride(size_t y_stride)52 inline UnpoolMicrokernelTester& y_stride(size_t y_stride) { 53 assert(y_stride != 0); 54 this->y_stride_ = y_stride; 55 return *this; 56 } 57 y_stride()58 inline size_t y_stride() const { 59 if (this->y_stride_ == 0) { 60 return c(); 61 } else { 62 assert(this->y_stride_ >= c()); 63 return this->y_stride_; 64 } 65 } 66 iterations(size_t iterations)67 inline UnpoolMicrokernelTester& iterations(size_t iterations) { 68 this->iterations_ = iterations; 69 return *this; 70 } 71 iterations()72 inline size_t iterations() const { 73 return this->iterations_; 74 } 75 Test(xnn_x32_unpool_ukernel_function unpool)76 void Test(xnn_x32_unpool_ukernel_function unpool) const { 77 std::random_device random_device; 78 auto rng = std::mt19937(random_device()); 79 auto x_rng = std::bind(std::uniform_int_distribution<uint32_t>(), std::ref(rng)); 80 auto i_rng = std::bind(std::uniform_int_distribution<uint32_t>(0, uint32_t(p() - 1)), std::ref(rng)); 81 82 std::vector<uint32_t> x(c()); 83 std::vector<uint32_t> i(c()); 84 std::vector<uint32_t> y((p() - 1) * y_stride() + c()); 85 std::vector<uint32_t*> indirect_y(p()); 86 std::vector<uint32_t> y_ref((p() - 1) * y_stride() + c()); 87 for (size_t iteration = 0; iteration < iterations(); iteration++) { 88 std::generate(x.begin(), x.end(), std::ref(x_rng)); 89 std::generate(i.begin(), i.end(), std::ref(i_rng)); 90 std::generate(y.begin(), y.end(), std::ref(x_rng)); 91 92 for (size_t i = 0; i < indirect_y.size(); i++) { 93 indirect_y[i] = y.data() + i * y_stride(); 94 } 95 std::shuffle(indirect_y.begin(), indirect_y.end(), rng); 96 97 // Compute reference output. 98 std::fill(y_ref.begin(), y_ref.end(), f()); 99 for (size_t k = 0; k < c(); k++) { 100 const uint32_t idx = i[k]; 101 (indirect_y[idx] - y.data() + y_ref.data())[k] = x[k]; 102 } 103 104 // Call optimized micro-kernel. 105 unpool(p(), c(), f(), x.data(), i.data(), indirect_y.data()); 106 107 // Verify results. 108 for (size_t i = 0; i < p(); i++) { 109 for (size_t k = 0; k < c(); k++) { 110 ASSERT_EQ(y_ref[i * y_stride() + k], y[i * y_stride() + k]) 111 << "at pixel " << i << ", channel " << k 112 << ", p = " << p() << ", c = " << c(); 113 } 114 } 115 } 116 } 117 118 private: 119 size_t p_{1}; 120 size_t c_{1}; 121 uint32_t f_{0}; 122 size_t y_stride_{0}; 123 size_t iterations_{15}; 124 }; 125