diff --git a/paddle/fluid/jit/CMakeLists.txt b/paddle/fluid/jit/CMakeLists.txt index 85148f1387405cf763ac20704b7f42e3cdb68d21..3ad7b1b16cbc7a88ff846ebc631c1acda2c25360 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 6e768b66f881798e1a9461b9943c6bedc46caae0..233d1dc981fb229f0cb034aa8f8df1c891ec5d0a 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 535b3a89dd60f9190f181e70b7c595f2caf990e4..0000000000000000000000000000000000000000 --- 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 19c9d38034c781e77911583ce04467f417b197f5..796af1a50d8689fdd37ea82d03fe45946ec44b05 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 535e92fe88473e85443d0d2a4180393a2dc6623f..986612c6672a3c2c4ca3e60a197c8a0756a18f2d 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 0000000000000000000000000000000000000000..92a1f9582c931fbc182471ea538c3876cb9794d1 --- /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 50dadaf4ae2276b5656811ec9b0fdc12851e75f7..eaf3c1221c8a20243b3fea810e2a2ecc4628d0d6 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 01e58776b305742dbd519bdd9d8f4c4c6e4ff77b..f4e56a5a04a623cd19695b004ba3ba8665db5a11 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 5136afaf02566c8875b2734bb75ff6c16d6c41b1..726ebf328931b5e7138604dd1840d40f10d47250 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 163200184453b34415bba9fc9fbec2167e9a8ab9..2fd0f23270c9ae6d22f6b68f7e15a957dfc1d205 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 f847b3702d13953df7b2a8a57848e5a247c4ab3f..9cef7664b1fa5c0d25431b77e6dfc64809fe8cbc 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 0000000000000000000000000000000000000000..0d297da500a496e3542cb643f54a4e8a080fb2e0 --- /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 0000000000000000000000000000000000000000..daaecd55bfe673d2ca74c07931f28a207e67631e --- /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 7726ea23865d476327a626571082b0cc9e467b1c..0000000000000000000000000000000000000000 --- 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 90e2e4b7f798f9031fb3d58fd97e1219104f25b2..11c61b3179b65af5dd10c522c7b3a07372e1446f 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 e41f78a48777b7a2d8508591e3d7ed568c1fdc36..6cf8b98b1cbcb270ffad8223774ed7e1aaa25486 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 69304c800fd73cf84ce5e1924cd82bb6b06e1ebd..6f92ac44d63799d169c84092cbdcb88b743a10a4 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 1579610c7a63b893d9b40ab2e18a022c3cf7bab0..b54ea3c4aa132cb2fbfae2fe4d10142cff94e000 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 94aae67376007f945bbb7a8dadc4abd815ecd43e..0000000000000000000000000000000000000000 --- 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 6ec321168cdd4c835f4132895a6d12544bead1ef..130b16f8dd32c4f4a83117b89888588043f56932 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 02f93dcaf09705c387e228158445278a83a92af5..54063e1192a61a17e384a0545638d7a8b28a39c3 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 81ced383eb36a26a4f3061e927ffc9a814a38018..62cfc330ae3ffefc4fa2552ded56cede23068b4e 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 3516a6a96389004496b186ee691a6875586faf78..82e1fa873f8d10780254e5c36ab7ef0dc8519490 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 9d261c8a95726f1e582e03da2c5cd8b868d82f4f..94e8ce4e04aa413471826da7c52e52a8a3e3d4d9 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 4ddae883d19759f7dc62b240d336225a0abd2c81..752b5a3021af500b0b6f549245c557f84dfb6b60 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 287cb2e08e366758745bfb8410bc0946d269469a..46056c9d064665617924a3eb42df236ec66e8d91 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))