From 8c69764d12a65a1f17fc5519e1508da095c7a065 Mon Sep 17 00:00:00 2001 From: tensor-tang Date: Thu, 27 Sep 2018 15:48:26 +0800 Subject: [PATCH] add vmul unit tests --- .../fluid/operators/math/jit_kernel_blas.cc | 1 - .../fluid/operators/math/jit_kernel_test.cc | 60 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/operators/math/jit_kernel_blas.cc b/paddle/fluid/operators/math/jit_kernel_blas.cc index 4ce60ffc043..00213841c33 100644 --- a/paddle/fluid/operators/math/jit_kernel_blas.cc +++ b/paddle/fluid/operators/math/jit_kernel_blas.cc @@ -113,7 +113,6 @@ FOR_EACH_ISA_ALL_BLOCK(VMUL_MKL_DOUBLE) /// lt8 #ifdef PADDLE_USE_MKLML -VMUL_MKL_FLOAT(jit::avx, kLT8) VMUL_MKL_FLOAT(jit::avx2, kLT8) VMUL_MKL_FLOAT(jit::avx512f, kLT8) #endif diff --git a/paddle/fluid/operators/math/jit_kernel_test.cc b/paddle/fluid/operators/math/jit_kernel_test.cc index 6b250291018..d9c8bb6d430 100644 --- a/paddle/fluid/operators/math/jit_kernel_test.cc +++ b/paddle/fluid/operators/math/jit_kernel_test.cc @@ -13,12 +13,72 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/fluid/operators/math/jit_kernel.h" +#include #include #include #include "gflags/gflags.h" #include "glog/logging.h" #include "gtest/gtest.h" +inline double GetCurrentUS() { + struct timeval time; + gettimeofday(&time, NULL); + return 1e+6 * time.tv_sec + time.tv_usec; +} + +template +void RandomVec(const int n, T* a) { + static unsigned int seed = 100; + std::mt19937 rng(seed++); + std::uniform_real_distribution uniform_dist(0, 1); + const T lower = static_cast(-20.f); + const T upper = static_cast(20.f); + for (int i = 0; i < n; ++i) { + a[i] = static_cast(uniform_dist(rng) * (upper - lower) + lower); + } +} + +constexpr int repeat = 10000; + +TEST(JitKernel, vmul) { + namespace jit = paddle::operators::math::jitkernel; + + auto ref = [](const int n, const float* x, const float* y, float* z) { + for (int i = 0; i < n; ++i) { + z[i] = x[i] * y[i]; + } + }; + + for (int d : {7, 8, 15, 16, 30, 256}) { + std::vector x(d), y(d); + std::vector zref(d), ztgt(d); + RandomVec(d, x.data()); + RandomVec(d, y.data()); + const auto& ker = + jit::KernelPool::Instance().template Get>(d); + + const float* x_data = x.data(); + const float* y_data = y.data(); + float* ztgt_data = ztgt.data(); + float* zref_data = zref.data(); + auto st = GetCurrentUS(); + for (int i = 0; i < repeat; ++i) { + ker->Compute(d, x_data, y_data, ztgt_data); + } + auto mt = GetCurrentUS(); + for (int i = 0; i < repeat; ++i) { + ref(d, x_data, y_data, zref_data); + } + auto et = GetCurrentUS(); + + VLOG(3) << "Vec size " << d << ": refer takes: " << (et - mt) / repeat + << " us, tgt takes: " << (mt - st) / repeat; + for (int i = 0; i < d; ++i) { + EXPECT_NEAR(ztgt_data[i], zref_data[i], 1e-3); + } + } +} + TEST(JitKernel, pool) { namespace jit = paddle::operators::math::jitkernel; const int frame_size = 4; -- GitLab