From 51536f7f52130237ea9e9ad1a00687ba5dd5b955 Mon Sep 17 00:00:00 2001 From: dengkaipeng Date: Thu, 21 Mar 2019 05:25:34 +0000 Subject: [PATCH] StrideASum. test=develop --- paddle/fluid/operators/jit/helper.cc | 2 +- paddle/fluid/operators/jit/kernel_base.h | 4 ++-- paddle/fluid/operators/jit/more/mix/mix.cc | 2 +- paddle/fluid/operators/jit/more/mkl/mkl.cc | 6 +++--- paddle/fluid/operators/jit/more/mkl/mkl.h | 4 ++-- paddle/fluid/operators/jit/refer/CMakeLists.txt | 2 +- paddle/fluid/operators/jit/refer/refer.cc | 2 +- paddle/fluid/operators/jit/refer/refer.h | 8 ++++---- paddle/fluid/operators/jit/test.cc | 1 + 9 files changed, 16 insertions(+), 15 deletions(-) diff --git a/paddle/fluid/operators/jit/helper.cc b/paddle/fluid/operators/jit/helper.cc index fe508788ef..f868c847bd 100644 --- a/paddle/fluid/operators/jit/helper.cc +++ b/paddle/fluid/operators/jit/helper.cc @@ -56,7 +56,7 @@ const char* to_string(KernelType kt) { ONE_CASE(kMatMul); ONE_CASE(kHMax); ONE_CASE(kHSum); - ONE_CASE(kStrideSum); + ONE_CASE(kStrideASum); ONE_CASE(kSoftmax); ONE_CASE(kEmbSeqPool); ONE_CASE(kSgd); diff --git a/paddle/fluid/operators/jit/kernel_base.h b/paddle/fluid/operators/jit/kernel_base.h index 6fd8a59d55..fdd41a830a 100644 --- a/paddle/fluid/operators/jit/kernel_base.h +++ b/paddle/fluid/operators/jit/kernel_base.h @@ -53,7 +53,7 @@ typedef enum { kVSquare, kVSub, kVTanh, - kStrideSum, + kStrideASum, kStrideScal, } KernelType; @@ -132,7 +132,7 @@ DECLARE_KERNELTUPLE(XYNTuple, VCopy); DECLARE_KERNELTUPLE(XRNTuple, HMax); DECLARE_KERNELTUPLE(XRNTuple, HSum); -DECLARE_KERNELTUPLE(XRNSTuple, StrideSum); +DECLARE_KERNELTUPLE(XRNSTuple, StrideASum); typedef struct { void* gates; // gates: x_ch, x_ih, x_fh, x_oh diff --git a/paddle/fluid/operators/jit/more/mix/mix.cc b/paddle/fluid/operators/jit/more/mix/mix.cc index 58a44d4b55..463e45f6ce 100644 --- a/paddle/fluid/operators/jit/more/mix/mix.cc +++ b/paddle/fluid/operators/jit/more/mix/mix.cc @@ -54,7 +54,7 @@ void Softmax(const T* x, T* y, int n, int bs, int m) { auto compute_hmax = KernelFuncs, CPUPlace>::Cache().At(n); auto compute_hsum = KernelFuncs, CPUPlace>::Cache().At(n); auto compute_vscal = KernelFuncs, CPUPlace>::Cache().At(n); - auto compute_stridesum = KernelFuncs, CPUPlace>::Cache().At(n); + auto compute_stridesum = KernelFuncs, CPUPlace>::Cache().At(n); auto compute_stridescal = KernelFuncs, CPUPlace>::Cache().At(n); auto compute_vaddbias = KernelFuncs, CPUPlace>::Cache().At(n); diff --git a/paddle/fluid/operators/jit/more/mkl/mkl.cc b/paddle/fluid/operators/jit/more/mkl/mkl.cc index 2828d75815..9e21e2b8d3 100644 --- a/paddle/fluid/operators/jit/more/mkl/mkl.cc +++ b/paddle/fluid/operators/jit/more/mkl/mkl.cc @@ -147,12 +147,12 @@ void ASum(const double* x, double* res, int n) { } template <> -void StrideSum(const float* x, float* res, int n, int stride) { +void StrideASum(const float* x, float* res, int n, int stride) { res[0] = platform::dynload::cblas_sasum(n, x, stride); } template <> -void StrideSum(const double* x, double* res, int n, int stride) { +void StrideASum(const double* x, double* res, int n, int stride) { res[0] = platform::dynload::cblas_dasum(n, x, stride); } @@ -174,7 +174,7 @@ bool VScalKernel::CanBeUsed(const int& d) const { template <> bool StrideScalKernel::CanBeUsed(const int& d) const { - return platform::MayIUse(platform::avx512f) && d > 512; + return true; } template <> diff --git a/paddle/fluid/operators/jit/more/mkl/mkl.h b/paddle/fluid/operators/jit/more/mkl/mkl.h index 1e974c095f..2f135f9e7a 100644 --- a/paddle/fluid/operators/jit/more/mkl/mkl.h +++ b/paddle/fluid/operators/jit/more/mkl/mkl.h @@ -129,7 +129,7 @@ template void ASum(const T* x, T* res, int n); template -void StrideSum(const T* x, T* res, int n, int stride); +void StrideASum(const T* x, T* res, int n, int stride); template void StrideScal(const T* a, const T* x, T* y, int n, int stride); @@ -155,7 +155,7 @@ void Softmax(const T* x, T* y, int n, int bs, int m=1) { VScal(&sum, &y[i * n], &y[i * n], n); } else { for (int j = 0; j < m; ++j) { - StrideSum(&y[i * n + j], &sum, n/m, m); + StrideASum(&y[i * n + j], &sum, n/m, m); sum = static_cast(1) / sum; StrideScal(&sum, &y[i * n + j], &y[i * n + j], n/m, m); } diff --git a/paddle/fluid/operators/jit/refer/CMakeLists.txt b/paddle/fluid/operators/jit/refer/CMakeLists.txt index 9a39809c93..7133f59662 100644 --- a/paddle/fluid/operators/jit/refer/CMakeLists.txt +++ b/paddle/fluid/operators/jit/refer/CMakeLists.txt @@ -33,7 +33,7 @@ USE_JITKERNEL_REFER(kMatMul) USE_JITKERNEL_REFER(kVSquare) USE_JITKERNEL_REFER(kHSum) USE_JITKERNEL_REFER(kHMax) -USE_JITKERNEL_REFER(kStrideSum) +USE_JITKERNEL_REFER(kStrideASum) USE_JITKERNEL_REFER(kSoftmax) USE_JITKERNEL_REFER(kEmbSeqPool) USE_JITKERNEL_REFER(kSgd) diff --git a/paddle/fluid/operators/jit/refer/refer.cc b/paddle/fluid/operators/jit/refer/refer.cc index 704124e805..460cb6c580 100644 --- a/paddle/fluid/operators/jit/refer/refer.cc +++ b/paddle/fluid/operators/jit/refer/refer.cc @@ -52,7 +52,7 @@ REGISTER_REFER_KERNEL(SeqPool); REGISTER_REFER_KERNEL(MatMul); REGISTER_REFER_KERNEL(HMax); REGISTER_REFER_KERNEL(HSum); -REGISTER_REFER_KERNEL(StrideSum); +REGISTER_REFER_KERNEL(StrideASum); REGISTER_REFER_KERNEL(Softmax); REGISTER_REFER_KERNEL(EmbSeqPool); REGISTER_REFER_KERNEL(Sgd); diff --git a/paddle/fluid/operators/jit/refer/refer.h b/paddle/fluid/operators/jit/refer/refer.h index dee9245524..e3387f60a6 100644 --- a/paddle/fluid/operators/jit/refer/refer.h +++ b/paddle/fluid/operators/jit/refer/refer.h @@ -412,10 +412,10 @@ void HSum(const T* x, T* res, int n) { } template -void StrideSum(const T* x, T* res, int n, int stride) { +void StrideASum(const T* x, T* res, int n, int stride) { res[0] = x[0]; for (int i = stride; i < n; i+=stride) { - res[0] += x[i]; + res[0] += std::abs(x[i]); } } @@ -442,7 +442,7 @@ void Softmax(const T* x, T* y, int n, int bs = 1, int m = 1) { VScal(&scalar, y, y, n); } else { for (int j = 0; j < m; j++) { - StrideSum(&y[j], &scalar, n, m); + StrideASum(&y[j], &scalar, n, m); scalar = static_cast(1) / scalar; StrideScal(&scalar, &y[j], &y[j], n, m); } @@ -554,7 +554,7 @@ DECLARE_REFER_KERNEL(GRUHtPart2); DECLARE_REFER_KERNEL(HMax); DECLARE_REFER_KERNEL(HSum); -DECLARE_REFER_KERNEL(StrideSum); +DECLARE_REFER_KERNEL(StrideASum); // others DECLARE_REFER_KERNEL(CRFDecoding); diff --git a/paddle/fluid/operators/jit/test.cc b/paddle/fluid/operators/jit/test.cc index 93a448166f..c47ec01d3e 100644 --- a/paddle/fluid/operators/jit/test.cc +++ b/paddle/fluid/operators/jit/test.cc @@ -727,6 +727,7 @@ void TestKernelSoftmax() { if (m > n || n % m != 0) { continue; } + VLOG(10) << "Softmax: " << bs << ", " << n << ", " << m; auto ref = jit::GetReferFunc(); EXPECT_TRUE(ref != nullptr); std::vector x(bs * n), y(bs * n); -- GitLab