operator.h 3.8 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 22
#include <map>

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

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

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

69 70 71 72 73 74
protected:
  std::shared_ptr<Scope> scope_;
  std::string type_;
  VariableNameMap inputs_;
  VariableNameMap outputs_;
  AttributeMap attrs_;
L
liuruilong 已提交
75

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

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

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

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

朔-望's avatar
朔-望 已提交
98
} // namespace framework
L
liuruilong 已提交
99
} // namespace paddle_mobile