jit_kernel_crf_decode.cc 17.9 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* 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 <limits>
#include <string>
#include "paddle/fluid/operators/math/jit_kernel_macro.h"
19 20 21
#ifdef __AVX__
#include <immintrin.h>
#endif
T
tensor-tang 已提交
22 23 24 25 26 27 28

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

/* CRF Decode JitKernel */
T
tensor-tang 已提交
29
template <typename T, platform::cpu_isa_t isa, jit_block>
T
tensor-tang 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
class CRFDecodeKernelImpl : public CRFDecodeKernel<T> {
 public:
  explicit CRFDecodeKernelImpl(int tag_num) : CRFDecodeKernel<T>() {
    this->num_ = tag_num;
  }
  void Compute(const int seq_len, const T* x, const T* w, T* alpha,
               int* track) const override {
    constexpr int state_trans_base_idx = 2;
    for (int i = 0; i < this->num_; ++i) {
      alpha[i] = w[i] + x[i];
    }
    for (int k = 1; k < seq_len; ++k) {
      for (int i = 0; i < this->num_; ++i) {
        T max_score = -std::numeric_limits<T>::max();
        int max_j = 0;
        for (int j = 0; j < this->num_; ++j) {
          T score = alpha[(k - 1) * this->num_ + j] +
                    w[(j + state_trans_base_idx) * this->num_ + i];
          if (score > max_score) {
            max_score = score;
            max_j = j;
          }
        }
        alpha[k * this->num_ + i] = max_score + x[k * this->num_ + i];
        track[k * this->num_ + i] = max_j;
      }
    }
  }
};

#define INIT_ALPHA(step_size)                                               \
  /* Setup the alpha initial value.*/                                       \
  int i_offset = 0;                                                         \
  int last_offset = this->rest_ - step_size;                                \
  for (int i = 0; i <= this->end_; ++i) {                                   \
    /* weights, input and alpha values. */                                  \
    __m256 w_content, x_content, alpha_content;                             \
    /* Load the relevant data into the variables from un-aligned address.*/ \
    w_content = _mm256_loadu_ps(w + i_offset);                              \
    x_content = _mm256_loadu_ps(x + i_offset);                              \
    alpha_content = _mm256_add_ps(w_content, x_content);                    \
    _mm256_storeu_ps(alpha + i_offset, alpha_content);                      \
    i_offset += step_size;                                                  \
    if (i == this->end_ - 1) {                                              \
      if (this->rest_ > 0) {                                                \
        i_offset += last_offset;                                            \
      } else {                                                              \
        break;                                                              \
      }                                                                     \
    }                                                                       \
  }

#define UPDATE_ALPHA(step_size)                                               \
  /* Update the alpha and track values. */                                    \
  __m256 x_content = _mm256_loadu_ps(x + seq_offset + this->num_ + j_offset); \
  max_score = _mm256_add_ps(max_score, x_content);                            \
  _mm256_storeu_ps(alpha + seq_offset + this->num_ + j_offset, max_score);    \
  _mm256_storeu_si256(                                                        \
      reinterpret_cast<__m256i*>(track + seq_offset + this->num_ + j_offset), \
      max_j);                                                                 \
  /* Calculate the offset of next step*/                                      \
  j_offset += step_size;                                                      \
  if (j == this->end_ - 1) {                                                  \
    if (this->rest_ > 0) {                                                    \
      j_offset += last_offset;                                                \
    } else {                                                                  \
      break;                                                                  \
    }                                                                         \
  }

#define INTRIAVX_FLOAT(block)                                                  \
  template <>                                                                  \
T
tensor-tang 已提交
102
  CRFDecodeKernelImpl<float, platform::avx, block>::CRFDecodeKernelImpl(       \
T
tensor-tang 已提交
103 104 105
      int tag_num)                                                             \
      : CRFDecodeKernel<float>() {                                             \
    this->num_ = tag_num;                                                      \
106 107
    this->end_ = this->num_ / YMM_FLOAT_BLOCK;                                 \
    this->rest_ = this->num_ % YMM_FLOAT_BLOCK;                                \
T
tensor-tang 已提交
108 109
  }                                                                            \
  template <>                                                                  \
