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

make EigenTensor default unaligned and follow comments

上级 cff8762f
......@@ -36,14 +36,15 @@ struct EigenDim {
};
// 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 {
using Type = Eigen::TensorMap<Eigen::Tensor<T, D, Eigen::RowMajor, IndexType>,
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<Eigen::Tensor<T, D, MajorType, IndexType>>;
using ConstType =
Eigen::TensorMap<Eigen::Tensor<const T, D, Eigen::RowMajor, IndexType>,
Eigen::Aligned>;
Eigen::TensorMap<Eigen::Tensor<const T, D, MajorType, IndexType>>;
static Type From(Tensor& tensor, DDim dims) {
return Type(tensor.data<T>(), EigenDim<D>::From(dims));
......@@ -60,50 +61,24 @@ struct EigenTensor {
}
};
// Interpret paddle::platform::Tensor as EigenVecotr and EigenConstVector.
template <typename T, typename IndexType = Eigen::DenseIndex>
struct EigenVector {
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); }
template <typename T, int MajorType = Eigen::RowMajor,
typename IndexType = Eigen::DenseIndex>
struct EigenVector : public EigenTensor<T, 1, MajorType, IndexType> {
// 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(
tensor, make_ddim({static_cast<int>(product(tensor.dims_))}));
}
static ConstType From(const Tensor& tensor) {
return EigenTensor<T, 1>::From(tensor);
}
static ConstType Flatten(const Tensor& tensor) {
static typename EigenTensor<T, 1>::ConstType Flatten(const Tensor& tensor) {
return EigenTensor<T, 1>::From(
tensor, make_ddim({static_cast<int>(product(tensor.dims_))}));
}
};
// Interpret paddle::platform::Tensor as EigenMatrix and EigenConstMatrix.
template <typename T, typename IndexType = Eigen::DenseIndex>
struct EigenMatrix {
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);
}
};
template <typename T, int MajorType = Eigen::RowMajor,
typename IndexType = Eigen::DenseIndex>
using EigenMatrix = EigenTensor<T, 2, MajorType, IndexType>;
} // namespace framework
} // namespace paddle
......@@ -37,15 +37,12 @@ class Tensor {
template <bool less, size_t i, typename... args>
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;
template <typename T, typename IndexType>
template <typename T, int MajorType, typename IndexType>
friend struct EigenVector;
template <typename T, typename IndexType>
friend struct EigenMatrix;
public:
Tensor() : offset_(0) {}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册