operator.h 4.3 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

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

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
36
namespace framework {
L
liuruilong 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49
static std::unordered_map<
    std::string, std::pair<std::vector<std::string>, std::vector<std::string>>>
    op_input_output_key = {{"conv2d", {{"Input"}, {"Output"}}},
                           {"relu", {{"X"}, {"Out"}}},
                           {"softmax", {{"X"}, {"Out"}}},
                           {"mul", {{"X"}, {"Out"}}},
                           {"elementwise_add", {{"X", "Y"}, {"Out"}}},
                           {"pool2d", {{"X"}, {"Out"}}},
                           {"batch_norm", {{"X"}, {"Y"}}},
                           {"lrn", {{"X"}, {"Out"}}},
                           {"concat", {{"X"}, {"Out"}}},
                           {"feed", {{"X"}, {"Out"}}},
                           {"fetch", {{"X"}, {"Out"}}}};
朔-望's avatar
朔-望 已提交
50

朔-望's avatar
朔-望 已提交
51 52 53
template <typename Dtype>
class OperatorBase : PaddleMobileObject {
 public:
54 55 56 57 58
  OperatorBase(const std::string &type, const VariableNameMap &inputs,
               const VariableNameMap &outputs, const AttributeMap &attrs,
               std::shared_ptr<Scope> scope);
  virtual ~OperatorBase() {}
  virtual void Run() const = 0;
朔-望's avatar
朔-望 已提交
59

60 61 62 63 64 65 66
  const VariableNameMap &Inputs() const { return inputs_; }
  const VariableNameMap &Outputs() const { return outputs_; }
  const std::string &Type() const { return type_; }
  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
朔-望 已提交
67
    }
68
  }
朔-望's avatar
朔-望 已提交
69

朔-望's avatar
朔-望 已提交
70
 protected:
71 72 73 74 75
  std::shared_ptr<Scope> scope_;
  std::string type_;
  VariableNameMap inputs_;
  VariableNameMap outputs_;
  AttributeMap attrs_;
L
liuruilong 已提交
76

朔-望's avatar
朔-望 已提交
77
 private:
78
  void CheckAllInputOutputSet() const;
朔-望's avatar
朔-望 已提交
79
};
朔-望's avatar
朔-望 已提交
80

朔-望's avatar
朔-望 已提交
81 82
template <typename Dtype>
class OperatorWithKernel : public OperatorBase<Dtype> {
朔-望's avatar
朔-望 已提交
83
 public:
84 85 86 87 88 89
  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) {}
  virtual void InferShape() const = 0;
  virtual void Run() const = 0;
朔-望's avatar
朔-望 已提交
90
};
朔-望's avatar
朔-望 已提交
91

朔-望's avatar
朔-望 已提交
92 93 94
template <typename Dtype, typename P>
class OpKernelBase : PaddleMobileObject {
 public:
95
  virtual void Compute(const P &para) const = 0;
朔-望's avatar
朔-望 已提交
96

97
  virtual ~OpKernelBase() = default;
朔-望's avatar
朔-望 已提交
98
};
朔-望's avatar
朔-望 已提交
99

Z
zhaojiaying01 已提交
100 101 102 103 104 105 106
#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 已提交
107
class FusionOpMatcher : PaddleMobileObject {
L
liuruilong 已提交
108 109 110 111 112 113 114 115 116
 public:
  FusionOpMatcher() {}

  virtual std::string Type() = 0;

  virtual void FolderNodes(Node &node) {
    node.Folder(node_.Depth(), Type(), {});
  }

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

L
liuruilong 已提交
119
  std::string BeginType() { return node_.BeginType(); }
L
liuruilong 已提交
120 121 122 123 124 125 126

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

朔-望's avatar
朔-望 已提交
127 128
}  // namespace framework
}  // namespace paddle_mobile