From 6529a3944143c9b39afaeac5c051f88e0d74c86c Mon Sep 17 00:00:00 2001 From: wanghuancoder Date: Thu, 17 Jun 2021 20:26:20 +0800 Subject: [PATCH] InterpreterCore run (#33640) * runtimecontext * ExecutionContextV2 * refine * refine * pass test * fix bug * fix bug * InterpreterCore run --- paddle/fluid/pybind/pybind.cc | 103 ++++++++++++++++++++-------------- 1 file changed, 62 insertions(+), 41 deletions(-) diff --git a/paddle/fluid/pybind/pybind.cc b/paddle/fluid/pybind/pybind.cc index 830ca049bee..8be338df653 100644 --- a/paddle/fluid/pybind/pybind.cc +++ b/paddle/fluid/pybind/pybind.cc @@ -40,6 +40,7 @@ limitations under the License. */ #include "paddle/fluid/framework/lod_rank_table.h" #include "paddle/fluid/framework/lod_tensor.h" #include "paddle/fluid/framework/lod_tensor_array.h" +#include "paddle/fluid/framework/new_exec.h" #include "paddle/fluid/framework/op_info.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/op_version_registry.h" @@ -53,7 +54,6 @@ limitations under the License. */ #include "paddle/fluid/framework/trainer.h" #include "paddle/fluid/framework/type_defs.h" #include "paddle/fluid/framework/version.h" -#include "paddle/fluid/framework/new_exec.h" #include "paddle/fluid/imperative/layer.h" #include "paddle/fluid/memory/allocation/allocator_strategy.h" #include "paddle/fluid/memory/allocation/mmap_allocator.h" @@ -90,7 +90,6 @@ limitations under the License. */ #include "paddle/fluid/pybind/ps_gpu_wrapper_py.h" #include "paddle/fluid/pybind/pybind_boost_headers.h" - #if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL) #include "paddle/fluid/pybind/nccl_wrapper_py.h" #endif @@ -1897,45 +1896,67 @@ All parameter, weight, gradient are variables in Paddle. }); py::class_(m, "InterpreterCore") - .def(py::init()) - .def("run", [](InterpreterCore &self, const std::unordered_map& input_dict, std::vector vec_fetch_name) { - pybind11::gil_scoped_release release; - std::vector vec_tensor; - std::vector vec_name; - //vec_tensor.reserve( feed.size() ); - //vec_tensor.reserve( feed.size ()) ; - - //auto new_res = input_dict.cast(); - - for ( auto & item : input_dict ) - { - //cerr << "test flag " << test_flag << endl; - //cerr << item.first << endl; - framework::LoDTensor t; - SetTensorFromPyArray(&t, item.second, - platform::CPUPlace(), false); - - //cerr << t.dims() << endl; - //cerr << t.data()[0] << endl; - - vec_name.push_back( item.first ); - vec_tensor.push_back( t ); - } - - - - //std::cerr << "11" << std::endl; - std::vector vec_out; - self.run(vec_name, vec_tensor, vec_fetch_name, vec_out); - //self.Run(prog, scope, block_id, create_local_scope, create_vars, - // fetch_vars); - std::vector< py::array> vec_ret; - for( size_t i = 0; i < vec_out.size(); ++i ) - { - vec_ret.push_back( TensorToPyArray(vec_out[i], true) ) ; - } - return vec_ret; - }); + .def(py::init()) + .def("run", + [](InterpreterCore &self, + const std::unordered_map &input_dict, + std::vector vec_fetch_name) { + pybind11::gil_scoped_release release; + std::vector vec_tensor; + std::vector vec_name; + // vec_tensor.reserve( feed.size() ); + // vec_tensor.reserve( feed.size ()) ; + + // auto new_res = input_dict.cast(); + + for (auto &item : input_dict) { + // cerr << "test flag " << test_flag << endl; + // cerr << item.first << endl; + framework::LoDTensor t; + SetTensorFromPyArray( + &t, item.second, platform::CPUPlace(), false); + + // cerr << t.dims() << endl; + // cerr << t.data()[0] << endl; + + vec_name.push_back(item.first); + vec_tensor.push_back(t); + } + + // std::cerr << "11" << std::endl; + std::vector vec_out; + self.run(vec_name, vec_tensor, vec_fetch_name, vec_out); + // self.Run(prog, scope, block_id, create_local_scope, create_vars, + // fetch_vars); + std::vector vec_ret; + for (size_t i = 0; i < vec_out.size(); ++i) { + vec_ret.push_back(TensorToPyArray(vec_out[i], true)); + } + return vec_ret; + }) + .def("run", + [](InterpreterCore &self, + const std::unordered_map &input_dict, + std::vector vec_fetch_name) { + pybind11::gil_scoped_release release; + std::vector vec_tensor; + std::vector vec_name; + + for (auto &item : input_dict) { + vec_name.push_back(item.first); + vec_tensor.push_back(item.second); + } + + std::vector vec_out; + self.run(vec_name, vec_tensor, vec_fetch_name, vec_out); + + std::vector vec_ret; + for (size_t i = 0; i < vec_out.size(); ++i) { + vec_ret.push_back(TensorToPyArray(vec_out[i], true)); + } + return vec_ret; + }); m.def("init_gflags", framework::InitGflags); m.def("init_glog", framework::InitGLOG); -- GitLab