operator.h 4.4 KB
Newer Older
朔-望's avatar
朔-望 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
==============================================================================*/

#pragma once

L
liuruilong 已提交
21
#include <map>
Z
zhaojiaying01 已提交
22 23
#include <string>
#include <utility>
Z
zhaojiaying01 已提交
24
#include <vector>
朔-望's avatar
朔-望 已提交
25 26 27
#include "common/type_define.h"
#include "common/types.h"
#include "common/variant.h"
Z
zhaojiaying01 已提交
28 29
#include "framework/attribute.h"
#include "framework/block_desc.h"
Z
zhaojiaying01 已提交
30 31 32 33 34 35
#include "framework/op_info.h"
#include "framework/op_kernel_type.h"
#include "framework/paddle_mobile_object.h"
#include "framework/scope.h"
#include "framework/tensor.h"
#include "framework/variable.h"
朔-望's avatar
朔-望 已提交
36 37

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
38
namespace framework {
L
liuruilong 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51
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
朔-望 已提交
52

朔-望's avatar
朔-望 已提交
53 54 55
template <typename Dtype>
class OperatorBase : PaddleMobileObject {
 public:
56 57 58 59 60
  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
朔-望 已提交
61

62 63 64 65 66 67 68
  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
朔-望 已提交
69
    }
70
  }
朔-望's avatar
朔-望 已提交
71

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

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

朔-望's avatar
朔-望 已提交
83 84
template <typename Dtype>
class OperatorWithKernel : public OperatorBase<Dtype> {
朔-望's avatar
朔-望 已提交
85
 public:
86 87 88 89 90 91
  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
朔-望 已提交
92
};
朔-望's avatar
朔-望 已提交
93

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

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

Z
zhaojiaying01 已提交
102 103 104 105 106 107 108
#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) {}

朔-望's avatar
朔-望 已提交
109 110
}  // namespace framework
}  // namespace paddle_mobile