kernel_base.h 3.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 "paddle/fluid/operators/jit/macro.h"
T
tensor-tang 已提交
17 18 19 20
#include "paddle/fluid/platform/macros.h"

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

23
typedef enum {
T
tensor-tang 已提交
24 25 26
  non_kernel = 0,
  vmul = 1,
  vadd = 2,
27 28 29 30
  vaddrelu,
  vsub,
  vscal,
  vaddbias,
31 32 33 34
  vrelu,
  videntity,
  vexp,
  vsigmoid,
T
tensor-tang 已提交
35 36
  vtanh,
  lstmctht,
37 38 39 40
  lstmc1h1,
  gruh1,
  gruhtpart1,
  gruhtpart2
41
} KernelType;
T
tensor-tang 已提交
42

T
tensor-tang 已提交
43
template <typename T>
44
struct XYZNTuples {
T
tensor-tang 已提交
45 46 47 48 49
  typedef T data_type;
  typedef int attr_type;
  typedef void (*func_type)(const T*, const T*, T*, int);
};

50 51 52
template <typename T>
struct AXYNTuples : public XYZNTuples<T> {};

53 54 55 56 57 58 59
template <typename T>
struct XYNTuples {
  typedef T data_type;
  typedef int attr_type;
  typedef void (*func_type)(const T*, T*, int);
};

T
tensor-tang 已提交
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 102 103 104
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;
  rnn_attr_s(int _d, KernelType _act_gate, KernelType _act_cand)
      : 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;
  lstm_attr_s(int _d, KernelType _act_gate, KernelType _act_cand,
              KernelType _act_cell, bool _use_peephole = false)
      : 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*);
};

105 106 107 108 109 110 111
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*);
};

T
tensor-tang 已提交
112 113 114 115
// Just for adding to kernel pool without template
class Kernel {
 public:
  Kernel() = default;
T
tensor-tang 已提交
116
  virtual ~Kernel() = default;
T
tensor-tang 已提交
117 118 119
  DISABLE_COPY_AND_ASSIGN(Kernel);
};

T
tensor-tang 已提交
120
template <typename KernelTuples>
T
tensor-tang 已提交
121
class KernelImpl : public Kernel {
T
tensor-tang 已提交
122 123 124 125
  using T = typename KernelTuples::data_type;
  using Func = typename KernelTuples::func_type;
  using Attr = typename KernelTuples::attr_type;

T
tensor-tang 已提交
126
 public:
T
tensor-tang 已提交
127
  virtual Func GetFunc() const { return func; }
T
tensor-tang 已提交
128 129 130 131 132 133
  virtual bool UseMe(Attr attr) const = 0;

 protected:
  Func func{nullptr};
};

T
tensor-tang 已提交
134 135
template <typename KernelTuples>
class ReferKernel : public KernelImpl<KernelTuples> {
T
tensor-tang 已提交
136 137
 public:
  // Refer code can always be used
T
tensor-tang 已提交
138 139 140
  bool UseMe(typename KernelTuples::attr_type attr) const override {
    return true;
  }
T
tensor-tang 已提交
141 142
};

T
tensor-tang 已提交
143
}  // namespace jit
T
tensor-tang 已提交
144 145
}  // namespace operators
}  // namespace paddle