From ba1f5b5c5857631d41ff0376a815da0e2248a5e4 Mon Sep 17 00:00:00 2001 From: Yu Yang Date: Fri, 22 Sep 2017 11:51:56 -0700 Subject: [PATCH] Sync computation when Python invoke `run` * Since GPU is an async device by default. We should sync computation when Python invoke `run`. So Python can get the correct computation result --- paddle/platform/device_context.h | 5 +++-- paddle/pybind/pybind.cc | 8 +++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/paddle/platform/device_context.h b/paddle/platform/device_context.h index a106592e4..f6a39a8e2 100644 --- a/paddle/platform/device_context.h +++ b/paddle/platform/device_context.h @@ -34,13 +34,14 @@ class DeviceContext { template DeviceType* get_eigen_device() const; + + virtual void Wait() const {} }; class CPUDeviceContext : public DeviceContext { public: CPUDeviceContext(); explicit CPUDeviceContext(CPUPlace place); - virtual ~CPUDeviceContext() {} Eigen::DefaultDevice* eigen_device() const; @@ -59,7 +60,7 @@ class CUDADeviceContext : public DeviceContext { virtual ~CUDADeviceContext(); /*! \brief Wait for all operations completion in the stream. */ - void Wait() const; + void Wait() const override; /*! \brief Return place in the device context. */ Place GetPlace() const override; diff --git a/paddle/pybind/pybind.cc b/paddle/pybind/pybind.cc index c7009a604..f5cba1b78 100644 --- a/paddle/pybind/pybind.cc +++ b/paddle/pybind/pybind.cc @@ -238,7 +238,13 @@ All parameter, weight, gradient are variables in Paddle. return Backward(forwardOp, no_grad_vars).release(); }) .def("infer_shape", &OperatorBase::InferShape) - .def("run", &OperatorBase::Run) + .def("run", + [](OperatorBase &self, + const Scope &scope, + const platform::DeviceContext &dev_ctx) { + self.Run(scope, dev_ctx); + dev_ctx.Wait(); + }) .def("type", [](const OperatorBase &op) -> std::string { return op.Type(); }) .def("outputs", -- GitLab