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

W
wangliu 已提交
21
#include <map>
朔-望's avatar
朔-望 已提交
22

W
wangliu 已提交
23 24 25 26 27 28 29 30 31 32 33
#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"
W
wangliu 已提交
34

W
wangliu 已提交
35 36
namespace paddle_mobile {
    namespace framework {
W
wangliu 已提交
37

W
wangliu 已提交
38
        template <typename Dtype> class OperatorBase : PaddleMobileObject {
W
wangliu 已提交
39
          public:
W
wangliu 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
            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;

            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 {
                if (this->scope_) {
                    this->scope_->EraseVars(this->inputs_.at("Filter"));
                    this->scope_->EraseVars(this->inputs_.at("Input"));
                }
W
wangliu 已提交
56 57
            }

W
wangliu 已提交
58 59 60 61 62 63 64
          protected:
            std::shared_ptr<Scope> scope_;
            std::string type_;
            VariableNameMap inputs_;
            VariableNameMap outputs_;
            AttributeMap attrs_;

W
wangliu 已提交
65
          private:
W
wangliu 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
            void CheckAllInputOutputSet() const;
        };

        template <typename Dtype>
        class OperatorWithKernel : public OperatorBase<Dtype> {
          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;
        };

        template <typename Dtype, typename P>
        class OpKernelBase : PaddleMobileObject {
          public:
            virtual void Compute(const P &para) const = 0;

            virtual ~OpKernelBase() = default;
W
wangliu 已提交
89 90
        };

W
wangliu 已提交
91 92
    } // namespace framework
} // namespace paddle_mobile