T
tensor-tang 已提交
110
  void CRFDecodeKernelImpl<float, platform::avx, block>::Compute(              \
T
tensor-tang 已提交
111 112
      const int seq_len, const float* x, const float* w, float* alpha,         \
      int* track) const {                                                      \
113
    INIT_ALPHA(YMM_FLOAT_BLOCK)                                                \
T
tensor-tang 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    /* Use the column-major strategy to get the location of maximum score.*/   \
    int seq_offset = 0;                                                        \
    constexpr int state_trans_base_idx = 2;                                    \
    for (int k = 1; k < seq_len; ++k) {                                        \
      int j_offset = 0;                                                        \
      for (int j = 0; j <= this->end_; ++j) {                                  \
        /* Initialize the variables of maximum score and location.*/           \
        __m256 max_score = _mm256_set1_ps(-std::numeric_limits<float>::max()); \
        __m256i max_j = _mm256_set1_epi32(0);                                  \
        /* Calculate the offset of transition_weights.*/                       \
        int trans_offset = state_trans_base_idx * this->num_ + j_offset;       \
        for (int i = 0; i < this->num_; ++i) {                                 \
          /* Initalize the content of alpha variable with related offset.*/    \
          __m256 alpha_content = _mm256_broadcast_ss(alpha + seq_offset + i);  \
          /* Obtain the content of weights from un-aligned address.*/          \
          __m256 w_content = _mm256_loadu_ps(w + trans_offset);                \
          __m256 score_v = _mm256_add_ps(alpha_content, w_content);            \
          __m256 mask = _mm256_cmp_ps(score_v, max_score, _CMP_GT_OS);         \
          /* According to the mask value, update the index of the max_score.*/ \
          /* AVX instructions.*/                                               \
          __m128i lo_max_j = _mm256_extractf128_si256(max_j, 0);               \
          __m128i hi_max_j = _mm256_extractf128_si256(max_j, 1);               \
          __m128i lo_mask = _mm256_extractf128_si256((__m256i)mask, 0);        \
          __m128i hi_mask = _mm256_extractf128_si256((__m256i)mask, 1);        \
          lo_max_j = _mm_andnot_si128(lo_mask, lo_max_j);                      \
          hi_max_j = _mm_andnot_si128(hi_mask, hi_max_j);                      \
          lo_mask = _mm_and_si128(lo_mask, _mm_set1_epi32(i));                 \
          hi_mask = _mm_and_si128(hi_mask, _mm_set1_epi32(i));                 \
          lo_max_j = _mm_or_si128(lo_mask, lo_max_j);                          \
          hi_max_j = _mm_or_si128(hi_mask, hi_max_j);                          \
          max_j = _mm256_insertf128_si256(max_j, lo_max_j, 0);                 \
          max_j = _mm256_insertf128_si256(max_j, hi_max_j, 1);                 \
          /* AVX done*/                                                        \
          /* Update the max_score value.*/                                     \
          max_score = _mm256_max_ps(max_score, score_v);                       \
          trans_offset += this->num_;                                          \
        }                                                                      \
151
        UPDATE_ALPHA(YMM_FLOAT_BLOCK)                                          \
T
tensor-tang 已提交
152 153 154 155 156
      }                                                                        \
      seq_offset += this->num_;                                                \
    }                                                                          \
  }

T
tensor-tang 已提交
157
#define INTRIAVX2_FLOAT(isa, block)                                            \
T
tensor-tang 已提交
158
  template <>                                                                  \
T
tensor-tang 已提交
159
  CRFDecodeKernelImpl<float, isa, block>::CRFDecodeKernelImpl(int tag_num)     \
T
tensor-tang 已提交
160 161
      : CRFDecodeKernel<float>() {                                             \
    this->num_ = tag_num;                                                      \
162 163
    this->end_ = this->num_ / YMM_FLOAT_BLOCK;                                 \
    this->rest_ = this->num_ % YMM_FLOAT_BLOCK;                                \
T
tensor-tang 已提交
164 165
  }                                                                            \
  template <>                                                                  \
