/* 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. */ #pragma once #include #include #include #include #include "common/enforce.h" #include "common/type_define.h" #include "common/types.h" #include "common/variant.h" #include "framework/attribute.h" #include "framework/op_info.h" #include "framework/op_kernel_type.h" #include "framework/op_registry.h" #include "framework/paddle_mobile_object.h" #include "framework/program/block_desc.h" #include "framework/program/program-optimize/node.h" #include "framework/scope.h" #include "framework/tensor.h" #include "framework/variable.h" namespace paddle_mobile { namespace framework { static std::unordered_map< std::string, std::pair, std::vector>> 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"}}}}; template class OperatorBase : PaddleMobileObject { public: OperatorBase(const std::string &type, const VariableNameMap &inputs, const VariableNameMap &outputs, const AttributeMap &attrs, std::shared_ptr scope); virtual ~OperatorBase() {} virtual void Run() const = 0; virtual void InferShape() 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 std::vector &var_names) const { if (this->scope_) { this->scope_->EraseVars(var_names); } } protected: std::shared_ptr scope_; std::string type_; VariableNameMap inputs_; VariableNameMap outputs_; AttributeMap attrs_; private: void CheckAllInputOutputSet() const; }; template class OperatorWithKernel : public OperatorBase { public: OperatorWithKernel(const std::string &type, const VariableNameMap &inputs, const VariableNameMap &outputs, const AttributeMap &attrs, std::shared_ptr scope) : OperatorBase(type, inputs, outputs, attrs, scope) {} virtual void Run() const = 0; virtual void InferShape() const = 0; }; template class OpKernelBase : PaddleMobileObject { public: virtual void Compute(const P ¶) const = 0; virtual ~OpKernelBase() = default; }; #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(type, inputs, outputs, attrs, scope) {} class FusionOpMatcher : PaddleMobileObject { public: FusionOpMatcher() {} virtual std::string Type() = 0; virtual void FolderNodes(Node &node) { node.Folder(node_.Depth(), Type(), {}); } virtual Node &BeginNode() { return node_; } std::string BeginType() { return node_.BeginType(); } protected: Node node_; std::string type_; std::shared_ptr new_opdesc_; }; } // namespace framework } // namespace paddle_mobile