Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace lut32norm yaml with table header #7444

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ MICROKERNEL_DEFS = [
"src/s8-maxpool/s8-maxpool-minmax.h",
"src/s8-vclamp/s8-vclamp.h",
"src/s32-f32-vcvt/s32-f32-vcvt.h",
"src/u8-lut32norm/u8-lut32norm.h",
"src/u8-maxpool/u8-maxpool-minmax.h",
"src/u8-vclamp/u8-vclamp.h",
"src/xx-fill/xx-fill.h",
Expand Down
18 changes: 18 additions & 0 deletions src/u8-lut32norm/u8-lut32norm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2023 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.

#ifndef XNN_UKERNEL_WITH_PARAMS
#define XNN_UKERNEL_WITH_PARAMS(arch_flags, ukernel, datatype, params_type, init_params) \
XNN_UKERNEL(arch_flags, ukernel, datatype)
#define XNN_DEFINED_UKERNEL_WITH_PARAMS
#endif

#ifndef XNN_UKERNEL
#define XNN_UKERNEL(arch_flags, ukernel, datatype) \
XNN_UKERNEL_WITH_PARAMS(arch_flags, ukernel, datatype, void, /*init_params=*/nullptr)
#define XNN_DEFINED_UKERNEL
#endif

XNN_UKERNEL_WITH_PARAMS(0, xnn_u8_lut32norm_ukernel__scalar, uint8_t, void, nullptr)
7 changes: 3 additions & 4 deletions src/xnnpack/lut.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,14 @@ DECLARE_X8_LUT_UKERNEL_FUNCTION(xnn_x8_lut_ukernel__wasmsimd_u48)
DECLARE_X8_LUT_UKERNEL_FUNCTION(xnn_x8_lut_ukernel__wasmsimd_u64)


#define DECLARE_U8_LUT32NORM_UKERNEL_FUNCTION(fn_name) \
#define XNN_UKERNEL(arch_flags, fn_name, datatype) \
XNN_INTERNAL void fn_name( \
size_t n, \
const uint8_t* x, \
const uint32_t* t, \
uint8_t* y);

DECLARE_U8_LUT32NORM_UKERNEL_FUNCTION(xnn_u8_lut32norm_ukernel__scalar)

#include "u8-lut32norm/u8-lut32norm.h"
#undef XNN_UKERNEL

#ifdef __cplusplus
} // extern "C"
Expand Down
1 change: 0 additions & 1 deletion test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,6 @@ xnnpack_unit_test(
xnnpack_unit_test(
name = "u8_lut32norm_test",
srcs = [
"lut-norm-microkernel-tester.h",
"u8-lut32norm.cc",
],
deps = MICROKERNEL_TEST_DEPS,
Expand Down
100 changes: 0 additions & 100 deletions test/lut-norm-microkernel-tester.h

This file was deleted.

190 changes: 138 additions & 52 deletions test/u8-lut32norm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,146 @@
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
//
// Auto-generated file. Do not edit!
// Specification: test/u8-lut32norm.yaml
// Generator: tools/generate-lut-norm-test.py

#include <cstddef>

#include <gtest/gtest.h>
#include "xnnpack/lut.h"
#include "lut-norm-microkernel-tester.h"

TEST(U8_LUT32NORM__SCALAR, n_eq_1) {
LUTNormMicrokernelTester()
.n(1)
.Test(xnn_u8_lut32norm_ukernel__scalar);
}

TEST(U8_LUT32NORM__SCALAR, small_n) {
for (size_t n = 2; n <= 16; n++) {
LUTNormMicrokernelTester()
.n(n)
.Test(xnn_u8_lut32norm_ukernel__scalar);
}
}

TEST(U8_LUT32NORM__SCALAR, large_n) {
for (size_t n = 16; n <= 128; n+=2) {
LUTNormMicrokernelTester()
.n(n)
.Test(xnn_u8_lut32norm_ukernel__scalar);
}
}

TEST(U8_LUT32NORM__SCALAR, n_eq_1_inplace) {
LUTNormMicrokernelTester()
.n(1)
.inplace(true)
.Test(xnn_u8_lut32norm_ukernel__scalar);
}

TEST(U8_LUT32NORM__SCALAR, small_n_inplace) {
for (size_t n = 2; n <= 16; n++) {
LUTNormMicrokernelTester()
.n(n)
.inplace(true)
.Test(xnn_u8_lut32norm_ukernel__scalar);
}
}

TEST(U8_LUT32NORM__SCALAR, large_n_inplace) {
for (size_t n = 16; n <= 128; n+=2) {
LUTNormMicrokernelTester()
.n(n)
.inplace(true)
.Test(xnn_u8_lut32norm_ukernel__scalar);
}
}

#include <algorithm>
#include <cassert>
#include <cstdint>
#include <cstdlib>
#include <functional>
#include <limits>
#include <random>
#include <vector>

#include "xnnpack/microfnptr.h"
#include "xnnpack/buffer.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 u32rng = [&]() {
return std::uniform_int_distribution<uint32_t>(
1, std::numeric_limits<uint32_t>::max() / (257 * n()))(rng);
};

xnnpack::Buffer<uint8_t> x(n());
xnnpack::Buffer<uint32_t> t(256);
xnnpack::Buffer<uint8_t> y(n());
xnnpack::Buffer<float> y_ref(n());
for (size_t iteration = 0; iteration < iterations(); iteration++) {
xnnpack::fill_uniform_random_bits(x.data(), x.size(), rng);
std::generate(t.begin(), t.end(), std::ref(u32rng));
if (inplace()) {
xnnpack::fill_uniform_random_bits(y.data(), y.size(), rng);
}
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};
};

