From fd0a954fbf081021b6b71c03d274ee3ea870ec6c Mon Sep 17 00:00:00 2001 From: tensor-tang Date: Fri, 14 Dec 2018 11:47:43 +0000 Subject: [PATCH] enable blas jitcode vmul, vadd, vaddrelu, vscal and vaddbias --- paddle/fluid/operators/jit/gen/CMakeLists.txt | 5 +++ paddle/fluid/operators/jit/gen/blas.cc | 38 +++++++++++++------ paddle/fluid/operators/jit/gen/blas.h | 25 +++++++++--- 3 files changed, 52 insertions(+), 16 deletions(-) diff --git a/paddle/fluid/operators/jit/gen/CMakeLists.txt b/paddle/fluid/operators/jit/gen/CMakeLists.txt index 98d9231fa..ef74a7118 100644 --- a/paddle/fluid/operators/jit/gen/CMakeLists.txt +++ b/paddle/fluid/operators/jit/gen/CMakeLists.txt @@ -10,3 +10,8 @@ endfunction() # use gen jitcode kernel by name USE_JITKERNEL_GEN(vmul) +USE_JITKERNEL_GEN(vadd) +#USE_JITKERNEL_GEN(vsub) # TODO(TJ): enable me +USE_JITKERNEL_GEN(vaddrelu) +USE_JITKERNEL_GEN(vscal) +USE_JITKERNEL_GEN(vaddbias) diff --git a/paddle/fluid/operators/jit/gen/blas.cc b/paddle/fluid/operators/jit/gen/blas.cc index 3e5ce5406..b24f44c9f 100644 --- a/paddle/fluid/operators/jit/gen/blas.cc +++ b/paddle/fluid/operators/jit/gen/blas.cc @@ -104,18 +104,28 @@ void VXXJitCode::genCode() { ret(); } -class VMulCreator : public JitCodeCreator { - public: - bool UseMe(const int& attr) const override { - return platform::MayIUse(platform::avx); +#define DECLARE_BLAS_CREATOR(name) \ + class name##Creator : public JitCodeCreator { \ + public: \ + bool UseMe(const int& attr) const override { \ + return platform::MayIUse(platform::avx); \ + } \ + size_t CodeSize(const int& d) const override { \ + return 96 + d / YMM_FLOAT_BLOCK * 4 * 8; \ + } \ + std::unique_ptr CreateJitCode(const int& attr) const override { \ + return make_unique(attr, CodeSize(attr)); \ + } \ } - size_t CodeSize(const int& d) const override { - return 96 + d / YMM_FLOAT_BLOCK * 4 * 8; - } - std::unique_ptr CreateJitCode(const int& attr) const override { - return make_unique(attr, CodeSize(attr)); - } -}; + +DECLARE_BLAS_CREATOR(VMul); +DECLARE_BLAS_CREATOR(VAdd); +DECLARE_BLAS_CREATOR(VSub); +DECLARE_BLAS_CREATOR(VAddRelu); +DECLARE_BLAS_CREATOR(VScal); +DECLARE_BLAS_CREATOR(VAddBias); + +#undef DECLARE_BLAS_CREATOR } // namespace gen } // namespace jit @@ -125,3 +135,9 @@ class VMulCreator : public JitCodeCreator { namespace gen = paddle::operators::jit::gen; REGISTER_JITKERNEL_GEN(vmul, gen::VMulCreator); +REGISTER_JITKERNEL_GEN(vadd, gen::VAddCreator); +// TODO(TJ): enable sub +// REGISTER_JITKERNEL_GEN(vsub, gen::VSubCreator); +REGISTER_JITKERNEL_GEN(vaddrelu, gen::VAddReluCreator); +REGISTER_JITKERNEL_GEN(vscal, gen::VScalCreator); +REGISTER_JITKERNEL_GEN(vaddbias, gen::VAddBiasCreator); diff --git a/paddle/fluid/operators/jit/gen/blas.h b/paddle/fluid/operators/jit/gen/blas.h index 60f328056..5a2192052 100644 --- a/paddle/fluid/operators/jit/gen/blas.h +++ b/paddle/fluid/operators/jit/gen/blas.h @@ -15,6 +15,7 @@ #pragma once #include +#include "glog/logging.h" #include "paddle/fluid/operators/jit/gen/jitcode.h" namespace paddle { @@ -33,6 +34,9 @@ class VXXJitCode : public JitCode { type_(type), scalar_index_(scalar_index), with_relu_(with_relu) { + if (!(type_ == operand_type::mul || type_ == operand_type::add)) { + LOG(FATAL) << "Do not support this operand type: " << type_; + } this->genCode(); } @@ -78,11 +82,22 @@ class VXXJitCode : public JitCode { ymm_t ymm_zero = ymm_t(3); }; -class VMulJitCode : public VXXJitCode { - public: - explicit VMulJitCode(int d, size_t code_size, void* code_ptr = nullptr) - : VXXJitCode(d, operand_type::mul, 0, false, code_size, code_ptr) {} -}; +#define DECLARE_BLAS_JITCODE(name, op_type, scalar_idx, with_relu) \ + class name##JitCode : public VXXJitCode { \ + public: \ + explicit name##JitCode(int d, size_t code_size, void* code_ptr = nullptr) \ + : VXXJitCode(d, op_type, scalar_idx, with_relu, code_size, code_ptr) { \ + } \ + }; + +DECLARE_BLAS_JITCODE(VMul, operand_type::mul, 0, false); +DECLARE_BLAS_JITCODE(VAdd, operand_type::add, 0, false); +DECLARE_BLAS_JITCODE(VSub, operand_type::sub, 0, false); +DECLARE_BLAS_JITCODE(VAddRelu, operand_type::add, 0, true); +DECLARE_BLAS_JITCODE(VScal, operand_type::mul, 1, false); +DECLARE_BLAS_JITCODE(VAddBias, operand_type::add, 1, false); + +#undef DECLARE_BLAS_JITCODE } // namespace gen } // namespace jit -- GitLab