jit_code.cc 6.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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_code.h"
T
tensor-tang 已提交
16
#include "paddle/fluid/operators/math/jit_kernel.h"  // TODO(TJ): remove me
17 18 19 20 21 22 23 24 25

namespace paddle {
namespace operators {
namespace math {
namespace jitkernel {
namespace gen {

using namespace platform::jit;  // NOLINT

T
tensor-tang 已提交
26
bool VXXJitCode::init(int d, int scalar_index) {
27 28
  // It's not necessary to use avx512 since it would slow down the frequency
  // and this kernel is not compute bound.
T
tensor-tang 已提交
29
  return MayIUse(avx) && scalar_index >= 0 && scalar_index <= 2;
30 31
}

T
tensor-tang 已提交
32
void VXXJitCode::generate() {
T
tensor-tang 已提交
33
  // do not need push stack, and do not need save avx512reg if do not use avx512
T
tensor-tang 已提交
34
  int offset = 0;
T
tensor-tang 已提交
35 36 37
  if (with_relu_) {
    vxorps(ymm_zero, ymm_zero, ymm_zero);
  }
T
tensor-tang 已提交
38 39 40 41 42
  if (scalar_index_ == 1) {
    vbroadcastss(ymm_src1, ptr[param1]);
  } else if (scalar_index_ == 2) {
    vbroadcastss(ymm_src2, ptr[param2]);
  }
43
  for (int i = 0; i < num_ / YMM_FLOAT_BLOCK; ++i) {
T
tensor-tang 已提交
44 45 46 47 48 49
    if (scalar_index_ != 1) {
      vmovups(ymm_src1, ptr[param1 + offset]);
    }
    if (scalar_index_ != 2) {
      vmovups(ymm_src2, ptr[param2 + offset]);
    }
T
tensor-tang 已提交
50 51 52 53 54
    if (type_ == operand_type::mul) {
      vmulps(ymm_dst, ymm_src1, ymm_src2);
    } else if (type_ == operand_type::add) {
      vaddps(ymm_dst, ymm_src1, ymm_src2);
    }
T
tensor-tang 已提交
55 56 57
    if (with_relu_) {
      vmaxps(ymm_dst, ymm_zero, ymm_dst);
    }
T
tensor-tang 已提交
58
    vmovups(ptr[param3 + offset], ymm_dst);
59
    offset += sizeof(float) * YMM_FLOAT_BLOCK;
T
tensor-tang 已提交
60
  }
61
  int rest = num_ % YMM_FLOAT_BLOCK;
T
tensor-tang 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  int block = XMM_FLOAT_BLOCK;
  while (rest > 0) {
    if (rest >= 4) {
      if (scalar_index_ != 1) {
        vmovups(xmm_src1, ptr[param1 + offset]);
      }
      if (scalar_index_ != 2) {
        vmovups(xmm_src2, ptr[param2 + offset]);
      }
    } else if (rest >= 2) {
      if (scalar_index_ != 1) {
        vmovq(xmm_src1, ptr[param1 + offset]);
      }
      if (scalar_index_ != 2) {
        vmovq(xmm_src2, ptr[param2 + offset]);
      }
    } else {
      if (scalar_index_ != 1) {
        vmovss(xmm_src1, ptr[param1 + offset]);
      }
      if (scalar_index_ != 2) {
        vmovss(xmm_src2, ptr[param2 + offset]);
      }
T
tensor-tang 已提交
85
    }
T
tensor-tang 已提交
86 87 88 89 90 91 92 93 94
    switch (type_) {
      case operand_type::mul:
        vmulps(xmm_dst, xmm_src1, xmm_src2);
        break;
      case operand_type::add:
        vaddps(xmm_dst, xmm_src1, xmm_src2);
        break;
      default:
        break;
T
tensor-tang 已提交
95
    }
T
tensor-tang 已提交
96 97 98
    if (with_relu_) {
      vmaxps(xmm_dst, xmm_zero, xmm_dst);
    }
T
tensor-tang 已提交
99 100 101 102 103 104
    if (rest >= 4) {
      vmovups(ptr[param3 + offset], xmm_dst);
    } else if (rest >= 2) {
      vmovq(ptr[param3 + offset], xmm_dst);
    } else {
      vmovss(ptr[param3 + offset], xmm_dst);
T
tensor-tang 已提交
105
    }
T
tensor-tang 已提交
106 107 108
    offset += sizeof(float) * block;
    rest -= block;
    block /= 2;
T
tensor-tang 已提交
109 110 111
  }
  ret();
}
T
tensor-tang 已提交
112

T
tensor-tang 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
const float exp_float_consts[] ALIGN32 = {REPEAT_8TIMES(1.f),
                                          REPEAT_8TIMES(2.f),
                                          REPEAT_8TIMES(0.5f),
                                          REPEAT_8TIMES(EXP_HIG),
                                          REPEAT_8TIMES(EXP_LOW),
                                          REPEAT_8TIMES(CEPHES_LOG2EF),
                                          REPEAT_8TIMES(CEPHES_EXP_C1),
                                          REPEAT_8TIMES(CEPHES_EXP_C2),
                                          REPEAT_8TIMES(CEPHES_EXP_P0),
                                          REPEAT_8TIMES(CEPHES_EXP_P1),
                                          REPEAT_8TIMES(CEPHES_EXP_P2),
                                          REPEAT_8TIMES(CEPHES_EXP_P3),
                                          REPEAT_8TIMES(CEPHES_EXP_P4),
                                          REPEAT_8TIMES(CEPHES_EXP_P5),
                                          REPEAT_8TIMES(EXP_MAX_INPUT),
                                          REPEAT_8TIMES(SIGMOID_THRESHOLD_MAX),
                                          REPEAT_8TIMES(SIGMOID_THRESHOLD_MIN)};
T
tensor-tang 已提交
130

T
tensor-tang 已提交
131 132
const int exp_int_0x7f[] ALIGN32 = {REPEAT_8TIMES(0x7f)};
int g_tmp_mem[16] ALIGN32 = {0};
T
tensor-tang 已提交
133

134
bool VActJitCode::init(int d, operand_type type) {
T
tensor-tang 已提交
135 136
  // TODO(TJ): implement avx512, avx_exp is slower than mkl when d >= 256
  return MayIUse(avx);
T
tensor-tang 已提交
137 138
}

139 140 141 142 143 144
void VActJitCode::generate() {
  xmm_t xmm_zero = xmm_t(2);
  ymm_t ymm_zero = ymm_t(2);
  if (type_ == operand_type::relu) {
    vxorps(ymm_zero, ymm_zero, ymm_zero);
  }
T
tensor-tang 已提交
145
  int offset = 0;
146
  for (int i = 0; i < num_ / YMM_FLOAT_BLOCK; ++i) {
147 148 149
    vmovups(ymm_src, ptr[param1 + offset]);
    switch (type_) {
      case operand_type::relu:
T
tensor-tang 已提交
150
        relu_jmm<ymm_t>(ymm_dst, ymm_src, ymm_zero);
151 152
        break;
      case operand_type::exp:
T
tensor-tang 已提交
153
        exp_jmm<ymm_t>(ymm_dst, ymm_src, 2, 3, 4, 5);
154 155
        break;
      case operand_type::sigmoid:
T
tensor-tang 已提交
156
        sigmoid_jmm<ymm_t>(ymm_dst, ymm_src, 2, 3, 4, 5);
157 158
        break;
      case operand_type::tanh:
T
tensor-tang 已提交
159
        tanh_jmm<ymm_t>(ymm_dst, ymm_src, 2, 3, 4, 5);
160 161 162 163 164 165 166
        break;
      case operand_type::identity:
        break;
      default:
        break;
    }
    vmovups(ptr[param2 + offset], ymm_dst);
167
    offset += sizeof(float) * YMM_FLOAT_BLOCK;
168
  }
169
  int rest = num_ % YMM_FLOAT_BLOCK;
T
tensor-tang 已提交
170 171 172 173 174 175 176 177
  int block = XMM_FLOAT_BLOCK;
  while (rest > 0) {
    if (rest >= 4) {
      vmovups(xmm_src, ptr[param1 + offset]);
    } else if (rest >= 2) {
      vmovq(xmm_src, ptr[param1 + offset]);
    } else {
      vmovss(xmm_src, ptr[param1 + offset]);
T
tensor-tang 已提交
178 179 180
    }
    switch (type_) {
      case operand_type::relu:
T
tensor-tang 已提交
181
        relu_jmm<xmm_t>(xmm_dst, xmm_src, xmm_zero);
T
tensor-tang 已提交
182 183
        break;
      case operand_type::exp:
T
tensor-tang 已提交
184
        exp_jmm<xmm_t>(xmm_dst, xmm_src, 2, 3, 4, 5);
T
tensor-tang 已提交
185
        break;
T
tensor-tang 已提交
186 187 188 189 190 191
      case operand_type::sigmoid:
        sigmoid_jmm<xmm_t>(xmm_dst, xmm_src, 2, 3, 4, 5);
        break;
      case operand_type::tanh:
        tanh_jmm<xmm_t>(xmm_dst, xmm_src, 2, 3, 4, 5);
        break;
T
tensor-tang 已提交
192 193 194
      default:
        break;
    }
T
tensor-tang 已提交
195 196 197 198 199 200
    if (rest >= 4) {
      vmovups(ptr[param2 + offset], xmm_dst);
    } else if (rest >= 2) {
      vmovq(ptr[param2 + offset], xmm_dst);
    } else {
      vmovss(ptr[param2 + offset], xmm_dst);
T
tensor-tang 已提交
201
    }
T
tensor-tang 已提交
202 203 204
    offset += sizeof(float) * block;
    rest -= block;
    block /= 2;
205
  }
T
tensor-tang 已提交
206 207 208
  ret();
}

209 210 211 212 213
}  // namespace gen
}  // namespace jitkernel
}  // namespace math
}  // namespace operators
}  // namespace paddle