#define XNN_TEST_LUTNORM_N_EQ_1(ukernel, arch_flags, ...) \
TEST(ukernel, n_eq_1) \
{ \
LUTNormMicrokernelTester().n(1).Test(ukernel); \
}
#define XNN_TEST_LUTNORM_SMALL_N(ukernel, arch_flags, ...) \
TEST(ukernel, small_n) \
{ \
for (size_t i = 2; i <= 16; i++) { \
LUTNormMicrokernelTester().n(i).Test(ukernel); \
} \
}
#define XNN_TEST_LUTNORM_LARGE_N(ukernel, arch_flags, ...) \
TEST(ukernel, large_n) \
{ \
for (size_t i = 16; i <= 128; i += 2) { \
LUTNormMicrokernelTester().n(i).Test(ukernel); \
} \
}
#define XNN_TEST_LUTNORM_N_EQ_1_INPLACE(ukernel, arch_flags, ...) \
TEST(ukernel, n_eq_1_inplace) \
{ \
LUTNormMicrokernelTester().n(1).inplace(true).Test(ukernel); \
}
#define XNN_TEST_LUTNORM_SMALL_N_INPLACE(ukernel, arch_flags, ...) \
TEST(ukernel, small_n_inplace) \
{ \
for (size_t i = 2; i <= 16; i++) { \
LUTNormMicrokernelTester().n(i).inplace(true).Test(ukernel); \
} \
}
#define XNN_TEST_LUTNORM_LARGE_N_INPLACE(ukernel, arch_flags, ...) \
TEST(ukernel, large_n_inplace) \
{ \
for (size_t i = 16; i <= 128; i += 2) { \
LUTNormMicrokernelTester().n(i).inplace(true).Test(ukernel); \
} \
}

#define XNN_UKERNEL_WITH_PARAMS(arch_flags, ukernel, datatype, params_type, init_params) \
XNN_TEST_LUTNORM_N_EQ_1(ukernel, arch_flags, init_params); \
XNN_TEST_LUTNORM_SMALL_N(ukernel, arch_flags, init_params); \
XNN_TEST_LUTNORM_LARGE_N(ukernel, arch_flags, init_params); \
XNN_TEST_LUTNORM_N_EQ_1_INPLACE(ukernel, arch_flags, init_params); \
XNN_TEST_LUTNORM_SMALL_N_INPLACE(ukernel, arch_flags, init_params); \
XNN_TEST_LUTNORM_LARGE_N_INPLACE(ukernel, arch_flags, init_params);
#include "u8-lut32norm/u8-lut32norm.h"
#undef XNN_UKERNEL_WITH_PARAMS
7 changes: 0 additions & 7 deletions test/u8-lut32norm.yaml

This file was deleted.

Loading