operator.h 6.0 KB
Newer Older
Z
zhaojiaying01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* 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. */
朔-望's avatar
朔-望 已提交
14 15 16

#pragma once

L
liuruilong 已提交
17
#include <map>
Z
zhaojiaying01 已提交
18
#include <string>
Z
zhaojiaying01 已提交
19
#include <vector>
L
liuruilong 已提交
20

L
liuruilong 已提交
21

L
liuruilong 已提交
22 23
#include "common/enforce.h"
#include "common/type_define.h"
L
liuruilong 已提交
24 25
#include "common/types.h"
#include "common/variant.h"
L
liuruilong 已提交
26
#include "framework/attribute.h"
L
liuruilong 已提交
27
#include "framework/op_info.h"
L
liuruilong 已提交
28
#include "framework/op_kernel_type.h"
L
liuruilong 已提交
29 30
#include "framework/op_registry.h"
#include "framework/program/block_desc.h"
L
liuruilong 已提交
31
#include "framework/program/program-optimize/node.h"
L
liuruilong 已提交
32 33 34
#include "framework/scope.h"
#include "framework/tensor.h"
#include "framework/variable.h"
L
liuruilong 已提交
35 36
#include "framework/cl/cl_scope.h"
#include "framework/cl/cl_helper.h"
朔-望's avatar
朔-望 已提交
37 38

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
39
namespace framework {
40 41
using std::string;
using std::vector;
朔-望's avatar
朔-望 已提交
42

W
wangliu 已提交
43 44 45 46 47 48 49 50 51 52 53 54
template <typename T>
static T *GetVarValue(const string &key, const VariableNameMap &var_map,
                      const Scope &scope) {
  auto var_vec = var_map.at(key);
  if (!var_vec.empty()) {
    auto var = scope.FindVar(var_vec[0]);
    return var->GetMutable<T>();
  } else {
    return nullptr;
  }
}

朔-望's avatar
朔-望 已提交
55
template <typename Dtype>
L
liuruilong 已提交
56
class OperatorBase {
朔-望's avatar
朔-望 已提交
57
 public:
L
liuruilong 已提交
58 59 60
  /*
   *  @b op 基类的实例化方法, op 获取到了 输入、参数以及提前分配好的输出 tensor
   * */
61 62 63 64
  OperatorBase(const std::string &type, const VariableNameMap &inputs,
               const VariableNameMap &outputs, const AttributeMap &attrs,
               std::shared_ptr<Scope> scope);
  virtual ~OperatorBase() {}
65
  void Run() const;
L
liuruilong 已提交
66
  std::vector<string> GetOutKeys() const;
67
  std::vector<string> GetInputKeys() const;
68
  virtual void RunImpl() const = 0;
朔-望's avatar
朔-望 已提交
69

E
eclipsess 已提交
70
  virtual void Init() = 0;
L
liuruilong 已提交
71 72 73
  /*
   * @b op 运算所需的输入, 如上一层的输出结果、卷积核
   * */
74
  const VariableNameMap &Inputs() const { return inputs_; }
L
liuruilong 已提交
75 76 77
  /*
   * @b op 的输出, 内存会提前被分配好, 运算结果会被存到分配好的内存内
   * */
78
  const VariableNameMap &Outputs() const { return outputs_; }
L
liuruilong 已提交
79 80 81
  /*
   * @b op 类型
   * */
82
  const std::string &Type() const { return type_; }
L
liuruilong 已提交
83 84 85
  /*
   * @b op 运算所需要用到的参数: 如 conv 运算所需要用到的 stride
   * */
86 87 88 89
  const AttributeMap &Attrs() const { return attrs_; }
  void ClearVariables(const std::vector<std::string> &var_names) const {
    if (this->scope_) {
      this->scope_->EraseVars(var_names);
朔-望's avatar
朔-望 已提交
90
    }
91
  }
L
liuruilong 已提交
92 93 94 95
  /*
   * @b 根据输入形状和参数计算出输出形状
   * */
  virtual void InferShape() const = 0;
L
liuruilong 已提交
96

朔-望's avatar
朔-望 已提交
97
 protected:
98 99 100 101 102
  std::shared_ptr<Scope> scope_;
  std::string type_;
  VariableNameMap inputs_;
  VariableNameMap outputs_;
  AttributeMap attrs_;
L
liuruilong 已提交
103

朔-望's avatar
朔-望 已提交
104
 private:
105
  void CheckAllInputOutputSet() const;
朔-望's avatar
朔-望 已提交
106
};
朔-望's avatar
朔-望 已提交
107

L
liuruilong 已提交
108 109 110
/*
 * @b 这个类为所有带有运算的 op 的父类, 这个 op 继承与 OperatorBase
 * */
L
liuruilong 已提交
111
template <typename Dtype, typename ParamType, typename KernelType>
朔-望's avatar
朔-望 已提交
112
class OperatorWithKernel : public OperatorBase<Dtype> {
朔-望's avatar
朔-望 已提交
113
 public:
114 115 116
  OperatorWithKernel(const std::string &type, const VariableNameMap &inputs,
                     const VariableNameMap &outputs, const AttributeMap &attrs,
                     std::shared_ptr<Scope> scope)
L
liuruilong 已提交
117
      : OperatorBase<Dtype>(type, inputs, outputs, attrs, scope),
L
liuruilong 已提交
118 119
        param_(inputs, outputs, attrs, *scope),
        kernel_(scope->GetCLScpoe()) {}
L
liuruilong 已提交
120

L
liuruilong 已提交
121
  virtual void RunImpl() const { this->kernel_.Compute(this->param_); }
122

W
wangliu 已提交
123
  virtual void InferShape() const = 0;
L
liuruilong 已提交
124

E
eclipsess 已提交
125
  void Init() {
126 127 128 129
    //    for (auto i : this->inputs_) {
    //      DLOG << i.first;
    //      DLOG << i.second;
    //    }
E
eclipsess 已提交
130
    PADDLE_MOBILE_ENFORCE(kernel_.Init(&param_), "  %s kernel init failed",
L
liuruilong 已提交
131 132 133
                          this->type_.c_str());
  }

L
liuruilong 已提交
134 135 136
 protected:
  KernelType kernel_;
  ParamType param_;
朔-望's avatar
朔-望 已提交
137
};
朔-望's avatar
朔-望 已提交
138

L
liuruilong 已提交
139 140 141
/*
 * @b 所有kernel的父类
 * */
朔-望's avatar
朔-望 已提交
142
template <typename Dtype, typename P>
L
liuruilong 已提交
143
class OpKernelBase {
朔-望's avatar
朔-望 已提交
144
 public:
L
liuruilong 已提交
145 146 147 148 149
  OpKernelBase() = default;

