From 57c27b4e0013e4d3f51b41ae6950f70ae11be2e1 Mon Sep 17 00:00:00 2001 From: qijun Date: Wed, 19 Jul 2017 17:11:49 +0800 Subject: [PATCH] make EigenTensor default unaligned and follow comments --- paddle/framework/eigen.h | 53 +++++++++++---------------------------- paddle/framework/tensor.h | 7 ++---- 2 files changed, 16 insertions(+), 44 deletions(-) diff --git a/paddle/framework/eigen.h b/paddle/framework/eigen.h index f5865635b..4ba4fd4d1 100644 --- a/paddle/framework/eigen.h +++ b/paddle/framework/eigen.h @@ -36,14 +36,15 @@ struct EigenDim { }; // Interpret paddle::platform::Tensor as EigenTensor and EigenConstTensor. -template +template struct EigenTensor { - using Type = Eigen::TensorMap, - Eigen::Aligned>; + // TODO(qijun) Now, default type in unaligned, and we will make a benchmark on + // the speed of aligned and unaligned version in future. + using Type = Eigen::TensorMap>; using ConstType = - Eigen::TensorMap, - Eigen::Aligned>; + Eigen::TensorMap>; static Type From(Tensor& tensor, DDim dims) { return Type(tensor.data(), EigenDim::From(dims)); @@ -60,50 +61,24 @@ struct EigenTensor { } }; -// Interpret paddle::platform::Tensor as EigenVecotr and EigenConstVector. -template -struct EigenVector { - using Type = Eigen::TensorMap, - Eigen::Aligned>; - - using ConstType = - Eigen::TensorMap, - Eigen::Aligned>; - // From is to transfer a one dimension Tensor into a one dimension EigenVector - static Type From(Tensor& tensor) { return EigenTensor::From(tensor); } - +template +struct EigenVector : public EigenTensor { // Flatten is to reshape a Tensor into a one dimension EigenVector - static Type Flatten(Tensor& tensor) { + static typename EigenTensor::Type Flatten(Tensor& tensor) { return EigenTensor::From( tensor, make_ddim({static_cast(product(tensor.dims_))})); } - static ConstType From(const Tensor& tensor) { - return EigenTensor::From(tensor); - } - - static ConstType Flatten(const Tensor& tensor) { + static typename EigenTensor::ConstType Flatten(const Tensor& tensor) { return EigenTensor::From( tensor, make_ddim({static_cast(product(tensor.dims_))})); } }; -// Interpret paddle::platform::Tensor as EigenMatrix and EigenConstMatrix. -template -struct EigenMatrix { - using Type = Eigen::TensorMap, - Eigen::Aligned>; - - using ConstType = - Eigen::TensorMap, - Eigen::Aligned>; - - static Type From(Tensor& tensor) { return EigenTensor::From(tensor); } - - static ConstType From(const Tensor& tensor) { - return EigenTensor::From(tensor); - } -}; +template +using EigenMatrix = EigenTensor; } // namespace framework } // namespace paddle diff --git a/paddle/framework/tensor.h b/paddle/framework/tensor.h index 8fbf42e7f..8fd131cf8 100644 --- a/paddle/framework/tensor.h +++ b/paddle/framework/tensor.h @@ -37,15 +37,12 @@ class Tensor { template friend struct paddle::pybind::details::CastToPyBufferImpl; - template + template friend struct EigenTensor; - template + template friend struct EigenVector; - template - friend struct EigenMatrix; - public: Tensor() : offset_(0) {} -- GitLab