jit_code.cc 8.4 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 135
bool VActJitCode::init(int d, operand_type type) {
  bool ok = MayIUse(avx);
T
tensor-tang 已提交
136 137
  if (type == operand_type::relu || type == operand_type::exp) {
    // TODO(TJ): implement avx512, avx_exp is slower than mkl when d >= 256
138 139
    return ok;
  } else {
T
tensor-tang 已提交
140
    // TODO(TJ): support more
141
    return ok && d % 8 == 0;
142
  }
T
tensor-tang 已提交
143 144
}

145 146 147 148
void VActJitCode::sigmoid_ymm(ymm_t& ymm_dst, ymm_t& ymm_src, int fx_idx,
                              int fy_idx, int mask_idx, int tmp_idx) {
  // y = 1 / (1 + e^-x)
  ymm_t ymm_tmp = ymm_t(tmp_idx);
T
tensor-tang 已提交
149 150 151 152 153 154 155 156 157
  reg64_t reg_ptr_global = rax;
  push(reg_ptr_global);
  mov(reg_ptr_global, reinterpret_cast<size_t>(exp_float_consts));
  vmovaps(ymm_tmp, ptr[reg_ptr_global + OFFSET_SIGMOID_MAX]);
  vminps(ymm_src, ymm_src, ymm_tmp);
  vmovaps(ymm_tmp, ptr[reg_ptr_global + OFFSET_SIGMOID_MIN]);
  vmaxps(ymm_src, ymm_src, ymm_tmp);
  vxorps(ymm_tmp, ymm_tmp, ymm_tmp);
  vsubps(ymm_src, ymm_tmp, ymm_src);
T
tensor-tang 已提交
158
  exp_jmm<ymm_t>(ymm_dst, ymm_src, fx_idx, fy_idx, mask_idx, tmp_idx);
T
tensor-tang 已提交
159 160 161 162 163 164
  vmovaps(ymm_tmp, ptr[reg_ptr_global + OFFSET_EXP_ONE]);
  vaddps(ymm_dst, ymm_dst, ymm_tmp);
  vdivps(ymm_dst, ymm_tmp, ymm_dst);
  pop(reg_ptr_global);
}

165 166
void VActJitCode::tanh_ymm(ymm_t& ymm_dst, ymm_t& ymm_src, int fx_idx,
                           int fy_idx, int mask_idx, int tmp_idx) {
T
tensor-tang 已提交
167
  // y = 2 / (1 + e^(-2x)) - 1
168 169
  ymm_t ymm_tmp = ymm_t(tmp_idx);
  ymm_t ymm_zero = ymm_t(mask_idx);
T
tensor-tang 已提交
170 171 172 173 174 175 176
  reg64_t reg_ptr_global = rax;
  push(reg_ptr_global);
  mov(reg_ptr_global, reinterpret_cast<size_t>(exp_float_consts));
  vmovaps(ymm_tmp, ptr[reg_ptr_global + OFFSET_EXP_TWO]);
  vxorps(ymm_zero, ymm_zero, ymm_zero);
  vsubps(ymm_tmp, ymm_zero, ymm_tmp);
  vmulps(ymm_src, ymm_src, ymm_tmp);
T
tensor-tang 已提交
177
  exp_jmm<ymm_t>(ymm_dst, ymm_src, fx_idx, fy_idx, mask_idx, tmp_idx);
T
tensor-tang 已提交
178 179 180 181 182 183 184 185 186
  vmovaps(ymm_tmp, ptr[reg_ptr_global + OFFSET_EXP_ONE]);
  vaddps(ymm_dst, ymm_dst, ymm_tmp);
  vmovaps(ymm_tmp, ptr[reg_ptr_global + OFFSET_EXP_TWO]);
  vdivps(ymm_dst, ymm_tmp, ymm_dst);
  vmovaps(ymm_tmp, ptr[reg_ptr_global + OFFSET_EXP_ONE]);
  vsubps(ymm_dst, ymm_dst, ymm_tmp);
  pop(reg_ptr_global);
}

187 188 189 190 191 192
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 已提交
193
  int offset = 0;
194
  for (int i = 0; i < num_ / YMM_FLOAT_BLOCK; ++i) {
195 196 197
    vmovups(ymm_src, ptr[param1 + offset]);
    switch (type_) {
      case operand_type::relu:
T
tensor-tang 已提交
198
        relu_jmm<ymm_t>(ymm_dst, ymm_src, ymm_zero);
199 200
        break;
      case operand_type::exp:
T
tensor-tang 已提交
201
        exp_jmm<ymm_t>(ymm_dst, ymm_src, 2, 3, 4, 5);
202 203 204 205 206 207 208 209 210 211 212 213 214
        break;
      case operand_type::sigmoid:
        sigmoid_ymm(ymm_dst, ymm_src, 2, 3, 4, 5);
        break;
      case operand_type::tanh:
        tanh_ymm(ymm_dst, ymm_src, 2, 3, 4, 5);
        break;
      case operand_type::identity:
        break;
      default:
        break;
    }
    vmovups(ptr[param2 + offset], ymm_dst);
215
    offset += sizeof(float) * YMM_FLOAT_BLOCK;
216
  }
T
tensor-tang 已提交
217
  if (type_ != operand_type::relu && type_ != operand_type::exp) {
218 219 220 221
    // TODO(TJ): remove me
    ret();
    return;
  }
222
  int rest = num_ % YMM_FLOAT_BLOCK;
T
tensor-tang 已提交
223 224 225 226 227 228 229 230
  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 已提交
231 232 233
    }
    switch (type_) {
      case operand_type::relu:
T
tensor-tang 已提交
234
        relu_jmm<xmm_t>(xmm_dst, xmm_src, xmm_zero);
T
tensor-tang 已提交
235 236
        break;
      case operand_type::exp:
T
tensor-tang 已提交
237
        exp_jmm<xmm_t>(xmm_dst, xmm_src, 2, 3, 4, 5);
T
tensor-tang 已提交
238 239 240 241
        break;
      default:
        break;
    }
T
tensor-tang 已提交
242 243 244 245 246 247
    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 已提交
248
    }
T
tensor-tang 已提交
249 250 251
    offset += sizeof(float) * block;
    rest -= block;
    block /= 2;
252
  }
T
tensor-tang 已提交
253 254 255
  ret();
}

256 257 258 259 260
}  // namespace gen
}  // namespace jitkernel
}  // namespace math
}  // namespace operators
}  // namespace paddle