kernel_base.h 6.5 KB
Newer Older
T
tensor-tang 已提交
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. */

#pragma once
16
#include <cstdint>
17
#include "paddle/fluid/operators/jit/macro.h"
T
tensor-tang 已提交
18 19 20 21
#include "paddle/fluid/platform/macros.h"

namespace paddle {
namespace operators {
T
tensor-tang 已提交
22
namespace jit {
T
tensor-tang 已提交
23

24
typedef enum {
T
tensor-tang 已提交
25
  kNone = 0,
26 27 28
  // sort by alphabet
  kCRFDecoding = 1,
  kEmbSeqPool = 2,
T
tensor-tang 已提交
29 30 31
  kGRUH1,
  kGRUHtPart1,
  kGRUHtPart2,
32 33 34 35
  kHSum,  // horizontal max
  kHMax,  // horizontal sum
  kLSTMCtHt,
  kLSTMC1H1,
T
tensor-tang 已提交
36
  kLayerNorm,
37
  kMatMul,
T
tensor-tang 已提交
38
  kNCHW16CMulNC,
T
tensor-tang 已提交
39
  kSeqPool,
40
  kSoftmax,
41 42 43 44 45 46 47 48 49 50 51 52
  kVAdd,
  kVAddBias,
  kVAddRelu,
  kVExp,
  kVIdentity,
  kVMul,
  kVRelu,
  kVScal,
  kVSigmoid,
  kVSquare,
  kVSub,
  kVTanh,
53
} KernelType;
T
tensor-tang 已提交
54

55 56
typedef enum {
  kNonePoolType = 0,
T
tensor-tang 已提交
57
  kSum = 1,
58 59 60 61
  kAvg,
  kSqrt,
} SeqPoolType;

T
tensor-tang 已提交
62
template <typename T>
63
struct XYZNTuples {
T
tensor-tang 已提交
64 65 66 67 68
  typedef T data_type;
  typedef int attr_type;
  typedef void (*func_type)(const T*, const T*, T*, int);
};

69 70 71
template <typename T>
struct AXYNTuples : public XYZNTuples<T> {};

72 73 74 75 76 77 78
template <typename T>
struct XYNTuples {
  typedef T data_type;
  typedef int attr_type;
  typedef void (*func_type)(const T*, T*, int);
};

79 80 81 82
// x, return and int
template <typename T>
struct XRNTuples : public XYNTuples<T> {};

T
tensor-tang 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
typedef struct {
  void* gates;  // gates: x_ch, x_ih, x_fh, x_oh
  const void* ct_1;
  void* ct;
  void* ht;
  /* weight_peephole and checked data are only used in peephole*/
  const void* wp{nullptr};  //  W_ic, W_fc, W_oc
  void* checked{nullptr};   // size: 2 * d
} lstm_t;

typedef struct {
  void* gates;  // gates: {x_update, x_reset; x_state}
  const void* ht_1;
  void* ht;
} gru_t;

struct rnn_attr_s {
  int d;
  KernelType act_gate, act_cand;
  rnn_attr_s() = default;
T
tensor-tang 已提交
103
  explicit rnn_attr_s(int _d, KernelType _act_gate, KernelType _act_cand)
T
tensor-tang 已提交
104 105 106 107 108 109 110
      : d(_d), act_gate(_act_gate), act_cand(_act_cand) {}
};

struct lstm_attr_s : public rnn_attr_s {
  bool use_peephole;
  KernelType act_cell;
  lstm_attr_s() = default;
T
tensor-tang 已提交
111 112
  explicit lstm_attr_s(int _d, KernelType _act_gate, KernelType _act_cand,
                       KernelType _act_cell, bool _use_peephole = false)
T
tensor-tang 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
      : rnn_attr_s(_d, _act_gate, _act_cand),
        use_peephole(_use_peephole),
        act_cell(_act_cell) {}
};

typedef struct rnn_attr_s gru_attr_t;
typedef struct lstm_attr_s lstm_attr_t;

template <typename T>
struct LSTMTuples {
  typedef T data_type;
  typedef lstm_attr_t attr_type;
  typedef void (*func_type)(lstm_t*, const lstm_attr_t*);
};

128 129 130 131 132 133 134
template <typename T>
struct GRUTuples {
  typedef T data_type;
  typedef gru_attr_t attr_type;
  typedef void (*func_type)(gru_t*, const gru_attr_t*);
};

135
typedef struct seq_pool_attr_s {
T
tensor-tang 已提交
136
  int h, w;  // h should always be the first one
T
tensor-tang 已提交
137
  SeqPoolType type;
138
  seq_pool_attr_s() = default;
T
tensor-tang 已提交
139
  explicit seq_pool_attr_s(int width, SeqPoolType pool_type, int height = 1)
140
      : h(height), w(width), type(pool_type) {}
T
tensor-tang 已提交
141 142 143 144 145 146 147 148 149
} seq_pool_attr_t;

template <typename T>
struct SeqPoolTuples {
  typedef T data_type;
  typedef seq_pool_attr_t attr_type;
  typedef void (*func_type)(const T*, T*, const seq_pool_attr_t*);
};

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
typedef struct emb_seq_pool_attr_s {
  int64_t table_height, table_width;
  int64_t index_height, index_width;
  int64_t out_width;
  SeqPoolType pool_type;
  emb_seq_pool_attr_s() = default;
  explicit emb_seq_pool_attr_s(int64_t tbl_height, int64_t tbl_width,
                               int64_t idx_height, int64_t idx_width,
                               int64_t output_width,
                               SeqPoolType seqpool_type = SeqPoolType::kSum)
      : table_height(tbl_height),
        table_width(tbl_width),
        index_height(idx_height),
        index_width(idx_width),
        out_width(output_width),
        pool_type(seqpool_type) {}
} emb_seq_pool_attr_t;

template <typename T>
struct EmbSeqPoolTuples {
  typedef T data_type;
  typedef emb_seq_pool_attr_t attr_type;
  typedef void (*func_type)(const T*, const int64_t*, T*,
                            const emb_seq_pool_attr_t*);
};

176 177 178 179 180 181 182 183
typedef struct matmul_attr_s {
  int m, n, k;
  void* packed_weight{nullptr};
  matmul_attr_s() = default;
  explicit matmul_attr_s(int m_, int n_, int k_, void* packed_weight_ = nullptr)
      : m(m_), n(n_), k(k_), packed_weight(packed_weight_) {}
} matmul_attr_t;

T
tensor-tang 已提交
184 185 186
template <typename T>
struct MatMulTuples {
  typedef T data_type;
187 188
  typedef matmul_attr_t attr_type;
  typedef void (*func_type)(const T*, const T*, T*, const matmul_attr_t*);
T
tensor-tang 已提交
189 190
};

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
template <typename T>
struct CRFDecodingTuples {
  typedef T data_type;
  typedef int attr_type;
  typedef void (*func_type)(const int, const T*, const T*, T*, int*, int);
};

template <typename T>
struct LayerNormTuples {
  typedef T data_type;
  typedef int attr_type;
  typedef void (*func_type)(T*, T*, T*, T*, const T*, const T*, int,
                            const float, int);
};

206 207 208 209 210 211 212
template <typename T>
struct SoftmaxTuples {
  typedef T data_type;
  typedef int attr_type;
  typedef void (*func_type)(const T*, T*, int, int);
};

T
tensor-tang 已提交
213 214 215 216 217 218 219 220
// nChw16c = nChw16c .* NC
template <typename T>
struct NCHW16CMulNCTuples {
  typedef T data_type;
  typedef int attr_type;
  typedef void (*func_type)(const T*, const T*, T*, int, int);
};

T
tensor-tang 已提交
221 222 223 224
// Just for adding to kernel pool without template
class Kernel {
 public:
  Kernel() = default;
T
tensor-tang 已提交
225
  virtual ~Kernel() = default;
T
tensor-tang 已提交
226 227 228
  DISABLE_COPY_AND_ASSIGN(Kernel);
};

T
tensor-tang 已提交
229
template <typename KernelTuples>
T
tensor-tang 已提交
230
class KernelMore : public Kernel {
231
 public:
T
tensor-tang 已提交
232 233 234
  using T = typename KernelTuples::data_type;
  using Func = typename KernelTuples::func_type;
  using Attr = typename KernelTuples::attr_type;
T
tensor-tang 已提交
235
  virtual Func GetFunc() const { return func; }
T
tensor-tang 已提交
236 237
  virtual bool UseMe(const Attr& attr) const = 0;
  virtual const char* ImplType() const = 0;
T
tensor-tang 已提交
238 239 240 241 242

 protected:
  Func func{nullptr};
};

T
tensor-tang 已提交
243
template <typename KernelTuples>
T
tensor-tang 已提交
244
class ReferKernel : public KernelMore<KernelTuples> {
T
tensor-tang 已提交
245 246
 public:
  // Refer code can always be used
T
tensor-tang 已提交
247
  bool UseMe(const typename KernelTuples::attr_type& attr) const override {
T
tensor-tang 已提交
248 249
    return true;
  }
T
tensor-tang 已提交
250
  const char* ImplType() const override { return "Refer"; }
T
tensor-tang 已提交
251 252
};

T
tensor-tang 已提交
253
}  // namespace jit
T
tensor-tang 已提交
254 255
}  // namespace operators
}  // namespace paddle