  OpKernelBase(CLScope *clscope): cl_helper_(clscope) {
  }

L
liuruilong 已提交
150 151 152 153 154
  /*
   * @b 所有kernel 需实现 Compute 方法
   * @p para 这个参数为 kernel 运算时所需要用到参数组成的一个结构体,
   *    所有结构体存在与: paddle-mobile/src/operators/op_param.h
   * */
E
eclipsess 已提交
155 156 157 158 159 160 161
#ifdef PADDLE_MOBILE_MALI_GPU
  OpKernelBase() { acl_op_ = nullptr; }
  void *GetAclOp() const { return acl_op_; }
  void SetAclOp(void *op, void *ob) const {
    reinterpret_cast<OpKernelBase<Dtype, P> *>(ob)->acl_op_ = op;
  }
#endif
162
  virtual void Compute(const P &para) const = 0;
163
  virtual bool Init(P *para) { return true; }
164
  virtual ~OpKernelBase() = default;
E
eclipsess 已提交
165 166 167 168 169

 private:
#ifdef PADDLE_MOBILE_MALI_GPU
  void *acl_op_;
#endif
L
liuruilong 已提交
170 171 172 173

  CLHelper cl_helper_;


朔-望's avatar
朔-望 已提交
174
};
朔-望's avatar
朔-望 已提交
175

Z
zhaojiaying01 已提交
176 177 178 179 180 181 182
#define DEFINE_OP_CONSTRUCTOR(cls, parent_cls)                                 \
  cls(const std::string &type, const ::paddle_mobile::VariableNameMap &inputs, \
      const ::paddle_mobile::VariableNameMap &outputs,                         \
      const ::paddle_mobile::framework::AttributeMap &attrs,                   \
      std::shared_ptr<::paddle_mobile::framework::Scope> scope)                \
      : parent_cls<Dtype, T>(type, inputs, outputs, attrs, scope) {}

L
liuruilong 已提交
183
class FusionOpMatcher {
L
liuruilong 已提交
184 185 186 187 188
 public:
  FusionOpMatcher() {}

  virtual std::string Type() = 0;

L
liuruilong 已提交
189 190 191
  virtual void FolderNodes(
      Node *node,
      std::vector<std::shared_ptr<framework::Node>> *removed_nodes) {
L
liuruilong 已提交
192
    node->Folder(node_.Depth(), Type(), {}, removed_nodes);
L
liuruilong 已提交
193 194
  }

L
liuruilong 已提交
195
  virtual Node &BeginNode() { return node_; }
L
liuruilong 已提交
196

L
liuruilong 已提交
197
  std::string BeginType() { return node_.Type(); }
L
liuruilong 已提交
198

199 200
  virtual std::vector<std::pair<int, std::string>> NeedCheck() { return {}; }

L
for  
liuruilong 已提交
201
  //  virtual  bool Fusion();
L
liuruilong 已提交
202 203 204 205 206 207
 protected:
  Node node_;
  std::string type_;
  std::shared_ptr<OpDesc> new_opdesc_;
};

朔-望's avatar
朔-望 已提交
208 209
}  // namespace framework
}  // namespace paddle_mobile