From eb28f8b3ede85b4b3c14e4ad4fa4a5bd10c14076 Mon Sep 17 00:00:00 2001 From: Chen Xin Date: Wed, 25 Aug 2021 11:25:29 +0800 Subject: [PATCH] move ArraysMatch function into src/tim/vx/test_utils.h Signed-off-by: Chen Xin --- src/tim/vx/ops/activations_test.cc | 15 +----------- src/tim/vx/ops/addn_test.cc | 2 +- src/tim/vx/ops/avg_pool_test.cc | 17 ++----------- src/tim/vx/ops/conv1d_test.cc | 15 +----------- src/tim/vx/ops/groupedconv2d_test.cc | 15 +----------- src/tim/vx/ops/instancenormalization_test.cc | 15 +----------- src/tim/vx/ops/layernormalization_test.cc | 15 +----------- src/tim/vx/ops/logsoftmax_test.cc | 15 +----------- src/tim/vx/ops/matmul_test.cc | 15 +----------- src/tim/vx/ops/moments_test.cc | 18 ++------------ src/tim/vx/ops/resize1d_test.cc | 15 +----------- src/tim/vx/test_utils.h | 25 ++++++++++++++++++++ 12 files changed, 38 insertions(+), 144 deletions(-) diff --git a/src/tim/vx/ops/activations_test.cc b/src/tim/vx/ops/activations_test.cc index 4ffa959..a920db3 100644 --- a/src/tim/vx/ops/activations_test.cc +++ b/src/tim/vx/ops/activations_test.cc @@ -28,19 +28,6 @@ #include "gtest/gtest.h" #include "src/tim/vx/test_utils.h" -namespace { -template -::testing::AssertionResult ArraysMatch(const std::vector& expected, - const std::vector& actual, - T abs_error){ - for (size_t i = 0; i < expected.size(); ++i){ - EXPECT_NEAR(expected[i], actual[i], abs_error) << "at index:" << i; - } - - return ::testing::AssertionSuccess(); -} -} - TEST(Linear, shape_5_1_fp32) { auto ctx = tim::vx::Context::Create(); auto graph = ctx->CreateGraph(); @@ -218,5 +205,5 @@ TEST(Gelu, shape_5_1_uint8_Quantized) { std::vector output(golden.size()); EXPECT_TRUE(output_tensor->CopyDataFromTensor(output.data())); - EXPECT_EQ(golden, output); + EXPECT_TRUE(ArraysMatch(golden, output, (uint8_t)1)); } \ No newline at end of file diff --git a/src/tim/vx/ops/addn_test.cc b/src/tim/vx/ops/addn_test.cc index 03cbfa9..88e9ecf 100644 --- a/src/tim/vx/ops/addn_test.cc +++ b/src/tim/vx/ops/addn_test.cc @@ -164,5 +164,5 @@ TEST(AddN, shape_2_2_uint8_Quantized) { std::vector output(4); EXPECT_TRUE(output_tensor->CopyDataFromTensor(output.data())); - EXPECT_EQ(golden, output); + EXPECT_TRUE(ArraysMatch(golden, output, (uint8_t)1)); } \ No newline at end of file diff --git a/src/tim/vx/ops/avg_pool_test.cc b/src/tim/vx/ops/avg_pool_test.cc index d0b3cef..8838f3d 100644 --- a/src/tim/vx/ops/avg_pool_test.cc +++ b/src/tim/vx/ops/avg_pool_test.cc @@ -28,19 +28,6 @@ #include "gtest/gtest.h" #include "src/tim/vx/test_utils.h" -namespace { -template -::testing::AssertionResult ArraysMatch(const std::vector& expected, - const std::vector& actual, - T abs_error){ - for (size_t i = 0; i < expected.size(); ++i){ - EXPECT_NEAR(expected[i], actual[i], abs_error) << "at index:" << i; - } - - return ::testing::AssertionSuccess(); -} -} - TEST(AVG, shape_3_3_1_2_fp32_kernel_2_stride_1) { auto ctx = tim::vx::Context::Create(); auto graph = ctx->CreateGraph(); @@ -183,7 +170,7 @@ TEST(AVG, shape_3_3_1_1_uint8_kernel_2_stride_1) { std::vector output(golden.size()); EXPECT_TRUE(output_tensor->CopyDataFromTensor(output.data())); - EXPECT_EQ(golden, output); + EXPECT_TRUE(ArraysMatch(golden, output, (uint8_t)1)); } TEST(AVG, shape_60_52_3_5_fp32_kernel_35_stride_5) { @@ -398,5 +385,5 @@ TEST(AVG_ANDROID, shape_60_52_3_5_uint8_kernel_35_stride_5) { std::vector output(golden.size()); EXPECT_TRUE(output_tensor->CopyDataFromTensor(output.data())); - EXPECT_EQ(golden, output); + EXPECT_TRUE(ArraysMatch(golden, output, (uint8_t)1)); } \ No newline at end of file diff --git a/src/tim/vx/ops/conv1d_test.cc b/src/tim/vx/ops/conv1d_test.cc index 6c1b6bd..b024c67 100644 --- a/src/tim/vx/ops/conv1d_test.cc +++ b/src/tim/vx/ops/conv1d_test.cc @@ -24,22 +24,9 @@ #include "tim/vx/context.h" #include "tim/vx/graph.h" #include "tim/vx/ops/conv1d.h" - +#include "src/tim/vx/test_utils.h" #include "gtest/gtest.h" -namespace { -template -::testing::AssertionResult ArraysMatch(const std::vector& expected, - const std::vector& actual, - T abs_error){ - for (size_t i = 0; i < expected.size(); ++i){ - EXPECT_NEAR(expected[i], actual[i], abs_error) << "at index:" << i; - } - - return ::testing::AssertionSuccess(); -} -} - TEST(Conv1d, shape_3_6_1_float_ksize_1_stride_1_weights_3_no_bias_whcn) { auto ctx = tim::vx::Context::Create(); auto graph = ctx->CreateGraph(); diff --git a/src/tim/vx/ops/groupedconv2d_test.cc b/src/tim/vx/ops/groupedconv2d_test.cc index a1d7594..b6ae3ca 100644 --- a/src/tim/vx/ops/groupedconv2d_test.cc +++ b/src/tim/vx/ops/groupedconv2d_test.cc @@ -24,22 +24,9 @@ #include "tim/vx/context.h" #include "tim/vx/graph.h" #include "tim/vx/ops/groupedconv2d.h" - +#include "src/tim/vx/test_utils.h" #include "gtest/gtest.h" -namespace { -template -::testing::AssertionResult ArraysMatch(const std::vector& expected, - const std::vector& actual, - T abs_error){ - for (size_t i = 0; i < expected.size(); ++i){ - EXPECT_NEAR(expected[i], actual[i], abs_error) << "at index:" << i; - } - - return ::testing::AssertionSuccess(); -} -} - TEST(GroupedConv2d, shape_3_3_6_1_float_group_1_no_bias_whcn) { auto ctx = tim::vx::Context::Create(); auto graph = ctx->CreateGraph(); diff --git a/src/tim/vx/ops/instancenormalization_test.cc b/src/tim/vx/ops/instancenormalization_test.cc index 4123d7a..7111057 100644 --- a/src/tim/vx/ops/instancenormalization_test.cc +++ b/src/tim/vx/ops/instancenormalization_test.cc @@ -24,22 +24,9 @@ #include "tim/vx/context.h" #include "tim/vx/graph.h" #include "tim/vx/ops/instancenormalization.h" - +#include "src/tim/vx/test_utils.h" #include "gtest/gtest.h" -namespace { -template -::testing::AssertionResult ArraysMatch(const std::vector& expected, - const std::vector& actual, - T abs_error){ - for (size_t i = 0; i < expected.size(); ++i){ - EXPECT_NEAR(expected[i], actual[i], abs_error) << "at index:" << i; - } - - return ::testing::AssertionSuccess(); -} -} - TEST(InstanceNorm, shape_3_6_1_float) { auto ctx = tim::vx::Context::Create(); auto graph = ctx->CreateGraph(); diff --git a/src/tim/vx/ops/layernormalization_test.cc b/src/tim/vx/ops/layernormalization_test.cc index 19466d2..e749903 100644 --- a/src/tim/vx/ops/layernormalization_test.cc +++ b/src/tim/vx/ops/layernormalization_test.cc @@ -24,22 +24,9 @@ #include "tim/vx/context.h" #include "tim/vx/graph.h" #include "tim/vx/ops/layernormalization.h" - +#include "src/tim/vx/test_utils.h" #include "gtest/gtest.h" -namespace { -template -::testing::AssertionResult ArraysMatch(const std::vector& expected, - const std::vector& actual, - T abs_error){ - for (size_t i = 0; i < expected.size(); ++i){ - EXPECT_NEAR(expected[i], actual[i], abs_error) << "at index:" << i; - } - - return ::testing::AssertionSuccess(); -} -} - TEST(LayerNorm, axis_0_shape_3_6_1_float) { auto ctx = tim::vx::Context::Create(); auto graph = ctx->CreateGraph(); diff --git a/src/tim/vx/ops/logsoftmax_test.cc b/src/tim/vx/ops/logsoftmax_test.cc index e64f7ad..681621d 100644 --- a/src/tim/vx/ops/logsoftmax_test.cc +++ b/src/tim/vx/ops/logsoftmax_test.cc @@ -24,22 +24,9 @@ #include "tim/vx/context.h" #include "tim/vx/graph.h" #include "tim/vx/ops/logsoftmax.h" - +#include "src/tim/vx/test_utils.h" #include "gtest/gtest.h" -namespace { -template -::testing::AssertionResult ArraysMatch(const std::vector& expected, - const std::vector& actual, - T abs_error){ - for (size_t i = 0; i < expected.size(); ++i){ - EXPECT_NEAR(expected[i], actual[i], abs_error) << "at index:" << i; - } - - return ::testing::AssertionSuccess(); -} -} - TEST(LogSoftmax, shape_6_1_float_axis_0) { auto ctx = tim::vx::Context::Create(); auto graph = ctx->CreateGraph(); diff --git a/src/tim/vx/ops/matmul_test.cc b/src/tim/vx/ops/matmul_test.cc index 47ad77f..60f6395 100644 --- a/src/tim/vx/ops/matmul_test.cc +++ b/src/tim/vx/ops/matmul_test.cc @@ -24,22 +24,9 @@ #include "tim/vx/context.h" #include "tim/vx/graph.h" #include "tim/vx/ops/matmul.h" - +#include "src/tim/vx/test_utils.h" #include "gtest/gtest.h" -namespace { -template -::testing::AssertionResult ArraysMatch(const std::vector& expected, - const std::vector& actual, - T abs_error){ - for (size_t i = 0; i < expected.size(); ++i){ - EXPECT_NEAR(expected[i], actual[i], abs_error) << "at index:" << i; - } - - return ::testing::AssertionSuccess(); -} -} - TEST(Matmul, shape_2_6_shape_6_2_float) { auto ctx = tim::vx::Context::Create(); auto graph = ctx->CreateGraph(); diff --git a/src/tim/vx/ops/moments_test.cc b/src/tim/vx/ops/moments_test.cc index a03038f..7e2c34b 100644 --- a/src/tim/vx/ops/moments_test.cc +++ b/src/tim/vx/ops/moments_test.cc @@ -25,23 +25,9 @@ #include "tim/vx/context.h" #include "tim/vx/graph.h" #include "tim/vx/ops/moments.h" - +#include "src/tim/vx/test_utils.h" #include "gtest/gtest.h" -namespace { -template -::testing::AssertionResult ArraysMatch(const std::vector& expected, - const std::vector& actual, - T abs_error, - const std::string& msg){ - for (size_t i = 0; i < expected.size(); ++i){ - EXPECT_NEAR(expected[i], actual[i], abs_error) << msg << " at index:" << i; - } - - return ::testing::AssertionSuccess(); -} -} - TEST(Moments, shape_6_3_1_float_axes_0_1) { auto ctx = tim::vx::Context::Create(); auto graph = ctx->CreateGraph(); @@ -113,7 +99,7 @@ TEST(Moments, shape_3_6_1_float_axes_1_keepdims) { -4, 0, 4, -5, 0, 5, -6, 0, 6, - -7, 0, 7 + -7, 0, 7 }; std::vector mean_golden = { diff --git a/src/tim/vx/ops/resize1d_test.cc b/src/tim/vx/ops/resize1d_test.cc index 49d97ce..357dc42 100644 --- a/src/tim/vx/ops/resize1d_test.cc +++ b/src/tim/vx/ops/resize1d_test.cc @@ -24,22 +24,9 @@ #include "tim/vx/context.h" #include "tim/vx/graph.h" #include "tim/vx/ops/resize1d.h" - +#include "src/tim/vx/test_utils.h" #include "gtest/gtest.h" -namespace { -template -::testing::AssertionResult ArraysMatch(const std::vector& expected, - const std::vector& actual, - T abs_error){ - for (size_t i = 0; i < expected.size(); ++i){ - EXPECT_NEAR(expected[i], actual[i], abs_error) << "at index:" << i; - } - - return ::testing::AssertionSuccess(); -} -} - TEST(Resize1d, shape_4_2_1_float_nearest_whcn) { auto ctx = tim::vx::Context::Create(); auto graph = ctx->CreateGraph(); diff --git a/src/tim/vx/test_utils.h b/src/tim/vx/test_utils.h index 7f96641..e1e81af 100644 --- a/src/tim/vx/test_utils.h +++ b/src/tim/vx/test_utils.h @@ -109,5 +109,30 @@ inline std::vector Dequantize(const std::vector& data, float scale, return f; } +namespace { +template +::testing::AssertionResult ArraysMatch(const std::vector& expected, + const std::vector& actual, + T abs_error){ + for (size_t i = 0; i < expected.size(); ++i){ + EXPECT_NEAR(expected[i], actual[i], abs_error) << "at index:" << i; + } + return ::testing::AssertionSuccess(); +} +} + +namespace { +template +::testing::AssertionResult ArraysMatch(const std::vector& expected, + const std::vector& actual, + T abs_error, + const std::string& msg){ + for (size_t i = 0; i < expected.size(); ++i){ + EXPECT_NEAR(expected[i], actual[i], abs_error) << msg << " at index:" << i; + } + + return ::testing::AssertionSuccess(); +} +} #endif /* TIM_VX_TEST_UTILS_H_ */ \ No newline at end of file