T
tensor-tang 已提交
166
  void CRFDecodeKernelImpl<float, isa, block>::Compute(                        \
T
tensor-tang 已提交
167 168
      const int seq_len, const float* x, const float* w, float* alpha,         \
      int* track) const {                                                      \
169
    INIT_ALPHA(YMM_FLOAT_BLOCK)                                                \
T
tensor-tang 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    /* Use the column-major strategy to get the location of maximum score.*/   \
    int seq_offset = 0;                                                        \
    constexpr int state_trans_base_idx = 2;                                    \
    for (int k = 1; k < seq_len; ++k) {                                        \
      int j_offset = 0;                                                        \
      for (int j = 0; j <= this->end_; ++j) {                                  \
        /* Initialize the variables of maximum score and location.*/           \
        __m256 max_score = _mm256_set1_ps(-std::numeric_limits<float>::max()); \
        __m256i max_j = _mm256_set1_epi32(0);                                  \
        /* Calculate the offset of transition_weights.*/                       \
        int trans_offset = state_trans_base_idx * this->num_ + j_offset;       \
        for (int i = 0; i < this->num_; ++i) {                                 \
          /* Initalize the content of alpha variable with related offset.*/    \
          __m256 alpha_content = _mm256_broadcast_ss(alpha + seq_offset + i);  \
          /* Obtain the content of weights from un-aligned address.*/          \
          __m256 w_content = _mm256_loadu_ps(w + trans_offset);                \
          __m256 score_v = _mm256_add_ps(alpha_content, w_content);            \
          __m256 mask = _mm256_cmp_ps(score_v, max_score, _CMP_GT_OS);         \
          /* According to the mask value, update the index of the max_score.*/ \
          /* AVX2 instructions.*/                                              \
          max_j = _mm256_or_si256(                                             \
              _mm256_andnot_si256((__m256i)mask, max_j),                       \
              _mm256_and_si256((__m256i)mask, _mm256_set1_epi32(i)));          \
          /* Update the max_score value.*/                                     \
          max_score = _mm256_max_ps(max_score, score_v);                       \
          trans_offset += this->num_;                                          \
        }                                                                      \
197
        UPDATE_ALPHA(YMM_FLOAT_BLOCK)                                          \
T
tensor-tang 已提交
198 199 200 201 202 203 204
      }                                                                        \
      seq_offset += this->num_;                                                \
    }                                                                          \
  }

#define INTRIAVX512_FLOAT(block)                                               \
  template <>                                                                  \
T
tensor-tang 已提交
205
  CRFDecodeKernelImpl<float, platform::avx512f, block>::CRFDecodeKernelImpl(   \
T
tensor-tang 已提交
206 207 208
      int tag_num)                                                             \
      : CRFDecodeKernel<float>() {                                             \
    this->num_ = tag_num;                                                      \
209 210
    this->end_ = this->num_ / ZMM_FLOAT_BLOCK;                                 \
    this->rest_ = this->num_ % ZMM_FLOAT_BLOCK;                                \
T
tensor-tang 已提交
211 212
  }                                                                            \
  template <>                                                                  \
