operator.h 2.9 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 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/* 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

#include <map>

#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 {
namespace framework {

L
liuruilong 已提交
38 39 40 41
template <typename Dtype> class OperatorBase : PaddleMobileObject {
public:
  OperatorBase(const std::string &type, const VariableNameMap &inputs,
               const VariableNameMap &outputs, const AttributeMap &attrs,
朔-望's avatar
朔-望 已提交
42 43 44
               std::shared_ptr<Scope> scope);
  virtual ~OperatorBase() {}
  virtual void Run();
L
liuruilong 已提交
45 46 47 48
  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_; }
朔-望's avatar
朔-望 已提交
49

L
liuruilong 已提交
50
protected:
朔-望's avatar
朔-望 已提交
51 52 53 54 55 56
  std::shared_ptr<Scope> scope_;
  std::string type_;
  VariableNameMap inputs_;
  VariableNameMap outputs_;
  AttributeMap attrs_;

L
liuruilong 已提交
57
private:
朔-望's avatar
朔-望 已提交
58 59 60 61 62 63
  void CheckAllInputOutputSet() const;
  virtual void RunImpl() const = 0;
};

template <typename Dtype>
class OperatorWithKernel : public OperatorBase<Dtype> {
L
liuruilong 已提交
64 65 66
public:
  OperatorWithKernel(const std::string &type, const VariableNameMap &inputs,
                     const VariableNameMap &outputs, const AttributeMap &attrs,
朔-望's avatar
朔-望 已提交
67 68 69 70
                     std::shared_ptr<Scope> scope)
      : OperatorBase<Dtype>(type, inputs, outputs, attrs, scope) {}
  virtual void InferShape() const = 0;

L
liuruilong 已提交
71
protected:
朔-望's avatar
朔-望 已提交
72 73
  virtual void RunImpl() const = 0;

L
liuruilong 已提交
74
private:
朔-望's avatar
朔-望 已提交
75 76
};

L
liuruilong 已提交
77 78 79
template <typename Dtype, typename P> class OpKernelBase : PaddleMobileObject {
public:
  virtual void Compute(const P &para) const = 0;
朔-望's avatar
朔-望 已提交
80 81 82 83

  virtual ~OpKernelBase() = default;
};

L
liuruilong 已提交
84 85
} // namespace framework
} // namespace paddle_mobile