提交 cb198fa7 编写于 作者: Q qijun

merge baidu/develop

...@@ -13,8 +13,10 @@ See the License for the specific language governing permissions and ...@@ -13,8 +13,10 @@ See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#include "paddle/framework/executor.h" #include "paddle/framework/executor.h"
#include <iostream>
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "paddle/framework/lod_tensor.h"
#include "paddle/framework/op_registry.h" #include "paddle/framework/op_registry.h"
#include "paddle/framework/scope.h" #include "paddle/framework/scope.h"
...@@ -40,30 +42,39 @@ Executor::Executor(const std::vector<platform::Place>& places) { ...@@ -40,30 +42,39 @@ Executor::Executor(const std::vector<platform::Place>& places) {
void Executor::Run(const ProgramDesc& pdesc, Scope* scope, void Executor::Run(const ProgramDesc& pdesc, Scope* scope,
std::vector<Tensor>* outputs) { std::vector<Tensor>* outputs) {
// operators running
// TODO(tonyyang-svail): // TODO(tonyyang-svail):
// - only runs the first block // - only runs the first block
// - only runs on the first device // - only runs on the first device
Scope& local_scope = scope->NewScope(); // - test on gpu
auto& block = pdesc.blocks(0); auto& block = pdesc.blocks(0);
auto& device_context = device_contexts_[0]; auto& device = device_contexts_[0];
// TODO(tonyyang-svail):
// - runs on a new local scope
// Scope& local_scope = scope->NewScope();
for (auto& var : block.vars()) { for (auto& var : block.vars()) {
local_scope.NewVar(var.name()); scope->NewVar(var.name());
} }
// std::vector<op_ptr> ops;
for (auto& op_desc : block.ops()) { for (auto& op_desc : block.ops()) {
auto op = framework::OpRegistry::CreateOp(op_desc); auto op = paddle::framework::OpRegistry::CreateOp(op_desc);
// InferShape is now doing inside Run method. op->Run(*scope, *device);
op->Run(local_scope, *device_context);
} }
// TODO(tonyyang-svail): need to test gpu device // TODO(tonyyang-svail): need to test gpu device
for (auto& device_context : device_contexts_) { for (auto& device_context : device_contexts_) {
device_context->Wait(); device_context->Wait();
} }
// // print tensor value
// for (auto& var : block.vars()) {
// std::cout << var.name() << std::endl;
// auto v = scope->FindVar(var.name());
// const LoDTensor& t = v->Get<LoDTensor>();
// for (int i = 0; i < t.numel(); ++i)
// std::cout << t.data<float>()[i] << " ";
// std::cout << std::endl;
// }
} }
} // namespace framework } // namespace framework
......
...@@ -15,15 +15,49 @@ limitations under the License. */ ...@@ -15,15 +15,49 @@ limitations under the License. */
#include "paddle/framework/executor.h" #include "paddle/framework/executor.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "paddle/framework/attribute.h" #include "paddle/framework/attribute.h"
#include "paddle/framework/grad_op_builder.h" #include "paddle/framework/grad_op_builder.h"
#include "paddle/framework/op_registry.h" #include "paddle/framework/op_registry.h"
#include "paddle/framework/operator.h" #include "paddle/framework/operator.h"
#include <vector>
USE_OP(elementwise_add); USE_OP(elementwise_add);
USE_OP(gaussian_random);
using std::string;
using namespace paddle::platform; using namespace paddle::platform;
using namespace paddle::framework; using namespace paddle::framework;
typedef paddle::framework::BlockDesc proto_block;
typedef paddle::framework::OpDesc proto_op;
void add_gaussian_random_op(string var_name, proto_block* block) {
std::vector<int> dim{2, 3};
// insert variable
auto a = block->add_vars();
a->set_name(var_name);
auto a_lt = a->mutable_lod_tensor();
a_lt->set_data_type(paddle::framework::DataType::FP32);
for (int i : dim) {
a_lt->add_dims(i);
}
// insert operation
auto op = block->add_ops();
op->set_type("gaussian_random");
auto dims = op->add_attrs();
dims->set_name("dims");
dims->set_type(paddle::framework::AttrType::INTS);
for (int i : dim) {
dims->add_ints(i);
}
auto Out = op->add_outputs();
Out->set_parameter("Out");
Out->add_arguments(var_name);
}
class ExecutorTester : public ::testing::Test { class ExecutorTester : public ::testing::Test {
public: public:
virtual void SetUp() override { virtual void SetUp() override {
...@@ -31,35 +65,25 @@ class ExecutorTester : public ::testing::Test { ...@@ -31,35 +65,25 @@ class ExecutorTester : public ::testing::Test {
root_block->set_idx(0); root_block->set_idx(0);
root_block->set_parent_idx(-1); root_block->set_parent_idx(-1);
auto a = root_block->add_vars(); add_gaussian_random_op("a", root_block);
a->set_name("a"); add_gaussian_random_op("b", root_block);
auto a_lt = a->mutable_lod_tensor();
a_lt->set_data_type(paddle::framework::DataType::FP32);
a_lt->add_dims(640);
a_lt->add_dims(640);
auto b = root_block->add_vars();
b->set_name("b");
auto b_lt = b->mutable_lod_tensor();
b_lt->set_data_type(paddle::framework::DataType::FP32);
b_lt->add_dims(640);
b_lt->add_dims(640);
auto c = root_block->add_vars(); auto c = root_block->add_vars();
c->set_name("c"); c->set_name("c");
auto c_lt = c->mutable_lod_tensor(); auto c_lt = c->mutable_lod_tensor();
c_lt->set_data_type(paddle::framework::DataType::FP32); c_lt->set_data_type(paddle::framework::DataType::FP32);
c_lt->add_dims(640);
c_lt->add_dims(640);
auto op1 = root_block->add_ops(); auto op = root_block->add_ops();
op1->set_type("elementwise_add"); op->set_type("elementwise_add");
auto X = op1->add_inputs(); auto X = op->add_inputs();
X->set_parameter("X"); X->set_parameter("X");
X->add_arguments("a"); X->add_arguments("a");
auto Y = op1->add_inputs(); auto Y = op->add_inputs();
Y->set_parameter("Y"); Y->set_parameter("Y");
Y->add_arguments("b"); Y->add_arguments("b");
auto Out = op->add_outputs();
Out->set_parameter("Out");
Out->add_arguments("c");
} }
protected: protected:
...@@ -83,9 +107,7 @@ TEST_F(ExecutorTester, InitCPU) { ...@@ -83,9 +107,7 @@ TEST_F(ExecutorTester, InitCPU) {
TEST_F(ExecutorTester, InitGPU) { TEST_F(ExecutorTester, InitGPU) {
std::vector<Place> places; std::vector<Place> places;
GPUPlace gpu_place0(0); GPUPlace gpu_place0(0);
GPUPlace gpu_place1(1);
places.push_back(gpu_place0); places.push_back(gpu_place0);
places.push_back(gpu_place1);
Executor* executor = new Executor(places); Executor* executor = new Executor(places);
executor->Run(pdesc_, &scope_, outputs_); executor->Run(pdesc_, &scope_, outputs_);
......
...@@ -43,6 +43,7 @@ int GetCurrentDeviceId() { ...@@ -43,6 +43,7 @@ int GetCurrentDeviceId() {
} }
void SetDeviceId(int id) { void SetDeviceId(int id) {
PADDLE_ENFORCE(id < GetDeviceCount(), "id must less than GPU count")
PADDLE_ENFORCE(cudaSetDevice(id), PADDLE_ENFORCE(cudaSetDevice(id),
"cudaSetDevice failed in paddle::platform::SetDeviceId"); "cudaSetDevice failed in paddle::platform::SetDeviceId");
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册