diff --git a/paddle/framework/operator.cc b/paddle/framework/operator.cc index e1e122091f7759b1a68f1f982bc2a35e8241f9f0..25c545d3f9bb9dad81845c42a3b35bfaa649f0e5 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 4600b06009bcef7d0774d25b816aac4733f30795..bfa2190557e73fccec005461b690632b93d3c221 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 0000000000000000000000000000000000000000..b6af478d82d83a0f719b8d5ff27b945b413419f7 --- /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 ad212c5b2c47312743362db4926c80bf056e100d..cf5c3eec8123f955fc91780a903c5dd17b99efc2 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 11528e1194e4516891034fa8febdac3ba6eed204..a46ba4c7032c91d2bd37f20e4f0df566007c96f0 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 5883a55272f0f24c94d48bc43c62ddb7bef15465..d71e0aae58b18e2e18535a9731d46cb4dad173d2 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; }