From 3c49e7b1e4b7b9f8f67fa4b12b05cf648808a40c Mon Sep 17 00:00:00 2001 From: qijun Date: Wed, 13 Sep 2017 14:17:51 +0800 Subject: [PATCH] move EigenDeviceConverter to device_context.h --- paddle/framework/operator.cc | 4 ++-- paddle/framework/operator.h | 19 ++----------------- paddle/operators/math/activation.h | 20 ++++++++++++++++++++ paddle/platform/device_context.cc | 7 ++++--- paddle/platform/device_context.h | 19 ++++++++++++++++++- paddle/platform/device_context_test.cc | 2 +- 6 files changed, 47 insertions(+), 24 deletions(-) create mode 100644 paddle/operators/math/activation.h diff --git a/paddle/framework/operator.cc b/paddle/framework/operator.cc index e1e122091f..25c545d3f9 100644 --- a/paddle/framework/operator.cc +++ b/paddle/framework/operator.cc @@ -22,14 +22,14 @@ namespace framework { template <> Eigen::DefaultDevice& ExecutionContext::GetEigenDevice< platform::CPUPlace, Eigen::DefaultDevice>() const { - return *device_context_->get_eigen_device(); + return *device_context_->get_eigen_device(); } #ifndef PADDLE_ONLY_CPU template <> Eigen::GpuDevice& ExecutionContext::GetEigenDevice() const { - return *device_context_->get_eigen_device(); + return *device_context_->get_eigen_device(); } #endif diff --git a/paddle/framework/operator.h b/paddle/framework/operator.h index 4600b06009..bfa2190557 100644 --- a/paddle/framework/operator.h +++ b/paddle/framework/operator.h @@ -331,21 +331,6 @@ class InferShapeContext { const Scope& scope_; }; -template -struct EigenDeviceConverter; - -template <> -struct EigenDeviceConverter { - using EigenDeviceType = Eigen::DefaultDevice; -}; - -#ifndef PADDLE_ONLY_CPU -template <> -struct EigenDeviceConverter { - using EigenDeviceType = Eigen::GpuDevice; -}; -#endif - class ExecutionContext : public InferShapeContext { public: ExecutionContext(const OperatorBase& op, const Scope& scope, @@ -353,8 +338,8 @@ class ExecutionContext : public InferShapeContext { : InferShapeContext(op, scope), device_context_(device_context) {} template ::EigenDeviceType> + typename DeviceType = typename platform::EigenDeviceConverter< + PlaceType>::EigenDeviceType> DeviceType& GetEigenDevice() const; platform::Place GetPlace() const { return device_context_->GetPlace(); } diff --git a/paddle/operators/math/activation.h b/paddle/operators/math/activation.h new file mode 100644 index 0000000000..b6af478d82 --- /dev/null +++ b/paddle/operators/math/activation.h @@ -0,0 +1,20 @@ +#include "paddle/framework/eigen.h" +#include "paddle/framework/tensor.h" + +namespace paddle { +namespace operators { +namespace math { + +template +struct sigmoid { + void operator()(const platform::DeviceContext& deice_context, + const framework::Tensor& input, framework::Tensor* output) { + auto x = framework::EigenVector::Flatten(*output); + auto y = framework::EigenVector::Flatten(input); + auto* place = device_context.get_eigen_device(); + y.device(*place) = 1. / (1. + (-x).exp()); + } +}; +} +} +} diff --git a/paddle/platform/device_context.cc b/paddle/platform/device_context.cc index ad212c5b2c..cf5c3eec81 100644 --- a/paddle/platform/device_context.cc +++ b/paddle/platform/device_context.cc @@ -16,8 +16,8 @@ namespace paddle { namespace platform { template <> -Eigen::DefaultDevice* DeviceContext::get_eigen_device() - const { +Eigen::DefaultDevice* +DeviceContext::get_eigen_device() const { return reinterpret_cast(this)->eigen_device(); } @@ -91,7 +91,8 @@ class EigenCudaStreamDevice : public Eigen::StreamInterface { }; template <> -Eigen::GpuDevice* DeviceContext::get_eigen_device() const { +Eigen::GpuDevice* DeviceContext::get_eigen_device() + const { return reinterpret_cast(this)->eigen_device(); } diff --git a/paddle/platform/device_context.h b/paddle/platform/device_context.h index 11528e1194..a46ba4c703 100644 --- a/paddle/platform/device_context.h +++ b/paddle/platform/device_context.h @@ -27,12 +27,29 @@ limitations under the License. */ namespace paddle { namespace platform { +template +struct EigenDeviceConverter; + +template <> +struct EigenDeviceConverter { + using EigenDeviceType = Eigen::DefaultDevice; +}; + +#ifndef PADDLE_ONLY_CPU +template <> +struct EigenDeviceConverter { + using EigenDeviceType = Eigen::GpuDevice; +}; +#endif + class DeviceContext { public: virtual ~DeviceContext() {} virtual Place GetPlace() const = 0; - template + template ::EigenDeviceType> DeviceType* get_eigen_device() const; }; diff --git a/paddle/platform/device_context_test.cc b/paddle/platform/device_context_test.cc index 5883a55272..d71e0aae58 100644 --- a/paddle/platform/device_context_test.cc +++ b/paddle/platform/device_context_test.cc @@ -24,7 +24,7 @@ TEST(Device, Init) { for (int i = 0; i < count; i++) { DeviceContext* device_context = new CUDADeviceContext(GPUPlace(i)); Eigen::GpuDevice* gpu_device = - device_context->template get_eigen_device(); + device_context->template get_eigen_device(); ASSERT_NE(nullptr, gpu_device); delete device_context; } -- GitLab