T
tensor-tang 已提交
213
  void CRFDecodeKernelImpl<float, platform::avx512f, block>::Compute(          \
T
tensor-tang 已提交
214 215
      const int seq_len, const float* x, const float* w, float* alpha,         \
      int* track) const {                                                      \
216
    INIT_ALPHA(ZMM_FLOAT_BLOCK)                                                \
T
tensor-tang 已提交
217 218 219 220 221 222 223
    /* Use the column-major strategy to get the location of maximum score.*/   \
    int seq_offset = 0;                                                        \
    constexpr int state_trans_base_idx = 2;                                    \
    for (int k = 1; k < seq_len; ++k) {                                        \
      int j_offset = 0;                                                        \
      for (int j = 0; j <= this->end_; ++j) {                                  \
        /* Initialize the variables of maximum score and location.*/           \
T
tensor-tang 已提交
224
        __m512 max_score = _mm512_set1_ps(-std::numeric_limits<float>::max()); \
T
tensor-tang 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
        __m512i max_j = _mm512_setzero_si512();                                \
        /* Calculate the offset of transition_weights.*/                       \
        int trans_offset = state_trans_base_idx * this->num_ + j_offset;       \
        for (int i = 0; i < this->num_; ++i) {                                 \
          /* Initalize the content of alpha variable with related offset.*/    \
          __m512 alpha_content = _mm512_set1_ps(*(alpha + seq_offset + i));    \
          /* Obtain the content of weights from un-aligned address.*/          \
          __m512 w_content = _mm512_loadu_ps(w + trans_offset);                \
          __m512 score_v = _mm512_add_ps(alpha_content, w_content);            \
          __mmask16 mask = _mm512_cmp_ps_mask(score_v, max_score, _CMP_GT_OS); \
          /* AVX512 instructions.*/                                            \
          max_j = _mm512_mask_set1_epi32(max_j, mask, i);                      \
          /* Update the max_score value.*/                                     \
          max_score = _mm512_max_ps(max_score, score_v);                       \
          trans_offset += this->num_;                                          \
        }                                                                      \
        /* Update the alpha and track values.*/                                \
        __m512 x_content =                                                     \
            _mm512_loadu_ps(x + seq_offset + this->num_ + j_offset);           \
        max_score = _mm512_add_ps(max_score, x_content);                       \
T
tensor-tang 已提交
245
        _mm512_storeu_ps(alpha + seq_offset + this->num_ + j_offset,           \
T
tensor-tang 已提交
246 247 248 249 250
                         max_score);                                           \
        _mm512_storeu_si512(reinterpret_cast<__m512i*>(track + seq_offset +    \
                                                       this->num_ + j_offset), \
                            max_j);                                            \
        /* Calculate the offset of next step*/                                 \
251
        j_offset += ZMM_FLOAT_BLOCK;                                           \
T
tensor-tang 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
        if (j == this->end_ - 1) {                                             \
          if (this->rest_ > 0) {                                               \
            j_offset += last_offset;                                           \
          } else {                                                             \
            break;                                                             \
          }                                                                    \
        }                                                                      \
      }                                                                        \
      seq_offset += this->num_;                                                \
    }                                                                          \
  }

#ifdef __AVX__
INTRIAVX_FLOAT(kEQ8);
INTRIAVX_FLOAT(kGT8LT16);
INTRIAVX_FLOAT(kEQ16);
INTRIAVX_FLOAT(kGT16);
#endif
#ifdef __AVX2__
T
tensor-tang 已提交
271 272 273 274
INTRIAVX2_FLOAT(platform::avx2, kEQ8);
INTRIAVX2_FLOAT(platform::avx2, kGT8LT16);
INTRIAVX2_FLOAT(platform::avx2, kEQ16);
INTRIAVX2_FLOAT(platform::avx2, kGT16);
T
tensor-tang 已提交
275 276
#endif
#ifdef __AVX512F__
T
tensor-tang 已提交
277 278
INTRIAVX2_FLOAT(platform::avx512f, kEQ8);
INTRIAVX2_FLOAT(platform::avx512f, kGT8LT16);
T
tensor-tang 已提交
279 280 281 282 283 284 285 286 287 288
INTRIAVX512_FLOAT(kEQ16);
INTRIAVX512_FLOAT(kGT16);
#endif

#undef INTRIAVX512_FLOAT
#undef INTRIAVX2_FLOAT
#undef INTRIAVX_FLOAT
#undef INIT_ALPHA
#undef UPDATE_ALPHA

T
tensor-tang 已提交
289
REGISTER_JITKERNEL_DEPRECATED(crf_decode, CRFDecodeKernel);
T
tensor-tang 已提交
290 291 292 293 294

}  // namespace jitkernel
}  // namespace math
}  // namespace operators
}  // namespace paddle