operator.h 4.6 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 19
#include <string>
#include <utility>
Z
zhaojiaying01 已提交
20
#include <vector>
L
liuruilong 已提交
21

E
eclipsess 已提交
22
#include "common/enforce.h"
朔-望's avatar
朔-望 已提交
23 24 25
#include "common/type_define.h"
#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"
E
eclipsess 已提交
29
#include "framework/op_registry.h"
L
liuruilong 已提交
30
#include "framework/paddle_mobile_object.h"
L
liuruilong 已提交
31
#include "framework/program/block_desc.h"
L
liuruilong 已提交
32
#include "framework/program/program-optimize/node.h"
L
liuruilong 已提交
33 34 35
#include "framework/scope.h"
#include "framework/tensor.h"
#include "framework/variable.h"
朔-望's avatar
朔-望 已提交
36 37

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

朔-望's avatar
朔-望 已提交
42 43 44
template <typename Dtype>
class OperatorBase : PaddleMobileObject {
 public:
L
liuruilong 已提交
45 46 47
  /*
   *  @b op 基类的实例化方法, op 获取到了 输入、参数以及提前分配好的输出 tensor
   * */
48 49 50 51
  OperatorBase(const std::string &type, const VariableNameMap &inputs,
               const VariableNameMap &outputs, const AttributeMap &attrs,
               std::shared_ptr<Scope> scope);
  virtual ~OperatorBase() {}
52
  void Run() const;
L
liuruilong 已提交
53
  std::vector<string> GetOutKeys() const;
54
  virtual void RunImpl() const = 0;
朔-望's avatar
朔-望 已提交
55

L
liuruilong 已提交
56 57 58
  /*
   * @b op 运算所需的输入, 如上一层的输出结果、卷积核
   * */
59
  const VariableNameMap &Inputs() const { return inputs_; }
L
liuruilong 已提交
60 61 62
  /*
   * @b op 的输出, 内存会提前被分配好, 运算结果会被存到分配好的内存内
   * */
63
  const VariableNameMap &Outputs() const { return outputs_; }
L
liuruilong 已提交
64 65 66
  /*
   * @b op 类型
   * */
67
  const std::string &Type() const { return type_; }
L
liuruilong 已提交
68 69 70
  /*
   * @b op 运算所需要用到的参数: 如 conv 运算所需要用到的 stride
   * */
71 72 73 74
  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
朔-望 已提交
75
    }
76
  }
L
liuruilong 已提交
77 78 79 80
  /*
   * @b 根据输入形状和参数计算出输出形状
   * */
  virtual void InferShape() const = 0;
朔-望's avatar
朔-望 已提交
81

朔-望's avatar
朔-望 已提交
82
 protected:
83 84 85 86 87
  std::shared_ptr<Scope> scope_;
  std::string type_;
  VariableNameMap inputs_;
  VariableNameMap outputs_;
  AttributeMap attrs_;
L
liuruilong 已提交
88

朔-望's avatar
朔-望 已提交
89
 private:
90
  void CheckAllInputOutputSet() const;
朔-望's avatar
朔-望 已提交
91
};
朔-望's avatar
朔-望 已提交
92

L
liuruilong 已提交
93 94 95
/*
 * @b 这个类为所有带有运算的 op 的父类, 这个 op 继承与 OperatorBase
 * */
朔-望's avatar
朔-望 已提交
96 97
template <typename Dtype>
class OperatorWithKernel : public OperatorBase<Dtype> {
朔-望's avatar
朔-望 已提交
98
 public:
99 100 101 102
  OperatorWithKernel(const std::string &type, const VariableNameMap &inputs,
                     const VariableNameMap &outputs, const AttributeMap &attrs,
                     std::shared_ptr<Scope> scope)
      : OperatorBase<Dtype>(type, inputs, outputs, attrs, scope) {}
103 104

  virtual void RunImpl() const = 0;
W
wangliu 已提交
105
  virtual void InferShape() const = 0;
朔-望's avatar
朔-望 已提交
106
};
朔-望's avatar
朔-望 已提交
107

L
liuruilong 已提交
108 109 110
/*
 * @b 所有kernel的父类
 * */
朔-望's avatar
朔-望 已提交
111 112 113
template <typename Dtype, typename P>
class OpKernelBase : PaddleMobileObject {
 public:
L
liuruilong 已提交
114 115 116 117 118
  /*
   * @b 所有kernel 需实现 Compute 方法
   * @p para 这个参数为 kernel 运算时所需要用到参数组成的一个结构体,
   *    所有结构体存在与: paddle-mobile/src/operators/op_param.h
   * */
119 120
  virtual void Compute(const P &para) const = 0;
  virtual ~OpKernelBase() = default;
朔-望's avatar
朔-望 已提交
121
};
朔-望's avatar
朔-望 已提交
122

Z
zhaojiaying01 已提交
123 124 125 126 127 128 129
#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 已提交
130
class FusionOpMatcher : PaddleMobileObject {
L
liuruilong 已提交
131 132 133 134 135
 public:
  FusionOpMatcher() {}

  virtual std::string Type() = 0;

L
liuruilong 已提交
136 137
  virtual void FolderNodes(Node *node) {
    node->Folder(node_.Depth(), Type(), {});
L
liuruilong 已提交
138 139
  }

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

L
liuruilong 已提交
142
  std::string BeginType() { return node_.BeginType(); }
L
liuruilong 已提交
143 144 145 146 147 148 149

 protected:
  Node node_;
  std::string type_;
  std::shared_ptr<OpDesc> new_opdesc_;
};

朔-望's avatar
朔-望 已提交
150 151
}  // namespace framework
}  // namespace paddle_mobile