|
|
|
@ -13,11 +13,10 @@ See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License. */
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
// for windows compile eigen with logging
|
|
|
|
|
// logging.h and windows.h conflict
|
|
|
|
|
#define GLOG_NO_ABBREVIATED_SEVERITIES
|
|
|
|
|
|
|
|
|
|
#include "paddle/fluid/framework/tensor.h"
|
|
|
|
|
#include <math_constants.h>
|
|
|
|
|
#include "unsupported/Eigen/CXX11/Tensor"
|
|
|
|
|
|
|
|
|
|
namespace paddle {
|
|
|
|
@ -49,11 +48,13 @@ struct EigenTensor {
|
|
|
|
|
using ConstType =
|
|
|
|
|
Eigen::TensorMap<Eigen::Tensor<const T, D, MajorType, IndexType>>;
|
|
|
|
|
|
|
|
|
|
static Type From(Tensor& tensor, DDim dims) {
|
|
|
|
|
static Type From(Tensor& tensor, DDim dims) { // NOLINT
|
|
|
|
|
return Type(tensor.data<T>(), EigenDim<D>::From(dims));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Type From(Tensor& tensor) { return From(tensor, tensor.dims_); }
|
|
|
|
|
static Type From(Tensor& tensor) { // NOLINT
|
|
|
|
|
return From(tensor, tensor.dims_);
|
|
|
|
|
} // NOLINT
|
|
|
|
|
|
|
|
|
|
static ConstType From(const Tensor& tensor, DDim dims) {
|
|
|
|
|
return ConstType(tensor.data<T>(), EigenDim<D>::From(dims));
|
|
|
|
@ -67,7 +68,8 @@ struct EigenTensor {
|
|
|
|
|
template <typename T, int MajorType = Eigen::RowMajor,
|
|
|
|
|
typename IndexType = Eigen::DenseIndex>
|
|
|
|
|
struct EigenMatrix : public EigenTensor<T, 2, MajorType, IndexType> {
|
|
|
|
|
static typename EigenMatrix::Type Reshape(Tensor& tensor, int num_col_dims) {
|
|
|
|
|
static typename EigenMatrix::Type Reshape(Tensor& tensor, // NOLINT
|
|
|
|
|
int num_col_dims) {
|
|
|
|
|
int rank = tensor.dims_.size();
|
|
|
|
|
PADDLE_ENFORCE(num_col_dims > 0 && num_col_dims < rank,
|
|
|
|
|
"`num_col_dims` must be between (0, rank_of_tensor).");
|
|
|
|
@ -89,11 +91,12 @@ template <typename T, int MajorType = Eigen::RowMajor,
|
|
|
|
|
typename IndexType = Eigen::DenseIndex>
|
|
|
|
|
struct EigenVector : public EigenTensor<T, 1, MajorType, IndexType> {
|
|
|
|
|
// Flatten reshapes a Tensor into an EigenVector.
|
|
|
|
|
static typename EigenVector::Type Flatten(Tensor& tensor) {
|
|
|
|
|
static typename EigenVector::Type Flatten(Tensor& tensor) { // NOLINT
|
|
|
|
|
return EigenVector::From(tensor, {product(tensor.dims_)});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static typename EigenVector::ConstType Flatten(const Tensor& tensor) {
|
|
|
|
|
static typename EigenVector::ConstType Flatten(
|
|
|
|
|
const Tensor& tensor) { // NOLINT
|
|
|
|
|
return EigenVector::From(tensor, {product(tensor.dims_)});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
@ -107,7 +110,7 @@ struct EigenScalar {
|
|
|
|
|
using ConstType = Eigen::TensorMap<
|
|
|
|
|
Eigen::TensorFixedSize<const T, Eigen::Sizes<>, MajorType, IndexType>>;
|
|
|
|
|
|
|
|
|
|
static Type From(Tensor& tensor) { return Type(tensor.data<T>()); }
|
|
|
|
|
static Type From(Tensor& tensor) { return Type(tensor.data<T>()); } // NOLINT
|
|
|
|
|
|
|
|
|
|
static ConstType From(const Tensor& tensor) {
|
|
|
|
|
return ConstType(tensor.data<T>());
|
|
|
|
|