提交 57c27b4e 编写于 作者: Q qijun

make EigenTensor default unaligned and follow comments

上级 cff8762f
...@@ -36,14 +36,15 @@ struct EigenDim { ...@@ -36,14 +36,15 @@ struct EigenDim {
}; };
// Interpret paddle::platform::Tensor as EigenTensor and EigenConstTensor. // Interpret paddle::platform::Tensor as EigenTensor and EigenConstTensor.
template <typename T, size_t D, typename IndexType = Eigen::DenseIndex> template <typename T, size_t D, int MajorType = Eigen::RowMajor,
typename IndexType = Eigen::DenseIndex>
struct EigenTensor { struct EigenTensor {
using Type = Eigen::TensorMap<Eigen::Tensor<T, D, Eigen::RowMajor, IndexType>, // TODO(qijun) Now, default type in unaligned, and we will make a benchmark on
Eigen::Aligned>; // the speed of aligned and unaligned version in future.
using Type = Eigen::TensorMap<Eigen::Tensor<T, D, MajorType, IndexType>>;
using ConstType = using ConstType =
Eigen::TensorMap<Eigen::Tensor<const T, D, Eigen::RowMajor, IndexType>, Eigen::TensorMap<Eigen::Tensor<const T, D, MajorType, IndexType>>;
Eigen::Aligned>;
static Type From(Tensor& tensor, DDim dims) { static Type From(Tensor& tensor, DDim dims) {
return Type(tensor.data<T>(), EigenDim<D>::From(dims)); return Type(tensor.data<T>(), EigenDim<D>::From(dims));
...@@ -60,50 +61,24 @@ struct EigenTensor { ...@@ -60,50 +61,24 @@ struct EigenTensor {
} }
}; };
// Interpret paddle::platform::Tensor as EigenVecotr and EigenConstVector. template <typename T, int MajorType = Eigen::RowMajor,
template <typename T, typename IndexType = Eigen::DenseIndex> typename IndexType = Eigen::DenseIndex>
struct EigenVector { struct EigenVector : public EigenTensor<T, 1, MajorType, IndexType> {
using Type = Eigen::TensorMap<Eigen::Tensor<T, 1, Eigen::RowMajor, IndexType>,
Eigen::Aligned>;
using ConstType =
Eigen::TensorMap<Eigen::Tensor<const T, 1, Eigen::RowMajor, IndexType>,
Eigen::Aligned>;
// From is to transfer a one dimension Tensor into a one dimension EigenVector
static Type From(Tensor& tensor) { return EigenTensor<T, 1>::From(tensor); }
// Flatten is to reshape a Tensor into a one dimension EigenVector // Flatten is to reshape a Tensor into a one dimension EigenVector
static Type Flatten(Tensor& tensor) { static typename EigenTensor<T, 1>::Type Flatten(Tensor& tensor) {
return EigenTensor<T, 1>::From( return EigenTensor<T, 1>::From(
tensor, make_ddim({static_cast<int>(product(tensor.dims_))})); tensor, make_ddim({static_cast<int>(product(tensor.dims_))}));
} }
static ConstType From(const Tensor& tensor) { static typename EigenTensor<T, 1>::ConstType Flatten(const Tensor& tensor) {
return EigenTensor<T, 1>::From(tensor);
}
static ConstType Flatten(const Tensor& tensor) {
return EigenTensor<T, 1>::From( return EigenTensor<T, 1>::From(
tensor, make_ddim({static_cast<int>(product(tensor.dims_))})); tensor, make_ddim({static_cast<int>(product(tensor.dims_))}));
} }
}; };
// Interpret paddle::platform::Tensor as EigenMatrix and EigenConstMatrix. template <typename T, int MajorType = Eigen::RowMajor,
template <typename T, typename IndexType = Eigen::DenseIndex> typename IndexType = Eigen::DenseIndex>
struct EigenMatrix { using EigenMatrix = EigenTensor<T, 2, MajorType, IndexType>;
using Type = Eigen::TensorMap<Eigen::Tensor<T, 2, Eigen::RowMajor, IndexType>,
Eigen::Aligned>;
using ConstType =
Eigen::TensorMap<Eigen::Tensor<const T, 2, Eigen::RowMajor, IndexType>,
Eigen::Aligned>;
static Type From(Tensor& tensor) { return EigenTensor<T, 2>::From(tensor); }
static ConstType From(const Tensor& tensor) {
return EigenTensor<T, 2>::From(tensor);
}
};
} // namespace framework } // namespace framework
} // namespace paddle } // namespace paddle
...@@ -37,15 +37,12 @@ class Tensor { ...@@ -37,15 +37,12 @@ class Tensor {
template <bool less, size_t i, typename... args> template <bool less, size_t i, typename... args>
friend struct paddle::pybind::details::CastToPyBufferImpl; friend struct paddle::pybind::details::CastToPyBufferImpl;
template <typename T, size_t D, typename IndexType> template <typename T, size_t D, int MajorType, typename IndexType>
friend struct EigenTensor; friend struct EigenTensor;
template <typename T, typename IndexType> template <typename T, int MajorType, typename IndexType>
friend struct EigenVector; friend struct EigenVector;
template <typename T, typename IndexType>
friend struct EigenMatrix;
public: public:
Tensor() : offset_(0) {} Tensor() : offset_(0) {}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册