From ede0990fc096465c2a938a5100dd873c2a56fa6e Mon Sep 17 00:00:00 2001 From: WangZhen <23097963+0x45f@users.noreply.github.com> Date: Mon, 8 Aug 2022 14:24:19 +0800 Subject: [PATCH] [JitLayer]Rename Function to Engine and using new Function class to warp Engine (#44900) * Polish function code * Rename funciton to engine * Fix Log msg and doc * Rename Function to Engine and using new Function class to warp Engine * Rename EngineInfo * Adjust member variable order --- paddle/fluid/jit/CMakeLists.txt | 12 +++- paddle/fluid/jit/all.h | 8 +-- paddle/fluid/jit/ast.h | 59 ----------------- paddle/fluid/jit/compilation_unit.cc | 28 +++----- paddle/fluid/jit/compilation_unit.h | 18 +++-- paddle/fluid/jit/engine/CMakeLists.txt | 9 +++ .../base_function.h => engine/base_engine.h} | 4 +- .../executor_engine.cc} | 14 ++-- .../executor_engine.h} | 12 ++-- .../pe_function.cc => engine/pe_engine.cc} | 19 +++--- .../pe_function.h => engine/pe_engine.h} | 27 +++++--- paddle/fluid/jit/function.cc | 43 ++++++++++++ paddle/fluid/jit/function.h | 44 +++++++++++++ paddle/fluid/jit/function/CMakeLists.txt | 9 --- paddle/fluid/jit/function_utils.h | 6 +- paddle/fluid/jit/layer.cc | 50 +++++++------- paddle/fluid/jit/layer.h | 31 +++++---- paddle/fluid/jit/layer_test.cc | 5 +- paddle/fluid/jit/object.h | 66 ------------------- paddle/fluid/jit/serializer.cc | 40 +++++------ paddle/fluid/platform/flags.cc | 4 +- paddle/fluid/pybind/eager_functions.cc | 4 +- paddle/fluid/pybind/eager_utils.cc | 23 ++++--- paddle/fluid/pybind/eager_utils.h | 6 +- paddle/fluid/pybind/jit.cc | 31 +++++---- python/setup.py.in | 2 +- 26 files changed, 276 insertions(+), 298 deletions(-) delete mode 100644 paddle/fluid/jit/ast.h create mode 100644 paddle/fluid/jit/engine/CMakeLists.txt rename paddle/fluid/jit/{function/base_function.h => engine/base_engine.h} (95%) rename paddle/fluid/jit/{function/executor_function.cc => engine/executor_engine.cc} (80%) rename paddle/fluid/jit/{function/executor_function.h => engine/executor_engine.h} (80%) rename paddle/fluid/jit/{function/pe_function.cc => engine/pe_engine.cc} (85%) rename paddle/fluid/jit/{function/pe_function.h => engine/pe_engine.h} (77%) create mode 100644 paddle/fluid/jit/function.cc create mode 100644 paddle/fluid/jit/function.h delete mode 100644 paddle/fluid/jit/function/CMakeLists.txt delete mode 100644 paddle/fluid/jit/object.h diff --git a/paddle/fluid/jit/CMakeLists.txt b/paddle/fluid/jit/CMakeLists.txt index 85148f1387..3ad7b1b16c 100644 --- a/paddle/fluid/jit/CMakeLists.txt +++ b/paddle/fluid/jit/CMakeLists.txt @@ -1,4 +1,4 @@ -add_subdirectory(function) +add_subdirectory(engine) proto_library(paddle_jit_property_proto SRCS property.proto) cc_library( @@ -31,6 +31,11 @@ cc_library( SRCS function_schema.cc DEPS jit_function_utils) +cc_library( + jit_function + SRCS function.cc + DEPS jit_function_utils jit_executor_engine jit_pe_engine) + cc_library( jit_layer SRCS layer.cc @@ -39,8 +44,9 @@ cc_library( jit_serializer_utils jit_compilation_unit jit_function_schema - jit_executor_function - jit_pe_function) + jit_executor_engine + jit_pe_engine + jit_function) if(WITH_TESTING AND NOT WIN32) add_custom_target( diff --git a/paddle/fluid/jit/all.h b/paddle/fluid/jit/all.h index 6e768b66f8..233d1dc981 100644 --- a/paddle/fluid/jit/all.h +++ b/paddle/fluid/jit/all.h @@ -14,7 +14,7 @@ #pragma once -#include "function/base_function.h" // NOLINT -#include "layer.h" // NOLINT -#include "serializer.h" // NOLINT -#include "serializer_utils.h" // NOLINT +#include "function.h" //NOLINT +#include "layer.h" // NOLINT +#include "serializer.h" // NOLINT +#include "serializer_utils.h" // NOLINT diff --git a/paddle/fluid/jit/ast.h b/paddle/fluid/jit/ast.h deleted file mode 100644 index 535b3a89dd..0000000000 --- a/paddle/fluid/jit/ast.h +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright (c) 2022 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 "paddle/fluid/framework/variable.h" - -namespace paddle { -namespace jit { -using Variable = paddle::framework::Variable; -class BaseFunction; -class CompilationUnit; - -class ClassType { - public: - ClassType(const std::vector& names, - std::weak_ptr cu) - : const_names_(names), compilation_unit_(cu) {} - - static std::shared_ptr Create( - const std::vector& names, - std::weak_ptr cu) { - return std::make_shared(names, cu); - } - - // const std::vector Methods() const; - - // const Variable& GetAttribute(size_t slot) const; - // const Variable& GetAttribute(const std::string& name) const; - - // size_t AddAttribute(const std::string& name, Variable val); - - private: - // TODO(dev): disingwish parameter and buffer - std::vector const_names_; - std::vector const_value_; - - std::vector methods_; - std::vector static_method_; - std::weak_ptr compilation_unit_; -}; - -} // namespace jit -} // namespace paddle diff --git a/paddle/fluid/jit/compilation_unit.cc b/paddle/fluid/jit/compilation_unit.cc index 19c9d38034..796af1a50d 100644 --- a/paddle/fluid/jit/compilation_unit.cc +++ b/paddle/fluid/jit/compilation_unit.cc @@ -16,37 +16,27 @@ #include "paddle/phi/core/enforce.h" -#include "paddle/fluid/jit/function/base_function.h" +#include "paddle/fluid/jit/engine/base_engine.h" namespace paddle { namespace jit { -std::shared_ptr CompilationUnit::Function( +std::shared_ptr CompilationUnit::GetEngine( const std::string &name) const { PADDLE_ENFORCE_EQ( - function_map_.count(name), + engine_map_.count(name), 1, phi::errors::InvalidArgument( - "Funciton name %s is not exist in function_map_.", name)); - return function_map_.at(name); + "Funciton named %s is not exist in engine_map_.", name)); + return engine_map_.at(name); } -void CompilationUnit::SetFunction( - const std::string &name, const std::shared_ptr &function) { - function_map_[name] = function; +void CompilationUnit::SetEngine(const std::string &name, + const std::shared_ptr &engine) { + engine_map_[name] = engine; } -std::vector CompilationUnit::FunctionNames() const { - std::vector names; - for (auto it = function_map_.begin(); it != function_map_.end(); it++) { - names.emplace_back(it->first); - } - return names; -} - -const Name2FunctionMap &CompilationUnit::FunctionMap() const { - return function_map_; -} +const Name2EngineMap &CompilationUnit::EngineMap() const { return engine_map_; } } // namespace jit } // namespace paddle diff --git a/paddle/fluid/jit/compilation_unit.h b/paddle/fluid/jit/compilation_unit.h index 535e92fe88..986612c667 100644 --- a/paddle/fluid/jit/compilation_unit.h +++ b/paddle/fluid/jit/compilation_unit.h @@ -21,26 +21,24 @@ namespace paddle { namespace jit { -class BaseFunction; -using Name2FunctionMap = - std::unordered_map>; +class BaseEngine; +using Name2EngineMap = + std::unordered_map>; class CompilationUnit { public: CompilationUnit() = default; ~CompilationUnit() {} - std::shared_ptr Function(const std::string &name) const; + std::shared_ptr GetEngine(const std::string &name) const; - void SetFunction(const std::string &name, - const std::shared_ptr &function); + void SetEngine(const std::string &name, + const std::shared_ptr &engine); - std::vector FunctionNames() const; - - const Name2FunctionMap &FunctionMap() const; + const Name2EngineMap &EngineMap() const; private: - Name2FunctionMap function_map_; + Name2EngineMap engine_map_; }; } // namespace jit diff --git a/paddle/fluid/jit/engine/CMakeLists.txt b/paddle/fluid/jit/engine/CMakeLists.txt new file mode 100644 index 0000000000..92a1f9582c --- /dev/null +++ b/paddle/fluid/jit/engine/CMakeLists.txt @@ -0,0 +1,9 @@ +cc_library( + jit_executor_engine + SRCS executor_engine.cc + DEPS executor) + +cc_library( + jit_pe_engine + SRCS pe_engine.cc + DEPS parallel_executor) diff --git a/paddle/fluid/jit/function/base_function.h b/paddle/fluid/jit/engine/base_engine.h similarity index 95% rename from paddle/fluid/jit/function/base_function.h rename to paddle/fluid/jit/engine/base_engine.h index 50dadaf4ae..eaf3c1221c 100644 --- a/paddle/fluid/jit/function/base_function.h +++ b/paddle/fluid/jit/engine/base_engine.h @@ -22,14 +22,14 @@ namespace jit { using Tensor = paddle::experimental::Tensor; using DenseTensor = phi::DenseTensor; -class BaseFunction { +class BaseEngine { public: virtual std::vector operator()( const std::vector &inputs) = 0; virtual std::vector operator()(const std::vector &inputs) = 0; - virtual ~BaseFunction() {} + virtual ~BaseEngine() {} }; } // namespace jit diff --git a/paddle/fluid/jit/function/executor_function.cc b/paddle/fluid/jit/engine/executor_engine.cc similarity index 80% rename from paddle/fluid/jit/function/executor_function.cc rename to paddle/fluid/jit/engine/executor_engine.cc index 01e58776b3..f4e56a5a04 100644 --- a/paddle/fluid/jit/function/executor_function.cc +++ b/paddle/fluid/jit/engine/executor_engine.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/fluid/jit/function/executor_function.h" +#include "paddle/fluid/jit/engine/executor_engine.h" #include "paddle/fluid/framework/program_desc.h" #include "paddle/fluid/framework/variable.h" @@ -21,9 +21,9 @@ namespace paddle { namespace jit { -ExecutorFunction::ExecutorFunction(const std::shared_ptr &info, - const Name2VariableMap ¶ms_dict, - const phi::Place &place) +ExecutorEngine::ExecutorEngine(const std::shared_ptr &info, + const Name2VariableMap ¶ms_dict, + const phi::Place &place) : info_(info), place_(place), inner_exe_(place_) { info_->RemoveDescFeedFetch(); PADDLE_ENFORCE_GT( @@ -35,13 +35,13 @@ ExecutorFunction::ExecutorFunction(const std::shared_ptr &info, VLOG(6) << framework::GenScopeTreeDebugInfo(&scope_); } -std::vector ExecutorFunction::operator()( +std::vector ExecutorEngine::operator()( const std::vector &inputs) { auto dense_tensors = utils::ToDenseTensors(inputs); return utils::ToTensors(this->operator()(dense_tensors)); } -std::vector ExecutorFunction::operator()( +std::vector ExecutorEngine::operator()( const std::vector &inputs) { utils::ShareIntoScope(info_->InputArgNames(), inputs, &scope_); inner_exe_.Run(info_->ProgramDesc(), @@ -55,7 +55,7 @@ std::vector ExecutorFunction::operator()( return outputs; } -const std::shared_ptr &ExecutorFunction::Info() const { +const std::shared_ptr &ExecutorEngine::Info() const { return info_; } diff --git a/paddle/fluid/jit/function/executor_function.h b/paddle/fluid/jit/engine/executor_engine.h similarity index 80% rename from paddle/fluid/jit/function/executor_function.h rename to paddle/fluid/jit/engine/executor_engine.h index 5136afaf02..726ebf3289 100644 --- a/paddle/fluid/jit/function/executor_function.h +++ b/paddle/fluid/jit/engine/executor_engine.h @@ -19,20 +19,20 @@ #include "paddle/fluid/framework/executor.h" #include "paddle/fluid/framework/scope.h" -#include "paddle/fluid/jit/function/base_function.h" +#include "paddle/fluid/jit/engine/base_engine.h" #include "paddle/fluid/jit/function_schema.h" #include "paddle/fluid/jit/function_utils.h" namespace paddle { namespace jit { -class ExecutorFunction : public BaseFunction { +class ExecutorEngine : public BaseEngine { public: - ExecutorFunction(const std::shared_ptr &info, - const Name2VariableMap ¶ms_dict, - const phi::Place &place); + ExecutorEngine(const std::shared_ptr &info, + const Name2VariableMap ¶ms_dict, + const phi::Place &place); - ~ExecutorFunction() noexcept {} + ~ExecutorEngine() noexcept {} std::vector operator()(const std::vector &inputs); diff --git a/paddle/fluid/jit/function/pe_function.cc b/paddle/fluid/jit/engine/pe_engine.cc similarity index 85% rename from paddle/fluid/jit/function/pe_function.cc rename to paddle/fluid/jit/engine/pe_engine.cc index 1632001844..2fd0f23270 100644 --- a/paddle/fluid/jit/function/pe_function.cc +++ b/paddle/fluid/jit/engine/pe_engine.cc @@ -12,10 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/fluid/jit/function/pe_function.h" +#include "paddle/fluid/jit/engine/pe_engine.h" #include "paddle/fluid/framework/block_desc.h" #include "paddle/fluid/framework/details/build_strategy.h" +#include "paddle/fluid/framework/details/execution_strategy.h" +#include "paddle/fluid/framework/ir/graph.h" +#include "paddle/fluid/framework/parallel_executor.h" #include "paddle/fluid/framework/program_desc.h" #include "paddle/phi/core/enforce.h" @@ -54,9 +57,9 @@ static ExecutionStrategy GetExecutionStrategy(const platform::Place &place) { return execution_strategy; } -PEFunction::PEFunction(const std::shared_ptr &info, - const Name2VariableMap ¶ms_dict, - const phi::Place &place) +PEEngine::PEEngine(const std::shared_ptr &info, + const Name2VariableMap ¶ms_dict, + const phi::Place &place) : info_(info), place_(place) { info_->RemoveDescFeedFetch(); PADDLE_ENFORCE_GT( @@ -69,7 +72,7 @@ PEFunction::PEFunction(const std::shared_ptr &info, CreateGraphAndPE(); } -void PEFunction::CreateGraphAndPE() { +void PEEngine::CreateGraphAndPE() { framework::details::BuildStrategy build_strategy; auto execution_strategy = GetExecutionStrategy(place_); @@ -85,12 +88,12 @@ void PEFunction::CreateGraphAndPE() { inner_pe_->SkipMemoryReuse(/*scope_idx=*/0, info_->InputArgNames()); } -std::vector PEFunction::operator()(const std::vector &inputs) { +std::vector PEEngine::operator()(const std::vector &inputs) { auto dense_tensors = utils::ToDenseTensors(inputs); return utils::ToTensors(this->operator()(dense_tensors)); } -std::vector PEFunction::operator()( +std::vector PEEngine::operator()( const std::vector &inputs) { utils::ShareIntoScope(info_->InputArgNames(), inputs, &scope_); @@ -109,7 +112,7 @@ std::vector PEFunction::operator()( return outputs; } -const std::shared_ptr &PEFunction::Info() const { return info_; } +const std::shared_ptr &PEEngine::Info() const { return info_; } } // namespace jit } // namespace paddle diff --git a/paddle/fluid/jit/function/pe_function.h b/paddle/fluid/jit/engine/pe_engine.h similarity index 77% rename from paddle/fluid/jit/function/pe_function.h rename to paddle/fluid/jit/engine/pe_engine.h index f847b3702d..9cef7664b1 100644 --- a/paddle/fluid/jit/function/pe_function.h +++ b/paddle/fluid/jit/engine/pe_engine.h @@ -16,29 +16,36 @@ #include -#include "paddle/fluid/framework/details/execution_strategy.h" -#include "paddle/fluid/framework/ir/graph.h" -#include "paddle/fluid/framework/parallel_executor.h" #include "paddle/fluid/framework/scope.h" -#include "paddle/fluid/jit/function/base_function.h" +#include "paddle/fluid/jit/engine/base_engine.h" #include "paddle/fluid/jit/function_schema.h" #include "paddle/fluid/jit/function_utils.h" namespace paddle { -namespace jit { +namespace framework { +class ParallelExecutor; +namespace details { +class ExecutionStrategy; +} +namespace ir { +class Graph; +} +} // namespace framework + +namespace jit { using ExecutionStrategy = framework::details::ExecutionStrategy; using ParallelExecutor = framework::ParallelExecutor; using Graph = framework::ir::Graph; -class PEFunction : public BaseFunction { +class PEEngine : public BaseEngine { public: - PEFunction(const std::shared_ptr &info, - const Name2VariableMap ¶ms_dict, - const phi::Place &place); + PEEngine(const std::shared_ptr &info, + const Name2VariableMap ¶ms_dict, + const phi::Place &place); - ~PEFunction() noexcept {} + ~PEEngine() noexcept {} void CreateGraphAndPE(); diff --git a/paddle/fluid/jit/function.cc b/paddle/fluid/jit/function.cc new file mode 100644 index 0000000000..0d297da500 --- /dev/null +++ b/paddle/fluid/jit/function.cc @@ -0,0 +1,43 @@ +// Copyright (c) 2022 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. + +#include "paddle/fluid/jit/function.h" + +#include +#include + +#include "paddle/phi/api/include/tensor.h" +#include "paddle/phi/core/dense_tensor.h" + +#include "paddle/fluid/jit/engine/base_engine.h" +#include "paddle/fluid/jit/function_utils.h" + +namespace paddle { +namespace jit { + +Function::Function(BaseEngine* engine) : engine_(engine) {} + +std::vector Function::operator()( + const std::vector& inputs) const { + auto dense_tensors = utils::ToDenseTensors(inputs); + return utils::ToTensors(this->operator()(dense_tensors)); +} + +std::vector Function::operator()( + const std::vector& inputs) const { + return (*engine_)(inputs); +} + +} // namespace jit +} // namespace paddle diff --git a/paddle/fluid/jit/function.h b/paddle/fluid/jit/function.h new file mode 100644 index 0000000000..daaecd55bf --- /dev/null +++ b/paddle/fluid/jit/function.h @@ -0,0 +1,44 @@ +// Copyright (c) 2022 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 "paddle/phi/api/include/tensor.h" + +namespace paddle { +namespace jit { +class BaseEngine; +using DenseTensor = phi::DenseTensor; +using Tensor = paddle::experimental::Tensor; + +class Function { + public: + explicit Function(BaseEngine* engine); + + std::vector operator()(const std::vector& inputs) const; + + std::vector operator()( + const std::vector& inputs) const; + + ~Function() = default; + + private: + BaseEngine* engine_; +}; + +} // namespace jit +} // namespace paddle diff --git a/paddle/fluid/jit/function/CMakeLists.txt b/paddle/fluid/jit/function/CMakeLists.txt deleted file mode 100644 index 7726ea2386..0000000000 --- a/paddle/fluid/jit/function/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -cc_library( - jit_executor_function - SRCS executor_function.cc - DEPS executor) - -cc_library( - jit_pe_function - SRCS pe_function.cc - DEPS parallel_executor) diff --git a/paddle/fluid/jit/function_utils.h b/paddle/fluid/jit/function_utils.h index 90e2e4b7f7..11c61b3179 100644 --- a/paddle/fluid/jit/function_utils.h +++ b/paddle/fluid/jit/function_utils.h @@ -58,9 +58,9 @@ void ShareParamsIntoScope(const std::vector ¶m_names, void RemoveFeedFetch(framework::ProgramDesc *program_desc); template -std::shared_ptr MakeFunction(const std::shared_ptr &info, - const Name2VariableMap ¶ms_dict, - const phi::Place &place) { +std::shared_ptr MakeEngine(const std::shared_ptr &info, + const Name2VariableMap ¶ms_dict, + const phi::Place &place) { return std::make_shared(info, params_dict, place); } diff --git a/paddle/fluid/jit/layer.cc b/paddle/fluid/jit/layer.cc index e41f78a487..6cf8b98b1c 100644 --- a/paddle/fluid/jit/layer.cc +++ b/paddle/fluid/jit/layer.cc @@ -15,62 +15,68 @@ #include "paddle/fluid/jit/layer.h" #include "paddle/fluid/framework/variable.h" +#include "paddle/phi/core/enforce.h" +#include "paddle/phi/core/errors.h" #include "paddle/fluid/jit/compilation_unit.h" -#include "paddle/fluid/jit/function/base_function.h" +#include "paddle/fluid/jit/engine/base_engine.h" +#include "paddle/fluid/jit/function.h" #include "paddle/fluid/jit/function_schema.h" -#include "paddle/phi/core/enforce.h" -#include "paddle/phi/core/errors.h" namespace paddle { namespace jit { -Layer::Layer(const Name2VariableMap& params_dict, - const Name2VariableMap& attrs_dict, +Layer::Layer(const Name2VariableMap& params_map, + const Name2VariableMap& attrs_map, + const Name2FunctionInfoMap& info_map, const phi::Place& place) - : params_dict_(params_dict), attrs_dict_(attrs_dict) { + : params_map_(params_map), attrs_map_(attrs_map), info_map_(info_map) { unit_.reset(new CompilationUnit()); } -std::shared_ptr Layer::Function(const std::string& name) const { - return unit_->Function(name); +jit::Function Layer::Function(const std::string& name) const { + return jit::Function(unit_->GetEngine(name).get()); } std::vector Layer::forward(const std::vector& inputs) { - auto func = Function("forward"); - return (*func)(inputs); + auto func = this->Function("forward"); + return func(inputs); } std::vector Layer::forward( const std::vector& inputs) { - auto func = Function("forward"); - return (*func)(inputs); + auto func = this->Function("forward"); + return func(inputs); } void Layer::to(const phi::Place& place) {} -void Layer::SetFunction(const std::string& name, - const std::shared_ptr& function) { - unit_->SetFunction(name, function); +void Layer::SetEngine(const std::string& name, + const std::shared_ptr& engine) { + unit_->SetEngine(name, engine); } -std::vector Layer::FunctionNames() const { - return unit_->FunctionNames(); -} +const Name2EngineMap& Layer::EngineMap() const { return unit_->EngineMap(); } -const Name2FunctionMap& Layer::FunctionMap() const { - return unit_->FunctionMap(); +const std::shared_ptr& Layer::FunctionInfo( + const std::string& name) const { + PADDLE_ENFORCE_EQ( + info_map_.count(name), + 1, + phi::errors::InvalidArgument( + "FuncitonInfo named %s is not exist in info_map_.", name)); + return info_map_.at(name); } #define PD_SPECIALZE_ATTRIBUTE_TYPE(T) \ template <> \ T Layer::Attribute(const std::string& name) const { \ - if (attrs_dict_.find(name) == attrs_dict_.end()) { \ + if (attrs_map_.find(name) == attrs_map_.end()) { \ PADDLE_THROW(phi::errors::NotFound( \ "Attribute can not found %s, please check if it exists.")); \ return T(); \ } \ - auto var = attrs_dict_.at(name); \ + auto var = attrs_map_.at(name); \ T ret = var->Get(); \ return ret; \ } diff --git a/paddle/fluid/jit/layer.h b/paddle/fluid/jit/layer.h index 69304c800f..6f92ac44d6 100644 --- a/paddle/fluid/jit/layer.h +++ b/paddle/fluid/jit/layer.h @@ -14,6 +14,7 @@ #pragma once +#include #include #include #include @@ -21,7 +22,7 @@ #include "paddle/phi/api/include/tensor.h" #include "paddle/phi/common/place.h" -#include "function/base_function.h" //NOLINT +#include "function.h" //NOLINT namespace paddle { @@ -31,22 +32,26 @@ class Variable; namespace jit { class CompilationUnit; +class FunctionInfo; using DenseTensor = phi::DenseTensor; using Tensor = paddle::experimental::Tensor; using Variable = paddle::framework::Variable; using Name2VariableMap = std::unordered_map>; -using Name2FunctionMap = - std::unordered_map>; +using Name2EngineMap = + std::unordered_map>; +using Name2FunctionInfoMap = + std::unordered_map>; class Layer { public: - Layer(const Name2VariableMap& params_dict, - const Name2VariableMap& attrs_dict_, + Layer(const Name2VariableMap& params_map, + const Name2VariableMap& attrs_map_, + const Name2FunctionInfoMap& info_map, const phi::Place& place); - std::shared_ptr Function(const std::string& name) const; + jit::Function Function(const std::string& name) const; template T Attribute(const std::string& name) const; @@ -57,16 +62,18 @@ class Layer { void to(const phi::Place& place); - void SetFunction(const std::string& name, - const std::shared_ptr& function); + void SetEngine(const std::string& name, + const std::shared_ptr& engine); - std::vector FunctionNames() const; + const Name2EngineMap& EngineMap() const; - const Name2FunctionMap& FunctionMap() const; + const std::shared_ptr& FunctionInfo( + const std::string& name) const; private: - Name2VariableMap params_dict_; - Name2VariableMap attrs_dict_; + Name2VariableMap params_map_; + Name2VariableMap attrs_map_; + Name2FunctionInfoMap info_map_; std::shared_ptr unit_; }; diff --git a/paddle/fluid/jit/layer_test.cc b/paddle/fluid/jit/layer_test.cc index 1579610c7a..b54ea3c4aa 100644 --- a/paddle/fluid/jit/layer_test.cc +++ b/paddle/fluid/jit/layer_test.cc @@ -26,6 +26,7 @@ #include "paddle/phi/core/tensor_utils.h" #include "paddle/phi/kernels/funcs/math_function.h" +#include "paddle/fluid/jit/function.h" #include "paddle/fluid/jit/function_utils.h" #include "paddle/fluid/jit/layer.h" #include "paddle/fluid/jit/serializer.h" @@ -102,7 +103,7 @@ TEST(CpuLayerTest, Construct) { EXPECT_NEAR(out_data[0], 0.02194316, 1e-6); auto func = layer.Function("infer"); - outs = (*func)(inputs); + outs = func(inputs); out_data = outs[0].data(); EXPECT_NEAR(out_data[0], 1.41562390, 1e-6); auto pow_out = @@ -127,7 +128,7 @@ TEST(GpuLayerTest, Construct) { EXPECT_NEAR(out_data[0], 0.02194316, 1e-6); auto func = layer.Function("infer"); - outs = (*func)(inputs); + outs = func(inputs); gpu_tensor = outs[0]; cpu_tensor = paddle::experimental::copy_to(gpu_tensor, phi::CPUPlace(), true); out_data = cpu_tensor.data(); diff --git a/paddle/fluid/jit/object.h b/paddle/fluid/jit/object.h deleted file mode 100644 index 94aae67376..0000000000 --- a/paddle/fluid/jit/object.h +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright (c) 2022 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 "paddle/fluid/framework/variable.h" - -namespace paddle { -namespace jit { -class ClassType; - -namespace internal { - -class Object { - public: - Object(const std::shared_ptr& type, size_t num_slot) - : type_(type) { - slots_.resize(num_slot); - } - - static std::unique_ptr Create(std::shared_ptr type, - size_t num_slot) { - return std::make_unique(type, num_slot); - } - - std::shared_ptr Type() const { return type_; } - - void SetSlot(size_t slot, Variable val) { - if (slot >= slots_.size()) { - slots_.resize(slot); - } - slots_[slot] = std::move(val); - } - - const Variable& GetSlot(size_t slot) { - // TODO(dev): Add ENFORCE_LT(slot, size()); - return slots_[slot]; - } - - Variable GetAttr(const std::string& name) const; - - void SetAttr(const std::string& name, Variable val); - - private: - std::shared_ptr type_; - // Store Tensors and Attributes - std::vector slots_; -}; - -} // namespace internal -} // namespace jit -} // namespace paddle diff --git a/paddle/fluid/jit/serializer.cc b/paddle/fluid/jit/serializer.cc index 6ec321168c..130b16f8dd 100644 --- a/paddle/fluid/jit/serializer.cc +++ b/paddle/fluid/jit/serializer.cc @@ -20,8 +20,8 @@ #include "paddle/fluid/framework/variable.h" #include "paddle/fluid/platform/device_context.h" -#include "paddle/fluid/jit/function/executor_function.h" -#include "paddle/fluid/jit/function/pe_function.h" +#include "paddle/fluid/jit/engine/executor_engine.h" +#include "paddle/fluid/jit/engine/pe_engine.h" #include "paddle/fluid/jit/layer.h" #include "paddle/fluid/jit/property.h" #include "paddle/fluid/jit/serializer_utils.h" @@ -30,18 +30,18 @@ DECLARE_string(jit_engine_type); namespace paddle { namespace jit { - +using Name2FunctionInfoMap = + std::unordered_map>; Layer Deserializer::operator()(const std::string& path, const phi::Place& place) { const auto& pdmodel_paths = utils::PdmodelFilePaths(path); // set is ordered std::set param_names_set; - std::vector> infos; + Name2FunctionInfoMap info_map; for (auto& it : pdmodel_paths) { auto& func_name = it.first; auto program_desc = LoadProgram(it.second); - // TODO(dev): load int/float attrs std::vector persist_var_names; auto all_var_desc = program_desc.Block(0).AllVars(); for (auto* desc_ptr : all_var_desc) { @@ -51,8 +51,8 @@ Layer Deserializer::operator()(const std::string& path, } param_names_set.insert(persist_var_names.begin(), persist_var_names.end()); - infos.emplace_back(std::make_shared( - func_name, persist_var_names, program_desc)); + info_map[func_name] = std::make_shared( + func_name, persist_var_names, program_desc); } Name2VariableMap params_dict; @@ -64,23 +64,23 @@ Layer Deserializer::operator()(const std::string& path, VLOG(3) << "Read Property Success!"; } - Layer layer = Layer(params_dict, attrs_dict, place); + Layer layer = Layer(params_dict, attrs_dict, info_map, place); - for (auto& info : infos) { + for (auto it = info_map.begin(); it != info_map.end(); ++it) { + const std::string& func_name = it->first; + auto& info = it->second; if (FLAGS_jit_engine_type == "Executor") { - VLOG(3) << "Add function type: ExecutorFunction. name: " - << info->FunctionName(); - layer.SetFunction( - info->FunctionName(), - utils::MakeFunction(info, params_dict, place)); + VLOG(3) << "Add function type: ExecutorEngine. Function name: " + << func_name; + layer.SetEngine( + func_name, + utils::MakeEngine(info, params_dict, place)); } else if (FLAGS_jit_engine_type == "PE") { - VLOG(3) << "Add function type: PEFunction. name: " - << info->FunctionName(); - layer.SetFunction( - info->FunctionName(), - utils::MakeFunction(info, params_dict, place)); + VLOG(3) << "Add function type: PEEngine. Function name: " << func_name; + layer.SetEngine(func_name, + utils::MakeEngine(info, params_dict, place)); } else { - PD_THROW("Invalid JitLayer funciton type."); + PD_THROW("Invalid JitLayer engine type."); } } diff --git a/paddle/fluid/platform/flags.cc b/paddle/fluid/platform/flags.cc index 02f93dcaf0..54063e1192 100644 --- a/paddle/fluid/platform/flags.cc +++ b/paddle/fluid/platform/flags.cc @@ -1006,8 +1006,8 @@ PADDLE_DEFINE_EXPORTED_bool( * default=PE * Example: * Note: - * FLAGS_jit_engine_type == Executor, using ExecutorFunction by default - * FLAGS_jit_engine_type == PE, using PEFunction by default + * FLAGS_jit_engine_type == Executor, using ExecutorEngine by default + * FLAGS_jit_engine_type == PE, using PEEngine by default */ PADDLE_DEFINE_EXPORTED_string(jit_engine_type, "PE", diff --git a/paddle/fluid/pybind/eager_functions.cc b/paddle/fluid/pybind/eager_functions.cc index 81ced383eb..62cfc330ae 100644 --- a/paddle/fluid/pybind/eager_functions.cc +++ b/paddle/fluid/pybind/eager_functions.cc @@ -372,8 +372,8 @@ static PyObject* eager_api_jit_function_call(PyObject* self, PyObject* args, PyObject* kwargs) { EAGER_TRY - std::shared_ptr function = - CastPyArg2BaseFunction(PyTuple_GET_ITEM(args, 0), 0); + std::shared_ptr function = + CastPyArg2BaseEngine(PyTuple_GET_ITEM(args, 0), 0); std::vector ins = CastPyArg2VectorOfTensor(PyTuple_GET_ITEM(args, 1), 1); std::vector outs = (*function)(ins); diff --git a/paddle/fluid/pybind/eager_utils.cc b/paddle/fluid/pybind/eager_utils.cc index 3516a6a963..82e1fa873f 100644 --- a/paddle/fluid/pybind/eager_utils.cc +++ b/paddle/fluid/pybind/eager_utils.cc @@ -22,8 +22,8 @@ limitations under the License. */ #include "paddle/fluid/framework/convert_utils.h" #include "paddle/fluid/framework/scope.h" #include "paddle/fluid/framework/scope_guard.h" -#include "paddle/fluid/jit/function/executor_function.h" -#include "paddle/fluid/jit/function/pe_function.h" +#include "paddle/fluid/jit/engine/executor_engine.h" +#include "paddle/fluid/jit/engine/pe_engine.h" #include "paddle/fluid/memory/allocation/allocator.h" #include "paddle/fluid/operators/py_func_op.h" #include "paddle/fluid/operators/utils.h" @@ -54,8 +54,8 @@ extern PyTypeObject* g_customplace_pytype; extern PyTypeObject* g_framework_tensor_pytype; extern PyTypeObject* g_framework_lodtensorarray_pytype; extern PyTypeObject* g_custom_op_kernel_ctx_pytype; -extern PyTypeObject* g_executor_function_pytype; -extern PyTypeObject* g_pe_function_pytype; +extern PyTypeObject* g_executor_engine_pytype; +extern PyTypeObject* g_pe_engine_pytype; int TensorDtype2NumpyDtype(phi::DataType dtype) { switch (dtype) { @@ -232,19 +232,18 @@ std::shared_ptr CastPyArg2VarBase(PyObject* obj, return py::cast>(obj); } -std::shared_ptr CastPyArg2BaseFunction(PyObject* obj, - ssize_t arg_pos) { +std::shared_ptr CastPyArg2BaseEngine(PyObject* obj, + ssize_t arg_pos) { if (PyObject_IsInstance( - obj, reinterpret_cast(g_executor_function_pytype))) { - return ::pybind11::handle(obj) - .cast>(); + obj, reinterpret_cast(g_executor_engine_pytype))) { + return ::pybind11::handle(obj).cast>(); } else if (PyObject_IsInstance( - obj, reinterpret_cast(g_pe_function_pytype))) { - return ::pybind11::handle(obj).cast>(); + obj, reinterpret_cast(g_pe_engine_pytype))) { + return ::pybind11::handle(obj).cast>(); } else { PADDLE_THROW(platform::errors::InvalidArgument( "argument (position %d) must be " - "BaseFunction, but got %s", + "BaseEngine, but got %s", arg_pos + 1, reinterpret_cast(obj->ob_type)->tp_name)); } diff --git a/paddle/fluid/pybind/eager_utils.h b/paddle/fluid/pybind/eager_utils.h index 9d261c8a95..94e8ce4e04 100644 --- a/paddle/fluid/pybind/eager_utils.h +++ b/paddle/fluid/pybind/eager_utils.h @@ -20,7 +20,7 @@ typedef SSIZE_T ssize_t; #include "paddle/fluid/eager/hooks.h" #include "paddle/fluid/framework/lod_tensor.h" #include "paddle/fluid/framework/tensor.h" -#include "paddle/fluid/jit/function/base_function.h" +#include "paddle/fluid/jit/engine/base_engine.h" #include "paddle/fluid/platform/place.h" #include "paddle/phi/common/backend.h" #include "paddle/phi/common/data_type.h" @@ -75,8 +75,8 @@ framework::proto::VarType::Type CastPyArg2ProtoType(PyObject* obj, std::unordered_map CastPyArg2Vocab(PyObject* obj, ssize_t arg_pos); std::vector CastPyArg2Strings(PyObject* obj, ssize_t arg_pos); -std::shared_ptr CastPyArg2BaseFunction(PyObject* obj, - ssize_t arg_pos); +std::shared_ptr CastPyArg2BaseEngine(PyObject* obj, + ssize_t arg_pos); PyObject* ToPyObject(int value); PyObject* ToPyObject(uint32_t value); diff --git a/paddle/fluid/pybind/jit.cc b/paddle/fluid/pybind/jit.cc index 4ddae883d1..752b5a3021 100644 --- a/paddle/fluid/pybind/jit.cc +++ b/paddle/fluid/pybind/jit.cc @@ -18,8 +18,8 @@ limitations under the License. */ #include "paddle/fluid/imperative/layer.h" #include "paddle/fluid/platform/place.h" -#include "paddle/fluid/jit/function/executor_function.h" -#include "paddle/fluid/jit/function/pe_function.h" +#include "paddle/fluid/jit/engine/executor_engine.h" +#include "paddle/fluid/jit/engine/pe_engine.h" #include "paddle/fluid/jit/function_schema.h" #include "paddle/fluid/jit/layer.h" #include "paddle/fluid/jit/serializer.h" @@ -29,27 +29,26 @@ namespace py = pybind11; namespace paddle { namespace pybind { -PyTypeObject *g_executor_function_pytype = nullptr; -PyTypeObject *g_pe_function_pytype = nullptr; +PyTypeObject *g_executor_engine_pytype = nullptr; +PyTypeObject *g_pe_engine_pytype = nullptr; using Variable = paddle::framework::Variable; void BindJit(pybind11::module *m) { py::class_(*m, "Layer", R"DOC(Layer Class.)DOC") .def("function_dict", - &jit::Layer::FunctionMap, + &jit::Layer::EngineMap, py::return_value_policy::reference); - py::class_> - executor_function( - *m, "ExectorFunction", R"DOC(ExectorFunction Class.)DOC"); - g_executor_function_pytype = - reinterpret_cast(executor_function.ptr()); - executor_function.def("info", &jit::ExecutorFunction::Info); - - py::class_> pe_function( - *m, "PEFunction", R"DOC(PEFunction Class.)DOC"); - g_pe_function_pytype = reinterpret_cast(pe_function.ptr()); - pe_function.def("info", &jit::PEFunction::Info); + py::class_> + executor_engine(*m, "ExecutorEngine", R"DOC(ExecutorEngine Class.)DOC"); + g_executor_engine_pytype = + reinterpret_cast(executor_engine.ptr()); + executor_engine.def("info", &jit::ExecutorEngine::Info); + + py::class_> pe_engine( + *m, "PEEngine", R"DOC(PEEngine Class.)DOC"); + g_pe_engine_pytype = reinterpret_cast(pe_engine.ptr()); + pe_engine.def("info", &jit::PEEngine::Info); py::class_>( *m, "FunctionInfo", R"DOC(FunctionInfo Class.)DOC") diff --git a/python/setup.py.in b/python/setup.py.in index 287cb2e08e..46056c9d06 100755 --- a/python/setup.py.in +++ b/python/setup.py.in @@ -628,7 +628,7 @@ headers = ( # utils api headers list(find_files('*.h', '@PADDLE_SOURCE_DIR@/paddle/utils', recursive=True))) # paddle utils headers -jit_layer_headers = ['layer.h', 'serializer.h', 'serializer_utils.h', 'all.h', 'base_function.h'] +jit_layer_headers = ['layer.h', 'serializer.h', 'serializer_utils.h', 'all.h', 'function.h'] for f in jit_layer_headers: headers += list(find_files(f, '@PADDLE_SOURCE_DIR@/paddle/fluid/jit', recursive=True)) -- GitLab