Skip to content

Commit d55481c

Browse files
authored
Merge pull request #14241 from tensor-tang/refine/jit/vmulcode
Refine/jit/vmulcode
2 parents d277a2e + 8465e78 commit d55481c

File tree

4 files changed

+39
-21
lines changed

4 files changed

+39
-21
lines changed

paddle/fluid/operators/math/jit_code.cc

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,43 @@ namespace gen {
2525
using namespace platform::jit; // NOLINT
2626

2727
bool VMulJitCode::init(int d) {
28-
// TODO(TJ): maybe one AVX is enough, AVX above would slow down freq
29-
// try more with avx2 or avx512
30-
if (MayIUse(avx) || MayIUse(avx2)) {
31-
return d % AVX_FLOAT_BLOCK == 0;
32-
} else {
33-
return false;
34-
}
28+
// It's not necessary to use avx512 since it would slow down the frequency
29+
// and this kernel is not compute bound.
30+
return MayIUse(avx);
3531
}
3632

3733
void VMulJitCode::generate() {
3834
// do not need push stack, and do not need save avx512reg if do not use avx512
39-
int stride = sizeof(float) * AVX_FLOAT_BLOCK;
35+
int offset = 0;
4036
for (int i = 0; i < num_ / AVX_FLOAT_BLOCK; ++i) {
41-
vmovups(ymm_src1, ptr[param1 + i * stride]);
42-
vmovups(ymm_src2, ptr[param2 + i * stride]);
37+
vmovups(ymm_src1, ptr[param1 + offset]);
38+
vmovups(ymm_src2, ptr[param2 + offset]);
4339
vmulps(ymm_dst, ymm_src1, ymm_src2);
44-
vmovups(ptr[param3 + stride * i], ymm_dst);
40+
vmovups(ptr[param3 + offset], ymm_dst);
41+
offset += sizeof(float) * AVX_FLOAT_BLOCK;
42+
}
43+
int rest = num_ % AVX_FLOAT_BLOCK;
44+
if (rest >= 4) {
45+
vmovups(xmm_src1, ptr[param1 + offset]);
46+
vmovups(xmm_src2, ptr[param2 + offset]);
47+
vmulps(xmm_dst, xmm_src1, xmm_src2);
48+
vmovups(ptr[param3 + offset], xmm_dst);
49+
offset += sizeof(float) * 4;
50+
rest -= 4;
51+
}
52+
if (rest >= 2) {
53+
vmovq(xmm_src1, ptr[param1 + offset]);
54+
vmovq(xmm_src2, ptr[param2 + offset]);
55+
vmulps(xmm_dst, xmm_src1, xmm_src2);
56+
vmovq(ptr[param3 + offset], xmm_dst);
57+
offset += sizeof(float) * 2;
58+
rest -= 2;
59+
}
60+
if (rest > 0) {
61+
vmovss(xmm_src1, ptr[param1 + offset]);
62+
vmovss(xmm_src2, ptr[param2 + offset]);
63+
vmulss(xmm_dst, xmm_src1, xmm_src2);
64+
vmovss(ptr[param3 + offset], xmm_dst);
4565
}
4666
ret();
4767
}

paddle/fluid/operators/math/jit_code.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,12 @@ class VMulJitCode : public JitCode {
4545
reg64_t param3{abi_param3};
4646

4747
xmm_t xmm_src1 = xmm_t(0);
48-
ymm_t ymm_src1 = ymm_t(0);
49-
zmm_t zmm_src1 = zmm_t(0);
5048
xmm_t xmm_src2 = xmm_t(1);
51-
ymm_t ymm_src2 = ymm_t(1);
52-
zmm_t zmm_src2 = zmm_t(1);
53-
5449
xmm_t xmm_dst = xmm_t(2);
50+
51+
ymm_t ymm_src1 = ymm_t(0);
52+
ymm_t ymm_src2 = ymm_t(1);
5553
ymm_t ymm_dst = ymm_t(2);
56-
zmm_t zmm_dst = zmm_t(2);
5754
};
5855

5956
} // namespace gen

paddle/fluid/operators/math/jit_kernel_blas.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ class VMulKernelImpl : public VMulKernel<T> {
6565

6666
explicit VMulKernelImpl(int d) : VMulKernel<T>() {
6767
if (useJIT(d)) {
68-
constexpr size_t sz = 256 * 1024; // TODO(TJ): should be related with d
69-
jitcode_.reset(new gen::VMulJitCode(d, sz));
68+
// roughly estimate the size of code
69+
size_t sz = 96 + d / AVX_FLOAT_BLOCK * 4 * 8;
70+
jitcode_.reset(new gen::VMulJitCode(d, sz > 4096 ? sz : 4096));
7071
this->Compute =
7172
jitcode_->getCode<void (*)(const T*, const T*, T*, int)>();
7273
return;

paddle/fluid/operators/math/jit_kernel_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ void vmul_mkl(const int n, const float* x, const float* y, float* z) {
578578

579579
TEST(JitKernel, vmul) {
580580
namespace jit = paddle::operators::math::jitkernel;
581-
for (int d : {7, 8, 15, 16, 30, 256, 512, 1000, 1024}) {
581+
for (int d : {7, 8, 15, 16, 20, 30, 256, 512, 1000, 1024}) {
582582
std::vector<float> x(d), y(d);
583583
std::vector<float> zref(d), ztgt(d);
584584
RandomVec<float>(d, x.data());
@@ -800,7 +800,7 @@ TEST(JitKernel, pool) {
800800
EXPECT_TRUE(std::dynamic_pointer_cast<const jit::Kernel>(pvmul_f) !=
801801
std::dynamic_pointer_cast<const jit::Kernel>(pvmul_d));
802802

803-
const auto& pvmul_from_key = jit::KernelPool::Instance().Get("vmulfany");
803+
const auto& pvmul_from_key = jit::KernelPool::Instance().Get("vmulfjit4");
804804
EXPECT_EQ(pvmul_f, pvmul_from_key);
805805
const auto& pvmul_from_key2 = jit::KernelPool::Instance().Get("vmulfjit");
806806
EXPECT_TRUE(pvmul_from_key2 == nullptr);

0 commit comments

Comments
 (0)