/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/fluid/operators/math/jit_kernel.h" #include #include "paddle/fluid/operators/math/jit_kernel_macro.h" #ifdef PADDLE_WITH_MKLML #include "paddle/fluid/platform/dynload/mklml.h" #endif #ifdef __AVX__ #include #endif namespace paddle { namespace operators { namespace math { namespace jitkernel { namespace jit = platform::jit; /* VMUL JitKernel */ template class VMulKernelImpl : public VMulKernel { public: explicit VMulKernelImpl(int d) : VMulKernel() { this->num_ = d; } void Compute(const T* x, const T* y, T* z) const override { for (int i = 0; i < this->num_; ++i) { z[i] = x[i] * y[i]; } } }; #ifdef PADDLE_WITH_MKLML #define MKL_FLOAT(isa, block) \ template <> \ void VMulKernelImpl::Compute( \ const float* x, const float* y, float* z) const { \ platform::dynload::vsMul(this->num_, x, y, z); \ } #define MKL_DOUBLE(isa, block) \ template <> \ void VMulKernelImpl::Compute( \ const double* x, const double* y, double* z) const { \ platform::dynload::vdMul(this->num_, x, y, z); \ } FOR_EACH_ISA(MKL_FLOAT, kGT16); FOR_EACH_ISA_BLOCK(MKL_DOUBLE); #endif #define INTRI8_FLOAT(isa) \ template <> \ void VMulKernelImpl::Compute( \ const float* x, const float* y, float* z) const { \ __m256 tmpx, tmpy; \ tmpx = _mm256_loadu_ps(x); \ tmpy = _mm256_loadu_ps(y); \ tmpx = _mm256_mul_ps(tmpx, tmpy); \ _mm256_storeu_ps(z, tmpx); \ } // avx > for > mkl #ifdef __AVX__ INTRI8_FLOAT(jit::avx); #endif #ifdef __AVX2__ INTRI8_FLOAT(jit::avx2); #endif #ifdef __AVX512F__ INTRI8_FLOAT(jit::avx512f); #endif // TODO(TJ): eq16 test and complete avx512 #undef INTRI8_FLOAT #undef MKL_FLOAT #undef MKL_DOUBLE /* VADD JitKernel */ template class VAddKernelImpl : public VAddKernel { public: explicit VAddKernelImpl(int d) : VAddKernel() { this->num_ = d; } void Compute(const T* x, const T* y, T* z) const override { for (int i = 0; i < this->num_; ++i) { z[i] = x[i] + y[i]; } } }; #ifdef PADDLE_WITH_MKLML #define MKL_FLOAT(isa, block) \ template <> \ void VAddKernelImpl::Compute( \ const float* x, const float* y, float* z) const { \ platform::dynload::vsAdd(this->num_, x, y, z); \ } #define MKL_DOUBLE(isa, block) \ template <> \ void VAddKernelImpl::Compute( \ const double* x, const double* y, double* z) const { \ platform::dynload::vdAdd(this->num_, x, y, z); \ } FOR_EACH_ISA(MKL_FLOAT, kGT16); FOR_EACH_ISA_BLOCK(MKL_DOUBLE); #endif #define INTRI8_FLOAT(isa) \ template <> \ void VAddKernelImpl::Compute( \ const float* x, const float* y, float* z) const { \ __m256 tmpx, tmpy; \ tmpx = _mm256_loadu_ps(x); \ tmpy = _mm256_loadu_ps(y); \ tmpx = _mm256_add_ps(tmpx, tmpy); \ _mm256_storeu_ps(z, tmpx); \ } #ifdef __AVX__ INTRI8_FLOAT(jit::avx); #endif #ifdef __AVX2__ INTRI8_FLOAT(jit::avx2); #endif #ifdef __AVX512F__ INTRI8_FLOAT(jit::avx512f); #endif // TODO(TJ): eq16 test and complete avx512 #undef INTRI8_FLOAT #undef MKL_FLOAT #undef MKL_DOUBLE /* VSCAL JitKernel */ template class VScalKernelImpl : public VScalKernel { public: explicit VScalKernelImpl(int d) : VScalKernel() { this->num_ = d; } void Compute(const T a, const T* x, T* y) const override { for (int i = 0; i < this->num_; ++i) { y[i] = a * x[i]; } } void Compute(const T a, T* x) const override { for (int i = 0; i < this->num_; ++i) { x[i] = a * x[i]; } } }; #ifdef PADDLE_WITH_MKLML #define MKL_FLOAT(isa, block) \ template <> \ void VScalKernelImpl::Compute(const float a, float* x) \ const { \ platform::dynload::cblas_sscal(this->num_, a, x, 1); \ } #define MKL_DOUBLE(isa, block) \ template <> \ void VScalKernelImpl::Compute(const double a, double* x) \ const { \ platform::dynload::cblas_dscal(this->num_, a, x, 1); \ } FOR_EACH_ISA(MKL_FLOAT, kGT16); FOR_EACH_ISA_BLOCK(MKL_DOUBLE); #endif #define INTRI8_FLOAT(isa) \ template <> \ void VScalKernelImpl::Compute( \ const float a, const float* x, float* y) const { \ __m256 tmp; \ __m256 scalar = _mm256_set1_ps(a); \ tmp = _mm256_loadu_ps(x); \ tmp = _mm256_mul_ps(tmp, scalar); \ _mm256_storeu_ps(y, tmp); \ } #define INTRI8_INPLACE_FLOAT(isa) \ template <> \ void VScalKernelImpl::Compute(const float a, float* x) \ const { \ __m256 tmp; \ __m256 scalar = _mm256_set1_ps(a); \ tmp = _mm256_loadu_ps(x); \ tmp = _mm256_mul_ps(tmp, scalar); \ _mm256_storeu_ps(x, tmp); \ } #ifdef __AVX__ INTRI8_FLOAT(jit::avx); INTRI8_INPLACE_FLOAT(jit::avx); #endif #ifdef __AVX2__ INTRI8_FLOAT(jit::avx2); INTRI8_INPLACE_FLOAT(jit::avx2); #endif #ifdef __AVX512F__ INTRI8_FLOAT(jit::avx512f); INTRI8_INPLACE_FLOAT(jit::avx512f); #endif // TODO(TJ): eq16 test and complete avx512 #undef INTRI8_FLOAT #undef INTRI8_INPLACE_FLOAT #undef MKL_FLOAT #undef MKL_DOUBLE /* VAddBias JitKernel */ template class VAddBiasKernelImpl : public VAddBiasKernel { public: explicit VAddBiasKernelImpl(int d) : VAddBiasKernel() { this->num_ = d; } void Compute(const T a, const T* x, T* y) const override { for (int i = 0; i < this->num_; ++i) { y[i] = x[i] + a; } } }; #define INTRI8_FLOAT(isa) \ template <> \ void VAddBiasKernelImpl::Compute( \ const float a, const float* x, float* y) const { \ __m256 tmp = _mm256_loadu_ps(x); \ tmp = _mm256_add_ps(tmp, _mm256_set1_ps(a)); \ _mm256_storeu_ps(y, tmp); \ } #define INTRI16_FLOAT(isa) \ template <> \ void VAddBiasKernelImpl::Compute( \ const float a, const float* x, float* y) const { \ __m256 tmp0 = _mm256_loadu_ps(x); \ __m256 tmp1 = _mm256_loadu_ps(x + 8); \ tmp0 = _mm256_add_ps(tmp0, _mm256_set1_ps(a)); \ tmp1 = _mm256_add_ps(tmp1, _mm256_set1_ps(a)); \ _mm256_storeu_ps(y, tmp0); \ _mm256_storeu_ps(y + 8, tmp1); \ } #ifdef __AVX__ INTRI8_FLOAT(jit::avx); INTRI16_FLOAT(jit::avx); #endif #ifdef __AVX2__ INTRI8_FLOAT(jit::avx2); INTRI16_FLOAT(jit::avx2); #endif #ifdef __AVX512F__ INTRI8_FLOAT(jit::avx512f); INTRI16_FLOAT(jit::avx512f); #endif // TODO(TJ): eq16 test and complete avx512 #undef INTRI8_FLOAT #undef INTRI16_FLOAT #undef INTRI_GT8LT16_FLOAT #undef INTRI_GT16_FLOAT REGISTER_JITKERNEL(vmul, VMulKernel); REGISTER_JITKERNEL(vadd, VAddKernel); REGISTER_JITKERNEL(vscal, VScalKernel); REGISTER_JITKERNEL(vaddb, VAddBiasKernel); } // namespace jitkernel } // namespace math } // namespace operators } // namespace paddle