jit_code.cc 6.7 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
  while (rest > 0) {
T
tensor-tang 已提交
63
    int block = XMM_FLOAT_BLOCK;
T
tensor-tang 已提交
64
    if (rest >= 4) {
T
tensor-tang 已提交
65
      block = 4;
T
tensor-tang 已提交
66 67 68 69 70 71 72
      if (scalar_index_ != 1) {
        vmovups(xmm_src1, ptr[param1 + offset]);
      }
      if (scalar_index_ != 2) {
        vmovups(xmm_src2, ptr[param2 + offset]);
      }
    } else if (rest >= 2) {
T
tensor-tang 已提交
73
      block = 2;
T
tensor-tang 已提交
74 75 76 77 78 79 80
      if (scalar_index_ != 1) {
        vmovq(xmm_src1, ptr[param1 + offset]);
      }
      if (scalar_index_ != 2) {
        vmovq(xmm_src2, ptr[param2 + offset]);
      }
    } else {
T
tensor-tang 已提交
81
      block = 1;
T
tensor-tang 已提交
82 83 84 85 86 87
      if (scalar_index_ != 1) {
        vmovss(xmm_src1, ptr[param1 + offset]);
      }
      if (scalar_index_ != 2) {
        vmovss(xmm_src2, ptr[param2 + offset]);
      }
T
tensor-tang 已提交
88
    }
T
tensor-tang 已提交
89 90 91 92 93 94 95 96 97
    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 已提交
98
    }
T
tensor-tang 已提交
99 100 101
    if (with_relu_) {
      vmaxps(xmm_dst, xmm_zero, xmm_dst);
    }
T
tensor-tang 已提交
102 103 104 105 106 107
    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 已提交
108
    }
T
tensor-tang 已提交
109 110
    offset += sizeof(float) * block;
    rest -= block;
T
tensor-tang 已提交
111 112 113
  }
  ret();
}
T
tensor-tang 已提交
114

T
tensor-tang 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
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 已提交
132

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

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

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

213 214 215 216 217
}  // namespace gen
}  // namespace jitkernel
}  // namespace math
}  // namespace operators
}  // namespace paddle