From 299be048775ba32eecce57d22a89551fd6b0f92d Mon Sep 17 00:00:00 2001 From: tensor-tang Date: Thu, 13 Jun 2019 08:45:11 +0000 Subject: [PATCH] fix mul kernel test --- .../lite/kernels/arm/conv_compute_test.cc | 15 ++ .../lite/kernels/arm/mul_compute_test.cc | 178 +++++++++--------- 2 files changed, 104 insertions(+), 89 deletions(-) diff --git a/paddle/fluid/lite/kernels/arm/conv_compute_test.cc b/paddle/fluid/lite/kernels/arm/conv_compute_test.cc index 4b95aa5ce4a..e4d80265d77 100644 --- a/paddle/fluid/lite/kernels/arm/conv_compute_test.cc +++ b/paddle/fluid/lite/kernels/arm/conv_compute_test.cc @@ -124,6 +124,20 @@ TEST(conv_arm, init) { TEST(conv_arm, compute) { DeviceInfo::Init(); +#if 1 + for (auto n : {2}) { + for (auto ic : {6}) { + for (auto oc : {6}) { + for (auto ih : {9}) { + for (auto iw : {9}) { + for (auto flag_bias : {false, true}) { + for (auto flag_relu : {false, true}) { + for (auto depthwise : {false, true}) { + for (auto dilation : {1}) { + for (auto stride : {1, 2}) { + for (auto padding : {0, 1, 2}) { + for (auto ks : {1, 3, 5}) { +#else for (auto n : {1, 2}) { for (auto ic : {6, 32 /*, 128*/}) { for (auto oc : {6, 32 /*, 128*/}) { @@ -136,6 +150,7 @@ TEST(conv_arm, compute) { for (auto stride : {1, 2}) { for (auto padding : {0, 1, 2}) { for (auto ks : {1, 3, 5}) { +#endif int group = 1; if (depthwise) { // depthwise convolution ? group = oc = ic; diff --git a/paddle/fluid/lite/kernels/arm/mul_compute_test.cc b/paddle/fluid/lite/kernels/arm/mul_compute_test.cc index cef99b17607..22cbfed5fd2 100644 --- a/paddle/fluid/lite/kernels/arm/mul_compute_test.cc +++ b/paddle/fluid/lite/kernels/arm/mul_compute_test.cc @@ -14,7 +14,10 @@ #include "paddle/fluid/lite/kernels/arm/mul_compute.h" #include +#include +#include #include +#include #include #include #include "paddle/fluid/lite/arm/math/funcs.h" @@ -25,6 +28,17 @@ namespace lite { namespace kernels { namespace arm { +template +void FillData(T* a, const int n, const T lower = static_cast(-2.f), + const T upper = static_cast(2.f)) { + static unsigned int seed = 100; + std::mt19937 rng(seed++); + std::uniform_real_distribution uniform_dist(0, 1); + for (int i = 0; i < n; ++i) { + a[i] = static_cast(uniform_dist(rng) * (upper - lower) + lower); + } +} + TEST(mul_arm, retrive_op) { auto mul = KernelRegistry::Global().Create("mul"); @@ -33,114 +47,100 @@ TEST(mul_arm, retrive_op) { } TEST(mul_arm, init) { - FcCompute mul; + MulCompute mul; ASSERT_EQ(mul.precision(), PRECISION(kFloat)); ASSERT_EQ(mul.target(), TARGET(kARM)); } TEST(mul_arm, compare_test) { - lite::Tensor x, w, b, out, ref; - constexpr int batch_size = 2; - x.Resize({batch_size, 3}); - w.Resize({3, 4}); - b.Resize({1, 4}); - out.Resize({batch_size, 4}); - ref.Resize({batch_size, 4}); - - auto x_data = x.mutable_data(); - auto w_data = w.mutable_data(); - auto b_data = b.mutable_data(); - auto out_data = out.mutable_data(); - auto ref_data = ref.mutable_data(); - - for (int64_t i = 0; i < x.dims().product(); i++) { - x_data[i] = static_cast(i); - } - for (int64_t i = 0; i < w.dims().product(); i++) { - w_data[i] = static_cast(i); - } - for (int64_t i = 0; i < b.dims().product(); i++) { - b_data[i] = static_cast(i); + using T = float; + + for (int m : {1, 2, 3, 4}) { + for (int n : {1, 2, 3, 4}) { + for (int k : {1, 2, 3, 4}) { + lite::Tensor x, y, out, ref; + x.Resize({m, k}); + y.Resize({k, n}); + out.Resize({m, n}); + ref.Resize({m, n}); + + auto* x_data = x.mutable_data(); + auto* y_data = y.mutable_data(); + auto* out_data = out.mutable_data(); + auto* ref_data = ref.mutable_data(); + + FillData(x_data, x.dims().production()); + FillData(y_data, y.dims().production()); + FillData(out_data, out.dims().production()); + FillData(ref_data, out.dims().production()); + + MulCompute mul; + operators::MulParam param; + + param.x = &x; + param.y = &y; + param.output = &out; + + DeviceInfo::Init(); + std::unique_ptr ctx(new KernelContext); + ctx->As(); + mul.SetParam(param); + mul.SetContext(std::move(ctx)); + mul.PrepareForRun(); + + mul.Run(); + + lite::arm::math::mul_compute_eigen(x_data, m, k, y_data, k, n, + ref_data); + for (int i = 0; i < out.dims().production(); i++) { + EXPECT_NEAR(out_data[i], ref_data[i], 1e-3); + } + } + } } +} + +TEST(mul_arm, num_col_dims) { + using T = float; - lite::arm::math::fc_compute_eigen(x_data, batch_size, 3, // - w_data, 3, 4, // - b_data, ref_data); + lite::Tensor x, y, out, ref; + x.Resize({2, 3, 4}); + y.Resize({3, 4, 5}); + out.Resize({2, 5}); + ref.Resize({2, 5}); - // mul compute kernel - FcCompute mul; - operators::FcParam param; + auto* x_data = x.mutable_data(); + auto* y_data = y.mutable_data(); + auto* out_data = out.mutable_data(); + auto* ref_data = ref.mutable_data(); - param.in_num_col_dims = 1; - param.input = &x; - param.w = &w; - param.bias = &b; + FillData(x_data, x.dims().production()); + FillData(y_data, y.dims().production()); + FillData(out_data, out.dims().production()); + FillData(ref_data, out.dims().production()); + + MulCompute mul; + operators::MulParam param; + + param.x = &x; + param.y = &y; param.output = &out; - param.in_mat_dims = x.dims(); + param.x_num_col_dims = 1; + param.y_num_col_dims = 2; DeviceInfo::Init(); std::unique_ptr ctx(new KernelContext); ctx->As(); mul.SetParam(param); mul.SetContext(std::move(ctx)); - mul.Run(); + mul.PrepareForRun(); - VLOG(3) << "output vs ref"; - for (int i = 0; i < out.dims().product(); i++) { - VLOG(3) << out_data[i] << " vs " << ref_data[i]; - } - - for (int i = 0; i < out.dims().product(); ++i) { - EXPECT_NEAR(out_data[i], ref_data[i], 1e-5); - } -} + mul.Run(); -TEST(mul_arm, num_col_dims) { - FcCompute mul; - operators::FcParam param; - - lite::Tensor x; - lite::Tensor w; - lite::Tensor bias; - lite::Tensor output; - - x.Resize({1, 2, 3}); - w.Resize({3, 4}); - bias.Resize({1, 4}); - output.Resize({2, 4}); - - auto* x_data = x.mutable_data(); - auto* w_data = w.mutable_data(); - auto* bias_data = bias.mutable_data(); - auto* output_data = output.mutable_data(); - - for (int64_t i = 0; i < x.dims().product(); i++) { - x_data[i] = static_cast(i); - } - for (int64_t i = 0; i < w.dims().product(); i++) { - w_data[i] = static_cast(i); - } - for (int64_t i = 0; i < bias.dims().product(); i++) { - bias_data[i] = static_cast(i); + lite::arm::math::mul_compute_eigen(x_data, 2, 12, y_data, 12, 5, ref_data); + for (int i = 0; i < out.dims().production(); i++) { + EXPECT_NEAR(out_data[i], ref_data[i], 1e-3); } - for (int64_t i = 0; i < output.dims().product(); i++) { - output_data[i] = static_cast(i); - } - - param.in_num_col_dims = 2; - param.input = &x; - param.w = &w; - param.bias = &bias; - param.output = &output; - param.in_mat_dims = x.dims(); - - std::unique_ptr ctx(new KernelContext); - ctx->As(); - DeviceInfo::Init(); - - mul.SetParam(param); - mul.SetContext(std::move(ctx)); - mul.Run(); } } // namespace arm -- GitLab